Improve and clean up luacontroller digiline_send on globalstep feature

This commit is contained in:
Jeija 2014-03-19 09:09:39 +01:00
parent b50721c701
commit 39a0e56c18

@ -2,7 +2,7 @@
-- ports = get_real_portstates(pos): gets if inputs are powered from outside -- ports = get_real_portstates(pos): gets if inputs are powered from outside
-- newport = merge_portstates(state1, state2): just does result = state1 or state2 for every port -- newport = merge_portstates(state1, state2): just does result = state1 or state2 for every port
-- action_setports(pos, rule, state): activates/deactivates the mesecons according to the portstates (helper for action) -- action_setports(pos, rule, state): activates/deactivates the mesecons according to the portstates (helper for action)
-- action(pos, ports, digiline_msgs): Applies new portstates to a luacontroller at pos, and sends pending digiline messages -- action(pos, ports): Applies new portstates to a luacontroller at pos
-- lc_update(pos): updates the controller at pos by executing the code -- lc_update(pos): updates the controller at pos by executing the code
-- reset_meta (pos, code, errmsg): performs a software-reset, installs new code and prints error messages -- reset_meta (pos, code, errmsg): performs a software-reset, installs new code and prints error messages
-- reset (pos): performs a hardware reset, turns off all ports -- reset (pos): performs a hardware reset, turns off all ports
@ -99,7 +99,7 @@ local setport = function (pos, rule, state)
end end
end end
local action = function (pos, ports, digiline_msgs) local action = function (pos, ports)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local name = node.name local name = node.name
local vports = minetest.registered_nodes[name].virtual_portstates local vports = minetest.registered_nodes[name].virtual_portstates
@ -116,13 +116,6 @@ local action = function (pos, ports, digiline_msgs)
if ports.c ~= vports.c then setport(pos, rules.c, ports.c) end if ports.c ~= vports.c then setport(pos, rules.c, ports.c) end
if ports.d ~= vports.d then setport(pos, rules.d, ports.d) end if ports.d ~= vports.d then setport(pos, rules.d, ports.d) end
end end
if digiline then
for i = 1, #digiline_msgs do
digiline:receptor_send(pos, digiline.rules.default,
digiline_msgs[i].channel, digiline_msgs[i].msg)
end
end
end end
-------------------- --------------------
@ -241,6 +234,16 @@ local gettimer = function(pos)
return timer return timer
end end
local getdigiline_send = function(pos)
if not digiline then return end
-- Send messages on next serverstep
return function(channel, msg)
minetest.after(0, function()
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
end)
end
end
local create_environment = function(pos, mem, event) local create_environment = function(pos, mem, event)
-- Gather variables for the environment -- Gather variables for the environment
local vports = minetest.registered_nodes[minetest.get_node(pos).name].virtual_portstates local vports = minetest.registered_nodes[minetest.get_node(pos).name].virtual_portstates
@ -253,10 +256,7 @@ local create_environment = function(pos, mem, event)
port = vports, port = vports,
interrupt = getinterrupt(pos), interrupt = getinterrupt(pos),
timer = gettimer(pos), timer = gettimer(pos),
digiline_msgs = {}, digiline_send = getdigiline_send(pos),
digiline_send = function(channel, msg)
table.insert(lc_digiline_msgs, {["channel"]=channel, ["msg"]=msg})
end,
mem = mem, mem = mem,
tostring = tostring, tostring = tostring,
tonumber = tonumber, tonumber = tonumber,
@ -373,7 +373,6 @@ end
-- Parsing function -- -- Parsing function --
---------------------- ----------------------
lc_digiline_msgs = nil
lc_update = function (pos, event) lc_update = function (pos, event)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if not interrupt_allow(meta, event) then return end if not interrupt_allow(meta, event) then return end
@ -385,11 +384,10 @@ lc_update = function (pos, event)
-- make sure code is ok and create environment -- make sure code is ok and create environment
local prohibited = code_prohibited(code) local prohibited = code_prohibited(code)
if prohibited then return prohibited end if prohibited then return prohibited end
local env = create_environment(pos, mem, event) local env = create_environment(pos, mem, event)
-- create the sandbox and execute code -- create the sandbox and execute code
lc_digiline_msgs = {}
local chunk, msg = create_sandbox (code, env) local chunk, msg = create_sandbox (code, env)
if not chunk then return msg end if not chunk then return msg end
local success, msg = pcall(f) local success, msg = pcall(f)
@ -398,8 +396,8 @@ lc_update = function (pos, event)
save_memory(meta, mem) save_memory(meta, mem)
-- Actually set the ports and send digiline messages -- Actually set the ports
minetest.after(0, action, pos, env.port, lc_digiline_msgs) minetest.after(0, action, pos, env.port)
end end
local reset_meta = function(pos, code, errmsg) local reset_meta = function(pos, code, errmsg)
@ -418,7 +416,7 @@ end
local reset = function (pos) local reset = function (pos)
minetest.get_meta(pos):set_string("lc_interrupts", "") minetest.get_meta(pos):set_string("lc_interrupts", "")
action(pos, {a=false, b=false, c=false, d=false}, {}) action(pos, {a=false, b=false, c=false, d=false})
end end
-- ______ -- ______