Improve overheating (#334)

New overheating system that doesn’t use the meta.
This commit is contained in:
numberZero 2017-04-14 23:14:17 +04:00 committed by GitHub
parent 2554164674
commit 54b9eaffa2
4 changed files with 55 additions and 20 deletions

@ -66,30 +66,63 @@ minetest.register_on_placenode(mesecon.on_placenode)
minetest.register_on_dignode(mesecon.on_dignode) minetest.register_on_dignode(mesecon.on_dignode)
-- Overheating service for fast circuits -- Overheating service for fast circuits
local OVERHEAT_MAX = mesecon.setting("overheat_max", 20)
local COOLDOWN_TIME = mesecon.setting("cooldown_time", 2.0)
local COOLDOWN_STEP = mesecon.setting("cooldown_granularity", 0.5)
local COOLDOWN_MULTIPLIER = OVERHEAT_MAX / COOLDOWN_TIME
local cooldown_timer = 0.0
local object_heat = {}
-- returns true if heat is too high -- returns true if heat is too high
mesecon.do_overheat = function(pos) function mesecon.do_overheat(pos)
local meta = minetest.get_meta(pos) local id = minetest.hash_node_position(pos)
local heat = meta:get_int("heat") or 0 local heat = (object_heat[id] or 0) + 1
object_heat[id] = heat
heat = heat + 1 if heat >= OVERHEAT_MAX then
meta:set_int("heat", heat) minetest.log("action", "Node overheats at " .. minetest.pos_to_string(pos))
object_heat[id] = nil
if heat < mesecon.setting("overheat_max", 20) then
mesecon.queue:add_action(pos, "cooldown", {}, 1, nil, 0)
else
return true return true
end end
return false return false
end end
function mesecon.do_cooldown(pos)
mesecon.queue:add_function("cooldown", function (pos) local id = minetest.hash_node_position(pos)
local meta = minetest.get_meta(pos) object_heat[id] = nil
local heat = meta:get_int("heat")
if (heat > 0) then
meta:set_int("heat", heat - 1)
end end
end)
function mesecon.get_heat(pos)
local id = minetest.hash_node_position(pos)
return object_heat[id] or 0
end
function mesecon.move_hot_nodes(moved_nodes)
local new_heat = {}
for _, n in ipairs(moved_nodes) do
local old_id = minetest.hash_node_position(n.oldpos)
local new_id = minetest.hash_node_position(n.pos)
new_heat[new_id] = object_heat[old_id]
object_heat[old_id] = nil
end
for id, heat in pairs(new_heat) do
object_heat[id] = heat
end
end
local function global_cooldown(dtime)
cooldown_timer = cooldown_timer + dtime
if cooldown_timer < COOLDOWN_STEP then
return -- don't overload the CPU
end
local cooldown = COOLDOWN_MULTIPLIER * cooldown_timer
cooldown_timer = 0
for id, heat in pairs(object_heat) do
heat = heat - cooldown
if heat <= 0 then
object_heat[id] = nil -- free some RAM
else
object_heat[id] = heat
end
end
end
minetest.register_globalstep(global_cooldown)

@ -74,7 +74,8 @@ local function register_gate(name, inputnumber, assess, recipe, description)
assess = assess, assess = assess,
onstate = basename.."_on", onstate = basename.."_on",
offstate = basename.."_off", offstate = basename.."_off",
inputnumber = inputnumber inputnumber = inputnumber,
after_dig_node = mesecon.do_cooldown,
},{ },{
tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^".. tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
"jeija_gate_"..name..".png"}, "jeija_gate_"..name..".png"},

@ -315,7 +315,7 @@ local function create_environment(pos, mem, event)
port = vports_copy, port = vports_copy,
event = event, event = event,
mem = mem, mem = mem,
heat = minetest.get_meta(pos):get_int("heat"), heat = mesecon.get_heat(pos),
heat_max = mesecon.setting("overheat_max", 20), heat_max = mesecon.setting("overheat_max", 20),
print = safe_print, print = safe_print,
interrupt = get_interrupt(pos), interrupt = get_interrupt(pos),
@ -485,7 +485,6 @@ local function reset_meta(pos, code, errmsg)
"image_button[3.75,6;2.5,1;jeija_luac_runbutton.png;program;]".. "image_button[3.75,6;2.5,1;jeija_luac_runbutton.png;program;]"..
"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]".. "image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
"label[0.1,5;"..errmsg.."]") "label[0.1,5;"..errmsg.."]")
meta:set_int("heat", 0)
meta:set_int("luac_id", math.random(1, 65535)) meta:set_int("luac_id", math.random(1, 65535))
end end
@ -626,6 +625,7 @@ for d = 0, 1 do
d = d == 1, d = d == 1,
}, },
after_dig_node = function (pos, node) after_dig_node = function (pos, node)
mesecon.do_cooldown(pos)
mesecon.receptor_off(pos, output_rules) mesecon.receptor_off(pos, output_rules)
end, end,
is_luacontroller = true, is_luacontroller = true,

@ -246,3 +246,4 @@ mesecon.register_mvps_stopper("doors:door_steel_t_1")
mesecon.register_mvps_stopper("doors:door_steel_b_2") mesecon.register_mvps_stopper("doors:door_steel_b_2")
mesecon.register_mvps_stopper("doors:door_steel_t_2") mesecon.register_mvps_stopper("doors:door_steel_t_2")
mesecon.register_mvps_stopper("default:chest_locked") mesecon.register_mvps_stopper("default:chest_locked")
mesecon.register_on_mvps_move(mesecon.move_hot_nodes)