Make sure that power flow won't exceed network capabilities

This commit is contained in:
mrkubax10 2023-11-14 17:05:36 +01:00
parent 340c46bf48
commit 42130c90a7
2 changed files with 24 additions and 5 deletions

26
api.lua
View File

@ -239,7 +239,7 @@ industrialtest.api.powerFlow=function(pos)
local endpointMeta=minetest.get_meta(endpoint.position)
-- TODO: if supplying machine power flow is too large for receiving machine to handle then that machine should explode
local transferredToEndpoint=false
if industrialtest.api.transferPower(meta,endpointMeta,powerDistribution)>0 then
if industrialtest.api.transferPower(meta,endpointMeta,math.min(powerDistribution,endpoint.flow))>0 then
transferred=true
transferredToEndpoint=true
end
@ -277,6 +277,18 @@ local function addNodeToNetwork(pos,networkMasterPos)
meta:set_string("industrialtest.networks",minetest.serialize(networks))
end
local function clampFlow(pos,flow)
local def=minetest.registered_nodes[minetest.get_node(pos).name]
local newFlow
if def.groups and def.groups._industrialtest_cable then
newFlow=def._industrialtest_cableFlow
else
local meta=minetest.get_meta(pos)
newFlow=meta:get_int("industrialtest.powerFlow")
end
return math.min(flow,newFlow)
end
-- \brief Creates network map starting from node at pos
-- \param pos vector
-- \returns table with network map
@ -297,11 +309,14 @@ industrialtest.api.createNetworkMap=function(pos)
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
distance=0,
flow=meta:get_int("industrialtest.powerFlow")
})
end
end
@ -326,17 +341,20 @@ industrialtest.api.createNetworkMap=function(pos)
if directionAssigned then
table.insert(workers,{
position=conn,
distance=worker.distance+1
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
distance=worker.distance,
flow=clampFlow(conn,worker.flow)
})
if #connections==1 then
table.remove(workers,i)

View File

@ -103,7 +103,8 @@ local function registerCable(name,displayName,size,flow,registerInsulated)
"group:_industrialtest_hasPowerOutput",
"group:_industrialtest_cable"
},
on_construct=cable.onConstruct
on_construct=cable.onConstruct,
_industrialtest_cableFlow=flow
}
if industrialtest.mtgAvailable then
definition.groups={