No Description

evolution_methods.c 17KB

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