From c48237fb85d17dceedbecbc9f6173f876ebbbce8 Mon Sep 17 00:00:00 2001 From: SX Date: Sun, 25 Oct 2020 10:58:14 +0200 Subject: [PATCH 1/6] Technic network-ng compatibility with fully contained network move capability --- compat/compat.lua | 5 ++ compat/technic_networks.lua | 98 +++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 compat/technic_networks.lua diff --git a/compat/compat.lua b/compat/compat.lua index d31156f..ab30286 100644 --- a/compat/compat.lua +++ b/compat/compat.lua @@ -19,6 +19,7 @@ dofile(MP.."/compat/elevator.lua") dofile(MP.."/compat/signs.lua") dofile(MP.."/compat/itemframes.lua") dofile(MP.."/compat/anchor.lua") +dofile(MP.."/compat/technic_networks.lua") dofile(MP.."/compat/telemosaic.lua") dofile(MP.."/compat/beds.lua") dofile(MP.."/compat/ropes.lua") @@ -84,6 +85,10 @@ jumpdrive.target_region_compat = function(source_pos1, source_pos2, target_pos1, jumpdrive.areas_compat(source_pos1, source_pos2, delta_vector) end + if has_technic_mod then + jumpdrive.technic_network_compat(source_pos1, source_pos2, target_pos1, delta_vector) + end + -- async compat functions below here minetest.after(1.0, function() diff --git a/compat/technic_networks.lua b/compat/technic_networks.lua new file mode 100644 index 0000000..67575b7 --- /dev/null +++ b/compat/technic_networks.lua @@ -0,0 +1,98 @@ + +local vecadd = vector.add +local poshash = minetest.hash_node_position +local cables = technic.cables +local machines = technic.machine_tiers + +-- Function to move technic network to another position +local network_node_arrays = {"PR_nodes","BA_nodes","RE_nodes"} +local function move_network(net, delta) + for _,tblname in ipairs(network_node_arrays) do + local tbl = network[tblname] + for i=#tbl,1,-1 do + tbl[i] = vecadd(tbl[i], delta) + end + end + local new_net_id = vecadd(technic.network2pos(net.id), delta) + local all_nodes = net.all_nodes + for old_node_id, old_pos in pairs(all_nodes) do + local new_pos = vecadd(old_pos, delta) + local node_id = poshash(new_pos) + cables[old_node_id] = nil + new_all_nodes[node_id] = new_pos + cables[node_id] = new_net_id + end + net.all_nodes = new_all_nodes +end + +local function add_edge_cable_net(pos, size, delta, t) + -- Find connected positions outside edge of box from given position + -- NOTE: At source position firt found would be enough but not at target + local x = pos.x == 0 and -1 or (pos.x == size.x and 1) + local y = pos.y == 0 and -1 or (pos.y == size.y and 1) + local z = pos.z == 0 and -1 or (pos.z == size.z and 1) + if x then + local v = vecadd(vecadd(pos, {x=x,y=0,z=0}),delta) + local net_id = cables[poshash(v)] + if net_id then t[net_id] = true end + end + if y then + local v = vecadd(vecadd(pos, {x=0,y=y,z=0}),delta) + local net_id = cables[poshash(v)] + if net_id then t[net_id] = true end + end + if z then + local v = vecadd(vecadd(pos, {x=0,y=0,z=z}),delta) + local net_id = cables[poshash(v)] + if net_id then t[net_id] = true end + end +end + +jumpdrive.technic_network_compat = function(source_pos1, source_pos2, target_pos1, delta_vector) + -- Check for technic mod compatibility + if not technic.remove_network or not technic.networks then return end + + -- search results + local jump_networks = {} -- jumped fully contained networks + local edge_networks = {} -- networks crossing jumped area edge + + -- search for networks in area + local size = vector.apply(vector.subtract(source_pos1, source_pos2),math.abs) + for x=0,size.x do + for y=0,size.y do + for z=0,size.z do + local local_pos = {x=x,y=y,z=z} + local src_pos = vecadd(source_pos1, local_pos) + local net_id = cables[poshash(src_pos)] + if net_id then + -- Add to (dirty) jumped networks + jump_networks[net_id] = true + if x==0 or x==size.x or y==0 or y==size.y or z==0 or z==size.z then + -- Candidate for edge network, check neighbors across border + add_edge_cable_net(local_pos, size, source_pos1, edge_networks) + add_edge_cable_net(local_pos, size, target_pos1, edge_networks) + end + elseif x==0 or x==size.x or y==0 or y==size.y or z==0 or z==size.z then + -- Check for unconnected nodes that might connect to network at target location + local name = minetest.get_node(src_pos).name + if machines[name] or technic.get_cable_tier(name) then + add_edge_cable_net(local_pos, size, target_pos1, edge_networks) + end + end + end + end + end + for net_id, _ in pairs(edge_networks) do + -- Remove edge networks to create contained networks list + jump_networks[net_id] = nil + -- And clean up network crossing jumped area edges + technic.remove_network(net_id) + end + for net_id, _ in pairs(jump_networks) do + -- Move fully contained networks with jumpdrive + local net = technic.networks[net_id] + if net then + move_network(net, delta_vector) + end + end +end From 74f51b06555f946ac7225a449ed8c48f04ddffc5 Mon Sep 17 00:00:00 2001 From: SX Date: Sun, 25 Oct 2020 17:03:14 +0200 Subject: [PATCH 2/6] Full edge network detection, fix typos --- compat/technic_networks.lua | 50 ++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/compat/technic_networks.lua b/compat/technic_networks.lua index 67575b7..22f43c8 100644 --- a/compat/technic_networks.lua +++ b/compat/technic_networks.lua @@ -1,6 +1,7 @@ local vecadd = vector.add local poshash = minetest.hash_node_position +local get_node = minetest.get_node local cables = technic.cables local machines = technic.machine_tiers @@ -8,26 +9,35 @@ local machines = technic.machine_tiers local network_node_arrays = {"PR_nodes","BA_nodes","RE_nodes"} local function move_network(net, delta) for _,tblname in ipairs(network_node_arrays) do - local tbl = network[tblname] + local tbl = net[tblname] for i=#tbl,1,-1 do tbl[i] = vecadd(tbl[i], delta) end end - local new_net_id = vecadd(technic.network2pos(net.id), delta) + local new_net_id = poshash(vecadd(technic.network2pos(net.id), delta)) + local new_all_nodes = {} local all_nodes = net.all_nodes for old_node_id, old_pos in pairs(all_nodes) do local new_pos = vecadd(old_pos, delta) local node_id = poshash(new_pos) cables[old_node_id] = nil - new_all_nodes[node_id] = new_pos cables[node_id] = new_net_id + new_all_nodes[node_id] = new_pos end net.all_nodes = new_all_nodes end +local function vecadd2(a, b, c) + return { + x = a.x + b.x + c.x, + y = a.y + b.y + c.y, + z = a.z + b.z + c.z, + } +end + local function add_edge_cable_net(pos, size, delta, t) -- Find connected positions outside edge of box from given position - -- NOTE: At source position firt found would be enough but not at target + -- NOTE: At source position first found would be enough but not at target local x = pos.x == 0 and -1 or (pos.x == size.x and 1) local y = pos.y == 0 and -1 or (pos.y == size.y and 1) local z = pos.z == 0 and -1 or (pos.z == size.z and 1) @@ -46,6 +56,7 @@ local function add_edge_cable_net(pos, size, delta, t) local net_id = cables[poshash(v)] if net_id then t[net_id] = true end end + return x, y, z end jumpdrive.technic_network_compat = function(source_pos1, source_pos2, target_pos1, delta_vector) @@ -65,17 +76,28 @@ jumpdrive.technic_network_compat = function(source_pos1, source_pos2, target_pos local src_pos = vecadd(source_pos1, local_pos) local net_id = cables[poshash(src_pos)] if net_id then - -- Add to (dirty) jumped networks + -- Add to (dirty) jumped fully contained networks jump_networks[net_id] = true - if x==0 or x==size.x or y==0 or y==size.y or z==0 or z==size.z then - -- Candidate for edge network, check neighbors across border - add_edge_cable_net(local_pos, size, source_pos1, edge_networks) - add_edge_cable_net(local_pos, size, target_pos1, edge_networks) - end - elseif x==0 or x==size.x or y==0 or y==size.y or z==0 or z==size.z then - -- Check for unconnected nodes that might connect to network at target location - local name = minetest.get_node(src_pos).name - if machines[name] or technic.get_cable_tier(name) then + end + if x==0 or x==size.x or y==0 or y==size.y or z==0 or z==size.z then + -- Candidate for border crossing network, check neighbors across border + if net_id then + -- Inside network is active, check outside for active and inactive + local x, y, z = add_edge_cable_net(local_pos, size, source_pos1, edge_networks) + if x then + local name = get_node(vecadd2(local_pos, target_pos1, {x=x,y=0,z=0})).name + if machines[name] or technic.get_cable_tier(name) then edge_networks[net_id] = true end + end + if y then + local name = get_node(vecadd2(local_pos, target_pos1, {x=0,y=y,z=0})).name + if machines[name] or technic.get_cable_tier(name) then edge_networks[net_id] = true end + end + if z then + local name = get_node(vecadd2(local_pos, target_pos1, {x=0,y=0,z=z})).name + if machines[name] or technic.get_cable_tier(name) then edge_networks[net_id] = true end + end + else + -- Inside is inactive, check outside for active add_edge_cable_net(local_pos, size, target_pos1, edge_networks) end end From 9b75a2b03e930f8cff8fbba5c29681a28dd29c0e Mon Sep 17 00:00:00 2001 From: SX Date: Sun, 25 Oct 2020 17:37:09 +0200 Subject: [PATCH 3/6] Luacheck, check for technic mod --- compat/technic_networks.lua | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/compat/technic_networks.lua b/compat/technic_networks.lua index 22f43c8..a212755 100644 --- a/compat/technic_networks.lua +++ b/compat/technic_networks.lua @@ -2,8 +2,15 @@ local vecadd = vector.add local poshash = minetest.hash_node_position local get_node = minetest.get_node -local cables = technic.cables -local machines = technic.machine_tiers +local cables = technic and technic.cables +local machines = technic and technic.machine_tiers + +-- Check for technic mod compatibility, compatible with technic plus +if not technic.remove_network or not technic.networks or not cables or not machines then + minetest.log("warning", "[jumpdrive] Incompatible technic mod loaded") + jumpdrive.technic_network_compat = function() end + return +end -- Function to move technic network to another position local network_node_arrays = {"PR_nodes","BA_nodes","RE_nodes"} @@ -42,17 +49,17 @@ local function add_edge_cable_net(pos, size, delta, t) local y = pos.y == 0 and -1 or (pos.y == size.y and 1) local z = pos.z == 0 and -1 or (pos.z == size.z and 1) if x then - local v = vecadd(vecadd(pos, {x=x,y=0,z=0}),delta) + local v = vecadd2(pos, {x=x,y=0,z=0}, delta) local net_id = cables[poshash(v)] if net_id then t[net_id] = true end end if y then - local v = vecadd(vecadd(pos, {x=0,y=y,z=0}),delta) + local v = vecadd2(pos, {x=0,y=y,z=0}, delta) local net_id = cables[poshash(v)] if net_id then t[net_id] = true end end if z then - local v = vecadd(vecadd(pos, {x=0,y=0,z=z}),delta) + local v = vecadd2(pos, {x=0,y=0,z=z}, delta) local net_id = cables[poshash(v)] if net_id then t[net_id] = true end end @@ -60,8 +67,6 @@ local function add_edge_cable_net(pos, size, delta, t) end jumpdrive.technic_network_compat = function(source_pos1, source_pos2, target_pos1, delta_vector) - -- Check for technic mod compatibility - if not technic.remove_network or not technic.networks then return end -- search results local jump_networks = {} -- jumped fully contained networks @@ -83,17 +88,17 @@ jumpdrive.technic_network_compat = function(source_pos1, source_pos2, target_pos -- Candidate for border crossing network, check neighbors across border if net_id then -- Inside network is active, check outside for active and inactive - local x, y, z = add_edge_cable_net(local_pos, size, source_pos1, edge_networks) - if x then - local name = get_node(vecadd2(local_pos, target_pos1, {x=x,y=0,z=0})).name + local dir_x, dir_y, dir_z = add_edge_cable_net(local_pos, size, source_pos1, edge_networks) + if dir_x then + local name = get_node(vecadd2(local_pos, target_pos1, {x=dir_x,y=0,z=0})).name if machines[name] or technic.get_cable_tier(name) then edge_networks[net_id] = true end end - if y then - local name = get_node(vecadd2(local_pos, target_pos1, {x=0,y=y,z=0})).name + if dir_y then + local name = get_node(vecadd2(local_pos, target_pos1, {x=0,y=dir_y,z=0})).name if machines[name] or technic.get_cable_tier(name) then edge_networks[net_id] = true end end - if z then - local name = get_node(vecadd2(local_pos, target_pos1, {x=0,y=0,z=z})).name + if dir_z then + local name = get_node(vecadd2(local_pos, target_pos1, {x=0,y=0,z=dir_z})).name if machines[name] or technic.get_cable_tier(name) then edge_networks[net_id] = true end end else From aee351440eac94453a5a465277e5d13f587bd5cc Mon Sep 17 00:00:00 2001 From: SX Date: Sun, 25 Oct 2020 17:41:01 +0200 Subject: [PATCH 4/6] Also check for technic name space --- compat/technic_networks.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/technic_networks.lua b/compat/technic_networks.lua index a212755..d2c89d3 100644 --- a/compat/technic_networks.lua +++ b/compat/technic_networks.lua @@ -6,7 +6,7 @@ local cables = technic and technic.cables local machines = technic and technic.machine_tiers -- Check for technic mod compatibility, compatible with technic plus -if not technic.remove_network or not technic.networks or not cables or not machines then +if not technic or not technic.remove_network or not technic.networks or not cables or not machines then minetest.log("warning", "[jumpdrive] Incompatible technic mod loaded") jumpdrive.technic_network_compat = function() end return From 2087759fbf69b16d4e06a4a6b3f2938a4a2f5994 Mon Sep 17 00:00:00 2001 From: SX Date: Sun, 25 Oct 2020 21:31:20 +0200 Subject: [PATCH 5/6] Reassign moved network for new id key --- compat/technic_networks.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compat/technic_networks.lua b/compat/technic_networks.lua index d2c89d3..f324f9e 100644 --- a/compat/technic_networks.lua +++ b/compat/technic_networks.lua @@ -32,6 +32,9 @@ local function move_network(net, delta) new_all_nodes[node_id] = new_pos end net.all_nodes = new_all_nodes + technic.networks[net.id] = nil + net.id = new_net_id + technic.networks[net.id] = net end local function vecadd2(a, b, c) From 84fa15c6371c3688b9434cfea6c4c6a5da12b85c Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 27 Oct 2020 08:51:44 +0100 Subject: [PATCH 6/6] fix luacheck --- .luacheckrc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 3a50c67..9f59920 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -9,7 +9,8 @@ globals = { -- write "travelnet", "pipeworks", - "beds" + "beds", + "technic" } read_globals = { @@ -28,7 +29,6 @@ read_globals = { "digilines", "mesecons", "mesecon", - "technic", "locator", "display_api", "areas",