working switching station off-delay

This commit is contained in:
Thomas Rudin 2020-01-03 16:04:06 +01:00
parent 8c0481ed5e
commit e003e7ea04
2 changed files with 71 additions and 4 deletions

View File

@ -1,11 +1,78 @@
local has_monitoring_mod = minetest.get_modpath("monitoring")
local switches = {} -- pos_hash -> time_us
local active_switching_stations_metric, switching_stations_usage_metric
if has_monitoring_mod then
active_switching_stations_metric = monitoring.gauge(
"technic_active_switching_stations",
"Number of active switching stations"
)
switching_stations_usage_metric = monitoring.counter(
"technic_switching_stations_usage",
"usage in microseconds cpu time"
)
end
minetest.register_abm({
nodenames = {"technic:switching_station"},
label = "Switching Station",
interval = 1.1,
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
technic.switching_station_run(pos)
action = function(pos)
local hash = minetest.hash_node_position(pos)
local time_us = minetest.get_us_time()
switches[hash] = time_us
end
})
local off_delay_seconds = 600
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < 1.0 then
return
end
timer = 0
local now = minetest.get_us_time()
local off_delay_micros = off_delay_seconds*1000*1000
local active_switches = 0
for hash, time_us in pairs(switches) do
local pos = minetest.get_position_from_hash(hash)
local diff = now - time_us
minetest.get_voxel_manip(pos, pos)
local node = minetest.get_node(pos)
if node.name ~= "technic:switching_station" then
-- station vanished
switches[hash] = nil
elseif diff < off_delay_micros then
-- station active
active_switches = active_switches + 1
technic.switching_station_run(pos)
else
-- station timed out
switches[hash] = nil
end
end
local time_usage = minetest.get_us_time() - now
if has_monitoring_mod then
active_switching_stations_metric.set(active_switches)
switching_stations_usage_metric.inc(time_usage)
end
end)

View File

@ -1,3 +1,3 @@
name = technic
depends = default, pipeworks, technic_worldgen, basic_materials
optional_depends = bucket, screwdriver, mesecons, mesecons_mvps, digilines, digiline_remote, intllib, unified_inventory, vector_extras, dye
optional_depends = bucket, screwdriver, mesecons, mesecons_mvps, digilines, digiline_remote, intllib, unified_inventory, vector_extras, dye, monitoring