Нема описа

battle_initiative.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /****************************************************************************
  2. * Copyright (C) 2015-2016 by the SotS Team *
  3. * *
  4. * This file is part of Sovereign of the Skies. *
  5. * *
  6. * Sovereign of the Skies is free software: you can redistribute it *
  7. * and/or modify it *
  8. * under the terms of the GNU Lesser General Public License as published *
  9. * by the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version provided you include a copy of the *
  11. * licence and this header. *
  12. * *
  13. * Sovereign of the Skies is distributed in the hope that it will be *
  14. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU Lesser General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU Lesser General Public *
  19. * License along with Sovereign of the Skies. *
  20. * If not, see <http://www.gnu.org/licenses/>. *
  21. ****************************************************************************/
  22. /**
  23. * @file battle_initiative.c
  24. * @author Sturmvogel
  25. * @date 15 dec 2016
  26. * @brief Methods calculating speed based stats in-battle
  27. */
  28. /* === INCLUDE === */
  29. #include <battle_abilities.h>
  30. #include <battle_common.h>
  31. #include <battle_custom_structs.h>
  32. #include <battle_fractions.h>
  33. #include <battle_help.h>
  34. #include <battle_initiative.h>
  35. #include <battle_locations.h>
  36. #include <battle_structs.h>
  37. #include <constants/abilities.h>
  38. #include <math.h>
  39. #include <pkmn_item_effects.h>
  40. #include <pokemon.h>
  41. /* === EXTERN STRUCTS === */
  42. struct move_info move_table[1024];
  43. /* === IMPLEMENTATIONS === */
  44. u16 get_speed(u8 bank) {
  45. u32 speed = battle_participants[bank].spd << 16;
  46. switch (battle_item_get_effect(bank, 1)) {
  47. case ITEM_EFFECT_IRONBALL:
  48. speed >>= 1;
  49. break;
  50. case ITEM_EFFECT_CHOICESCARF:
  51. speed = (speed * 150) / 100;
  52. break;
  53. case ITEM_EFFECT_QUICKPOWDER:
  54. if (battle_participants[bank].poke_species == POKE_DITTO && !battle_participants[bank].status2.transformed)
  55. speed <<= 1;
  56. break;
  57. }
  58. if (ability_has_effect(bank, 0, 1)) {
  59. u8 weather_effects = ability_weather_effects();
  60. switch (battle_participants[bank].ability_id) {
  61. case ABILITY_CHLOROPHYLL:
  62. if (weather_effects &&
  63. (battle_weather.flags.harsh_sun || battle_weather.flags.permament_sun || battle_weather.flags.sun))
  64. speed *= 2;
  65. break;
  66. case ABILITY_SWIFT_SWIM:
  67. if (weather_effects && (battle_weather.flags.rain || battle_weather.flags.downpour ||
  68. battle_weather.flags.permament_rain || battle_weather.flags.heavy_rain))
  69. speed *= 2;
  70. break;
  71. case ABILITY_SAND_RUSH:
  72. if (weather_effects && (battle_weather.flags.sandstorm || battle_weather.flags.permament_sandstorm))
  73. speed *= 2;
  74. break;
  75. case ABILITY_QUICK_FEET:
  76. if (battle_participants[bank].status.flags.burn || battle_participants[bank].status.flags.poison ||
  77. battle_participants[bank].status.flags.toxic_poison)
  78. speed = speed + ((speed * 50) / 100);
  79. else if (battle_participants[bank].status.flags.paralysis) {
  80. /* cancel para */
  81. speed *= 4;
  82. speed = speed + ((speed * 50) / 100);
  83. }
  84. break;
  85. }
  86. }
  87. if (battle_participants[bank].status.flags.paralysis)
  88. speed >>= 2;
  89. if (custom_battle_elements.ptr->side_affecting[get_side_from_bank(bank)].tailwind)
  90. speed *= 2;
  91. if (battle_status_3[bank].unburden)
  92. speed *= 2;
  93. // TODO: /* SWAMP EFFECT */
  94. speed = (speed * stat_buffs[battle_participants[bank].spd_buff].dividend) /
  95. (stat_buffs[battle_participants[bank].spd_buff].divisor);
  96. return (u16)(speed >> 16);
  97. }
  98. u8 speed_alt_from_item(u8 bank, u8 item_effect) {
  99. switch (item_effect) {
  100. case ITEM_EFFECT_QUICKCLAW:
  101. if ((battle_turn_random % 100) > item_get_quality(battle_participants[bank].held_item)) {
  102. return 1;
  103. }
  104. break;
  105. case ITEM_EFFECT_CUSTAPBERRY:
  106. // TODO: implement HP CONDITION
  107. return 1;
  108. break;
  109. case ITEM_EFFECT_LAGGINGTAIL:
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. enum init_enum get_first_to_strike(u8 bank_one, u8 bank_two, u8 ignore_prio) {
  115. enum init_enum result = TIE;
  116. u8 quash_one = custom_battle_elements.ptr->bank_affecting[bank_one].quashed;
  117. u8 quash_two = custom_battle_elements.ptr->bank_affecting[bank_two].quashed;
  118. if (quash_one && !quash_two)
  119. result = TWO;
  120. else if (!quash_one && quash_two)
  121. result = ONE;
  122. else if (!ignore_prio) {
  123. u16 move_one = battle_participants[bank_one].moves[battle_stuff_ptr.ptr->chosen_move_position[bank_one]];
  124. u16 move_two = battle_participants[bank_two].moves[battle_stuff_ptr.ptr->chosen_move_position[bank_two]];
  125. s8 prio_one = 0;
  126. s8 prio_two = 0;
  127. /* note priority changing effects and abilities not added as of yet */
  128. /* NOTE: !HANDLE PRIORITY LIKE A PROPERTY! */
  129. if (battle_menu_chosen_item[bank_one] == 0)
  130. prio_one = move_table[move_one].priority;
  131. if (battle_menu_chosen_item[bank_two] == 0)
  132. prio_two = move_table[move_two].priority;
  133. if (prio_one > prio_two)
  134. result = ONE;
  135. else if (prio_two > prio_one)
  136. result = TWO;
  137. }
  138. if (result == TIE) {
  139. s8 brack_one = speed_alt_from_item(bank_one, battle_item_get_effect(bank_one, true));
  140. s8 brack_two = speed_alt_from_item(bank_two, battle_item_get_effect(bank_two, true));
  141. if (brack_one > brack_two)
  142. result = ONE;
  143. else if (brack_two > brack_one)
  144. result = TWO;
  145. else {
  146. u8 stall_one = ability_has_ability(bank_one, ABILITY_STALL);
  147. u8 stall_two = ability_has_ability(bank_two, ABILITY_STALL);
  148. if (stall_one && !stall_two)
  149. result = TWO;
  150. else if (stall_two && !stall_one)
  151. result = ONE;
  152. else {
  153. u16 speed_one = get_speed(bank_one);
  154. u16 speed_two = get_speed(bank_two);
  155. if (custom_battle_elements.ptr->field_affecting.trick_room || (stall_one && stall_two)) {
  156. u16 swap_speed = speed_one;
  157. speed_one = speed_two;
  158. speed_two = swap_speed;
  159. }
  160. if (speed_one > speed_two)
  161. result = ONE;
  162. else if (speed_two > speed_one)
  163. result = TWO;
  164. else if (random() & 1)
  165. result = ONE;
  166. }
  167. }
  168. }
  169. return result;
  170. }