Module:Calculator/ItemEconomy

From Melvor Idle
Revision as of 00:28, 1 March 2024 by Ricewind (talk | contribs) (Fix nil handling of input parameter)

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

local p = {}

local shared = require('Module:Shared')
local eco = require("Module:ItemEconomy")

ECOIN = "INPUT"
ECOOUT = "OUTPUT"

local function parseEconomy(eco)
	if eco == nil then
		return ECOOUT
	end
	
	if eco:upper() == ECOIN then
        return ECOIN
    elseif eco:upper() == ECOOUT then
        return ECOOUT
    end
    
    return ECOOUT
end

--
-- Returns the estimated output items and TRUE/FALSE if the Ring of Wealth has a benefit.
--
local function calculateOutput(itemAmount, pChance, dChance, eChance)
	local estimatedOutput = eco.estimatedOutput(itemAmount, pChance, dChance, eChance)
	local canUseRoW = eco.ringOfWealthHasEffect(pChance, dChance, eChance)
	
	return estimatedOutput, canUseRoW
end

--
-- Returns the estimated input items and TRUE/FALSE if the Ring of Wealth has a benefit.
--
local function calculateInput(itemAmount, pChance, dChance, eChance)
	local estimatedInput = eco.estimatedInput(itemAmount, pChance, dChance, eChance)
	
	local baseModifier = eco.estimatedInput(1, pChance, dChance, eChance)
	local rowModifier = eco.estimatedInput(1, pChance - 3, dChance + 7, eChance)

	return estimatedOutput, rowModifier > baseModifier
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

function p._main(args)
	local itemAmount = targetAmount
	local economyType = parseEconomy(args.economyType)
	
	local pChance = shared.toNumberOrDefault(args.preservationChance, 0)
	local dChance = shared.toNumberOrDefault(args.duplicationChance, 0)
	local eChance = shared.toNumberOrDefault(args.extraItemChance, 0)
	
	local result = 0
	local row = false
	
	if economyType == ECOOUT then
		result, row = calculateOutput(itemAmount, pChance, dChance, eChance)
	else
		result, row = calculateInput(itemAmount, pChance, dChance, eChance)
	end
	
	return result, row
end

return p