-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbnode.cpp
306 lines (239 loc) · 7.52 KB
/
bnode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF OUTRAGE
ENTERTAINMENT, INC. ("OUTRAGE"). OUTRAGE, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1996-2000 OUTRAGE ENTERTAINMENT, INC. ALL RIGHTS RESERVED.
*/
#ifdef MACINTOSH
#include <string.h>
#endif
#include "memory.h"
#include "bnode.h"
#include "room.h"
#include "mem.h"
#include "vecmat.h"
#include "terrain.h"
#include "room.h"
#include "findintersection.h"
#include "BOA.h"
#include "psrand.h"
bn_list BNode_terrain_list[8];
bool BNode_allocated = false;
bool BNode_verified = false;
//-- Priority Queue
class pq_item {
public:
pq_item(int node_index, int parent_node, float n_cost) {
node = node_index;
p_node = parent_node;
cost = n_cost;
next = NULL;
}
int node;
int p_node;
float cost;
pq_item *next;
};
class bpq {
pq_item *q_head;
public:
bpq() { q_head = NULL; }
void push(pq_item *new_node) {
new_node->next = q_head;
q_head = new_node;
}
pq_item *pop() {
pq_item *cur_item = q_head;
pq_item *prev_item = NULL;
pq_item *best_item = q_head;
pq_item *best_prev_item = NULL;
if (q_head == NULL)
return NULL;
while (cur_item != NULL) {
if (cur_item->cost < best_item->cost) {
best_prev_item = prev_item;
best_item = cur_item;
}
prev_item = cur_item;
cur_item = cur_item->next;
}
// Actually remove this item from the list
if (best_item == q_head) {
q_head = best_item->next;
} else {
best_prev_item->next = best_item->next;
}
return best_item;
}
};
int BNode_Path[MAX_BNODES_PER_ROOM];
int BNode_PathNumNodes;
void BNode_UpdatePathInfo(pq_item **node_list, int start, int end) {
int cur_node = end;
int i;
BNode_PathNumNodes = 0;
// mprintf((0, "start crash loop\n"));
while (cur_node != -1) {
BNode_Path[BNode_PathNumNodes++] = cur_node;
cur_node = node_list[cur_node]->p_node;
}
// mprintf((0, "end crash loop\n"));
// Reverse the list (so it is what we want)
for (i = 0; i<BNode_PathNumNodes> > 1; i++) {
int temp;
temp = BNode_Path[i];
BNode_Path[i] = BNode_Path[BNode_PathNumNodes - i - 1];
BNode_Path[BNode_PathNumNodes - i - 1] = temp;
}
/* for(i = 0; i < BNode_PathNumNodes; i++)
{
mprintf((0, "Node %d\n", BNode_Path[i]));
}
*/
}
// Ok to use Highest_room_index offset stuff
bool BNode_FindPath(int start_room, int i, int j, float rad) {
bpq PQPath;
int counter;
pq_item *start_node = new pq_item(i, -1, 0.0f);
pq_item *cur_node;
bool f_found = false;
// mprintf((0, "++++++++++\nFind path from %d to %d in room %d\n", i, j, start_room));
start_room = BOA_INDEX(start_room);
pq_item **node_list;
bn_list *bnlist = BNode_GetBNListPtr(start_room);
ASSERT(bnlist);
ASSERT(i >= 0 && i < bnlist->num_nodes && j >= 0 && j < bnlist->num_nodes);
node_list = (pq_item **)mem_malloc(bnlist->num_nodes * sizeof(pq_item *));
memset(node_list, 0, bnlist->num_nodes * sizeof(pq_item *));
PQPath.push(start_node);
while (cur_node = PQPath.pop()) {
node_list[cur_node->node] = cur_node;
if (cur_node->node == j) {
BNode_UpdatePathInfo(node_list, i, j);
f_found = true;
goto done;
}
int num_edges;
num_edges = bnlist->nodes[cur_node->node].num_edges;
for (counter = 0; counter < num_edges; counter++) {
int next_node;
pq_item *list_item;
float new_cost;
// if(!BOA_PassablePortal(cur_node->roomnum, counter, false, false))
// continue;
if (next_node = bnlist->nodes[cur_node->node].edges[counter].end_room != start_room)
continue;
next_node = bnlist->nodes[cur_node->node].edges[counter].end_index;
ASSERT(bnlist->nodes[cur_node->node].edges[counter].cost > 0);
new_cost = cur_node->cost + bnlist->nodes[cur_node->node].edges[counter].cost;
list_item = node_list[next_node];
if (list_item != NULL && list_item->cost < new_cost)
continue;
if (list_item == NULL) {
list_item = new pq_item(next_node, cur_node->node, new_cost);
node_list[next_node] = list_item;
PQPath.push(list_item);
} else {
list_item->cost = new_cost;
list_item->p_node = cur_node->node;
}
}
}
done:
for (counter = 0; counter < bnlist->num_nodes; counter++) {
if (node_list[counter])
delete node_list[counter];
}
mem_free(node_list); // DAJ LEAKFIX
return f_found;
}
char BNode_vis[MAX_BNODES_PER_ROOM];
#define VIS_NO_CHECK 2
#define VIS_OK 1
#define VIS_NO 2
bn_list *BNode_GetBNListPtr(int roomnum, bool f_in_load_level) {
if (roomnum == -1) {
return NULL;
} else if (roomnum >= 0 && roomnum <= Highest_room_index) {
// if(!f_in_load_level)
// {
// ASSERT(!(Rooms[roomnum].flags & RF_EXTERNAL));
// }
if (!Rooms[roomnum].used) {
return NULL;
}
room *rp = &Rooms[roomnum];
return &rp->bn_info;
} else if (ROOMNUM_OUTSIDE(roomnum)) {
return &BNode_terrain_list[TERRAIN_REGION(roomnum)];
} else if (roomnum <= Highest_room_index + 8) {
return &BNode_terrain_list[roomnum - Highest_room_index - 1];
}
return NULL;
}
void BNode_ClearBNodeInfo(void) {
int i, j;
for (i = 0; i < 8; i++) {
if (BNode_allocated) {
for (j = 0; j < BNode_terrain_list[i].num_nodes; j++) {
if (BNode_terrain_list[i].nodes[j].num_edges) {
mem_free(BNode_terrain_list[i].nodes[j].edges);
}
}
mem_free(BNode_terrain_list[i].nodes);
}
BNode_terrain_list[i].num_nodes = 0;
BNode_terrain_list[i].nodes = NULL;
}
BNode_allocated = false;
BNode_verified = false;
}
void BNode_FreeRoom(room *rp) {
int i;
for (i = 0; i < rp->bn_info.num_nodes; i++) {
if (rp->bn_info.nodes[i].num_edges) {
mem_free(rp->bn_info.nodes[i].edges);
}
}
if (rp->bn_info.num_nodes) {
mem_free(rp->bn_info.nodes);
rp->bn_info.nodes = NULL;
rp->bn_info.num_nodes = 0;
}
}
void BNode_RemapTerrainRooms(int old_hri, int new_hri) {
int delta = new_hri - old_hri;
int i;
if (!BNode_allocated)
return;
if (delta == 0)
return;
if (new_hri < 0)
return;
ASSERT(delta <= 1);
for (i = 0; i <= new_hri + BOA_num_terrain_regions; i++) {
if ((i <= new_hri && Rooms[i].used) || (i > new_hri)) {
int j;
int k;
// Skip external rooms
if ((i <= new_hri) && (Rooms[i].flags & RF_EXTERNAL))
continue;
bn_list *bnlist = BNode_GetBNListPtr(i);
ASSERT(bnlist);
for (j = 0; j < bnlist->num_nodes; j++) {
for (k = 0; k < bnlist->nodes[j].num_edges; k++) {
if (bnlist->nodes[j].edges[k].end_room >= new_hri) {
bnlist->nodes[j].edges[k].end_room += delta;
}
}
}
}
}
}