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)
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 (optional) omit Vector
-- \returns table with network map
industrialtest.api.createNetworkMap=function(pos)
industrialtest.api.createNetworkMap=function(pos,omit)
local workers={}
local map={}
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 visitedNodes={[serializedSourcePos]=true}
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
addNodeToNetwork(conn,pos)
local def=minetest.registered_nodes[minetest.get_node(conn).name]
@ -320,6 +322,7 @@ industrialtest.api.createNetworkMap=function(pos)
})
end
end
end
while #workers>0 do
for i=1,#workers do
local worker=workers[i]
@ -331,6 +334,7 @@ industrialtest.api.createNetworkMap=function(pos)
local directionAssigned=false
local foundNewNode=false
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
if not visitedNodes[serializedPos] then
local def=minetest.registered_nodes[minetest.get_node(conn).name]
@ -363,6 +367,7 @@ industrialtest.api.createNetworkMap=function(pos)
end
end
end
end
if not foundNewNode then
table.remove(workers,i)
break
@ -389,12 +394,13 @@ industrialtest.api.removeNodeFromNetwork=function(pos,nodePos)
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 (optional) omit Vector
-- \returns nil
industrialtest.api.createNetworkMapForNode=function(pos)
industrialtest.api.createNetworkMapForNode=function(pos,omit)
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))
end

View File

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