Module:ItemEconomy/Sandbox

From Melvor Idle
< Module:ItemEconomy
Revision as of 21:38, 17 March 2024 by Ricewind (talk | contribs) (Rewrite output estimation functions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:ItemEconomy/Sandbox/doc

local number = require('Module:Number')

local p = {}

local ItemEconomy = {
	-- Amount of items created per action. (ie. 3 bolts per action).
	itemsPerAction,
	-- The chance from 0 to 80 to preserve resources.
	preservationChance,
	-- The chance from 0 to 100 to duplicate the crafting output.
	duplicationChance,
	-- The chance from 0 to 100 to get extra output items (not doubled).
	extraItemChance,
	-- The amount of extra items received, when the extraItemChance is rolled.
	extraItemAmount,
	-- The amount of extra items always received per action (not doubled).
	flatExtraItems,
	-- The chance to get extra base items (can be doubled).
	extraBaseItemChance,
	-- The amount of extra base items received.
	extraBaseItems
}

-- Make constructor so external modules can more easily use the 
-- ItemEconomy object, should they desire
function ItemEconomy:new()
    local newObj = {}
    
    self.__index = self
    return setmetatable(newObj, self)
end

local function verifyEconomyValues(itemEconomy)
	-- Set default values if some are nil
	itemEconomy.itemsPerAction = itemEconomy.itemsPerAction or 1
    itemEconomy.preservationChance = itemEconomy.preservationChance or 0
    itemEconomy.duplicationChance = itemEconomy.duplicationChance or 0
    itemEconomy.extraItemChance = itemEconomy.extraItemChance or 0
    itemEconomy.extraItemAmount = itemEconomy.extraItemAmount or 1
    itemEconomy.flatExtraItems = itemEconomy.flatExtraItems or 1
    itemEconomy.extraBaseItemChance = itemEconomy.extraBaseItemChance or 0
    itemEconomy.extraBaseItems = itemEconomy.extraBaseItems or 0
    
    -- Clamp economy values
    itemEconomy.preservationChance = number.clamp(itemEconomy.preservationChance, 0, 80) / 100
    itemEconomy.duplicationChance = number.clamp(itemEconomy.duplicationChance, 0, 100) / 100
    itemEconomy.extraItemChance = number.clamp(itemEconomy.extraItemChance, 0, 100) / 100
    itemEconomy.extraBaseItems = number.clamp(itemEconomy.extraBaseItems, 0, 100) / 100
    
    return itemEconomy
end

--- Estimates the output multiplier of a crafting process based on ItemEconomy values.
-- @param inputPerAction (number) The number of input items required per crafting action.
-- @param outputPerAction (number) The number of output items produced per crafting action.
-- @param itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @return (number) The estimated output multiplier of the crafting process.
function p.estimatedOutputMultiplier(inputPerAction, outputPerAction, itemEconomy)
	-- Equal the inputAmount and inputPerAction, so the base action count is 1.
	return p.estimatedOutput(inputPerAction, inputPerAction, outputPerAction, itemEconomy)
end

--- Estimates the output of a crafting process based on input and ItemEconomy values.
-- @param inputAmount (number) The total amount of input items available for crafting.
-- @param inputPerAction (number) The number of input items required per crafting action.
-- @param outputPerAction (number) The number of output items produced per crafting action.
-- @param itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @return (number) The estimated output of the crafting process.
function p.estimatedOutput(inputAmount, inputPerAction, outputPerAction, itemEconomy)
	eco = verifyEconomyValues(itemEconomy)

	inputPerAction = inputPerAction or 1
	inputAmount = inputAmount or inputPerAction
	outputPerAction = outputPerAction or 1
	baseActions = inputAmount / itemsPerAction
	
	-- Calculate the effective actions taken.
	local totalActions = baseActions / (1 - eco.preservationChance)
	-- Calculates extra items that can't be doubled.
	local extraItems = (totalActions * eco.extraItemChance * eco.extraItemAmount) + (totalActions * eco.flatExtraItems)
	-- Calculates extra items that CAN be doubled.
	local baseItems = (outputPerAction + eco.extraBaseItemChance * eco.extraBaseItems)
	
	-- Total output
	local output = (1 + (eco.extraBaseItemChance * eco.extraBaseItems)) * (1 + eco.duplicationChance)
end

function p.estimatedInput(outputAmount, preservationChance, duplicationChance, extraItemChance)

end

--
-- Calculates if equipping the Ring Of Wealth will result in more items for the player.
-- PreservationChance (number from 0 - 80)
-- DuplicationChance (number from 0 - 100)
-- ExtraItemChance (number from 0 - ?)
--
function p.ringOfWealthHasEffect(preservationChance, duplicationChance, extraItemChance)
end

p.ItemEconomy = ItemEconomy

return p