Debian Codegraph @

Descent3/BOA.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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/BOA.cpp $
21 * $Revision: 121 $
22 * $Date: 4/19/00 5:09p $
23 * $Author: Matt $
24 *
25 * Description goes here
26 *
27 * $Log: /DescentIII/main/BOA.cpp $
28 *
29 * 121 4/19/00 5:09p Matt
30 * From Duane for 1.4
31 * Added checks, asserts, and fixes for bad return values
32 * Mac-only optimizations and mem alloc/free change
33 *
34 * 120 3/20/00 12:01p Matt
35 * Merge of Duane's post-1.3 changes.
36 * Changed some malloc/free calls to mem_malloc/mem_free (Mac only)
37 *
38 * 119 10/08/99 4:27p Chris
39 * Fixed a bug with terrain regioning
40 *
41 * 118 6/11/99 6:06p Chris
42 *
43 * 117 6/11/99 6:00p Chris
44 * Added status bars for boa_vis
45 *
46 * 116 6/11/99 12:22p Chris
47 * NewEditor changes
48 *
49 * 115 6/11/99 12:20p Chris
50 * New Editor changes
51 *
52 * 114 5/23/99 12:32a Chris
53 * Forcefields are now ignored in vis stuff
54 *
55 * 113 5/21/99 3:27a Chris
56 * Fuck me... Broken fix... Fixed? I hope.
57 *
58 * 112 5/21/99 1:35a Chris
59 * Fixed a bug in vis (outside areas could only see the first room down)
60 * :(
61 *
62 * 111 5/20/99 1:16a Chris
63 * Made going outside more expensive
64 *
65 * 110 5/10/99 10:10p Ardussi
66 * changes to compile on Mac
67 *
68 * 109 5/09/99 3:25p Jason
69 * fixed potential BOA problem
70 *
71 * 108 5/03/99 5:54p Kevin
72 * removed logging code
73 *
74 * 107 5/03/99 5:45p Jason
75 * trying to track down boa problem
76 *
77 * 106 5/03/99 5:12p Jason
78 * fixing BOA vis problem...
79 *
80 * 105 5/01/99 5:12p Jason
81 * made BOA checksum finally reliable
82 *
83 * 104 5/01/99 4:45p Chris
84 * Fixed a bug with bogus vis checksums
85 *
86 * 103 4/30/99 2:46p Chris
87 * Added a pragma for turning optimazatons off for checksum functions
88 *
89 * 102 4/30/99 11:54a Chris
90 *
91 * 101 4/29/99 5:43p Chris
92 * Added the check for bad center points
93 *
94 * 100 4/28/99 1:20p Chris
95 * Added the ability to block portals
96 *
97 * 99 4/27/99 12:57p Chris
98 *
99 * 98 4/27/99 11:42a Chris
100 * Fixed a multi-system induced bug
101 *
102 * 97 4/27/99 10:48a Chris
103 * Slowly and carefully improving BNodes
104 *
105 * 96 4/21/99 1:30p Matt
106 * Make breakable glass use the new breakable flag, instead of
107 * piggybacking on the destroyable flag.
108 *
109 * 95 4/21/99 3:03a Chris
110 * Fixed bug with blastable doors and the AI code. :)
111 *
112 * 94 4/20/99 8:55p Chris
113 * Fixed problem with robots not being able to open locked doors that a
114 * player has the key for.
115 *
116 * 93 4/18/99 5:39a Chris
117 * Vastly improved the path node system
118 *
119 * $NoKeywords: $
120 */
121
122 #ifdef EDITOR
123 #include "editor\d3edit.h"
124 #endif
125 #ifdef NEWEDITOR
126 #include "neweditor\globals.h"
127 #endif
128
129 #include "BOA.h"
130 #include "vecmat.h"
131 #include "room.h"
132 #include <string.h>
133 #include <stdlib.h>
134 #include "object.h"
135 #include "bsp.h"
136 #include "pserror.h"
137 #include "findintersection.h"
138 #include "mem.h"
139 #include "doorway.h"
140 #include "string.h"
141
142 #define BOA_VERSION 25
143
144 static const uint8_t bbf_lookup[27] = {(0),
145 (0x01),
146 (0x02),
147 (0x04),
148 (0x08),
149 (0x10),
150 (0x20),
151 (0x01 | 0x02),
152 (0x01 | 0x04),
153 (0x01 | 0x10),
154 (0x01 | 0x20),
155 (0x02 | 0x04),
156 (0x02 | 0x08),
157 (0x02 | 0x20),
158 (0x04 | 0x08),
159 (0x04 | 0x10),
160 (0x08 | 0x10),
161 (0x08 | 0x20),
162 (0x10 | 0x20),
163 (0x01 | 0x02 | 0x04),
164 (0x01 | 0x02 | 0x20),
165 (0x01 | 0x04 | 0x10),
166 (0x01 | 0x10 | 0x20),
167 (0x08 | 0x02 | 0x04),
168 (0x08 | 0x02 | 0x20),
169 (0x08 | 0x04 | 0x10),
170 (0x08 | 0x10 | 0x20)};
171
172 uint16_t BOA_Array[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS][MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
173 float BOA_cost_array[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS][MAX_PATH_PORTALS];
174 int BOA_mine_checksum = 0;
175 int BOA_vis_checksum = 0; // this checksum is for the VIS bit of the boa array
176 bool BOA_vis_valid = 0; // Is the vis table up to date and valid to use?
177 int BOA_AABB_checksum = 0;
178 int BOA_AABB_ROOM_checksum[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
179
180 static bool BOA_f_making_boa = false;
181
182 int BOA_num_mines = 0;
183 int BOA_num_terrain_regions = 0;
184
185 int BOA_num_connect[MAX_BOA_TERRAIN_REGIONS];
186 connect_data BOA_connect[MAX_BOA_TERRAIN_REGIONS][MAX_PATH_PORTALS];
187
188 static void add_mine_room(int room, int mine, char *checked);
189 static void compute_mine_info();
190 static void add_terrain_cell(int cell, int t_region, char *checked);
191 static void compute_terrain_region_info();
192 static void compute_sound_dist_info();
193 static void clear_BOA();
194 static void compute_costs();
195 static void update_path_info(q_item *node_list[MAX_ROOMS], int start, int end);
196 static void FindPath(int i, int j);
197 static void compute_next_segs();
198 static void compute_blockage_info();
199 static void ComputeBOAVisFaceUpperLeft(room *rp, face *fp, vector *upper_left, float *xdiff, float *ydiff,
200 vector *center);
201 static int BOAGetRoomChecksum(int i);
202 static bool IsPathPointValid(int room, vector *pos);
203 static void ValidateRoomPathPoint(int room, char *message, int len);
204 static void verify_connections();
205 static void find_small_portals();
206 static void compute_robot_path_info();
207
208 bool BOA_PassablePortal(int room, int portal_index, bool f_for_sound, bool f_making_robot_path_invalid_list) {
209 if (room == -1) {
210 return false;
211 }
212
213 room = BOA_INDEX(room);
214
215 if (room > Highest_room_index && room <= Highest_room_index + BOA_num_terrain_regions) {
216 int tr;
217
218 tr = room - Highest_room_index - 1;
219
220 // Inside room/portal
221 int temp_room = BOA_connect[tr][portal_index].roomnum;
222 int temp_portal_index = BOA_connect[tr][portal_index].portal;
223
224 // External room/portal
225 room = Rooms[temp_room].portals[temp_portal_index].croom;
226 portal_index = Rooms[temp_room].portals[temp_portal_index].cportal;
227 }
228
229 ASSERT(room >= 0 && room <= Highest_room_index && Rooms[room].used);
230 face *fp = &Rooms[room].faces[Rooms[room].portals[portal_index].portal_face];
231
232 if (Rooms[room].portals[portal_index].croom < 0)
233 return false;
234
235 if (!BOA_f_making_boa) {
236 if (BOA_cost_array[room][portal_index] < 0.0f && !(room <= Highest_room_index && (Rooms[room].flags & RF_EXTERNAL)))
237 return false;
238
239 if (!f_for_sound) {
240 if (Rooms[room].portals[portal_index].flags & PF_TOO_SMALL_FOR_ROBOT)
241 return false;
242 }
243
244 if ((Rooms[room].portals[portal_index].flags & PF_RENDER_FACES) &&
245 !(Rooms[room].portals[portal_index].flags & PF_RENDERED_FLYTHROUGH) ||
246 (Rooms[room].portals[portal_index].flags & PF_BLOCK)) {
247 return false;
248 }
249 } else {
250 if (f_making_robot_path_invalid_list) {
251 if (Rooms[room].portals[portal_index].flags & PF_TOO_SMALL_FOR_ROBOT)
252 return false;
253 }
254
255 if ((Rooms[room].portals[portal_index].flags & PF_BLOCK) &&
256 !(Rooms[room].portals[portal_index].flags & PF_BLOCK_REMOVABLE))
257 return false;
258
259 if ((Rooms[room].portals[portal_index].flags & PF_RENDER_FACES) &&
260 !(Rooms[room].portals[portal_index].flags & PF_RENDERED_FLYTHROUGH)) {
261 if (!(GameTextures[fp->tmap].flags & (TF_BREAKABLE | TF_FORCEFIELD))) {
262 return false;
263 }
264 }
265 }
266
267 return true;
268 }
269
270 extern object *GetDoorObject(room *rp);
271
272 bool BOA_LockedDoor(object *obj, int roomnum) {
273 if (roomnum >= 0 && roomnum <= Highest_room_index && Rooms[roomnum].used && (Rooms[roomnum].flags & RF_DOOR)) {
274 if (!obj) {
275 return DoorwayLocked(&Rooms[roomnum]) && DoorwayPosition(&Rooms[roomnum]) < 0.5f;
276 } else {
277 object *d_obj = GetDoorObject(&Rooms[roomnum]);
278
279 if (d_obj)
280 return (!DoorwayOpenable(d_obj->handle, obj->handle)) && DoorwayPosition(&Rooms[roomnum]) < 0.5f;
281 else
282 return false;
283 }
284 }
285
286 return false;
287 }
288
289 // bool BOA_IsPathClearForRobot(int start_room, int end_room)
290 //{
291 // start_room = BOA_INDEX(start_room);
292 // end_room = BOA_INDEX(end_room);
293 //
294 // if(BOA_TOO_SMALL_FOR_ROBOT(start_room, end_room))
295 // {
296 // return false;
297 // }
298 //
299 // if(!BOA_HasPossibleBlockage(start_room, end_room))
300 // {
301 // return true;
302 // }
303 //
304 // int last_room;
305 // int next_room = start_room;
306 // ASSERT(next_room >= 0 && next_room <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
307 //
308 // while(BOA_INDEX(next_room) != BOA_INDEX(start_room) && (BOA_INDEX(next_room) != BOA_INDEX(end_room)) &&
309 //(next_room != BOA_NO_PATH))
310 // {
311 // if(BOA_LockedDoor(next_room))
312 // {
313 // return false;
314 // }
315 //
316 // last_room = next_room;
317 // next_room = BOA_GetNextRoom(next_room, end_room);
318 //
319 // if(BOA_DetermineStartRoomPortal(last_room, NULL, next_room, NULL, false, NULL) == -1)
320 // {
321 // return false;
322 // }
323 // }
324 //
325 // return true;
326 //}
327
328 int BOA_DetermineStartRoomPortal(int start_room, vector *start_pos, int end_room, vector *end_pos, bool f_for_sound,
329 bool f_making_robot_path_invalid_list, int *blocked_portal) {
330 int i;
331
332 if (start_room > Highest_room_index && end_room > Highest_room_index)
333 return -1;
334
335 start_room = BOA_INDEX(start_room);
336 end_room = BOA_INDEX(end_room);
337
338 if (start_room <= Highest_room_index) {
339 for (i = 0; i < Rooms[start_room].num_portals; i++) {
340 if (!BOA_PassablePortal(start_room, i, f_for_sound, f_making_robot_path_invalid_list))
341 continue;
342
343 if (end_room <= Highest_room_index) {
344 if (Rooms[start_room].portals[i].croom == end_room)
345 break;
346 } else {
347 if (Rooms[Rooms[start_room].portals[i].croom].flags & RF_EXTERNAL) {
348 int cell = GetTerrainCellFromPos(&Rooms[start_room].portals[i].path_pnt);
349 ASSERT(cell != -1); // DAJ -1FIX
350
351 if (Highest_room_index + TERRAIN_REGION(cell) + 1 == end_room)
352 break;
353 }
354 }
355 }
356
357 if (i >= Rooms[start_room].num_portals)
358 i = -1;
359 } else {
360 for (i = 0; i < BOA_num_connect[start_room - Highest_room_index - 1]; i++) {
361 ASSERT(end_room <= Highest_room_index);
362
363 if (BOA_connect[start_room - Highest_room_index - 1][i].roomnum == end_room) {
364 int next_portal = BOA_connect[start_room - Highest_room_index - 1][i].portal;
365 int external_room = Rooms[end_room].portals[next_portal].croom;
366 int external_portal = Rooms[end_room].portals[next_portal].cportal;
367
368 if (BOA_PassablePortal(external_room, external_portal, f_for_sound, f_making_robot_path_invalid_list)) {
369 break;
370 }
371 }
372 }
373
374 if (i >= BOA_num_connect[start_room - Highest_room_index - 1])
375 i = -1;
376 }
377
378 return i;
379 }
380
381 bool BOA_ComputeMinDist(int start_room, int end_room, float max_check_dist, float *dist, int *num_blockages) {
382 *dist = 0.0f;
383
384 start_room = BOA_INDEX(start_room);
385 end_room = BOA_INDEX(end_room);
386
387 if (start_room == end_room) {
388 return true;
389 }
390
391 if (start_room == Highest_room_index + 1 && end_room > Highest_room_index) {
392 return true;
393 }
394
395 if (end_room == Highest_room_index + 1 && start_room > Highest_room_index) {
396 return true;
397 }
398
399 int cur_room = end_room;
400 int last_room;
401
402 if (start_room < 0 || end_room < 0) {
403 return false;
404 }
405
406 if (start_room > Highest_room_index + BOA_num_terrain_regions ||
407 end_room > Highest_room_index + BOA_num_terrain_regions) {
408 return false;
409 }
410
411 if (start_room <= Highest_room_index && !Rooms[start_room].used)
412 return false;
413
414 if (end_room <= Highest_room_index && !Rooms[end_room].used)
415 return false;
416
417 do {
418 last_room = cur_room;
419
420 if (cur_room <= Highest_room_index && num_blockages && (Rooms[cur_room].flags & RF_DOOR) &&
421 (cur_room != end_room)) {
422 float door_position = DoorwayGetPosition(&Rooms[cur_room]);
423
424 *num_blockages += 1.0f - door_position;
425 }
426
427 cur_room = BOA_NEXT_ROOM(cur_room, start_room);
428 int last_portal;
429
430 if (last_room == cur_room || cur_room == BOA_NO_PATH)
431 return false;
432
433 if (BOA_INDEX(last_room) != BOA_INDEX(cur_room)) {
434 last_portal = BOA_DetermineStartRoomPortal(last_room, NULL, cur_room, NULL);
435 }
436
437 if (last_room == end_room) {
438 int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
439
440 if (cur_room != start_room)
441 *dist += BOA_cost_array[cur_room][this_portal];
442 if (max_check_dist > 0.0 && max_check_dist < *dist)
443 return false;
444 } else if (cur_room == start_room) {
445 *dist += BOA_cost_array[last_room][last_portal];
446 if (max_check_dist > 0.0 && max_check_dist < *dist)
447 return false;
448 } else if ((cur_room != last_room) && (cur_room != BOA_NO_PATH)) {
449 int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
450
451 *dist += BOA_cost_array[last_room][last_portal] + BOA_cost_array[cur_room][this_portal];
452 if (max_check_dist > 0.0f && max_check_dist < *dist)
453 return false;
454 }
455
456 } while ((cur_room != start_room) && (cur_room != last_room) && (cur_room != BOA_NO_PATH));
457
458 if (cur_room == BOA_NO_PATH) {
459 return false;
460 }
461 return true;
462 }
463
464 bool BOA_IsSoundAudible(int start_room, int end_room) {
465 int s_index = start_room;
466 int e_index = end_room;
467
468 if (start_room == -1 || end_room == -1) {
469 return false;
470 }
471
472 if ((!ROOMNUM_OUTSIDE(s_index)) && s_index <= Highest_room_index) {
473 if (!Rooms[s_index].used) {
474 return false;
475 }
476 } else if (ROOMNUM_OUTSIDE(s_index)) {
477 s_index = TERRAIN_REGION(start_room) + Highest_room_index + 1;
478 } else {
479 ASSERT(s_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
480 }
481
482 if ((!ROOMNUM_OUTSIDE(e_index)) && e_index <= Highest_room_index) {
483 if (!Rooms[e_index].used) {
484 return false;
485 }
486 } else if (ROOMNUM_OUTSIDE(e_index)) {
487 e_index = TERRAIN_REGION(end_room) + Highest_room_index + 1;
488 } else {
489 ASSERT(e_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
490 }
491
492 return ((BOA_Array[s_index][e_index] & BOA_SOUND_PROP) != 0);
493 }
494
495 bool BOA_HasPossibleBlockage(int start_room, int end_room) {
496 int s_index = start_room;
497 int e_index = end_room;
498
499 if (start_room == -1 || end_room == -1) {
500 return false;
501 }
502
503 if ((!ROOMNUM_OUTSIDE(s_index)) && s_index <= Highest_room_index) {
504 if (!Rooms[s_index].used) {
505 return false;
506 }
507 } else if (ROOMNUM_OUTSIDE(s_index)) {
508 s_index = TERRAIN_REGION(start_room) + Highest_room_index + 1;
509 } else {
510 ASSERT(s_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
511 }
512
513 if ((!ROOMNUM_OUTSIDE(e_index)) && e_index <= Highest_room_index) {
514 if (!Rooms[e_index].used) {
515 return false;
516 }
517 } else if (ROOMNUM_OUTSIDE(e_index)) {
518 e_index = TERRAIN_REGION(end_room) + Highest_room_index + 1;
519 } else {
520 ASSERT(e_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
521 }
522
523 return ((BOA_Array[s_index][e_index] & BOAF_BLOCKAGE) != 0);
524 }
525 bool BOA_IsVisible(int start_room, int end_room) {
526 int s_index = start_room;
527 int e_index = end_room;
528
529 if (!BOA_vis_valid)
530 return true;
531
532 if (start_room == end_room)
533 return true;
534
535 if (start_room == -1 || end_room == -1) {
536 return false;
537 }
538
539 if ((!ROOMNUM_OUTSIDE(s_index)) && s_index <= Highest_room_index) {
540 if (!Rooms[s_index].used) {
541 return false;
542 }
543 } else if (ROOMNUM_OUTSIDE(s_index)) {
544 s_index = TERRAIN_REGION(start_room) + Highest_room_index + 1;
545 } else {
546 ASSERT(s_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
547 }
548
549 if ((!ROOMNUM_OUTSIDE(e_index)) && e_index <= Highest_room_index) {
550 if (!Rooms[e_index].used) {
551 return false;
552 }
553 } else if (ROOMNUM_OUTSIDE(e_index)) {
554 e_index = TERRAIN_REGION(end_room) + Highest_room_index + 1;
555 } else {
556 ASSERT(e_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
557 }
558
559 return ((BOA_Array[s_index][e_index] & BOAF_VIS) != 0);
560 }
561
562 int BOA_GetNextRoom(int start_room, int end_room) {
563 int s_index = start_room;
564 int e_index = end_room;
565
566 if (start_room == -1 || end_room == -1) {
567 return false;
568 }
569
570 if ((!ROOMNUM_OUTSIDE(s_index)) && s_index <= Highest_room_index) {
571 if (!Rooms[s_index].used) {
572 return false;
573 }
574 } else if (ROOMNUM_OUTSIDE(s_index)) {
575 s_index = TERRAIN_REGION(start_room) + Highest_room_index + 1;
576 } else {
577 ASSERT(s_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
578 }
579
580 if ((!ROOMNUM_OUTSIDE(e_index)) && e_index <= Highest_room_index) {
581 if (!Rooms[e_index].used) {
582 return false;
583 }
584 } else if (ROOMNUM_OUTSIDE(e_index)) {
585 e_index = TERRAIN_REGION(end_room) + Highest_room_index + 1;
586 } else {
587 ASSERT(e_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
588 }
589
590 return ((BOA_Array[s_index][e_index] & BOA_ROOM_MASK));
591 }
592
593 void add_mine_room(int room, int mine, char *checked) {
594 int i;
595
596 Rooms[room].flags |= (mine << 20);
597 checked[room] = 1;
598
599 for (i = 0; i < Rooms[room].num_portals; i++) {
600 if (Rooms[room].portals[i].croom >= 0 && !checked[Rooms[room].portals[i].croom]) {
601 add_mine_room(Rooms[room].portals[i].croom, mine, checked);
602 }
603 }
604 }
605
606 void compute_mine_info() {
607 int i;
608 char checked[MAX_ROOMS];
609 bool done = false;
610 int first_free;
611 int cur_mine = 0;
612
613 for (i = 0; i <= Highest_room_index; i++) {
614 room *rp = &Rooms[i];
615
616 if (rp->used) {
617 rp->flags &= ~RFM_MINE;
618 }
619
620 checked[i] = 0;
621 }
622
623 while (!done) {
624 ASSERT(cur_mine < 32);
625
626 done = true;
627
628 for (i = 0; i <= Highest_room_index; i++) {
629 room *rp = &Rooms[i];
630
631 if (rp->used && !checked[i]) {
632 first_free = i;
633 done = false;
634 break;
635 }
636 }
637
638 if (!done) {
639 add_mine_room(first_free, cur_mine, checked);
640 cur_mine++;
641 }
642 }
643
644 BOA_num_mines = cur_mine;
645 }
646
647 void add_terrain_cell(int cell, int t_region, char *checked) {
648 int depth = 0;
649 int i;
650 uint16_t stack[TERRAIN_WIDTH * TERRAIN_DEPTH];
651 char on_stack[TERRAIN_WIDTH * TERRAIN_DEPTH];
652
653 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
654 on_stack[i] = false;
655
656 stack[depth++] = cell;
657 on_stack[cell] = true;
658
659 while (depth > 0) {
660 cell = stack[--depth];
661 Terrain_seg[cell].flags |= (t_region << 5);
662 checked[cell] = 1;
663
664 int xcounter, ycounter;
665
666 int xstart = cell % TERRAIN_WIDTH - 1;
667 int xend = cell % TERRAIN_WIDTH + 1;
668 int ystart = cell / TERRAIN_WIDTH - 1;
669 int yend = cell / TERRAIN_WIDTH + 1;
670
671 if (xstart < 0)
672 xstart = 0;
673 if (xend >= TERRAIN_WIDTH)
674 xend = TERRAIN_WIDTH - 1;
675 if (ystart < 0)
676 ystart = 0;
677 if (yend >= TERRAIN_DEPTH)
678 yend = TERRAIN_DEPTH - 1;
679
680 // This should be a faster interative why to do a square with center at original position
681 int cur_node = TERRAIN_WIDTH * ystart + xstart;
682 int next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1;
683
684 for (ycounter = ystart; ycounter <= yend; ycounter++) {
685 for (xcounter = xstart; xcounter <= xend; xcounter++) {
686 if (!on_stack[cur_node] && !checked[cur_node] && Terrain_seg[cur_node].y < MAX_TERRAIN_HEIGHT) {
687 stack[depth++] = cur_node;
688 on_stack[cur_node] = true;
689 }
690 cur_node += 1;
691 }
692
693 cur_node += next_y_delta;
694 }
695 }
696 }
697
698 void compute_terrain_region_info() {
699 int i;
700 bool done = false;
701 bool f_warning = false;
702 char checked[TERRAIN_WIDTH * TERRAIN_DEPTH];
703
704 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
705 Terrain_seg[i].flags &= (~TFM_REGION_MASK);
706 ASSERT((Terrain_seg[i].flags & TFM_REGION_MASK) == 0);
707 checked[i] = 0;
708 }
709
710 #ifdef _DEBUG
711 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
712 ASSERT((Terrain_seg[i].flags & TFM_REGION_MASK) == 0);
713 ASSERT(((Terrain_seg[i].flags & TFM_REGION_MASK) >> 5) == 0);
714 }
715 #endif
716
717 // Find saturated points
718 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
719 if (Terrain_seg[i].y > MAX_TERRAIN_HEIGHT - 6.0f) {
720 checked[i] = 1;
721 }
722 }
723
724 char t_region = 1;
725
726 while (!done) {
727 if (t_region >= MAX_BOA_TERRAIN_REGIONS) {
728 #ifdef EDITOR
729 OutrageMessageBox("This terrain has too many regions!\nAI will not work correctly outside!\nSee Chris and-or "
730 "saturate useless areas!");
731 #endif
732 break;
733 }
734
735 int first_free;
736
737 done = true;
738
739 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
740 if (!checked[i]) {
741 first_free = i;
742 done = false;
743 break;
744 }
745 }
746
747 if (!done) {
748 add_terrain_cell(first_free, t_region, checked);
749 t_region++;
750 }
751 }
752
753 BOA_num_terrain_regions = t_region;
754
755 #ifdef _DEBUG
756 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
757 ASSERT(TERRAIN_REGION(i) < BOA_num_terrain_regions);
758 }
759 #endif
760
761 for (i = 0; i < MAX_BOA_TERRAIN_REGIONS; i++) {
762 BOA_num_connect[i] = 0;
763 }
764
765 for (i = 0; i <= Highest_room_index; i++) {
766 if ((Rooms[i].used) && (Rooms[i].flags & RF_EXTERNAL)) {
767 int j;
768
769 for (j = 0; j < Rooms[i].num_portals; j++) {
770 int cell = GetTerrainCellFromPos(&Rooms[i].portals[j].path_pnt);
771 ASSERT(cell != -1); // DAJ -1FIX
772
773 int region = TERRAIN_REGION(cell);
774
775 if (!(BOA_num_connect[region] < MAX_PATH_PORTALS)) {
776 f_warning = true;
777 break;
778 }
779
780 // if(region != 0)
781 // {
782 // BOA_connect[0][BOA_num_connect[region]].roomnum =
783 // Rooms[i].portals[j].croom; BOA_connect[0][BOA_num_connect[region]].portal =
784 // Rooms[i].portals[j].cportal;
785 // }
786
787 if (!(Rooms[Rooms[i].portals[j].croom].flags & RF_EXTERNAL)) {
788 if (BOA_PassablePortal(i, j)) {
789 BOA_connect[region][BOA_num_connect[region]].roomnum = Rooms[i].portals[j].croom;
790 BOA_connect[region][BOA_num_connect[region]].portal = Rooms[i].portals[j].cportal;
791 BOA_num_connect[region]++;
792 }
793 }
794 }
795 }
796 }
797
798 #ifdef EDITOR
799 if (f_warning) {
800 OutrageMessageBox(
801 "This terrain has too many fly through\nterrain-mine connections!\n\nAI will not work correctly outside!\nIf "
802 "you really cannot fly outside\nignore this message.\n\nSee Chris for specific instructions!");
803 }
804 #endif
805 }
806
807 #define MAX_SOUND_PROP_DIST 400.0f
808
809 void compute_sound_dist_info() {
810 int i;
811 int j;
812
813 for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
814 for (j = 0; j <= i; j++) {
815 BOA_Array[i][j] |= BOA_SOUND_PROP;
816 BOA_Array[j][i] |= BOA_SOUND_PROP;
817
818 if ((i > Highest_room_index || j > Highest_room_index) && (i != j)) {
819 BOA_Array[i][j] &= ~BOA_SOUND_PROP;
820 BOA_Array[j][i] &= ~BOA_SOUND_PROP;
821 }
822 }
823 }
824
825 for (i = 0; i <= Highest_room_index; i++) {
826 for (j = 0; j < i; j++) {
827 float dist;
828 bool f_ok = BOA_ComputeMinDist(i, j, MAX_SOUND_PROP_DIST, &dist);
829
830 if (!f_ok || dist > MAX_SOUND_PROP_DIST) {
831 BOA_Array[i][j] &= ~BOA_SOUND_PROP;
832 BOA_Array[j][i] &= ~BOA_SOUND_PROP;
833 }
834 }
835 }
836
837 for (i = 0; i < BOA_num_terrain_regions; i++) {
838 int j;
839 int k;
840
841 for (j = 0; j < BOA_num_connect[i]; j++) {
842 int croom = BOA_connect[i][j].roomnum;
843
844 for (k = 0; k <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; k++) {
845 if (BOA_Array[croom][k] & BOA_SOUND_PROP) {
846 BOA_Array[Highest_room_index + i + 1][k] |= BOA_SOUND_PROP;
847 BOA_Array[k][Highest_room_index + i + 1] |= BOA_SOUND_PROP;
848 }
849 }
850 }
851 }
852 }
853
854 void clear_BOA() {
855 int i, j;
856
857 for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
858 for (j = 0; j <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; j++) {
859 BOA_Array[i][j] = i; // No flags are set and i to j points to i (so, no path exists)
860 }
861 }
862
863 for (i = 0; i < MAX_ROOMS; i++) {
864 BOA_AABB_ROOM_checksum[i] = 0;
865 }
866
867 BOA_num_mines = 0;
868 BOA_num_terrain_regions = 0;
869 }
870
871 void compute_costs() {
872 int i, j;
873 vector from_pnt; //, to_pnt;
874 vector portal_pnt;
875
876 for (i = 0; i <= Highest_room_index; i++) {
877
878 if (Rooms[i].used) {
879 ASSERT(Rooms[i].num_portals <= MAX_PATH_PORTALS);
880
881 for (j = 0; j < Rooms[i].num_portals; j++) {
882 if (BOA_PassablePortal(i, j)) {
883 ComputeRoomCenter(&from_pnt, &Rooms[i]);
884 // ComputeRoomCenter(&to_pnt, &Rooms[Rooms[i].portals[j].croom]);
885
886 ComputePortalCenter(&portal_pnt, &Rooms[i], j);
887
888 BOA_cost_array[i][j] = vm_VectorDistance(&from_pnt, &portal_pnt);
889 } else {
890 BOA_cost_array[i][j] = -1.0;
891 }
892 }
893 } else {
894 for (j = 0; j < Rooms[i].num_portals; j++) {
895 BOA_cost_array[i][j] = -1.0;
896 }
897 }
898 }
899
900 for (i = Highest_room_index + 1; i <= Highest_room_index + BOA_num_terrain_regions; i++) {
901 int j;
902 for (j = 0; j < BOA_num_connect[i]; j++) {
903 BOA_cost_array[i][j] = 100000.0f;
904 }
905 }
906 }
907
908 void update_path_info(q_item *node_list[MAX_ROOMS], int start, int end) {
909 int cur_room;
910 int par_room;
911
912 while (end != start) {
913 cur_room = end;
914 par_room = node_list[end]->parent;
915
916 while (par_room != -1) {
917 BOA_Array[par_room][end] = cur_room;
918
919 cur_room = node_list[cur_room]->parent;
920 par_room = node_list[cur_room]->parent;
921 }
922
923 end = node_list[end]->parent;
924 }
925 }
926
927 void FindPath(int i, int j) {
928 pq PQPath;
929 int counter;
930 q_item *start_node = new q_item(BOA_INDEX(i), -1, 0.0);
931 q_item *cur_node;
932
933 q_item *node_list[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
934
935 // mprintf(0, "Find path for %d to %d\n", i, j);
936
937 if (i == -1 || j == -1) {
938 delete start_node;
939 return;
940 }
941
942 memset(node_list, 0, sizeof(q_item *) * (MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS));
943
944 PQPath.push(start_node);
945 ASSERT(start_node->roomnum <= Highest_room_index + BOA_num_terrain_regions);
946
947 while ((cur_node = PQPath.pop())) {
948 node_list[BOA_INDEX(cur_node->roomnum)] = cur_node;
949 ASSERT(BOA_INDEX(cur_node->roomnum) >= 0 &&
950 BOA_INDEX(cur_node->roomnum) <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
951
952 if (cur_node->roomnum == j) {
953 update_path_info(node_list, i, j);
954 goto done;
955 }
956
957 if (i > j) {
958 int num_portals;
959 bool f_room = true;
960 int t_index;
961
962 if (cur_node->roomnum <= Highest_room_index) {
963 num_portals = Rooms[cur_node->roomnum].num_portals;
964 } else {
965 t_index = cur_node->roomnum - Highest_room_index - 1;
966 num_portals = BOA_num_connect[t_index];
967 f_room = false;
968 }
969
970 for (counter = 0; counter < num_portals; counter++) {
971 int next_room;
972 q_item *list_item;
973 float new_cost;
974
975 if (!BOA_PassablePortal(cur_node->roomnum, counter))
976 continue;
977
978 if (f_room)
979 next_room = Rooms[cur_node->roomnum].portals[counter].croom;
980 else
981 next_room = BOA_connect[t_index][counter].roomnum;
982
983 if (next_room < 0 || next_room == BOA_NO_PATH)
984 continue;
985
986 if ((next_room <= Highest_room_index) && (Rooms[next_room].flags & RF_EXTERNAL)) {
987 ASSERT(cur_node->roomnum <= Highest_room_index);
988
989 int cell = GetTerrainCellFromPos(&Rooms[cur_node->roomnum].portals[counter].path_pnt);
990 ASSERT(cell >= 0 && cell < TERRAIN_WIDTH * TERRAIN_DEPTH);
991
992 next_room = Highest_room_index + TERRAIN_REGION(cell) + 1;
993 ASSERT(next_room <= Highest_room_index + BOA_num_terrain_regions);
994 }
995
996 int next_portal;
997 if (BOA_INDEX(next_room) != BOA_INDEX(cur_node->roomnum)) {
998 next_portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_node->roomnum, NULL);
999 }
1000
1001 new_cost = cur_node->cost + BOA_cost_array[BOA_INDEX(cur_node->roomnum)][counter] +
1002 BOA_cost_array[BOA_INDEX(next_room)][next_portal];
1003
1004 list_item = node_list[BOA_INDEX(next_room)];
1005 if (list_item != NULL && list_item->cost <= new_cost)
1006 continue;
1007
1008 if (list_item == NULL) {
1009 list_item = new q_item(BOA_INDEX(next_room), cur_node->roomnum, new_cost);
1010 node_list[BOA_INDEX(next_room)] = list_item;
1011 PQPath.push(list_item);
1012 ASSERT(list_item->roomnum <= Highest_room_index + BOA_num_terrain_regions);
1013 } else {
1014 list_item->cost = new_cost;
1015 list_item->parent = cur_node->roomnum;
1016 }
1017 }
1018 } else {
1019 int num_portals;
1020 bool f_room = true;
1021 int t_index;
1022
1023 if (cur_node->roomnum <= Highest_room_index) {
1024 num_portals = Rooms[cur_node->roomnum].num_portals;
1025 } else {
1026 t_index = cur_node->roomnum - Highest_room_index - 1;
1027 num_portals = BOA_num_connect[t_index];
1028 f_room = false;
1029 }
1030
1031 for (counter = 0; counter < num_portals; counter++) {
1032 int next_room;
1033 q_item *list_item;
1034 float new_cost;
1035
1036 if (!BOA_PassablePortal(cur_node->roomnum, counter))
1037 continue;
1038
1039 if (f_room)
1040 next_room = Rooms[cur_node->roomnum].portals[counter].croom;
1041 else
1042 next_room = BOA_connect[t_index][counter].roomnum;
1043
1044 if (next_room < 0 || next_room == BOA_NO_PATH)
1045 continue;
1046
1047 if ((next_room <= Highest_room_index) && (Rooms[next_room].flags & RF_EXTERNAL)) {
1048 ASSERT(cur_node->roomnum <= Highest_room_index);
1049 int cell = GetTerrainCellFromPos(&Rooms[cur_node->roomnum].portals[counter].path_pnt);
1050 ASSERT(cell != -1); // DAJ -1FIX
1051 next_room = Highest_room_index + TERRAIN_REGION(cell) + 1;
1052 }
1053
1054 int next_portal;
1055 if (BOA_INDEX(next_room) != BOA_INDEX(cur_node->roomnum)) {
1056 next_portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_node->roomnum, NULL);
1057 }
1058
1059 new_cost = cur_node->cost + BOA_cost_array[BOA_INDEX(cur_node->roomnum)][counter] +
1060 BOA_cost_array[BOA_INDEX(next_room)][next_portal];
1061
1062 list_item = node_list[BOA_INDEX(next_room)];
1063 if (list_item != NULL && list_item->cost <= new_cost)
1064 continue;
1065
1066 if (list_item == NULL) {
1067 list_item = new q_item(BOA_INDEX(next_room), cur_node->roomnum, new_cost);
1068 node_list[BOA_INDEX(next_room)] = list_item;
1069 PQPath.push(list_item);
1070 ASSERT(list_item->roomnum <= Highest_room_index + BOA_num_terrain_regions);
1071 } else {
1072 list_item->cost = new_cost;
1073 list_item->parent = cur_node->roomnum;
1074 }
1075 }
1076 }
1077 }
1078
1079 // Mark as an impossible path.
1080 BOA_Array[i][j] = BOA_NO_PATH;
1081
1082 // mprintf(0, "Found an impossible path\n");
1083
1084 done:
1085 for (counter = 0; counter <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; counter++) {
1086 if (node_list[counter])
1087 delete node_list[counter];
1088 }
1089
1090 return;
1091 }
1092
1093 void compute_next_segs() {
1094 int i, j;
1095
1096 for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
1097 if (i <= Highest_room_index && (!Rooms[i].used))
1098 continue;
1099
1100 if (i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
1101 continue;
1102
1103 if (i > Highest_room_index + BOA_num_terrain_regions)
1104 continue;
1105
1106 for (j = Highest_room_index + MAX_BOA_TERRAIN_REGIONS; j >= 0; j--) {
1107 if (j <= Highest_room_index && (!Rooms[j].used))
1108 continue;
1109
1110 if (j <= Highest_room_index && (Rooms[j].flags & RF_EXTERNAL))
1111 continue;
1112
1113 if (j > Highest_room_index + BOA_num_terrain_regions)
1114 continue;
1115
1116 if (i == Highest_room_index + 1 && j > Highest_room_index) {
1117 BOA_Array[i][j] = j;
1118 BOA_Array[j][i] = i;
1119 continue;
1120 }
1121
1122 if (j == Highest_room_index + 1 && i > Highest_room_index) {
1123 BOA_Array[i][j] = j;
1124 BOA_Array[j][i] = i;
1125 continue;
1126 }
1127
1128 if (i != j && BOA_Array[i][j] == i)
1129 FindPath(i, j);
1130 if (i != j && BOA_Array[j][i] == j)
1131 FindPath(j, i);
1132 }
1133 }
1134 }
1135
1136 void compute_blockage_info() {
1137 int i, j;
1138
1139 for (i = 0; i <= Highest_room_index + BOA_num_terrain_regions; i++) {
1140 if (i <= Highest_room_index && (!Rooms[i].used))
1141 continue;
1142
1143 if (i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
1144 continue;
1145
1146 if (i > Highest_room_index + BOA_num_terrain_regions)
1147 continue;
1148
1149 for (j = 0; j <= Highest_room_index + BOA_num_terrain_regions; j++) {
1150 int cur_room = i;
1151
1152 if (i == j)
1153 continue;
1154
1155 if (i == Highest_room_index + 1 && j > Highest_room_index)
1156 continue;
1157
1158 if (j == Highest_room_index + 1 && i > Highest_room_index)
1159 continue;
1160
1161 if (j <= Highest_room_index && (!Rooms[i].used))
1162 continue;
1163
1164 if (j <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
1165 continue;
1166
1167 if (BOA_NEXT_ROOM(cur_room, j) != BOA_NO_PATH && BOA_NEXT_ROOM(cur_room, j) != cur_room) {
1168 int last_room = cur_room;
1169
1170 do {
1171 if (cur_room <= Highest_room_index && (Rooms[cur_room].flags & RF_DOOR)) {
1172 BOA_Array[i][j] |= BOAF_BLOCKAGE;
1173 break;
1174 }
1175
1176 last_room = cur_room;
1177 cur_room = BOA_NEXT_ROOM(cur_room, j);
1178
1179 if (last_room != cur_room) {
1180 BOA_f_making_boa = false;
1181 if (BOA_DetermineStartRoomPortal(last_room, NULL, cur_room, NULL, true) == -1) {
1182 BOA_Array[i][j] |= BOAF_BLOCKAGE;
1183 BOA_f_making_boa = true;
1184 break;
1185 }
1186 BOA_f_making_boa = true;
1187 }
1188
1189 } while (cur_room != j);
1190 }
1191 }
1192 }
1193 }
1194
1195 // Goes through all the valid points in the indoor engine and returns a unique
1196 // checksum
1197
1198 #pragma optimize("", off)
1199
1200 int BOAGetMineChecksum() {
1201 int i, t, k;
1202 int total = 0;
1203
1204 for (i = 0; i <= Highest_room_index; i++) {
1205 room *rp = &Rooms[i];
1206
1207 if (!Rooms[i].used)
1208 continue;
1209
1210 for (t = 0; t < rp->num_faces; t++) {
1211 face *fp = &rp->faces[t];
1212
1213 for (k = 0; k < fp->num_verts; k++) {
1214 int x, y, z;
1215 x = floor(rp->verts[fp->face_verts[k]].x);
1216 y = floor(rp->verts[fp->face_verts[k]].y);
1217 z = floor(rp->verts[fp->face_verts[k]].z);
1218 total += x + y + z;
1219 }
1220
1221 total += fp->num_verts << 4;
1222
1223 if (fp->portal_num != -1) {
1224 // factor in portal
1225 portal *pp = &rp->portals[fp->portal_num];
1226
1227 total += fp->portal_num;
1228
1229 int flags = pp->flags;
1230
1231 flags &= (PF_BLOCK | PF_BLOCK_REMOVABLE | PF_RENDERED_FLYTHROUGH);
1232
1233 total += flags;
1234 }
1235 }
1236 total += rp->num_faces << 8;
1237
1238 total += (rp->num_portals) << (i % 16);
1239
1240 total += i;
1241 }
1242
1243 // Now do terrain
1244 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
1245 total += Terrain_seg[i].ypos;
1246
1247 total += BOA_VERSION << 24;
1248
1249 mprintf(0, "Computed Checksum is %d\n", (int)total);
1250 return total;
1251 }
1252
1253 int BOAGetRoomChecksum(int i) {
1254 int t, k;
1255 float total = 0;
1256
1257 {
1258 room *rp = &Rooms[i];
1259
1260 if (!Rooms[i].used)
1261 return 0;
1262
1263 for (t = 0; t < rp->num_faces; t++) {
1264 face *fp = &rp->faces[t];
1265
1266 for (k = 0; k < fp->num_verts; k++) {
1267 total += rp->verts[fp->face_verts[k]].x;
1268 total += rp->verts[fp->face_verts[k]].y;
1269 total += rp->verts[fp->face_verts[k]].z;
1270 }
1271
1272 total += fp->num_verts << 4;
1273 }
1274 total += rp->num_faces << 8;
1275
1276 total += (rp->num_portals) << (i % 16);
1277
1278 total += i;
1279 }
1280
1281 total += BOA_VERSION << 24;
1282
1283 return (int)total;
1284 }
1285
1286 #pragma optimize("", on)
1287
1288 bool IsPathPointValid(int room, vector *pos) {
1289 // vector c_pnt = Rooms[room].path_pnt;
1290 int i;
1291
1292 if (Rooms[room].flags & RF_EXTERNAL)
1293 return true;
1294
1295 for (i = 0; i < Rooms[room].num_portals; i++) {
1296 fvi_info hit_info;
1297 fvi_query fq;
1298 int fate;
1299
1300 if (!BOA_PassablePortal(room, i))
1301 continue;
1302
1303 vector portal_pnt = Rooms[room].portals[i].path_pnt;
1304
1305 // shoot a ray from the light position to the current vertex
1306 fq.p0 = &portal_pnt;
1307 fq.p1 = pos;
1308 fq.startroom = room;
1309
1310 fq.rad = 5.0f;
1311 fq.flags =
1312 FQ_SOLID_PORTALS | FQ_NO_RELINK; // chrishack -- Might want to make FQ_IGNORE_MOVING_OBJECTS into a passed arg
1313 fq.thisobjnum = -1;
1314 fq.ignore_obj_list = NULL;
1315
1316 fate = fvi_FindIntersection(&fq, &hit_info);
1317
1318 if (fate != HIT_NONE) {
1319 return false;
1320 }
1321 }
1322
1323 return true;
1324 }
1325
1326 #define MAX_SUBDIVISIONS 12 // Actually 19 (we don't do extremities)
1327
1328 void ValidateRoomPathPoint(int room, char *message, int len) {
1329 vector pos;
1330 int i, j, k;
1331
1332 pos = Rooms[room].path_pnt;
1333
1334 if (IsPathPointValid(room, &pos))
1335 return;
1336
1337 bool f_found = false;
1338 // vector best_pnt;
1339 // float best_distance;
1340
1341 vector diff = (Rooms[room].max_xyz - Rooms[room].min_xyz) / MAX_SUBDIVISIONS;
1342
1343 for (i = 1; i < MAX_SUBDIVISIONS - 1; i++) {
1344 for (j = 1; j < MAX_SUBDIVISIONS - 1; j++) {
1345 for (k = 1; k < MAX_SUBDIVISIONS - 1; k++) {
1346 vector t_pnt = Rooms[room].min_xyz;
1347 t_pnt.x += diff.x * i;
1348 t_pnt.y += diff.y * j;
1349 t_pnt.z += diff.z * k;
1350
1351 if (IsPathPointValid(room, &t_pnt)) {
1352 Rooms[room].path_pnt = t_pnt;
1353 return;
1354 }
1355 }
1356 }
1357 }
1358
1359 if (!message)
1360 mprintf(0, "Room %d has a bad center point\n", room);
1361 else {
1362 char new_message[300];
1363 snprintf(new_message, sizeof(new_message), "Room %d has a bad center point\n", room);
1364
1365 if (strlen(message) + strlen(new_message) < (uint32_t)len) {
1366 strcat(message, new_message);
1367 }
1368 }
1369 }
1370
1371 void BOA_ComputePathPoints(char *message, int len) {
1372 int i;
1373 int j;
1374
1375 for (i = 0; i <= Highest_room_index; i++) {
1376 if (Rooms[i].used) {
1377 for (j = 0; j < Rooms[i].num_portals; j++) {
1378 ComputePortalCenter(&Rooms[i].portals[j].path_pnt, &Rooms[i], j);
1379 }
1380
1381 if (!(Rooms[i].flags & RF_MANUAL_PATH_PNT)) {
1382 Rooms[i].path_pnt = (Rooms[i].max_xyz + Rooms[i].min_xyz) / 2.0f;
1383 ValidateRoomPathPoint(i, message, len);
1384 }
1385 }
1386 }
1387 }
1388
1389 // Given a face, computes the upper left corner of the face
1390 void ComputeBOAVisFaceUpperLeft(room *rp, face *fp, vector *upper_left, float *xdiff, float *ydiff, vector *center) {
1391 matrix face_matrix, trans_matrix;
1392 vector fvec;
1393 vector avg_vert;
1394 vector verts[MAX_VERTS_PER_FACE];
1395 vector rot_vert;
1396 int i;
1397
1398 // find the center point of this face
1399 vm_MakeZero(&avg_vert);
1400 for (i = 0; i < fp->num_verts; i++)
1401 avg_vert += rp->verts[fp->face_verts[i]];
1402
1403 avg_vert /= fp->num_verts;
1404
1405 // Make the orientation matrix
1406 // Reverse the normal because we're looking "at" the face, not from it
1407 fvec = -fp->normal;
1408
1409 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
1410 // Make the transformation matrix
1411
1412 angvec avec;
1413 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
1414 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
1415
1416 // Rotate all the points
1417 for (i = 0; i < fp->num_verts; i++) {
1418 vector vert = rp->verts[fp->face_verts[i]];
1419
1420 vert -= avg_vert;
1421 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
1422
1423 verts[i] = rot_vert;
1424 }
1425
1426 // Find left most point
1427 int leftmost_point = -1;
1428 float leftmost_x = 900000.00f; // a big number
1429
1430 for (i = 0; i < fp->num_verts; i++) {
1431 if (verts[i].x < leftmost_x) {
1432 leftmost_point = i;
1433 leftmost_x = verts[i].x;
1434 }
1435 }
1436
1437 ASSERT(leftmost_point != -1);
1438
1439 // Find top most point
1440 int topmost_point = -1;
1441 float topmost_y = -900000.0f; // a big number
1442
1443 for (i = 0; i < fp->num_verts; i++) {
1444 if (verts[i].y > topmost_y) {
1445 topmost_point = i;
1446 topmost_y = verts[i].y;
1447 }
1448 }
1449
1450 ASSERT(topmost_point != -1);
1451
1452 // Find right most point
1453 int rightmost_point = -1;
1454 float rightmost_x = -900000.00f; // a big number
1455
1456 for (i = 0; i < fp->num_verts; i++) {
1457 if (verts[i].x > rightmost_x) {
1458 rightmost_point = i;
1459 rightmost_x = verts[i].x;
1460 }
1461 }
1462
1463 ASSERT(rightmost_point != -1);
1464
1465 // Find bottom most point
1466 int bottommost_point = -1;
1467 float bottommost_y = 900000.0f; // a big number
1468
1469 for (i = 0; i < fp->num_verts; i++) {
1470 if (verts[i].y < bottommost_y) {
1471 bottommost_point = i;
1472 bottommost_y = verts[i].y;
1473 }
1474 }
1475
1476 ASSERT(bottommost_point != -1);
1477
1478 // now set the base vertex, which is where we base uv 0,0 on
1479
1480 vector base_vector;
1481
1482 base_vector.x = verts[leftmost_point].x;
1483 base_vector.y = verts[topmost_point].y;
1484 base_vector.z = 0;
1485
1486 // Figure out grid resolution
1487 if (xdiff)
1488 *xdiff = verts[rightmost_point].x - verts[leftmost_point].x;
1489
1490 if (ydiff)
1491 *ydiff = verts[topmost_point].y - verts[bottommost_point].y;
1492
1493 // Find upper left corner
1494 vm_TransposeMatrix(&trans_matrix);
1495 vm_MatrixMulVector(&rot_vert, &base_vector, &trans_matrix);
1496
1497 if (upper_left)
1498 *upper_left = rot_vert + avg_vert;
1499
1500 if (center)
1501 *center = avg_vert;
1502 }
1503
1504 #if (defined(EDITOR) || defined(NEWEDITOR))
1505
1506 #ifdef NEWEDITOR
1507 #include "neweditor/stdafx.h"
1508 #include "neweditor/resource.h"
1509 #include "neweditor/ProgressDialog.h"
1510
1511 // Displays/Updates the Visability Progress bar
1512 // state = 0 //create
1513 // state = 1 //update
1514 // state = 2 //destroy
1515 void DoBOAVisProgressDialog(float percentage, int state, char *message = NULL) {
1516 int pos = percentage * 100.0f;
1517 if (pos < 0)
1518 pos = 0;
1519 if (pos > 100)
1520 pos = 100;
1521
1522 static CProgressDialog *gProgressDialog = NULL;
1523 switch (state) {
1524 case 0: {
1525 gProgressDialog = new CProgressDialog;
1526
1527 gProgressDialog->Create(IDD_LOADLEVELPROGRESS, NULL);
1528 gProgressDialog->m_TitleText = message;
1529 gProgressDialog->ShowWindow(SW_SHOW);
1530
1531 gProgressDialog->UpdateData(false);
1532 gProgressDialog->m_ProgressBar.SetStep(100);
1533 gProgressDialog->m_ProgressBar.SetPos(pos);
1534
1535 } break;
1536
1537 case 1: {
1538 if (gProgressDialog && gProgressDialog->m_hWnd) {
1539 gProgressDialog->m_ProgressBar.SetPos(pos);
1540 defer();
1541 }
1542 } break;
1543
1544 case 2: {
1545 if (gProgressDialog && gProgressDialog->m_hWnd) {
1546 gProgressDialog->DestroyWindow();
1547 delete gProgressDialog;
1548 gProgressDialog = NULL;
1549 }
1550
1551 } break;
1552 }
1553 }
1554 #endif
1555
1556 // Higher res means longer vis times :(
1557 #define VIS_TABLE_RESOLUTION 6
1558 // Goes through all the rooms and determines their visibility in relation to one another
1559 void MakeBOAVisTable(bool from_lighting) {
1560 int cur_check = BOAGetMineChecksum();
1561 int vis_stack[MAX_ROOMS * 10];
1562 int stack_count = 0;
1563 uint8_t already_checked[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
1564 uint8_t precomputed[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS][MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
1565
1566 // Removed this as we have to make a better checksum
1567
1568 if (cur_check == BOA_vis_checksum) {
1569 // Already done, so bail
1570 if (!from_lighting)
1571 EditorMessageBox("BOA vis table already computed.");
1572 return;
1573 }
1574
1575 BOA_vis_checksum = BOA_mine_checksum = 0;
1576 #ifdef NEWEDITOR
1577 DoBOAVisProgressDialog(0.0f, 0, "Computing BOA");
1578 DoBOAVisProgressDialog(0.0f, 1);
1579 #endif
1580 MakeBOA();
1581 #ifdef NEWEDITOR
1582 DoBOAVisProgressDialog(100.0f, 2);
1583 #endif
1584
1585 // Now compute all room to room visibility stuff
1586 int i, t, j;
1587
1588 mprintf(0, "Computing visibility for %d rooms.\n", Highest_room_index);
1589
1590 for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
1591 for (t = 0; t <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; t++) {
1592 BOA_Array[i][t] &= ~BOAF_VIS;
1593 precomputed[i][t] = 255;
1594 }
1595
1596 BOA_Array[i][i] |= BOAF_VIS;
1597 }
1598
1599 #ifdef NEWEDITOR
1600 DoBOAVisProgressDialog(0.0f, 0, "Computing BOA Vis. Table");
1601 #endif
1602
1603 for (i = 0; i <= Highest_room_index; i++) {
1604 if (Rooms[i].used == 0)
1605 continue;
1606
1607 room *rp = &Rooms[i];
1608
1609 #ifdef NEWEDITOR
1610 DoBOAVisProgressDialog((float)(i + 1) / (float)(Highest_room_index + 1), 1);
1611 #endif
1612 mprintf_at(2, 4, 0, "Room=%d ", i);
1613
1614 if (rp->flags & RF_EXTERNAL)
1615 continue;
1616
1617 // Clear the already_checked array
1618 memset(already_checked, 0, MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS);
1619
1620 already_checked[i] = 1; // don't check self
1621
1622 // Make all the rooms connecting to this room automatically visible
1623 // Make all the rooms connecting to this room automatically visible
1624 for (t = 0; t < rp->num_portals; t++) {
1625 int croom = rp->portals[t].croom;
1626
1627 if (croom >= 0) {
1628 BOA_Array[i][croom] |= BOAF_VIS;
1629 already_checked[croom] = 1;
1630
1631 // If this is an external room, mark the terrain as visible
1632 if (Rooms[croom].flags & RF_EXTERNAL) {
1633 int cp;
1634 int xxx;
1635
1636 BOA_Array[i][Highest_room_index + 1] |= BOAF_VIS;
1637
1638 for (xxx = 0; xxx < Rooms[croom].num_portals; xxx++) {
1639 int cell = GetTerrainCellFromPos(&Rooms[croom].portals[xxx].path_pnt);
1640 ASSERT(cell != -1); // DAJ -1FIX
1641 int region = TERRAIN_REGION(cell);
1642
1643 BOA_Array[i][Highest_room_index + region + 1] |= BOAF_VIS;
1644 }
1645
1646 for (cp = 0; cp <= Highest_room_index; cp++) {
1647 if (Rooms[cp].used && (Rooms[cp].flags & RF_EXTERNAL) && !already_checked[cp]) {
1648 vis_stack[stack_count] = cp;
1649 stack_count++;
1650 ASSERT(stack_count < MAX_ROOMS * 10);
1651 }
1652 }
1653 }
1654 }
1655 }
1656
1657 // Now push all of the connecting rooms portals onto the stack
1658 for (t = 0; t < rp->num_portals; t++) {
1659 int roomnum = rp->portals[t].croom;
1660 for (j = 0; j < Rooms[roomnum].num_portals; j++) {
1661 int croom = Rooms[roomnum].portals[j].croom;
1662
1663 if (croom >= 0 && !already_checked[croom]) {
1664 vis_stack[stack_count] = croom;
1665 stack_count++;
1666 ASSERT(stack_count < MAX_ROOMS * 10);
1667 }
1668 }
1669 }
1670
1671 // Check visibility until we are done with this room
1672 while (stack_count > 0) {
1673 int check_room = vis_stack[--stack_count];
1674 if (already_checked[check_room])
1675 continue;
1676
1677 already_checked[check_room] = 1;
1678
1679 if (precomputed[check_room][i] != 255) {
1680 precomputed[i][check_room] = precomputed[check_room][i];
1681 if (precomputed[i][check_room] == 1) {
1682 BOA_Array[i][check_room] |= BOAF_VIS;
1683
1684 // if this portal can be seen, add all its portals to the stack
1685 // and then set this room to be visible
1686 for (int tk = 0; tk < Rooms[check_room].num_portals; tk++) {
1687 int croom = Rooms[check_room].portals[tk].croom;
1688 if (croom >= 0) {
1689 vis_stack[stack_count] = croom;
1690 stack_count++;
1691 ASSERT(stack_count < MAX_ROOMS * 10);
1692 }
1693 }
1694 }
1695
1696 continue;
1697 }
1698
1699 int done = 0;
1700
1701 for (t = 0; t < rp->num_portals && !done; t++) {
1702 face *src_fp = &rp->faces[rp->portals[t].portal_face];
1703 float src_width, src_height;
1704 vector src_upper_left, src_center;
1705 matrix src_matrix;
1706 vector src_verts[MAX_VERTS_PER_FACE], *src_vertp[MAX_VERTS_PER_FACE];
1707
1708 for (j = 0; j < src_fp->num_verts; j++) {
1709 src_verts[j] = rp->verts[src_fp->face_verts[j]];
1710 src_vertp[j] = &src_verts[j];
1711 }
1712
1713 vector fvec = -src_fp->normal;
1714 vm_VectorToMatrix(&src_matrix, &fvec, NULL, NULL);
1715
1716 ComputeBOAVisFaceUpperLeft(rp, src_fp, &src_upper_left, &src_width, &src_height, &src_center);
1717
1718 if (src_width > VIS_TABLE_RESOLUTION) {
1719 float num = src_width / VIS_TABLE_RESOLUTION;
1720 src_width = VIS_TABLE_RESOLUTION;
1721
1722 src_upper_left += (src_matrix.rvec * (num / 2));
1723 src_matrix.rvec *= num;
1724 }
1725
1726 if (src_height > VIS_TABLE_RESOLUTION) {
1727 float num = src_height / VIS_TABLE_RESOLUTION;
1728 src_height = VIS_TABLE_RESOLUTION;
1729
1730 src_upper_left -= (src_matrix.uvec * (num / 2));
1731 src_matrix.uvec *= num;
1732 }
1733
1734 for (j = 0; j < Rooms[check_room].num_portals && !done; j++) {
1735 face *dest_fp = &Rooms[check_room].faces[Rooms[check_room].portals[j].portal_face];
1736 float dest_width, dest_height;
1737 vector dest_upper_left, dest_center;
1738 matrix dest_matrix;
1739 vector dest_verts[MAX_VERTS_PER_FACE], *dest_vertp[MAX_VERTS_PER_FACE];
1740
1741 for (int tj = 0; tj < dest_fp->num_verts; tj++) {
1742 dest_verts[tj] = Rooms[check_room].verts[dest_fp->face_verts[tj]];
1743 dest_vertp[tj] = &dest_verts[tj];
1744 }
1745
1746 fvec = -dest_fp->normal;
1747 vm_VectorToMatrix(&dest_matrix, &fvec, NULL, NULL);
1748
1749 ComputeBOAVisFaceUpperLeft(&Rooms[check_room], dest_fp, &dest_upper_left, &dest_width, &dest_height,
1750 &dest_center);
1751
1752 if (dest_width > VIS_TABLE_RESOLUTION) {
1753 float num = dest_width / VIS_TABLE_RESOLUTION;
1754 dest_width = VIS_TABLE_RESOLUTION;
1755
1756 dest_upper_left += (dest_matrix.rvec * (num / 2));
1757 dest_matrix.rvec *= num;
1758 }
1759
1760 if (dest_height > VIS_TABLE_RESOLUTION) {
1761 float num = dest_height / VIS_TABLE_RESOLUTION;
1762 dest_height = VIS_TABLE_RESOLUTION;
1763
1764 dest_upper_left -= (dest_matrix.uvec * (num / 2));
1765 dest_matrix.uvec *= num;
1766 }
1767
1768 vector src_vector, src_ybase;
1769 src_ybase = src_upper_left;
1770
1771 for (int sy = 0; sy < src_height && !done; sy++, src_ybase -= src_matrix.uvec) {
1772 src_vector = src_ybase;
1773
1774 for (int sx = 0; sx < src_width && !done; sx++, src_vector += src_matrix.rvec) {
1775 vector src2 = src_vector;
1776 vector subvec;
1777
1778 if ((check_point_to_face(&src2, &src_fp->normal, src_fp->num_verts, src_vertp)))
1779 continue;
1780
1781 vector dest_vector, dest_ybase;
1782 dest_ybase = dest_upper_left;
1783
1784 for (int dy = 0; dy < dest_height && !done; dy++, dest_ybase -= dest_matrix.uvec) {
1785 dest_vector = dest_ybase;
1786
1787 for (int dx = 0; dx < dest_width && !done; dx++, dest_vector += dest_matrix.rvec) {
1788 src2 += 0.1f * rp->faces[rp->portals[t].portal_face].normal;
1789 dest_vector += 0.1f * Rooms[check_room].faces[Rooms[check_room].portals[j].portal_face].normal;
1790
1791 subvec = dest_vector - src2;
1792 if (subvec * src_fp->normal > 0.0f)
1793 continue;
1794 /*subvec=src2-dest_vector;
1795 if (subvec * dest_fp->normal>0)
1796 continue;*/
1797
1798 if ((check_point_to_face(&dest_vector, &dest_fp->normal, dest_fp->num_verts, dest_vertp)))
1799 continue;
1800
1801 if (!fvi_QuickRoomCheck(&src2, rp, false) && !fvi_QuickRoomCheck(&src2, rp, true))
1802 continue;
1803
1804 if (!(Rooms[check_room].flags & RF_EXTERNAL) &&
1805 (!fvi_QuickRoomCheck(&dest_vector, &Rooms[check_room], false) &&
1806 !fvi_QuickRoomCheck(&dest_vector, &Rooms[check_room], true)))
1807 continue;
1808
1809 // Check to see if we can see to this portal point
1810 fvi_query fq;
1811 fvi_info hit_data;
1812 int fate;
1813
1814 fq.p0 = &src2;
1815 fq.startroom = i;
1816 fq.p1 = &dest_vector;
1817 fq.rad = .01f;
1818 fq.thisobjnum = -1;
1819 fq.ignore_obj_list = NULL;
1820 fq.flags =
1821 FQ_BACKFACE |
1822 FQ_IGNORE_RENDER_THROUGH_PORTALS; // Gaurentees in wierd geometry cases (as the ray will hit as it
1823 // comes back in the mine) that the ray cannot start outside the
1824 // mine like with a slightly non-planar portal or other non-nice
1825 // situations like zero-width door portal/room-face combos
1826
1827 fate = fvi_FindIntersection(&fq, &hit_data);
1828 if (fate == HIT_NONE) {
1829 // if this portal can be seen, add all its portals to the stack
1830 // and then set this room to be visible
1831 for (int k = 0; k < Rooms[check_room].num_portals; k++) {
1832 int croom = Rooms[check_room].portals[k].croom;
1833 if (croom >= 0) {
1834 vis_stack[stack_count] = croom;
1835 stack_count++;
1836 ASSERT(stack_count < MAX_ROOMS * 10);
1837 }
1838 }
1839
1840 BOA_Array[i][check_room] |= BOAF_VIS;
1841 precomputed[i][check_room] = 1;
1842
1843 if (Rooms[check_room].flags & RF_EXTERNAL) {
1844 int cp;
1845 int xxx;
1846
1847 BOA_Array[i][Highest_room_index + 1] |= BOAF_VIS;
1848
1849 for (xxx = 0; xxx < Rooms[check_room].num_portals; xxx++) {
1850 int cell = GetTerrainCellFromPos(&Rooms[check_room].portals[xxx].path_pnt);
1851 ASSERT(cell != -1); // DAJ -1FIX
1852 int region = TERRAIN_REGION(cell);
1853
1854 BOA_Array[i][Highest_room_index + region + 1] |= BOAF_VIS;
1855 }
1856
1857 for (cp = 0; cp <= Highest_room_index; cp++) {
1858 if (Rooms[cp].used && (Rooms[cp].flags & RF_EXTERNAL) && !already_checked[cp]) {
1859 vis_stack[stack_count] = cp;
1860 stack_count++;
1861 ASSERT(stack_count < MAX_ROOMS * 10);
1862 }
1863 }
1864 }
1865
1866 done = 1;
1867 }
1868
1869 src2 -= 0.1f * rp->faces[rp->portals[t].portal_face].normal;
1870 dest_vector -= 0.1f * Rooms[check_room].faces[Rooms[check_room].portals[j].portal_face].normal;
1871 }
1872 }
1873 }
1874 }
1875 }
1876 }
1877
1878 if (done == 0)
1879 precomputed[i][check_room] = 0;
1880 }
1881 }
1882
1883 for (i = 0; i <= Highest_room_index; i++) {
1884 if (Rooms[i].used == 0)
1885 continue;
1886
1887 for (t = 0; t <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; t++) {
1888 if ((BOA_Array[i][t] & BOAF_VIS)) {
1889 BOA_Array[t][i] |= BOAF_VIS;
1890 }
1891 }
1892 }
1893
1894 #ifdef NEWEDITOR
1895 DoBOAVisProgressDialog(100.0f, 2);
1896 #endif
1897
1898 if (!from_lighting)
1899 EditorMessageBox("BOA vis table computed!");
1900
1901 BOA_vis_checksum = BOA_mine_checksum;
1902 BOA_vis_valid = 1;
1903 }
1904 #endif
1905
1906 void verify_connections() {
1907 int i;
1908 int j;
1909
1910 for (i = 0; i <= Highest_room_index + BOA_num_terrain_regions; i++) {
1911 if (i <= Highest_room_index && (!Rooms[i].used))
1912 continue;
1913
1914 if (i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
1915 continue;
1916
1917 for (j = 0; j <= Highest_room_index + BOA_num_terrain_regions; j++) {
1918 if (j <= Highest_room_index && (!Rooms[j].used))
1919 continue;
1920
1921 if (j <= Highest_room_index && (Rooms[j].flags & RF_EXTERNAL))
1922 continue;
1923
1924 if (i == Highest_room_index + 1 && j > Highest_room_index) {
1925 continue;
1926 }
1927
1928 if (j == Highest_room_index + 1 && i > Highest_room_index) {
1929 continue;
1930 }
1931
1932 if (i == j)
1933 continue;
1934
1935 int next_room = BOA_NEXT_ROOM(i, j);
1936
1937 if (next_room != i && next_room != BOA_NO_PATH) {
1938 int portal;
1939 portal = BOA_DetermineStartRoomPortal(i, NULL, next_room, NULL);
1940 portal = BOA_DetermineStartRoomPortal(next_room, NULL, i, NULL);
1941 }
1942 }
1943 }
1944 }
1945
1946 void find_small_portals() {
1947 int i, j;
1948 int counter = 0;
1949
1950 for (i = 0; i <= Highest_room_index; i++) {
1951 if (Rooms[i].used) {
1952 for (j = 0; j < Rooms[i].num_portals; j++) {
1953 face *fp = &Rooms[i].faces[Rooms[i].portals[j].portal_face];
1954
1955 float xdiff;
1956 float ydiff;
1957 ComputeBOAVisFaceUpperLeft(&Rooms[i], fp, NULL, &xdiff, &ydiff, NULL);
1958
1959 if (xdiff < 6.0f || ydiff < 6.0f) {
1960 counter++;
1961 Rooms[i].portals[j].flags |= PF_TOO_SMALL_FOR_ROBOT;
1962 } else {
1963 Rooms[i].portals[j].flags &= (~PF_TOO_SMALL_FOR_ROBOT);
1964 }
1965 }
1966 }
1967 }
1968
1969 mprintf(0, " Found %d small portals... :)\n", counter);
1970 }
1971
1972 void compute_robot_path_info() {
1973 int i, j;
1974
1975 for (i = 0; i <= Highest_room_index + BOA_num_terrain_regions; i++) {
1976 if (i <= Highest_room_index && (!Rooms[i].used))
1977 continue;
1978
1979 if (i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
1980 continue;
1981
1982 if (i > Highest_room_index + BOA_num_terrain_regions)
1983 continue;
1984
1985 for (j = 0; j <= Highest_room_index + BOA_num_terrain_regions; j++) {
1986 int cur_room = i;
1987
1988 if (j <= Highest_room_index && (!Rooms[i].used))
1989 continue;
1990
1991 if (j <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
1992 continue;
1993
1994 if (i == j)
1995 continue;
1996
1997 if (i == Highest_room_index + 1 && j > Highest_room_index)
1998 continue;
1999
2000 if (j == Highest_room_index + 1 && i > Highest_room_index)
2001 continue;
2002
2003 if (BOA_NEXT_ROOM(cur_room, j) != BOA_NO_PATH && BOA_NEXT_ROOM(cur_room, j) != cur_room) {
2004 int last_room = cur_room;
2005
2006 do {
2007 last_room = cur_room;
2008 cur_room = BOA_NEXT_ROOM(cur_room, j);
2009
2010 if (last_room != cur_room) {
2011 if (BOA_DetermineStartRoomPortal(last_room, NULL, cur_room, NULL, false, true) == -1) {
2012 BOA_Array[i][j] |= BOAF_TOO_SMALL_FOR_ROBOT;
2013 break;
2014 }
2015 }
2016
2017 } while (cur_room != j);
2018 }
2019 }
2020 }
2021 }
2022
2023 void MakeBOA(void) {
2024 ASSERT(BOA_ROOM_MASK > MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS);
2025 int cur_check = BOAGetMineChecksum();
2026
2027 if (cur_check == BOA_vis_checksum)
2028 BOA_vis_valid = 1;
2029 else
2030 BOA_vis_valid = 0;
2031
2032 if (cur_check == BOA_mine_checksum)
2033 return;
2034
2035 // OutrageMessageBox("Reminder: You need to make BOA Vis on this level.\nThis is either because it hasn't\nbeen done
2036 // or Chris updated BOA.");
2037
2038 BOA_mine_checksum = cur_check;
2039 BOA_f_making_boa = true;
2040
2041 mprintf(0, "Making BOA and friends\n");
2042
2043 mprintf(0, "Finding small portals\n");
2044 find_small_portals();
2045 mprintf(0, "Done Finding small portals\n");
2046
2047 mprintf(0, " Start computing path points.\n");
2048 BOA_ComputePathPoints();
2049 mprintf(0, " Done computing path points.\n");
2050
2051 clear_BOA();
2052 compute_costs();
2053
2054 mprintf(0, " Start computing mines.\n");
2055 compute_mine_info();
2056 mprintf(0, " Done computing %d mines.\n", BOA_num_mines);
2057
2058 mprintf(0, " Start computing terrain regions.\n");
2059 compute_terrain_region_info();
2060 mprintf(0, " Done computing %d terrain regions.\n", BOA_num_terrain_regions);
2061
2062 mprintf(0, " Making designers wait for no particular reason...\n");
2063 compute_next_segs();
2064 mprintf(0, " Done with the sodomy...\n");
2065
2066 mprintf(0, " Start computing blockage info.\n");
2067 compute_blockage_info();
2068 mprintf(0, " Done computing blockage info.\n");
2069
2070 mprintf(0, " Start computing sound prop.\n");
2071 compute_sound_dist_info();
2072 mprintf(0, " Done computing sound prop.\n");
2073
2074 mprintf(0, " Start computing invalid robot path info.\n");
2075 compute_robot_path_info();
2076 mprintf(0, " Done computing invalid robot path info.\n");
2077
2078 mprintf(0, " Verifying connections\n");
2079 verify_connections();
2080 mprintf(0, " Done with verification\n");
2081
2082 // {
2083 // int cur_seg = 0;
2084 // vector pos;
2085 // matrix orient = IDENTITY_MATRIX;
2086 //
2087 // while (cur_seg != Highest_segment_index) {
2088 //
2089 // pos = average_pnt(&Segments[cur_seg]);
2090 //
2091 // ObjCreate( 7, 0, cur_seg, &pos, &orient, 0);
2092 // cur_seg = BOA_Array[cur_seg][Highest_segment_index] & BOA_SEG_MASK;
2093 //
2094 // }
2095 // }
2096
2097 BOA_f_making_boa = false;
2098 mprintf(0, "BOA is done\n");
2099 }
2100
2101 static int Current_sort_room;
2102
2103 static int face_sort_func1(const int16_t *a, const int16_t *b) {
2104 if (Rooms[Current_sort_room].faces[*a].min_xyz.y > Rooms[Current_sort_room].faces[*b].min_xyz.y)
2105 return -1;
2106 else if (Rooms[Current_sort_room].faces[*a].min_xyz.y < Rooms[Current_sort_room].faces[*b].min_xyz.y)
2107 return 1;
2108 else
2109 return 0;
2110 }
2111
2112 static int face_sort_func2(const int16_t *a, const int16_t *b) {
2113 if (Rooms[Current_sort_room].faces[*a].max_xyz.y < Rooms[Current_sort_room].faces[*b].max_xyz.y)
2114 return -1;
2115 else if (Rooms[Current_sort_room].faces[*a].max_xyz.y > Rooms[Current_sort_room].faces[*b].max_xyz.y)
2116 return 1;
2117 else
2118 return 0;
2119 }
2120
2121 #define MAX_REGIONS_PER_ROOM 200
2122
2123 void ComputeAABB(bool f_full) {
2124 int i;
2125 int j;
2126 int k;
2127
2128 int computed_room_check[MAX_ROOMS];
2129
2130 mprintf(0, "Computing AABB's");
2131
2132 // Determines the room and face min/max information
2133 int cur_check = BOAGetMineChecksum();
2134
2135 if (cur_check == BOA_AABB_checksum && !f_full) {
2136 mprintf(0, " (partial)!\n");
2137 } else {
2138 for (i = 0; i < MAX_ROOMS; i++) {
2139 if (Rooms[i].used) {
2140 computed_room_check[i] = BOAGetRoomChecksum(i);
2141 } else {
2142 BOA_AABB_ROOM_checksum[i] = computed_room_check[i] = 0;
2143 }
2144 }
2145
2146 int16_t *num_structs_per_room = (int16_t *)mem_malloc((Highest_room_index + 1) * sizeof(int16_t));
2147 int16_t **r_struct_list;
2148
2149 // Allocate the structure that tells what struct each face is in
2150 r_struct_list = (int16_t **)mem_malloc((Highest_room_index + 1) * sizeof(int16_t *));
2151 for (i = 0; i <= Highest_room_index; i++) {
2152 if (Rooms[i].used) {
2153 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2154 continue;
2155
2156 r_struct_list[i] = (int16_t *)mem_malloc(Rooms[i].num_faces * sizeof(int16_t));
2157 }
2158 }
2159
2160 int16_t count;
2161
2162 BOA_AABB_checksum = cur_check;
2163 mprintf(0, " (full)!\n");
2164
2165 for (i = 0; i <= Highest_room_index; i++) {
2166 Current_sort_room = i;
2167 if (Rooms[i].used) {
2168 float average_y;
2169 average_y = 0.0f;
2170
2171 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2172 continue;
2173
2174 for (j = 0; j < Rooms[i].num_faces; j++) {
2175 vector face_min;
2176 vector face_max;
2177
2178 for (k = 0; k < Rooms[i].faces[j].num_verts; k++) {
2179 if (k == 0 || Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].x < face_min.x)
2180 face_min.x = Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].x;
2181
2182 if (k == 0 || Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].y < face_min.y)
2183 face_min.y = Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].y;
2184
2185 if (k == 0 || Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].z < face_min.z)
2186 face_min.z = Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].z;
2187
2188 if (k == 0 || Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].x > face_max.x)
2189 face_max.x = Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].x;
2190
2191 if (k == 0 || Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].y > face_max.y)
2192 face_max.y = Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].y;
2193
2194 if (k == 0 || Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].z > face_max.z)
2195 face_max.z = Rooms[i].verts[Rooms[i].faces[j].face_verts[k]].z;
2196 }
2197
2198 Rooms[i].faces[j].min_xyz = face_min;
2199 Rooms[i].faces[j].max_xyz = face_max;
2200
2201 average_y += ((face_min.y + face_max.y) / 2);
2202 }
2203
2204 average_y /= Rooms[i].num_faces;
2205
2206 // Do the rooms now
2207
2208 room *rp = &Rooms[i];
2209
2210 rp->max_xyz.x = rp->max_xyz.y = rp->max_xyz.z = -9999999.0;
2211 rp->min_xyz.x = rp->min_xyz.y = rp->min_xyz.z = 9999999.0;
2212
2213 for (int t = 0; t < rp->num_faces; t++) {
2214 face *fp = &rp->faces[t];
2215
2216 if (fp->max_xyz.x > rp->max_xyz.x)
2217 rp->max_xyz.x = fp->max_xyz.x;
2218
2219 if (fp->max_xyz.y > rp->max_xyz.y)
2220 rp->max_xyz.y = fp->max_xyz.y;
2221
2222 if (fp->max_xyz.z > rp->max_xyz.z)
2223 rp->max_xyz.z = fp->max_xyz.z;
2224
2225 if (fp->min_xyz.x < rp->min_xyz.x)
2226 rp->min_xyz.x = fp->min_xyz.x;
2227
2228 if (fp->min_xyz.y < rp->min_xyz.y)
2229 rp->min_xyz.y = fp->min_xyz.y;
2230
2231 if (fp->min_xyz.z < rp->min_xyz.z)
2232 rp->min_xyz.z = fp->min_xyz.z;
2233 }
2234 }
2235 }
2236
2237 // Determine number of independant structures and classify each face
2238 for (i = 0; i <= Highest_room_index; i++) {
2239 if (Rooms[i].used) {
2240 room *rp = &Rooms[i];
2241 int num_struct = 0;
2242 int count1, count2;
2243
2244 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2245 continue;
2246
2247 int16_t *nfaces;
2248 bool *used;
2249
2250 int n_new;
2251
2252 nfaces = (int16_t *)mem_malloc(sizeof(int16_t) * rp->num_faces);
2253 used = (bool *)mem_malloc(sizeof(bool) * rp->num_faces);
2254
2255 for (count1 = 0; count1 < rp->num_faces; count1++) {
2256 used[count1] = false;
2257 }
2258
2259 next_struct:
2260
2261 for (count1 = 0; count1 < rp->num_faces; count1++) {
2262 if (!used[count1])
2263 break;
2264 }
2265
2266 if (count1 >= rp->num_faces)
2267 goto done;
2268
2269 num_struct++;
2270
2271 n_new = 1;
2272 nfaces[0] = count1;
2273 used[count1] = true;
2274 r_struct_list[i][count1] = num_struct - 1;
2275 // r_struct_list[i][count1] = 0;
2276
2277 while (n_new > 0) {
2278 int seed = nfaces[--n_new];
2279 int count3;
2280
2281 for (count1 = 0; count1 < rp->faces[seed].num_verts; count1++) {
2282 int c_vert = rp->faces[seed].face_verts[count1];
2283
2284 for (count2 = 0; count2 < rp->num_faces; count2++) {
2285 if (used[count2] == false) {
2286 for (count3 = 0; count3 < rp->faces[count2].num_verts; count3++) {
2287 if (c_vert == rp->faces[count2].face_verts[count3]) {
2288 nfaces[n_new++] = count2;
2289 used[count2] = true;
2290 r_struct_list[i][count2] = num_struct - 1;
2291 // r_struct_list[i][count2] = 0;
2292 break;
2293 }
2294 }
2295 }
2296 }
2297 }
2298 }
2299
2300 goto next_struct;
2301
2302 done:
2303
2304 mem_free(nfaces);
2305 mem_free(used);
2306
2307 ASSERT(num_struct < MAX_REGIONS_PER_ROOM); // get chris
2308 num_structs_per_room[i] = num_struct;
2309 // mprintf(0, "%d structs in r %d\n", num_struct, i);
2310 }
2311 }
2312
2313 // Determine Area of each region for external shell remap
2314 for (i = 0; i <= Highest_room_index; i++) {
2315 if (Rooms[i].used) {
2316 room *rp = &Rooms[i];
2317
2318 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2319 continue;
2320
2321 if (num_structs_per_room[i] == 1)
2322 continue;
2323 // else
2324 // num_structs_per_room[i] = 1;
2325 //
2326 // continue;
2327
2328 vector *s_max_xyz = (vector *)mem_malloc(num_structs_per_room[i] * sizeof(vector));
2329 vector *s_min_xyz = (vector *)mem_malloc(num_structs_per_room[i] * sizeof(vector));
2330
2331 for (count = 0; count < num_structs_per_room[i]; count++) {
2332
2333 s_max_xyz[count].x = s_max_xyz[count].y = s_max_xyz[count].z = -9999999.0;
2334 s_min_xyz[count].x = s_min_xyz[count].y = s_min_xyz[count].z = 9999999.0;
2335
2336 for (int t = 0; t < rp->num_faces; t++) {
2337 if (r_struct_list[i][t] != count)
2338 continue;
2339
2340 face *fp = &rp->faces[t];
2341
2342 if (fp->max_xyz.x > s_max_xyz[count].x)
2343 s_max_xyz[count].x = fp->max_xyz.x;
2344
2345 if (fp->max_xyz.y > s_max_xyz[count].y)
2346 s_max_xyz[count].y = fp->max_xyz.y;
2347
2348 if (fp->max_xyz.z > s_max_xyz[count].z)
2349 s_max_xyz[count].z = fp->max_xyz.z;
2350
2351 if (fp->min_xyz.x < s_min_xyz[count].x)
2352 s_min_xyz[count].x = fp->min_xyz.x;
2353
2354 if (fp->min_xyz.y < s_min_xyz[count].y)
2355 s_min_xyz[count].y = fp->min_xyz.y;
2356
2357 if (fp->min_xyz.z < s_min_xyz[count].z)
2358 s_min_xyz[count].z = fp->min_xyz.z;
2359 }
2360 }
2361
2362 int best = 0;
2363 vector diff = s_max_xyz[0] - s_min_xyz[0];
2364 float best_size = fabs(diff.x * diff.y * diff.z);
2365
2366 for (count = 1; count < num_structs_per_room[i]; count++) {
2367 diff = s_max_xyz[count] - s_min_xyz[count];
2368 float size = fabs(diff.x * diff.y * diff.z);
2369
2370 if (size > best_size) {
2371 best = count;
2372 best_size = size;
2373 }
2374 }
2375
2376 // mprintf(0, "Room %d Best Shell is %d\n", i, best);
2377 if (best != 0) {
2378 for (count = 0; count < rp->num_faces; count++) {
2379 if (r_struct_list[i][count] == 0) {
2380 r_struct_list[i][count] = best;
2381 } else if (r_struct_list[i][count] == best) {
2382 r_struct_list[i][count] = 0;
2383 }
2384 }
2385 }
2386
2387 mem_free(s_max_xyz);
2388 mem_free(s_min_xyz);
2389 }
2390 }
2391
2392 // Breaks up the main shell by cube and pushes faces into the appropriate region list
2393 for (i = 0; i <= Highest_room_index; i++) {
2394 if (Rooms[i].used) {
2395 int x;
2396 Current_sort_room = i;
2397 room *rp = &Rooms[i];
2398
2399 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2400 continue;
2401
2402 vector min_xyz;
2403 vector max_xyz;
2404 int pamount[6];
2405 int nonpart = 0;
2406
2407 if (rp->num_bbf_regions != 0) {
2408 for (x = 0; x < rp->num_bbf_regions; x++) {
2409 mem_free(rp->bbf_list[x]);
2410 }
2411 mem_free(rp->bbf_list);
2412 mem_free(rp->num_bbf);
2413 mem_free(rp->bbf_list_min_xyz);
2414 mem_free(rp->bbf_list_max_xyz);
2415 mem_free(rp->bbf_list_sector);
2416 rp->num_bbf_regions = 0;
2417 }
2418
2419 // temporary malloc
2420 rp->num_bbf_regions = 27 + num_structs_per_room[i] - 1;
2421 rp->bbf_list = (int16_t **)mem_malloc(MAX_REGIONS_PER_ROOM * sizeof(int16_t *));
2422 for (x = 0; x < MAX_REGIONS_PER_ROOM; x++) {
2423 rp->bbf_list[x] = (int16_t *)mem_malloc(rp->num_faces * sizeof(int16_t));
2424 }
2425 rp->num_bbf = (int16_t *)mem_malloc(MAX_REGIONS_PER_ROOM * sizeof(int16_t));
2426 rp->bbf_list_min_xyz = (vector *)mem_malloc(MAX_REGIONS_PER_ROOM * sizeof(vector));
2427 rp->bbf_list_max_xyz = (vector *)mem_malloc(MAX_REGIONS_PER_ROOM * sizeof(vector));
2428 rp->bbf_list_sector = (uint8_t *)mem_malloc(MAX_REGIONS_PER_ROOM * sizeof(uint8_t));
2429
2430 for (x = 0; x < 27; x++) {
2431 rp->bbf_list_sector[x] = bbf_lookup[x];
2432 }
2433
2434 vector diff;
2435 // vector min_diff;
2436 // vector max_diff;
2437 diff.x = diff.y = diff.z = 15.0f;
2438
2439 min_xyz = max_xyz = (rp->min_xyz + rp->max_xyz) / 2.0f;
2440
2441 min_xyz -= diff;
2442 max_xyz += diff;
2443
2444 if (min_xyz.x <= rp->min_xyz.x)
2445 min_xyz.x = rp->min_xyz.x + 2.5f;
2446 if (min_xyz.y <= rp->min_xyz.y)
2447 min_xyz.y = rp->min_xyz.y + 2.5f;
2448 if (min_xyz.z <= rp->min_xyz.z)
2449 min_xyz.z = rp->min_xyz.z + 2.5f;
2450 if (max_xyz.x >= rp->max_xyz.x)
2451 max_xyz.x = rp->max_xyz.x - 2.5f;
2452 if (max_xyz.y >= rp->max_xyz.y)
2453 max_xyz.y = rp->max_xyz.y - 2.5f;
2454 if (max_xyz.z >= rp->max_xyz.z)
2455 max_xyz.z = rp->max_xyz.z - 2.5f;
2456
2457 if (min_xyz.x >= max_xyz.x) {
2458 min_xyz.x = rp->min_xyz.x;
2459 max_xyz.x = rp->max_xyz.x;
2460 }
2461 if (min_xyz.y >= max_xyz.y) {
2462 min_xyz.y = rp->min_xyz.y;
2463 max_xyz.y = rp->max_xyz.y;
2464 }
2465 if (min_xyz.z >= max_xyz.z) {
2466 min_xyz.z = rp->min_xyz.z;
2467 max_xyz.z = rp->max_xyz.z;
2468 }
2469
2470 rp->bbf_min_xyz = min_xyz;
2471 rp->bbf_max_xyz = max_xyz;
2472
2473 // chrishack -- add post face add snap
2474
2475 for (count = 0; count < 6; count++) {
2476 pamount[count] = 0;
2477 }
2478
2479 for (count = 0; count < rp->num_bbf_regions; count++) {
2480 rp->num_bbf[count] = 0;
2481 }
2482
2483 for (count = 0; count < rp->num_faces; count++) {
2484 face *fp = &rp->faces[count];
2485 bool f_part = false;
2486 bool list[6];
2487 int x;
2488
2489 for (x = 0; x < 6; x++)
2490 list[x] = false;
2491
2492 if (fp->max_xyz.x <= min_xyz.x) {
2493 list[0] = true;
2494 f_part = true;
2495 }
2496 if (fp->max_xyz.y <= min_xyz.y) {
2497 list[1] = true;
2498 f_part = true;
2499 }
2500 if (fp->max_xyz.z <= min_xyz.z) {
2501 list[2] = true;
2502 f_part = true;
2503 }
2504 if ((!list[0]) && fp->min_xyz.x >= max_xyz.x) {
2505 list[3] = true;
2506 f_part = true;
2507 }
2508 if ((!list[1]) && fp->min_xyz.y >= max_xyz.y) {
2509 list[4] = true;
2510 f_part = true;
2511 }
2512 if ((!list[2]) && fp->min_xyz.z >= max_xyz.z) {
2513 list[5] = true;
2514 f_part = true;
2515 }
2516
2517 uint8_t sector = 0;
2518
2519 if (f_part == false) {
2520 if (r_struct_list[i][count] == 0)
2521 rp->bbf_list[0][rp->num_bbf[0]++] = count;
2522 else {
2523 if (rp->num_bbf[27 + r_struct_list[i][count] - 1] == 0)
2524 rp->bbf_list_sector[27 + r_struct_list[i][count] - 1] = sector;
2525 else
2526 rp->bbf_list_sector[27 + r_struct_list[i][count] - 1] &= sector;
2527
2528 rp->bbf_list[27 + r_struct_list[i][count] - 1][rp->num_bbf[27 + r_struct_list[i][count] - 1]++] = count;
2529 }
2530
2531 nonpart++;
2532 } else {
2533 // bool f_found = false;
2534 // int best;
2535
2536 for (x = 0; x < 6; x++) {
2537 if (list[x]) {
2538 sector |= 0x01 << x;
2539 }
2540 }
2541
2542 int slot;
2543
2544 switch (sector) {
2545 case 0x01:
2546 slot = 1;
2547 break;
2548 case 0x02:
2549 slot = 2;
2550 break;
2551 case 0x04:
2552 slot = 3;
2553 break;
2554 case 0x08:
2555 slot = 4;
2556 break;
2557 case 0x10:
2558 slot = 5;
2559 break;
2560 case 0x20:
2561 slot = 6;
2562 break;
2563 case (0x01 | 0x02):
2564 slot = 7;
2565 break;
2566 case (0x01 | 0x04):
2567 slot = 8;
2568 break;
2569 case (0x01 | 0x10):
2570 slot = 9;
2571 break;
2572 case (0x01 | 0x20):
2573 slot = 10;
2574 break;
2575 case (0x02 | 0x04):
2576 slot = 11;
2577 break;
2578 case (0x02 | 0x08):
2579 slot = 12;
2580 break;
2581 case (0x02 | 0x20):
2582 slot = 13;
2583 break;
2584 case (0x04 | 0x08):
2585 slot = 14;
2586 break;
2587 case (0x04 | 0x10):
2588 slot = 15;
2589 break;
2590 case (0x08 | 0x10):
2591 slot = 16;
2592 break;
2593 case (0x08 | 0x20):
2594 slot = 17;
2595 break;
2596 case (0x10 | 0x20):
2597 slot = 18;
2598 break;
2599 case 0x01 | 0x02 | 0x04:
2600 slot = 19;
2601 break;
2602 case 0x01 | 0x02 | 0x20:
2603 slot = 20;
2604 break;
2605 case 0x01 | 0x04 | 0x10:
2606 slot = 21;
2607 break;
2608 case 0x01 | 0x10 | 0x20:
2609 slot = 22;
2610 break;
2611 case 0x08 | 0x02 | 0x04:
2612 slot = 23;
2613 break;
2614 case 0x08 | 0x02 | 0x20:
2615 slot = 24;
2616 break;
2617 case 0x08 | 0x04 | 0x10:
2618 slot = 25;
2619 break;
2620 case 0x08 | 0x10 | 0x20:
2621 slot = 26;
2622 break;
2623 default:
2624 Int3();
2625 }
2626
2627 if (r_struct_list[i][count] == 0)
2628 rp->bbf_list[slot][rp->num_bbf[slot]++] = count;
2629 else {
2630 if (rp->num_bbf[27 + r_struct_list[i][count] - 1] == 0)
2631 rp->bbf_list_sector[27 + r_struct_list[i][count] - 1] = sector;
2632 else
2633 rp->bbf_list_sector[27 + r_struct_list[i][count] - 1] &= sector;
2634
2635 rp->bbf_list[27 + r_struct_list[i][count] - 1][rp->num_bbf[27 + r_struct_list[i][count] - 1]++] = count;
2636 }
2637
2638 ASSERT(slot > 0 && slot < rp->num_bbf_regions);
2639 }
2640 }
2641 }
2642 }
2643
2644 mem_free(num_structs_per_room);
2645 for (i = 0; i <= Highest_room_index; i++) {
2646 if (Rooms[i].used) {
2647 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2648 continue;
2649
2650 mem_free(r_struct_list[i]);
2651 }
2652 }
2653 mem_free(r_struct_list);
2654 num_structs_per_room = NULL;
2655 r_struct_list = NULL;
2656
2657 // Finds the min/max of each region
2658 for (i = 0; i <= Highest_room_index; i++) {
2659 if (Rooms[i].used) {
2660 room *rp = &Rooms[i];
2661
2662 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2663 continue;
2664
2665 for (count = 0; count < rp->num_bbf_regions; count++) {
2666 rp->bbf_list_max_xyz[count].x = rp->bbf_list_max_xyz[count].y = rp->bbf_list_max_xyz[count].z = -9999999.0;
2667 rp->bbf_list_min_xyz[count].x = rp->bbf_list_min_xyz[count].y = rp->bbf_list_min_xyz[count].z = 9999999.0;
2668
2669 for (int t = 0; t < rp->num_bbf[count]; t++) {
2670 face *fp = &rp->faces[rp->bbf_list[count][t]];
2671
2672 if (fp->max_xyz.x > rp->bbf_list_max_xyz[count].x)
2673 rp->bbf_list_max_xyz[count].x = fp->max_xyz.x;
2674
2675 if (fp->max_xyz.y > rp->bbf_list_max_xyz[count].y)
2676 rp->bbf_list_max_xyz[count].y = fp->max_xyz.y;
2677
2678 if (fp->max_xyz.z > rp->bbf_list_max_xyz[count].z)
2679 rp->bbf_list_max_xyz[count].z = fp->max_xyz.z;
2680
2681 if (fp->min_xyz.x < rp->bbf_list_min_xyz[count].x)
2682 rp->bbf_list_min_xyz[count].x = fp->min_xyz.x;
2683
2684 if (fp->min_xyz.y < rp->bbf_list_min_xyz[count].y)
2685 rp->bbf_list_min_xyz[count].y = fp->min_xyz.y;
2686
2687 if (fp->min_xyz.z < rp->bbf_list_min_xyz[count].z)
2688 rp->bbf_list_min_xyz[count].z = fp->min_xyz.z;
2689 }
2690 }
2691 }
2692 }
2693
2694 // Remove unneccessary groups
2695 for (count = 0; count <= Highest_room_index; count++) {
2696 if (Rooms[count].used) {
2697 // mprintf(0, "==================\n", count);
2698 // mprintf(0, "Room %d\n", count);
2699
2700 if (BOA_AABB_ROOM_checksum[count] != 0 && BOA_AABB_ROOM_checksum[count] == computed_room_check[count])
2701 continue;
2702
2703 room *rp = &Rooms[count];
2704 for (i = 0; i < rp->num_bbf_regions; i++) {
2705 // mprintf(0, "Region %d has %d faces in it.\n", i, rp->num_bbf[i]);
2706
2707 if (rp->num_bbf[i] == 0) {
2708 for (j = i + 1; j < rp->num_bbf_regions; j++) {
2709 int16_t *temp = rp->bbf_list[j - 1];
2710
2711 rp->bbf_list[j - 1] = rp->bbf_list[j];
2712 rp->bbf_list[j] = temp;
2713
2714 rp->num_bbf[j - 1] = rp->num_bbf[j];
2715 rp->bbf_list_min_xyz[j - 1] = rp->bbf_list_min_xyz[j];
2716 rp->bbf_list_max_xyz[j - 1] = rp->bbf_list_max_xyz[j];
2717 rp->bbf_list_sector[j - 1] = rp->bbf_list_sector[j];
2718 }
2719
2720 rp->num_bbf_regions--;
2721 i--;
2722 }
2723 }
2724 }
2725 }
2726
2727 // Sub-divide structures
2728 for (i = 0; i <= Highest_room_index; i++) {
2729 if (Rooms[i].used) {
2730 room *rp = &Rooms[i];
2731 int original_bbf_regions = rp->num_bbf_regions;
2732
2733 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2734 continue;
2735
2736 for (j = 0; j < original_bbf_regions; j++) {
2737 if (rp->num_bbf[j] > 15) {
2738 // Test 3 split type
2739 vector split = (rp->bbf_list_min_xyz[j] + rp->bbf_list_max_xyz[j]) / 2.0f;
2740 int num_faces[3][3];
2741
2742 int sp;
2743 int gel;
2744
2745 for (sp = 0; sp < 3; sp++) {
2746 for (gel = 0; gel < 3; gel++) {
2747 num_faces[sp][gel] = 0;
2748 }
2749 }
2750
2751 for (sp = 0; sp < 3; sp++) {
2752 float val = ((float *)&split)[sp];
2753
2754 for (count = 0; count < rp->num_bbf[j]; count++) {
2755 if (((float *)(&rp->faces[rp->bbf_list[j][count]].max_xyz))[sp] <= val)
2756 gel = 2;
2757 else if (((float *)(&rp->faces[rp->bbf_list[j][count]].min_xyz))[sp] >= val)
2758 gel = 0;
2759 else
2760 gel = 1;
2761
2762 num_faces[sp][gel]++;
2763 }
2764 }
2765
2766 int diff[3];
2767 // mprintf(0, "Split plane results r %d s %d n %d\n", i, j, rp->num_bbf[j]);
2768 for (sp = 0; sp < 3; sp++) {
2769 // mprintf(0, "%d %d %d\n", num_faces[sp][0], num_faces[sp][1], num_faces[sp][2]);
2770
2771 if (abs(num_faces[sp][0] - num_faces[sp][1]) > abs(num_faces[sp][1] - num_faces[sp][2]))
2772 diff[sp] = abs(num_faces[sp][0] - num_faces[sp][1]);
2773 else
2774 diff[sp] = abs(num_faces[sp][1] - num_faces[sp][2]);
2775 }
2776
2777 int best = 0;
2778 float b_diff = diff[0];
2779
2780 for (sp = 1; sp < 3; sp++) {
2781 if (b_diff > diff[sp]) {
2782 best = sp;
2783 b_diff = diff[sp];
2784 }
2785 }
2786
2787 // mprintf(0, "Split Plane is %d\n", best);
2788
2789 if (rp->num_bbf_regions <= MAX_REGIONS_PER_ROOM - 2) {
2790 rp->num_bbf[rp->num_bbf_regions] = 0;
2791 rp->num_bbf[rp->num_bbf_regions + 1] = 0;
2792
2793 sp = best;
2794 for (count = 0; count < rp->num_bbf[j]; count++) {
2795 float val = ((float *)&split)[sp];
2796
2797 if (((float *)(&rp->faces[rp->bbf_list[j][count]].max_xyz))[sp] <= val) {
2798 rp->bbf_list[rp->num_bbf_regions][rp->num_bbf[rp->num_bbf_regions]++] = rp->bbf_list[j][count];
2799
2800 int t;
2801 for (t = count; t < rp->num_bbf[j] - 1; t++) {
2802 rp->bbf_list[j][t] = rp->bbf_list[j][t + 1];
2803 }
2804
2805 rp->num_bbf[j]--;
2806 count--;
2807 } else if (((float *)(&rp->faces[rp->bbf_list[j][count]].min_xyz))[sp] >= val) {
2808 rp->bbf_list[rp->num_bbf_regions + 1][rp->num_bbf[rp->num_bbf_regions + 1]++] =
2809 rp->bbf_list[j][count];
2810
2811 int t;
2812 for (t = count; t < rp->num_bbf[j] - 1; t++) {
2813 rp->bbf_list[j][t] = rp->bbf_list[j][t + 1];
2814 }
2815
2816 rp->num_bbf[j]--;
2817 count--;
2818 }
2819 }
2820
2821 rp->num_bbf_regions += 2;
2822 }
2823 }
2824 }
2825 }
2826 }
2827
2828 // Remove unneccessary groups
2829 for (count = 0; count <= Highest_room_index; count++) {
2830 if (Rooms[count].used) {
2831 // mprintf(0, "==================\n", count);
2832 // mprintf(0, "Room %d\n", count);
2833 if (BOA_AABB_ROOM_checksum[count] != 0 && BOA_AABB_ROOM_checksum[count] == computed_room_check[count])
2834 continue;
2835
2836 room *rp = &Rooms[count];
2837 for (i = 0; i < rp->num_bbf_regions; i++) {
2838 // mprintf(0, "Region %d has %d faces in it.\n", i, rp->num_bbf[i]);
2839
2840 if (rp->num_bbf[i] == 0) {
2841 for (j = i + 1; j < rp->num_bbf_regions; j++) {
2842 int16_t *temp = rp->bbf_list[j - 1];
2843
2844 rp->bbf_list[j - 1] = rp->bbf_list[j];
2845 rp->bbf_list[j] = temp;
2846
2847 rp->num_bbf[j - 1] = rp->num_bbf[j];
2848 rp->bbf_list_min_xyz[j - 1] = rp->bbf_list_min_xyz[j];
2849 rp->bbf_list_max_xyz[j - 1] = rp->bbf_list_max_xyz[j];
2850 rp->bbf_list_sector[j - 1] = rp->bbf_list_sector[j];
2851 }
2852
2853 rp->num_bbf_regions--;
2854 i--;
2855 }
2856 }
2857 }
2858 }
2859
2860 // Finds the min/max of each region
2861 for (i = 0; i <= Highest_room_index; i++) {
2862 if (Rooms[i].used) {
2863 room *rp = &Rooms[i];
2864
2865 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2866 continue;
2867
2868 for (count = 0; count < rp->num_bbf_regions; count++) {
2869 rp->bbf_list_max_xyz[count].x = rp->bbf_list_max_xyz[count].y = rp->bbf_list_max_xyz[count].z = -9999999.0;
2870 rp->bbf_list_min_xyz[count].x = rp->bbf_list_min_xyz[count].y = rp->bbf_list_min_xyz[count].z = 9999999.0;
2871
2872 for (int t = 0; t < rp->num_bbf[count]; t++) {
2873 face *fp = &rp->faces[rp->bbf_list[count][t]];
2874
2875 if (fp->max_xyz.x > rp->bbf_list_max_xyz[count].x)
2876 rp->bbf_list_max_xyz[count].x = fp->max_xyz.x;
2877
2878 if (fp->max_xyz.y > rp->bbf_list_max_xyz[count].y)
2879 rp->bbf_list_max_xyz[count].y = fp->max_xyz.y;
2880
2881 if (fp->max_xyz.z > rp->bbf_list_max_xyz[count].z)
2882 rp->bbf_list_max_xyz[count].z = fp->max_xyz.z;
2883
2884 if (fp->min_xyz.x < rp->bbf_list_min_xyz[count].x)
2885 rp->bbf_list_min_xyz[count].x = fp->min_xyz.x;
2886
2887 if (fp->min_xyz.y < rp->bbf_list_min_xyz[count].y)
2888 rp->bbf_list_min_xyz[count].y = fp->min_xyz.y;
2889
2890 if (fp->min_xyz.z < rp->bbf_list_min_xyz[count].z)
2891 rp->bbf_list_min_xyz[count].z = fp->min_xyz.z;
2892 }
2893 }
2894 }
2895 }
2896
2897 // Remaps all the regions to their best sectors
2898 for (i = 0; i <= Highest_room_index; i++) {
2899 if (Rooms[i].used) {
2900 room *rp = &Rooms[i];
2901 vector min_xyz = rp->bbf_min_xyz;
2902 vector max_xyz = rp->bbf_max_xyz;
2903
2904 if (BOA_AABB_ROOM_checksum[i] != 0 && BOA_AABB_ROOM_checksum[i] == computed_room_check[i])
2905 continue;
2906
2907 for (j = 0; j < rp->num_bbf_regions; j++) {
2908 for (count = 0; count < rp->num_bbf[j]; count++) {
2909 face *fp = &rp->faces[rp->bbf_list[j][count]];
2910 bool list[6];
2911 int x;
2912
2913 for (x = 0; x < 6; x++)
2914 list[x] = false;
2915
2916 if (fp->max_xyz.x <= min_xyz.x) {
2917 list[0] = true;
2918 }
2919 if (fp->max_xyz.y <= min_xyz.y) {
2920 list[1] = true;
2921 }
2922 if (fp->max_xyz.z <= min_xyz.z) {
2923 list[2] = true;
2924 }
2925 if ((!list[0]) && fp->min_xyz.x >= max_xyz.x) {
2926 list[3] = true;
2927 }
2928 if ((!list[1]) && fp->min_xyz.y >= max_xyz.y) {
2929 list[4] = true;
2930 }
2931 if ((!list[2]) && fp->min_xyz.z >= max_xyz.z) {
2932 list[5] = true;
2933 }
2934
2935 uint8_t sector = 0;
2936
2937 for (x = 0; x < 6; x++) {
2938 if (list[x]) {
2939 sector |= 0x01 << x;
2940 }
2941 }
2942
2943 if (count == 0)
2944 rp->bbf_list_sector[j] = sector;
2945 else
2946 rp->bbf_list_sector[j] &= sector;
2947 }
2948 }
2949 }
2950 }
2951
2952 // Reallocate remaining data structures
2953 // Remove extra slots
2954 for (count = 0; count <= Highest_room_index; count++) {
2955 if (Rooms[count].used) {
2956 room *rp = &Rooms[count];
2957 if (BOA_AABB_ROOM_checksum[count] != 0 && BOA_AABB_ROOM_checksum[count] == computed_room_check[count])
2958 continue;
2959
2960 for (j = 0; j < rp->num_bbf_regions; j++) {
2961 rp->bbf_list[j] = (int16_t *)mem_realloc(rp->bbf_list[j], (sizeof(int16_t) * rp->num_bbf[j]));
2962 }
2963 for (j = rp->num_bbf_regions; j < MAX_REGIONS_PER_ROOM; j++) {
2964 mem_free(rp->bbf_list[j]);
2965 }
2966
2967 rp->bbf_list = (int16_t **)mem_realloc(rp->bbf_list, rp->num_bbf_regions * sizeof(int16_t *));
2968 rp->num_bbf = (int16_t *)mem_realloc(rp->num_bbf, rp->num_bbf_regions * sizeof(int16_t));
2969 rp->bbf_list_min_xyz = (vector *)mem_realloc(rp->bbf_list_min_xyz, rp->num_bbf_regions * sizeof(vector));
2970 rp->bbf_list_max_xyz = (vector *)mem_realloc(rp->bbf_list_max_xyz, rp->num_bbf_regions * sizeof(vector));
2971 rp->bbf_list_sector =
2972 (uint8_t *)mem_realloc(rp->bbf_list_sector, rp->num_bbf_regions * sizeof(uint8_t));
2973 }
2974 }
2975
2976 // // Print out remainding groups
2977 // for(count = 0; count <= Highest_room_index; count++)
2978 // {
2979 // if(Rooms[count].used)
2980 // {
2981 // mprintf(0, "==================\n", count);
2982 // mprintf(0, "Room %d\n", count);
2983 //
2984 // room *rp = &Rooms[count];
2985 // for(i = 0; i < rp->num_bbf_regions; i++)
2986 // {
2987 // mprintf(0, "Region %d has %d faces in it.\n", i, rp->num_bbf[i]);
2988 // }
2989 // }
2990 // }
2991
2992 for (i = 0; i < MAX_ROOMS; i++) {
2993 BOA_AABB_ROOM_checksum[i] = computed_room_check[i];
2994 }
2995 }
2996
2997 // I had to add it :( Rooms need this done even the checksum is correct
2998 for (i = 0; i <= Highest_room_index; i++) {
2999 if (Rooms[i].used) {
3000 room *rp = &Rooms[i];
3001
3002 rp->max_xyz.x = rp->max_xyz.y = rp->max_xyz.z = -9999999.0;
3003 rp->min_xyz.x = rp->min_xyz.y = rp->min_xyz.z = 9999999.0;
3004
3005 for (int t = 0; t < rp->num_faces; t++) {
3006 face *fp = &rp->faces[t];
3007
3008 if (fp->max_xyz.x > rp->max_xyz.x)
3009 rp->max_xyz.x = fp->max_xyz.x;
3010
3011 if (fp->max_xyz.y > rp->max_xyz.y)
3012 rp->max_xyz.y = fp->max_xyz.y;
3013
3014 if (fp->max_xyz.z > rp->max_xyz.z)
3015 rp->max_xyz.z = fp->max_xyz.z;
3016
3017 if (fp->min_xyz.x < rp->min_xyz.x)
3018 rp->min_xyz.x = fp->min_xyz.x;
3019
3020 if (fp->min_xyz.y < rp->min_xyz.y)
3021 rp->min_xyz.y = fp->min_xyz.y;
3022
3023 if (fp->min_xyz.z < rp->min_xyz.z)
3024 rp->min_xyz.z = fp->min_xyz.z;
3025 }
3026 }
3027 }
3028
3029 for (i = 0; i <= Highest_object_index; i++) {
3030 if (Objects[i].type != OBJ_NONE) {
3031 if (!(Objects[i].flags & OF_BIG_OBJECT))
3032 if (Objects[i].size >= MIN_BIG_OBJ_RAD)
3033 BigObjAdd(i);
3034
3035 if (Objects[i].type == OBJ_ROOM) {
3036 Objects[i].min_xyz = Rooms[Objects[i].id].min_xyz;
3037 Objects[i].max_xyz = Rooms[Objects[i].id].max_xyz;
3038 } else {
3039 ObjSetAABB(&Objects[i]);
3040 ObjSetOrient(&Objects[i], &Objects[i].orient);
3041 }
3042 }
3043 }
3044
3045 mprintf(0, "Done Computing AABB's.\n");
3046 }