-
Notifications
You must be signed in to change notification settings - Fork 5
/
boa.cpp
2790 lines (2202 loc) · 84.7 KB
/
boa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF OUTRAGE
ENTERTAINMENT, INC. ("OUTRAGE"). OUTRAGE, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1996-2000 OUTRAGE ENTERTAINMENT, INC. ALL RIGHTS RESERVED.
*/
#ifdef EDITOR
#include "editor\d3edit.h"
#endif
#ifdef NEWEDITOR
#include "neweditor\globals.h"
#endif
#include "BOA.h"
#include "vecmat.h"
#include "room.h"
#include <string.h>
#include <stdlib.h>
#include <search.h>
#include "object.h"
#include "bsp.h"
#include "pserror.h"
#include "FindIntersection.h"
#include "mem.h"
#include "doorway.h"
#include "string.h"
#define BOA_VERSION 25
const ubyte bbf_lookup[27] = {(0),
(0x01),
(0x02),
(0x04),
(0x08),
(0x10),
(0x20),
(0x01 | 0x02),
(0x01 | 0x04),
(0x01 | 0x10),
(0x01 | 0x20),
(0x02 | 0x04),
(0x02 | 0x08),
(0x02 | 0x20),
(0x04 | 0x08),
(0x04 | 0x10),
(0x08 | 0x10),
(0x08 | 0x20),
(0x10 | 0x20),
(0x01 | 0x02 | 0x04),
(0x01 | 0x02 | 0x20),
(0x01 | 0x04 | 0x10),
(0x01 | 0x10 | 0x20),
(0x08 | 0x02 | 0x04),
(0x08 | 0x02 | 0x20),
(0x08 | 0x04 | 0x10),
(0x08 | 0x10 | 0x20)};
unsigned short BOA_Array[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS][MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
float BOA_cost_array[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS][MAX_PATH_PORTALS];
int BOA_mine_checksum = 0;
int BOA_vis_checksum = 0; // this checksum is for the VIS bit of the boa array
bool BOA_vis_valid = 0; // Is the vis table up to date and valid to use?
int BOA_AABB_checksum = 0;
int BOA_AABB_ROOM_checksum[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
bool BOA_f_making_boa = false;
int BOA_num_mines = 0;
int BOA_num_terrain_regions = 0;
int BOA_num_connect[MAX_BOA_TERRAIN_REGIONS];
connect_data BOA_connect[MAX_BOA_TERRAIN_REGIONS][MAX_PATH_PORTALS];
void ComputeBOAVisFaceUpperLeft(room *rp, face *fp, vector *upper_left, float *xdiff, float *ydiff, vector *center);
bool BOA_PassablePortal(int room, int portal_index, bool f_for_sound, bool f_making_robot_path_invalid_list) {
if (room == -1) {
return false;
}
room = BOA_INDEX(room);
if (room > Highest_room_index && room <= Highest_room_index + BOA_num_terrain_regions) {
int tr;
tr = room - Highest_room_index - 1;
// Inside room/portal
int temp_room = BOA_connect[tr][portal_index].roomnum;
int temp_portal_index = BOA_connect[tr][portal_index].portal;
// External room/portal
room = Rooms[temp_room].portals[temp_portal_index].croom;
portal_index = Rooms[temp_room].portals[temp_portal_index].cportal;
}
ASSERT(room >= 0 && room <= Highest_room_index && Rooms[room].used);
face *fp = &Rooms[room].faces[Rooms[room].portals[portal_index].portal_face];
if (Rooms[room].portals[portal_index].croom < 0)
return false;
if (!BOA_f_making_boa) {
if (BOA_cost_array[room][portal_index] < 0.0f && !(room <= Highest_room_index && (Rooms[room].flags & RF_EXTERNAL)))
return false;
if (!f_for_sound) {
if (Rooms[room].portals[portal_index].flags & PF_TOO_SMALL_FOR_ROBOT)
return false;
}
if ((Rooms[room].portals[portal_index].flags & PF_RENDER_FACES) &&
!(Rooms[room].portals[portal_index].flags & PF_RENDERED_FLYTHROUGH) ||
(Rooms[room].portals[portal_index].flags & PF_BLOCK)) {
return false;
}
} else {
if (f_making_robot_path_invalid_list) {
if (Rooms[room].portals[portal_index].flags & PF_TOO_SMALL_FOR_ROBOT)
return false;
}
if ((Rooms[room].portals[portal_index].flags & PF_BLOCK) &&
!(Rooms[room].portals[portal_index].flags & PF_BLOCK_REMOVABLE))
return false;
if ((Rooms[room].portals[portal_index].flags & PF_RENDER_FACES) &&
!(Rooms[room].portals[portal_index].flags & PF_RENDERED_FLYTHROUGH)) {
if (!(GameTextures[fp->tmap].flags & (TF_BREAKABLE | TF_FORCEFIELD))) {
return false;
}
}
}
return true;
}
extern object *GetDoorObject(room *rp);
int BOA_DetermineStartRoomPortal(int start_room, vector *start_pos, int end_room, vector *end_pos, bool f_for_sound,
bool f_making_robot_path_invalid_list, int *blocked_portal) {
int i;
if (start_room > Highest_room_index && end_room > Highest_room_index)
return -1;
start_room = BOA_INDEX(start_room);
end_room = BOA_INDEX(end_room);
if (start_room <= Highest_room_index) {
for (i = 0; i < Rooms[start_room].num_portals; i++) {
if (!BOA_PassablePortal(start_room, i, f_for_sound, f_making_robot_path_invalid_list))
continue;
if (end_room <= Highest_room_index) {
if (Rooms[start_room].portals[i].croom == end_room)
break;
} else {
if (Rooms[Rooms[start_room].portals[i].croom].flags & RF_EXTERNAL) {
int cell = GetTerrainCellFromPos(&Rooms[start_room].portals[i].path_pnt);
if (Highest_room_index + TERRAIN_REGION(cell) + 1 == end_room)
break;
}
}
}
if (i >= Rooms[start_room].num_portals)
i = -1;
} else {
for (i = 0; i < BOA_num_connect[start_room - Highest_room_index - 1]; i++) {
ASSERT(end_room <= Highest_room_index);
if (BOA_connect[start_room - Highest_room_index - 1][i].roomnum == end_room) {
int next_portal = BOA_connect[start_room - Highest_room_index - 1][i].portal;
int external_room = Rooms[end_room].portals[next_portal].croom;
int external_portal = Rooms[end_room].portals[next_portal].cportal;
if (BOA_PassablePortal(external_room, external_portal, f_for_sound, f_making_robot_path_invalid_list)) {
break;
}
}
}
if (i >= BOA_num_connect[start_room - Highest_room_index - 1])
i = -1;
}
return i;
}
bool BOA_ComputeMinDist(int start_room, int end_room, float max_check_dist, float *dist, int *num_blockages) {
*dist = 0.0f;
start_room = BOA_INDEX(start_room);
end_room = BOA_INDEX(end_room);
if (start_room == end_room) {
return true;
}
if (start_room == Highest_room_index + 1 && end_room > Highest_room_index) {
return true;
}
if (end_room == Highest_room_index + 1 && start_room > Highest_room_index) {
return true;
}
int cur_room = end_room;
int last_room;
if (start_room < 0 || end_room < 0) {
return false;
}
if (start_room > Highest_room_index + BOA_num_terrain_regions ||
end_room > Highest_room_index + BOA_num_terrain_regions) {
return false;
}
if (start_room <= Highest_room_index && !Rooms[start_room].used)
return false;
if (end_room <= Highest_room_index && !Rooms[end_room].used)
return false;
do {
last_room = cur_room;
if (cur_room <= Highest_room_index && num_blockages && (Rooms[cur_room].flags & RF_DOOR) &&
(cur_room != end_room)) {
float door_position = DoorwayGetPosition(&Rooms[cur_room]);
*num_blockages += 1.0f - door_position;
}
cur_room = BOA_NEXT_ROOM(cur_room, start_room);
int last_portal;
if (last_room == cur_room || cur_room == BOA_NO_PATH)
return false;
if (BOA_INDEX(last_room) != BOA_INDEX(cur_room)) {
last_portal = BOA_DetermineStartRoomPortal(last_room, NULL, cur_room, NULL);
}
if (last_room == end_room) {
int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
if (cur_room != start_room)
*dist += BOA_cost_array[cur_room][this_portal];
if (max_check_dist > 0.0 && max_check_dist < *dist)
return false;
} else if (cur_room == start_room) {
*dist += BOA_cost_array[last_room][last_portal];
if (max_check_dist > 0.0 && max_check_dist < *dist)
return false;
} else if ((cur_room != last_room) && (cur_room != BOA_NO_PATH)) {
int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
*dist += BOA_cost_array[last_room][last_portal] + BOA_cost_array[cur_room][this_portal];
if (max_check_dist > 0.0f && max_check_dist < *dist)
return false;
}
} while ((cur_room != start_room) && (cur_room != last_room) && (cur_room != BOA_NO_PATH));
if (cur_room == BOA_NO_PATH) {
return false;
}
return true;
}
bool BOA_IsVisible(int start_room, int end_room) {
int s_index = start_room;
int e_index = end_room;
if (!BOA_vis_valid)
return true;
if (start_room == end_room)
return true;
if (start_room == -1 || end_room == -1) {
return false;
}
if ((!ROOMNUM_OUTSIDE(s_index)) && s_index <= Highest_room_index) {
if (!Rooms[s_index].used) {
return false;
}
} else if (ROOMNUM_OUTSIDE(s_index)) {
s_index = TERRAIN_REGION(start_room) + Highest_room_index + 1;
} else {
ASSERT(s_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
}
if ((!ROOMNUM_OUTSIDE(e_index)) && e_index <= Highest_room_index) {
if (!Rooms[e_index].used) {
return false;
}
} else if (ROOMNUM_OUTSIDE(e_index)) {
e_index = TERRAIN_REGION(end_room) + Highest_room_index + 1;
} else {
ASSERT(e_index <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
}
return ((BOA_Array[s_index][e_index] & BOAF_VIS) != 0);
}
void add_mine_room(int room, int mine, char *checked) {
int i;
Rooms[room].flags |= (mine << 20);
checked[room] = 1;
for (i = 0; i < Rooms[room].num_portals; i++) {
if (Rooms[room].portals[i].croom >= 0 && !checked[Rooms[room].portals[i].croom]) {
add_mine_room(Rooms[room].portals[i].croom, mine, checked);
}
}
}
void compute_mine_info() {
int i;
char checked[MAX_ROOMS];
bool done = false;
int first_free;
int cur_mine = 0;
for (i = 0; i <= Highest_room_index; i++) {
room *rp = &Rooms[i];
if (rp->used) {
rp->flags &= ~RFM_MINE;
}
checked[i] = 0;
}
while (!done) {
ASSERT(cur_mine < 32);
done = true;
for (i = 0; i <= Highest_room_index; i++) {
room *rp = &Rooms[i];
if (rp->used && !checked[i]) {
first_free = i;
done = false;
break;
}
}
if (!done) {
add_mine_room(first_free, cur_mine, checked);
cur_mine++;
}
}
BOA_num_mines = cur_mine;
}
void add_terrain_cell(int cell, int t_region, char *checked) {
int depth = 0;
int i;
#ifdef MACINTOSH // JCA: Mac compiler cannot allocate more than 32k on stack as local variables
unsigned short *stack;
char *on_stack;
stack = (unsigned short *)malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(unsigned short));
on_stack = (char *)malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(char));
#else
unsigned short stack[TERRAIN_WIDTH * TERRAIN_DEPTH];
char on_stack[TERRAIN_WIDTH * TERRAIN_DEPTH];
#endif
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
on_stack[i] = false;
stack[depth++] = cell;
on_stack[cell] = true;
while (depth > 0) {
cell = stack[--depth];
Terrain_seg[cell].flags |= (t_region << 5);
checked[cell] = 1;
int xcounter, ycounter;
int xstart = cell % TERRAIN_WIDTH - 1;
int xend = cell % TERRAIN_WIDTH + 1;
int ystart = cell / TERRAIN_WIDTH - 1;
int yend = cell / TERRAIN_WIDTH + 1;
if (xstart < 0)
xstart = 0;
if (xend >= TERRAIN_WIDTH)
xend = TERRAIN_WIDTH - 1;
if (ystart < 0)
ystart = 0;
if (yend >= TERRAIN_DEPTH)
yend = TERRAIN_DEPTH - 1;
// This should be a faster interative why to do a square with center at original position
int cur_node = TERRAIN_WIDTH * ystart + xstart;
int next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1;
for (ycounter = ystart; ycounter <= yend; ycounter++) {
for (xcounter = xstart; xcounter <= xend; xcounter++) {
if (!on_stack[cur_node] && !checked[cur_node] && Terrain_seg[cur_node].y < MAX_TERRAIN_HEIGHT) {
stack[depth++] = cur_node;
on_stack[cur_node] = true;
}
cur_node += 1;
}
cur_node += next_y_delta;
}
}
#ifdef MACINTOSH
free(stack);
free(on_stack);
#endif
}
void compute_terrain_region_info() {
int i;
bool done = false;
bool f_warning = false;
#ifdef MACINTOSH // JCA: Mac compiler cannot allocate more than 32k on stack as local variables
char *checked;
checked = (char *)malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(char));
#else
char checked[TERRAIN_WIDTH * TERRAIN_DEPTH];
#endif
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
Terrain_seg[i].flags &= (~TFM_REGION_MASK);
ASSERT((Terrain_seg[i].flags & TFM_REGION_MASK) == 0);
checked[i] = 0;
}
#ifdef _DEBUG
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
ASSERT((Terrain_seg[i].flags & TFM_REGION_MASK) == 0);
ASSERT(((Terrain_seg[i].flags & TFM_REGION_MASK) >> 5) == 0);
}
#endif
// Find saturated points
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
if (Terrain_seg[i].y > MAX_TERRAIN_HEIGHT - 6.0f) {
checked[i] = 1;
}
}
char t_region = 1;
while (!done) {
if (t_region >= MAX_BOA_TERRAIN_REGIONS) {
#ifdef EDITOR
OutrageMessageBox("This terrain has too many regions!\nAI will not work correctly outside!\nSee Chris and-or "
"saturate useless areas!");
#endif
break;
}
int first_free;
done = true;
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
if (!checked[i]) {
first_free = i;
done = false;
break;
}
}
if (!done) {
add_terrain_cell(first_free, t_region, checked);
t_region++;
}
}
BOA_num_terrain_regions = t_region;
#ifdef _DEBUG
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
ASSERT(TERRAIN_REGION(i) < BOA_num_terrain_regions);
}
#endif
for (i = 0; i < MAX_BOA_TERRAIN_REGIONS; i++) {
BOA_num_connect[i] = 0;
}
for (i = 0; i <= Highest_room_index; i++) {
if ((Rooms[i].used) && (Rooms[i].flags & RF_EXTERNAL)) {
int j;
for (j = 0; j < Rooms[i].num_portals; j++) {
int cell = GetTerrainCellFromPos(&Rooms[i].portals[j].path_pnt);
int region = TERRAIN_REGION(cell);
if (!(BOA_num_connect[region] < MAX_PATH_PORTALS)) {
f_warning = true;
break;
}
// if(region != 0)
// {
// BOA_connect[0][BOA_num_connect[region]].roomnum =
//Rooms[i].portals[j].croom; BOA_connect[0][BOA_num_connect[region]].portal = Rooms[i].portals[j].cportal;
// }
if (!(Rooms[Rooms[i].portals[j].croom].flags & RF_EXTERNAL)) {
if (BOA_PassablePortal(i, j)) {
BOA_connect[region][BOA_num_connect[region]].roomnum = Rooms[i].portals[j].croom;
BOA_connect[region][BOA_num_connect[region]].portal = Rooms[i].portals[j].cportal;
BOA_num_connect[region]++;
}
}
}
}
}
#ifdef EDITOR
if (f_warning) {
OutrageMessageBox(
"This terrain has too many fly through\nterrain-mine connections!\n\nAI will not work correctly outside!\nIf "
"you really cannot fly outside\nignore this message.\n\nSee Chris for specific instructions!");
}
#endif
#ifdef MACINTOSH
free(checked);
#endif
}
#define MAX_SOUND_PROP_DIST 400.0f
void compute_sound_dist_info() {
int i;
int j;
for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
for (j = 0; j <= i; j++) {
BOA_Array[i][j] |= BOA_SOUND_PROP;
BOA_Array[j][i] |= BOA_SOUND_PROP;
if ((i > Highest_room_index || j > Highest_room_index) && (i != j)) {
BOA_Array[i][j] &= ~BOA_SOUND_PROP;
BOA_Array[j][i] &= ~BOA_SOUND_PROP;
}
}
}
for (i = 0; i <= Highest_room_index; i++) {
for (j = 0; j < i; j++) {
float dist;
bool f_ok = BOA_ComputeMinDist(i, j, MAX_SOUND_PROP_DIST, &dist);
if (!f_ok || dist > MAX_SOUND_PROP_DIST) {
BOA_Array[i][j] &= ~BOA_SOUND_PROP;
BOA_Array[j][i] &= ~BOA_SOUND_PROP;
}
}
}
for (i = 0; i < BOA_num_terrain_regions; i++) {
int j;
int k;
for (j = 0; j < BOA_num_connect[i]; j++) {
int croom = BOA_connect[i][j].roomnum;
for (k = 0; k <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; k++) {
if (BOA_Array[croom][k] & BOA_SOUND_PROP) {
BOA_Array[Highest_room_index + i + 1][k] |= BOA_SOUND_PROP;
BOA_Array[k][Highest_room_index + i + 1] |= BOA_SOUND_PROP;
}
}
}
}
}
void clear_BOA() {
int i, j;
for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
for (j = 0; j <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; j++) {
BOA_Array[i][j] = i; // No flags are set and i to j points to i (so, no path exists)
}
}
for (i = 0; i < MAX_ROOMS; i++) {
BOA_AABB_ROOM_checksum[i] = 0;
}
BOA_num_mines = 0;
BOA_num_terrain_regions = 0;
}
void compute_costs() {
int i, j;
vector from_pnt; //, to_pnt;
vector portal_pnt;
for (i = 0; i <= Highest_room_index; i++) {
if (Rooms[i].used) {
ASSERT(Rooms[i].num_portals <= MAX_PATH_PORTALS);
for (j = 0; j < Rooms[i].num_portals; j++) {
if (BOA_PassablePortal(i, j)) {
ComputeRoomCenter(&from_pnt, &Rooms[i]);
// ComputeRoomCenter(&to_pnt, &Rooms[Rooms[i].portals[j].croom]);
ComputePortalCenter(&portal_pnt, &Rooms[i], j);
BOA_cost_array[i][j] = vm_VectorDistance(&from_pnt, &portal_pnt);
} else {
BOA_cost_array[i][j] = -1.0;
}
}
} else {
for (j = 0; j < Rooms[i].num_portals; j++) {
BOA_cost_array[i][j] = -1.0;
}
}
}
for (i = Highest_room_index + 1; i <= Highest_room_index + BOA_num_terrain_regions; i++) {
int j;
for (j = 0; j < BOA_num_connect[i]; j++) {
BOA_cost_array[i][j] = 100000.0f;
}
}
}
void update_path_info(q_item *node_list[MAX_ROOMS], int start, int end) {
int cur_room;
int par_room;
while (end != start) {
cur_room = end;
par_room = node_list[end]->parent;
while (par_room != -1) {
BOA_Array[par_room][end] = cur_room;
cur_room = node_list[cur_room]->parent;
par_room = node_list[cur_room]->parent;
}
end = node_list[end]->parent;
}
}
void FindPath(int i, int j) {
pq PQPath;
int counter;
q_item *start_node = new q_item(BOA_INDEX(i), -1, 0.0);
q_item *cur_node;
q_item *node_list[MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS];
// mprintf((0, "Find path for %d to %d\n", i, j));
if (i == -1 || j == -1) {
delete start_node;
return;
}
memset(node_list, 0, sizeof(q_item *) * (MAX_ROOMS + MAX_BOA_TERRAIN_REGIONS));
PQPath.push(start_node);
ASSERT(start_node->roomnum <= Highest_room_index + BOA_num_terrain_regions);
while (cur_node = PQPath.pop()) {
node_list[BOA_INDEX(cur_node->roomnum)] = cur_node;
ASSERT(BOA_INDEX(cur_node->roomnum) >= 0 &&
BOA_INDEX(cur_node->roomnum) <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS);
if (cur_node->roomnum == j) {
update_path_info(node_list, i, j);
goto done;
}
if (i > j) {
int num_portals;
bool f_room = true;
int t_index;
if (cur_node->roomnum <= Highest_room_index) {
num_portals = Rooms[cur_node->roomnum].num_portals;
} else {
t_index = cur_node->roomnum - Highest_room_index - 1;
num_portals = BOA_num_connect[t_index];
f_room = false;
}
for (counter = 0; counter < num_portals; counter++) {
int next_room;
q_item *list_item;
float new_cost;
if (!BOA_PassablePortal(cur_node->roomnum, counter))
continue;
if (f_room)
next_room = Rooms[cur_node->roomnum].portals[counter].croom;
else
next_room = BOA_connect[t_index][counter].roomnum;
if (next_room < 0 || next_room == BOA_NO_PATH)
continue;
if ((next_room <= Highest_room_index) && (Rooms[next_room].flags & RF_EXTERNAL)) {
ASSERT(cur_node->roomnum <= Highest_room_index);
int cell = GetTerrainCellFromPos(&Rooms[cur_node->roomnum].portals[counter].path_pnt);
ASSERT(cell >= 0 && cell < TERRAIN_WIDTH * TERRAIN_DEPTH);
next_room = Highest_room_index + TERRAIN_REGION(cell) + 1;
ASSERT(next_room <= Highest_room_index + BOA_num_terrain_regions);
}
int next_portal;
if (BOA_INDEX(next_room) != BOA_INDEX(cur_node->roomnum)) {
next_portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_node->roomnum, NULL);
}
new_cost = cur_node->cost + BOA_cost_array[BOA_INDEX(cur_node->roomnum)][counter] +
BOA_cost_array[BOA_INDEX(next_room)][next_portal];
list_item = node_list[BOA_INDEX(next_room)];
if (list_item != NULL && list_item->cost <= new_cost)
continue;
if (list_item == NULL) {
list_item = new q_item(BOA_INDEX(next_room), cur_node->roomnum, new_cost);
node_list[BOA_INDEX(next_room)] = list_item;
PQPath.push(list_item);
ASSERT(list_item->roomnum <= Highest_room_index + BOA_num_terrain_regions);
} else {
list_item->cost = new_cost;
list_item->parent = cur_node->roomnum;
}
}
} else {
int num_portals;
bool f_room = true;
int t_index;
if (cur_node->roomnum <= Highest_room_index) {
num_portals = Rooms[cur_node->roomnum].num_portals;
} else {
t_index = cur_node->roomnum - Highest_room_index - 1;
num_portals = BOA_num_connect[t_index];
f_room = false;
}
for (counter = 0; counter < num_portals; counter++) {
int next_room;
q_item *list_item;
float new_cost;
if (!BOA_PassablePortal(cur_node->roomnum, counter))
continue;
if (f_room)
next_room = Rooms[cur_node->roomnum].portals[counter].croom;
else
next_room = BOA_connect[t_index][counter].roomnum;
if (next_room < 0 || next_room == BOA_NO_PATH)
continue;
if ((next_room <= Highest_room_index) && (Rooms[next_room].flags & RF_EXTERNAL)) {
ASSERT(cur_node->roomnum <= Highest_room_index);
next_room = Highest_room_index +
TERRAIN_REGION(GetTerrainCellFromPos(&Rooms[cur_node->roomnum].portals[counter].path_pnt)) + 1;
}
int next_portal;
if (BOA_INDEX(next_room) != BOA_INDEX(cur_node->roomnum)) {
next_portal = BOA_DetermineStartRoomPortal(next_room, NULL, cur_node->roomnum, NULL);
}
new_cost = cur_node->cost + BOA_cost_array[BOA_INDEX(cur_node->roomnum)][counter] +
BOA_cost_array[BOA_INDEX(next_room)][next_portal];
list_item = node_list[BOA_INDEX(next_room)];
if (list_item != NULL && list_item->cost <= new_cost)
continue;
if (list_item == NULL) {
list_item = new q_item(BOA_INDEX(next_room), cur_node->roomnum, new_cost);
node_list[BOA_INDEX(next_room)] = list_item;
PQPath.push(list_item);
ASSERT(list_item->roomnum <= Highest_room_index + BOA_num_terrain_regions);
} else {
list_item->cost = new_cost;
list_item->parent = cur_node->roomnum;
}
}
}
}
// Mark as an impossible path.
BOA_Array[i][j] = BOA_NO_PATH;
// mprintf((0, "Found an impossible path\n"));
done:
for (counter = 0; counter <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; counter++) {
if (node_list[counter])
delete node_list[counter];
}
return;
}
void compute_next_segs() {
int i, j;
for (i = 0; i <= Highest_room_index + MAX_BOA_TERRAIN_REGIONS; i++) {
if (i <= Highest_room_index && (!Rooms[i].used))
continue;
if (i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
continue;
if (i > Highest_room_index + BOA_num_terrain_regions)
continue;
for (j = Highest_room_index + MAX_BOA_TERRAIN_REGIONS; j >= 0; j--) {
if (j <= Highest_room_index && (!Rooms[j].used))
continue;
if (j <= Highest_room_index && (Rooms[j].flags & RF_EXTERNAL))
continue;
if (j > Highest_room_index + BOA_num_terrain_regions)
continue;
if (i == Highest_room_index + 1 && j > Highest_room_index) {
BOA_Array[i][j] = j;
BOA_Array[j][i] = i;
continue;
}
if (j == Highest_room_index + 1 && i > Highest_room_index) {
BOA_Array[i][j] = j;
BOA_Array[j][i] = i;
continue;
}
if (i != j && BOA_Array[i][j] == i)
FindPath(i, j);
if (i != j && BOA_Array[j][i] == j)
FindPath(j, i);
}
}
}
void compute_blockage_info() {
int i, j;
for (i = 0; i <= Highest_room_index + BOA_num_terrain_regions; i++) {
if (i <= Highest_room_index && (!Rooms[i].used))
continue;
if (i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
continue;
if (i > Highest_room_index + BOA_num_terrain_regions)
continue;
for (j = 0; j <= Highest_room_index + BOA_num_terrain_regions; j++) {
int cur_room = i;
if (i == j)
continue;
if (i == Highest_room_index + 1 && j > Highest_room_index)
continue;
if (j == Highest_room_index + 1 && i > Highest_room_index)
continue;
if (j <= Highest_room_index && (!Rooms[i].used))
continue;
if (j <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL))
continue;
if (BOA_NEXT_ROOM(cur_room, j) != BOA_NO_PATH && BOA_NEXT_ROOM(cur_room, j) != cur_room) {
int last_room = cur_room;
do {
if (cur_room <= Highest_room_index && (Rooms[cur_room].flags & RF_DOOR)) {
BOA_Array[i][j] |= BOAF_BLOCKAGE;
break;
}
last_room = cur_room;
cur_room = BOA_NEXT_ROOM(cur_room, j);
if (last_room != cur_room) {
BOA_f_making_boa = false;
if (BOA_DetermineStartRoomPortal(last_room, NULL, cur_room, NULL, true) == -1) {
BOA_Array[i][j] |= BOAF_BLOCKAGE;
BOA_f_making_boa = true;
break;
}
BOA_f_making_boa = true;
}
} while (cur_room != j);
}
}
}
}
// Goes through all the valid points in the indoor engine and returns a unique
// checksum
#pragma optimize("", off)
int BOAGetMineChecksum() {
int i, t, k;
int total = 0;
for (i = 0; i <= Highest_room_index; i++) {
room *rp = &Rooms[i];
if (!Rooms[i].used)
continue;
for (t = 0; t < rp->num_faces; t++) {
face *fp = &rp->faces[t];
for (k = 0; k < fp->num_verts; k++) {
total += rp->verts[fp->face_verts[k]].x;
total += rp->verts[fp->face_verts[k]].y;
total += rp->verts[fp->face_verts[k]].z;
}
total += fp->num_verts << 4;
if (fp->portal_num != -1) {
// factor in portal
portal *pp = &rp->portals[fp->portal_num];
total += fp->portal_num;
int flags = pp->flags;
flags &= (PF_BLOCK | PF_BLOCK_REMOVABLE | PF_RENDERED_FLYTHROUGH);
total += flags;
}
}
total += rp->num_faces << 8;
total += (rp->num_portals) << (i % 16);
total += i;
}
// Now do terrain
for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
total += Terrain_seg[i].ypos;
total += BOA_VERSION << 24;
mprintf((0, "Computed Checksum is %d\n", (int)total));
return total;
}
int BOAGetRoomChecksum(int i) {
int t, k;
float total = 0;
{
room *rp = &Rooms[i];