client/game/kkr.dll/weapon_health.c (2458B)
1 #include "recomp_types.h" 2 #include "kkr.dll/weapon_health.h" 3 4 /* Keep animation results intact when an absolute HP update includes their damage. */ 5 static struct weapon_health { 6 uint32_t weapon; 7 uint64_t slots; 8 struct weapon_health *next; 9 } *pending; 10 11 void weapon_health_forget(uint32_t weapon) 12 { 13 for (struct weapon_health **entry = &pending; *entry; entry = &(*entry)->next) { 14 if ((*entry)->weapon != weapon) continue; 15 struct weapon_health *removed = *entry; 16 *entry = removed->next; 17 free(removed); 18 return; 19 } 20 } 21 22 int weapon_health_take(uint32_t weapon, uint32_t slot) 23 { 24 for (struct weapon_health *entry = pending; entry; entry = entry->next) { 25 if (entry->weapon != weapon) continue; 26 uint64_t bit = UINT64_C(1) << slot; 27 int included = (entry->slots & bit) != 0; 28 entry->slots &= ~bit; 29 if (!entry->slots) weapon_health_forget(weapon); 30 return included; 31 } 32 return 0; 33 } 34 35 static void snapshot_weapons(uint32_t node, uint32_t nil, uint32_t type, uint32_t id) 36 { 37 if (node == nil) return; 38 snapshot_weapons(MEM32(node), nil, type, id); 39 40 /* KKR sub_3607A320's weapons use sub_360AB4C0's fifty-entry result ring. */ 41 uint32_t actor = MEM32(node + 20); 42 uint32_t holder = MEM32(node + 12) == 50000 ? MEM32(actor + 0x16c) : 0; 43 if (holder && MEM32(holder + 0x58) == type && MEM32(holder + 0x5c) == id) { 44 for (unsigned slot = 0; slot < 16; slot++) { 45 uint32_t weapon = MEM32(holder + 0x0c + slot * 4); 46 if (!weapon) continue; 47 uint64_t slots = 0; 48 for (uint32_t index = MEM32(weapon + 0x18); index != MEM32(weapon + 0x14); index = (index + 1) % 50) 49 slots |= UINT64_C(1) << index; 50 if (!slots) continue; 51 struct weapon_health *entry = pending; 52 while (entry && entry->weapon != weapon) entry = entry->next; 53 if (!entry) { 54 entry = malloc(sizeof(*entry)); 55 if (!entry) abort(); 56 *entry = (struct weapon_health){weapon, 0, pending}; 57 pending = entry; 58 } 59 entry->slots |= slots; 60 } 61 } 62 snapshot_weapons(MEM32(node + 8), nil, type, id); 63 } 64 65 void weapon_health_snapshot(uint32_t type, uint32_t id) 66 { 67 uint32_t actors = MEM32(0x3806c110); 68 if (actors) snapshot_weapons(MEM32(MEM32(actors + 4) + 4), MEM32(actors + 8), type, id); 69 }