Fix Microcontroller bug (discovered by Fenris) and some others that propably noone found

This commit is contained in:
Jeija 2012-09-08 15:10:31 +02:00
parent 4ae7bc9aa6
commit f959c16763
3 changed files with 22 additions and 46 deletions

@ -60,14 +60,13 @@ function mesecon:receptor_on(pos, rules)
end end
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
local np = {} local np = {
np.x = pos.x + rule.x x = pos.x + rule.x,
np.y = pos.y + rule.y y = pos.y + rule.y,
np.z = pos.z + rule.z z = pos.z + rule.z}
if mesecon:rules_link(pos, np, rules) then if mesecon:rules_link(pos, np, rules) then
mesecon:turnon(np, pos) mesecon:turnon(np, pos)
end end
i=i+1
end end
end end
@ -76,17 +75,14 @@ function mesecon:receptor_off(pos, rules)
rules = mesecon:get_rules("default") rules = mesecon:get_rules("default")
end end
local connected = false
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
local np = {} local np = {
np.x = pos.x + rule.x x = pos.x + rule.x,
np.y = pos.y + rule.y y = pos.y + rule.y,
np.z = pos.z + rule.z z = pos.z + rule.z}
if mesecon:rules_link(pos, np, rules) and mesecon:connected_to_pw_src(np) == false then if mesecon:rules_link(pos, np, rules) and not mesecon:connected_to_pw_src(np) then
mesecon:turnoff(np, pos) mesecon:turnoff(np, pos)
end end
i=i+1
end end
end end

@ -2,23 +2,19 @@
--Receptors --Receptors
function mesecon:is_receptor_node(nodename) function mesecon:is_receptor_node(nodename)
local i = 1
for i, receptor in ipairs(mesecon.receptors) do for i, receptor in ipairs(mesecon.receptors) do
if receptor.onstate == nodename then if receptor.onstate == nodename then
return true return true
end end
i = i + 1
end end
return false return false
end end
function mesecon:is_receptor_node_off(nodename, pos, ownpos) function mesecon:is_receptor_node_off(nodename, pos, ownpos)
local i = 1
for i, receptor in ipairs(mesecon.receptors) do for i, receptor in ipairs(mesecon.receptors) do
if receptor.offstate == nodename then if receptor.offstate == nodename then
return true return true
end end
i = i + 1
end end
return false return false
end end
@ -87,7 +83,6 @@ end
function mesecon:deactivate(pos) function mesecon:deactivate(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1
for i, action in ipairs(mesecon.actions_off) do for i, action in ipairs(mesecon.actions_off) do
action(pos, node) action(pos, node)
end end
@ -95,7 +90,6 @@ end
function mesecon:changesignal(pos) function mesecon:changesignal(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1
for i, action in ipairs(mesecon.actions_change) do for i, action in ipairs(mesecon.actions_change) do
action(pos, node) action(pos, node)
end end
@ -189,7 +183,6 @@ end
function mesecon:turnon(pos) function mesecon:turnon(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1
if mesecon:is_conductor_off(node.name) then if mesecon:is_conductor_off(node.name) then
local rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
@ -215,7 +208,6 @@ end
function mesecon:turnoff(pos) --receptor rules used because output could have been dug function mesecon:turnoff(pos) --receptor rules used because output could have been dug
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1
local rules local rules
if mesecon:is_conductor_on(node.name) then if mesecon:is_conductor_on(node.name) then
@ -246,7 +238,7 @@ function mesecon:connected_to_pw_src(pos, checked)
local c = 1 local c = 1
if checked == nil then checked = {} end if checked == nil then checked = {} end
while checked[c] ~= nil do --find out if node has already been checked (to prevent from endless loop) while checked[c] ~= nil do --find out if node has already been checked (to prevent from endless loop)
if compare_pos(checked[c], pos) then if compare_pos(checked[c], pos) then
return false, checked return false, checked
end end
c = c + 1 c = c + 1
@ -265,7 +257,6 @@ function mesecon:connected_to_pw_src(pos, checked)
local connected local connected
local rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
local i = 1
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
local np = {} local np = {}
np.x = pos.x + rule.x np.x = pos.x + rule.x
@ -417,7 +408,6 @@ end
--Rules rotation Functions: --Rules rotation Functions:
function mesecon:rotate_rules_right(rules) function mesecon:rotate_rules_right(rules)
local i=1
local nr={}; local nr={};
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
@ -429,7 +419,6 @@ function mesecon:rotate_rules_right(rules)
end end
function mesecon:rotate_rules_left(rules) function mesecon:rotate_rules_left(rules)
local i=1
local nr={}; local nr={};
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
@ -441,7 +430,6 @@ function mesecon:rotate_rules_left(rules)
end end
function mesecon:rotate_rules_down(rules) function mesecon:rotate_rules_down(rules)
local i=1
local nr={}; local nr={};
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
@ -453,7 +441,6 @@ function mesecon:rotate_rules_down(rules)
end end
function mesecon:rotate_rules_up(rules) function mesecon:rotate_rules_up(rules)
local i=1
local nr={}; local nr={};
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}

@ -72,6 +72,7 @@ minetest.register_node(nodename, {
"button_exit[3.5,1;2,3;program;Program]") "button_exit[3.5,1;2,3;program;Program]")
meta:set_string("infotext", "Unprogrammed Microcontroller") meta:set_string("infotext", "Unprogrammed Microcontroller")
meta:set_int("heat", 0) meta:set_int("heat", 0)
meta:set_int("working", 0)
local r = "" local r = ""
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0" for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
meta:set_string("eeprom", r) meta:set_string("eeprom", r)
@ -181,12 +182,15 @@ function yc_code_remove_commentary(code)
end end
function yc_parsecode(code, pos) function yc_parsecode(code, pos)
local meta = minetest.env:get_meta(pos)
if meta:get_int("working") == 1 then return false end
meta:set_int("working", 1)
local endi = 1 local endi = 1
local Lreal = yc_get_real_portstates(pos) local Lreal = yc_get_real_portstates(pos)
local Lvirtual = yc_get_virtual_portstates(pos) local Lvirtual = yc_get_virtual_portstates(pos)
if Lvirtual == nil then return nil end if Lvirtual == nil then return nil end
local c local c
local eeprom = minetest.env:get_meta(pos):get_string("eeprom") local eeprom = meta:get_string("eeprom")
while true do while true do
command, endi = parse_get_command(code, endi) command, endi = parse_get_command(code, endi)
if command == nil then return nil end if command == nil then return nil end
@ -231,6 +235,7 @@ function yc_parsecode(code, pos)
minetest.env:get_meta(pos):set_string("eeprom", eeprom) end minetest.env:get_meta(pos):set_string("eeprom", eeprom) end
end end
yc_action(pos, Lvirtual) yc_action(pos, Lvirtual)
minetest.env:get_meta(pos):set_int("working", 0)
return true return true
end end
@ -569,29 +574,17 @@ end
--Real I/O functions --Real I/O functions
function yc_action(pos, L) --L-->Lvirtual function yc_action(pos, L) --L-->Lvirtual
Lv = yc_get_virtual_portstates(pos) local Lv = yc_get_virtual_portstates(pos)
local meta = minetest.env:get_meta(pos) local metatable = minetest.env:get_meta(pos):to_table()
local code = meta:get_string("code")
local afterid = meta:get_int("afterid")
local heat = meta:get_int("heat")
local eeprom = meta:get_string("eeprom")
local infotext = meta:get_string("infotext")
local formspec = meta:get_string("formspec")
local name = "mesecons_microcontroller:microcontroller" local name = "mesecons_microcontroller:microcontroller"
..tonumber(L.d and 1 or 0) ..tonumber(L.d and 1 or 0)
..tonumber(L.c and 1 or 0) ..tonumber(L.c and 1 or 0)
..tonumber(L.b and 1 or 0) ..tonumber(L.b and 1 or 0)
..tonumber(L.a and 1 or 0) ..tonumber(L.a and 1 or 0)
minetest.env:add_node(pos, {name=name}) minetest.env:add_node(pos, {name=name})
local meta = minetest.env:get_meta(pos) minetest.env:get_meta(pos):from_table(metatable)
meta:set_string("code", code)
meta:set_int("heat", heat)
meta:set_int("afterid", afterid)
meta:set_string("eeprom", eeprom)
meta:set_string("infotext", infotext)
meta:set_string("formspec", formspec)
yc_action_setports(pos, L, Lv, rules) yc_action_setports(pos, L, Lv)
end end
function yc_action_setports(pos, L, Lv) function yc_action_setports(pos, L, Lv)
@ -602,7 +595,7 @@ function yc_action_setports(pos, L, Lv)
if L.a == true then mesecon:receptor_on(pos, rules) if L.a == true then mesecon:receptor_on(pos, rules)
else mesecon:receptor_off(pos, rules) end else mesecon:receptor_off(pos, rules) end
end end
if Lv.b ~= L.b then if Lv.b ~= L.b then
rules = mesecon:get_rules(name.."0010") rules = mesecon:get_rules(name.."0010")
if L.b == true then mesecon:receptor_on(pos, rules) if L.b == true then mesecon:receptor_on(pos, rules)
else mesecon:receptor_off(pos, rules) end else mesecon:receptor_off(pos, rules) end
@ -692,7 +685,7 @@ end
function yc_overheat_off(pos) function yc_overheat_off(pos)
rules = mesecon:get_rules("mesecons_microcontroller:microcontroller1111") rules = mesecon:get_rules("mesecons_microcontroller:microcontroller1111")
mesecon:receptor_off(pos, rules); mesecon:receptor_off(pos, rules)
end end
mesecon:register_on_signal_change(function(pos, node) mesecon:register_on_signal_change(function(pos, node)