2,875
edits
(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 | -- @return ItemEconomy Returns a verified ItemEconomy object. | ||
function ItemEconomy: | function ItemEconomy:verify() | ||
-- Set default values if some are nil | -- Set default values if some are nil | ||
self.inputsPerAction = self.inputsPerAction or 1 | |||
self.outputsPerAction = self.outputsPerAction or 1 | |||
self.preservationChance = self.preservationChance or 0 | |||
self.duplicationChance = self.duplicationChance or 0 | |||
self.extraItemChance = self.extraItemChance or 0 | |||
self.extraItemAmount = self.extraItemAmount or 0 | |||
self.flatExtraItems = self.flatExtraItems or 0 | |||
self.extraBaseItemChance = self.extraBaseItemChance or 0 | |||
self.extraBaseItems = self.extraBaseItems or 0 | |||
-- Clamp economy values (clamp the verified copy values) | -- Clamp economy values (clamp the verified copy values) | ||
self.preservationChanceP = number.clamp(cpy.preservationChance, 0, 80) / 100 | |||
self.duplicationChanceP = number.clamp(cpy.duplicationChance, 0, 100) / 100 | |||
self.extraItemChanceP = number.clamp(cpy.extraItemChance, 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: | 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: | 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: | local rowEco = itemEconomy:copy():verify() | ||
-- Apply Ring of Wealth buffs. | -- Apply Ring of Wealth buffs. |
edits