From 809192f353af2faeb779df85104d4d7ed8d8943d Mon Sep 17 00:00:00 2001 From: Jeija Date: Sun, 14 Feb 2016 20:55:50 +0100 Subject: [PATCH] Use vector helper functions instead of old builtin cmpPos and addPosRule functions --- mesecons/actionqueue.lua | 2 +- mesecons/init.lua | 8 ++++---- mesecons/internal.lua | 24 ++++++++++++------------ mesecons/services.lua | 2 +- mesecons/util.lua | 14 +++----------- mesecons/wires.lua | 6 +++--- mesecons_extrawires/vertical.lua | 8 ++++---- mesecons_mvps/init.lua | 16 ++++++---------- mesecons_pistons/init.lua | 8 ++++---- mesecons_pressureplates/init.lua | 2 +- mesecons_random/init.lua | 8 ++++---- mesecons_torch/init.lua | 2 +- 12 files changed, 44 insertions(+), 56 deletions(-) diff --git a/mesecons/actionqueue.lua b/mesecons/actionqueue.lua index fa4079f..a773008 100644 --- a/mesecons/actionqueue.lua +++ b/mesecons/actionqueue.lua @@ -23,7 +23,7 @@ function mesecon.queue:add_action(pos, func, params, time, overwritecheck, prior -- Otherwise, add the action to the queue if overwritecheck then -- check if old action has to be overwritten / removed: for i, ac in ipairs(mesecon.queue.actions) do - if(mesecon.cmpPos(pos, ac.pos) + if(vector.equals(pos, ac.pos) and mesecon.cmpAny(overwritecheck, ac.owcheck)) then toremove = i break diff --git a/mesecons/init.lua b/mesecons/init.lua index 8645f03..3ab4f4a 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -74,7 +74,7 @@ mesecon.queue:add_function("receptor_on", function (pos, rules) -- if area (any of the rule targets) is not loaded, keep trying and call this again later for _, rule in ipairs(mesecon.flattenrules(rules)) do - local np = mesecon.addPosRule(pos, rule) + local np = vector.add(pos, rule) -- if area is not loaded, keep trying if minetest.get_node_or_nil(np) == nil then mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules) @@ -84,7 +84,7 @@ mesecon.queue:add_function("receptor_on", function (pos, rules) -- execute action for _, rule in ipairs(mesecon.flattenrules(rules)) do - local np = mesecon.addPosRule(pos, rule) + local np = vector.add(pos, rule) local rulenames = mesecon.rules_link_rule_all(pos, rule) for _, rulename in ipairs(rulenames) do mesecon.turnon(np, rulename) @@ -101,7 +101,7 @@ mesecon.queue:add_function("receptor_off", function (pos, rules) -- if area (any of the rule targets) is not loaded, keep trying and call this again later for _, rule in ipairs(mesecon.flattenrules(rules)) do - local np = mesecon.addPosRule(pos, rule) + local np = vector.add(pos, rule) if minetest.get_node_or_nil(np) == nil then mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules) return @@ -109,7 +109,7 @@ mesecon.queue:add_function("receptor_off", function (pos, rules) end for _, rule in ipairs(mesecon.flattenrules(rules)) do - local np = mesecon.addPosRule(pos, rule) + local np = vector.add(pos, rule) local rulenames = mesecon.rules_link_rule_all(pos, rule) for _, rulename in ipairs(rulenames) do if not mesecon.connected_to_receptor(np, mesecon.invertRule(rule)) then diff --git a/mesecons/internal.lua b/mesecons/internal.lua index 741abf1..41c5c52 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -374,7 +374,7 @@ function mesecon.turnon(pos, link) -- call turnon on neighbors: normal rules for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do - local np = mesecon.addPosRule(f.pos, r) + local np = vector.add(f.pos, r) -- area not loaded, postpone action if not minetest.get_node_or_nil(np) then @@ -420,7 +420,7 @@ function mesecon.turnoff(pos, link) -- call turnoff on neighbors: normal rules for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do - local np = mesecon.addPosRule(f.pos, r) + local np = vector.add(f.pos, r) -- area not loaded, postpone action if not minetest.get_node_or_nil(np) then @@ -458,7 +458,7 @@ function mesecon.connected_to_receptor(pos, link) for _, rule in ipairs(mesecon.rule2meta(link, rules)) do local links = mesecon.rules_link_rule_all_inverted(pos, rule) for _, l in ipairs(links) do - local np = mesecon.addPosRule(pos, l) + local np = vector.add(pos, l) if mesecon.find_receptor_on(np, mesecon.invertRule(l)) then return true end @@ -485,7 +485,7 @@ function mesecon.find_receptor_on(pos, link) -- call turnoff on neighbors: normal rules for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do - local np = mesecon.addPosRule(f.pos, r) + local np = vector.add(f.pos, r) local links = mesecon.rules_link_rule_all_inverted(f.pos, r) for _, l in ipairs(links) do @@ -513,10 +513,10 @@ function mesecon.rules_link(output, input, dug_outputrules) --output/input are p for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do -- Check if output sends to input - if mesecon.cmpPos(mesecon.addPosRule(output, outputrule), input) then + if vector.equals(vector.add(output, outputrule), input) then for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do -- Check if input accepts from output - if mesecon.cmpPos(mesecon.addPosRule(input, inputrule), output) then + if vector.equals(vector.add(input, inputrule), output) then return true, inputrule end end @@ -526,7 +526,7 @@ function mesecon.rules_link(output, input, dug_outputrules) --output/input are p end function mesecon.rules_link_rule_all(output, rule) - local input = mesecon.addPosRule(output, rule) + local input = vector.add(output, rule) local inputnode = minetest.get_node(input) local inputrules = mesecon.get_any_inputrules (inputnode) if not inputrules then @@ -536,7 +536,7 @@ function mesecon.rules_link_rule_all(output, rule) for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do -- Check if input accepts from output - if mesecon.cmpPos(mesecon.addPosRule(input, inputrule), output) then + if vector.equals(vector.add(input, inputrule), output) then table.insert(rules, inputrule) end end @@ -545,7 +545,7 @@ end function mesecon.rules_link_rule_all_inverted(input, rule) --local irule = mesecon.invertRule(rule) - local output = mesecon.addPosRule(input, rule) + local output = vector.add(input, rule) local outputnode = minetest.get_node(output) local outputrules = mesecon.get_any_outputrules (outputnode) if not outputrules then @@ -554,7 +554,7 @@ function mesecon.rules_link_rule_all_inverted(input, rule) local rules = {} for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do - if mesecon.cmpPos(mesecon.addPosRule(output, outputrule), input) then + if vector.equals(vector.add(output, outputrule), input) then table.insert(rules, mesecon.invertRule(outputrule)) end end @@ -577,7 +577,7 @@ function mesecon.is_powered(pos, rule) for _, rule in ipairs(mesecon.flattenrules(rules)) do local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) for _, rname in ipairs(rulenames) do - local np = mesecon.addPosRule(pos, rname) + local np = vector.add(pos, rname) local nn = minetest.get_node(np) if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname)) or mesecon.is_receptor_on (nn.name)) then @@ -588,7 +588,7 @@ function mesecon.is_powered(pos, rule) else local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) for _, rname in ipairs(rulenames) do - local np = mesecon.addPosRule(pos, rname) + local np = vector.add(pos, rname) local nn = minetest.get_node(np) if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname)) or mesecon.is_receptor_on (nn.name)) then diff --git a/mesecons/services.lua b/mesecons/services.lua index 215fb31..5d0f81e 100644 --- a/mesecons/services.lua +++ b/mesecons/services.lua @@ -16,7 +16,7 @@ mesecon.on_placenode = function (pos, node) -- also call receptor_on if itself is powered already, so that neighboring -- conductors will be activated (when pushing an on-conductor with a piston) for _, s in ipairs(sources) do - local rule = {x = pos.x - s.x, y = pos.y - s.y, z = pos.z - s.z} + local rule = vector.subtract(pos, s) mesecon.turnon(pos, rule) end --mesecon.receptor_on (pos, mesecon.conductor_get_rules(node)) diff --git a/mesecons/util.lua b/mesecons/util.lua index dd33f1c..24acfcf 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -50,7 +50,7 @@ function mesecon.rule2bit(findrule, allrules) end for m,metarule in ipairs( allrules) do for _, rule in ipairs(metarule ) do - if mesecon.cmpPos(findrule, rule) then + if vector.equals(findrule, rule) then return m end end @@ -69,7 +69,7 @@ function mesecon.rule2metaindex(findrule, allrules) for m, metarule in ipairs( allrules) do for _, rule in ipairs(metarule ) do - if mesecon.cmpPos(findrule, rule) then + if vector.equals(findrule, rule) then return m end end @@ -133,15 +133,7 @@ function mesecon.set_bit(binary,bit,value) end function mesecon.invertRule(r) - return {x = -r.x, y = -r.y, z = -r.z} -end - -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) - return (p1.x == p2.x and p1.y == p2.y and p1.z == p2.z) + return vector.multiply(r, -1) end function mesecon.tablecopy(table) -- deep table copy diff --git a/mesecons/wires.lua b/mesecons/wires.lua index 18ae8f5..d77904e 100644 --- a/mesecons/wires.lua +++ b/mesecons/wires.lua @@ -22,7 +22,7 @@ local wire_getconnect = function (from_pos, self_pos) end for _, r in ipairs(mesecon.flattenrules(rules)) do - if (mesecon.cmpPos(mesecon.addPosRule(self_pos, r), from_pos)) then + if (vector.equals(vector.add(self_pos, r), from_pos)) then return true end end @@ -35,7 +35,7 @@ local wire_updateconnect = function (pos) local connections = {} for _, r in ipairs(mesecon.rules.default) do - if wire_getconnect(pos, mesecon.addPosRule(pos, r)) then + if wire_getconnect(pos, vector.add(pos, r)) then table.insert(connections, r) end end @@ -83,7 +83,7 @@ local update_on_place_dig = function (pos, node) if (not rules) then return end for _, r in ipairs(mesecon.flattenrules(rules)) do - local np = mesecon.addPosRule(pos, r) + local np = vector.add(pos, r) if minetest.registered_nodes[minetest.get_node(np).name] and minetest.registered_nodes[minetest.get_node(np).name].mesecon_wire then wire_updateconnect(np) diff --git a/mesecons_extrawires/vertical.lua b/mesecons_extrawires/vertical.lua index cac2ae2..b1d0405 100644 --- a/mesecons_extrawires/vertical.lua +++ b/mesecons_extrawires/vertical.lua @@ -42,8 +42,8 @@ local vertical_updatepos = function (pos) local node = minetest.get_node(pos) if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].is_vertical_conductor then - local node_above = minetest.get_node(mesecon.addPosRule(pos, vertical_rules[1])) - local node_below = minetest.get_node(mesecon.addPosRule(pos, vertical_rules[2])) + local node_above = minetest.get_node(vector.add(pos, vertical_rules[1])) + local node_below = minetest.get_node(vector.add(pos, vertical_rules[2])) local namestate = minetest.registered_nodes[node.name].vertical_conductor_state local above = minetest.registered_nodes[node_above.name] @@ -67,8 +67,8 @@ end local vertical_update = function (pos, node) vertical_updatepos(pos) -- this one - vertical_updatepos(mesecon.addPosRule(pos, vertical_rules[1])) -- above - vertical_updatepos(mesecon.addPosRule(pos, vertical_rules[2])) -- below + vertical_updatepos(vector.add(pos, vertical_rules[1])) -- above + vertical_updatepos(vector.add(pos, vertical_rules[2])) -- below end -- Vertical wire diff --git a/mesecons_mvps/init.lua b/mesecons_mvps/init.lua index 2f4edfc..3a3ff29 100644 --- a/mesecons_mvps/init.lua +++ b/mesecons_mvps/init.lua @@ -175,7 +175,7 @@ function mesecon.mvps_push_or_pull(pos, stackdir, movedir, maximum, all_pull_sti -- add nodes for _, n in ipairs(nodes) do - local np = mesecon.addPosRule(n.pos, movedir) + local np = vector.add(n.pos, movedir) minetest.add_node(np, n.node) minetest.get_meta(np):from_table(n.meta) @@ -186,7 +186,7 @@ function mesecon.mvps_push_or_pull(pos, stackdir, movedir, maximum, all_pull_sti for i in ipairs(nodes) do moved_nodes[i] = {} moved_nodes[i].oldpos = nodes[i].pos - nodes[i].pos = mesecon.addPosRule(nodes[i].pos, movedir) + nodes[i].pos = vector.add(nodes[i].pos, movedir) moved_nodes[i].pos = nodes[i].pos moved_nodes[i].node = nodes[i].node moved_nodes[i].meta = nodes[i].meta @@ -207,12 +207,8 @@ end) function mesecon.mvps_move_objects(pos, dir, nodestack) local objects_to_move = {} - -- Move object at tip of stack - local pushpos = mesecon.addPosRule(pos, -- get pos at tip of stack - {x = dir.x * #nodestack, - y = dir.y * #nodestack, - z = dir.z * #nodestack}) - + -- Move object at tip of stack, pushpos is position at tip of stack + local pushpos = vector.add(pos, vector.multiply(dir, #nodestack)) local objects = minetest.get_objects_inside_radius(pushpos, 1) for _, obj in ipairs(objects) do @@ -223,7 +219,7 @@ function mesecon.mvps_move_objects(pos, dir, nodestack) if tonumber(minetest.setting_get("movement_gravity")) > 0 and dir.y == 0 then -- If gravity positive and dir horizontal, push players standing on the stack for _, n in ipairs(nodestack) do - local p_above = mesecon.addPosRule(n.pos, {x=0, y=1, z=0}) + local p_above = vector.add(n.pos, {x=0, y=1, z=0}) local objects = minetest.get_objects_inside_radius(p_above, 1) for _, obj in ipairs(objects) do table.insert(objects_to_move, obj) @@ -234,7 +230,7 @@ function mesecon.mvps_move_objects(pos, dir, nodestack) for _, obj in ipairs(objects_to_move) do local entity = obj:get_luaentity() if not entity or not mesecon.is_mvps_unmov(entity.name) then - local np = mesecon.addPosRule(obj:getpos(), dir) + local np = vector.add(obj:getpos(), dir) --move only if destination is not solid local nn = minetest.get_node(np) diff --git a/mesecons_pistons/init.lua b/mesecons_pistons/init.lua index bad061a..5bf380c 100644 --- a/mesecons_pistons/init.lua +++ b/mesecons_pistons/init.lua @@ -57,7 +57,7 @@ end local piston_remove_pusher = function(pos, node) local pistonspec = minetest.registered_nodes[node.name].mesecons_piston local dir = piston_get_direction(pistonspec.dir, node) - local pusherpos = mesecon.addPosRule(pos, dir) + local pusherpos = vector.add(pos, dir) local pushername = minetest.get_node(pusherpos).name -- make sure there actually is a pusher (for compatibility reasons mainly) @@ -78,7 +78,7 @@ local piston_on = function(pos, node) local pistonspec = minetest.registered_nodes[node.name].mesecons_piston local dir = piston_get_direction(pistonspec.dir, node) - local np = mesecon.addPosRule(pos, dir) + local np = vector.add(pos, dir) local maxpush = mesecon.setting("piston_max_push", 15) local success, stack, oldstack = mesecon.mvps_push(np, dir, maxpush) if success then @@ -719,12 +719,12 @@ end local piston_get_stopper = function (node, dir, stack, stackid) pistonspec = minetest.registered_nodes[node.name].mesecons_piston dir = piston_get_direction(pistonspec.dir, node) - local pusherpos = mesecon.addPosRule(stack[stackid].pos, dir) + local pusherpos = vector.add(stack[stackid].pos, dir) local pushernode = minetest.get_node(pusherpos) if minetest.registered_nodes[node.name].mesecons_piston.pusher == pushernode.name then for _, s in ipairs(stack) do - if mesecon.cmpPos(s.pos, pusherpos) -- pusher is also to be pushed + if vector.equals(s.pos, pusherpos) -- pusher is also to be pushed and s.node.param2 == node.param2 then return false end diff --git a/mesecons_pressureplates/init.lua b/mesecons_pressureplates/init.lua index b00db9a..1ca7cd8 100644 --- a/mesecons_pressureplates/init.lua +++ b/mesecons_pressureplates/init.lua @@ -17,7 +17,7 @@ pp_on_timer = function (pos, elapsed) if not basename then return end local objs = minetest.get_objects_inside_radius(pos, 1) - local two_below = mesecon.addPosRule(pos, {x = 0, y = -2, z = 0}) + local two_below = vector.add(pos, vector.new(0, -2, 0)) if objs[1] == nil and node.name == basename .. "_on" then minetest.add_node(pos, {name = basename .. "_off"}) diff --git a/mesecons_random/init.lua b/mesecons_random/init.lua index 0136309..9e0e333 100644 --- a/mesecons_random/init.lua +++ b/mesecons_random/init.lua @@ -66,10 +66,10 @@ minetest.register_node("mesecons_random:ghoststone_active", { offstate = "mesecons_random:ghoststone" }}, on_construct = function(pos) - --remove shadow - pos2 = {x = pos.x, y = pos.y + 1, z = pos.z} - if ( minetest.get_node(pos2).name == "air" ) then - minetest.dig_node(pos2) + -- remove shadow + shadowpos = vector.add(pos, vector.new(0, 1, 0)) + if (minetest.get_node(shadowpos).name == "air") then + minetest.dig_node(shadowpos) end end }) diff --git a/mesecons_torch/init.lua b/mesecons_torch/init.lua index 5d1ad8f..8b1632b 100644 --- a/mesecons_torch/init.lua +++ b/mesecons_torch/init.lua @@ -91,7 +91,7 @@ minetest.register_abm({ action = function(pos, node) local is_powered = false for _, rule in ipairs(torch_get_input_rules(node)) do - local src = mesecon.addPosRule(pos, rule) + local src = vector.add(pos, rule) if mesecon.is_power_on(src) then is_powered = true end