Author Topic: Experience (Math Help)  (Read 147 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 509
  • Karma: 22
    • View Profile
    • iViscosity Gaming
Experience (Math Help)
« on: December 20, 2016, 09:30:21 PM »
So I'm not the greatest when it comes to math in Lua, so I'm going to ask for some help here. I was looking up how to set up a system where after each level, the amount of experience needed to get to the next level is changed (but not by a specific amount, and can be altered based on the amount of levels, if I add more in). After googling a bit, I found a C++ equivalent of something I need (found here). Basically, it looked like this:

Code: C
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. int main(void)
  5. {
  6.     int levels = 40;
  7.     int xp_for_first_level = 1000;
  8.     int xp_for_last_level = 1000000;
  9.  
  10.     double B = log((double)xp_for_last_level / xp_for_first_level) / (levels - 1);
  11.     double A = (double)xp_for_first_level / (exp(B) - 1.0);
  12.  
  13.     for (int i = 1; i <= levels; i++)
  14.     {
  15.         int old_xp = round(A * exp(B * (i - 1)));
  16.         int new_xp = round(A * exp(B * i));
  17.         std::cout << i << " " << (new_xp - old_xp) << std::endl;
  18.     }
  19. }
  20.  

I tried to convert this into Lua, but as you can probably see it probably isn't right:

Code: Lua
  1. function CalcXPRequired()
  2.    local levels = #EXP.Config.Levels
  3.    local xp_for_first_level = 1000
  4.    local xp_for_last_level = 14391160
  5.  
  6.    local B = math.log( xp_for_last_level / xp_for_first_level ) / ( levels - 1 )
  7.    local A = xp_for_first_level / ( math.exp( B ) - 1.0 )
  8.    local required
  9.  
  10.    for i = 1, levels do
  11.       local old_xp = math.Round( A * math.exp( B * ( i - 1 ) ) )
  12.       local new_xp = math.Round( A * math.exp( B * i ) )
  13.  

As you can see, it isn't even finished, as I don't think that's correct and I don't know where to go from here. I was wondering if anyone could help me set up a system that, where the max experience is 'xp_for_last_level' and the experience needed to level up varies between each level, and can be altered to a lower amount (between each level) if more levels are added, and / or higher if the 'xp_for_last_level' is increased.

If you need more clarification, I can try to help explain.


(Unless, of course, this is right. I don't know if it's right or wrong but I don't know how to finish it so I can't test it )


EDIT:


I did some messing around, and I think I figured out how to get it right. I have this (edited to work in codepad.org)


Code: Lua
  1.  
  2. for i = 1, 22 do
  3.    local xp_for_first_level = 1000
  4.    local xp_for_last_level = 14391160
  5.  
  6.  
  7.    local B = math.log( xp_for_last_level / xp_for_first_level ) / ( 22 - 1 )
  8.    local A = xp_for_first_level / ( math.exp( B ) - 1.0 )
  9.  
  10.  
  11.    local old_xp = math.floor( A * math.exp( B * ( i - 1 ) ) )
  12.    local new_xp = math.floor( A * math.exp( B * i ) )
  13.  
  14.  
  15. print( new_xp - old_xp )
  16. end
  17.  


Output:


Code: [Select]

1000
1577
2489
3927
6195
9772
15418
24324
38374
60540
95509
150678
237714
375025
591648
933402
1472560
2323151
3665069
5782114
9122025
14391160


Then I tried changing the amount of levels:


Code: Lua
  1.  
  2. for i = 1, 100 do
  3.    local xp_for_first_level = 1000
  4.    local xp_for_last_level = 14391160
  5.  
  6.  
  7.    local B = math.log( xp_for_last_level / xp_for_first_level ) / ( 100 - 1 )
  8.    local A = xp_for_first_level / ( math.exp( B ) - 1.0 )
  9.  
  10.  
  11.    local old_xp = math.floor( A * math.exp( B * ( i - 1 ) ) )
  12.    local new_xp = math.floor( A * math.exp( B * i ) )
  13.  
  14.  
  15. print( new_xp - old_xp )
  16. end
  17.  


Output:
Code: [Select]

1000
1101
1214
1336
1473
1621
1787
1968
2168
2387
2631
2897
3192
3515
3873
4266
4699
5176
5702
6281
6919
7621
8395
9247
10187
11221
12360
13615
14998
16521
18198
20046
22082
24323
26794
29515
32511
35812
39450
43454
47868
52727
58082
63980
70476
77633
85515
94199
103764
114300
125907
138691
152774
168288
185375
204199
224933
247774
272933
300647
331176
364803
401847
442650
487598
537110
591648
651726
717903
790800
871100
959552
1056987
1164316
1282542
1412773
1556229
1714252
1888319
2080063
2291276
2523936
2780222
3062529
3373505
3716056
4093391
4509041
4966897
5471244
6026805
6638776
7312890
8055454
8873418
9774441
10766955
11860251
13064561
14391160


So, I think I figured it out. Now I just have to check the amount of experience a person has VS the experience required to level up, which shouldn't be too hard.
« Last Edit: December 21, 2016, 05:22:39 PM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7553
  • Karma: 337
  • Sertafide Ulysses Jenius
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Experience (Math Help)
« Reply #1 on: December 21, 2016, 02:59:32 PM »
The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 696
  • Karma: 101
  • :)
    • View Profile
    • My Homepage
Re: Experience (Math Help)
« Reply #2 on: December 21, 2016, 03:43:07 PM »
Just gonna throw this out: that's C++, not PHP. :P
bw81@ulysses-forums ~ % whoami
Homepage

I have 10 more Keybase invites! PM me for more information or to request one!
An easy intro to PGP and public-key cryptography

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 509
  • Karma: 22
    • View Profile
    • iViscosity Gaming
Re: Experience (Math Help)
« Reply #3 on: December 21, 2016, 05:21:34 PM »
Just gonna throw this out: that's C++, not PHP.
Sorry, was very late and I was just working on some things for my webserver so my brain was set to PHP mode

Sent using Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 509
  • Karma: 22
    • View Profile
    • iViscosity Gaming
Re: Experience (Math Help)
« Reply #4 on: December 21, 2016, 05:25:40 PM »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord
Teamspeak

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.