From d3f40e0fd0081f8be362afeef6f72d37cff60c92 Mon Sep 17 00:00:00 2001 From: Carter Kolwey Date: Sun, 5 Mar 2017 04:09:23 -0600 Subject: [PATCH] Only update the associated network(s) when placing/digging nodes --- technic/machines/register/cables.lua | 17 ++++++++++++++--- technic/machines/supply_converter.lua | 1 + technic/machines/switching_station.lua | 26 ++++++++++++++------------ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/technic/machines/register/cables.lua b/technic/machines/register/cables.lua index 28984c0..a7e72a3 100644 --- a/technic/machines/register/cables.lua +++ b/technic/machines/register/cables.lua @@ -11,8 +11,19 @@ function technic.get_cable_tier(name) return cable_tier[name] end -local function clear_networks() - technic.networks = {} +local function clear_networks(pos) + local positions = { + {x=pos.x+1, y=pos.y, z=pos.z}, + {x=pos.x-1, y=pos.y, z=pos.z}, + {x=pos.x, y=pos.y+1, z=pos.z}, + {x=pos.x, y=pos.y-1, z=pos.z}, + {x=pos.x, y=pos.y, z=pos.z+1}, + {x=pos.x, y=pos.y, z=pos.z-1}} + for _,connected_pos in pairs(positions) do + if technic.cables[minetest.hash_node_position(connected_pos)] then + technic.networks[technic.cables[minetest.hash_node_position(connected_pos)]] = nil + end + end end function technic.register_cable(tier, size) @@ -55,7 +66,7 @@ end local function clear_nets_if_machine(pos, node) for tier, machine_list in pairs(technic.machines) do if machine_list[node.name] ~= nil then - return clear_networks() + return clear_networks(pos) end end end diff --git a/technic/machines/supply_converter.lua b/technic/machines/supply_converter.lua index 32597de..a88651b 100644 --- a/technic/machines/supply_converter.lua +++ b/technic/machines/supply_converter.lua @@ -59,6 +59,7 @@ minetest.register_node("technic:supply_converter", { meta:set_float("active", false) end, technic_run = run, + technic_on_disable = run, }) minetest.register_craft({ diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua index f7ec0b6..2051acf 100644 --- a/technic/machines/switching_station.lua +++ b/technic/machines/switching_station.lua @@ -32,6 +32,7 @@ -- This way the supplies are separated per network. technic.networks = {} +technic.cables = {} local S = technic.getter @@ -64,7 +65,8 @@ minetest.register_node("technic:switching_station",{ -------------------------------------------------- -- Add a wire node to the LV/MV/HV network -local add_new_cable_node = function(nodes, pos) +local add_new_cable_node = function(nodes, pos, network_id) + technic.cables[minetest.hash_node_position(pos)] = network_id -- Ignore if the node has already been added for i = 1, #nodes do if pos.x == nodes[i].x and @@ -78,31 +80,31 @@ local add_new_cable_node = function(nodes, pos) end -- Generic function to add found connected nodes to the right classification array -local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below) +local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below, network_id) technic.get_or_load_node(pos) local meta = minetest.get_meta(pos) local name = minetest.get_node(pos).name if technic.is_tier_cable(name, tier) then - add_new_cable_node(all_nodes, pos) + add_new_cable_node(all_nodes, pos,network_id) elseif machines[name] then --dprint(name.." is a "..machines[name]) if machines[name] == technic.producer then - add_new_cable_node(PR_nodes, pos) + add_new_cable_node(PR_nodes, pos, network_id) elseif machines[name] == technic.receiver then - add_new_cable_node(RE_nodes, pos) + add_new_cable_node(RE_nodes, pos, network_id) elseif machines[name] == technic.producer_receiver then - add_new_cable_node(PR_nodes, pos) - add_new_cable_node(RE_nodes, pos) + add_new_cable_node(PR_nodes, pos, network_id) + add_new_cable_node(RE_nodes, pos, network_id) elseif machines[name] == "SPECIAL" and (pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and from_below then -- Another switching station -> disable it - add_new_cable_node(SP_nodes, pos) + add_new_cable_node(SP_nodes, pos, network_id) meta:set_int("active", 0) meta:set_string("active_pos", minetest.serialize(sw_pos)) elseif machines[name] == technic.battery then - add_new_cable_node(BA_nodes, pos) + add_new_cable_node(BA_nodes, pos, network_id) end meta:set_int(tier.."_EU_timeout", 2) -- Touch node @@ -110,7 +112,7 @@ local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nod end -- Traverse a network given a list of machines and a cable type name -local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos) +local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos, network_id) local pos = all_nodes[i] local positions = { {x=pos.x+1, y=pos.y, z=pos.z}, @@ -121,7 +123,7 @@ local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_no {x=pos.x, y=pos.y, z=pos.z-1}} --print("ON") for i, cur_pos in pairs(positions) do - check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3) + check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3, network_id) end end @@ -153,7 +155,7 @@ local get_network = function(sw_pos, pos1, tier) local all_nodes = {pos1} repeat traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, - i, technic.machines[tier], tier, sw_pos) + i, technic.machines[tier], tier, sw_pos, minetest.hash_node_position(pos1)) i = i + 1 until all_nodes[i] == nil technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes,