No Description

evolution_methods.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /****************************************************************************
  2. * Copyright (C) 2015-2017 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 evolution_methods.c
  24. * @author Sturmvogel
  25. * @date 18 apr 2017
  26. * @brief decide if a pokémon is able to evolve and what the consequences are.
  27. *
  28. */
  29. #include <game_engine.h>
  30. #include <pkmn_attributes.h>
  31. #include <agb_debug.h>
  32. #include <math.h>
  33. #include <config/core.h>
  34. #include <constants/moves.h>
  35. #include <pokemon.h>
  36. #include <constants/ptypes.h>
  37. #include <evolution_methods.h>
  38. #define EVO_NULL \
  39. { \
  40. 0, 0, 0, { false, 0 } \
  41. }
  42. #define MAX_EVOLUTIONS 5
  43. #define EVO_NO_EVO \
  44. (struct evo_result) { false, false, 0 }
  45. #define HAPPY_BOUND 219
  46. #define BEAUTY_BOUND 170
  47. enum evo_source
  48. {
  49. LEVEL_UP,
  50. TRADE,
  51. STONE_EVOLUTION,
  52. STONE_REQUEST
  53. };
  54. struct evo_information
  55. {
  56. u16 method;
  57. u16 argument;
  58. u16 evolve_to;
  59. struct
  60. {
  61. u16 gender : 2;
  62. u16 versatile : 14;
  63. } argument_2;
  64. };
  65. struct evo_result
  66. {
  67. u8 can_evolve;
  68. u8 consume_item;
  69. u8 evolve_to;
  70. };
  71. struct evo_information evolutions[][MAX_EVOLUTIONS] = {
  72. {EVO_NULL, EVO_NULL, EVO_NULL, EVO_NULL, EVO_NULL}, //Nothing
  73. {{EVO_LEVEL_MOVE_TYPE, 7, 20, {GENDER_DC, TYPE_GRASS}}, EVO_NULL, EVO_NULL, EVO_NULL, EVO_NULL}, //BISASAM
  74. {{EVO_LEVEL_UP, 32, 3, {GENDER_DC, 0}}, EVO_NULL, EVO_NULL, EVO_NULL, EVO_NULL}, //BISAKNOSP
  75. };
  76. struct evo_call_arguments
  77. {
  78. u16 item;
  79. u16 level;
  80. enum evo_source source;
  81. u16 stoneId;
  82. struct pokemon *poke;
  83. struct evo_information evolution;
  84. };
  85. typedef struct evo_result (*evolution_callback)(struct evo_call_arguments);
  86. struct evo_result evolve_by_level(struct evo_call_arguments arguments)
  87. {
  88. u8 gender = pokemon_get_gender(arguments.poke);
  89. u8 gender_arg = arguments.evolution.argument_2.gender;
  90. dprintf("A pokemon with gender value %d\n", gender);
  91. if (gender_arg == 1)
  92. {
  93. if (gender)
  94. return EVO_NO_EVO;
  95. }
  96. if (gender_arg == 2)
  97. {
  98. if (!gender)
  99. return EVO_NO_EVO;
  100. }
  101. if (arguments.evolution.argument <= arguments.level && arguments.source == LEVEL_UP)
  102. {
  103. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  104. }
  105. else
  106. {
  107. return (struct evo_result){false, false, 0};
  108. }
  109. }
  110. struct evo_result evolve_by_trade_group(struct evo_call_arguments arguments)
  111. {
  112. if (arguments.source != TRADE)
  113. {
  114. return EVO_NO_EVO;
  115. }
  116. if (arguments.evolution.method == EVO_TRADE_ITEM)
  117. {
  118. if (arguments.item != arguments.evolution.argument)
  119. return (struct evo_result){false, false, 0};
  120. else
  121. return (struct evo_result){true, true, arguments.evolution.evolve_to};
  122. }
  123. else if (arguments.evolution.method == EVO_TRADE)
  124. {
  125. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  126. }
  127. else
  128. {
  129. dprintf("An invalid trade group method was reached in \"evolve_by_trade_group\"\nmethod: %d", arguments.evolution.method);
  130. return (struct evo_result){false, false, 0};
  131. }
  132. }
  133. struct evo_result evolve_random(struct evo_call_arguments arguments)
  134. {
  135. if (arguments.source != LEVEL_UP)
  136. {
  137. return EVO_NO_EVO;
  138. }
  139. u32 pid = pokemon_get_attribute(arguments.poke, ATTR_PID, NULL);
  140. pid = pid & 0xFFFF;
  141. u8 mod = (pid % 10);
  142. dprintf("A pokemon tries to evolve at random: pid: %d, low: %d, mod: %d\n", pid, pid, mod);
  143. if (mod >= 5)
  144. {
  145. if (arguments.evolution.method == EVO_PERSO_HIGH)
  146. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  147. else
  148. return EVO_NO_EVO;
  149. }
  150. else
  151. {
  152. if (arguments.evolution.method == EVO_PERSO_LOW)
  153. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  154. else
  155. return EVO_NO_EVO;
  156. }
  157. }
  158. struct evo_result evolve_by_stone(struct evo_call_arguments arguments)
  159. {
  160. u8 gender = pokemon_get_gender(arguments.poke);
  161. u8 gender_arg = arguments.evolution.argument_2.gender;
  162. if (gender_arg == 1)
  163. {
  164. if (gender)
  165. return EVO_NO_EVO;
  166. }
  167. if (gender_arg == 2)
  168. {
  169. if (!gender)
  170. return EVO_NO_EVO;
  171. }
  172. if (arguments.source != STONE_EVOLUTION && arguments.source != STONE_REQUEST)
  173. {
  174. return (struct evo_result){false, false, 0};
  175. }
  176. if (arguments.stoneId == arguments.evolution.argument)
  177. {
  178. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  179. }
  180. else
  181. {
  182. return (struct evo_result){false, false, 0};
  183. }
  184. }
  185. struct evo_result evolve_by_pokemon(struct evo_call_arguments arguments)
  186. {
  187. u8 has_required_pokemon = false;
  188. u16 species_required = arguments.evolution.argument_2.versatile;
  189. dprintf("Required: %d\n", species_required);
  190. for (int i = 0; i < 6; ++i)
  191. {
  192. u16 current_species = pokemon_get_attribute(&(pokemon_party_player[i]), ATTR_SPECIES, NULL);
  193. dprintf("Found pkmn: %d\n", current_species);
  194. if (current_species == species_required)
  195. {
  196. has_required_pokemon = true;
  197. break;
  198. }
  199. }
  200. if (!has_required_pokemon)
  201. return EVO_NO_EVO;
  202. return evolve_by_level(arguments);
  203. }
  204. struct evo_result evolve_by_atk_def(struct evo_call_arguments arguments)
  205. {
  206. u32 atk = pokemon_get_attribute(arguments.poke, ATTR_ATTACK, NULL);
  207. u32 def = pokemon_get_attribute(arguments.poke, ATTR_DEFENCE, NULL);
  208. dprintf("A pokemon wants to evolve by atk and def.\n");
  209. dprintf("Level required: %d, pkmn level: %d, pkmn atk: %d, pkmn def: %d\n", arguments.evolution.argument, arguments.level, atk, def);
  210. if (arguments.evolution.method == EVO_ATK)
  211. {
  212. if (atk > def)
  213. return evolve_by_level(arguments);
  214. else
  215. return EVO_NO_EVO;
  216. }
  217. if (arguments.evolution.method == EVO_DEF)
  218. {
  219. if (def > atk)
  220. return evolve_by_level(arguments);
  221. else
  222. return EVO_NO_EVO;
  223. }
  224. if (arguments.evolution.method == EVO_ADEQU)
  225. {
  226. if (atk == def)
  227. return evolve_by_level(arguments);
  228. else
  229. return EVO_NO_EVO;
  230. }
  231. dprintf("invalid atk and def evo code reached.\n");
  232. return EVO_NO_EVO;
  233. }
  234. struct evo_result evolve_by_type(struct evo_call_arguments arguments)
  235. {
  236. u8 has_required_pokemon = false;
  237. u16 type_required = arguments.evolution.argument_2.versatile;
  238. dprintf("Required: %d\n", type_required);
  239. for (int i = 0; i < 6; ++i)
  240. {
  241. u16 current_species = pokemon_get_attribute(&(pokemon_party_player[i]), ATTR_SPECIES, NULL);
  242. if (current_species == 0)
  243. continue;
  244. u8 type_one = pokemon_base_stats[current_species].type_one;
  245. u8 type_two = pokemon_base_stats[current_species].type_two;
  246. dprintf("Found type: %d/%d\n", type_one, type_two);
  247. if (type_one == type_required || type_two == type_required)
  248. {
  249. has_required_pokemon = true;
  250. break;
  251. }
  252. }
  253. if (!has_required_pokemon)
  254. return EVO_NO_EVO;
  255. return evolve_by_level(arguments);
  256. }
  257. struct evo_result evolve_by_happiness(struct evo_call_arguments arguments)
  258. {
  259. u32 happiness = pokemon_get_attribute(arguments.poke, ATTR_HAPPINESS, NULL);
  260. dprintf("A pokemon wants to evolve by happiness.\n");
  261. dprintf("Happiness value: %d; needed: %d\n", happiness, HAPPY_BOUND);
  262. if ((happiness > HAPPY_BOUND) && (arguments.source == LEVEL_UP))
  263. {
  264. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  265. }
  266. else
  267. {
  268. return (struct evo_result){false, false, 0};
  269. }
  270. }
  271. struct evo_result evolve_by_special_place(struct evo_call_arguments arguments)
  272. {
  273. u16 value = var_get(EVO_VAR);
  274. dprintf("A pokemon tried to evolve using the var evo method: value: %d needed: %d\n", value, arguments.evolution.argument_2.versatile);
  275. if (arguments.evolution.argument_2.versatile != value)
  276. {
  277. return EVO_NO_EVO;
  278. }
  279. return evolve_by_level(arguments);
  280. }
  281. struct evo_result evolve_by_beauty(struct evo_call_arguments arguments)
  282. {
  283. u32 beauty = pokemon_get_attribute(arguments.poke, ATTR_BEAUTY, NULL);
  284. dprintf("A pokemon tires to evolve by beauty: value: %d; required: %d.\n", beauty, BEAUTY_BOUND);
  285. if (beauty > BEAUTY_BOUND && arguments.source == LEVEL_UP)
  286. return evolve_by_level(arguments);
  287. return EVO_NO_EVO;
  288. }
  289. struct evo_result evolve_by_worn_item(struct evo_call_arguments arguments)
  290. {
  291. struct evo_result level_result = evolve_by_level(arguments);
  292. if (!level_result.can_evolve)
  293. return EVO_NO_EVO;
  294. u16 item_to_wear = arguments.evolution.argument_2.versatile;
  295. dprintf("A pokemon tried to evolve by item. pkmn_item: %d; argument item: %d", arguments.item, item_to_wear);
  296. if (arguments.item != item_to_wear)
  297. return EVO_NO_EVO;
  298. return (struct evo_result){true, false, arguments.evolution.evolve_to};
  299. }
  300. struct evo_result evolve_by_worn_item_day(struct evo_call_arguments arguments)
  301. {
  302. dprintf("A pokemon tried to use the (not implmented) evolve_by_worn_item_day evo method.\n");
  303. return evolve_by_worn_item(arguments);
  304. }
  305. struct evo_result evolve_by_worn_item_night(struct evo_call_arguments arguments)
  306. {
  307. dprintf("A pokemon tried to use the (not implmented) evolve_by_worn_item_night evo method.\n");
  308. return evolve_by_worn_item(arguments);
  309. }
  310. struct evo_result evolve_by_move(struct evo_call_arguments arguments)
  311. {
  312. u16 move_one = pokemon_get_attribute(arguments.poke, ATTR_ATTACK_1, NULL);
  313. u16 move_two = pokemon_get_attribute(arguments.poke, ATTR_ATTACK_2, NULL);
  314. u16 move_three = pokemon_get_attribute(arguments.poke, ATTR_ATTACK_3, NULL);
  315. u16 move_four = pokemon_get_attribute(arguments.poke, ATTR_ATTACK_4, NULL);
  316. u16 move_needed = arguments.evolution.argument_2.versatile;
  317. dprintf("A pokemon tried to evolve using evolve_by_move: needed: %d, one: %d, two: %d, three: %d, four: %d\n", move_needed, move_one, move_two, move_three, move_four);
  318. if ((move_one == move_needed) || (move_two == move_needed) || (move_three == move_needed) || (move_four == move_needed))
  319. return evolve_by_level(arguments);
  320. else
  321. return EVO_NO_EVO;
  322. }
  323. struct evo_result evolve_by_move_type(struct evo_call_arguments arguments)
  324. {
  325. u16 needed_type = arguments.evolution.argument_2.versatile;
  326. u8 knows_required_move = false;
  327. for (int i = ATTR_ATTACK_1; i <= ATTR_ATTACK_4; ++i)
  328. {
  329. u16 current_move = pokemon_get_attribute(arguments.poke, i, NULL);
  330. if (current_move == 0)
  331. continue;
  332. u8 current_type = move_table[current_move].type;
  333. dprintf("found move type: %d on move %d\n", current_type, current_move);
  334. if (current_type == needed_type)
  335. {
  336. knows_required_move = true;
  337. break;
  338. }
  339. }
  340. if (knows_required_move)
  341. return evolve_by_level(arguments);
  342. else
  343. return EVO_NO_EVO;
  344. }
  345. struct evo_result evolve_no_method(struct evo_call_arguments arguments)
  346. {
  347. (void)arguments;
  348. //For shedninja
  349. return EVO_NO_EVO;
  350. }
  351. struct evo_result evolve_invalid_method(struct evo_call_arguments arguments)
  352. {
  353. (void)arguments;
  354. dprintf("A pokemon tried to execute an evolution method that is not yet implemented.\n");
  355. return (struct evo_result){false, false, 0};
  356. }
  357. static evolution_callback evolution_methods[] =
  358. {
  359. evolve_invalid_method, //Method 0 INVALID
  360. evolve_by_happiness, //Method 1
  361. evolve_invalid_method, //TODO: Happiness DAY Method 2
  362. evolve_invalid_method, //TODO: Happiness NIGHT Method 3
  363. evolve_by_level, //Method 4
  364. evolve_by_trade_group, //Method 5
  365. evolve_by_trade_group, //Method 6
  366. evolve_by_stone, //Method 7
  367. evolve_by_atk_def, //Method 8
  368. evolve_by_atk_def, //Method 9
  369. evolve_by_atk_def, //Method 10
  370. evolve_random, //Method 11
  371. evolve_random, //Method 12
  372. evolve_by_level, //Shedninja SPAWN Method 13
  373. evolve_no_method, //Shedninja SPAWNED //Method 14
  374. evolve_by_beauty, //Method 15
  375. evolve_by_worn_item, //Method 16
  376. evolve_by_worn_item_day, //TODO implement day Method 17
  377. evolve_by_worn_item_night, //TODO implement night Method 18
  378. evolve_invalid_method, //TODO implement level night Method 19
  379. evolve_invalid_method, //TODO implement level day Method 20
  380. evolve_by_special_place, //Method 21
  381. evolve_by_move, //Method 22
  382. evolve_by_pokemon, //Method 23
  383. evolve_by_type, //Method 24
  384. evolve_by_move_type, //Method 25
  385. evolve_no_method, //Method 26
  386. evolve_no_method, //Method 26
  387. evolve_no_method, //Method 26
  388. evolve_no_method, //Method 26
  389. };
  390. u16 evolution_try_evolve(struct pokemon *pokemon, enum evo_source source, u16 stoneId)
  391. {
  392. u16 held_item = pokemon_get_attribute(pokemon, ATTR_HELD_ITEM, NULL);
  393. u16 species = pokemon_get_attribute(pokemon, ATTR_SPECIES, NULL);
  394. u16 level = pokemon_get_attribute(pokemon, ATTR_LEVEL, NULL);
  395. dprintf("Species %d tried to evolve.\n", species);
  396. dprintf("Cause: %d\n", source);
  397. if (species > 2)
  398. {
  399. dprintf("Currently no evolution possible due to short table!\n");
  400. return 0;
  401. }
  402. struct evo_information *current_evolution_structure = evolutions[species];
  403. struct evo_result result;
  404. for (int i = 0; i < MAX_EVOLUTIONS; ++i)
  405. {
  406. if (current_evolution_structure[i].method != 0)
  407. {
  408. dprintf("found valid evolution with method %d for species %d\n", current_evolution_structure[i].method, species);
  409. struct evo_call_arguments args = {held_item, level, source, stoneId, pokemon, current_evolution_structure[i]};
  410. result = evolution_methods[current_evolution_structure[i].method](args);
  411. if (result.can_evolve)
  412. break;
  413. }
  414. }
  415. if (result.can_evolve)
  416. {
  417. if (result.consume_item)
  418. {
  419. u16 zero = 0;
  420. pokemon_set_attribute(pokemon, ATTR_HELD_ITEM, &zero);
  421. }
  422. return result.evolve_to;
  423. }
  424. else
  425. return 0;
  426. }