PDA

View Full Version : getLevelFromXP



Ian
03-21-2014, 09:05 PM
This is something I made for the script I'm working on, what it does is it gets the players skill level in any skill using their xp in that skill. This has the advantage of not needing to change from your current tab to your skill tab and back if you want to be able to check your skill level at any time.

To use this you'll need a TIntegerArray of the amounts of XP for each level. This can be stored locally or globally depending on what you want to use it for for other things in the script. For mine I will store it globally and declare it at the start of the script where I'm declaring other arrays that will be referenced throughout the script.

You will also need to know how much XP you have in the skill you want to check. In my script, it checks the existing XP at startup, then uses TRSChatbox.getXP (http://villavu.com/forum/showthread.php?t=107292) by Ashaman88; to keep track of additional gains.

TIntegerArray:

XPByLevel:= [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, 200000000];


Function:

function getLevelFromXP(XP: Integer): Integer;
var
i: Integer;
begin
for i:= 0 to (length(XPByLevel) - 1) do
begin
if (XP > XPByLevel[i]) and (XP < XPByLevel[i + 1]) then
result:= (i + 1);
if result then
break();
end;
end;


Example:

writeLn('Your current Mining Level is ' + ToStr(getLevelFromXP(MiningXP)) + '.');


:)

Brotein
03-21-2014, 09:17 PM
Why not use something like this for getting the XP? Rather than storing it all in an array. Get the level with tabStats.getSkillLevel() during startup and plug that into this.

function getXpLevel(XpLevel : Integer) : Integer;
var
i, Diff, Level : Integer;
Points : Extended;
begin
Points := 0;
for i := 1 to (XpLevel - 1) do
begin
Level := i;
Diff := floor(Level + 300 * pow(2, Level/7));
Points := Points + Diff;
Result := floor(Points / 4);
end;
end;

I converted this from the python version...

http://mirekw.com/rs/RSDOnline/Guides/exp_formula.gif

points = 0
for level in range(1,100):
diff = int(level + 300 * math.pow(2, float(level)/7) )
points += diff
str = "Level %d = %d" % (level + 1, points / 4)
print str

Ian
03-21-2014, 09:21 PM
Why not use something like this for getting the XP? Rather than storing it all in an array.

function getXpLevel(XpLevel : Integer) : Integer;
var
i, Diff, Level : Integer;
Points : Extended;
begin
Points := 0;
for i := 1 to (XpLevel - 1) do
begin
Level := i;
Diff := floor(Level + 300 * pow(2, Level/7));
Points := Points + Diff;
Result := floor(Points / 4);
end;
end;

I converted this from the python version...

points = 0
for level in range(1,100):
diff = int(level + 300 * math.pow(2, float(level)/7) )
points += diff
str = "Level %d = %d" % (level + 1, points / 4)
print str
Cool, I didn't know the formula for how Jagex calculated the difference in experience between the levels, thanks! :)

Brotein
03-21-2014, 09:23 PM
No problem I was building a script the other day and looked around for the formula and found a page that had it in javascript and python and just converted it.

Ian
03-21-2014, 09:27 PM
No problem I was building a script the other day and looked around for the formula and found a page that had it in javascript and python and just converted it.

For some reason when I run yours I get

Exception in Script: Runtime error: "Invalid floating point operation" at line 274, column 20
Line 274 is

Diff := floor(Level + 300 * pow(2, Level/7));

and column 20 is Level.

EDIT: Turns out it was user error, nvm :P

The Mayor
03-21-2014, 09:52 PM
Runescape wikia has some formulas related to experience. Even estimating XP remaining to next level from current XP.

http://runescape.wikia.com/wiki/Experience

Although you'd have to rearrange that to get the current level from the current XP.

masterBB
03-21-2014, 11:17 PM
Why not use something like this for getting the XP? Rather than storing it all in an array. Get the level with tabStats.getSkillLevel() during startup and plug that into this.

function getXpLevelUNREADABLE(XpLevel : Integer) : Integer;
var
i: Integer;
begin
for i := 1 to (XpLevel - 1) do
Result := Result + floor(i + 300 * pow(2, i/7));
Result := Result shr 2;
end;

function getXpLevelBLACKMAGIC(XpLevel : Integer) : Integer;
begin
Dec(XpLevel);
if(Xplevel > 1) then
Result := getXpLevelBLACKMAGIC(XpLevel) + (floor(Xplevel + 300 * pow(2, Xplevel/7)) shr 2)
else
Result := floor(Xplevel + 300 * pow(2, Xplevel/7)) shr 2;
end;

Based on yours.

e: Ugh, just tested it in simba, black magic doesn't work:blushing:
function getXpLevelBLACKMAGIC(XpLevel : Integer) : Integer;
function ADABAKEBRA(l: Integer): Integer;
begin
Dec(l);
Result := floor(l + 300 * pow(2, l/7));
if(l > 1) then
IncEx(Result, ADABAKEBRA(l));
end;
begin
Result := ADABAKEBRA(Xplevel) shr 2;
end;

function getXpLevelUNREADABLE2(XpLevel : Integer) : Integer;
begin
repeat
Dec(XpLevel);
Result := Result + floor(XpLevel + 300 * pow(2, XpLevel/7));
until(XpLevel <= 1);
Result := Result shr 2;
end;

tehq
04-07-2014, 06:59 PM
One could write various combinations of these functions in order to determine the experience needed from x level to y level, the experience required from your current experience to z level, etc. The process to eliminate the increasing error in each calculation (LevelFromExp) is trivial.


function ExpFromLevel(Level: integer): integer;
begin
Result := (round((Level / 4) + (75 * pow(2, (Level / 7)))));
writeLn('Experience: (' + toStr(Level) + ' to ' + toStr(Level + 1) + ') = ' + toStr(Result));
end;

function LevelFromExp(Experience: integer): integer;

{ Maximum error in approximation occurs at level 120 ~ +12 experience }

var
CurrentExp, n: integer;
begin
for n := 1 to 120 do
begin
if CurrentExp < Experience then
CurrentExp := round((1 / 4) *((n + (300 * pow(2, n / 7))))) + CurrentExp
else
break;
Result := n;
end;
writeLn('Level: ' + toStr(Result));
end;


function statExpToGoal(CurrentLevel, EndLevel: integer): integer;
var
i: integer;
begin
for i := CurrentLevel to EndLevel - 1 do
Result := ExpFromLevel(i) + Result;
writeLn('Experience: (' + toStr(CurrentLevel) + ' to ' + toStr(EndLevel) + ') = '+ toStr(Result));
end;

function varExpToGoal(Experience, EndLevel: integer): integer
var
CurrentLevel, NextLevel, i: integer;
begin
CurrentLevel := LevelFromExp(Experience);
NextLevel := statExpToGoal(1, CurrentLevel + 1);
Result := NextLevel - Experience;
for i := CurrentLevel + 1 to EndLevel - 1 do
incEx(Result, ExpFromLevel(i));
writeLn('Experience: (' + toStr(Experience) + ' experience to level ' + toStr(EndLevel) + ') = ' + toStr(Result));
end;
Usage:
statExpToGoal(74, 79);
varExpToGoal(6500000, 95);
ExpFromLevel(74);
LevelFromExp(105000);

Shatterhand
06-03-2014, 07:08 PM
Thanks for the xp array, always wanted to have it. :D