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