Module:ItemEconomy: Difference between revisions

From Melvor Idle
(First version)
 
m (Fix nil exceptions)
Line 1: Line 1:
local p = {}
local p = {}


function p._clamp(value, min, max)
local function clamp(value, min, max)
     return math.min(math.max(value, min), max)
     return math.min(math.max(value, min), max)
end
end
Line 18: Line 18:


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


     local preservationP = p._clamp(preservationChance, 0, 80) / 100
     local preservationP = clamp(preservationChance, 0, 80) / 100
     local duplicationP = p._clamp(duplicationChance, 0, 100) / 100
     local duplicationP = clamp(duplicationChance, 0, 100) / 100
     local extraItemP = math.max(extraItemChance, 0) / 100
     local extraItemP = math.max(extraItemChance, 0) / 100
      
      
     local output = targetOutput
     local output = outputAmount or 0
      
      
     return (output - output * preservationP) / (1 + duplicationP + extraItemP)
     return (output - output * preservationP) / (1 + duplicationP + extraItemP)

Revision as of 00:31, 1 March 2024

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