Carter Kolwey
2017-03-05 d3f40e0fd0081f8be362afeef6f72d37cff60c92
Only update the associated network(s) when placing/digging nodes
3 files modified
44 ■■■■■ changed files
technic/machines/register/cables.lua 17 ●●●● patch | view | raw | blame | history
technic/machines/supply_converter.lua 1 ●●●● patch | view | raw | blame | history
technic/machines/switching_station.lua 26 ●●●● patch | view | raw | blame | history
technic/machines/register/cables.lua
@@ -11,8 +11,19 @@
    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 @@
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
technic/machines/supply_converter.lua
@@ -59,6 +59,7 @@
        meta:set_float("active", false)
    end,
    technic_run = run,
    technic_on_disable = run,
})
minetest.register_craft({
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 @@
--------------------------------------------------
-- 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 @@
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 @@
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 @@
        {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 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,