Skip to content

Commit

Permalink
Don't trigger an "off" event to itself when luacontroller turns a por…
Browse files Browse the repository at this point in the history
…t off

I hope this doesn't break anyone's setup.
  • Loading branch information
Jeija committed Nov 29, 2014
1 parent 2a51e40 commit d2373eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mesecons/internal.lua
Expand Up @@ -598,7 +598,7 @@ function mesecon.is_powered(pos, rule)
local nn = minetest.get_node(np)
if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname))
or mesecon.is_receptor_on (nn.name)) then
sourcepos.insert(np)
table.insert(sourcepos, np)
end
end
end
Expand Down
33 changes: 32 additions & 1 deletion mesecons_luacontroller/init.lua
Expand Up @@ -133,6 +133,23 @@ local function set_port_states(pos, ports)
local new_name = generate_name(ports)

if name ~= new_name and vports then
-- Problem:
-- We need to place the new node first so that when turning
-- off some port, it won't stay on because the rules indicate
-- there is an onstate output port there.
-- When turning the output off then, it will however cause feedback
-- so that the luacontroller will receive an "off" event by turning
-- its output off.
-- Solution / Workaround:
-- Remember which output was turned off and ignore next "off" event.
local meta = minetest.get_meta(pos)
local ign = minetest.deserialize(meta:get_string("ignore_offevents")) or {}
if ports.a and not vports.a and not mesecon.is_powered(pos, rules.a) then ign.A = true end
if ports.b and not vports.b and not mesecon.is_powered(pos, rules.b) then ign.B = true end
if ports.c and not vports.c and not mesecon.is_powered(pos, rules.c) then ign.C = true end
if ports.d and not vports.d and not mesecon.is_powered(pos, rules.d) then ign.D = true end
meta:set_string("ignore_offevents", minetest.serialize(ign))

minetest.swap_node(pos, {name = new_name, param2 = node.param2})

if ports.a ~= vports.a then set_port(pos, rules.a, ports.a) end
Expand Down Expand Up @@ -163,6 +180,19 @@ local function overheat(pos, meta)
end
end

------------------------
-- Ignored off events --
------------------------

local function ignore_event(event, meta)
if event.type ~= "off" then return false end
local ignore_offevents = minetest.deserialize(meta:get_string("ignore_offevents")) or {}
if ignore_offevents[event.pin.name] then
ignore_offevents[event.pin.name] = nil
meta:set_string("ignore_offevents", minetest.serialize(ignore_offevents))
return true
end
end

-------------------------
-- Parsing and running --
Expand Down Expand Up @@ -361,6 +391,7 @@ end
local function run(pos, event)
local meta = minetest.get_meta(pos)
if overheat(pos) then return end
if ignore_event(event, meta) then return end

-- Load code & mem from meta
local mem = load_memory(meta)
Expand Down Expand Up @@ -499,7 +530,7 @@ for d = 0, 1 do
rules = input_rules[cid],
action_change = function (pos, _, rule_name, new_state)
update_real_port_states(pos, rule_name, new_state)
run(pos, {type=new_state, pin=rule_name})
run(pos, {type=new_state, pin=rule_name})
end,
},
receptor = {
Expand Down

0 comments on commit d2373eb

Please sign in to comment.