| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/bnode.cpp $ |
| 21 | * $Revision: 23 $ |
| 22 | * $Date: 10/21/99 2:36p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * BOA Helper Node functions |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/bnode.cpp $ |
| 28 | * |
| 29 | * 23 10/21/99 2:36p Matt |
| 30 | * Mac merge |
| 31 | * |
| 32 | * 22 10/08/99 4:27p Chris |
| 33 | * Removed a needless ASSERT |
| 34 | * |
| 35 | * 21 5/20/99 8:09p Chris |
| 36 | * Patched up the bnode crashes |
| 37 | * |
| 38 | * 20 5/19/99 2:40p Chris |
| 39 | * |
| 40 | * 19 5/13/99 4:23p Matt |
| 41 | * Fixed, hopefully, a problem remapping nodes. |
| 42 | * |
| 43 | * 18 5/10/99 10:07p Ardussi |
| 44 | * changes to compile on Mac |
| 45 | * |
| 46 | * 17 5/09/99 11:43p Chris |
| 47 | * Fixed a bug with no generating a random node (worst case for the |
| 48 | * terrain) |
| 49 | * |
| 50 | * 16 5/09/99 5:24p Chris |
| 51 | * Maxed the bnode distance to the AI_SEE distance |
| 52 | * |
| 53 | * 15 5/06/99 5:18p Chris |
| 54 | * Updated the BNode system - vastly improved error checking and verify |
| 55 | * |
| 56 | * 14 5/02/99 6:16a Chris |
| 57 | * Massive bnode error checking improvements |
| 58 | * |
| 59 | * 13 5/01/99 4:06p Jeff |
| 60 | * fixed #include for linux |
| 61 | * |
| 62 | * 12 4/28/99 8:27a Chris |
| 63 | * |
| 64 | * 11 4/28/99 5:33a Chris |
| 65 | * Further improved the BNode system. (It actually works inside and |
| 66 | * outside now) |
| 67 | * |
| 68 | * 10 4/27/99 11:37p Chris |
| 69 | * Improving the BNode system |
| 70 | * |
| 71 | * 9 4/27/99 3:01a Chris |
| 72 | * Improving Bnode stuff |
| 73 | * |
| 74 | * 8 4/26/99 11:11a Chris |
| 75 | * Updated Bnode system |
| 76 | * |
| 77 | * 7 4/25/99 8:57p Chris |
| 78 | * Improving the Bnode system |
| 79 | * |
| 80 | * 6 4/18/99 4:35p Matt |
| 81 | * Changed BNode_FreeRoom() to take a room pointer instead of a room |
| 82 | * number, since it was being called for rooms in the scrap buffer, which |
| 83 | * weren't in the room list. Also added code to InitRoom() to init the |
| 84 | * bnode data. |
| 85 | * |
| 86 | * 5 4/18/99 6:50a Chris |
| 87 | * |
| 88 | * 4 4/18/99 5:39a Chris |
| 89 | * Vastly improved the path node system |
| 90 | * |
| 91 | * 3 4/15/99 5:48p Chris |
| 92 | * Fixed a bug with rendering the BOAPath nodes |
| 93 | * |
| 94 | * 2 4/14/99 3:12p Chris |
| 95 | * Beginning to add BoaNode stuff |
| 96 | * |
| 97 | * $NoKeywords: $ |
| 98 | */ |
| 99 | |
| 100 | |
| 101 | #include "memory.h" |
| 102 | #include "bnode.h" |
| 103 | #include "room.h" |
| 104 | #include "mem.h" |
| 105 | #include "vecmat.h" |
| 106 | #include "terrain.h" |
| 107 | #include "room.h" |
| 108 | #include "findintersection.h" |
| 109 | #include "BOA.h" |
| 110 | #include "psrand.h" |
| 111 | |
| 112 | bn_list BNode_terrain_list[8]; |
| 113 | bool BNode_allocated = false; |
| 114 | bool BNode_verified = false; |
| 115 | |
| 116 | //-- Priority Queue |
| 117 | class pq_item { |
| 118 | public: |
| 119 | pq_item(int node_index, int parent_node, float n_cost) { |
| 120 | node = node_index; |
| 121 | p_node = parent_node; |
| 122 | cost = n_cost; |
| 123 | next = NULL; |
| 124 | } |
| 125 | |
| 126 | int node; |
| 127 | int p_node; |
| 128 | float cost; |
| 129 | |
| 130 | pq_item *next; |
| 131 | }; |
| 132 | |
| 133 | class bpq { |
| 134 | pq_item *q_head; |
| 135 | |
| 136 | public: |
| 137 | bpq() { q_head = NULL; } |
| 138 | |
| 139 | void push(pq_item *new_node) { |
| 140 | new_node->next = q_head; |
| 141 | q_head = new_node; |
| 142 | } |
| 143 | |
| 144 | pq_item *pop() { |
| 145 | pq_item *cur_item = q_head; |
| 146 | pq_item *prev_item = NULL; |
| 147 | pq_item *best_item = q_head; |
| 148 | pq_item *best_prev_item = NULL; |
| 149 | |
| 150 | if (q_head == NULL) |
| 151 | return NULL; |
| 152 | |
| 153 | while (cur_item != NULL) { |
| 154 | if (cur_item->cost < best_item->cost) { |
| 155 | |
| 156 | best_prev_item = prev_item; |
| 157 | best_item = cur_item; |
| 158 | } |
| 159 | |
| 160 | prev_item = cur_item; |
| 161 | cur_item = cur_item->next; |
| 162 | } |
| 163 | |
| 164 | // Actually remove this item from the list |
| 165 | if (best_item == q_head) { |
| 166 | q_head = best_item->next; |
| 167 | } else { |
| 168 | best_prev_item->next = best_item->next; |
| 169 | } |
| 170 | |
| 171 | return best_item; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | static float BNode_QuickDist(vector *pos1, vector *pos2) { |
| 176 | return fabs(pos1->x - pos2->x) + fabs(pos1->y - pos2->y) + fabs(pos1->z - pos2->z); |
| 177 | } |
| 178 | |
| 179 | int BNode_Path[MAX_BNODES_PER_ROOM]; |
| 180 | int BNode_PathNumNodes; |
| 181 | |
| 182 | static void BNode_UpdatePathInfo(pq_item **node_list, int start, int end) { |
| 183 | int cur_node = end; |
| 184 | int i; |
| 185 | |
| 186 | BNode_PathNumNodes = 0; |
| 187 | |
| 188 | // mprintf(0, "start crash loop\n"); |
| 189 | while (cur_node != -1) { |
| 190 | BNode_Path[BNode_PathNumNodes++] = cur_node; |
| 191 | cur_node = node_list[cur_node]->p_node; |
| 192 | } |
| 193 | // mprintf(0, "end crash loop\n"); |
| 194 | |
| 195 | // Reverse the list (so it is what we want) |
| 196 | for (i = 0; i < (BNode_PathNumNodes >> 1); i++) { |
| 197 | int temp; |
| 198 | |
| 199 | temp = BNode_Path[i]; |
| 200 | BNode_Path[i] = BNode_Path[BNode_PathNumNodes - i - 1]; |
| 201 | BNode_Path[BNode_PathNumNodes - i - 1] = temp; |
| 202 | } |
| 203 | |
| 204 | /* for(i = 0; i < BNode_PathNumNodes; i++) |
| 205 | { |
| 206 | mprintf(0, "Node %d\n", BNode_Path[i]); |
| 207 | } |
| 208 | */ |
| 209 | } |
| 210 | |
| 211 | // Ok to use Highest_room_index offset stuff |
| 212 | bool BNode_FindPath(int start_room, int i, int j, float rad) { |
| 213 | bpq PQPath; |
| 214 | int counter; |
| 215 | pq_item *start_node = new pq_item(i, -1, 0.0f); |
| 216 | pq_item *cur_node; |
| 217 | bool f_found = false; |
| 218 | |
| 219 | // mprintf(0, "++++++++++\nFind path from %d to %d in room %d\n", i, j, start_room); |
| 220 | |
| 221 | start_room = BOA_INDEX(start_room); |
| 222 | |
| 223 | pq_item **node_list; |
| 224 | bn_list *bnlist = BNode_GetBNListPtr(start_room); |
| 225 | |
| 226 | ASSERT(bnlist); |
| 227 | ASSERT(i >= 0 && i < bnlist->num_nodes && j >= 0 && j < bnlist->num_nodes); |
| 228 | |
| 229 | node_list = (pq_item **)mem_malloc(bnlist->num_nodes * sizeof(pq_item *)); |
| 230 | memset(node_list, 0, bnlist->num_nodes * sizeof(pq_item *)); |
| 231 | |
| 232 | PQPath.push(start_node); |
| 233 | |
| 234 | while ((cur_node = PQPath.pop())) { |
| 235 | node_list[cur_node->node] = cur_node; |
| 236 | |
| 237 | if (cur_node->node == j) { |
| 238 | BNode_UpdatePathInfo(node_list, i, j); |
| 239 | f_found = true; |
| 240 | goto done; |
| 241 | } |
| 242 | |
| 243 | int num_edges; |
| 244 | |
| 245 | num_edges = bnlist->nodes[cur_node->node].num_edges; |
| 246 | |
| 247 | for (counter = 0; counter < num_edges; counter++) { |
| 248 | int next_node; |
| 249 | pq_item *list_item; |
| 250 | float new_cost; |
| 251 | |
| 252 | // if(!BOA_PassablePortal(cur_node->roomnum, counter, false, false)) |
| 253 | // continue; |
| 254 | |
| 255 | if ((next_node = bnlist->nodes[cur_node->node].edges[counter].end_room != start_room)) |
| 256 | continue; |
| 257 | |
| 258 | next_node = bnlist->nodes[cur_node->node].edges[counter].end_index; |
| 259 | |
| 260 | ASSERT(bnlist->nodes[cur_node->node].edges[counter].cost > 0); |
| 261 | new_cost = cur_node->cost + bnlist->nodes[cur_node->node].edges[counter].cost; |
| 262 | |
| 263 | list_item = node_list[next_node]; |
| 264 | |
| 265 | if (list_item != NULL && list_item->cost < new_cost) |
| 266 | continue; |
| 267 | |
| 268 | if (list_item == NULL) { |
| 269 | list_item = new pq_item(next_node, cur_node->node, new_cost); |
| 270 | |
| 271 | node_list[next_node] = list_item; |
| 272 | PQPath.push(list_item); |
| 273 | } else { |
| 274 | list_item->cost = new_cost; |
| 275 | list_item->p_node = cur_node->node; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | done: |
| 281 | for (counter = 0; counter < bnlist->num_nodes; counter++) { |
| 282 | if (node_list[counter]) |
| 283 | delete node_list[counter]; |
| 284 | } |
| 285 | |
| 286 | mem_free(node_list); // DAJ LEAKFIX |
| 287 | return f_found; |
| 288 | } |
| 289 | |
| 290 | // TODO: MTS: Unused? |
| 291 | int BNode_GenerateBestPathThroughRoom(int sroom, int spnt, int croom, int eroom, int eportal, int max_nodes, |
| 292 | vector *pos_list) { |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | static char BNode_vis[MAX_BNODES_PER_ROOM]; |
| 297 | #define VIS_NO_CHECK 2 |
| 298 | #define VIS_OK 1 |
| 299 | #define VIS_NO 2 |
| 300 | |
| 301 | int BNode_FindDirLocalVisibleBNode(int roomnum, vector *pos, vector *fvec, float rad) { |
| 302 | int i; |
| 303 | float best_dot = -1.01f; |
| 304 | float closest_dist = 800.0f; |
| 305 | int closest_node = -1; |
| 306 | bool f_retry = false; |
| 307 | |
| 308 | float min_bn_rad = rad / 4.0f; |
| 309 | if (min_bn_rad > MAX_BNODE_SIZE) { |
| 310 | min_bn_rad = MAX_BNODE_SIZE; |
| 311 | } |
| 312 | |
| 313 | bn_list *bnlist = BNode_GetBNListPtr(roomnum); |
| 314 | |
| 315 | retry: |
| 316 | |
| 317 | for (i = 0; i < bnlist->num_nodes; i++) { |
| 318 | vector to = bnlist->nodes[i].pos - *pos; |
| 319 | float dist = vm_NormalizeVector(&to); |
| 320 | |
| 321 | if (dist < closest_dist) { |
| 322 | float dot = *fvec * to; |
| 323 | |
| 324 | if (dot > 0.0f || f_retry) { |
| 325 | float node_size = 0.0f; |
| 326 | |
| 327 | /* if(!f_retry) |
| 328 | { |
| 329 | for(j = 0; j < bnlist->nodes[i].num_edges; j++) |
| 330 | { |
| 331 | if(bnlist->nodes[i].edges[j].max_rad > node_size) |
| 332 | { |
| 333 | node_size = bnlist->nodes[i].edges[j].max_rad; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | */ |
| 338 | if (!f_retry || (f_retry && BNode_vis[i] == VIS_NO_CHECK)) { |
| 339 | fvi_query fq; |
| 340 | fvi_info hit_info; |
| 341 | |
| 342 | fq.p0 = pos; |
| 343 | fq.startroom = (roomnum > Highest_room_index && roomnum <= Highest_room_index + 8) |
| 344 | ? GetTerrainRoomFromPos(pos) |
| 345 | : roomnum; |
| 346 | fq.p1 = &bnlist->nodes[i].pos; |
| 347 | fq.rad = min_bn_rad; |
| 348 | fq.thisobjnum = -1; |
| 349 | fq.ignore_obj_list = NULL; |
| 350 | fq.flags = FQ_CHECK_OBJS | FQ_NO_RELINK | FQ_IGNORE_NON_LIGHTMAP_OBJECTS; |
| 351 | |
| 352 | if (fvi_FindIntersection(&fq, &hit_info) == HIT_NONE) { |
| 353 | BNode_vis[i] = VIS_OK; |
| 354 | best_dot = dot; |
| 355 | closest_node = i; |
| 356 | } else { |
| 357 | if (!f_retry) { |
| 358 | BNode_vis[i] = VIS_NO; |
| 359 | } else { |
| 360 | if (BNode_vis[i] == VIS_OK) { |
| 361 | closest_node = i; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } else { |
| 366 | if (f_retry && BNode_vis[i] == VIS_OK) { |
| 367 | closest_node = i; |
| 368 | } |
| 369 | } |
| 370 | } else { |
| 371 | BNode_vis[i] = VIS_NO_CHECK; |
| 372 | } |
| 373 | } else { |
| 374 | BNode_vis[i] = VIS_NO_CHECK; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (closest_node == -1 && !f_retry) { |
| 379 | f_retry = true; |
| 380 | goto retry; |
| 381 | } |
| 382 | |
| 383 | ASSERT(bnlist->num_nodes > 0); |
| 384 | if (closest_node == -1 && bnlist->num_nodes > 0) { |
| 385 | closest_node = ps_rand() % bnlist->num_nodes; |
| 386 | } |
| 387 | |
| 388 | ASSERT(closest_node != -1); |
| 389 | |
| 390 | return closest_node; |
| 391 | } |
| 392 | |
| 393 | int BNode_FindClosestLocalVisibleBNode(int roomnum, vector *pos, float rad) { |
| 394 | int i, j; |
| 395 | float closest_dist = 800.0f; |
| 396 | int closest_node = -1; |
| 397 | bool f_retry = false; |
| 398 | |
| 399 | float min_bn_rad = rad / 4.0f; |
| 400 | if (min_bn_rad > MAX_BNODE_SIZE) { |
| 401 | min_bn_rad = MAX_BNODE_SIZE; |
| 402 | } |
| 403 | |
| 404 | bn_list *bnlist = BNode_GetBNListPtr(roomnum); |
| 405 | |
| 406 | retry: |
| 407 | |
| 408 | for (i = 0; i < bnlist->num_nodes; i++) { |
| 409 | float dist = BNode_QuickDist(&bnlist->nodes[i].pos, pos); |
| 410 | |
| 411 | if (dist < closest_dist) { |
| 412 | float node_size = 0.0f; |
| 413 | |
| 414 | if (!f_retry) { |
| 415 | for (j = 0; j < bnlist->nodes[i].num_edges; j++) { |
| 416 | if (bnlist->nodes[i].edges[j].max_rad > node_size) { |
| 417 | node_size = bnlist->nodes[i].edges[j].max_rad; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (f_retry || node_size >= min_bn_rad) { |
| 423 | fvi_query fq; |
| 424 | fvi_info hit_info; |
| 425 | |
| 426 | if (!f_retry) { |
| 427 | fq.p0 = pos; |
| 428 | fq.startroom = (roomnum > Highest_room_index && roomnum <= Highest_room_index + 8) |
| 429 | ? GetTerrainRoomFromPos(pos) |
| 430 | : roomnum; |
| 431 | fq.p1 = &bnlist->nodes[i].pos; |
| 432 | fq.rad = min_bn_rad; |
| 433 | fq.thisobjnum = -1; |
| 434 | fq.ignore_obj_list = NULL; |
| 435 | fq.flags = 0; |
| 436 | } |
| 437 | |
| 438 | if (f_retry || fvi_FindIntersection(&fq, &hit_info) == HIT_NONE) { |
| 439 | closest_dist = dist; |
| 440 | closest_node = i; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (closest_node == -1 && !f_retry) { |
| 447 | f_retry = true; |
| 448 | goto retry; |
| 449 | } |
| 450 | |
| 451 | ASSERT(bnlist->num_nodes > 0); |
| 452 | if (closest_node == -1 && bnlist->num_nodes > 0) { |
| 453 | closest_node = ps_rand() % bnlist->num_nodes; |
| 454 | } |
| 455 | |
| 456 | ASSERT(closest_node != -1); |
| 457 | |
| 458 | return closest_node; |
| 459 | } |
| 460 | |
| 461 | bn_list *BNode_GetBNListPtr(int roomnum, bool f_in_load_level) { |
| 462 | if (roomnum == -1) { |
| 463 | return NULL; |
| 464 | } else if (roomnum >= 0 && roomnum <= Highest_room_index) { |
| 465 | // if(!f_in_load_level) |
| 466 | // { |
| 467 | // ASSERT(!(Rooms[roomnum].flags & RF_EXTERNAL)); |
| 468 | // } |
| 469 | |
| 470 | if (!Rooms[roomnum].used) { |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | room *rp = &Rooms[roomnum]; |
| 475 | return &rp->bn_info; |
| 476 | } else if (ROOMNUM_OUTSIDE(roomnum)) { |
| 477 | return &BNode_terrain_list[TERRAIN_REGION(roomnum)]; |
| 478 | } else if (roomnum <= Highest_room_index + 8) { |
| 479 | return &BNode_terrain_list[roomnum - Highest_room_index - 1]; |
| 480 | } |
| 481 | |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | void BNode_ClearBNodeInfo(void) { |
| 486 | int i, j; |
| 487 | |
| 488 | for (i = 0; i < 8; i++) { |
| 489 | if (BNode_allocated) { |
| 490 | for (j = 0; j < BNode_terrain_list[i].num_nodes; j++) { |
| 491 | if (BNode_terrain_list[i].nodes[j].num_edges) { |
| 492 | mem_free(BNode_terrain_list[i].nodes[j].edges); |
| 493 | } |
| 494 | } |
| 495 | mem_free(BNode_terrain_list[i].nodes); |
| 496 | } |
| 497 | |
| 498 | BNode_terrain_list[i].num_nodes = 0; |
| 499 | BNode_terrain_list[i].nodes = NULL; |
| 500 | } |
| 501 | |
| 502 | BNode_allocated = false; |
| 503 | BNode_verified = false; |
| 504 | } |
| 505 | |
| 506 | // Unused? |
| 507 | bool BNode_MakeSubPath(int16_t sroom, int16_t spnt, int16_t eroom, int16_t epnt, int flags, float size, int16_t *roomlist, |
| 508 | int16_t *pnts, int max_elements) { |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | void BNode_FreeRoom(room *rp) { |
| 513 | int i; |
| 514 | |
| 515 | for (i = 0; i < rp->bn_info.num_nodes; i++) { |
| 516 | if (rp->bn_info.nodes[i].num_edges) { |
| 517 | mem_free(rp->bn_info.nodes[i].edges); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | if (rp->bn_info.num_nodes) { |
| 522 | mem_free(rp->bn_info.nodes); |
| 523 | rp->bn_info.nodes = NULL; |
| 524 | rp->bn_info.num_nodes = 0; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void BNode_RemapTerrainRooms(int old_hri, int new_hri) { |
| 529 | int delta = new_hri - old_hri; |
| 530 | int i; |
| 531 | |
| 532 | if (!BNode_allocated) |
| 533 | return; |
| 534 | |
| 535 | if (delta == 0) |
| 536 | return; |
| 537 | |
| 538 | if (new_hri < 0) |
| 539 | return; |
| 540 | |
| 541 | ASSERT(delta <= 1); |
| 542 | |
| 543 | for (i = 0; i <= new_hri + BOA_num_terrain_regions; i++) { |
| 544 | |
| 545 | if ((i <= new_hri && Rooms[i].used) || (i > new_hri)) { |
| 546 | int j; |
| 547 | int k; |
| 548 | |
| 549 | // Skip external rooms |
| 550 | if ((i <= new_hri) && (Rooms[i].flags & RF_EXTERNAL)) |
| 551 | continue; |
| 552 | |
| 553 | bn_list *bnlist = BNode_GetBNListPtr(i); |
| 554 | ASSERT(bnlist); |
| 555 | |
| 556 | for (j = 0; j < bnlist->num_nodes; j++) { |
| 557 | for (k = 0; k < bnlist->nodes[j].num_edges; k++) { |
| 558 | if (bnlist->nodes[j].edges[k].end_room >= new_hri) { |
| 559 | bnlist->nodes[j].edges[k].end_room += delta; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | } |