Remove disconnected machines after cable is destructed

This commit is contained in:
mrkubax10 2023-11-14 20:49:05 +01:00
parent 42130c90a7
commit 5ea9002630
2 changed files with 71 additions and 50 deletions

16
api.lua
View File

@ -289,10 +289,11 @@ local function clampFlow(pos,flow)
return math.min(flow,newFlow) return math.min(flow,newFlow)
end end
-- \brief Creates network map starting from node at pos -- \brief Creates network map starting from node at pos, optionally omitting node at omit
-- \param pos vector -- \param pos vector
-- \param (optional) omit Vector
-- \returns table with network map -- \returns table with network map
industrialtest.api.createNetworkMap=function(pos) industrialtest.api.createNetworkMap=function(pos,omit)
local workers={} local workers={}
local map={} local map={}
local connections=industrialtest.api.getConnections(pos,"i") local connections=industrialtest.api.getConnections(pos,"i")
@ -302,6 +303,7 @@ industrialtest.api.createNetworkMap=function(pos)
local serializedSourcePos=pos.x..","..pos.y..","..pos.z local serializedSourcePos=pos.x..","..pos.y..","..pos.z
local visitedNodes={[serializedSourcePos]=true} local visitedNodes={[serializedSourcePos]=true}
for _,conn in ipairs(connections) do for _,conn in ipairs(connections) do
if not omit or conn.x~=omit.x or conn.y~=omit.y or conn.z~=omit.z then
visitedNodes[conn.x..","..conn.y..","..conn.z]=true visitedNodes[conn.x..","..conn.y..","..conn.z]=true
addNodeToNetwork(conn,pos) addNodeToNetwork(conn,pos)
local def=minetest.registered_nodes[minetest.get_node(conn).name] local def=minetest.registered_nodes[minetest.get_node(conn).name]
@ -320,6 +322,7 @@ industrialtest.api.createNetworkMap=function(pos)
}) })
end end
end end
end
while #workers>0 do while #workers>0 do
for i=1,#workers do for i=1,#workers do
local worker=workers[i] local worker=workers[i]
@ -331,6 +334,7 @@ industrialtest.api.createNetworkMap=function(pos)
local directionAssigned=false local directionAssigned=false
local foundNewNode=false local foundNewNode=false
for _,conn in ipairs(connections) do for _,conn in ipairs(connections) do
if not omit or conn.x~=omit.x or conn.y~=omit.y or conn.z~=omit.z then
local serializedPos=conn.x..","..conn.y..","..conn.z local serializedPos=conn.x..","..conn.y..","..conn.z
if not visitedNodes[serializedPos] then if not visitedNodes[serializedPos] then
local def=minetest.registered_nodes[minetest.get_node(conn).name] local def=minetest.registered_nodes[minetest.get_node(conn).name]
@ -363,6 +367,7 @@ industrialtest.api.createNetworkMap=function(pos)
end end
end end
end end
end
if not foundNewNode then if not foundNewNode then
table.remove(workers,i) table.remove(workers,i)
break break
@ -389,12 +394,13 @@ industrialtest.api.removeNodeFromNetwork=function(pos,nodePos)
end end
end end
-- \brief Creates network map and writes it to node metadata at pos -- \brief Creates network map and writes it to node metadata at pos, optionally omitting node at omit
-- \param pos Vector -- \param pos Vector
-- \param (optional) omit Vector
-- \returns nil -- \returns nil
industrialtest.api.createNetworkMapForNode=function(pos) industrialtest.api.createNetworkMapForNode=function(pos,omit)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local network=industrialtest.api.createNetworkMap(pos) local network=industrialtest.api.createNetworkMap(pos,omit)
meta:set_string("industrialtest.network",minetest.serialize(network)) meta:set_string("industrialtest.network",minetest.serialize(network))
end end

View File

@ -21,6 +21,9 @@ cable.onConstruct=function(pos)
local connections=industrialtest.api.getConnections(pos) local connections=industrialtest.api.getConnections(pos)
for _,conn in ipairs(connections) do for _,conn in ipairs(connections) do
local meta=minetest.get_meta(conn) local meta=minetest.get_meta(conn)
if industrialtest.api.isNetworkMaster(meta) then
industrialtest.api.createNetworkMapForNode(conn)
else
local networks=industrialtest.api.isAttachedToNetwork(meta) local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then if networks then
for _,network in ipairs(networks) do for _,network in ipairs(networks) do
@ -28,6 +31,17 @@ cable.onConstruct=function(pos)
end end
end end
end end
end
end
cable.onDestruct=function(pos)
local meta=minetest.get_meta(pos)
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
industrialtest.api.createNetworkMapForNode(network,pos)
end
end
end end
local function registerCable(name,displayName,size,flow,registerInsulated) local function registerCable(name,displayName,size,flow,registerInsulated)
@ -104,6 +118,7 @@ local function registerCable(name,displayName,size,flow,registerInsulated)
"group:_industrialtest_cable" "group:_industrialtest_cable"
}, },
on_construct=cable.onConstruct, on_construct=cable.onConstruct,
on_destruct=cable.onDestruct,
_industrialtest_cableFlow=flow _industrialtest_cableFlow=flow
} }
if industrialtest.mtgAvailable then if industrialtest.mtgAvailable then