Anonymous

Module:ItemEconomy: Difference between revisions

From Melvor Idle
m
Fix nil exceptions
(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)
2,875

edits