forked from mt-mods/jumpdrive
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapgen.lua
66 lines (51 loc) · 1.23 KB
/
mapgen.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local metric_mapgen_events_count
if minetest.get_modpath("monitoring") then
metric_mapgen_events_count = monitoring.gauge(
"jumpdrive_mapgen_events_count",
"number of events in the mapgen cache"
)
end
local events = {} -- list of {minp, maxp, time}
jumpdrive.mapgen = {}
jumpdrive.mapgen.reset = function()
events = {}
end
-- update last mapgen event time
minetest.register_on_generated(function(minp, maxp)
table.insert(events, {
minp = minp,
maxp = maxp,
time = minetest.get_us_time()
})
end)
-- cleanup
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < 5 then return end
timer=0
local time = minetest.get_us_time()
local delay_seconds = 10
local copied_events = events
events = {}
local count = 0
for _, event in ipairs(copied_events) do
if event.time > (time - (delay_seconds * 1000000)) then
-- still recent
table.insert(events, event)
count = count + 1
end
end
if metric_mapgen_events_count then
metric_mapgen_events_count.set(count)
end
end)
-- true = mapgen recently active in that area
jumpdrive.check_mapgen = function(pos)
for _, event in ipairs(events) do
if vector.distance(pos, event.minp) < 200 then
return true
end
end
return false
end