Anonymous

Module:ItemEconomy: Difference between revisions

From Melvor Idle
m
Add deep copy method instead of implied copy.
(Apply sandbox changes)
m (Add deep copy method instead of implied copy.)
Line 36: Line 36:
-- If any economy value is nil, it sets a default value.
-- If any economy value is nil, it sets a default value.
-- If any economy value is out of range, clamp it to the nearest valid range.
-- If any economy value is out of range, clamp it to the nearest valid range.
-- @return ItemEconomy Returns a verified copy of the ItemEconomy object.
-- @return ItemEconomy Returns a verified ItemEconomy object.
function ItemEconomy:getVerifiedEconomy()
function ItemEconomy:verify()
local cpy = {}
     -- Set default values if some are nil
     -- Set default values if some are nil
     cpy.inputsPerAction = self.inputsPerAction or 1
     self.inputsPerAction = self.inputsPerAction or 1
     cpy.outputsPerAction = self.outputsPerAction or 1
     self.outputsPerAction = self.outputsPerAction or 1
     cpy.preservationChance = self.preservationChance or 0
     self.preservationChance = self.preservationChance or 0
     cpy.duplicationChance = self.duplicationChance or 0
     self.duplicationChance = self.duplicationChance or 0
     cpy.extraItemChance = self.extraItemChance or 0
     self.extraItemChance = self.extraItemChance or 0
     cpy.extraItemAmount = self.extraItemAmount or 0
     self.extraItemAmount = self.extraItemAmount or 0
     cpy.flatExtraItems = self.flatExtraItems or 0
     self.flatExtraItems = self.flatExtraItems or 0
     cpy.extraBaseItemChance = self.extraBaseItemChance or 0
     self.extraBaseItemChance = self.extraBaseItemChance or 0
     cpy.extraBaseItems = self.extraBaseItems or 0
     self.extraBaseItems = self.extraBaseItems or 0
      
      
     -- Clamp economy values (clamp the verified copy values)
     -- Clamp economy values (clamp the verified copy values)
     cpy.preservationChanceP = number.clamp(cpy.preservationChance, 0, 80) / 100
     self.preservationChanceP = number.clamp(cpy.preservationChance, 0, 80) / 100
     cpy.duplicationChanceP = number.clamp(cpy.duplicationChance, 0, 100) / 100
     self.duplicationChanceP = number.clamp(cpy.duplicationChance, 0, 100) / 100
     cpy.extraItemChanceP = number.clamp(cpy.extraItemChance, 0, 100) / 100
     self.extraItemChanceP = number.clamp(cpy.extraItemChance, 0, 100) / 100
     cpy.extraBaseItemChanceP = number.clamp(cpy.extraBaseItemChance, 0, 100) / 100
     self.extraBaseItemChanceP = number.clamp(cpy.extraBaseItemChance, 0, 100) / 100
   
    return self
end
 
--- Creates a deep copy of ItemEconomy to prevent clashes with the caller.
function ItemEconomy:copy()
    local cpy = {}
    for k, v in pairs(self) do cpy[k] = v end
      
      
     return setmetatable(cpy, getmetatable(self))
     return setmetatable(cpy, getmetatable(self))
Line 72: Line 80:
-- @return (number) The estimated output of the crafting process.
-- @return (number) The estimated output of the crafting process.
function p.estimatedOutput(inputAmount, itemEconomy)
function p.estimatedOutput(inputAmount, itemEconomy)
local eco = itemEconomy:getVerifiedEconomy()
local eco = itemEconomy:verify()


-- Equal inputAmount to inputsPerAction to get 1 baseAction
-- Equal inputAmount to inputsPerAction to get 1 baseAction
Line 115: Line 123:


function p.estimatedInputVerbose(outputAmount, itemEconomy)
function p.estimatedInputVerbose(outputAmount, itemEconomy)
local eco = itemEconomy:getVerifiedEconomy()
local eco = itemEconomy:verify()


outputAmount = outputAmount or eco.outputsPerAction
outputAmount = outputAmount or eco.outputsPerAction
Line 132: Line 140:
-- @return TRUE if the Ring of Wealth is more beneficial than harmful.
-- @return TRUE if the Ring of Wealth is more beneficial than harmful.
function p.ringOfWealthHasEffect(itemEconomy)
function p.ringOfWealthHasEffect(itemEconomy)
local rowEco = itemEconomy:getVerifiedEconomy()
local rowEco = itemEconomy:copy():verify()
-- Apply Ring of Wealth buffs.
-- Apply Ring of Wealth buffs.
2,875

edits