diff options
author | Jeija <jeija@mesecons.net> | 2014-11-22 15:42:22 +0100 |
---|---|---|
committer | Jeija <jeija@mesecons.net> | 2014-11-22 15:42:22 +0100 |
commit | 5be179bf110b44bdc06df6dbfde4e61487cf0635 (patch) | |
tree | 018eb119344d66d6229ad65607d51360fd006934 /mesecons/util.lua | |
parent | ffacbfde5a956da910479139f490f8ffa3ae5a85 (diff) | |
download | mesecons-5be179bf110b44bdc06df6dbfde4e61487cf0635.tar mesecons-5be179bf110b44bdc06df6dbfde4e61487cf0635.tar.gz mesecons-5be179bf110b44bdc06df6dbfde4e61487cf0635.tar.bz2 mesecons-5be179bf110b44bdc06df6dbfde4e61487cf0635.tar.xz mesecons-5be179bf110b44bdc06df6dbfde4e61487cf0635.zip |
Replace mesecon:<some_function> with mesecon.<some_function> for greater
flexibility and because it was never inteded to be OOP in the first
place.
mesecon.receptor_on and mesecon.receptor_off are provided by wrappers
(mesecon:receptor_on/off) for compatibility, but will be removed. Mod
programmers that use mesecons: Please update!
Also, fix microcontroller polluting the global namespace and remove some
deprecated stuff.
Diffstat (limited to 'mesecons/util.lua')
-rw-r--r-- | mesecons/util.lua | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/mesecons/util.lua b/mesecons/util.lua index 65172ae..281997a 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -1,4 +1,4 @@ -function mesecon:move_node(pos, newpos) +function mesecon.move_node(pos, newpos) local node = minetest.get_node(pos) local meta = minetest.get_meta(pos):to_table() minetest.remove_node(pos) @@ -7,18 +7,18 @@ function mesecon:move_node(pos, newpos) end --[[ new functions: -mesecon:flattenrules(allrules) -mesecon:rule2bit(findrule, allrules) -mesecon:rule2meta(findrule, allrules) +mesecon.flattenrules(allrules) +mesecon.rule2bit(findrule, allrules) +mesecon.rule2meta(findrule, allrules) dec2bin(n) -mesecon:getstate(nodename, states) -mesecon:getbinstate(nodename, states) -mesecon:get_bit(binary, bit) -mesecon:set_bit(binary, bit, value) -mesecon:invertRule(r) +mesecon.getstate(nodename, states) +mesecon.getbinstate(nodename, states) +mesecon.get_bit(binary, bit) +mesecon.set_bit(binary, bit, value) +mesecon.invertRule(r) --]] -function mesecon:flattenrules(allrules) +function mesecon.flattenrules(allrules) --[[ { { @@ -53,7 +53,7 @@ function mesecon:flattenrules(allrules) --]] end -function mesecon:rule2bit(findrule, allrules) +function mesecon.rule2bit(findrule, allrules) --get the bit of the metarule the rule is in, or bit 1 if (allrules[1] and allrules[1].x) or @@ -62,14 +62,14 @@ function mesecon:rule2bit(findrule, allrules) end for m,metarule in ipairs( allrules) do for _, rule in ipairs(metarule ) do - if mesecon:cmpPos(findrule, rule) and mesecon:cmpSpecial(findrule, rule) then + if mesecon.cmpPos(findrule, rule) and mesecon.cmpSpecial(findrule, rule) then return m end end end end -function mesecon:rule2metaindex(findrule, allrules) +function mesecon.rule2metaindex(findrule, allrules) --get the metarule the rule is in, or allrules if allrules[1].x then @@ -77,20 +77,20 @@ function mesecon:rule2metaindex(findrule, allrules) end if not(findrule) then - return mesecon:flattenrules(allrules) + return mesecon.flattenrules(allrules) end for m, metarule in ipairs( allrules) do for _, rule in ipairs(metarule ) do - if mesecon:cmpPos(findrule, rule) and mesecon:cmpSpecial(findrule, rule) then + if mesecon.cmpPos(findrule, rule) and mesecon.cmpSpecial(findrule, rule) then return m end end end end -function mesecon:rule2meta(findrule, allrules) - local index = mesecon:rule2metaindex(findrule, allrules) +function mesecon.rule2meta(findrule, allrules) + local index = mesecon.rule2metaindex(findrule, allrules) if index == nil then if allrules[1].x then return allrules @@ -119,7 +119,7 @@ else end end -function mesecon:getstate(nodename, states) +function mesecon.getstate(nodename, states) for state, name in ipairs(states) do if name == nodename then return state @@ -128,23 +128,23 @@ function mesecon:getstate(nodename, states) error(nodename.." doesn't mention itself in "..dump(states)) end -function mesecon:getbinstate(nodename, states) - return dec2bin(mesecon:getstate(nodename, states)-1) +function mesecon.getbinstate(nodename, states) + return dec2bin(mesecon.getstate(nodename, states)-1) end -function mesecon:get_bit(binary,bit) +function mesecon.get_bit(binary,bit) bit = bit or 1 local c = binary:len()-(bit-1) return binary:sub(c,c) == "1" end -function mesecon:set_bit(binary,bit,value) +function mesecon.set_bit(binary,bit,value) if value == "1" then - if not mesecon:get_bit(binary,bit) then + if not mesecon.get_bit(binary,bit) then return dec2bin(tonumber(binary,2)+math.pow(2,bit-1)) end elseif value == "0" then - if mesecon:get_bit(binary,bit) then + if mesecon.get_bit(binary,bit) then return dec2bin(tonumber(binary,2)-math.pow(2,bit-1)) end end @@ -152,29 +152,29 @@ function mesecon:set_bit(binary,bit,value) end -function mesecon:invertRule(r) +function mesecon.invertRule(r) return {x = -r.x, y = -r.y, z = -r.z, sx = r.sx, sy = r.sy, sz = r.sz} end -function mesecon:addPosRule(p, r) +function mesecon.addPosRule(p, r) return {x = p.x + r.x, y = p.y + r.y, z = p.z + r.z} end -function mesecon:cmpPos(p1, p2) +function mesecon.cmpPos(p1, p2) return (p1.x == p2.x and p1.y == p2.y and p1.z == p2.z) end -function mesecon:cmpSpecial(r1, r2) +function mesecon.cmpSpecial(r1, r2) return (r1.sx == r2.sx and r1.sy == r2.sy and r1.sz == r2.sz) end -function mesecon:tablecopy(table) -- deep table copy +function mesecon.tablecopy(table) -- deep table copy if type(table) ~= "table" then return table end -- no need to copy local newtable = {} for idx, item in pairs(table) do if type(item) == "table" then - newtable[idx] = mesecon:tablecopy(item) + newtable[idx] = mesecon.tablecopy(item) else newtable[idx] = item end @@ -183,12 +183,12 @@ function mesecon:tablecopy(table) -- deep table copy return newtable end -function mesecon:cmpAny(t1, t2) +function mesecon.cmpAny(t1, t2) if type(t1) ~= type(t2) then return false end if type(t1) ~= "table" and type(t2) ~= "table" then return t1 == t2 end for i, e in pairs(t1) do - if not mesecon:cmpAny(e, t2[i]) then return false end + if not mesecon.cmpAny(e, t2[i]) then return false end end return true |