mirror of
https://github.com/minetest-mods/technic.git
synced 2025-04-15 08:50:39 +02:00
Fix O(n^2) network traversal
This commit is contained in:
parent
23e616defe
commit
b83bc2265b
@ -91,19 +91,22 @@ minetest.register_node("technic:switching_station",{
|
|||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
-- Functions to traverse the electrical network
|
-- Functions to traverse the electrical network
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
local function flatten(map)
|
||||||
|
local list = {}
|
||||||
|
for key, value in map do
|
||||||
|
list[#list + 1] = value
|
||||||
|
end
|
||||||
|
return list
|
||||||
|
end
|
||||||
|
|
||||||
-- Add a wire node to the LV/MV/HV network
|
-- Add a wire node to the LV/MV/HV network
|
||||||
local add_new_cable_node = function(nodes, pos, network_id)
|
local add_new_cable_node = function(nodes, pos, network_id)
|
||||||
technic.cables[minetest.hash_node_position(pos)] = network_id
|
local node_id = minetest.hash_node_position(pos)
|
||||||
-- Ignore if the node has already been added
|
technic.cables[node_id] = network_id
|
||||||
for i = 1, #nodes do
|
if nodes[node_id] then
|
||||||
if pos.x == nodes[i].x and
|
return false
|
||||||
pos.y == nodes[i].y and
|
|
||||||
pos.z == nodes[i].z then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
table.insert(nodes, {x=pos.x, y=pos.y, z=pos.z, visited=1})
|
nodes[node_id] = pos
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -188,6 +191,9 @@ local get_network = function(sw_pos, pos1, tier)
|
|||||||
i, technic.machines[tier], tier, sw_pos, network_id)
|
i, technic.machines[tier], tier, sw_pos, network_id)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
until all_nodes[i] == nil
|
until all_nodes[i] == nil
|
||||||
|
PR_nodes = flatten(PR_nodes)
|
||||||
|
BA_nodes = flatten(BA_nodes)
|
||||||
|
RE_nodes = flatten(BA_nodes)
|
||||||
technic.networks[network_id] = {tier = tier, PR_nodes = PR_nodes, RE_nodes = RE_nodes, BA_nodes = BA_nodes}
|
technic.networks[network_id] = {tier = tier, PR_nodes = PR_nodes, RE_nodes = RE_nodes, BA_nodes = BA_nodes}
|
||||||
return PR_nodes, BA_nodes, RE_nodes
|
return PR_nodes, BA_nodes, RE_nodes
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user