Debian Codegraph @

Descent3/aipath.cpp

raw symbols SCIP
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
19 #include "aipath.h"
20 #include "gamepath.h"
21 #include "AIGoal.h"
22 #include "BOA.h"
23 #include "AIMain.h"
24 #include "object.h"
25 #include "objinfo.h"
26 #include "memory.h"
27 #include "doorway.h"
28 #include "aistruct.h"
29 #include "terrain.h"
30 #include "psrand.h"
31 #include "findintersection.h"
32
33 #define MAX_DYNAMIC_PATHS 50
34 #define MAX_NODES 50
35
36 ai_dynamic_path AIDynamicPath[MAX_DYNAMIC_PATHS];
37 int AIAltPath[MAX_ROOMS];
38 int AIAltPathNumNodes;
39
40 static void AIUpdatePathInfo(q_item **node_list, int start, int end) {
41 int cur_room = end;
42 int i;
43
44 AIAltPathNumNodes = 0;
45
46 // mprintf(0, "start crash loop\n");
47 while (cur_room != -1) {
48 AIAltPath[AIAltPathNumNodes++] = cur_room;
49 cur_room = node_list[cur_room]->parent;
50 }
51 // mprintf(0, "end crash loop\n");
52
53 // Reverse the list (so it is what we want)
54 for (i = 0; i < (AIAltPathNumNodes >> 1); i++) {
55 int temp;
56
57 temp = AIAltPath[i];
58 AIAltPath[i] = AIAltPath[AIAltPathNumNodes - i - 1];
59 AIAltPath[AIAltPathNumNodes - i - 1] = temp;
60 }
61 }
62
63 // Ok to use Highest_room_index offset stuff
64 bool AIFindAltPath(object *obj, int i, int j, float *dist) {
65 i = BOA_INDEX(i);
66 j = BOA_INDEX(j);
67
68 pq PQPath;
69 int counter;
70 q_item *start_node = new q_item(i, -1, 0.0f);
71 q_item *cur_node;
72 bool f_found = false;
73
74 q_item *node_list[MAX_ROOMS + 8];
75
76 // mprintf(0, "AI: Finding an alternate path from %d to %d\n", i, j);
77
78 if (i == -1 || j == -1) {
79 delete start_node;
80 return false;
81 }
82
83 memset(node_list, 0, sizeof(q_item *) * (MAX_ROOMS + 8));
84
85 PQPath.push(start_node);
86
87 while ((cur_node = PQPath.pop())) {
88 node_list[BOA_INDEX(cur_node->roomnum)] = cur_node;
89 ASSERT(BOA_INDEX(cur_node->roomnum) >= 0 && BOA_INDEX(cur_node->roomnum) <= Highest_room_index + 8);
90
91 if (cur_node->roomnum == j) {
92 AIUpdatePathInfo(node_list, i, j);
93 f_found = true;
94 goto done;
95 }
96
97 int num_portals;
98 bool f_room = true;
99 int t_index;
100
101 if (cur_node->roomnum <= Highest_room_index) {
102 num_portals = Rooms[cur_node->roomnum].num_portals;
103 } else {
104 t_index = cur_node->roomnum - Highest_room_index - 1;
105 num_portals = BOA_num_connect[t_index];
106 f_room = false;
107 }
108
109 for (counter = 0; counter < num_portals; counter++) {
110 int next_room;
111 q_item *list_item;
112 float new_cost;
113
114 if (!BOA_PassablePortal(cur_node->roomnum, counter, false, false))
115 continue;
116
117 if (f_room)
118 next_room = Rooms[cur_node->roomnum].portals[counter].croom;
119 else
120 next_room = BOA_connect[t_index][counter].roomnum;
121
122 if (next_room <= Highest_room_index && Rooms[next_room].flags & RF_EXTERNAL) {
123 int next_portal;
124 next_portal = BOA_DetermineStartRoomPortal(BOA_INDEX(next_room), NULL, BOA_INDEX(cur_node->roomnum), NULL);
125
126 int cell = GetTerrainCellFromPos(&Rooms[next_room].portals[next_portal].path_pnt);
127 ASSERT(cell != -1); // DAJ -1FIX
128 next_room = Highest_room_index + TERRAIN_REGION(cell) + 1;
129 }
130
131 if (next_room < 0 || next_room == BOA_NO_PATH)
132 continue;
133
134 if (BOA_LockedDoor(obj, next_room))
135 continue;
136
137 int next_portal = 0;
138
139 if (BOA_INDEX(next_room) != BOA_INDEX(cur_node->roomnum)) {
140 next_portal = BOA_DetermineStartRoomPortal(BOA_INDEX(next_room), NULL, BOA_INDEX(cur_node->roomnum), NULL,
141 false, false, NULL);
142 }
143
144 if (next_portal == -1)
145 continue;
146
147 if (BOA_cost_array[cur_node->roomnum][counter] >= 0.0f)
148 new_cost = cur_node->cost +
149 (1.0f + 0.1f * ((float)ps_rand() - (float)D3_RAND_MAX / 2.0f) / ((float)D3_RAND_MAX / 2.0f)) *
150 (BOA_cost_array[cur_node->roomnum][counter] + BOA_cost_array[BOA_INDEX(next_room)][next_portal]);
151 else
152 continue;
153
154 // new_cost = cur_node->cost + BOA_cost_array[cur_node->roomnum][counter];
155 list_item = node_list[BOA_INDEX(next_room)];
156
157 if (list_item != NULL && list_item->cost < new_cost)
158 continue;
159
160 if (list_item == NULL) {
161 list_item = new q_item(BOA_INDEX(next_room), cur_node->roomnum, new_cost);
162 ASSERT(BOA_INDEX(cur_node->roomnum) >= 0 && BOA_INDEX(cur_node->roomnum) <= Highest_room_index + 8);
163
164 node_list[BOA_INDEX(next_room)] = list_item;
165 ASSERT(BOA_INDEX(next_room) >= 0 && BOA_INDEX(next_room) <= Highest_room_index + 8);
166 PQPath.push(list_item);
167 } else {
168 list_item->cost = new_cost;
169 list_item->parent = cur_node->roomnum;
170 }
171 }
172 }
173
174 // Mark as an impossible path.
175
176 // mprintf(0, "Found an impossible path\n");
177
178 done:
179 for (counter = 0; counter < MAX_ROOMS + 8; counter++) {
180 if (node_list[counter])
181 delete node_list[counter];
182 }
183
184 if (f_found && dist) {
185 *dist = 0.0f;
186
187 if (AIAltPathNumNodes > 1) {
188 int x;
189
190 for (x = 0; x < AIAltPathNumNodes - 1; x++) {
191 int cur_room = AIAltPath[x];
192 int next_room = AIAltPath[x + 1];
193
194 int cur_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, next_room, NULL);
195 int next_portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_room, NULL);
196
197 ASSERT(cur_portal != -1 && next_portal != -1);
198
199 *dist += BOA_cost_array[cur_room][cur_portal] + BOA_cost_array[next_room][next_portal];
200 }
201 }
202 }
203
204 // mprintf(0, "Done\n");
205 return f_found;
206 }
207
208 bool AIPathGetCurrentNodePos(ai_path_info *aip, vector *pos, int *room) {
209 int c_path = aip->cur_path;
210 int c_node = aip->cur_node;
211
212 int p_type = aip->path_type[c_path];
213 int p_index = aip->path_id[c_path];
214
215 if (p_type == AIP_DYNAMIC) {
216 // chrishack -- add asserts
217
218 *pos = AIDynamicPath[p_index].pos[c_node];
219 if (room) {
220 *room = AIDynamicPath[p_index].roomnum[c_node];
221 }
222
223 return true;
224 } else if (p_type == AIP_STATIC) {
225 // chrishack -- add asserts
226
227 *pos = GamePaths[p_index].pathnodes[c_node].pos;
228 if (room) {
229 *room = GamePaths[p_index].pathnodes[c_node].roomnum;
230 }
231
232 return true;
233 }
234
235 return false;
236 }
237
238 // Chrishack -- need to add static path flag stuff
239 static bool AIPathMoveToNextNode(ai_path_info *aip) {
240 int c_path = aip->cur_path;
241 int c_node = aip->cur_node;
242
243 int p_type = aip->path_type[c_path];
244 int p_index = aip->path_id[c_path];
245
246 c_node++;
247
248 if (c_node <= aip->path_end_node[c_path]) {
249 aip->cur_node++;
250 return true;
251 }
252
253 c_path++;
254
255 // We must want to check the next path
256 if (c_path < aip->num_paths) {
257 aip->cur_path++;
258 aip->cur_node = aip->path_start_node[aip->cur_path];
259
260 return true;
261 }
262
263 return false;
264 }
265
266 bool AIPathAtStart(ai_path_info *aip) {
267 if (aip->cur_path == 0 && aip->cur_node == aip->path_start_node[0])
268 return true;
269 else
270 return false;
271 }
272
273 bool AIPathAtEnd(ai_path_info *aip) {
274 if (aip->cur_path == aip->num_paths - 1 && aip->cur_node == aip->path_end_node[aip->num_paths - 1])
275 return true;
276 else
277 return false;
278 }
279
280 static void AIPathSetAtEnd(ai_path_info *aip) {
281 aip->cur_path = aip->num_paths - 1;
282 aip->cur_node = aip->path_end_node[aip->num_paths - 1];
283 }
284
285 void AIPathSetAtStart(ai_path_info *aip) {
286 aip->cur_path = 0;
287 aip->cur_node = aip->path_start_node[0];
288 }
289
290 static bool AIPathMoveToPrevNode(ai_path_info *aip) {
291 int c_path = aip->cur_path;
292 int c_node = aip->cur_node;
293
294 c_node--;
295
296 if (c_node >= 0) {
297 aip->cur_node--;
298 return true;
299 }
300
301 c_path--;
302
303 // We must want to check the next path
304 if (c_path > 0) {
305 aip->cur_path--;
306
307 if (aip->path_type[c_path] == AIP_DYNAMIC) {
308 aip->cur_node = AIDynamicPath[aip->path_id[c_path]].num_nodes - 1;
309 } else if (aip->path_type[c_path] == AIP_STATIC) {
310 aip->cur_node = aip->path_end_node[c_path];
311 }
312 return true;
313 }
314
315 return false;
316 }
317
318 bool AIPathGetNextNodePos(ai_path_info *aip, vector *pos, int *room) {
319 if (AIPathMoveToNextNode(aip)) {
320 AIPathGetCurrentNodePos(aip, pos, room);
321 AIPathMoveToPrevNode(aip);
322
323 return true;
324 }
325
326 return false;
327 }
328
329 bool AIPathGetPrevNodePos(ai_path_info *aip, vector *pos, int *room) {
330 if (AIPathMoveToPrevNode(aip)) {
331 AIPathGetCurrentNodePos(aip, pos, room);
332 AIPathMoveToNextNode(aip);
333
334 return true;
335 }
336
337 return false;
338 }
339
340 static void AIPathComplete(object *obj) { GoalPathComplete(obj); }
341
342 void AIPathMoveTurnTowardsNode(object *obj, vector *mdir, bool *f_moved) {
343 ai_path_info *aip = &obj->ai_info->path;
344 goal *g_ptr = &obj->ai_info->goals[obj->ai_info->path.goal_index];
345
346 bool f_too_far_from_end = false;
347
348 // game_path *p = &GamePaths[path_info->path_id];
349 int n = aip->cur_node;
350 int p = aip->cur_path;
351
352 bool f_reverse = (g_ptr->flags & GF_PATH_MOVE_REVERSE_DIR) != 0;
353
354 ai_frame *ai_info = obj->ai_info;
355
356 vector cur_pos;
357
358 AIPathGetCurrentNodePos(aip, &cur_pos);
359 // mprintf(0, "Cur pos = %f,%f,%f\n", XYZ(&cur_pos));
360
361 // mprintf(0, "1 R %d, Goal p %d, n %d\n", f_reverse, p, n);
362
363 // mprintf(0, "s %f, %f, %f\n", XYZ(&obj->pos));
364 // mprintf(0, "l %f, %f, %f\n", XYZ(&obj->last_pos));
365
366 // mprintf(0, "%d is at Node %d\n", OBJNUM(obj), n);
367
368 bool f_force_complete_stopping = false;
369
370 if (((!f_reverse && AIPathAtEnd(aip)) || (f_reverse && AIPathAtStart(aip))) &&
371 (((goal *)(&obj->ai_info->goals[aip->goal_index]))->circle_distance == 0.0f) &&
372 obj->movement_type == MT_PHYSICS) {
373 f_too_far_from_end = !AIMoveTowardsPosition(obj, &cur_pos, 1.0, true, mdir, f_moved);
374 if (!f_too_far_from_end) {
375 f_force_complete_stopping = true;
376 }
377 } else {
378 vector dir = cur_pos - obj->pos;
379
380 if (obj->movement_type == MT_WALKING) {
381 float dot = dir * obj->orient.uvec;
382 dir -= dot * obj->orient.uvec;
383 }
384 vm_NormalizeVector(&dir);
385
386 *mdir = dir;
387 *f_moved = false;
388 // AIMoveTowardsDir(obj, &dir);
389 }
390 // removed check if(f_reverse && (path_info->next_node < path_info->start_node))
391 // removed check {
392 // removed check mprintf(0, "AI ERROR: Fixed a bad predefined path next_node to goal_node");
393 // removed check path_info->next_node = path_info->start_node;
394 // removed check Int3();
395 // removed check }
396 // removed check
397 // removed check if(!(f_reverse) && path_info->next_node > path_info->end_node)
398 // removed check {
399 // removed check mprintf(0, "AI ERROR: Fixed a bad predefined path next_node to goal_node");
400 // removed check path_info->next_node = path_info->end_node;
401 // removed check Int3();
402 // removed check }
403
404 bool f_pass_node;
405 int last_n;
406 int last_p;
407
408 pass_node:
409
410 f_pass_node = false;
411 last_n = n;
412 last_p = p;
413
414 if (obj->movement_type == MT_WALKING) {
415 vector dir = cur_pos - obj->pos;
416
417 if (obj->movement_type == MT_WALKING) {
418 float dot = dir * obj->orient.uvec;
419 dir -= dot * obj->orient.uvec;
420 }
421 float dist = vm_GetMagnitude(&dir);
422
423 if (dist < 1.5f) {
424 f_pass_node = true;
425 }
426 }
427
428 if (f_force_complete_stopping) {
429 f_pass_node = true;
430 }
431
432 if (!f_pass_node) {
433 if (!f_reverse) {
434 if (AIPathAtStart(aip)) {
435 if (AIPathAtEnd(aip)) {
436 if ((obj->pos - cur_pos) * (obj->last_pos - cur_pos) <= 0.0f && !f_too_far_from_end)
437 f_pass_node = true;
438 } else {
439 vector next_pos;
440 AIPathGetNextNodePos(aip, &next_pos, NULL);
441
442 vector proj_line = obj->pos - cur_pos;
443 vector line = next_pos - cur_pos;
444 float proj = proj_line * line;
445
446 if (proj >= 0.0f)
447 f_pass_node = true;
448 }
449 } else {
450 vector prev_pos;
451 AIPathGetPrevNodePos(aip, &prev_pos, NULL);
452
453 vector proj = obj->pos - prev_pos;
454 vector line = cur_pos - prev_pos;
455 float line_len = vm_NormalizeVector(&line);
456 float proj_len = proj * line;
457
458 if (proj_len >= line_len)
459 f_pass_node = true;
460 }
461 } else {
462 if (AIPathAtEnd(aip)) {
463 if (AIPathAtStart(aip)) {
464 if ((obj->pos - cur_pos) * (obj->last_pos - cur_pos) <= 0.0f && !f_too_far_from_end)
465 f_pass_node = true;
466 } else {
467 vector prev_pos;
468 AIPathGetPrevNodePos(aip, &prev_pos, NULL);
469
470 vector proj_line = obj->pos - cur_pos;
471 vector line = prev_pos - cur_pos;
472 float proj = proj_line * line;
473
474 if (proj >= 0.0f)
475 f_pass_node = true;
476 }
477 } else {
478 vector next_pos;
479 AIPathGetNextNodePos(aip, &next_pos, NULL);
480
481 vector proj = obj->pos - next_pos;
482 vector line = cur_pos - next_pos;
483 float line_len = vm_NormalizeVector(&line);
484 float proj_len = proj * line;
485
486 if (proj_len >= line_len)
487 f_pass_node = true;
488 }
489 }
490 }
491
492 if (f_pass_node) {
493 // mprintf(0, "f_pass_node\n");
494
495 if (((f_reverse) && AIPathAtStart(aip)) || ((!f_reverse) && AIPathAtEnd(aip))) {
496 // mprintf(0, "At end node: ");
497
498 switch (g_ptr->flags & GFM_END_OF_PATH) {
499 case GF_PATH_REVERSE_AT_END:
500 // mprintf(0, "End reverse dir\n");
501 if (f_reverse)
502 g_ptr->flags &= ~GF_PATH_MOVE_REVERSE_DIR;
503 else
504 g_ptr->flags |= GF_PATH_MOVE_REVERSE_DIR;
505 return;
506 case GF_PATH_CIRCLE_AT_END:
507 // mprintf(0, "End circle dir\n");
508 if (f_reverse)
509 AIPathSetAtEnd(aip);
510 else
511 AIPathSetAtStart(aip);
512 return;
513 default:
514 // mprintf(0, "End kill goal\n");
515 AIPathComplete(obj);
516 return;
517 }
518 } else {
519 // mprintf(0, "Passed node\n");
520 if (g_ptr->flags & GF_PATH_MOVE_REVERSE_DIR)
521 AIPathMoveToPrevNode(aip);
522 else
523 AIPathMoveToNextNode(aip);
524 }
525
526 AIPathGetCurrentNodePos(aip, &cur_pos);
527
528 n = aip->cur_node;
529 p = aip->cur_path;
530
531 f_reverse = (g_ptr->flags & GF_PATH_MOVE_REVERSE_DIR) != 0;
532
533 if (n != last_n || p != last_p)
534 goto pass_node;
535 }
536 }
537
538 static bool AIPathFreeDPathSlot(int slot) {
539 ASSERT(AIDynamicPath[slot].use_count >= 0);
540 ASSERT(slot >= 0 && slot < MAX_DYNAMIC_PATHS && AIDynamicPath[slot].use_count != 0);
541
542 AIDynamicPath[slot].use_count--;
543
544 if (!AIDynamicPath[slot].use_count) {
545 AIDynamicPath[slot].num_nodes = 0;
546 AIDynamicPath[slot].owner_handle = OBJECT_HANDLE_NONE;
547 }
548
549 return true;
550 }
551
552 static bool AIPathGetDPathSlot(int *slot, int handle) {
553 int i;
554
555 for (i = 0; i < MAX_DYNAMIC_PATHS; i++) {
556 if (!AIDynamicPath[i].use_count) {
557 AIDynamicPath[i].use_count++;
558 *slot = i;
559 AIDynamicPath[i].owner_handle = handle;
560 return true;
561 } else if (ObjGet(AIDynamicPath[i].owner_handle) == NULL && AIDynamicPath[i].use_count == 1) {
562 AIPathFreeDPathSlot(i);
563
564 AIDynamicPath[i].use_count++;
565 *slot = i;
566 AIDynamicPath[i].owner_handle = handle;
567 return true;
568 }
569 }
570
571 mprintf(0, "Out of dynamic paths");
572 ASSERT(0); // -- get chris
573 return false;
574 }
575
576 bool AIPathInitPath(ai_path_info *aip) {
577 ASSERT(aip);
578
579 aip->num_paths = 0;
580 aip->cur_path = 0;
581 aip->cur_node = 0;
582
583 aip->goal_index = -1;
584 aip->goal_uid = -1;
585
586 return true;
587 }
588
589 bool AIPathFreePath(ai_path_info *aip) {
590 int i;
591
592 for (i = 0; i < aip->num_paths; i++) {
593 if (aip->path_type[i] == AIP_DYNAMIC) {
594 AIPathFreeDPathSlot(aip->path_id[i]);
595 }
596 }
597
598 AIPathInitPath(aip);
599
600 return true;
601 }
602
603 static bool AIPathAddDPath(ai_path_info *aip, int handle) {
604 int slot;
605 bool status;
606
607 if (aip->num_paths >= MAX_JOINED_PATHS) {
608 mprintf(0, "AI ERROR: Tried to join too many paths (Adding dpath)\n");
609 return false;
610 }
611
612 status = AIPathGetDPathSlot(&slot, handle);
613
614 if (status) {
615 aip->path_id[aip->num_paths] = slot;
616 aip->path_type[aip->num_paths] = AIP_DYNAMIC;
617
618 aip->path_start_node[aip->num_paths] = 0;
619 aip->path_end_node[aip->num_paths] = 0;
620
621 aip->path_flags[aip->num_paths] = 0;
622
623 aip->num_paths++;
624 }
625
626 return status;
627 }
628
629 static bool AIPathAddStaticPath(ai_path_info *aip, int path_id, int start_index, int end_index) {
630 // chrishack -- validation code is needed
631
632 if (aip->num_paths >= MAX_JOINED_PATHS) {
633 mprintf(0, "AI ERROR: Tried to join too many paths (Adding spath)\n");
634 return false;
635 }
636
637 aip->path_id[aip->num_paths] = path_id;
638 aip->path_type[aip->num_paths] = AIP_STATIC;
639 aip->path_start_node[aip->num_paths] = start_index;
640 aip->path_end_node[aip->num_paths] = end_index;
641
642 aip->num_paths++;
643
644 return true;
645 }
646
647 static inline bool AIPathAddDPathNode(ai_path_info *aip, int *slot, int *cur_node, vector *pos, int room, int handle) {
648 int status;
649
650 if (*cur_node == MAX_NODES) {
651 status = AIPathAddDPath(aip, handle);
652 if (!status)
653 Int3(); // chrishack -- out of dynamic paths
654
655 AIDynamicPath[*slot].num_nodes = MAX_NODES;
656 *cur_node = 0;
657 *slot = aip->path_id[aip->num_paths - 1];
658 }
659
660 AIDynamicPath[*slot].pos[*cur_node] = *pos;
661 AIDynamicPath[*slot].roomnum[(*cur_node)++] = room;
662
663 aip->path_end_node[aip->num_paths - 1] = *cur_node - 1;
664
665 return true;
666 }
667
668 static bool AIGenerateAltBNodePath(object *obj, vector *start_pos, int *start_room, vector *end_pos, int *end_room,
669 ai_path_info *aip, int *slot, int *cur_node, int handle) {
670 int x;
671 vector *pos;
672 bool f_path_exists = true;
673
674 bn_list *bnlist = BNode_GetBNListPtr(*start_room);
675 int last_node = BNode_FindDirLocalVisibleBNode(*start_room, start_pos, &obj->orient.fvec, obj->size);
676 if (last_node == -1) {
677 f_path_exists = false;
678 goto done;
679 }
680 ASSERT(last_node >= 0);
681
682 for (x = 0; x < AIAltPathNumNodes - 1 && f_path_exists; x++) {
683 int cur_room = BOA_INDEX(AIAltPath[x]);
684 int next_room = BOA_INDEX(AIAltPath[x + 1]);
685
686 bnlist = BNode_GetBNListPtr(cur_room);
687 ASSERT(bnlist);
688
689 if (next_room != cur_room && next_room != BOA_NO_PATH) {
690 int portal = BOA_DetermineStartRoomPortal(cur_room, NULL, next_room, NULL);
691 if (portal == -1) {
692 f_path_exists = false;
693 } else {
694 int bnode = -1;
695
696 if (cur_room >= 0 && cur_room <= Highest_room_index) {
697 bnode = Rooms[cur_room].portals[portal].bnode_index;
698 ASSERT(bnode >= 0 && bnode < BNode_GetBNListPtr(cur_room)->num_nodes);
699 } else {
700 int croom = BOA_connect[cur_room - Highest_room_index - 1][portal].roomnum;
701 int cportal = BOA_connect[cur_room - Highest_room_index - 1][portal].portal;
702
703 int r = Rooms[croom].portals[cportal].croom;
704 int p = Rooms[croom].portals[cportal].cportal;
705
706 ASSERT(Rooms[r].used && (Rooms[r].flags & RF_EXTERNAL));
707 bnode = Rooms[r].portals[p].bnode_index;
708 // ASSERT(bnode >= 0 && bnode < BNode_GetBNListPtr(r)->num_nodes);
709 }
710
711 // Add the last room...
712 bool f_ok = BNode_FindPath(cur_room, last_node, bnode, obj->size);
713 ASSERT(f_ok);
714 ASSERT(BNode_PathNumNodes);
715
716 int i;
717 for (i = 0; i < BNode_PathNumNodes; i++) {
718 pos = &bnlist->nodes[BNode_Path[i]].pos;
719 AIPathAddDPathNode(aip, slot, cur_node, pos, cur_room, handle);
720 }
721
722 int portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_room, NULL);
723 if (next_room <= Highest_room_index) {
724 bnode = Rooms[next_room].portals[portal].bnode_index;
725 ASSERT(bnode >= 0);
726 } else {
727 int croom = BOA_connect[next_room - Highest_room_index - 1][portal].roomnum;
728 int cportal = BOA_connect[next_room - Highest_room_index - 1][portal].portal;
729
730 int r = Rooms[croom].portals[cportal].croom;
731 int p = Rooms[croom].portals[cportal].cportal;
732
733 ASSERT(Rooms[r].used && (Rooms[r].flags & RF_EXTERNAL));
734 bnode = Rooms[r].portals[p].bnode_index;
735 // ASSERT(bnode >= 0 && bnode < BNode_GetBNListPtr(r)->num_nodes);
736 }
737
738 last_node = bnode;
739 ASSERT(last_node >= 0);
740 }
741 } else {
742 if (next_room == BOA_NO_PATH) {
743 f_path_exists = false;
744 }
745 }
746 }
747
748 if (f_path_exists) {
749 int bnode;
750
751 bn_list *bnlist = BNode_GetBNListPtr(*end_room);
752 bnode = BNode_FindClosestLocalVisibleBNode(*end_room, end_pos, obj->size);
753 if (bnode == -1) {
754 f_path_exists = false;
755 goto done;
756 }
757
758 // Add the last room...
759 bool f_ok = BNode_FindPath(*end_room, last_node, bnode, obj->size);
760 ASSERT(f_ok);
761 ASSERT(BNode_PathNumNodes);
762 int i;
763 for (i = 0; i < BNode_PathNumNodes; i++) {
764 pos = &bnlist->nodes[BNode_Path[i]].pos;
765 AIPathAddDPathNode(aip, slot, cur_node, pos, *end_room, handle);
766 }
767 }
768
769 done:
770 if (!f_path_exists)
771 AIPathFreePath(aip);
772
773 return f_path_exists;
774 }
775
776 static void AIGenerateAltBOAPath(vector *start_pos, vector *end_pos, ai_path_info *aip, int *slot, int *cur_node,
777 int handle) {
778 int x;
779 vector *pos = NULL;
780
781 for (x = 0; x < AIAltPathNumNodes - 1; x++) {
782 int cur_room = AIAltPath[x];
783 int next_room = AIAltPath[x + 1];
784
785 int i;
786
787 if (next_room >= 0 && next_room <= Highest_room_index) {
788 if (cur_room > Highest_room_index) {
789 i = BOA_DetermineStartRoomPortal(next_room, NULL, cur_room, NULL, false, false, NULL);
790 ASSERT(i != -1);
791 pos = &Rooms[next_room].portals[i].path_pnt;
792 } else {
793 for (i = 0; i < Rooms[next_room].num_portals; i++) {
794 if (cur_room <= Highest_room_index && Rooms[next_room].portals[i].croom == cur_room) {
795 pos = &Rooms[next_room].portals[i].path_pnt;
796 break;
797 }
798 }
799 }
800
801 // Add the adjoining room's portal point
802 ASSERT(i < Rooms[next_room].num_portals);
803 AIPathAddDPathNode(aip, slot, cur_node, pos, next_room, handle);
804
805 // Add its center point
806 pos = &Rooms[next_room].path_pnt;
807 AIPathAddDPathNode(aip, slot, cur_node, pos, next_room, handle);
808 } else {
809 if (BOA_INDEX(cur_room) <= Highest_room_index && cur_room != next_room) {
810 int p_index = BOA_DetermineStartRoomPortal(cur_room, NULL, next_room, NULL);
811
812 if (p_index >= 0) {
813 pos = &Rooms[cur_room].portals[p_index].path_pnt;
814 AIPathAddDPathNode(aip, slot, cur_node, pos, cur_room, handle);
815 }
816 }
817 }
818 }
819 }
820
821 // if(ROOMNUM_OUTSIDE(next_room) && ROOMNUM_OUTSIDE(*end_room) && (TERRAIN_REGION(next_room) ==
822 // TERRAIN_REGION(*end_room) || TERRAIN_REGION(*end_room) == 0 || TERRAIN_REGION(next_room) == 0))
823 // {
824 // next_room = *end_room;
825 // }
826
827 static bool AIGenerateBNodePath(object *obj, vector *start_pos, int *start_room, vector *end_pos, int *end_room,
828 ai_path_info *aip, int *slot, int *cur_node, int handle) {
829 int next_room = BOA_INDEX(*start_room);
830 bool f_path_exists = true;
831 vector *pos;
832
833 bn_list *bnlist; // = BNode_GetBNListPtr(*start_room);
834 int last_node = BNode_FindDirLocalVisibleBNode(*start_room, start_pos, &obj->orient.fvec, obj->size);
835 if (last_node == -1) {
836 f_path_exists = false;
837 goto done;
838 }
839
840 ASSERT(!(next_room >= 0 && next_room <= Highest_room_index && !Rooms[next_room].used));
841
842 // pos = &bnlist->nodes[last_node].pos;
843 // AIPathAddDPathNode(aip, slot, cur_node, pos, *start_room, handle);
844
845 while ((BOA_INDEX(next_room) != BOA_GetNextRoom(next_room, *end_room)) && (next_room != BOA_NO_PATH) &&
846 f_path_exists) {
847 int cur_room;
848
849 cur_room = next_room;
850 next_room = BOA_GetNextRoom(next_room, *end_room);
851 bnlist = BNode_GetBNListPtr(cur_room);
852 ASSERT(bnlist);
853
854 if (next_room != cur_room && next_room != BOA_NO_PATH) {
855 int portal = BOA_DetermineStartRoomPortal(cur_room, NULL, next_room, NULL);
856 if (portal == -1) {
857 f_path_exists = false;
858 } else {
859 int bnode;
860
861 if (cur_room <= Highest_room_index) {
862 bnode = Rooms[cur_room].portals[portal].bnode_index;
863 ASSERT(bnode >= 0 && bnode < BNode_GetBNListPtr(cur_room)->num_nodes);
864 } else {
865 int croom = BOA_connect[cur_room - Highest_room_index - 1][portal].roomnum;
866 int cportal = BOA_connect[cur_room - Highest_room_index - 1][portal].portal;
867
868 int r = Rooms[croom].portals[cportal].croom;
869 int p = Rooms[croom].portals[cportal].cportal;
870
871 ASSERT(Rooms[r].used && (Rooms[r].flags & RF_EXTERNAL));
872 bnode = Rooms[r].portals[p].bnode_index;
873 // ASSERT(bnode >= 0 && bnode < BNode_GetBNListPtr(r)->num_nodes);
874 }
875
876 // Add the last room...
877 bool f_ok = BNode_FindPath(cur_room, last_node, bnode, obj->size);
878 ASSERT(f_ok);
879 ASSERT(BNode_PathNumNodes);
880
881 int i;
882 for (i = 0; i < BNode_PathNumNodes; i++) {
883 pos = &bnlist->nodes[BNode_Path[i]].pos;
884 AIPathAddDPathNode(aip, slot, cur_node, pos, cur_room, handle);
885 }
886
887 int portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_room, NULL);
888 if (portal < 0) {
889 f_path_exists = false;
890 } else {
891 if (next_room <= Highest_room_index) {
892 last_node = Rooms[next_room].portals[portal].bnode_index;
893 ASSERT(last_node >= 0 && last_node < BNode_GetBNListPtr(next_room)->num_nodes);
894 } else {
895 int croom = BOA_connect[next_room - Highest_room_index - 1][portal].roomnum;
896 int cportal = BOA_connect[next_room - Highest_room_index - 1][portal].portal;
897
898 int r = Rooms[croom].portals[cportal].croom;
899 int p = Rooms[croom].portals[cportal].cportal;
900
901 ASSERT(Rooms[r].used && (Rooms[r].flags & RF_EXTERNAL));
902 last_node = Rooms[r].portals[p].bnode_index;
903 // ASSERT(last_node >= 0 && last_node < BNode_GetBNListPtr(r)->num_nodes);
904 }
905 }
906 }
907 } else {
908 if (next_room == BOA_NO_PATH) {
909 f_path_exists = false;
910 }
911 }
912 }
913
914 if (f_path_exists) {
915 int bnode;
916
917 bn_list *bnlist = BNode_GetBNListPtr(*end_room);
918 bnode = BNode_FindClosestLocalVisibleBNode(*end_room, end_pos, obj->size);
919 if (bnode == -1) {
920 f_path_exists = false;
921 goto done;
922 }
923
924 // Add the last room...
925 bool f_ok = BNode_FindPath(*end_room, last_node, bnode, obj->size);
926 ASSERT(f_ok);
927 ASSERT(BNode_PathNumNodes);
928 int i;
929 for (i = 0; i < BNode_PathNumNodes; i++) {
930 pos = &bnlist->nodes[BNode_Path[i]].pos;
931 AIPathAddDPathNode(aip, slot, cur_node, pos, *end_room, handle);
932 }
933 }
934
935 done:
936 if (!f_path_exists)
937 AIPathFreePath(aip);
938
939 return f_path_exists;
940 }
941
942 static bool AIGenerateBOAPath(vector *start_pos, int *start_room, vector *end_pos, int *end_room, ai_path_info *aip,
943 int *slot, int *cur_node, int handle) {
944 int next_room = *start_room;
945 bool f_path_exists = true;
946 vector *pos;
947
948 ASSERT(ROOMNUM_OUTSIDE(next_room) || (next_room >= 0 && next_room <= Highest_room_index && Rooms[next_room].used));
949
950 // Add in the center of the first room
951 if (!ROOMNUM_OUTSIDE(next_room)) {
952 pos = &Rooms[*start_room].path_pnt;
953 AIPathAddDPathNode(aip, slot, cur_node, pos, *start_room, handle);
954 }
955
956 if (ROOMNUM_OUTSIDE(next_room) && ROOMNUM_OUTSIDE(*end_room) &&
957 (TERRAIN_REGION(next_room) == TERRAIN_REGION(*end_room) || TERRAIN_REGION(*end_room) == 0 ||
958 TERRAIN_REGION(next_room) == 0)) {
959 next_room = *end_room;
960 }
961
962 while ((BOA_INDEX(next_room) != BOA_GetNextRoom(next_room, *end_room)) && (next_room != BOA_NO_PATH)) {
963 int cur_room;
964
965 cur_room = next_room;
966 next_room = BOA_GetNextRoom(next_room, *end_room);
967
968 if (next_room != cur_room && next_room != BOA_NO_PATH && BOA_INDEX(next_room) <= Highest_room_index) {
969 int i;
970
971 for (i = 0; i < Rooms[next_room].num_portals; i++) {
972 if (Rooms[next_room].portals[i].croom == cur_room) {
973 pos = &Rooms[next_room].portals[i].path_pnt;
974 break;
975 }
976
977 if (Rooms[Rooms[next_room].portals[i].croom].flags & RF_EXTERNAL) {
978 int cell = GetTerrainCellFromPos(&Rooms[next_room].portals[i].path_pnt);
979 ASSERT(cell != -1); // DAJ -1FIX
980 int region = TERRAIN_REGION(cell);
981
982 if (Highest_room_index + region + 1 == BOA_INDEX(cur_room)) {
983 pos = &Rooms[next_room].portals[i].path_pnt;
984 break;
985 }
986 }
987 }
988
989 // Add the adjoining room's portal point
990 ASSERT(i < Rooms[next_room].num_portals);
991 AIPathAddDPathNode(aip, slot, cur_node, pos, next_room, handle);
992
993 // Add its center point
994 pos = &Rooms[next_room].path_pnt;
995 AIPathAddDPathNode(aip, slot, cur_node, pos, next_room, handle);
996 } else {
997 if (next_room == BOA_NO_PATH) {
998 f_path_exists = false;
999 } else if (BOA_INDEX(cur_room) <= Highest_room_index && cur_room != next_room) {
1000 int p_index = BOA_DetermineStartRoomPortal(cur_room, NULL, next_room, NULL);
1001
1002 if (p_index >= 0) {
1003 pos = &Rooms[cur_room].portals[p_index].path_pnt;
1004 AIPathAddDPathNode(aip, slot, cur_node, pos, cur_room, handle);
1005 }
1006 }
1007 }
1008 }
1009
1010 return f_path_exists;
1011 }
1012
1013 bool AIPathAllocPath(object *obj, ai_frame *ai_info, void *goal_ptr, int *start_room, vector *start_pos, int *end_room,
1014 vector *end_pos, float rad, int flags, int handle, int ignore_obj) {
1015 bool status;
1016 bool f_path_exists = true;
1017
1018 ai_path_info *aip = &ai_info->path;
1019
1020 #ifdef _DEBUG
1021 if (AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index) {
1022 mprintf(0, "AI Note: In free path\n");
1023 }
1024 #endif
1025 AIPathFreePath(aip);
1026
1027 // if(ai_info->awareness <= AWARE_NONE && !(ai_info->flags & AIF_PERSISTANT)) // chrishack -- scalar max_nodes in
1028 // path by awareness return false;
1029
1030 aip->goal_index = (goal *)goal_ptr - ai_info->goals;
1031 aip->goal_uid = ((goal *)goal_ptr)->goal_uid;
1032
1033 status = AIPathAddDPath(aip, handle);
1034
1035 if (status) {
1036 int slot = aip->path_id[0];
1037 int cur_node = 0;
1038
1039 // Account for closed doors
1040 if (BOA_GetNextRoom(*start_room, *end_room) != BOA_NO_PATH) {
1041 bool f_make_alt = false;
1042 bool f_bline_ok = false;
1043 int last_room;
1044 fvi_query fq;
1045 fvi_info hit_info;
1046
1047 fq.p0 = start_pos;
1048 fq.startroom = *start_room;
1049 fq.p1 = end_pos;
1050 fq.rad = obj->size - .1f;
1051 if (fq.rad <= 0.0f)
1052 fq.rad = .1f;
1053 fq.thisobjnum = OBJNUM(obj);
1054 fq.ignore_obj_list = NULL;
1055 fq.flags = FQ_CHECK_OBJS | FQ_NO_RELINK | FQ_IGNORE_WEAPONS | FQ_IGNORE_POWERUPS;
1056
1057 int fate = fvi_FindIntersection(&fq, &hit_info);
1058
1059 if (fate == HIT_NONE ||
1060 ((fate == HIT_OBJECT || fate == HIT_SPHERE_2_POLY_OBJECT) && hit_info.hit_object[0] == ignore_obj)) {
1061 f_make_alt = false;
1062 f_bline_ok = true;
1063 } else if (BOA_Array[BOA_INDEX(*start_room)][BOA_INDEX(*end_room)] & BOAF_TOO_SMALL_FOR_ROBOT) {
1064 f_make_alt = true;
1065 } else if (BOA_HasPossibleBlockage(*start_room, *end_room)) {
1066 int next_room = BOA_INDEX(*start_room);
1067
1068 ASSERT(next_room >= 0 && next_room <= Highest_room_index + 8);
1069
1070 do {
1071 if (next_room != BOA_INDEX(*start_room) && BOA_LockedDoor(obj, next_room)) {
1072 f_make_alt = true;
1073 break;
1074 }
1075
1076 last_room = next_room;
1077 next_room = BOA_GetNextRoom(next_room, *end_room);
1078
1079 if (BOA_INDEX(last_room) != BOA_INDEX(next_room) && next_room != BOA_NO_PATH) {
1080 if (BOA_DetermineStartRoomPortal(last_room, NULL, next_room, NULL) == -1) {
1081 f_make_alt = true;
1082 break;
1083 }
1084 }
1085 } while (BOA_INDEX(next_room) != BOA_INDEX(*start_room) && (BOA_INDEX(next_room) != BOA_INDEX(*end_room)) &&
1086 (next_room != BOA_NO_PATH));
1087 }
1088
1089 error_make_alt:
1090
1091 if (f_make_alt) {
1092 f_path_exists = AIFindAltPath(obj, *start_room, *end_room);
1093
1094 if (f_path_exists) {
1095 bool f_bnode_ok = BNode_allocated && BNode_verified &&
1096 !((ROOMNUM_OUTSIDE(*start_room) && TERRAIN_REGION(*start_room) == 0) ||
1097 (ROOMNUM_OUTSIDE(*end_room) && TERRAIN_REGION(*end_room) == 0)) &&
1098 !(ROOMNUM_OUTSIDE(*start_room) && ROOMNUM_OUTSIDE(*end_room) &&
1099 TERRAIN_REGION(*start_room) != TERRAIN_REGION(*end_room));
1100
1101 if (f_bnode_ok)
1102 f_path_exists =
1103 AIGenerateAltBNodePath(obj, start_pos, start_room, end_pos, end_room, aip, &slot, &cur_node, handle);
1104 else
1105 AIGenerateAltBOAPath(start_pos, end_pos, aip, &slot, &cur_node, handle);
1106
1107 if (!f_path_exists) {
1108 mprintf(0, "Warning Alt path from %d to %d failed.\n", *start_room, *end_room);
1109 }
1110 }
1111 } else if (!f_bline_ok) {
1112 bool f_bnode_ok = BNode_allocated && BNode_verified &&
1113 !((ROOMNUM_OUTSIDE(*start_room) && TERRAIN_REGION(*start_room) == 0) ||
1114 (ROOMNUM_OUTSIDE(*end_room) && TERRAIN_REGION(*end_room) == 0)) &&
1115 !(ROOMNUM_OUTSIDE(*start_room) && ROOMNUM_OUTSIDE(*end_room) &&
1116 TERRAIN_REGION(*start_room) != TERRAIN_REGION(*end_room));
1117
1118 if (f_bnode_ok)
1119 f_path_exists =
1120 AIGenerateBNodePath(obj, start_pos, start_room, end_pos, end_room, aip, &slot, &cur_node, handle);
1121 else
1122 f_path_exists = AIGenerateBOAPath(start_pos, start_room, end_pos, end_room, aip, &slot, &cur_node, handle);
1123
1124 if (!f_path_exists) {
1125 f_make_alt = true;
1126 goto error_make_alt;
1127 }
1128 }
1129
1130 if (f_path_exists) {
1131 // Add the end point
1132 AIPathAddDPathNode(aip, &slot, &cur_node, end_pos, *end_room, handle);
1133
1134 // Makes sure that the last dynamic path
1135 AIDynamicPath[slot].num_nodes = cur_node;
1136 }
1137 } else {
1138 f_path_exists = false;
1139 // if(!(0x80000000 & *start_room) || !(0x80000000 & *end_room))
1140 // mprintf(0, "No path available from %d to %d\n", *start_room, *end_room);
1141
1142 #ifdef _DEBUG
1143 if (AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index) {
1144 mprintf(0, "AI Note: In free path\n");
1145 }
1146 #endif
1147
1148 AIPathFreePath(aip);
1149 return false;
1150 }
1151 } else {
1152 f_path_exists = false;
1153 mprintf(0, "No dynamic paths left\n");
1154 }
1155
1156 if (!f_path_exists) {
1157 #ifdef _DEBUG
1158 if (AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index) {
1159 mprintf(0, "AI Note: In free path\n");
1160 }
1161 #endif
1162
1163 AIPathFreePath(aip);
1164 return false;
1165 }
1166
1167 return true;
1168 }
1169
1170 bool AIPathSetAsStaticPath(object *obj, void *goal_ptr, int path_id, int start_node, int end_node, int cur_node) {
1171 bool status;
1172 ai_frame *ai_info = obj->ai_info;
1173 ai_path_info *aip = &ai_info->path;
1174
1175 #ifdef _DEBUG
1176 if (AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index) {
1177 mprintf(0, "AI Note: In free path\n");
1178 }
1179 #endif
1180 AIPathFreePath(aip);
1181
1182 aip->goal_index = (goal *)goal_ptr - ai_info->goals;
1183 aip->goal_uid = ((goal *)goal_ptr)->goal_uid;
1184
1185 status = AIPathAddStaticPath(aip, path_id, start_node, end_node);
1186
1187 if (status) {
1188 aip->cur_node = cur_node;
1189 }
1190
1191 return status;
1192 }
1193
1194 #ifdef _DEBUG
1195
1196 #include "hud.h"
1197
1198 int AIPath_test_end_room = 0;
1199 vector AIPath_test_end_pos = {0, 0, 0};
1200
1201 bool MakeTestPath(int *start_room, vector *pos) {
1202 bool status;
1203 ai_frame ai_info;
1204 int count = 0;
1205
1206 ai_info.awareness = AWARE_MOSTLY;
1207
1208 AIPathInitPath(&ai_info.path);
1209
1210 status = AIPathAllocPath(NULL, &ai_info, &ai_info.goals[0], start_room, pos, &AIPath_test_end_room,
1211 &AIPath_test_end_pos, 0.0f, 0, 0);
1212
1213 while (status) {
1214 vector pos;
1215 int room;
1216
1217 status = AIPathGetCurrentNodePos(&ai_info.path, &pos, &room);
1218
1219 if (status) {
1220 int id = FindObjectIDName("Shield");
1221 ObjCreate(OBJ_POWERUP, id, room, &pos, &Identity_matrix);
1222 count++;
1223 }
1224
1225 status = AIPathMoveToNextNode(&ai_info.path);
1226 }
1227
1228 AddHUDMessage("The test path has %d nodes.", count);
1229
1230 // #ifdef _DEBUG
1231 // if(AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index)
1232 // {
1233 // mprintf(0, "AI Note: In free path\n");
1234 // }
1235 // #endif
1236 AIPathFreePath(&ai_info.path);
1237
1238 return true;
1239 }
1240
1241 #endif