Fix remaining namespace pollutions (closes #311)

This commit is contained in:
sfan5 2017-01-15 23:12:13 +01:00
parent 54daee236e
commit ececf525b6
1 changed files with 65 additions and 63 deletions

View File

@ -1,6 +1,7 @@
EEPROM_SIZE = 255 local EEPROM_SIZE = 255
local microc_rules = {} local microc_rules = {}
local yc = {}
for a = 0, 1 do for a = 0, 1 do
for b = 0, 1 do for b = 0, 1 do
@ -20,6 +21,7 @@ end
if tostring(d) == "1" then if tostring(d) == "1" then
top = top.."^jeija_microcontroller_LED_D.png" top = top.."^jeija_microcontroller_LED_D.png"
end end
local groups
if tostring(d)..tostring(c)..tostring(b)..tostring(a) ~= "0000" then if tostring(d)..tostring(c)..tostring(b)..tostring(a) ~= "0000" then
groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 3, overheat = 1} groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 3, overheat = 1}
else else
@ -42,8 +44,8 @@ local mesecons = {effector =
{ {
rules = input_rules, rules = input_rules,
action_change = function (pos, node, rulename, newstate) action_change = function (pos, node, rulename, newstate)
yc_update_real_portstates(pos, node, rulename, newstate) yc.update_real_portstates(pos, node, rulename, newstate)
update_yc(pos) yc.update(pos)
end end
}} }}
if nodename ~= "mesecons_microcontroller:microcontroller0000" then if nodename ~= "mesecons_microcontroller:microcontroller0000" then
@ -127,8 +129,8 @@ minetest.register_node(nodename, {
"button[7.5,0.2;1.5,3;brsflop;RS-Flop]".. "button[7.5,0.2;1.5,3;brsflop;RS-Flop]"..
"button_exit[3.5,1;2,3;program;Program]") "button_exit[3.5,1;2,3;program;Program]")
meta:set_string("infotext", "Programmed Microcontroller") meta:set_string("infotext", "Programmed Microcontroller")
yc_reset (pos) yc.reset (pos)
update_yc(pos) update(pos)
end, end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
mesecons = mesecons, mesecons = mesecons,
@ -151,8 +153,8 @@ minetest.register_craft({
} }
}) })
function yc_reset(pos) yc.reset = function(pos)
yc_action(pos, {a=false, b=false, c=false, d=false}) yc.action(pos, {a=false, b=false, c=false, d=false})
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_int("afterid", 0) meta:set_int("afterid", 0)
local r = "" local r = ""
@ -160,7 +162,7 @@ function yc_reset(pos)
meta:set_string("eeprom", r) meta:set_string("eeprom", r)
end end
function update_yc(pos) yc.update = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if (mesecon.do_overheat(pos)) then if (mesecon.do_overheat(pos)) then
@ -172,10 +174,10 @@ function update_yc(pos)
end end
local code = meta:get_string("code") local code = meta:get_string("code")
code = yc_code_remove_commentary(code) code = yc.code_remove_commentary(code)
code = string.gsub(code, " ", "") --Remove all spaces code = string.gsub(code, " ", "") --Remove all spaces
code = string.gsub(code, " ", "") --Remove all tabs code = string.gsub(code, " ", "") --Remove all tabs
if yc_parsecode(code, pos) == nil then if yc.parsecode(code, pos) == nil then
meta:set_string("infotext", "Code not valid!\n"..code) meta:set_string("infotext", "Code not valid!\n"..code)
else else
meta:set_string("infotext", "Working Microcontroller\n"..code) meta:set_string("infotext", "Working Microcontroller\n"..code)
@ -184,7 +186,7 @@ end
--Code Parsing --Code Parsing
function yc_code_remove_commentary(code) yc.code_remove_commentary = function(code)
local is_string = false local is_string = false
for i = 1, #code do for i = 1, #code do
if code:sub(i, i) == '"' then if code:sub(i, i) == '"' then
@ -196,50 +198,50 @@ function yc_code_remove_commentary(code)
return code return code
end end
function yc_parsecode(code, pos) yc.parsecode = function(code, pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
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 = meta:get_string("eeprom") local eeprom = meta:get_string("eeprom")
while true do while true do
local command, params local command, params
command, endi = parse_get_command(code, endi) command, endi = yc.parse_get_command(code, endi)
if command == nil then return nil end if command == nil then return nil end
if command == true then break end --end of code if command == true then break end --end of code
if command == "if" then if command == "if" then
local r local r
r, endi = yc_command_if(code, endi, yc_merge_portstates(Lreal, Lvirtual), eeprom) r, endi = yc.command_if(code, endi, yc.merge_portstates(Lreal, Lvirtual), eeprom)
if r == nil then return nil end if r == nil then return nil end
if r == true then -- nothing if r == true then -- nothing
elseif r == false then elseif r == false then
local endi_new = yc_skip_to_else (code, endi) local endi_new = yc.skip_to_else (code, endi)
if endi_new == nil then --else > not found if endi_new == nil then --else > not found
endi = yc_skip_to_endif(code, endi) endi = yc.skip_to_endif(code, endi)
else else
endi = endi_new endi = endi_new
end end
if endi == nil then return nil end if endi == nil then return nil end
end end
else else
params, endi = parse_get_params(code, endi) params, endi = yc.parse_get_params(code, endi)
if not params then return nil end if not params then return nil end
end end
if command == "on" then if command == "on" then
L = yc_command_on (params, Lvirtual) L = yc.command_on (params, Lvirtual)
elseif command == "off" then elseif command == "off" then
L = yc_command_off(params, Lvirtual) L = yc.command_off(params, Lvirtual)
elseif command == "print" then elseif command == "print" then
local su = yc_command_print(params, eeprom, yc_merge_portstates(Lreal, Lvirtual)) local su = yc.command_print(params, eeprom, yc.merge_portstates(Lreal, Lvirtual))
if su ~= true then return nil end if su ~= true then return nil end
elseif command == "after" then elseif command == "after" then
local su = yc_command_after(params, pos) local su = yc.command_after(params, pos)
if su == nil then return nil end if su == nil then return nil end
elseif command == "sbi" then elseif command == "sbi" then
local new_eeprom local new_eeprom
new_eeprom, Lvirtual = yc_command_sbi (params, eeprom, yc_merge_portstates(Lreal, Lvirtual), Lvirtual) new_eeprom, Lvirtual = yc.command_sbi (params, eeprom, yc.merge_portstates(Lreal, Lvirtual), Lvirtual)
if new_eeprom == nil then return nil if new_eeprom == nil then return nil
else eeprom = new_eeprom end else eeprom = new_eeprom end
elseif command == "if" then --nothing elseif command == "if" then --nothing
@ -250,11 +252,11 @@ function yc_parsecode(code, pos)
if eeprom == nil then return nil else if eeprom == nil then return nil else
minetest.get_meta(pos):set_string("eeprom", eeprom) end minetest.get_meta(pos):set_string("eeprom", eeprom) end
end end
yc_action(pos, Lvirtual) yc.action(pos, Lvirtual)
return true return true
end end
function parse_get_command(code, starti) yc.parse_get_command = function(code, starti)
i = starti i = starti
local s local s
while s ~= "" do while s ~= "" do
@ -266,7 +268,7 @@ function parse_get_command(code, starti)
starti = starti + 1 starti = starti + 1
i = starti i = starti
elseif s == ">" then elseif s == ">" then
starti = yc_skip_to_endif(code, starti) starti = yc.skip_to_endif(code, starti)
if starti == nil then return nil end if starti == nil then return nil end
i = starti i = starti
else else
@ -280,7 +282,7 @@ function parse_get_command(code, starti)
return nil, nil return nil, nil
end end
function parse_get_params(code, starti) yc.parse_get_params = function(code, starti)
i = starti i = starti
local s local s
local params = {} local params = {}
@ -303,7 +305,7 @@ function parse_get_params(code, starti)
return nil, nil return nil, nil
end end
function yc_parse_get_eeprom_param(cond, starti) yc.parse_get_eeprom_param = function(cond, starti)
i = starti i = starti
local s local s
local addr local addr
@ -319,7 +321,7 @@ function yc_parse_get_eeprom_param(cond, starti)
return nil, nil return nil, nil
end end
function yc_skip_to_endif(code, starti) yc.skip_to_endif = function(code, starti)
local i = starti local i = starti
local s = false local s = false
local open_ifs = 1 local open_ifs = 1
@ -339,7 +341,7 @@ function yc_skip_to_endif(code, starti)
return nil return nil
end end
function yc_skip_to_else(code, starti) yc.skip_to_else = function(code, starti)
local i = starti local i = starti
local s = false local s = false
local open_ifs = 1 local open_ifs = 1
@ -360,29 +362,29 @@ function yc_skip_to_else(code, starti)
end end
--Commands --Commands
function yc_command_on(params, L) yc.command_on = function(params, L)
local rules = {} local rules = {}
for i, port in ipairs(params) do for i, port in ipairs(params) do
L = yc_set_portstate (port, true, L) L = yc.set_portstate (port, true, L)
end end
return L return L
end end
function yc_command_off(params, L) yc.command_off = function(params, L)
local rules = {} local rules = {}
for i, port in ipairs(params) do for i, port in ipairs(params) do
L = yc_set_portstate (port, false, L) L = yc.set_portstate (port, false, L)
end end
return L return L
end end
function yc_command_print(params, eeprom, L) yc.command_print = function(params, eeprom, L)
local s = "" local s = ""
for i, param in ipairs(params) do for i, param in ipairs(params) do
if param:sub(1,1) == '"' and param:sub(#param, #param) == '"' then if param:sub(1,1) == '"' and param:sub(#param, #param) == '"' then
s = s..param:sub(2, #param-1) s = s..param:sub(2, #param-1)
else else
r = yc_command_parsecondition(param, L, eeprom) r = yc.command_parsecondition(param, L, eeprom)
if r == "1" or r == "0" then if r == "1" or r == "0" then
s = s..r s = s..r
else return nil end else return nil end
@ -392,17 +394,17 @@ function yc_command_print(params, eeprom, L)
return true return true
end end
function yc_command_sbi(params, eeprom, L, Lv) yc.command_sbi = function(params, eeprom, L, Lv)
if params[1]==nil or params[2]==nil or params[3] ~=nil then return nil end if params[1]==nil or params[2]==nil or params[3] ~=nil then return nil end
local status = yc_command_parsecondition(params[2], L, eeprom) local status = yc.command_parsecondition(params[2], L, eeprom)
if status == nil then return nil, nil end if status == nil then return nil, nil end
if string.find("ABCD", params[1])~=nil and #params[1]==1 then --is a port if string.find("ABCD", params[1])~=nil and #params[1]==1 then --is a port
if status == "1" then if status == "1" then
Lv = yc_set_portstate (params[1], true, Lv) Lv = yc.set_portstate (params[1], true, Lv)
else else
Lv = yc_set_portstate (params[1], false, Lv) Lv = yc.set_portstate (params[1], false, Lv)
end end
return eeprom, Lv; return eeprom, Lv;
end end
@ -420,7 +422,7 @@ function yc_command_sbi(params, eeprom, L, Lv)
end end
-- after (delay) -- after (delay)
function yc_command_after(params, pos) yc.command_after = function(params, pos)
if params[1] == nil or params[2] == nil or params[3] ~= nil then return nil end if params[1] == nil or params[2] == nil or params[3] ~= nil then return nil end
--get time (maximum time is 200) --get time (maximum time is 200)
@ -436,14 +438,14 @@ function yc_command_after(params, pos)
local afterid = math.random(10000) local afterid = math.random(10000)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_int("afterid", afterid) meta:set_int("afterid", afterid)
minetest.after(time, yc_command_after_execute, {pos = pos, code = code, afterid = afterid}) minetest.after(time, yc.command_after_execute, {pos = pos, code = code, afterid = afterid})
return true return true
end end
function yc_command_after_execute(params) yc.command_after_execute = function(params)
local meta = minetest.get_meta(params.pos) local meta = minetest.get_meta(params.pos)
if meta:get_int("afterid") == params.afterid then --make sure the node has not been changed if meta:get_int("afterid") == params.afterid then --make sure the node has not been changed
if yc_parsecode(params.code, params.pos) == nil then if yc.parsecode(params.code, params.pos) == nil then
meta:set_string("infotext", "Code in after() not valid!") meta:set_string("infotext", "Code in after() not valid!")
else else
if code ~= nil then if code ~= nil then
@ -456,21 +458,21 @@ function yc_command_after_execute(params)
end end
--If --If
function yc_command_if(code, starti, L, eeprom) yc.command_if = function(code, starti, L, eeprom)
local cond, endi = yc_command_if_getcondition(code, starti) local cond, endi = yc.command_if_getcondition(code, starti)
if cond == nil then return nil end if cond == nil then return nil end
cond = yc_command_parsecondition(cond, L, eeprom) cond = yc.command_parsecondition(cond, L, eeprom)
local result local result
if cond == "0" then result = false if cond == "0" then result = false
elseif cond == "1" then result = true end elseif cond == "1" then result = true end
if not result then end if not result then end
return result, endi --endi from local cond, endi = yc_command_if_getcondition(code, starti) return result, endi --endi from local cond, endi = yc.command_if_getcondition(code, starti)
end end
--Condition parsing --Condition parsing
function yc_command_if_getcondition(code, starti) yc.command_if_getcondition = function(code, starti)
i = starti i = starti
local s local s
local brackets = 1 --1 Bracket to close local brackets = 1 --1 Bracket to close
@ -494,7 +496,7 @@ function yc_command_if_getcondition(code, starti)
return nil, nil return nil, nil
end end
function yc_command_parsecondition(cond, L, eeprom) yc.command_parsecondition = function(cond, L, eeprom)
cond = string.gsub(cond, "A", tonumber(L.a and 1 or 0)) cond = string.gsub(cond, "A", tonumber(L.a and 1 or 0))
cond = string.gsub(cond, "B", tonumber(L.b and 1 or 0)) cond = string.gsub(cond, "B", tonumber(L.b and 1 or 0))
cond = string.gsub(cond, "C", tonumber(L.c and 1 or 0)) cond = string.gsub(cond, "C", tonumber(L.c and 1 or 0))
@ -506,8 +508,8 @@ function yc_command_parsecondition(cond, L, eeprom)
while i<=l do while i<=l do
local s = cond:sub(i,i) local s = cond:sub(i,i)
if s == "#" then if s == "#" then
local addr, endi = yc_parse_get_eeprom_param(cond, i+1) local addr, endi = yc.parse_get_eeprom_param(cond, i+1)
local buf = yc_eeprom_read(tonumber(addr), eeprom) local buf = yc.eeprom_read(tonumber(addr), eeprom)
if buf == nil then return nil end if buf == nil then return nil end
local call = cond:sub(i, endi-1) local call = cond:sub(i, endi-1)
cond = string.gsub(cond, call, buf) cond = string.gsub(cond, call, buf)
@ -580,14 +582,14 @@ function yc_command_parsecondition(cond, L, eeprom)
end end
--Virtual-Hardware functions --Virtual-Hardware functions
function yc_eeprom_read(number, eeprom) yc.eeprom_read = function(number, eeprom)
if not number then return end if not number then return end
return eeprom:sub(number, number) return eeprom:sub(number, number)
end end
--Real I/O functions --Real I/O functions
function yc_action(pos, L) --L-->Lvirtual yc.action = function(pos, L) --L-->Lvirtual
local Lv = yc_get_virtual_portstates(pos) local Lv = yc.get_virtual_portstates(pos)
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)
@ -596,10 +598,10 @@ function yc_action(pos, L) --L-->Lvirtual
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
minetest.swap_node(pos, {name = name, param2 = node.param2}) minetest.swap_node(pos, {name = name, param2 = node.param2})
yc_action_setports(pos, L, Lv) yc.action_setports(pos, L, Lv)
end end
function yc_action_setports(pos, L, Lv) yc.action_setports = function(pos, L, Lv)
local name = "mesecons_microcontroller:microcontroller" local name = "mesecons_microcontroller:microcontroller"
local rules local rules
if Lv.a ~= L.a then if Lv.a ~= L.a then
@ -624,7 +626,7 @@ function yc_action_setports(pos, L, Lv)
end end
end end
function yc_set_portstate(port, state, L) yc.set_portstate = function(port, state, L)
if port == "A" then L.a = state if port == "A" then L.a = state
elseif port == "B" then L.b = state elseif port == "B" then L.b = state
elseif port == "C" then L.c = state elseif port == "C" then L.c = state
@ -633,7 +635,7 @@ function yc_set_portstate(port, state, L)
return L return L
end end
function yc_update_real_portstates(pos, node, rulename, newstate) yc.update_real_portstates = function(pos, node, rulename, newstate)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if rulename == nil then if rulename == nil then
meta:set_int("real_portstates", 1) meta:set_int("real_portstates", 1)
@ -657,7 +659,7 @@ function yc_update_real_portstates(pos, node, rulename, newstate)
meta:set_int("real_portstates", 1 + L[1] + 2*L[2] + 4*L[3] + 8*L[4]) meta:set_int("real_portstates", 1 + L[1] + 2*L[2] + 4*L[3] + 8*L[4])
end end
function yc_get_real_portstates(pos) -- determine if ports are powered (by itself or from outside) yc.get_real_portstates = function(pos) -- determine if ports are powered (by itself or from outside)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local L = {} local L = {}
local n = meta:get_int("real_portstates") - 1 local n = meta:get_int("real_portstates") - 1
@ -668,7 +670,7 @@ function yc_get_real_portstates(pos) -- determine if ports are powered (by itsel
return L return L
end end
function yc_get_virtual_portstates(pos) -- portstates according to the name yc.get_virtual_portstates = function(pos) -- portstates according to the name
local name = minetest.get_node(pos).name local name = minetest.get_node(pos).name
local b, a = string.find(name, ":microcontroller") local b, a = string.find(name, ":microcontroller")
if a == nil then return nil end if a == nil then return nil end
@ -682,7 +684,7 @@ function yc_get_virtual_portstates(pos) -- portstates according to the name
return Lvirtual return Lvirtual
end end
function yc_merge_portstates(Lreal, Lvirtual) yc.merge_portstates = function(Lreal, Lvirtual)
local L = {a=false, b=false, c=false, d=false} local L = {a=false, b=false, c=false, d=false}
if Lvirtual.a or Lreal.a then L.a = true end if Lvirtual.a or Lreal.a then L.a = true end
if Lvirtual.b or Lreal.b then L.b = true end if Lvirtual.b or Lreal.b then L.b = true end