Search Unity

Experience formula to code - Math

Discussion in 'Scripting' started by henmachuca, Oct 29, 2016.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Hello,

    I am trying to code an experience formula into my game but I have no clue on how to translate it to code. The formula is in the image and my attempt to code it are below. But they are not working.

    Can any one help me out ?

    Code (CSharp):
    1. /* Recursive call to calculate Pi = Pi-1 + tx
    2. with p1 = 10 to stop recursive call */
    3. int coefp(int x)
    4. {
    5. return (x == 1 ? 10 : coefp(x – 1) + (x > 192 ?
    6. 4096 : (x > 182 ? 1024 : (x > 162 ? 256 : (x >
    7. 142 ? 64 : (x > 122 ? 16 : ( x > 82 ? 4 : (x >
    8. 42 ? 1 : 0 ) ) ) ) ) ) ) );
    9. }
    10. /* Calculate the sum and call recursive function to
    11. calculate Pi */
    12. int coef(int x)
    13. {
    14. int n = 0;
    15. for (int i = 1; i < x; ++i) n += coefp(i);
    16. return 1000 + n;
    17. }
    18. /* Calculate the total XP */
    19. int xp(int level)
    20. {
    21. return _int64(coef(level - 1)*pow(level - 1, 2.5));
    22. }
    Thank you!
     
    Last edited: Oct 29, 2016
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I'm not sure what the Sum(from 1 to 0 ) P(i) should output. nothing or P(1). I chose nothing since its how it would work in a for loop. EDIT: Just realized it doesn't matter, since your multiplying that by 0^2.5 so it comes out zero no matter which approach you take.

    Code (CSharp):
    1.  long GetExp(int level)
    2.     {
    3.         float expMultiplier = Mathf.Pow(level - 1, 2.5f);
    4.         float baseExp = 1000f;
    5.         float p = 10f;
    6.         float t = 0f;
    7.         for (int i = 1; i < level; i++)
    8.         {
    9.             if (i == 43)
    10.                 t = 1f;
    11.             if (i == 83)
    12.                 t = 4f;
    13.             if (i == 123)
    14.                 t = 16f;
    15.             if (i == 143)
    16.                 t = 64f;
    17.             if (i == 163)
    18.                 t = 256f;
    19.             if (i == 183)
    20.                 t = 1024f;
    21.             if (i == 193)
    22.                 t = 4096f;
    23.             p += t;
    24.             baseExp += p;
    25.         }
    26.         return (long)(baseExp * expMultiplier);
    The output of the above method:
    1: 0
    2: 1,010
    3: 5,769
    4: 16,056
    5: 33,280
    6: 58,696
    7: 93,472
    8: 138,716
    9: 195,500
    10: 264,870
    11: 347,850
    12: 445,455
    13: 558,690
    14: 688,552
    15: 836,035
    16: 1,002,134
    17: 1,187,840
    18: 1,394,145
    19: 1,622,046
    20: 1,872,539
    21: 2,146,625
    41: 14,167,003
    61: 49,385,185
    81: 145,455,326
    101: 422,400,000
    121: 1,183,711,679
    141: 3,347,857,496
    161: 11,645,762,455
    181: 50,370,326,738
    201: 305,422,618,673
     
    Last edited: Oct 29, 2016
    henmachuca likes this.
  3. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Amazing @takatok , amazing!!
    Thank you very much.
     
  4. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    @takatok let me ask you something regarding the formula...

    Do you see a way of simplyfing the formula so it can fit an INT based method?
    Another question is how to get rig of the level 1 being 0 and starting the level 1 with the level 2 value.


    Thank you!
     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Change level-1 to just level in the first part of the method to start at 1:1000
    Not sure what you mean by an int based method? why would it matter? The answers come out ints
     
    Last edited: Feb 21, 2017
  6. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Because you are returning a variable of type LONG. Is it possible to return a variable of type int? (even if that meant having to do some adjustments).
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    the only difference is that an int caps out at 2.1 billion (around level 130). Unsigned int (4.2b) could get you to low 140s. Longs go up to 10^18 which is way beyond anything you'd need.

    If your characters will stay under level 130 just change all the longs in the code to int.
    If you need the character to make it to 201 in a int range, try changing the initial p to 5, possibly tweaking the later multipliers (256,1024,4096) to be half their values as well and see how big it gets.

    Keep using longs while you do this tweaking till you get the final level to be 2.1 billion or less. Then change it over to ints
     
unityunity