From e003e7ea043b00c9dd0e09d0f0f2632e65a1c62c Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Fri, 3 Jan 2020 16:04:06 +0100 Subject: [PATCH] working switching station off-delay --- .../machines/switching_station_globalstep.lua | 73 ++++++++++++++++++- technic/mod.conf | 2 +- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/technic/machines/switching_station_globalstep.lua b/technic/machines/switching_station_globalstep.lua index 426ce02..e6a097a 100644 --- a/technic/machines/switching_station_globalstep.lua +++ b/technic/machines/switching_station_globalstep.lua @@ -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) diff --git a/technic/mod.conf b/technic/mod.conf index f2cefaa..986b2e7 100644 --- a/technic/mod.conf +++ b/technic/mod.conf @@ -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