Няма описание

flag_manipulation.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 flag_manipulation.c
  24. * @author Sturmvogel
  25. * @date 25 may 2017
  26. * @brief functions for preserving flags and reusing difficulty
  27. */
  28. #include <pokeagb/pokeagb.h>
  29. #include <agb_debug.h>
  30. #define TRAINER_FLAG_SPACE_START 0x1000
  31. extern u16 tb_modify_flag_id(u16 flag_id);
  32. u16 trainerflag_fix_difficulty(u16 flag_id);
  33. u16 load_hword(void *ptr) {
  34. u8 *to_load = (u8 *)ptr;
  35. u16 result = *to_load;
  36. u16 result2 = *(to_load + 1) << 8;
  37. return result | result2;
  38. }
  39. u8 load_byte(void *ptr) { return (u8)(*((u8 *)ptr)); }
  40. u16 trainerflag_fix_difficulty(u16 flag_id) {
  41. u16 new_flag = ((flag_id - TRAINER_FLAG_SPACE_START + 1) / 3) + TRAINER_FLAG_SPACE_START;
  42. dprintf("trainerflag_fix_difficulty;; flag_id: 0x%x, reduced: 0x%x, status: %s\n", flag_id, new_flag,
  43. flag_check(new_flag) ? "true" : "false");
  44. return new_flag;
  45. }
  46. bool trainer_check_flag_on_spot(u8 npc_id) {
  47. void *script = npc_get_script_by_npc_id(npc_id);
  48. /* probably inject some script execution here */
  49. u16 flag = tb_modify_flag_id(load_hword(script + 2));
  50. /* check for line of sight */
  51. u8 hit_result = npc_trainer_and_raycast_hit(&npc_states[npc_id]);
  52. if (hit_result == 0)
  53. return false;
  54. if (flag_check(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START)))
  55. return false;
  56. if ((load_byte(script + 1) == 4) && (player_cant_double_battle() > 0))
  57. return false;
  58. spot_trainer_8080334(npc_id, script);
  59. spot_trainer_8081E68(&npc_states[npc_id], hit_result - 1);
  60. return true;
  61. }
  62. u8 trainerflag_read_fix(void *script_location) {
  63. script_location += 2;
  64. u16 flag = load_hword(script_location);
  65. // dprintf("trainerflag_read_fix: 0x%x", flag);
  66. flag = trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START);
  67. return flag_check(flag);
  68. }
  69. volatile u8 test;
  70. u16 trainerflag_opponent_fix(void) {
  71. return trainerflag_fix_difficulty(trainerbattle_flag_id + TRAINER_FLAG_SPACE_START);
  72. }
  73. bool trainerflag_check_fix(u16 flag) { return flag_check(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START)); }
  74. void trainerflag_set_fix(u16 flag) { flag_set(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START)); }
  75. void trainerflag_clear_fix(u16 flag) { flag_clear(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START)); }
  76. void flag_set_hack(u16 flag) {
  77. u8 *addr = flag_byte_access(flag);
  78. if (addr != NULL) {
  79. dprintf("flag 0x%x was set\n", flag);
  80. *addr |= 1 << (flag & 7);
  81. }
  82. }
  83. u16 var_get_hack(u16 var) {
  84. u16 *ptr = var_access(var);
  85. if (ptr != NULL)
  86. return *ptr;
  87. return var;
  88. }
  89. bool var_set_hack(u16 var, u16 val) {
  90. u16 *ptr = var_access(var);
  91. if (ptr != NULL) {
  92. dprintf("variable 0x%x was set to 0x%x (Address: 0x%x)\n", var, val, ptr);
  93. *ptr = val;
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool var_set_script_hack(struct ScriptEnvironment *env) {
  99. u16 var = script_read_halfword(env);
  100. u16 val = script_read_halfword(env);
  101. var_set_hack(var, val);
  102. return 0;
  103. }