PDA

View Full Version : Progress Bar and Time to Level Functions



Brotein
04-27-2015, 06:22 PM
Hey guys, I wrote this code to get some extra informmation for a progress report for an OpenGL bot I'm writing and thought I would share this. It builds off another thread by BMWxi with some functions we posted. Feel free to use/modify as you wish.


Quick Run Down:
So this code only requires one real input and that is CurrentXP. This can be obtained any way you would like but one way would be to grab the current experience in the stats tab at startup via tabStats.getSkillInfo(SKILL_ATTACK, INFO_CURRENT_XP)or by reading the chars at the texture for the skill you want in OpenGL. You should then regularly update this variable by measuring your change in XP and just adding to the original value. Then the rest is just simple arithmetic. You can derive every other value based on your CurrentXP using these functions. It's pretty straight forward. The progress bar is just 20 components representing 5% of a total 100%, you can change the characters with BAR_ELEMENT, just remember to add equal spacing for BAR_SPACE. I hope this can be useful in people's scripts by showing more information in addition to the current progress reports.



Functions:
function getLevelFromXP(XP: Integer): Integer;
function getXpFromLevel(Lvl : Integer) : Integer;
function xpPercent(CurrentXP, NextLvlXP : Integer) : Integer;
function timeToLevel(RemainingXP, HourlyXP : Integer) : Integer;
function drawBar(CurrentXP, NextLvlXP : Integer) : String;


CODE:
program PercentageBar;
{$I SRL-6/SRL.simba}

const
XP_ARRAY : array of Integer = [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];
BAR_ELEMENT = '#';
BAR_SPACE = ' ';

(*
getLevelFromXP
~~~~~~~~

.. code-block:: pascal

function getLevelFromXP(XP: Integer): Integer;

Takes 'XP' as Integer and outputs the corresponding level as Integer.

.. note::

- by BMWxi

Example:

.. code-block:: pascal

writeLn('Mining Level: ' + ToStr(getLevelFromXP(MiningXP));

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

(*
getXpFromLevel
~~~~~~~~

.. code-block:: pascal

function getXpFromLevel(Lvl : Integer) : Integer;

Takes 'Lvl' as Integer and outputs the corresponding base experience as Integer.

.. note::

- by Brotein, Zyt3x

Example:

.. code-block:: pascal

writeLn('XP at lvl90 Mining: ' + intToStr(getXpFromLevel(90));

*)
function getXPfromLevel(Level : Integer) : Integer;
var
i : Integer;
begin
for i := 1 to Level - 1 do
result += floor(i + 300 * pow(2, i/7));
result := floor(result / 4);
end;

(*
xpPercent
~~~~~~~~

.. code-block:: pascal

function xpPercent(CurrentXP, NextLvlXP : Integer) : Integer;

Takes 'CurrentXP' and 'NextLvlXP' as Integer and outputs the corresponding
Percentage as Integer, where CurrentXP is the current experience in the skill
and NextLvlXP is the baseline experience needed to get to the next level.
THIS IS NOT XP REMAINING BUT THE TOTAL XP NEEDED TO BE THE NEXT LEVEL.

.. note::

- by Brotein

Example:

.. code-block:: pascal

writeLn('Current %ToLvl: ' + intToStr(xpPercent(CurrentXP, NextLvlXP);

*)
function xpPercent(CurrentXP, NextLvlXP : Integer) : Integer;
var
temp : Integer := getXpFromLevel(getLevelFromXp(CurrentXP));
begin
result := floor(((CurrentXP - temp) / (NextLvlXP - temp)) * 100);
end;

(*
timeToLevel
~~~~~~~~

.. code-block:: pascal

function timeToLevel(RemainingXP, HourlyXP : Integer) : Integer;

Takes 'RemainingXP' and 'HourlyXP' as Integer and outputs the time to level
in milliseconds as Integer, where 'RemainingXP' is 'NextLevelXP' - 'CurrentXP'
and 'HourlyXP' is the experience per hour rate.

.. note::

- by Brotein

Example:

.. code-block:: pascal

writeLn('TTL: ' + intToStr(xpPercent(CurrentXP, NextLvlXP);

*)
function timeToLevel(RemainingXP, HourlyXP : Integer) : Integer;
begin
result := floor((RemainingXP / HourlyXP) * 3600000);
end;

(*
drawBar
~~~~~~~~

.. code-block:: pascal

function drawBar(CurrentXP, NextLvlXP : Integer) : String;

Takes 'CurrentXP' and 'NextLvlXP' as Integer and outputs a String progress bar
that is a combination of a total of 20 BAR_ELEMENT or BAR_SPACE. That means
each component is representative of 5% of a total 100%, thus the magic numbers.

OUTPUT EXAMPLE
- [################# ] 86% to Level: 10

.. note::

- by Brotein

Example:

.. code-block:: pascal

writeLn(drawBar(CurrentXP, NextLvlXP));

*)
function drawBar(CurrentXP, NextLvlXP : Integer) : String;
var
s : String;
i, temp : Integer;
begin
s := s + '[';
temp := floor(xpPercent(CurrentXP, NextLvlXP) / 5);
for i := 1 to temp do
s := s + BAR_ELEMENT;
for i := 1 to (20 - temp) do
s := s + BAR_SPACE;
s := s + ']';
s := s + ' ' + intToStr(xpPercent(CurrentXP, NextLvlXP)) + '% to Level: '
+ intToStr(getLevelFromXP(CurrentXP) + 1);
result := s;
end;


Example:
procedure progressReport;
var
CurrentXP : Integer := 1143485;
NextLvlXP : Integer := getXPFromLevel(getLevelFromXP(CurrentXP) + 1);
HourlyXP : Integer := 50000;
begin
clearDebug();
writeln('-------------------Progress Report-------------------');
writeLn('Current XP: ' + intToStr(CurrentXP));
writeln('Current Level: ' + intToStr(getLevelFromXP(CurrentXP)));
writeln('XP to Next Level: ' + intToStr(getXpFromLevel(getLevelFromXp(CurrentXP) + 1) - CurrentXP));
writeLn('XP/HR: ' + intToStr(HourlyXP));
writeln('Time to Level: ' + msToTime(timeToLevel(NextLvlXP - CurrentXP, HourlyXP), TIME_SHORT));
writeln(drawBar(currentXP, NextLvlXP));
end;


Output:

-------------------Progress Report-------------------
Current XP: 1143485
Current Level: 74
XP to Next Level: 66936
XP/HR: 50000
Time to Level: 01h 20m 19s
[######## ] 41% to Level: 75

Zyt3x
04-27-2015, 08:27 PM
Nice! Your current code for getLevelFromXP wont work when you have exactly as much xp as required to be that level (i.e you have exactly 83 xp).
Change
"if (XP > XP_ARRAY[i]) and (XP < XP_ARRAY[i + 1]) then" to
"if (XP >= XP_ARRAY[i]) and (XP < XP_ARRAY[i + 1]) then" and it'll work :)

You could also clean up getXPFromLevel (don't need that many variables):

function getXPfromLevel(Level : Integer) : Integer;
var
I : Integer;
begin
for I := 1 to Level-1 do
result += floor(I + 300 * pow(2, I/7));
result := floor(result / 4);
end;

Clarity
04-27-2015, 08:42 PM
Great contribution. +Rep

Except:

You must spread some Reputation around before giving it to Brotein again.
:(

Ian
04-27-2015, 08:48 PM
Nice Brotein! And thanks Zyt3x for the correction ;)

Brotein
04-27-2015, 09:34 PM
Nice! Your current code for getLevelFromXP wont work when you have exactly as much xp as required to be that level (i.e you have exactly 83 xp).
Change
"if (XP > XP_ARRAY[i]) and (XP < XP_ARRAY[i + 1]) then" to
"if (XP >= XP_ARRAY[i]) and (XP < XP_ARRAY[i + 1]) then" and it'll work :)

You could also clean up getXPFromLevel (don't need that many variables):

function getXPfromLevel(Level : Integer) : Integer;
var
I : Integer;
begin
for I := 1 to Level-1 do
result += floor(I + 300 * pow(2, I/7));
result := floor(result / 4);
end;

Thanks for the fixes!