Skip to content

Commit

Permalink
Merge pull request #249 from Hc747/skill-exp-lookup
Browse files Browse the repository at this point in the history
skill-exp-lookup
  • Loading branch information
Tynarus authored Feb 6, 2021
2 parents ca39a53 + 139e1d2 commit 6a71ec4
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/world/actor/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ class SkillShortcuts implements SkillShortcutMap {

export class Skills extends SkillShortcuts {

private static EXPERIENCE_LOOKUP_TABLE: number[] = [
0,83,174,276,388,512,650,801,969,1154,1358,1584,1833,2107,2411,2746,3115,3523,
3973,4470,5018,5624,6291,7028,7842,8740,9730,10824,12031,13363,14833,16456,18247,
20224,22406,24815,27473,30408,33648,37224,41171,45529,50339,55649,61512,67983,75127,
83014,91721,101333,111945,123660,136594,150872,166636,184040,203254,224466,247886,
273742,302288,333804,368599,407015,449428,496254,547953,605032,668051,737627,814445,
899257,992895,1096278,1210421,1336443,1475581,1629200,1798808,1986068,2192818,2421087,
2673114,2951373,3258594,3597792,3972294,4385776,4842295,5346332,5902831,6517253,7195629,
7944614,8771558,9684577,10692629,11805606,13034431
];

private static MAXIMUM_EXPERIENCE: number = 200000000;
private static MINIMUM_LEVEL: number = 0;
private static MAXIMUM_LEVEL: number = 99;
private static MAXIMUM_INDEX: number = Skills.EXPERIENCE_LOOKUP_TABLE.length - 1;

private _values: SkillValue[];

public constructor(private actor: Actor, values?: SkillValue[]) {
Expand All @@ -150,6 +166,10 @@ export class Skills extends SkillShortcuts {
}
}

private static confine(value: number, min: number, max: number): number {
return Math.max(min, Math.min(value, max));
}

public setHitpoints(hitpoints: number): void {
this.setLevel(Skill.HITPOINTS, hitpoints);
}
Expand All @@ -176,37 +196,28 @@ export class Skills extends SkillShortcuts {
return this.getLevel(skill, ignoreLevelModifications) >= level;
}

public getLevelForExp(exp: number): number {
let points = 0;
let output = 0;

for(let i = 1; i <= 99; i++) {
points += Math.floor(i + 300 * Math.pow(2, i / 7));
output = Math.floor(points / 4);
if(output >= exp) {
return i;
public getLevelForExp(exp: number, index: number | undefined = undefined): number {
const start = Skills.confine((index || Skills.MAXIMUM_INDEX), Skills.MINIMUM_LEVEL, Skills.MAXIMUM_INDEX);
for (let level = start; level >= 1; level--) {
const requirement = Skills.EXPERIENCE_LOOKUP_TABLE[level];
if (exp > requirement) {
return level + 1;
}
}

return 99;
return 1;
}

public getExpForLevel(level: number): number {
let xp: number = 0;

for(let i = 1; i < level; i++) {
xp += Math.floor(i + 300 * Math.pow(2, i / 7));
}

return Math.floor(xp / 4);
const index = Skills.confine(level - 1, Skills.MINIMUM_LEVEL, Skills.MAXIMUM_INDEX);
return Skills.EXPERIENCE_LOOKUP_TABLE[index];
}

public addExp(skill: number | SkillName, exp: number): void {
const currentExp = this.get(skill).exp;
const currentLevel = this.getLevelForExp(currentExp);
let finalExp = currentExp + (exp * serverConfig.expRate);
if(finalExp > 200000000) {
finalExp = 200000000;
if (finalExp > Skills.MAXIMUM_EXPERIENCE) {
finalExp = Skills.MAXIMUM_EXPERIENCE;
}

const finalLevel = this.getLevelForExp(finalExp);
Expand Down

0 comments on commit 6a71ec4

Please sign in to comment.