| 1 |
// |
| 2 |
// Copyright(C) 2022 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Thing ID |
| 16 |
// |
| 17 |
|
| 18 |
#include "p_tick.h" |
| 19 |
#include "z_zone.h" |
| 20 |
|
| 21 |
#include "thing_id.h" |
| 22 |
|
| 23 |
#define THING_ID_HASH_MAX 128 |
| 24 |
|
| 25 |
static thing_id_list_t* thing_id_list_hash[THING_ID_HASH_MAX]; |
| 26 |
|
| 27 |
static thing_id_list_t* dsda_NewThingIDList(short thing_id) { |
| 28 |
thing_id_list_t* result; |
| 29 |
|
| 30 |
result = Z_CallocLevel(1, sizeof(*result)); |
| 31 |
result->thing_id = thing_id; |
| 32 |
return result; |
| 33 |
} |
| 34 |
|
| 35 |
static thing_id_list_t* dsda_ThingIDList(short thing_id) { |
| 36 |
thing_id_list_t* result; |
| 37 |
short index; |
| 38 |
|
| 39 |
index = thing_id % THING_ID_HASH_MAX; |
| 40 |
|
| 41 |
result = thing_id_list_hash[index]; |
| 42 |
while (result && result->next && result->thing_id != thing_id) |
| 43 |
result = result->next; |
| 44 |
|
| 45 |
if (result) { |
| 46 |
if (result->thing_id != thing_id) { |
| 47 |
result->next = dsda_NewThingIDList(thing_id); |
| 48 |
result = result->next; |
| 49 |
} |
| 50 |
|
| 51 |
return result; |
| 52 |
} |
| 53 |
|
| 54 |
thing_id_list_hash[index] = dsda_NewThingIDList(thing_id); |
| 55 |
return thing_id_list_hash[index]; |
| 56 |
} |
| 57 |
|
| 58 |
static thing_id_list_entry_t* dsda_NewThingIDListEntry(mobj_t* mo) { |
| 59 |
thing_id_list_entry_t* result; |
| 60 |
|
| 61 |
result = Z_CallocLevel(1, sizeof(*result)); |
| 62 |
P_SetTarget(&result->mo, mo); |
| 63 |
return result; |
| 64 |
} |
| 65 |
|
| 66 |
void dsda_AddMobjThingID(mobj_t* mo, short thing_id) { |
| 67 |
thing_id_list_t* list; |
| 68 |
|
| 69 |
list = dsda_ThingIDList(thing_id); |
| 70 |
|
| 71 |
// Note that there is no uniqueness check - this is consistent with hexen |
| 72 |
if (!list->last) { |
| 73 |
list->first = dsda_NewThingIDListEntry(mo); |
| 74 |
list->last = list->first; |
| 75 |
} |
| 76 |
else { |
| 77 |
list->last->next = dsda_NewThingIDListEntry(mo); |
| 78 |
list->last = list->last->next; |
| 79 |
} |
| 80 |
|
| 81 |
mo->tid = thing_id; |
| 82 |
} |
| 83 |
|
| 84 |
void dsda_RemoveMobjThingID(mobj_t* mo) { |
| 85 |
thing_id_list_t* list; |
| 86 |
thing_id_list_entry_t* entry; |
| 87 |
thing_id_list_entry_t* next; |
| 88 |
|
| 89 |
list = dsda_ThingIDList(mo->tid); |
| 90 |
|
| 91 |
if (list->first && list->first->mo == mo) { |
| 92 |
next = list->first->next; |
| 93 |
P_SetTarget(&list->first->mo, NULL); |
| 94 |
// Z_Free(list->first); // TODO: might be freed during iteration, need solution |
| 95 |
list->first = next; |
| 96 |
if (!list->first) |
| 97 |
list->last = NULL; |
| 98 |
} |
| 99 |
else { |
| 100 |
for (entry = list->first; entry; entry = entry->next) |
| 101 |
if (entry->next && entry->next->mo == mo) { |
| 102 |
next = entry->next->next; |
| 103 |
P_SetTarget(&entry->next->mo, NULL); |
| 104 |
// Z_Free(entry->next); // TODO: might be freed during iteration, need solution |
| 105 |
entry->next = next; |
| 106 |
if (!entry->next) |
| 107 |
list->last = entry; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
mo->tid = 0; |
| 112 |
} |
| 113 |
|
| 114 |
// The allocated memory is automatically removed (zone memory) |
| 115 |
void dsda_WipeMobjThingIDList(void) { |
| 116 |
int i; |
| 117 |
|
| 118 |
for (i = 0; i < THING_ID_HASH_MAX; ++i) |
| 119 |
thing_id_list_hash[i] = NULL; |
| 120 |
} |
| 121 |
|
| 122 |
void dsda_BuildMobjThingIDList(void) { |
| 123 |
mobj_t *mo; |
| 124 |
thinker_t *th; |
| 125 |
|
| 126 |
dsda_WipeMobjThingIDList(); |
| 127 |
|
| 128 |
for (th = thinkercap.next; th != &thinkercap; th = th->next) { |
| 129 |
if (th->function != P_MobjThinker) |
| 130 |
continue; |
| 131 |
|
| 132 |
mo = (mobj_t *) th; |
| 133 |
|
| 134 |
if (mo->tid != 0) |
| 135 |
dsda_AddMobjThingID(mo, mo->tid); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void dsda_ResetThingIDSearch(thing_id_search_t* search) { |
| 140 |
search->done = false; |
| 141 |
search->start = NULL; |
| 142 |
} |
| 143 |
|
| 144 |
static void dsda_FinishThingIDSearch(thing_id_search_t* search) { |
| 145 |
search->done = true; |
| 146 |
search->start = NULL; |
| 147 |
} |
| 148 |
|
| 149 |
mobj_t* dsda_FindMobjFromThingID(short thing_id, thing_id_search_t* search) { |
| 150 |
thing_id_list_entry_t* p; |
| 151 |
|
| 152 |
p = search->start; |
| 153 |
|
| 154 |
if (p) |
| 155 |
p = p->next; |
| 156 |
else { |
| 157 |
thing_id_list_t* list = dsda_ThingIDList(thing_id); |
| 158 |
p = list->first; |
| 159 |
} |
| 160 |
|
| 161 |
if (p) { |
| 162 |
search->start = p; |
| 163 |
return p->mo; |
| 164 |
} |
| 165 |
|
| 166 |
dsda_FinishThingIDSearch(search); |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
mobj_t* dsda_FindMobjFromThingIDOrMobj(short thing_id, mobj_t* mo, thing_id_search_t* search) { |
| 171 |
if (search->done) |
| 172 |
return NULL; |
| 173 |
|
| 174 |
if (!thing_id) { |
| 175 |
dsda_FinishThingIDSearch(search); |
| 176 |
return mo; |
| 177 |
} |
| 178 |
|
| 179 |
return dsda_FindMobjFromThingID(thing_id, search); |
| 180 |
} |