Geen omschrijving

flag_manipulation.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #define TRAINER_FLAG_SPACE_START 0x1000
  30. extern u16 tb_modify_flag_id(u16 flag_id);
  31. u16 trainerflag_fix_difficulty(u16 flag_id);
  32. u16 load_hword(void* ptr)
  33. {
  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)
  40. {
  41. return (u8) (*((u8*)ptr));
  42. }
  43. u16 trainerflag_fix_difficulty(u16 flag_id)
  44. {
  45. u16 new_flag = ((flag_id - TRAINER_FLAG_SPACE_START + 1) / 3) + TRAINER_FLAG_SPACE_START;
  46. dprintf("trainerflag_fix_difficulty;; flag_id: 0x%x, reduced: 0x%x, status: %s\n", flag_id, new_flag, flag_check(new_flag) ? "true" : "false");
  47. return new_flag;
  48. }
  49. bool trainer_check_flag_on_spot(u8 npc_id)
  50. {
  51. void* script = npc_get_script_by_npc_id(npc_id);
  52. /* probably inject some script execution here */
  53. u16 flag = tb_modify_flag_id(load_hword(script+2));
  54. /* check for line of sight */
  55. u8 hit_result = npc_trainer_and_raycast_hit(&npc_states[npc_id]);
  56. if(hit_result == 0)
  57. return false;
  58. if(flag_check(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START)))
  59. return false;
  60. if((load_byte(script + 1) == 4) && (player_cant_double_battle() > 0))
  61. return false;
  62. spot_trainer_8080334(npc_id, script);
  63. spot_trainer_8081E68(&npc_states[npc_id], hit_result -1);
  64. return true;
  65. }
  66. u8 trainerflag_read_fix(void* script_location)
  67. {
  68. script_location+=2;
  69. u16 flag = load_hword(script_location);
  70. //dprintf("trainerflag_read_fix: 0x%x", flag);
  71. flag = trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START);
  72. return flag_check(flag);
  73. }
  74. volatile u8 test;
  75. u16 trainerflag_opponent_fix(void)
  76. {
  77. return trainerflag_fix_difficulty(trainerbattle_flag_id + TRAINER_FLAG_SPACE_START);
  78. }
  79. bool trainerflag_check_fix(u16 flag)
  80. {
  81. return flag_check(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START));
  82. }
  83. void trainerflag_set_fix(u16 flag)
  84. {
  85. flag_set(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START));
  86. }
  87. void trainerflag_clear_fix(u16 flag)
  88. {
  89. flag_clear(trainerflag_fix_difficulty(flag + TRAINER_FLAG_SPACE_START));
  90. }
  91. void flag_set(u16 flag)
  92. {
  93. u8* addr = flag_byte_access(flag);
  94. if(addr != NULL)
  95. *addr |= 1 << (flag & 7);
  96. }