Implement power storage nodes

This commit is contained in:
2023-04-18 21:48:08 +02:00
parent 8e322b3d79
commit 432fd72597
2 changed files with 188 additions and 16 deletions

50
api.lua
View File

@@ -201,6 +201,21 @@ industrialtest.api.transferPowerToItem=function(srcMeta,itemstack,amount)
srcMeta:set_int("industrialtest.powerAmount",srcMeta:get_int("industrialtest.powerAmount")-actualFlow)
return actualFlow
end
-- \brief Adds power to destination metadata while subtracting it from source itemstack
-- \param srcItemstack ItemStack from which subtract power
-- \param meta MetaDataRef to which add power
-- \param amount How much power should be transferred
-- \returns How much of power was actually transferred
industrialtest.api.transferPowerFromItem=function(srcItemstack,meta,amount)
local srcMeta=srcItemstack:get_meta()
local currentFlow=math.min(srcMeta:get_int("industrialtest.powerAmount"),amount)
if currentFlow==0 then
return 0
end
local actualFlow=industrialtest.api.addPower(meta,currentFlow)
industrialtest.api.addPowerToItem(srcItemstack,-actualFlow)
return actualFlow
end
-- \brief Transfers power from source node to all neighbouring nodes
-- \param pos Vector with position of source node
-- \returns two values: true if any neighbouring node has room for more power, false otherwise
@@ -241,24 +256,27 @@ industrialtest.api.powerFlow=function(pos)
local roomAvailable=false
local transferred=false
for key,value in ipairs(neighbours) do
if industrialtest.api.isPowerOutput(meta,key) and value~=0 then
if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then
transferred=true
end
local def=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name]
if def then
local updateFormspec=def._industrialtest_updateFormspec
if updateFormspec then
updateFormspec(neighbourPositions[key])
if value~=0 then
local normalizedKey=industrialtest.api.normalizeSide(pos,key)
if industrialtest.api.isPowerOutput(meta,normalizedKey) then
if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then
transferred=true
end
local onPowerFlow=def._industrialtest_onPowerFlow
if onPowerFlow and transferred then
onPowerFlow(neighbourPositions[key],industrialtest.api.getOppositeSide(key))
local def=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name]
if def then
local updateFormspec=def._industrialtest_updateFormspec
if updateFormspec then
updateFormspec(neighbourPositions[key])
end
local onPowerFlow=def._industrialtest_onPowerFlow
if onPowerFlow and transferred then
onPowerFlow(neighbourPositions[key],industrialtest.api.getOppositeSide(key))
end
end
minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay)
if not industrialtest.api.isFullyCharged(value) then
roomAvailable=true
end
end
minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay)
if not industrialtest.api.isFullyCharged(value) then
roomAvailable=true
end
end
end