Make industrialtest.api.addPower accept negative power amount

This commit is contained in:
mrkubax10 2023-03-21 13:43:17 +01:00
parent f82d4730eb
commit f74f36aeb3
1 changed files with 9 additions and 8 deletions

17
api.lua
View File

@ -19,11 +19,15 @@ local S=minetest.get_translator("industrialtest")
industrialtest.api={}
industrialtest.api.maceratorRecipes={}
local function clamp(num,min,max)
return math.max(math.min(num,max),min)
end
-- \brief Adds power storage to metadata
-- \param capacity How much EU item/node can store
-- \param flow How much EU can flow in or out item/node per industrialtest.updateDelay
-- \param ioConfig Input/Output configuration in following side order: -X, +X, -Y, +Y, -Z, +Z
-- a - bidirectional, i - input, o - output
-- a - bidirectional, i - input, o - output
-- \returns nil
industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig)
meta:set_int("industrialtest.powerCapacity",capacity)
@ -93,13 +97,10 @@ end
industrialtest.api.addPower=function(meta,amount)
local powerAmount=meta:get_int("industrialtest.powerAmount")
local powerCapacity=meta:get_int("industrialtest.powerCapacity")
if powerAmount+amount>powerCapacity then
local addedAmount=powerCapacity-powerAmount
meta:set_int("industrialtest.powerAmount",powerCapacity)
return addedAmount
end
meta:set_int("industrialtest.powerAmount",powerAmount+amount)
return amount
local prevPowerAmount=powerAmount
powerAmount=clamp(powerAmount+amount,0,powerCapacity)
meta:set_int("industrialtest.powerAmount",powerAmount)
return powerAmount-prevPowerAmount
end
industrialtest.api.addPowerToItem=function(itemstack,amount)
local meta=itemstack:get_meta()