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

17
api.lua
View File

@ -19,11 +19,15 @@ local S=minetest.get_translator("industrialtest")
industrialtest.api={} industrialtest.api={}
industrialtest.api.maceratorRecipes={} industrialtest.api.maceratorRecipes={}
local function clamp(num,min,max)
return math.max(math.min(num,max),min)
end
-- \brief Adds power storage to metadata -- \brief Adds power storage to metadata
-- \param capacity How much EU item/node can store -- \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 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 -- \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 -- \returns nil
industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig) industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig)
meta:set_int("industrialtest.powerCapacity",capacity) meta:set_int("industrialtest.powerCapacity",capacity)
@ -93,13 +97,10 @@ end
industrialtest.api.addPower=function(meta,amount) industrialtest.api.addPower=function(meta,amount)
local powerAmount=meta:get_int("industrialtest.powerAmount") local powerAmount=meta:get_int("industrialtest.powerAmount")
local powerCapacity=meta:get_int("industrialtest.powerCapacity") local powerCapacity=meta:get_int("industrialtest.powerCapacity")
if powerAmount+amount>powerCapacity then local prevPowerAmount=powerAmount
local addedAmount=powerCapacity-powerAmount powerAmount=clamp(powerAmount+amount,0,powerCapacity)
meta:set_int("industrialtest.powerAmount",powerCapacity) meta:set_int("industrialtest.powerAmount",powerAmount)
return addedAmount return powerAmount-prevPowerAmount
end
meta:set_int("industrialtest.powerAmount",powerAmount+amount)
return amount
end end
industrialtest.api.addPowerToItem=function(itemstack,amount) industrialtest.api.addPowerToItem=function(itemstack,amount)
local meta=itemstack:get_meta() local meta=itemstack:get_meta()