Module:ItemEconomy/Sandbox: Difference between revisions

no edit summary
(Input calculation draft)
No edit summary
 
(One intermediate revision by the same user not shown)
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 The ItemEconomy instance with verified and updated economy values.
-- @return ItemEconomy Returns a verified copy of the ItemEconomy object.
function ItemEconomy:verify()
function ItemEconomy:getVerifiedEconomy()
local cpy = {}
     -- Set default values if some are nil
     -- Set default values if some are nil
     self.inputsPerAction = self.inputsPerAction or 1
     cpy.inputsPerAction = self.inputsPerAction or 1
     self.outputsPerAction = self.outputsPerAction or 1
     cpy.outputsPerAction = self.outputsPerAction or 1
     self.preservationChance = self.preservationChance or 0
     cpy.preservationChance = self.preservationChance or 0
     self.duplicationChance = self.duplicationChance or 0
     cpy.duplicationChance = self.duplicationChance or 0
     self.extraItemChance = self.extraItemChance or 0
     cpy.extraItemChance = self.extraItemChance or 0
     self.extraItemAmount = self.extraItemAmount or 0
     cpy.extraItemAmount = self.extraItemAmount or 0
     self.flatExtraItems = self.flatExtraItems or 0
     cpy.flatExtraItems = self.flatExtraItems or 0
     self.extraBaseItemChance = self.extraBaseItemChance or 0
     cpy.extraBaseItemChance = self.extraBaseItemChance or 0
     self.extraBaseItems = self.extraBaseItems or 0
     cpy.extraBaseItems = self.extraBaseItems or 0
      
      
     -- Clamp economy values
     -- Clamp economy values (clamp the verified copy values)
     self.preservationChanceP = number.clamp(self.preservationChance, 0, 80) / 100
     cpy.preservationChanceP = number.clamp(cpy.preservationChance, 0, 80) / 100
     self.duplicationChanceP = number.clamp(self.duplicationChance, 0, 100) / 100
     cpy.duplicationChanceP = number.clamp(cpy.duplicationChance, 0, 100) / 100
     self.extraItemChanceP = number.clamp(self.extraItemChance, 0, 100) / 100
     cpy.extraItemChanceP = number.clamp(cpy.extraItemChance, 0, 100) / 100
     self.extraBaseItemChanceP = number.clamp(self.extraBaseItemChance, 0, 100) / 100
     cpy.extraBaseItemChanceP = number.clamp(cpy.extraBaseItemChance, 0, 100) / 100
      
      
     return self
     return setmetatable(cpy, getmetatable(self))
end
end


--- Estimates the output multiplier of a crafting process based on ItemEconomy values.
--- Calculates a ratio between input to output.
-- @param itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @param itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @return (number) The estimated output multiplier of the crafting process.
-- @return (number) The output ratio of the crafting process.
function p.estimatedOutputMultiplier(itemEconomy)
function p.estimatedOutputMultiplier(itemEconomy)
local minOutput = p.estimatedOutput(nil, itemEconomy)
local minOutput = p.estimatedOutput(nil, itemEconomy)
Line 71: Line 72:
-- @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)
eco = itemEconomy:verify()
local eco = itemEconomy:getVerifiedEconomy()


-- Equal inputAmount to inputsPerAction to get 1 baseAction
-- Equal inputAmount to inputsPerAction to get 1 baseAction
inputAmount = inputAmount or eco.inputsPerAction
inputAmount = inputAmount or eco.inputsPerAction
local baseActions = math.floor(inputAmount / eco.inputsPerAction)
local baseActions = inputAmount / eco.inputsPerAction
-- Can't craft with what you don't have...
if inputAmount < eco.inputsPerAction then
return 0
end
-- Calculate the effective actions taken.
-- Calculate the effective actions taken.
Line 88: Line 94:
end
end


--- Calculates a ratio between output to input.
-- @param itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @return (number) The input ratio of the crafting process.
function p.estimatedInputMultiplier(itemEconomy)
function p.estimatedInputMultiplier(itemEconomy)
local minInput = p.estimatedInput(nil, itemEconomy)
-- I'm fairly certain that the following holds true in any case.
return minInput / itemEconomy.outputsPerAction
return 1 / p.estimatedOutputMultiplier(itemEconomy)
end
end


--- Estimates the input required for a given output amount in a crafting process based on ItemEconomy values.
-- This function provides a detailed breakdown of the calculation process.
-- @param outputAmount The total amount of output items desired.
-- @param itemEconomy The ItemEconomy object containing ItemEconomy values.
-- @return The estimated input required for the given output amount.
function p.estimatedInput(outputAmount, itemEconomy)
function p.estimatedInput(outputAmount, itemEconomy)
eco = itemEconomy:verify()
-- Again, fairly certain Input is just 1 / Output.
-- Replace with p.estimatedInputVerbose is this ever isn't the case.
local inputMultiplier =  1 / p.estimatedOutputMultiplier(itemEconomy)
return math.max(outputAmount * inputMultiplier, itemEconomy.inputsPerAction)
end
 
function p.estimatedInputVerbose(outputAmount, itemEconomy)
local eco = itemEconomy:getVerifiedEconomy()


outputAmount = outputAmount or eco.outputsPerAction
outputAmount = outputAmount or eco.outputsPerAction
Line 107: Line 128:
end
end


--
--- Calculates if equipping the Ring Of Wealth will result in more items for the player.
-- Calculates if equipping the Ring Of Wealth will result in more items for the player.
-- @param itemEconomy The ItemEconomy object containing ItemEconomy values.
function p.ringOfWealthHasEffect()
-- @return TRUE if the Ring of Wealth is more beneficial than harmful.
function p.ringOfWealthHasEffect(itemEconomy)
local rowEco = itemEconomy:getVerifiedEconomy()
-- Apply Ring of Wealth buffs.
rowEco.preservationChance = rowEco.preservationChance - 3
rowEco.duplicationChance = rowEco.duplicationChance + 7
local rowMultiplier = p.estimatedOutputMultiplier(rowEco)
local playerMultiplier = p.estimatedOutputMultiplier(itemEconomy)
return rowMultiplier > playerMultiplier
end
end


p.ItemEconomy = ItemEconomy
p.ItemEconomy = ItemEconomy


-- Test stuff....
function p.Test()
local eco = ItemEconomy:new()
eco.inputsPerAction = 2
eco.outputsPerAction = 4
eco.flatExtraItems = 0
eco.preservationChance = 0
eco.duplicationChance = 0
eco.extraBaseItems = 0
eco.extraBaseItemChance = 0
mw.log(p.estimatedOutputMultiplier(eco))
mw.log(p.estimatedInputMultiplier(eco))
end
return p
return p
964

edits