From c8c2fd32daafed87beb820f516749b83dce3e4df Mon Sep 17 00:00:00 2001 From: Jeija Date: Mon, 13 Aug 2012 08:50:10 +0200 Subject: Code cleanup and ond more extremely small speedup --- mesecons/init.lua | 6 ++++-- mesecons/internal_api.lua | 29 ++++++++++++++++++----------- mesecons/services.lua | 12 ++++++++++++ mesecons/wires.lua | 4 ++-- 4 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 mesecons/services.lua diff --git a/mesecons/init.lua b/mesecons/init.lua index 0af1503..3697c7a 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -98,7 +98,6 @@ dofile(minetest.get_modpath("mesecons").."/settings.lua") --Internal API dofile(minetest.get_modpath("mesecons").."/internal_api.lua"); - -- API API API API API API API API API API API API API API API API API API function mesecon:add_receptor_node(nodename, rules, get_rules) --rules table is optional; if rules depend on param2 pass (nodename, nil, function get_rules) @@ -158,7 +157,7 @@ function mesecon:receptor_off(pos, rules) np.x = pos.x + rules[i].x np.y = pos.y + rules[i].y np.z = pos.z + rules[i].z - if not mesecon:connected_to_pw_src(np, {}) then + if not mesecon:connected_to_pw_src(np) then mesecon:turnoff(np) end i=i+1 @@ -220,3 +219,6 @@ print("[MESEcons] Main mod Loaded!") --The actual wires dofile(minetest.get_modpath("mesecons").."/wires.lua"); + +--Services like turnoff receptor on dignode and so on +dofile(minetest.get_modpath("mesecons").."/services.lua"); diff --git a/mesecons/internal_api.lua b/mesecons/internal_api.lua index d382328..7570091 100644 --- a/mesecons/internal_api.lua +++ b/mesecons/internal_api.lua @@ -288,21 +288,27 @@ end function mesecon:connected_to_pw_src(pos, checked) + if checked == nil then + checked = {} + end + local connected local i = 1 + while checked[i] ~= nil do --find out if node has already been checked if checked[i].x == pos.x and checked[i].y == pos.y and checked[i].z == pos.z then - return false + return false, checked end i = i + 1 end + checked[i] = {x=pos.x, y=pos.y, z=pos.z} --add current node to checked local node = minetest.env:get_node_or_nil(pos) - if node == nil then return false end + if node == nil then return false, checked end if mesecon:is_conductor_on(node.name) or mesecon:is_conductor_off(node.name) then - if mesecon:is_powered_from_receptor(pos) then --return if conductor is powered - return true + if mesecon:is_powered_by_receptor(pos) then --return if conductor is powered + return true, checked end local rules = mesecon:get_rules("default") --TODO: Use conductor specific rules @@ -312,16 +318,17 @@ function mesecon:connected_to_pw_src(pos, checked) np.x = pos.x + rules[i].x np.y = pos.y + rules[i].y np.z = pos.z + rules[i].z - if mesecon:connected_to_pw_src(np, checked) == true then - return true + connected, checked = mesecon:connected_to_pw_src(np, checked) + if connected then + return true end i=i+1 end end - return false + return false, checked end -function mesecon:is_powered_from_receptor(pos) +function mesecon:is_powered_by_receptor(pos) local rcpt local rcpt_pos = {} local rcpt_checked = {} --using a checked array speeds this up @@ -361,7 +368,7 @@ function mesecon:is_powered_from_receptor(pos) return false end -function mesecon:is_powered_from_conductor(pos) +function mesecon:is_powered_by_conductor(pos) local k=1 rules=mesecon:get_rules("default") --TODO: use conductor specific rules @@ -375,11 +382,11 @@ function mesecon:is_powered_from_conductor(pos) end function mesecon:is_powered(pos) - return mesecon:is_powered_from_conductor(pos) or mesecon:is_powered_from_receptor(pos) + return mesecon:is_powered_by_conductor(pos) or mesecon:is_powered_by_receptor(pos) end function mesecon:updatenode(pos) - if mesecon:connected_to_pw_src(pos, {}) then + if mesecon:connected_to_pw_src(pos) then mesecon:turnon(pos) else mesecon:turnoff(pos) diff --git a/mesecons/services.lua b/mesecons/services.lua new file mode 100644 index 0000000..cf3b78b --- /dev/null +++ b/mesecons/services.lua @@ -0,0 +1,12 @@ +minetest.register_on_dignode( + function(pos, oldnode, digger) + if mesecon:is_conductor_on(oldnode.name) then + local i = 1 + mesecon:receptor_off(pos) + end + + if mesecon:is_receptor_node(oldnode.name) then + mesecon:receptor_off(pos) + end + end +) diff --git a/mesecons/wires.lua b/mesecons/wires.lua index e4c7e6a..450aae2 100644 --- a/mesecons/wires.lua +++ b/mesecons/wires.lua @@ -87,8 +87,8 @@ for zmy=0, 1 do if zpy == 1 then table.insert(nodebox, box_zpy) end if xmy == 1 then table.insert(nodebox, box_xmy) end if zmy == 1 then table.insert(nodebox, box_zmy) end - nobump = xp+zp+xm+zm - if adjx and adjz and (nobump > 2) then + + if adjx and adjz and (xp + zp + xm + zm > 2) then table.insert(nodebox, box_bump1) table.insert(nodebox, box_bump2) tiles_off = { -- cgit v1.2.3 From c20a4b3a897b2c55ec6dd7b84fb6404c387d16d0 Mon Sep 17 00:00:00 2001 From: Jeija Date: Mon, 13 Aug 2012 09:37:55 +0200 Subject: Minimal speedup by not checking some nodes twice for receptors --- mesecons/init.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mesecons/init.lua b/mesecons/init.lua index 3697c7a..6715a2f 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -151,13 +151,16 @@ function mesecon:receptor_off(pos, rules) rules = mesecon:get_rules("default") end + local checked = {} + local connected = false local i = 1 while rules[i]~=nil do local np = {} np.x = pos.x + rules[i].x np.y = pos.y + rules[i].y np.z = pos.z + rules[i].z - if not mesecon:connected_to_pw_src(np) then + connected, checked = mesecon:connected_to_pw_src(np, checked) + if not connected then mesecon:turnoff(np) end i=i+1 -- cgit v1.2.3