Module:ScaledExperience

Module documentation
This documentation is transcluded from Module:ScaledExperience/doc. [edit] [history] [purge]
Module:ScaledExperience requires Module:Experience.
Module:ScaledExperience is required by Module:Arceuus library book calculator.

Usage

Helper module for experience calculations where each action's experience scales linearly with level, such as the genie lamps.

Functions

Determine Actions Needed

These functions return the number of actions required to reach a target goal. The only difference between these four functions is optional usage of a starting or target level instead of an experience value.

xp_to_xp_actions(startingExp, targetExp, scalar, base, intercept)
xp_to_level_actions(startingExp, targetLevel, scalar, base, intercept)
level_to_xp_actions(startingLevel, targetExp, scalar, base, intercept)
level_to_level_actions(startingLevel, targetExp, scalar, base, intercept)

Determine XP gained from Actions

These functions return the end experience after a certain number of actions have been taken. The only difference between these two functions is whether it starts from an experience value or a level value.

from_actions_xp(startingExp, actions, scalar, base, intercept)
from_actions_level(startingLevel, actions, scalar, base, intercept)

Parameters

startingExp, targetExp, startingLevel, and targetLevel

Self-explanatory, input the desired XP or Level depending on what function is used.

scalar

What multiplier of the skill level the experience scales by.

base [Optional]

A flat value to add that is not scaled with level.

intercept [Optional]

A level at which the experience starts scaling. At or below this level, only the base experience will be granted. You must add a base experience value to use this parameter, even if that value is 0.

Examples

  • Lamps from the genie random event reward 10 × the skill level, so only a scalar of 10 would need to be input.
  • Agility arena tickets reward 240 experience + 5 for every agility level over 40. Therefore the scalar, base, and intercept would be 5, 240, and 40 respectively.

local Experience = require( 'Module:Experience' )

local p = {}

-- Determine the XP an action gives at a given level
function p.action_xp_at_level(level, scalar, base, intercept)
	return base+(math.max(level-intercept,0)*scalar)
end

function p.xp_to_xp_actions(startingExp, targetExp, scalar, base, intercept)
	-- Accumulated exp starts off at whatever amount of exp the player has
	local accExp = startingExp
	local accActions = 0

	-- Since the amount of exp depends on the current level and the amount of starting xp
	-- is variable, each level needs to be checked.
	while accExp < targetExp do
		local currentLevel = Experience.level_at_xp({ args = { accExp } })

		-- Need to handle the case when you are already level 99, xp_to_level fails
		-- if attempting to get xp to level > 99
		local expToNext = 0
		if currentLevel ~= 99 then
			expToNext = Experience.xp_to_level({ args = { accExp, currentLevel+1 } })
		else
			expToNext = 200000000 - accExp
		end

		expToNext = math.min(expToNext, targetExp-accExp)

		local actionsToNext = math.ceil(expToNext/p.action_xp_at_level(currentLevel, scalar, base or 0, intercept or 0))

		accExp = accExp + actionsToNext*p.action_xp_at_level(currentLevel, scalar, base or 0, intercept or 0)
		accActions = accActions + actionsToNext
	end

	accExp = math.min(accExp, 200000000)

	local results = {
		endExp = accExp,
		endActions = accActions,
		endLevel = Experience.level_at_xp({ args = { accExp } }),
		startingExp = startingExp,
		startingLevel = Experience.level_at_xp({ args = {startingExp} })
	}

	return results
end

function p.xp_to_level_actions(startingExp, targetLevel, scalar, base, intercept)
	return p.xp_to_xp_actions(startingExp, Experience.xp_at_level({ args = {targetLevel}}), scalar, base or 0, intercept or 0)
end

function p.level_to_xp_actions(startingLevel, targetExp, scalar, base, intercept)
	return p.xp_to_xp_actions( Experience.xp_at_level({ args = {startingLevel}}), targetExp, scalar, base or 0, intercept or 0)
end

function p.level_to_level_actions(startingLevel, targetLevel, scalar)
	return p.xp_to_xp_actions( Experience.xp_at_level({ args = {startingLevel}}), Experience.xp_at_level({ args = {targetLevel}}), scalar, base or 0, intercept or 0)
end

function p.from_actions_xp(startingExp, actions, scalar, base, intercept)
	-- Accumulated exp starts off at whatever amount of exp the player has
	local accExp = startingExp
	local actionsLeft = actions

	-- Since the amount of exp depends on the current level and the amount of starting xp
	-- is variable, each level needs to be checked.
	while actionsLeft > 0 do
		local currentLevel = Experience.level_at_xp({ args = { accExp } })

		-- Need to handle the case when you are already level 99, xp_to_level fails
		-- if attempting to get xp to level > 99
		local expToNext = 0
		if currentLevel ~= 99 then
			expToNext = Experience.xp_to_level({ args = { accExp, currentLevel+1 } })
		else
			expToNext = 200000000 - accExp
		end

		local actionsToNext = math.ceil(expToNext/p.action_xp_at_level(currentLevel, scalar, base or 0, intercept or 0))
		
		if (actionsToNext <= actionsLeft) then
			accExp = accExp + actionsToNext*p.action_xp_at_level(currentLevel, scalar, base or 0, intercept or 0)
			actionsLeft = actionsLeft - actionsToNext
		else
			accExp = accExp + (actionsLeft*p.action_xp_at_level(currentLevel, scalar, base or 0, intercept or 0))
			break
		end
	end

	accExp = math.min(accExp, 200000000)

	local results = {
		endExp = accExp,
		endLevel = Experience.level_at_xp({ args = { accExp } }),
		startingExp = startingExp,
		startingLevel = Experience.level_at_xp({ args = {startingExp} })
	}
	return results
end


function p.from_actions_level(startingLevel, actions, scalar, base, intercept)
	return p.from_actions_xp(Experience.xp_at_level({ args = {startingLevel}}), actions, scalar, base or 0, intercept or 0)
end

return p