diff --git a/api/network.lua b/api/network.lua index ce283da..c5df7ff 100644 --- a/api/network.lua +++ b/api/network.lua @@ -271,6 +271,34 @@ function industrialtest.api.getNetwork(meta) return minetest.deserialize(meta:get_string("industrialtest.network")) end +-- \brief Returns amount of power that will flow from specified network master +-- \param pos Vector +-- \returns number +function industrialtest.api.getFlowingCurrent(pos) + local meta=minetest.get_meta(pos) + if not industrialtest.api.hasPowerStorage(meta) or meta:get_int("industrialtest.powerAmount")==0 then + return 0 + end + local network=industrialtest.api.getNetwork(meta) + if not network then + return 0 + end + + local amount=meta:get_int("industrialtest.powerAmount") + if amount==0 then + return 0 + end + local demand=0 + for _,endpoint in ipairs(network) do + local endpointMeta=minetest.get_meta(endpoint.position) + if industrialtest.api.hasPowerStorage(endpointMeta) then + demand=demand+math.min(endpointMeta:get_int("industrialtest.powerCapacity")-endpointMeta:get_int("industrialtest.powerAmount"),endpoint.flow) + end + end + + return math.min(amount,demand) +end + -- \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