Skip to content

Commit

Permalink
Improve and clean up luacontroller digiline_send on globalstep feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeija committed Mar 19, 2014
1 parent b50721c commit dac05e4
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions mesecons_luacontroller/init.lua
Expand Up @@ -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.d ~= vports.d then setport(pos, rules.d, ports.d) 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

--------------------
Expand Down Expand Up @@ -241,6 +234,16 @@ local gettimer = function(pos)
return timer
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)
-- Gather variables for the environment
local vports = minetest.registered_nodes[minetest.get_node(pos).name].virtual_portstates
Expand All @@ -253,10 +256,7 @@ local create_environment = function(pos, mem, event)
port = vports,
interrupt = getinterrupt(pos),
timer = gettimer(pos),
digiline_msgs = {},
digiline_send = function(channel, msg)
table.insert(lc_digiline_msgs, {["channel"]=channel, ["msg"]=msg})
end,
digiline_send = getdigiline_send(pos),
mem = mem,
tostring = tostring,
tonumber = tonumber,
Expand Down Expand Up @@ -373,7 +373,6 @@ end
-- Parsing function --
----------------------

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

-- make sure code is ok and create environment
local prohibited = code_prohibited(code)
if prohibited then return prohibited end
if prohibited then return prohibited end
local env = create_environment(pos, mem, event)

-- create the sandbox and execute code
lc_digiline_msgs = {}
local chunk, msg = create_sandbox (code, env)
if not chunk then return msg end
local success, msg = pcall(f)
Expand All @@ -399,7 +397,7 @@ lc_update = function (pos, event)
save_memory(meta, mem)

-- Actually set the ports and send digiline messages
minetest.after(0, action, pos, env.port, lc_digiline_msgs)
minetest.after(0, action, pos, env.port)
end

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

local reset = function (pos)
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

-- ______
Expand Down

0 comments on commit dac05e4

Please sign in to comment.