Advertisement
Guest User

Skill rate formulas for Dwarf Therapist.

a guest
Jan 16th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.55 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. const int max_xp = 29000;
  4.  
  5. const int xp_levels [] = {0, 500, 1100, 1800, 2600, 3500, 4500, 5600, 6800, 8100, 9500,
  6.         11000, 12600, 14300, 16100, 18000, 20000, 22100, 24300, 26600, 29000};
  7. // xp(lvl) = 500*lvl + 100 * (lvl * (lvl -1)) / 2
  8. // const int lev_size = sizeof(xp_levels) / sizeof(int);   // 21 , unused
  9.  
  10. int get_level_from_xp(int xp)   // Uses busection method. Assumes xp >= 0.
  11. { if (xp >= max_xp)
  12.     return 20;
  13.  
  14.   int min = 0;
  15.   int max = 19;
  16.   int current = 10;
  17.  
  18.   while (1)
  19.   { if (xp < xp_levels[current])
  20.     { max = current;
  21.       current = (min + max)/2;
  22.     } else if (xp > xp_levels[current+1])
  23.     { min = current;
  24.       current = (min + max)/2;
  25.     } else
  26.       return current;
  27.   }
  28. }
  29.  
  30. /* Simulation method for judging a dwarf's skill level and rate.
  31.    Simple version of the while loop, without the interpolation.
  32.    Inputs: dwarf's current xp and learning rate for a skill.
  33.    Output: rating based on simulating gaining experience in that skill. In <0, 1>
  34. */
  35. double simulate_skill_gain(int xp, int rate)
  36. { if (xp >= max_xp)
  37.     return 20.0 / 20.0;
  38.  
  39.   if (rate == 0)
  40.     return get_level_from_xp(xp) / 20.0;       // Obviously stays the same.
  41.  
  42.   int sim_xp = max_xp;          // 29k seems like a good value to me.
  43.   sim_xp = (sim_xp / 100.0) * rate; // This is how much XP will go towards skill learning.
  44.   int total_xp = sim_xp;
  45.   double ret = 0.0;
  46.   int curr_level = get_level_from_xp(xp);
  47.   int curr_xp = xp;
  48.  
  49.   while ((sim_xp > 0) && (curr_level < 20))
  50.   { int xp_gap = xp_levels[curr_level+1] - curr_xp;         // How much XP till mext level?
  51.     if (xp_gap > sim_xp)    
  52.       xp_gap = sim_xp;
  53.     ret += xp_gap * curr_level;
  54.     curr_level++;
  55.     curr_xp = xp_levels[curr_level];
  56.     sim_xp -= xp_gap;
  57.   }
  58.  
  59.   if (sim_xp > 0)
  60.     ret += 20 * sim_xp;
  61.   ret /= total_xp;
  62.   ret /= 20.0;
  63.   return ret;
  64. }
  65.  
  66. /* Simulation method for judging a dwarf's skill level and rate.
  67.    This version of the while loop is interpolating the level. It may be smoother.
  68.    Inputs: dwarf's current xp and learning rate for a skill.
  69.    Output: rating based on simulating gaining experience in that skill. in <0, 1>
  70. */
  71. double simulate_skill_gain_slopes(int xp, int rate)
  72. { if (xp >= max_xp)
  73.     return 20.0 / 20.0;
  74.  
  75.   if (rate == 0)
  76.     return get_level_from_xp(xp) / 20.0;       // Obviously stays the same.
  77.  
  78.   int sim_xp = max_xp;          // 29k seems like a good value to me.
  79.   sim_xp = (sim_xp / 100.0) * rate; // This is how much XP will go towards skill learning.
  80.   int total_xp = sim_xp;
  81.   double ret = 0.0;
  82.   int curr_level = get_level_from_xp(xp);
  83.   int curr_xp = xp;
  84.  
  85.   while ((sim_xp > 0) && (curr_level < 20))
  86.   { int xp_gap = xp_levels[curr_level+1] - curr_xp;         // How much XP till mext level?
  87.     if (xp_gap > sim_xp)    
  88.       xp_gap = sim_xp;
  89.     double low = (0.0 +curr_xp -xp_levels[curr_level]) / (xp_levels[curr_level+1] -xp_levels[curr_level]);
  90.     double high = (0.0 +curr_xp + xp_gap -xp_levels[curr_level]) / (xp_levels[curr_level+1] -xp_levels[curr_level]);
  91.     double avg_level = curr_level + (low + high) / 2.0;   // Average scaled level for this iteration.
  92.     ret += xp_gap * avg_level;
  93.     curr_level++;
  94.     curr_xp = xp_levels[curr_level];
  95.     sim_xp -= xp_gap;
  96.   }
  97.  
  98.   if (sim_xp > 0)
  99.     ret += 20 * sim_xp;
  100.   ret /= total_xp;
  101.   ret /= 20.0;
  102.   return ret;
  103. }
  104.  
  105. /* An approximate formula to judge how close a dwarf is to legendary +5
  106.    Inputs: dwarf's current xp and learning rate for a skill.
  107.    Output: rating in <0, 1>
  108. */
  109. double skill_rate_eval(int xp, int rate)
  110. { if (xp >= max_xp)
  111.     return 1.0;
  112.   rate = (rate < 1)? 1 : rate;  // 0 or 1 doesn't really matter.
  113.   double jobs_left = (29000.0 - xp) / rate;  // Ignore division by 60.
  114.   double normalised_XP = 0.5 * (1.0 - jobs_left/290.0);    // Scaled to <-49.5, 0.5>
  115.   if (normalised_XP >= 0)
  116.     normalised_XP += 0.5;    // Values <0, 0.5> reserved for those suitable for a task.
  117.   else
  118.     normalised_XP = -0.5 / (normalised_XP - 1.0);
  119.   return normalised_XP;
  120. }
  121.  
  122. /* I could add fancy options to use this from command line or reading from a file,
  123.    but that's not really necessary. Just use $ rates <file_in.txt >file_out.txt */
  124. int main(void)
  125. { int xp, rate;
  126.   int ret = 2;
  127.  
  128.   printf("xp, rate, slill_level, sim1, sim2, interp1\n");
  129.   while ((ret = scanf("%d %d", &xp, &rate)) == 2)
  130.     printf("%d, %d, %d, %f, %f, %f\n", xp, rate, get_level_from_xp(xp), simulate_skill_gain(xp, rate), simulate_skill_gain_slopes(xp, rate), skill_rate_eval(xp, rate));
  131.  
  132.   return ret;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement