Skip to content

Commit afad592

Browse files
EkdohibsUberi
authored andcommittedJun 5, 2013
Change mesecons signals so that they update effectors only after a globalstep, configurable to be on/off
1 parent 9a9df6c commit afad592

File tree

5 files changed

+150
-14
lines changed

5 files changed

+150
-14
lines changed
 

‎mesecons/init.lua

+45-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ mesecon.receptors={} -- saves all information about receptors | DEPRECATED
4949
mesecon.effectors={} -- saves all information about effectors | DEPRECATED
5050
mesecon.conductors={} -- saves all information about conductors | DEPRECATED
5151

52+
53+
local wpath = minetest.get_worldpath()
54+
local function read_file(fn)
55+
local f = io.open(fn, "r")
56+
if f==nil then return {} end
57+
local t = f:read("*all")
58+
f:close()
59+
if t=="" or t==nil then return {} end
60+
return minetest.deserialize(t)
61+
end
62+
63+
local function write_file(fn, tbl)
64+
local f = io.open(fn, "w")
65+
f:write(minetest.serialize(tbl))
66+
f:close()
67+
end
68+
69+
mesecon.to_update = read_file(wpath.."/mesecon_to_update")
70+
mesecon.r_to_update = read_file(wpath.."/mesecon_r_to_update")
71+
72+
minetest.register_on_shutdown(function()
73+
write_file(wpath.."/mesecon_to_update",mesecon.to_update)
74+
write_file(wpath.."/mesecon_r_to_update",mesecon.r_to_update)
75+
end)
76+
5277
-- Settings
5378
dofile(minetest.get_modpath("mesecons").."/settings.lua")
5479

@@ -76,7 +101,7 @@ dofile(minetest.get_modpath("mesecons").."/legacy.lua");
76101
-- API
77102
-- these are the only functions you need to remember
78103

79-
function mesecon:receptor_on(pos, rules)
104+
function mesecon:receptor_on_i(pos, rules)
80105
rules = rules or mesecon.rules.default
81106

82107
for _, rule in ipairs(rules) do
@@ -88,7 +113,16 @@ function mesecon:receptor_on(pos, rules)
88113
end
89114
end
90115

91-
function mesecon:receptor_off(pos, rules)
116+
function mesecon:receptor_on(pos, rules)
117+
if MESECONS_GLOBALSTEP then
118+
rules = rules or mesecon.rules.default
119+
mesecon.r_to_update[#mesecon.r_to_update+1]={pos=pos, rules=rules, action="on"}
120+
else
121+
mesecon:receptor_on_i(pos, rules)
122+
end
123+
end
124+
125+
function mesecon:receptor_off_i(pos, rules)
92126
rules = rules or mesecon.rules.default
93127

94128
for _, rule in ipairs(rules) do
@@ -104,6 +138,15 @@ function mesecon:receptor_off(pos, rules)
104138
end
105139
end
106140

141+
function mesecon:receptor_off(pos, rules)
142+
if MESECONS_GLOBALSTEP then
143+
rules = rules or mesecon.rules.default
144+
mesecon.r_to_update[#mesecon.r_to_update+1]={pos=pos, rules=rules, action="off"}
145+
else
146+
mesecon:receptor_off_i(pos, rules)
147+
end
148+
end
149+
107150

108151
print("[OK] Mesecons")
109152

‎mesecons/internal.lua

+100-9
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,115 @@ end
180180
--Signals
181181

182182
function mesecon:activate(pos, node, rulename)
183-
local effector = mesecon:get_effector(node.name)
184-
if effector and effector.action_on then
185-
effector.action_on (pos, node, rulename)
183+
if MESECONS_GLOBALSTEP then
184+
if rulename == nil then
185+
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
186+
mesecon:activate(pos, node, rule)
187+
end
188+
return
189+
end
190+
add_action(pos, "on", rulename)
191+
else
192+
local effector = mesecon:get_effector(node.name)
193+
if effector and effector.action_on then
194+
effector.action_on (pos, node, rulename)
195+
end
186196
end
187197
end
188198

189199
function mesecon:deactivate(pos, node, rulename)
190-
local effector = mesecon:get_effector(node.name)
191-
if effector and effector.action_off then
192-
effector.action_off (pos, node, rulename)
200+
if MESECONS_GLOBALSTEP then
201+
if rulename == nil then
202+
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
203+
mesecon:deactivate(pos, node, rule)
204+
end
205+
return
206+
end
207+
add_action(pos, "off", rulename)
208+
else
209+
local effector = mesecon:get_effector(node.name)
210+
if effector and effector.action_off then
211+
effector.action_off (pos, node, rulename)
212+
end
193213
end
194214
end
195215

196216
function mesecon:changesignal(pos, node, rulename, newstate)
197-
local effector = mesecon:get_effector(node.name)
198-
if effector and effector.action_change then
199-
effector.action_change (pos, node, rulename, newstate)
217+
218+
newstate = newstate or "on"
219+
--rulename = rulename or mesecon.rules.default
220+
if MESECONS_GLOBALSTEP then
221+
if rulename == nil then
222+
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
223+
mesecon:changesignal(pos, node, rule, newstate)
224+
end
225+
return
226+
end
227+
add_action(pos, "c"..newstate, rulename)
228+
else
229+
local effector = mesecon:get_effector(node.name)
230+
if effector and effector.action_change then
231+
effector.action_change (pos, node, rulename, newstate)
232+
end
233+
end
234+
end
235+
236+
function execute_actions(dtime)
237+
local nactions = mesecon.to_update
238+
mesecon.to_update = {}
239+
for _,i in ipairs(nactions) do
240+
node = minetest.env:get_node(i.pos)
241+
if node.name=="ignore" then
242+
add_action(i.pos, i.action, i.rname)
243+
else
244+
effector = mesecon:get_effector(node.name)
245+
if i.action == "on" then
246+
if effector and effector.action_on then
247+
effector.action_on(i.pos, node, i.rname)
248+
end
249+
elseif i.action == "off" then
250+
if effector and effector.action_off then
251+
effector.action_off(i.pos, node, i.rname)
252+
end
253+
elseif i.action == "con" then
254+
if effector and effector.action_change then
255+
effector.action_change(i.pos, node, i.rname, "on")
256+
end
257+
elseif i.action == "coff" then
258+
if effector and effector.action_change then
259+
effector.action_change(i.pos, node, i.rname, "off")
260+
end
261+
end
262+
end
263+
end
264+
local nactions = mesecon.r_to_update
265+
mesecon.r_to_update = {}
266+
for _,i in ipairs(nactions) do
267+
if i.action == "on" then
268+
mesecon:receptor_on_i(i.pos, i.rules)
269+
else
270+
mesecon:receptor_off_i(i.pos,i.rules)
271+
end
272+
end
273+
end
274+
275+
minetest.register_globalstep(execute_actions)
276+
277+
function add_action(pos, action, rname)
278+
for _,i in ipairs(mesecon.to_update) do
279+
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
280+
if (i.action == "on" and action == "on") or (i.action == "off" and action == "off") then
281+
--nothing
282+
elseif i.action == "coff" and action == "on" then i.action = "on"
283+
elseif i.action == "con" and action == "off" then i.action = "off"
284+
else
285+
if action == "on" or action == "con" then i.action = "con" end
286+
if action == "off" or action == "coff" then i.action = "coff" end
287+
end
288+
break
289+
end
200290
end
291+
mesecon.to_update[#mesecon.to_update+1] = {pos = pos, action = action, rname = rname}
201292
end
202293

203294
--Rules

‎mesecons/services.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
mesecon.on_placenode = function (pos)
2-
local node = minetest.env:get_node(pos)
1+
mesecon.on_placenode = function (pos, node)
32
if mesecon:is_receptor_on(node.name) then
43
mesecon:receptor_on(pos, mesecon:receptor_get_rules(node))
54
elseif mesecon:is_powered(pos) then
65
if mesecon:is_conductor(node.name) then
76
mesecon:turnon (pos)
87
mesecon:receptor_on (pos, mesecon:conductor_get_rules(node))
98
else
10-
mesecon:changesignal(pos, node)
9+
mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on")
1110
mesecon:activate(pos, node)
1211
end
1312
elseif mesecon:is_conductor_on(node.name) then

‎mesecons/settings.lua

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ PRESSURE_PLATE_INTERVAL = 0.1
55
OBJECT_DETECTOR_RADIUS = 6
66
PISTON_MAXIMUM_PUSH = 15
77
MOVESTONE_MAXIMUM_PUSH = 100
8+
MESECONS_GLOBALSTEP = true -- true = receptors/effectors won't be updated
9+
-- until next globalstep, decreases server load

‎mesecons_microcontroller/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ end
682682
--end
683683

684684
function yc_overheat(meta)
685+
if MESECONS_GLOBALSTEP then return false end
685686
h = meta:get_int("heat")
686687
if h == nil then return true end -- if nil the overheat
687688
if h>60 then

0 commit comments

Comments
 (0)
Please sign in to comment.