Advertisement
luckytyphlosion

N-shot percentage chance (Pokémon damage calc)

Apr 25th, 2019
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. private static double nShotPercentage(Move attack, Pokemon attacker, Pokemon defender, StatModifier atkMod,
  2.         StatModifier defMod, int numHitsNonCrit, int numHitsCrit) {
  3.     int rawHitDamageNC = damage(attack, attacker, defender, atkMod, defMod, MAX_RANGE, false);
  4.     int minDamageNC = rawHitDamageNC * MIN_RANGE / 255;
  5.     int[] probsNC = new int[rawHitDamageNC - minDamageNC + 1];
  6.     for (int i = MIN_RANGE; i <= MAX_RANGE; i++) {
  7.         int dmg = rawHitDamageNC * i / 255;
  8.         probsNC[dmg - minDamageNC]++;
  9.     }
  10.     int rawHitDamageCR = damage(attack, attacker, defender, atkMod, defMod, MAX_RANGE, true);
  11.     int minDamageCR = rawHitDamageCR * MIN_RANGE / 255;
  12.     int[] probsCR = new int[rawHitDamageCR - minDamageCR + 1];
  13.     for (int i = MIN_RANGE; i <= MAX_RANGE; i++) {
  14.         int dmg = rawHitDamageCR * i / 255;
  15.         probsCR[dmg - minDamageCR]++;
  16.     }
  17.     double chances = 0;
  18.     int rawHP = defender.getHP();
  19.     if (numHitsNonCrit > 0) {
  20.         for (int i = minDamageNC; i <= rawHitDamageNC; i++) {
  21.             chances += nShotPctInner(minDamageNC, rawHitDamageNC, minDamageCR, rawHitDamageCR, rawHP, 0, i,
  22.                     numHitsNonCrit, numHitsCrit, probsNC, probsCR);
  23.         }
  24.     } else {
  25.         for (int i = minDamageCR; i <= rawHitDamageCR; i++) {
  26.             chances += nShotPctInner(minDamageNC, rawHitDamageNC, minDamageCR, rawHitDamageCR, rawHP, 0, i,
  27.                     numHitsNonCrit, numHitsCrit, probsNC, probsCR);
  28.         }
  29.     }
  30.     return 100.0 * chances / Math.pow(MAX_RANGE - MIN_RANGE + 1, numHitsNonCrit + numHitsCrit);
  31. }
  32.  
  33. private static double nShotPctInner(int minDamageNC, int maxDamageNC, int minDamageCR, int maxDamageCR, int hp,
  34.         int stackedDmg, int rolledDamage, int hitsLeftNonCrit, int hitsLeftCrit, int[] probsNC, int[] probsCR) {
  35.     boolean wasCritical = false;
  36.     if (hitsLeftNonCrit > 0) {
  37.         hitsLeftNonCrit--;
  38.     } else {
  39.         hitsLeftCrit--;
  40.         wasCritical = true;
  41.     }
  42.     stackedDmg += rolledDamage;
  43.     if (stackedDmg >= hp || (stackedDmg + hitsLeftNonCrit * minDamageNC + hitsLeftCrit * minDamageCR) >= hp) {
  44.         return Math.pow(MAX_RANGE - MIN_RANGE + 1, hitsLeftNonCrit + hitsLeftCrit)
  45.                 * (wasCritical ? probsCR[rolledDamage - minDamageCR] : probsNC[rolledDamage - minDamageNC]);
  46.     } else if (hitsLeftNonCrit == 0 && hitsLeftCrit == 0) {
  47.         return 0;
  48.     } else if (stackedDmg + hitsLeftNonCrit * maxDamageNC + hitsLeftCrit * maxDamageCR < hp) {
  49.         return 0;
  50.     } else {
  51.         double chances = 0;
  52.         if (hitsLeftNonCrit > 0) {
  53.             for (int i = minDamageNC; i <= maxDamageNC; i++) {
  54.                 chances += nShotPctInner(minDamageNC, maxDamageNC, minDamageCR, maxDamageCR, hp, stackedDmg, i,
  55.                         hitsLeftNonCrit, hitsLeftCrit, probsNC, probsCR);
  56.             }
  57.         } else {
  58.             for (int i = minDamageCR; i <= maxDamageCR; i++) {
  59.                 chances += nShotPctInner(minDamageNC, maxDamageNC, minDamageCR, maxDamageCR, hp, stackedDmg, i,
  60.                         hitsLeftNonCrit, hitsLeftCrit, probsNC, probsCR);
  61.             }
  62.         }
  63.         return chances * (wasCritical ? probsCR[rolledDamage - minDamageCR] : probsNC[rolledDamage - minDamageNC]);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement