Module:ItemEconomy/Sandbox: Difference between revisions

From Melvor Idle
No edit summary
(Input calculation draft)
Line 24: Line 24:
}
}


-- Make constructor so external modules can more easily use the
--- Constructor function for creating new ItemEconomy instances.
-- ItemEconomy object, should they desire
-- @return ItemEconomy An instance of the ItemEconomy class.
function ItemEconomy:new()
function ItemEconomy:new()
     local newObj = {}
     local newObj = {}
Line 33: Line 33:
end
end


local function verifyEconomyValues(itemEconomy)
--- Verifies and updates the economy values of the ItemEconomy instance.
-- Set default values if some are nil
-- If any economy value is nil, it sets a default value.
itemEconomy.inputsPerAction = itemEconomy.inputsPerAction or 1
-- If any economy value is out of range, clamp it to the nearest valid range.
itemEconomy.outputsPerAction = itemEconomy.outputsPerAction or 1
-- @return ItemEconomy The ItemEconomy instance with verified and updated economy values.
     itemEconomy.preservationChance = itemEconomy.preservationChance or 0
function ItemEconomy:verify()
     itemEconomy.duplicationChance = itemEconomy.duplicationChance or 0
    -- Set default values if some are nil
     itemEconomy.extraItemChance = itemEconomy.extraItemChance or 0
    self.inputsPerAction = self.inputsPerAction or 1
     itemEconomy.extraItemAmount = itemEconomy.extraItemAmount or 0
    self.outputsPerAction = self.outputsPerAction or 1
     itemEconomy.flatExtraItems = itemEconomy.flatExtraItems or 0
     self.preservationChance = self.preservationChance or 0
     itemEconomy.extraBaseItemChance = itemEconomy.extraBaseItemChance or 0
     self.duplicationChance = self.duplicationChance or 0
     itemEconomy.extraBaseItems = itemEconomy.extraBaseItems or 0
     self.extraItemChance = self.extraItemChance or 0
     self.extraItemAmount = self.extraItemAmount or 0
     self.flatExtraItems = self.flatExtraItems or 0
     self.extraBaseItemChance = self.extraBaseItemChance or 0
     self.extraBaseItems = self.extraBaseItems or 0
      
      
     -- Clamp economy values
     -- Clamp economy values
     itemEconomy.preservationChance = number.clamp(itemEconomy.preservationChance, 0, 80) / 100
     self.preservationChanceP = number.clamp(self.preservationChance, 0, 80) / 100
     itemEconomy.duplicationChance = number.clamp(itemEconomy.duplicationChance, 0, 100) / 100
     self.duplicationChanceP = number.clamp(self.duplicationChance, 0, 100) / 100
     itemEconomy.extraItemChance = number.clamp(itemEconomy.extraItemChance, 0, 100) / 100
     self.extraItemChanceP = number.clamp(self.extraItemChance, 0, 100) / 100
     itemEconomy.extraBaseItemChance = number.clamp(itemEconomy.extraBaseItemChance, 0, 100) / 100
     self.extraBaseItemChanceP = number.clamp(self.extraBaseItemChance, 0, 100) / 100
      
      
     return itemEconomy
     return self
end
end


Line 58: Line 62:
-- @return (number) The estimated output multiplier of the crafting process.
-- @return (number) The estimated output multiplier of the crafting process.
function p.estimatedOutputMultiplier(itemEconomy)
function p.estimatedOutputMultiplier(itemEconomy)
return p.estimatedOutput(nil, inputPerAction, outputPerAction, itemEconomy)
local minOutput = p.estimatedOutput(nil, itemEconomy)
return minOutput / itemEconomy.inputsPerAction
end
end


Line 66: Line 71:
-- @return (number) The estimated output of the crafting process.
-- @return (number) The estimated output of the crafting process.
function p.estimatedOutput(inputAmount, itemEconomy)
function p.estimatedOutput(inputAmount, itemEconomy)
eco = verifyEconomyValues(itemEconomy)
eco = itemEconomy:verify()


-- Equal inputAmount to inputsPerAction to get 1 baseAction
-- Equal inputAmount to inputsPerAction to get 1 baseAction
Line 73: Line 78:
-- Calculate the effective actions taken.
-- Calculate the effective actions taken.
local totalActions = baseActions / (1 - eco.preservationChance)
local totalActions = baseActions / (1 - eco.preservationChanceP)
-- Calculates extra items that can't be doubled.
-- Calculates extra items that can't be doubled.
local extraItems = (totalActions * eco.extraItemChance * eco.extraItemAmount) + (totalActions * eco.flatExtraItems)
local extraItems = (totalActions * eco.extraItemChanceP * eco.extraItemAmount) + (totalActions * eco.flatExtraItems)
-- Calculates extra items that CAN be doubled.
-- Calculates extra items that CAN be doubled.
local baseItems = (eco.outputsPerAction + eco.extraBaseItemChance * eco.extraBaseItems)
local baseItems = (eco.outputsPerAction + eco.extraBaseItemChanceP * eco.extraBaseItems)
-- Total output
-- Total output
return (totalActions * baseItems * (1 + eco.duplicationChance)) + extraItems
return (totalActions * baseItems * (1 + eco.duplicationChanceP)) + extraItems
end
end


function p.estimatedInput(outputAmount, preservationChance, duplicationChance, extraItemChance)
function p.estimatedInputMultiplier(itemEconomy)
local minInput = p.estimatedInput(nil, itemEconomy)
return minInput / itemEconomy.outputsPerAction
end
 
function p.estimatedInput(outputAmount, itemEconomy)
eco = itemEconomy:verify()
 
outputAmount = outputAmount or eco.outputsPerAction
local preserveAmount = outputAmount * eco.inputsPerAction * (1 - eco.preservationChanceP)
local duplicateAmount = eco.outputsPerAction + eco.outputsPerAction * eco.duplicationChanceP
local extraBaseItems = eco.extraBaseItemChanceP * eco.extraBaseItems + eco.extraBaseItemChanceP * eco.extraBaseItems * eco.duplicationChanceP
local extraItems = eco.extraItemChanceP * eco.extraItemAmount


-- Total input required for given output.
local actualInput = preserveAmount / (duplicateAmount + extraBaseItems + extraItems + eco.flatExtraItems)
return math.max(actualInput, eco.inputsPerAction)
end
end


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


Line 101: Line 117:
function p.Test()
function p.Test()
local eco = ItemEconomy:new()
local eco = ItemEconomy:new()
eco.inputsPerAction = 2
eco.outputsPerAction = 4
eco.flatExtraItems = 0
eco.flatExtraItems = 0
eco.preservationChance = 0
eco.preservationChance = 0
eco.duplicationChance = 50
eco.duplicationChance = 0
eco.extraBaseItems = 1
eco.extraBaseItems = 0
eco.extraBaseItemChance = 100
eco.extraBaseItemChance = 0
return p.estimatedOutput(1000, eco)
 
mw.log(p.estimatedOutputMultiplier(eco))
mw.log(p.estimatedInputMultiplier(eco))
end
end
return p
return p

Revision as of 00:13, 18 March 2024

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

local number = require('Module:Number')

local p = {}

local ItemEconomy = {
	-- Amount of items required per action.
	inputsPerAction,
	-- Amount of items created per action. (ie. 3 bolts per action).
	outputsPerAction,
	-- 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
}

--- Constructor function for creating new ItemEconomy instances.
-- @return ItemEconomy An instance of the ItemEconomy class.
function ItemEconomy:new()
    local newObj = {}
    
    self.__index = self
    return setmetatable(newObj, self)
end

--- Verifies and updates the economy values of the ItemEconomy instance.
-- If any economy value is nil, it sets a default value.
-- If any economy value is out of range, clamp it to the nearest valid range.
-- @return ItemEconomy The ItemEconomy instance with verified and updated economy values.
function ItemEconomy:verify()
    -- Set default values if some are nil
    self.inputsPerAction = self.inputsPerAction or 1
    self.outputsPerAction = self.outputsPerAction or 1
    self.preservationChance = self.preservationChance or 0
    self.duplicationChance = self.duplicationChance or 0
    self.extraItemChance = self.extraItemChance or 0
    self.extraItemAmount = self.extraItemAmount or 0
    self.flatExtraItems = self.flatExtraItems or 0
    self.extraBaseItemChance = self.extraBaseItemChance or 0
    self.extraBaseItems = self.extraBaseItems or 0
    
    -- Clamp economy values
    self.preservationChanceP = number.clamp(self.preservationChance, 0, 80) / 100
    self.duplicationChanceP = number.clamp(self.duplicationChance, 0, 100) / 100
    self.extraItemChanceP = number.clamp(self.extraItemChance, 0, 100) / 100
    self.extraBaseItemChanceP = number.clamp(self.extraBaseItemChance, 0, 100) / 100
    
    return self
end

--- Estimates the output multiplier of a crafting process based on ItemEconomy values.
-- @param itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @return (number) The estimated output multiplier of the crafting process.
function p.estimatedOutputMultiplier(itemEconomy)
	local minOutput = p.estimatedOutput(nil, itemEconomy)
	return minOutput / itemEconomy.inputsPerAction
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 itemEconomy (table) The ItemEconomy object containing ItemEconomy values.
-- @return (number) The estimated output of the crafting process.
function p.estimatedOutput(inputAmount, itemEconomy)
	eco = itemEconomy:verify()

	-- Equal inputAmount to inputsPerAction to get 1 baseAction
	inputAmount = inputAmount or eco.inputsPerAction
	local baseActions = math.floor(inputAmount / eco.inputsPerAction)
	
	-- Calculate the effective actions taken.
	local totalActions = baseActions / (1 - eco.preservationChanceP)
	-- Calculates extra items that can't be doubled.
	local extraItems = (totalActions * eco.extraItemChanceP * eco.extraItemAmount) + (totalActions * eco.flatExtraItems)
	-- Calculates extra items that CAN be doubled.
	local baseItems = (eco.outputsPerAction + eco.extraBaseItemChanceP * eco.extraBaseItems)
	
	-- Total output
	return (totalActions * baseItems * (1 + eco.duplicationChanceP)) + extraItems
end

function p.estimatedInputMultiplier(itemEconomy)
	local minInput = p.estimatedInput(nil, itemEconomy)
	return minInput / itemEconomy.outputsPerAction 
end

function p.estimatedInput(outputAmount, itemEconomy)
	eco = itemEconomy:verify()

	outputAmount = outputAmount or eco.outputsPerAction
	local preserveAmount = outputAmount * eco.inputsPerAction * (1 - eco.preservationChanceP)
	local duplicateAmount = eco.outputsPerAction + eco.outputsPerAction * eco.duplicationChanceP
	local extraBaseItems = eco.extraBaseItemChanceP * eco.extraBaseItems + eco.extraBaseItemChanceP * eco.extraBaseItems * eco.duplicationChanceP
	local extraItems = eco.extraItemChanceP * eco.extraItemAmount

	-- Total input required for given output.
	local actualInput = preserveAmount / (duplicateAmount + extraBaseItems + extraItems + eco.flatExtraItems)
	return math.max(actualInput, eco.inputsPerAction)
end

--
-- Calculates if equipping the Ring Of Wealth will result in more items for the player.
function p.ringOfWealthHasEffect()
end

p.ItemEconomy = ItemEconomy

-- Test stuff....
function p.Test()
	local eco = ItemEconomy:new()
	eco.inputsPerAction = 2
	eco.outputsPerAction = 4
	eco.flatExtraItems = 0
	eco.preservationChance = 0
	eco.duplicationChance = 0
	eco.extraBaseItems = 0
	eco.extraBaseItemChance = 0

	mw.log(p.estimatedOutputMultiplier(eco))
	mw.log(p.estimatedInputMultiplier(eco))
	
end
return p