Greenadine

UWU

Jun 9th, 2020
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.41 KB | None | 0 0
  1. /**
  2.  * Make one of the battling Pokémon perform a move.
  3.  */
  4. public void pickMove(User user, int position) {
  5.     if (position < 0 || position > 4) { return; }
  6.  
  7.     if (user.getId() == user1) {
  8.         battler1PerformingMove = battler1.getMove(position);
  9.     }
  10.     else if (user.getId() == user2) {
  11.         battler2PerformingMove = battler2.getMove(position);
  12.     } else {
  13.         return;
  14.     }
  15.  
  16.     if (battler1PerformingMove != null && battler2PerformingMove != null) {
  17.         performTurn();
  18.     }
  19. }
  20.  
  21. private StringJoiner performMove(int battler, Move move) {
  22.     StringJoiner result = new StringJoiner("\n");
  23.  
  24.     Optional<User> user;
  25.     BattlePokemon pokemon;
  26.     Optional<User> opposingUser;
  27.     BattlePokemon opposingPokemon;
  28.  
  29.     if (battler == 1) {
  30.         user = Pokebot.bot.getCachedUserById(user1);
  31.         pokemon = battler1;
  32.         opposingUser = Pokebot.bot.getCachedUserById(user2);
  33.         opposingPokemon = battler2;
  34.  
  35.     } else {
  36.         user = Pokebot.bot.getCachedUserById(user2);
  37.         pokemon = battler2;
  38.         opposingPokemon = battler1;
  39.         opposingUser = Pokebot.bot.getCachedUserById(user1);
  40.     }
  41.  
  42.     Target moveTarget = move.getTarget();
  43.  
  44.     double damage;
  45.  
  46.     Map<Effect, Object> effects = move.getEffects().getAsMap();
  47.  
  48.     // Determine damage
  49.     if (move.isDamagingMove()) {
  50.         // Determine type matchup multiplier
  51.         TypeMatchup matchup;
  52.  
  53.         if (opposingPokemon.getSpecies().hasSecondaryTyping()) {
  54.             matchup = TypeMatchup.getTypeMatchup(move.getType(), opposingPokemon.getSpecies().getPrimaryTyping(), opposingPokemon.getSpecies().getSecondaryTyping());
  55.         } else {
  56.             matchup = TypeMatchup.getTypeMatchup(move.getType(), opposingPokemon.getSpecies().getPrimaryTyping());
  57.         }
  58.  
  59.         double type = matchup.getMultiplier();
  60.  
  61.         if (matchup == TypeMatchup.IMMUNE) {
  62.             result.add("It had no effect.");
  63.             return result;
  64.         }
  65.  
  66.         Random r = new Random();
  67.  
  68.         // Determine crit chance
  69.         double crit;
  70.  
  71.         if (effects.containsKey(Effect.ALWAYS_CRIT)) {
  72.             crit = 1.5;
  73.         }
  74.         else {
  75.             float critChance;
  76.  
  77.             if (effects.containsKey(Effect.HIGH_CRIT_CHANCE)) {
  78.                 critChance = (float) effects.get(Effect.HIGH_CRIT_CHANCE);
  79.             } else {
  80.                 critChance = 0.0625f;
  81.             }
  82.  
  83.             float f = r.nextFloat();
  84.  
  85.             if (f < critChance) {
  86.                 crit = 1.5;
  87.             } else {
  88.                 crit = 1;
  89.             }
  90.         }
  91.  
  92.         // Determine STAB bonus
  93.         double stab;
  94.  
  95.         if (pokemon.hasTyping(move.getType())) {
  96.             stab = 1.5;
  97.         } else {
  98.             stab = 1;
  99.         }
  100.  
  101.         // Generate random multiplier
  102.         double random = 0.85 + (1 - 0.85) * r.nextDouble();
  103.  
  104.         // Determine acting move power
  105.         int power;
  106.  
  107.         if (effects.containsKey(Effect.SUBSEQUENT_USE_POWER_DOUBLE)) {
  108.             if (battler == 1) {
  109.                 power = move.getPower() * battler1SubsequentUsePowerDouble;
  110.                 battler1SubsequentUsePowerDouble++;
  111.             } else {
  112.                 power = move.getPower() * battler2SubsequentUsePowerDouble;
  113.                 battler2SubsequentUsePowerDouble++;
  114.             }
  115.         }
  116.         else if (effects.containsKey(Effect.POWER_DOUBLE_AFTER_MOVE)) {
  117.             Move previousMove = (Move) effects.get(Effect.POWER_DOUBLE_AFTER_MOVE);
  118.  
  119.             if (battler == 1) {
  120.                 if (battler1PreviousMove == previousMove) {
  121.                     power = move.getPower() * 2;
  122.                 } else {
  123.                     power = move.getPower();
  124.                 }
  125.             } else {
  126.                 if (battler2PreviousMove == previousMove) {
  127.                     power = move.getPower() * 2;
  128.                 } else {
  129.                     power = move.getPower();
  130.                 }
  131.             }
  132.         }
  133.         else if (effects.containsKey(Effect.DOUBLE_POWER_BURN)) {
  134.             if (battler == 1) {
  135.                 if (battler2.getNonVolatileStatusCondition() == StatusCondition.BURN) {
  136.                     power = move.getPower() * 2;
  137.                 } else {
  138.                     power = move.getPower();
  139.                 }
  140.             } else {
  141.                 if (battler1.getNonVolatileStatusCondition() == StatusCondition.BURN) {
  142.                     power = move.getPower() * 2;
  143.                 } else {
  144.                     power = move.getPower();
  145.                 }
  146.             }
  147.         }
  148.         else if (effects.containsKey(Effect.DOUBLE_POWER_POISON)) {
  149.             if (battler == 1) {
  150.                 if (battler2.getNonVolatileStatusCondition() == StatusCondition.POISON || battler2.getNonVolatileStatusCondition() == StatusCondition.BADLY_POISONED) {
  151.                     power = move.getPower() * 2;
  152.                 } else {
  153.                     power = move.getPower();
  154.                 }
  155.             } else {
  156.                 if (battler1.getNonVolatileStatusCondition() == StatusCondition.POISON || battler1.getNonVolatileStatusCondition() == StatusCondition.BADLY_POISONED) {
  157.                     power = move.getPower() * 2;
  158.                 } else {
  159.                     power = move.getPower();
  160.                 }
  161.             }
  162.         }
  163.         else if (effects.containsKey(Effect.DOUBLE_POWER_PARALYSIS)) {
  164.             if (battler == 1) {
  165.                 if (battler2.getNonVolatileStatusCondition() == StatusCondition.PARALYSIS) {
  166.                     power = move.getPower() * 2;
  167.                 } else {
  168.                     power = move.getPower();
  169.                 }
  170.             } else {
  171.                 if (battler1.getNonVolatileStatusCondition() == StatusCondition.PARALYSIS) {
  172.                     power = move.getPower() * 2;
  173.                 } else {
  174.                     power = move.getPower();
  175.                 }
  176.             }
  177.         }
  178.         else if (effects.containsKey(Effect.DOUBLE_POWER_SLEEP)) {
  179.             if (battler == 1) {
  180.                 if (battler2.getNonVolatileStatusCondition() == StatusCondition.SLEEP) {
  181.                     power = move.getPower() * 2;
  182.                 } else {
  183.                     power = move.getPower();
  184.                 }
  185.             } else {
  186.                 if (battler1.getNonVolatileStatusCondition() == StatusCondition.SLEEP) {
  187.                     power = move.getPower() * 2;
  188.                 } else {
  189.                     power = move.getPower();
  190.                 }
  191.             }
  192.         }
  193.         else if (effects.containsKey(Effect.DOUBLE_POWER_FREEZE)) {
  194.             if (battler == 1) {
  195.                 if (battler2.getNonVolatileStatusCondition() == StatusCondition.FREEZE) {
  196.                     power = move.getPower() * 2;
  197.                 } else {
  198.                     power = move.getPower();
  199.                 }
  200.             } else {
  201.                 if (battler1.getNonVolatileStatusCondition() == StatusCondition.FREEZE) {
  202.                     power = move.getPower() * 2;
  203.                 } else {
  204.                     power = move.getPower();
  205.                 }
  206.             }
  207.         }
  208.         else {
  209.             power = move.getPower();
  210.         }
  211.  
  212.         // IMPORTANT Calculate and subtract final damage output
  213.         double modifier = crit * random * stab * type;
  214.  
  215.         if (move.getCategory() == MoveCategory.PHYSICAL) {
  216.             if (effects.containsKey(Effect.IGNORE_STAT_CHANGES)) {
  217.                 damage = ((((2 * pokemon.getLevel() / 5f) + 2) * power * pokemon.getAttack() / opposingPokemon.getInitDefense()) / 50 + 2) * modifier;
  218.             } else {
  219.                 damage = ((((2 * pokemon.getLevel() / 5f) + 2) * power * pokemon.getAttack() / opposingPokemon.getDefense()) / 50 + 2) * modifier;
  220.             }
  221.         } else {
  222.             if (effects.containsKey(Effect.IGNORE_STAT_CHANGES)) {
  223.                 damage = ((((2 * pokemon.getLevel() / 5f) + 2) * power * pokemon.getSpAttack() / opposingPokemon.getInitSpDefense()) / 50 + 2) * modifier;
  224.             } else {
  225.                 damage = ((((2 * pokemon.getLevel() / 5f) + 2) * power * pokemon.getSpAttack() / opposingPokemon.getSpDefense()) / 50 + 2) * modifier;
  226.             }
  227.         }
  228.  
  229.         boolean fainted = opposingPokemon.dealDamage((int) Math.floor(damage));
  230.  
  231.         if (matchup == TypeMatchup.HYPER_EFFECTIVE || matchup == TypeMatchup.SUPER_EFFECTIVE) {
  232.             result.add("It's super effective!");
  233.             result.add("**-" + damage + "**");
  234.         } else if (matchup == TypeMatchup.RESISTANT || matchup == TypeMatchup.SUPER_RESISTANT) {
  235.             result.add("It's not very effective...");
  236.             result.add("**-" + damage + "**");
  237.         }
  238.  
  239.         if (fainted) {
  240.             result.add(opposingPokemon.getDisplayName() + " fainted!");
  241.             return result;
  242.         }
  243.     } else {
  244.         damage = 0;
  245.     }
  246.  
  247.     // Execute all special effects
  248.     for (Map.Entry<Effect, Object>  entry : effects.entrySet()) {
  249.         Effect effect = entry.getKey();
  250.         Object value = entry.getValue();
  251.  
  252.         if (effect.toString().startsWith("RAISE_")) {
  253.             Statistic stat = Statistic.fromString(effects.toString().substring(6));
  254.  
  255.             int stages = (int) value;
  256.  
  257.             if (moveTarget == Target.SELF || moveTarget == Target.SINGLE_ADJACENT_ALLY || moveTarget == Target.ALL_ADJACENT_ALLIES || moveTarget == Target.ALL_ALLIES || effect.toString().contains("SELF")) {
  258.                 pokemon.raiseStat(stat, stages);
  259.  
  260.                 if (stages == 1) {
  261.                     result.add(pokemon.getDisplayName() + "'s " + stat.getName() + " rose!");
  262.                 } else if (stages == 2) {
  263.                     result.add(pokemon.getDisplayName() + "'s " + stat.getName() + " rose sharply!");
  264.                 } else {
  265.                     result.add(pokemon.getDisplayName() + "'s " + stat.getName() + " rose drastically!");
  266.                 }
  267.             }
  268.             else if (moveTarget == Target.SINGLE_ADJACENT_FOE || moveTarget == Target.ALL_ADJACENT_FOES || moveTarget == Target.ALL_FOES) {
  269.                 opposingPokemon.raiseStat(stat, stages);
  270.  
  271.                 if (stages == 1) {
  272.                     result.add(opposingPokemon.getDisplayName() + "'s " + stat.getName() + " rose!");
  273.                 } else if (stages == 2) {
  274.                     result.add(opposingPokemon.getDisplayName() + "'s " + stat.getName() + " rose sharply!");
  275.                 } else {
  276.                     result.add(opposingPokemon.getDisplayName() + "'s " + stat.getName() + " rose drastically!");
  277.                 }
  278.             }
  279.         }
  280.  
  281.         else if (effect.toString().startsWith("LOWER_")) {
  282.             Statistic stat = Statistic.fromString(effects.toString().substring(6));
  283.  
  284.             int stages = (int) value;
  285.  
  286.             if (moveTarget == Target.SELF || moveTarget == Target.SINGLE_ADJACENT_ALLY || moveTarget == Target.ALL_ADJACENT_ALLIES || moveTarget == Target.ALL_ALLIES || effect.toString().contains("SELF")) {
  287.                 pokemon.lowerStat(stat, stages);
  288.  
  289.                 if (stages == 1) {
  290.                     result.add(pokemon.getDisplayName() + "'s " + stat.getName() + " fell!");
  291.                 } else if (stages == 2) {
  292.                     result.add(pokemon.getDisplayName() + "'s " + stat.getName() + " harshly fell!");
  293.                 } else {
  294.                     result.add(pokemon.getDisplayName() + "'s " + stat.getName() + " severely fell!");
  295.                 }
  296.             }
  297.             else if (moveTarget == Target.SINGLE_ADJACENT_FOE || moveTarget == Target.ALL_ADJACENT_FOES || moveTarget == Target.ALL_FOES) {
  298.                 opposingPokemon.lowerStat(stat, stages);
  299.  
  300.                 if (stages == 1) {
  301.                     result.add(opposingPokemon.getDisplayName() + "'s " + stat.getName() + " fell!");
  302.                 } else if (stages == 2) {
  303.                     result.add(opposingPokemon.getDisplayName() + "'s " + stat.getName() + " harshly fell!");
  304.                 } else {
  305.                     result.add(opposingPokemon.getDisplayName() + "'s " + stat.getName() + " severely fell!");
  306.                 }
  307.             }
  308.         }
  309.  
  310.         else if (effect == Effect.ABSORB) {
  311.             int percentage = (int) value;
  312.  
  313.             int absorb = (int) Math.floor(damage / (percentage / 100f));
  314.  
  315.             boolean fullyHealed = pokemon.heal(absorb);
  316.  
  317.             if (fullyHealed) {
  318.                 result.add(pokemon.getDisplayName() + "'s HP was fully restored!");
  319.             } else {
  320.                 result.add(pokemon.getDisplayName() + " was healed for " + absorb + " HP!");
  321.             }
  322.         }
  323.  
  324.         else if (effect == Effect.HEAL) {
  325.             int percentage = (int) value;
  326.  
  327.             if (moveTarget == Target.SELF || moveTarget == Target.SINGLE_ADJACENT_ALLY || moveTarget == Target.ALL_ADJACENT_ALLIES || moveTarget == Target.ALL_ALLIES) {
  328.                 int heal = (int) Math.floor(pokemon.getMaxHp() / (percentage / 100f));
  329.  
  330.                 pokemon.heal(heal);
  331.                 result.add(pokemon.getDisplayName() + " was healed for " + heal + " HP!");
  332.             }
  333.             else if(moveTarget == Target.SINGLE_ADJACENT_FOE || moveTarget == Target.ALL_ADJACENT_FOES || moveTarget == Target.ALL_FOES) {
  334.                 int heal = (int) Math.floor(opposingPokemon.getMaxHp() / (percentage / 100f));
  335.  
  336.                 opposingPokemon.heal(heal);
  337.                 result.add(opposingPokemon.getDisplayName() + " was healed for " + heal + " HP!");
  338.             }
  339.         }
  340.  
  341.         else if (effect == Effect.HEAL_SELF) {
  342.             int percentage = (int) value;
  343.  
  344.             int heal = (int) Math.floor(pokemon.getMaxHp() / (percentage / 100f));
  345.  
  346.             pokemon.heal(heal);
  347.             result.add(pokemon.getDisplayName() + " was healed for " + heal + " HP!");
  348.         }
  349.  
  350.         else if (effect == Effect.HEAL_EVERY_TURN) {
  351.             if (battler1HealEveryTurn != 0) {
  352.                 result.add("A move like this is already active!");
  353.             } else {
  354.                 battler1HealEveryTurn = (double) effects.get(Effect.HEAL_EVERY_TURN);
  355.             }
  356.         }
  357.  
  358.         else if (effect == Effect.HEAL_STATUS_CONDITION) {
  359.             StatusCondition condition = (StatusCondition) effects.get(effect);
  360.  
  361.             if (moveTarget == Target.SELF || moveTarget == Target.SINGLE_ADJACENT_ALLY || moveTarget == Target.ALL_ADJACENT_ALLIES || moveTarget == Target.ALL_ALLIES) {
  362.                 if (pokemon.hasStatusCondition(condition)) {
  363.                     pokemon.cureStatusCondition(condition);
  364.                     result.add(pokemon.getDisplayName() + "'s " + condition.getName() + " has been cured!");
  365.                 }
  366.             }
  367.             else if(moveTarget == Target.SINGLE_ADJACENT_FOE || moveTarget == Target.ALL_ADJACENT_FOES || moveTarget == Target.ALL_FOES) {
  368.                 if (opposingPokemon.hasStatusCondition(condition)) {
  369.                     opposingPokemon.cureStatusCondition(condition);
  370.                     result.add(opposingPokemon.getDisplayName() + "'s " + condition.getName() + " has been cured!");
  371.                 }
  372.             }
  373.         }
  374.  
  375.         else if (effect == Effect.HEAL_ALL_STATUS_CONDITIONS) {
  376.             if (moveTarget == Target.SELF || moveTarget == Target.SINGLE_ADJACENT_ALLY || moveTarget == Target.ALL_ADJACENT_ALLIES || moveTarget == Target.ALL_ALLIES) {
  377.                 pokemon.cureAllStatusConditions();
  378.                 result.add("All of " + pokemon.getDisplayName() + "'s status conditions have been cured!");
  379.             }
  380.             else if(moveTarget == Target.SINGLE_ADJACENT_FOE || moveTarget == Target.ALL_ADJACENT_FOES || moveTarget == Target.ALL_FOES) {
  381.                 opposingPokemon.cureAllStatusConditions();
  382.                 result.add("All of " + opposingPokemon.getDisplayName() + "'s status conditions have been cured!");
  383.             }
  384.         }
  385.  
  386.         else if (effect == Effect.DAMAGE_SELF) {
  387.             int percentage = (int) effects.get(effect);
  388.  
  389.             pokemon.dealDamage((int) Math.floor(battler1.getMaxHp() / (percentage / 100f)));
  390.         }
  391.  
  392.         else if (effect == Effect.RECOIL_DAMAGE) {
  393.             int percentage = (int) effects.get(effect);
  394.  
  395.             pokemon.dealDamage((int) Math.floor(damage / (percentage / 100f)));
  396.         }
  397.  
  398.         else if (effect == Effect.CLEAR_STAT_CHANGES) {
  399.             if (moveTarget == Target.SELF || moveTarget == Target.SINGLE_ADJACENT_ALLY || moveTarget == Target.ALL_ADJACENT_ALLIES || moveTarget == Target.ALL_ALLIES) {
  400.                 pokemon.clearAllStatChanges();
  401.                 result.add(pokemon.getDisplayName() + "'s stat changes have been cleared!");
  402.             }
  403.             else if(moveTarget == Target.SINGLE_ADJACENT_FOE || moveTarget == Target.ALL_ADJACENT_FOES || moveTarget == Target.ALL_FOES) {
  404.                 opposingPokemon.clearAllStatChanges();
  405.                 result.add(opposingPokemon.getDisplayName() + "'s stat changes have been cleared!");
  406.             }
  407.             else {
  408.                 pokemon.clearAllStatChanges();
  409.                 opposingPokemon.clearAllStatChanges();
  410.                 result.add(opposingPokemon.getDisplayName() + "'s stat changes have been cleared!");
  411.             }
  412.         }
  413.  
  414.         else if (effect == Effect.CLEAR_ALL_STAT_CHANGES) {
  415.             pokemon.clearAllStatChanges();
  416.             opposingPokemon.clearAllStatChanges();
  417.             result.add(opposingPokemon.getDisplayName() + "'s stat changes have been cleared!");
  418.         }
  419.     }
  420.  
  421.     return result;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment