Module:Sandbox/User:MxFox/LinearExperience

From Illerai

This is the current revision of this page, as edited by Mark (Sọ̀rọ̀ | contribs) at 21:59, 4 November 2024 (1 revision imported). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:MxFox/LinearExperience/doc

local Experience = require( 'Module:Experience' )

local p = {}

function p.xp_to_xp_actions(startingExp, targetExp, scalar)
	-- 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/(currentLevel*scalar))

		accExp = accExp + actionsToNext*currentLevel*scalar
		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)
	return p.xp_to_xp_actions(startingExp, Experience.xp_at_level({ args = {targetLevel}}), scalar)
end

function p.level_to_xp_actions(startingLevel, targetExp, scalar)
	return p.xp_to_xp_actions( Experience.xp_at_level({ args = {startingLevel}}), targetExp, scalar)
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)
end

function p.from_actions_xp(startingExp, actions, scalar)
	-- 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/(currentLevel*scalar))
		
		if (actionsToNext <= actionsLeft) then
			accExp = accExp + actionsToNext*currentLevel*scalar
			actionsLeft = actionsLeft - actionsToNext
		else
			accExp = accExp + (actionsLeft*currentLevel*scalar)
			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)
	return p.from_actions_xp(Experience.xp_at_level({ args = {startingLevel}}), actions, scalar)
end

return p