Module:ItemEconomy

Revision as of 00:31, 1 March 2024 by Ricewind (talk | contribs) (Fix nil exceptions)

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

local p = {}

local function clamp(value, min, max)
    return math.min(math.max(value, min), max)
end

--
-- Calculates the expected output given the player's starting items and item economy modifiers.
-- PreservationChance (number from 0 - 80)
-- DuplicationChance (number from 0 - 100)
-- ExtraItemChance (number from 0 - ?)
--
function p.estimatedOutput(inputAmount, preservationChance, duplicationChance, extraItemChance)
	-- Default values to zero if they haven't been provided.
    preservationChance = preservationChance or 0
    duplicationChance = duplicationChance or 0
    extraItemChance = extraItemChance or 0

	-- Clamp values and turn them into decimals
    local preservationP = clamp(preservationChance, 0, 80) / 100
    local duplicationP = clamp(duplicationChance, 0, 100) / 100
    local extraItemP = math.max(extraItemChance, 0) / 100
    
    local input = inputAmount or 0
    
    local totalActions = input / (1 - preservationP)
    local extraItems = totalActions * extraItemP

    return (totalActions * (1 + duplicationP)) + extraItems
end

--
-- Calculates the expected input required for an output given the player's item economy modifiers.
-- PreservationChance (number from 0 - 80)
-- DuplicationChance (number from 0 - 100)
-- ExtraItemChance (number from 0 - ?)
--
function p.estimatedInput(outputAmount, preservationChance, duplicationChance, extraItemChance)
	-- Default values to zero if they haven't been provided.
    preservationChance = preservationChance or 0
    duplicationChance = duplicationChance or 0
    extraItemChance = extraItemChance or 0

    local preservationP = clamp(preservationChance, 0, 80) / 100
    local duplicationP = clamp(duplicationChance, 0, 100) / 100
    local extraItemP = math.max(extraItemChance, 0) / 100
    
    local output = outputAmount or 0
    
    return (output - output * preservationP) / (1 + duplicationP + extraItemP)
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)
	-- Default values to zero if they haven't been provided.
    preservationChance = preservationChance or 0
    duplicationChance = duplicationChance or 0
    extraItemChance = extraItemChance or 0
    
	local currentModifier = p.estimatedOutput(1, preservationChance, duplicationChance, extraItemChance)
	
	local rowPreserve = math.max(0, preservationChance - 3)
	local rowDupe = math.min(100, duplicationChance + 7)
	local rowModifier = p.estimatedOutput(1, rowPreserve, rowDupe, extraItemChance)
	
	return rowModifier > currentModifier
end

return p