Change mesecons signals so that they update effectors only after a globalstep, configurable to be on/off

This commit is contained in:
Novatux 2013-06-05 06:40:53 +02:00 committed by Anthony Zhang
parent 9a9df6cabf
commit afad592783
5 changed files with 150 additions and 14 deletions

View File

@ -49,6 +49,31 @@ mesecon.receptors={} -- saves all information about receptors | DEPRECATED
mesecon.effectors={} -- saves all information about effectors | DEPRECATED mesecon.effectors={} -- saves all information about effectors | DEPRECATED
mesecon.conductors={} -- saves all information about conductors | DEPRECATED mesecon.conductors={} -- saves all information about conductors | DEPRECATED
local wpath = minetest.get_worldpath()
local function read_file(fn)
local f = io.open(fn, "r")
if f==nil then return {} end
local t = f:read("*all")
f:close()
if t=="" or t==nil then return {} end
return minetest.deserialize(t)
end
local function write_file(fn, tbl)
local f = io.open(fn, "w")
f:write(minetest.serialize(tbl))
f:close()
end
mesecon.to_update = read_file(wpath.."/mesecon_to_update")
mesecon.r_to_update = read_file(wpath.."/mesecon_r_to_update")
minetest.register_on_shutdown(function()
write_file(wpath.."/mesecon_to_update",mesecon.to_update)
write_file(wpath.."/mesecon_r_to_update",mesecon.r_to_update)
end)
-- Settings -- Settings
dofile(minetest.get_modpath("mesecons").."/settings.lua") dofile(minetest.get_modpath("mesecons").."/settings.lua")
@ -76,7 +101,7 @@ dofile(minetest.get_modpath("mesecons").."/legacy.lua");
-- API -- API
-- these are the only functions you need to remember -- these are the only functions you need to remember
function mesecon:receptor_on(pos, rules) function mesecon:receptor_on_i(pos, rules)
rules = rules or mesecon.rules.default rules = rules or mesecon.rules.default
for _, rule in ipairs(rules) do for _, rule in ipairs(rules) do
@ -88,7 +113,16 @@ function mesecon:receptor_on(pos, rules)
end end
end end
function mesecon:receptor_off(pos, rules) function mesecon:receptor_on(pos, rules)
if MESECONS_GLOBALSTEP then
rules = rules or mesecon.rules.default
mesecon.r_to_update[#mesecon.r_to_update+1]={pos=pos, rules=rules, action="on"}
else
mesecon:receptor_on_i(pos, rules)
end
end
function mesecon:receptor_off_i(pos, rules)
rules = rules or mesecon.rules.default rules = rules or mesecon.rules.default
for _, rule in ipairs(rules) do for _, rule in ipairs(rules) do
@ -104,6 +138,15 @@ function mesecon:receptor_off(pos, rules)
end end
end end
function mesecon:receptor_off(pos, rules)
if MESECONS_GLOBALSTEP then
rules = rules or mesecon.rules.default
mesecon.r_to_update[#mesecon.r_to_update+1]={pos=pos, rules=rules, action="off"}
else
mesecon:receptor_off_i(pos, rules)
end
end
print("[OK] Mesecons") print("[OK] Mesecons")

View File

@ -180,25 +180,116 @@ end
--Signals --Signals
function mesecon:activate(pos, node, rulename) function mesecon:activate(pos, node, rulename)
if MESECONS_GLOBALSTEP then
if rulename == nil then
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
mesecon:activate(pos, node, rule)
end
return
end
add_action(pos, "on", rulename)
else
local effector = mesecon:get_effector(node.name) local effector = mesecon:get_effector(node.name)
if effector and effector.action_on then if effector and effector.action_on then
effector.action_on (pos, node, rulename) effector.action_on (pos, node, rulename)
end end
end end
end
function mesecon:deactivate(pos, node, rulename) function mesecon:deactivate(pos, node, rulename)
if MESECONS_GLOBALSTEP then
if rulename == nil then
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
mesecon:deactivate(pos, node, rule)
end
return
end
add_action(pos, "off", rulename)
else
local effector = mesecon:get_effector(node.name) local effector = mesecon:get_effector(node.name)
if effector and effector.action_off then if effector and effector.action_off then
effector.action_off (pos, node, rulename) effector.action_off (pos, node, rulename)
end end
end end
end
function mesecon:changesignal(pos, node, rulename, newstate) function mesecon:changesignal(pos, node, rulename, newstate)
newstate = newstate or "on"
--rulename = rulename or mesecon.rules.default
if MESECONS_GLOBALSTEP then
if rulename == nil then
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
mesecon:changesignal(pos, node, rule, newstate)
end
return
end
add_action(pos, "c"..newstate, rulename)
else
local effector = mesecon:get_effector(node.name) local effector = mesecon:get_effector(node.name)
if effector and effector.action_change then if effector and effector.action_change then
effector.action_change (pos, node, rulename, newstate) effector.action_change (pos, node, rulename, newstate)
end end
end end
end
function execute_actions(dtime)
local nactions = mesecon.to_update
mesecon.to_update = {}
for _,i in ipairs(nactions) do
node = minetest.env:get_node(i.pos)
if node.name=="ignore" then
add_action(i.pos, i.action, i.rname)
else
effector = mesecon:get_effector(node.name)
if i.action == "on" then
if effector and effector.action_on then
effector.action_on(i.pos, node, i.rname)
end
elseif i.action == "off" then
if effector and effector.action_off then
effector.action_off(i.pos, node, i.rname)
end
elseif i.action == "con" then
if effector and effector.action_change then
effector.action_change(i.pos, node, i.rname, "on")
end
elseif i.action == "coff" then
if effector and effector.action_change then
effector.action_change(i.pos, node, i.rname, "off")
end
end
end
end
local nactions = mesecon.r_to_update
mesecon.r_to_update = {}
for _,i in ipairs(nactions) do
if i.action == "on" then
mesecon:receptor_on_i(i.pos, i.rules)
else
mesecon:receptor_off_i(i.pos,i.rules)
end
end
end
minetest.register_globalstep(execute_actions)
function add_action(pos, action, rname)
for _,i in ipairs(mesecon.to_update) do
if i.pos.x == pos.x and i.pos.y == pos.y and i.pos.z == pos.z and i.rname.x == rname.x and i.rname.y == rname.y and i.rname.z == rname.z then
if (i.action == "on" and action == "on") or (i.action == "off" and action == "off") then
--nothing
elseif i.action == "coff" and action == "on" then i.action = "on"
elseif i.action == "con" and action == "off" then i.action = "off"
else
if action == "on" or action == "con" then i.action = "con" end
if action == "off" or action == "coff" then i.action = "coff" end
end
break
end
end
mesecon.to_update[#mesecon.to_update+1] = {pos = pos, action = action, rname = rname}
end
--Rules --Rules

View File

@ -1,5 +1,4 @@
mesecon.on_placenode = function (pos) mesecon.on_placenode = function (pos, node)
local node = minetest.env:get_node(pos)
if mesecon:is_receptor_on(node.name) then if mesecon:is_receptor_on(node.name) then
mesecon:receptor_on(pos, mesecon:receptor_get_rules(node)) mesecon:receptor_on(pos, mesecon:receptor_get_rules(node))
elseif mesecon:is_powered(pos) then elseif mesecon:is_powered(pos) then
@ -7,7 +6,7 @@ mesecon.on_placenode = function (pos)
mesecon:turnon (pos) mesecon:turnon (pos)
mesecon:receptor_on (pos, mesecon:conductor_get_rules(node)) mesecon:receptor_on (pos, mesecon:conductor_get_rules(node))
else else
mesecon:changesignal(pos, node) mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on")
mesecon:activate(pos, node) mesecon:activate(pos, node)
end end
elseif mesecon:is_conductor_on(node.name) then elseif mesecon:is_conductor_on(node.name) then

View File

@ -5,3 +5,5 @@ PRESSURE_PLATE_INTERVAL = 0.1
OBJECT_DETECTOR_RADIUS = 6 OBJECT_DETECTOR_RADIUS = 6
PISTON_MAXIMUM_PUSH = 15 PISTON_MAXIMUM_PUSH = 15
MOVESTONE_MAXIMUM_PUSH = 100 MOVESTONE_MAXIMUM_PUSH = 100
MESECONS_GLOBALSTEP = true -- true = receptors/effectors won't be updated
-- until next globalstep, decreases server load

View File

@ -682,6 +682,7 @@ end
--end --end
function yc_overheat(meta) function yc_overheat(meta)
if MESECONS_GLOBALSTEP then return false end
h = meta:get_int("heat") h = meta:get_int("heat")
if h == nil then return true end -- if nil the overheat if h == nil then return true end -- if nil the overheat
if h>60 then if h>60 then