Skip to content

Commit

Permalink
Change mesecons signals so that they update effectors only after a gl…
Browse files Browse the repository at this point in the history
…obalstep, configurable to be on/off
  • Loading branch information
Ekdohibs authored and Uberi committed Jun 5, 2013
1 parent 9a9df6c commit afad592
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 14 deletions.
47 changes: 45 additions & 2 deletions mesecons/init.lua
Expand Up @@ -49,6 +49,31 @@ mesecon.receptors={} -- saves all information about receptors | DEPRECATED
mesecon.effectors={} -- saves all information about effectors | 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
dofile(minetest.get_modpath("mesecons").."/settings.lua")

Expand Down Expand Up @@ -76,7 +101,7 @@ dofile(minetest.get_modpath("mesecons").."/legacy.lua");
-- API
-- 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

for _, rule in ipairs(rules) do
Expand All @@ -88,7 +113,16 @@ function mesecon:receptor_on(pos, rules)
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

for _, rule in ipairs(rules) do
Expand All @@ -104,6 +138,15 @@ function mesecon:receptor_off(pos, rules)
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")

Expand Down
109 changes: 100 additions & 9 deletions mesecons/internal.lua
Expand Up @@ -180,24 +180,115 @@ end
--Signals

function mesecon:activate(pos, node, rulename)
local effector = mesecon:get_effector(node.name)
if effector and effector.action_on then
effector.action_on (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)
if effector and effector.action_on then
effector.action_on (pos, node, rulename)
end
end
end

function mesecon:deactivate(pos, node, rulename)
local effector = mesecon:get_effector(node.name)
if effector and effector.action_off then
effector.action_off (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)
if effector and effector.action_off then
effector.action_off (pos, node, rulename)
end
end
end

function mesecon:changesignal(pos, node, rulename, newstate)
local effector = mesecon:get_effector(node.name)
if effector and effector.action_change then
effector.action_change (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)
if effector and effector.action_change then
effector.action_change (pos, node, rulename, newstate)
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
Expand Down
5 changes: 2 additions & 3 deletions mesecons/services.lua
@@ -1,13 +1,12 @@
mesecon.on_placenode = function (pos)
local node = minetest.env:get_node(pos)
mesecon.on_placenode = function (pos, node)
if mesecon:is_receptor_on(node.name) then
mesecon:receptor_on(pos, mesecon:receptor_get_rules(node))
elseif mesecon:is_powered(pos) then
if mesecon:is_conductor(node.name) then
mesecon:turnon (pos)
mesecon:receptor_on (pos, mesecon:conductor_get_rules(node))
else
mesecon:changesignal(pos, node)
mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on")
mesecon:activate(pos, node)
end
elseif mesecon:is_conductor_on(node.name) then
Expand Down
2 changes: 2 additions & 0 deletions mesecons/settings.lua
Expand Up @@ -5,3 +5,5 @@ PRESSURE_PLATE_INTERVAL = 0.1
OBJECT_DETECTOR_RADIUS = 6
PISTON_MAXIMUM_PUSH = 15
MOVESTONE_MAXIMUM_PUSH = 100
MESECONS_GLOBALSTEP = true -- true = receptors/effectors won't be updated
-- until next globalstep, decreases server load
1 change: 1 addition & 0 deletions mesecons_microcontroller/init.lua
Expand Up @@ -682,6 +682,7 @@ end
--end

function yc_overheat(meta)
if MESECONS_GLOBALSTEP then return false end
h = meta:get_int("heat")
if h == nil then return true end -- if nil the overheat
if h>60 then
Expand Down

0 comments on commit afad592

Please sign in to comment.