Level - 416rehman/mythic.obsidian GitHub Wiki
#gameplay The player only has XP property, this has to be converted to a player level, and vice versa. The formula is similar to Division 1's leveling where if player is level 1, then to reach level 2, the player XP must be atleast 1000 + (1000 * (2-1)) = 2000. Then for the player to reach level 3, they need atleast the all the previous level XPs, in this case 2000 (for level 2) added to 1000 + (1000 * (3-1)), in the end to reach level 3 the player must have 2000+3000=5000 xp.
//Converts a level such as 3 to its base XP. Level 1 starts at 0 xp
function levelToXp(level) {
let baseXp = 1000;
let xp = 0;
for (let i = 1; i < level; i++) {
xp += baseXp + (baseXp * (i - 1));
}
return xp;
}
//Converts a given XP amount, such as 2562, to its corresponding level
function xpToLevel(xp) {
let baseXp = 1000;
let level = 1;
while (xp >= baseXp) {
xp -= baseXp + (baseXp * (level - 1));
level++;
}
return level;
}
// Calculate xp needed from currentXP to reach desired level
function xpRequiredToReachLevel(currentXp, desiredLevel) {
return levelToXp(desiredLevel) - currentXp;
}
// Calculate xp needed from currentXP to reach next level
function xpRequiredToNextLevel(currentXp) {
let currentLevel = xpToLevel(currentXp);
return levelToXp(currentLevel + 1) - currentXp;
}
The main source of XP for leveling up is Quests. Quests can have one of the following types of XP rewards:
- Fixed XP: Grants fixed amount of XP. MUST HAVE A LEVEL REQUIREMENT to enforce the fixed XP amount does not result in the player ending up with more than 1 level-up upon completion.
Dynamic XP: Automatically grants the XP needed to reach the level cap by the time all Dynamic XP quests are completed. Never grants more than 1 level, so level requirement is not necessary to enforce the granted XP is not too much.
~~## Dynamic Leveling The quests will give dynamic XP based on how much XP is needed for the player to reach the level cap. This is achieved by dividing the amount of remaining dynamic-leveling quests with the xp needed by the player to reach the level cap. XP gained from quests will NEVER grant multiple levels, it will always be capped to the next level's 90%.
For example, if player has 400XP (Level 1), and a quest they are doing is going to grant them 10000XP (this will result in them being level 4, granting them 3 levels at once), this 10000XP will be clamped to make sure it only grants them XP upto 90% of their next level, which is level 2 (because the player is level 1 at the moment), so at the end of the quest, they will be granted enough to reach 90% of level 2.
NOTE: While the quest will only grant XP to bring the player XP to upto 90% of the NEXT level, the player may have earned more XP while doing the quest such as combat XP, opening chests and other actions, and this may result in them getting enough XP to bring their XP to 100% of the NEXT level, thus granting them more than 1 level.
const clamp = (number, min, max) => Math.max(min, Math.min(number, max));
function xpRewardOfQuest(levelCap, totalQuests, finishedQuests, playerXP){
return clamp(xpRequiredToReachLevel(playerXP, levelCap)/(totalQuests - finishedQuests), 0, xpRequiredToReachLevel(playerXP, xpToLevel(playerXP) + 1)*0.9)
}
let levelCap = 40
let totalQuests = 50
let playerXP= 400
let finishedQuests = 0
/** This function should simulate how much xp is granted to the player, console.log the output in a nice table like format */
function simulateQuestCompletion() {
console.log({levelCap, totalQuests, playerXP, finishedQuests})
for (let i = finishedQuests + 1; i <= totalQuests; i++) {
let questXP = xpRewardOfQuest(levelCap, totalQuests, finishedQuests, playerXP);
playerXP += questXP;
finishedQuests += 1;
console.log(`Quest ${i}: XP Reward: ${questXP}, XP Total: ${playerXP}, Level: ${xpToLevel(playerXP)}`);
let actionXP = 0
while (Math.random() > 0.5) {
actionXP += Math.random() * 100
}
if (actionXP > 0) {
playerXP += actionXP;
console.log(`ActionXP: XP Reward: ${actionXP}, XP Total: ${playerXP}, Level: ${xpToLevel(playerXP)}`);
}
}
}
const reset = () => {
levelCap = 40
totalQuests = 50
playerXP= 400
finishedQuests = 0
}
simulateQuestCompletion()
Total Quests MUST be greater than levelCap. (if level cap is 40, make sure there are atleast 40 quests)- On every level up,
- they heal to full
- and their skill-tree is checked for any skills they haven't unlocked yet, and skill points are rewarded. I.e if level cap is 10, and there are 30 locked skills, and the players current level is 4, the player is rewarded (int) (30/(10-4)) skill points to make sure they get all the skill points they will need by the time they hit level cap. If there are no skill points to be unlocked, no skill points are rewarded.
- Enemies spawn with levels according to their region. I.e a level 20 region will spawn level 20 enemies.
- Regions will always be upscaled to the player level, but never downscaled. I.e if the region is level 10, and the player is level 20, the region will be upscaled to level 20.
- The upscaling of regions is to ensure the player constantly has a challenge, and meaningful loot drops from enemies.