Skip to content

Commit 39a0e56

Browse files
author
Jeija
committedMar 19, 2014
Improve and clean up luacontroller digiline_send on globalstep feature
1 parent b50721c commit 39a0e56

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed
 

‎mesecons_luacontroller/init.lua

+17-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- ports = get_real_portstates(pos): gets if inputs are powered from outside
33
-- newport = merge_portstates(state1, state2): just does result = state1 or state2 for every port
44
-- action_setports(pos, rule, state): activates/deactivates the mesecons according to the portstates (helper for action)
5-
-- action(pos, ports, digiline_msgs): Applies new portstates to a luacontroller at pos, and sends pending digiline messages
5+
-- action(pos, ports): Applies new portstates to a luacontroller at pos
66
-- lc_update(pos): updates the controller at pos by executing the code
77
-- reset_meta (pos, code, errmsg): performs a software-reset, installs new code and prints error messages
88
-- reset (pos): performs a hardware reset, turns off all ports
@@ -99,7 +99,7 @@ local setport = function (pos, rule, state)
9999
end
100100
end
101101

102-
local action = function (pos, ports, digiline_msgs)
102+
local action = function (pos, ports)
103103
local node = minetest.get_node(pos)
104104
local name = node.name
105105
local vports = minetest.registered_nodes[name].virtual_portstates
@@ -116,13 +116,6 @@ local action = function (pos, ports, digiline_msgs)
116116
if ports.c ~= vports.c then setport(pos, rules.c, ports.c) end
117117
if ports.d ~= vports.d then setport(pos, rules.d, ports.d) end
118118
end
119-
120-
if digiline then
121-
for i = 1, #digiline_msgs do
122-
digiline:receptor_send(pos, digiline.rules.default,
123-
digiline_msgs[i].channel, digiline_msgs[i].msg)
124-
end
125-
end
126119
end
127120

128121
--------------------
@@ -241,6 +234,16 @@ local gettimer = function(pos)
241234
return timer
242235
end
243236

237+
local getdigiline_send = function(pos)
238+
if not digiline then return end
239+
-- Send messages on next serverstep
240+
return function(channel, msg)
241+
minetest.after(0, function()
242+
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
243+
end)
244+
end
245+
end
246+
244247
local create_environment = function(pos, mem, event)
245248
-- Gather variables for the environment
246249
local vports = minetest.registered_nodes[minetest.get_node(pos).name].virtual_portstates
@@ -253,10 +256,7 @@ local create_environment = function(pos, mem, event)
253256
port = vports,
254257
interrupt = getinterrupt(pos),
255258
timer = gettimer(pos),
256-
digiline_msgs = {},
257-
digiline_send = function(channel, msg)
258-
table.insert(lc_digiline_msgs, {["channel"]=channel, ["msg"]=msg})
259-
end,
259+
digiline_send = getdigiline_send(pos),
260260
mem = mem,
261261
tostring = tostring,
262262
tonumber = tonumber,
@@ -373,7 +373,6 @@ end
373373
-- Parsing function --
374374
----------------------
375375

376-
lc_digiline_msgs = nil
377376
lc_update = function (pos, event)
378377
local meta = minetest.get_meta(pos)
379378
if not interrupt_allow(meta, event) then return end
@@ -385,11 +384,10 @@ lc_update = function (pos, event)
385384

386385
-- make sure code is ok and create environment
387386
local prohibited = code_prohibited(code)
388-
if prohibited then return prohibited end
387+
if prohibited then return prohibited end
389388
local env = create_environment(pos, mem, event)
390389

391390
-- create the sandbox and execute code
392-
lc_digiline_msgs = {}
393391
local chunk, msg = create_sandbox (code, env)
394392
if not chunk then return msg end
395393
local success, msg = pcall(f)
@@ -398,8 +396,8 @@ lc_update = function (pos, event)
398396

399397
save_memory(meta, mem)
400398

401-
-- Actually set the ports and send digiline messages
402-
minetest.after(0, action, pos, env.port, lc_digiline_msgs)
399+
-- Actually set the ports
400+
minetest.after(0, action, pos, env.port)
403401
end
404402

405403
local reset_meta = function(pos, code, errmsg)
@@ -418,7 +416,7 @@ end
418416

419417
local reset = function (pos)
420418
minetest.get_meta(pos):set_string("lc_interrupts", "")
421-
action(pos, {a=false, b=false, c=false, d=false}, {})
419+
action(pos, {a=false, b=false, c=false, d=false})
422420
end
423421

424422
-- ______

0 commit comments

Comments
 (0)
Please sign in to comment.