From ececf525b6f717956a38fa314d2472f72451159d Mon Sep 17 00:00:00 2001
From: sfan5 <sfan5@live.de>
Date: Sun, 15 Jan 2017 23:12:13 +0100
Subject: Fix remaining namespace pollutions (closes #311)

---
 mesecons_microcontroller/init.lua | 128 +++++++++++++++++++-------------------
 1 file changed, 65 insertions(+), 63 deletions(-)

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