industrialtest.api.createNetworkMap fixes

This commit is contained in:
mrkubax10 2023-11-13 10:33:15 +01:00
parent edc70ad028
commit 531604544a

90
api.lua
View File

@ -283,22 +283,32 @@ end
industrialtest.api.createNetworkMap=function(pos)
local workers={}
local map={}
local connections=industrialtest.api.getConnections(pos)
local connections=industrialtest.api.getConnections(pos,"i")
if #connections==0 then
return map
end
local serializedSourcePos=pos.x..","..pos.y..","..pos.z
local visitedNodes={[serializedSourcePos]=true}
for _,value in ipairs(connections) do
visitedNodes[value.x..","..value.y..","..value.z]=true
table.insert(workers,{
position=value,
distance=1,
})
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,
})
else
table.insert(map,{
position=conn,
distance=0
})
end
end
while #workers>0 do
for _,worker in ipairs(workers) do
local connections=industrialtest.api.getConnections(worker.position)
for i=1,#workers do
local worker=workers[i]
local connections=industrialtest.api.getConnections(worker.position,"i")
if #connections==0 then
table.remove(workers,i)
break
@ -311,7 +321,7 @@ industrialtest.api.createNetworkMap=function(pos)
local def=minetest.registered_nodes[minetest.get_node(conn).name]
visitedNodes[serializedPos]=true
foundNewNode=true
addNodeToNetwork(conn,serializedSourcePos)
addNodeToNetwork(conn,pos)
if def.groups._industrialtest_cable then
if directionAssigned then
table.insert(workers,{
@ -344,6 +354,37 @@ industrialtest.api.createNetworkMap=function(pos)
end
return map
end
-- \brief Creates network map and writes it to node metadata at pos
-- \param pos Vector
-- \returns nil
industrialtest.api.createNetworkMapForNode=function(pos)
local meta=minetest.get_meta(pos)
local network=industrialtest.api.createNetworkMap(pos)
meta:set_string("industrialtest.network",minetest.serialize(network))
end
-- \brief Returns true if meta contains network map, false otherwise
-- \param meta MetaDataRef
-- \returns bool
industrialtest.api.isNetworkMaster=function(meta)
return meta:contains("industrialtest.network")
end
-- \brief Returns network table if node containing meta belongs to any networks, false otherwise
-- \param meta MetaDataRef
-- \returns bool or table
industrialtest.api.isAttachedToNetwork=function(meta)
if not meta:contains("industrialtest.networks") then
return false
end
local networks=minetest.deserialize(meta:get_string("industrialtest.networks"))
if #networks==0 then
return false
end
return networks
end
-- \brief Starts node timer of neighbouring nodes with center node
-- \param pos Vector with position of center node
-- \returns nil
@ -358,10 +399,12 @@ end
industrialtest.api.getOppositeSide=function(side)
return (side%2==0 and side-1 or side+1)
end
-- \brief Returns connections of node with power storage
-- \param pos Vector with position of node with power storage
-- \returns Table with side numbers
industrialtest.api.getConnections=function(pos)
-- \brief Returns connections of node with power storage. If direction is "i" only input connections will be returned, if direction is "o" only output connections
-- will be returned, if it's not provided all connections will be returned.
-- \param pos Vector
-- \param (optional) direction string
-- \returns table
industrialtest.api.getConnections=function(pos,direction)
local result={}
local neighbourPositions={
vector.offset(pos,-1,0,0),
@ -372,15 +415,18 @@ industrialtest.api.getConnections=function(pos)
vector.offset(pos,0,0,1)
}
local sourceMeta=minetest.get_meta(pos)
for key,value in ipairs(neighbourPositions) do
local def=minetest.registered_nodes[minetest.get_node(value).name]
if def.groups._industrialtest_cable then
table.insert(result,value)
else
local meta=minetest.get_meta(value)
local sourceDef=minetest.registered_nodes[minetest.get_node(pos).name]
local directionOutput=(not direction or direction=="o")
local directionInput=(not direction or direction=="i")
for key,conn in ipairs(neighbourPositions) do
local meta=minetest.get_meta(conn)
local def=minetest.registered_nodes[minetest.get_node(conn).name]
local powerOutput=(sourceDef.groups._industrialtest_cable or industrialtest.api.isPowerOutput(sourceMeta,key))
local powerInput=(sourceDef.groups._industrialtest_cable or industrialtest.api.isPowerInput(sourceMeta,key))
if def.groups._industrialtest_cable or industrialtest.api.hasPowerStorage(meta) then
local side=industrialtest.api.getOppositeSide(key)
if def.groups._industrialtest_hasPowerInput and industrialtest.api.isPowerOutput(sourceMeta,key) and industrialtest.api.isPowerInput(meta,side) then
table.insert(result,value)
if (powerOutput and directionInput and (def.groups._industrialtest_cable or industrialtest.api.isPowerInput(meta,side))) or ((def.groups._industrialtest_cable or industrialtest.api.isPowerOutput(meta,side)) and powerInput and directionOutput) then
table.insert(result,conn)
end
end
end