No Description

battle_help.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_help.c
  24. * @author Sturmvogel
  25. * @date 15 dec 2016
  26. * @brief Some general helper methods for battle
  27. */
  28. /* === INCLUDE === */
  29. #include <battle_common.h>
  30. #include <battle_custom_structs.h>
  31. #include <battle_help.h>
  32. #include <battle_locations.h>
  33. #include <game_engine.h>
  34. #include <pkmn_abilities.h>
  35. #include <pkmn_attributes.h>
  36. #include <pkmn_item_effects.h>
  37. #include <pkmn_items.h>
  38. #include <pkmn_types.h>
  39. /* === STATICS === */
  40. /* from kds emerald battle engine upgrade */
  41. u8 type_effectiveness_table[TYPE_FAIRY - 0x4][TYPE_FAIRY - 0x4] = {
  42. {10, 10, 10, 10, 10, 05, 10, 00, 05, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, // normal
  43. {20, 10, 05, 05, 10, 20, 05, 00, 20, 10, 10, 10, 10, 10, 05, 20, 10, 20, 05}, // fight
  44. {10, 20, 10, 10, 10, 05, 20, 10, 05, 10, 10, 10, 20, 05, 10, 10, 10, 10, 10}, // flying
  45. {10, 10, 10, 05, 05, 05, 10, 05, 00, 10, 10, 10, 20, 10, 10, 10, 10, 10, 20}, // poison
  46. {10, 10, 00, 20, 10, 20, 05, 10, 20, 10, 20, 10, 05, 20, 10, 10, 10, 10, 10}, // ground
  47. {10, 05, 20, 10, 05, 10, 20, 10, 05, 10, 20, 10, 10, 10, 10, 20, 10, 10, 10}, // rock
  48. {10, 05, 05, 05, 10, 10, 10, 05, 05, 10, 05, 10, 20, 10, 20, 10, 10, 20, 05}, // bug
  49. {00, 10, 10, 10, 10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 20, 10, 10, 05, 10}, // ghost
  50. {10, 10, 10, 10, 10, 20, 10, 10, 05, 10, 05, 05, 10, 05, 10, 20, 10, 10, 20}, // steel
  51. {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, // egg
  52. {10, 10, 10, 10, 10, 05, 20, 10, 20, 10, 05, 05, 20, 10, 10, 20, 05, 10, 10}, // fire
  53. {10, 10, 10, 10, 20, 20, 10, 10, 10, 10, 20, 05, 05, 10, 10, 10, 05, 10, 10}, // water
  54. {10, 10, 05, 05, 20, 20, 05, 10, 05, 10, 05, 20, 05, 10, 10, 10, 05, 10, 10}, // grass
  55. {10, 10, 20, 10, 00, 10, 10, 10, 10, 10, 10, 20, 05, 05, 10, 10, 05, 10, 10}, // electric
  56. {10, 20, 10, 20, 10, 10, 10, 10, 05, 10, 10, 10, 10, 10, 05, 10, 10, 00, 10}, // psychic
  57. {10, 10, 20, 10, 20, 10, 10, 10, 05, 10, 05, 05, 20, 10, 10, 05, 20, 10, 10}, // ice
  58. {10, 10, 10, 10, 10, 10, 10, 10, 05, 10, 10, 10, 10, 10, 10, 10, 20, 10, 00}, // dragon
  59. {10, 05, 10, 10, 10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 20, 10, 10, 05, 05}, // dark
  60. {10, 20, 10, 05, 10, 10, 10, 10, 05, 10, 05, 10, 10, 10, 10, 10, 20, 20, 10} // fairy
  61. };
  62. /* === IMPLEMENTATIONS === */
  63. u16 battle_damage_type_effectiveness_update(u8 attacking_type, u8 defending_type, u8 atk_bank, u8 def_bank,
  64. u16 chained_effect, u8 airstatus) {
  65. u8 effect, atype = attacking_type, dtype = defending_type;
  66. if (!chained_effect || atype == TYPE_EGG || dtype == TYPE_EGG)
  67. return chained_effect;
  68. if (atype >= TYPE_FAIRY)
  69. atype = atype - 5;
  70. if (dtype >= TYPE_FAIRY)
  71. dtype = dtype - 5;
  72. effect = type_effectiveness_table[atype][dtype];
  73. if (custom_battle_elements.ptr->various.inverse_battle) {
  74. if (effect == 20) {
  75. effect = 5;
  76. } else if (effect == 5 || effect == 0) {
  77. effect = 20;
  78. }
  79. }
  80. /* handle normal / fighting on ghost */
  81. if ((((attacking_type == TYPE_NORMAL || attacking_type == TYPE_FIGHTING) && defending_type == TYPE_GHOST &&
  82. ((battle_participants[def_bank].status2.foresight))) ||
  83. battle_participants[atk_bank].ability_id == ABILITY_SCRAPPY) &&
  84. effect == 0) {
  85. effect = 10;
  86. }
  87. /* handle other effectiveness changers here */
  88. switch (effect) {
  89. case 0:
  90. chained_effect = 0;
  91. break;
  92. case 5:
  93. chained_effect = chained_effect >> 1;
  94. break;
  95. case 20:
  96. chained_effect = chained_effect << 1;
  97. break;
  98. }
  99. return chained_effect;
  100. }
  101. u16 battle_apply_type_effectiveness(u16 chained_effect, u8 move_type, u8 target_bank, u8 atk_bank, u8 airstatus) {
  102. u8 defender_type1 = battle_participants[target_bank].type1;
  103. u8 defender_type2 = battle_participants[target_bank].type2;
  104. /* set different types */
  105. if (defender_type2 == defender_type1)
  106. defender_type2 = TYPE_EGG;
  107. chained_effect = battle_damage_type_effectiveness_update(move_type, defender_type1, atk_bank, target_bank,
  108. chained_effect, airstatus);
  109. chained_effect = battle_damage_type_effectiveness_update(move_type, defender_type2, atk_bank, target_bank,
  110. chained_effect, airstatus);
  111. return chained_effect;
  112. }
  113. u16 battle_type_effectiveness_calc(u16 move, u8 move_type, u8 atk_bank, u8 def_bank,
  114. u8 effects_handling_and_recording) {
  115. u16 chained_effect = 64;
  116. // TODO: double_type moves
  117. // TODO: get air status
  118. chained_effect = battle_apply_type_effectiveness(chained_effect, move_type, def_bank, atk_bank, 2);
  119. // TODO: save into structs
  120. // TODO: effect_handling_and_recording
  121. return chained_effect;
  122. }
  123. u8 battle_bank_has_type(u8 bank, u8 type) {
  124. return battle_participants[bank].type1 == type || battle_participants[bank].type2 == type;
  125. }
  126. u8 battle_item_get_effect(u8 bank, u8 check_negating_effects) {
  127. if (check_negating_effects) {
  128. if (battle_participants[bank].ability_id == ABILITY_KLUTZ ||
  129. custom_battle_elements.ptr->bank_affecting[bank].embargo)
  130. return ITEM_EFFECT_NOEFFECT;
  131. }
  132. if (battle_participants[bank].held_item == ITEM_ENIGMABERRY) {
  133. return battle_enigma_berry[bank].battle_effect_x12;
  134. } else {
  135. return item_get_x12(battle_participants[bank].held_item);
  136. }
  137. }
  138. u8 battle_bank_is_poison_resistant(u8 bank, u8 self_inflicted) { // 0 == can poison
  139. // 1 == is already poisoned
  140. // 2 == has other major condition
  141. // 3 == type doesn't allow it
  142. // 4 == ability doesn't allow it
  143. // 5 == safeguard protection
  144. // 8 == misty terrain doesn't allow it !TODO!
  145. if (battle_participants[bank].status.flags.poison || battle_participants[bank].status.flags.toxic_poison)
  146. return 1;
  147. if (battle_participants[bank].status.int_status)
  148. return 2;
  149. if (battle_bank_has_type(bank, TYPE_POISON) || battle_bank_has_type(bank, TYPE_STEEL))
  150. return 3;
  151. if (((battle_participants[bank].ability_id == ABILITY_IMMUNITY ||
  152. (battle_participants[bank].ability_id == ABILITY_LEAF_GUARD &&
  153. (battle_weather.flags.sun || battle_weather.flags.permament_sun || battle_weather.flags.harsh_sun)))))
  154. return 4;
  155. if (side_affecting_halfword[get_side_from_bank(bank)].safeguard_on && !self_inflicted)
  156. return 5;
  157. return 0;
  158. }
  159. u8 battle_count_party_pokemon(u8 bank) {
  160. struct pokemon *poke;
  161. if (get_side_from_bank(bank))
  162. poke = &party_opponent[0];
  163. else
  164. poke = &party_player[0];
  165. u8 usable_pokes = 0;
  166. for (u8 i = 0; i < 6; i++) {
  167. if (pokemon_get_attribute(&poke[i], ATTR_CURRENT_HP, 0) != 0 &&
  168. pokemon_get_attribute(&poke[i], ATTR_IS_EGG, 0) != 1 &&
  169. pokemon_get_attribute(&poke[i], ATTR_SPECIES, 0) != 0)
  170. usable_pokes++;
  171. }
  172. return usable_pokes;
  173. }