Skip to content

Commit 54b9eaf

Browse files
authored
Improve overheating (#334)
New overheating system that doesn’t use the meta.
1 parent 2554164 commit 54b9eaf

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

mesecons/services.lua

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,63 @@ minetest.register_on_placenode(mesecon.on_placenode)
6666
minetest.register_on_dignode(mesecon.on_dignode)
6767

6868
-- Overheating service for fast circuits
69+
local OVERHEAT_MAX = mesecon.setting("overheat_max", 20)
70+
local COOLDOWN_TIME = mesecon.setting("cooldown_time", 2.0)
71+
local COOLDOWN_STEP = mesecon.setting("cooldown_granularity", 0.5)
72+
local COOLDOWN_MULTIPLIER = OVERHEAT_MAX / COOLDOWN_TIME
73+
local cooldown_timer = 0.0
74+
local object_heat = {}
6975

7076
-- returns true if heat is too high
71-
mesecon.do_overheat = function(pos)
72-
local meta = minetest.get_meta(pos)
73-
local heat = meta:get_int("heat") or 0
74-
75-
heat = heat + 1
76-
meta:set_int("heat", heat)
77-
78-
if heat < mesecon.setting("overheat_max", 20) then
79-
mesecon.queue:add_action(pos, "cooldown", {}, 1, nil, 0)
80-
else
77+
function mesecon.do_overheat(pos)
78+
local id = minetest.hash_node_position(pos)
79+
local heat = (object_heat[id] or 0) + 1
80+
object_heat[id] = heat
81+
if heat >= OVERHEAT_MAX then
82+
minetest.log("action", "Node overheats at " .. minetest.pos_to_string(pos))
83+
object_heat[id] = nil
8184
return true
8285
end
83-
8486
return false
8587
end
8688

89+
function mesecon.do_cooldown(pos)
90+
local id = minetest.hash_node_position(pos)
91+
object_heat[id] = nil
92+
end
8793

88-
mesecon.queue:add_function("cooldown", function (pos)
89-
local meta = minetest.get_meta(pos)
90-
local heat = meta:get_int("heat")
94+
function mesecon.get_heat(pos)
95+
local id = minetest.hash_node_position(pos)
96+
return object_heat[id] or 0
97+
end
9198

92-
if (heat > 0) then
93-
meta:set_int("heat", heat - 1)
99+
function mesecon.move_hot_nodes(moved_nodes)
100+
local new_heat = {}
101+
for _, n in ipairs(moved_nodes) do
102+
local old_id = minetest.hash_node_position(n.oldpos)
103+
local new_id = minetest.hash_node_position(n.pos)
104+
new_heat[new_id] = object_heat[old_id]
105+
object_heat[old_id] = nil
106+
end
107+
for id, heat in pairs(new_heat) do
108+
object_heat[id] = heat
94109
end
95-
end)
110+
end
111+
112+
local function global_cooldown(dtime)
113+
cooldown_timer = cooldown_timer + dtime
114+
if cooldown_timer < COOLDOWN_STEP then
115+
return -- don't overload the CPU
116+
end
117+
local cooldown = COOLDOWN_MULTIPLIER * cooldown_timer
118+
cooldown_timer = 0
119+
for id, heat in pairs(object_heat) do
120+
heat = heat - cooldown
121+
if heat <= 0 then
122+
object_heat[id] = nil -- free some RAM
123+
else
124+
object_heat[id] = heat
125+
end
126+
end
127+
end
128+
minetest.register_globalstep(global_cooldown)

mesecons_gates/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ local function register_gate(name, inputnumber, assess, recipe, description)
7474
assess = assess,
7575
onstate = basename.."_on",
7676
offstate = basename.."_off",
77-
inputnumber = inputnumber
77+
inputnumber = inputnumber,
78+
after_dig_node = mesecon.do_cooldown,
7879
},{
7980
tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
8081
"jeija_gate_"..name..".png"},

mesecons_luacontroller/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ local function create_environment(pos, mem, event)
315315
port = vports_copy,
316316
event = event,
317317
mem = mem,
318-
heat = minetest.get_meta(pos):get_int("heat"),
318+
heat = mesecon.get_heat(pos),
319319
heat_max = mesecon.setting("overheat_max", 20),
320320
print = safe_print,
321321
interrupt = get_interrupt(pos),
@@ -485,7 +485,6 @@ local function reset_meta(pos, code, errmsg)
485485
"image_button[3.75,6;2.5,1;jeija_luac_runbutton.png;program;]"..
486486
"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
487487
"label[0.1,5;"..errmsg.."]")
488-
meta:set_int("heat", 0)
489488
meta:set_int("luac_id", math.random(1, 65535))
490489
end
491490

@@ -626,6 +625,7 @@ for d = 0, 1 do
626625
d = d == 1,
627626
},
628627
after_dig_node = function (pos, node)
628+
mesecon.do_cooldown(pos)
629629
mesecon.receptor_off(pos, output_rules)
630630
end,
631631
is_luacontroller = true,

mesecons_mvps/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,4 @@ mesecon.register_mvps_stopper("doors:door_steel_t_1")
246246
mesecon.register_mvps_stopper("doors:door_steel_b_2")
247247
mesecon.register_mvps_stopper("doors:door_steel_t_2")
248248
mesecon.register_mvps_stopper("default:chest_locked")
249+
mesecon.register_on_mvps_move(mesecon.move_hot_nodes)

0 commit comments

Comments
 (0)