From 5ea90026306e76890bc4d7a6c423edf2b28c80cf Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Tue, 14 Nov 2023 20:49:05 +0100 Subject: [PATCH] Remove disconnected machines after cable is destructed --- api.lua | 98 +++++++++++++++++++++++++++++------------------------- cables.lua | 23 ++++++++++--- 2 files changed, 71 insertions(+), 50 deletions(-) diff --git a/api.lua b/api.lua index a606c37..8e78f88 100644 --- a/api.lua +++ b/api.lua @@ -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,22 +303,24 @@ industrialtest.api.createNetworkMap=function(pos) local serializedSourcePos=pos.x..","..pos.y..","..pos.z local visitedNodes={[serializedSourcePos]=true} for _,conn in ipairs(connections) do - visitedNodes[conn.x..","..conn.y..","..conn.z]=true - addNodeToNetwork(conn,pos) - local def=minetest.registered_nodes[minetest.get_node(conn).name] - if def.groups._industrialtest_cable then - table.insert(workers,{ - position=conn, - distance=1, - flow=def._industrialtest_cableFlow - }) - else - local meta=minetest.get_meta(conn) - table.insert(map,{ - position=conn, - distance=0, - flow=meta:get_int("industrialtest.powerFlow") - }) + 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] + if def.groups._industrialtest_cable then + table.insert(workers,{ + position=conn, + distance=1, + flow=def._industrialtest_cableFlow + }) + else + local meta=minetest.get_meta(conn) + table.insert(map,{ + position=conn, + distance=0, + flow=meta:get_int("industrialtest.powerFlow") + }) + end end end while #workers>0 do @@ -331,34 +334,36 @@ industrialtest.api.createNetworkMap=function(pos) local directionAssigned=false local foundNewNode=false for _,conn in ipairs(connections) do - local serializedPos=conn.x..","..conn.y..","..conn.z - if not visitedNodes[serializedPos] then - local def=minetest.registered_nodes[minetest.get_node(conn).name] - visitedNodes[serializedPos]=true - foundNewNode=true - addNodeToNetwork(conn,pos) - if def.groups._industrialtest_cable then - if directionAssigned then - table.insert(workers,{ + 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] + visitedNodes[serializedPos]=true + foundNewNode=true + addNodeToNetwork(conn,pos) + if def.groups._industrialtest_cable then + if directionAssigned then + table.insert(workers,{ + position=conn, + distance=worker.distance+1, + flow=clampFlow(conn,worker.flow) + }) + else + worker.position=conn + worker.distance=worker.distance+1 + worker.flow=clampFlow(conn,worker.flow) + directionAssigned=true + end + else + table.insert(map,{ position=conn, - distance=worker.distance+1, + distance=worker.distance, flow=clampFlow(conn,worker.flow) }) - else - worker.position=conn - worker.distance=worker.distance+1 - worker.flow=clampFlow(conn,worker.flow) - directionAssigned=true - end - else - table.insert(map,{ - position=conn, - distance=worker.distance, - flow=clampFlow(conn,worker.flow) - }) - if #connections==1 then - table.remove(workers,i) - break + if #connections==1 then + table.remove(workers,i) + break + end end end end @@ -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 diff --git a/cables.lua b/cables.lua index 8f151d2..bf864be 100644 --- a/cables.lua +++ b/cables.lua @@ -21,15 +21,29 @@ cable.onConstruct=function(pos) local connections=industrialtest.api.getConnections(pos) for _,conn in ipairs(connections) do local meta=minetest.get_meta(conn) - local networks=industrialtest.api.isAttachedToNetwork(meta) - if networks then - for _,network in ipairs(networks) do - industrialtest.api.createNetworkMapForNode(network) + 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 + industrialtest.api.createNetworkMapForNode(network) + 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) local definition={ description=S(displayName.." Cable"), @@ -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