暫無描述

dynamic_overworld.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <pokeagb/pokeagb.h>
  2. #define MAX_PAL_STORE 16
  3. #define STRANGE_NPC_MAX 4
  4. struct PalStoreEntry {
  5. u8 reference_count;
  6. u16 tag;
  7. };
  8. struct StrangeNpcStruct {
  9. u8 field0;
  10. u8 field1;
  11. u8 field2;
  12. u8 field3;
  13. };
  14. extern struct PalStoreEntry stored_palettes[16];
  15. extern struct NpcType *npc_get_type(u16 id);
  16. extern void dprintf(const char *str, ...);
  17. extern struct StrangeNpcStruct strange_npc_table[4];
  18. s8 npc_dynamic_find_palette(u16 tag) {
  19. for (s8 i = 0; i < MAX_PAL_STORE; ++i) {
  20. if (stored_palettes[i].reference_count > 0 && stored_palettes[i].tag == tag)
  21. return i;
  22. }
  23. return -1;
  24. }
  25. s8 npc_dynamic_allocate_palette(u16 tag) {
  26. for (s8 i = 0; i < MAX_PAL_STORE; ++i) {
  27. if (stored_palettes[i].reference_count == 0) {
  28. stored_palettes[i].tag = tag;
  29. stored_palettes[i].reference_count++;
  30. return i;
  31. }
  32. }
  33. return -1;
  34. }
  35. u8 npc_dynamic_load_palette(u16 tag) {
  36. s8 store_entry = npc_dynamic_find_palette(tag);
  37. if (store_entry != -1) {
  38. stored_palettes[store_entry].reference_count++;
  39. return store_entry;
  40. }
  41. store_entry = npc_dynamic_allocate_palette(tag);
  42. if (store_entry == -1) {
  43. /* we do not have allocation space left */
  44. dprintf("npc_dynamic: ATTENTION - TRIED TO ALLOCATE DYNOVER PALETTE WITHOUT SPACE LEFT, INCREASING ZERO "
  45. "REFERENCE\n");
  46. stored_palettes[0].reference_count++;
  47. return 0;
  48. }
  49. pal_patch_for_npc(tag, (u8)store_entry);
  50. return (u8)store_entry;
  51. }
  52. void npc_dynamic_reset() {
  53. for (u8 i = 0; i < MAX_PAL_STORE; ++i) {
  54. stored_palettes[i].reference_count = 0;
  55. stored_palettes[i].tag = 0;
  56. }
  57. }
  58. void npc_dynamic_remove_entry(u8 id) {
  59. if (stored_palettes[id].reference_count > 0) {
  60. stored_palettes[id].reference_count--;
  61. dprintf("npc_dynamic: removed entry #%d\n", id);
  62. if (stored_palettes[id].reference_count == 0)
  63. stored_palettes[id].tag = 0;
  64. }
  65. }
  66. void npc_restore_state(u8 id, u16 x, u16 y) {
  67. for (u8 i = 0; i < STRANGE_NPC_MAX; ++i) {
  68. if (strange_npc_table[i].field0 != 0) {
  69. if (strange_npc_table[i].field2 == id)
  70. return;
  71. }
  72. }
  73. struct NpcState *npc_to_load = &npc_states[id];
  74. struct NpcType *type_to_load = npc_get_type(((u16)npc_to_load->type_id) | (((u16)npc_to_load->field1A << 8)));
  75. struct Template template_to_load;
  76. u32 f14;
  77. npc_to_objtemplate__with_indexed_objfunc(npc_to_load->type_id, npc_to_load->running_behavior, &template_to_load,
  78. &f14);
  79. template_to_load.pal_tag = 0xFFFF;
  80. s8 pal_slot = npc_dynamic_load_palette(type_to_load->pal_num);
  81. u8 obj_id = template_instanciate_forward_search(&template_to_load, 0, 0, 0);
  82. if (obj_id == 64)
  83. return;
  84. struct Object *npc_obj = &objects[obj_id];
  85. npc_fix_position(x + npc_to_load->to.x, y + npc_to_load->to.y, &npc_obj->pos1.x, &npc_obj->pos1.y);
  86. npc_obj->shift.x = -(type_to_load->pos_neg_center.x / 2);
  87. npc_obj->shift.y = -(type_to_load->pos_neg_center.y / 2);
  88. npc_obj->pos1.x += 8;
  89. npc_obj->pos1.y += (s8)npc_obj->shift.y + 16;
  90. npc_obj->gfx_table = type_to_load->gfx_table;
  91. if (npc_to_load->running_behavior == 11) {
  92. walkrun_init_something(id, obj_id);
  93. npc_to_load->oamid2 = arrow_init_something();
  94. }
  95. if (f14 != 0) {
  96. (void) obj_set_f18_to_r0_f42_to_40(npc_obj, f14);
  97. }
  98. npc_obj->final_oam.palette_num = pal_slot;
  99. npc_obj->bitfield2 |= 2;
  100. npc_obj->priv[0] = id;
  101. npc_to_load->oam_id = obj_id;
  102. if (!(npc_to_load->field1 & 0x10) && (npc_to_load->running_behavior != 11)) {
  103. obj_anim_image_start(npc_obj, npc_direction_to_obj_anim_image_number(npc_to_load->direction & 0xF));
  104. }
  105. npc_805EFF4(npc_to_load);
  106. npc_y_height_related(npc_to_load->height >> 4, npc_obj, 1);
  107. }
  108. u8 npc_spawn_with_provided_template(struct RomNpc *npc, struct Template *template, u8 map, u8 bank, s16 x, s16 y) {
  109. u8 state = rom_npc_to_npc_state(npc, map, bank);
  110. if (state >= 16)
  111. return 16;
  112. struct NpcState *created_state = &npc_states[state];
  113. struct NpcType *type = npc_get_type(created_state->type_id | (npc->field3 << 8));
  114. s8 pal_slot = npc_dynamic_load_palette(type->pal_num);
  115. if (created_state->running_behavior == 76)
  116. created_state->field1 |= 0x20;
  117. template->pal_tag = 0xFFFF;
  118. u8 obj_id = template_instanciate_forward_search(template, 0, 0, 0);
  119. if (obj_id == 64) {
  120. created_state->bitfield &= 0xFE;
  121. return 16;
  122. }
  123. struct Object *npc_object = &objects[obj_id];
  124. npc_fix_position(x + created_state->to.x, y + created_state->to.y, &npc_object->pos1.x, &npc_object->pos1.y);
  125. npc_object->shift.x = -(type->pos_neg_center.x / 2);
  126. npc_object->shift.y = -(type->pos_neg_center.y / 2);
  127. npc_object->pos1.x += 8;
  128. npc_object->pos1.y += (s8)npc_object->shift.y + 16;
  129. /* Set our allocated index */
  130. npc_object->final_oam.palette_num = pal_slot;
  131. npc_object->bitfield2 |= 2;
  132. npc_object->priv[0] = state;
  133. created_state->oam_id = obj_id;
  134. u8 unknown = (created_state->field1 & 0xEF) | (16 * (type->pal_slot_unk << 25 >> 31));
  135. created_state->field1 = unknown;
  136. if (!(unknown & 0x10)) {
  137. obj_anim_image_start(npc_object, npc_direction_to_obj_anim_image_number(created_state->direction & 0xF));
  138. }
  139. npc_y_height_related(created_state->height >> 4, npc_object, 1);
  140. npc_obj_offscreen_culling_and_flag_update(created_state, npc_object);
  141. return state;
  142. }