-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisi.c
1703 lines (1525 loc) · 41.1 KB
/
visi.c
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
/*
* V I S I . C
* The X Men, June 1996
* Copyright (c) 1996 Probe Entertainment Limited
* All Rights Reserved
*
* $Revision: 96 $
*
* $Header: /PcProjectX/visi.c 96 5/11/98 3:34p Oliverc $
*
* $Log: /PcProjectX/visi.c $
*
* 96 5/11/98 3:34p Oliverc
* Made Z_TRICK work as it should (but left disabled for patch beta 4 as
* it messes up on translucent external views in certain levels)
*
* 95 20/08/98 4:04p Oliverc
* Added SetViewportError() debug function (enabled on DEBUG_VIEWPORT
* build switch)
*
* 94 17/08/98 18:00 Philipy
* removed loads of unreferenced local variables
*
* 93 24/07/98 16:03 Phillipd
*
* 92 6/11/98 9:29a Phillipd
*
* 91 28/04/98 9:50 Oliverc
* Enabled debug rays for non-FINAL_RELEASE version
*
* 90 6/04/98 11:32 Collinsd
* Added show restart zone to debug menu.
*
* 89 3/04/98 18:10 Collinsd
* Moved position of xmem.h
*
* 88 23/03/98 9:45 Philipy
* fixed corruption bug in AddIndirectVisible
*
* 87 7/03/98 20:05 Collinsd
* Bike Glows now on option and debug rays no longer loaded.
*
* 86 3/03/98 17:00 Oliverc
* New multiplayer CTF mode stuff (1st attempt)
*
* 85 21/02/98 19:44 Oliverc
* Added optimisation: portals not visible if camera behind them
*
* 84 20/02/98 19:41 Oliverc
* 2nd prototype of capture the flag game
*
* 83 20/02/98 12:30 Oliverc
* Prototype goal load/release/check/display for capture the flag
* multiplayer
*
* 82 26/01/98 17:49 Oliverc
* Moved calculation of clip planes to once per frame instead of once per
* portal vertex (!) and optimised clip plane test
*
* 81 24/01/98 18:42 Oliverc
* 2D portal extent now considered empty is min >= max (rather than just
* >)
*
* 80 1/24/98 5:10p Phillipd
*
* 79 1/23/98 11:10a Phillipd
* malloc Bug fixed
*
* 78 22/01/98 15:06 Oliverc
* Added loading of pre-calculated group connectivity, visibility,
* indirect visibility, and sound attentuation tables
*
* 77 5/01/98 19:04 Collinsd
* Olly fixed visiblegroups list.
*
* 76 20/12/97 17:41 Oliverc
* 1st attempt at fixing portal Z-clipping bug
*
* 75 1/12/97 10:51 Oliverc
* Fixed memory alloced but not freed bugs
*
* 74 11/29/97 4:36p Phillipd
* Xmem is now in effect...use it allways....
*
* 73 7/11/97 9:24 Collinsd
* Added point in group bounding box function
*
* 72 6/11/97 19:03 Collinsd
* Find Overlapping Visible Groups function Added.
*
* 71 30/10/97 17:00 Collinsd
* Clipping of BGObjects now works.
*
* 70 30/10/97 11:20 Oliverc
* Added FindClipGroup() function for clipping objects that span multiple
* groups
*
* 69 24/10/97 10:37 Philipy
* Updated debug menus
*
* 68 24/10/97 10:01 Collinsd
* Added display of all zones/forces.
*
* 67 23/10/97 14:22 Oliverc
* Changed lights to check visibility between displayed group and group
* light is in
*
* 66 23/10/97 13:52 Collinsd
* Added code to enable/disable compilation of software version.
* SOFTWARE_ENABLE & softblit.lib.
*
* 65 22/10/97 19:27 Collinsd
* Fixed bug in debug code....
*
* 64 22/10/97 15:48 Collinsd
* Ooopss
*
* 63 22/10/97 15:37 Collinsd
* Added error checking in secondary missile collision.
*
* 62 21/10/97 13:14 Philipy
* MAXLEVELS is now defined in main.h
*
* 61 15/10/97 19:06 Collinsd
* Fixed guts comming out bug. and fixed mutually visible group bug.
*
* 60 26/09/97 11:13 Oliverc
* Added function to test visible overlap between two groups
*
* 59 25/09/97 16:58 Collinsd
* Added group link list to pickups/secondary added more code for
* bgobjects.
*
* 58 25/09/97 16:05 Oliverc
* Added group connectivity and visibility functions
*
* 57 22/09/97 10:40 Collinsd
* Software version works again. ( Now with trasnsluecency )
*
* 56 18/09/97 11:09 Collinsd
* Added Chris's 565 stretch.
*
* 55 17/09/97 16:37 Collinsd
* Blit now works in software.
*
* 54 16/09/97 17:52 Collinsd
* More of Chris's stuff works.
*
* 53 16/09/97 11:00 Collinsd
* Added Chris's code
*
* 52 15/09/97 10:46 Oliverc
* Started to fix portal clipping bug
*
* 51 12/09/97 10:50 Collinsd
* Added global portal clipping on/off using shift f7
*
* 50 2/09/97 17:37 Oliverc
* Added debug lines display from .RAY file
*
* 49 20/08/97 12:18 Oliverc
* Triangle visibility statistics now kept (min, max, avg) for all
* background groups and written to .VIS file in level folder iff player
* using vanilla '-data' command line option to indicate he wants to use
* PROJXDATA environment variable
*
* 48 17/07/97 15:38 Collinsd
* BGObjects now use compobjs.
*
* 47 16/07/97 11:31 Collinsd
* Added collision zone display.
*
* 46 15/07/97 10:11 Collinsd
* Added display of triggerzones on Shift F10 in debug mode.
*
* 45 8/07/97 16:30 Collinsd
* Dicked about with include files FUCK!
*
* 44 5/07/97 16:31 Collinsd
* Put OPT_ON's around opimisations off
*
* 43 6/24/97 11:12a Phillipd
*
* 42 6/12/97 2:27p Phillipd
*
* 41 5/22/97 9:19a Phillipd
*
* 40 26/04/97 14:49 Collinsd
* Optimisations now on def.
*
* 39 27-02-97 2:08p Collinsd
* Added optimisation to various files.
*
* 38 15-02-97 9:32p Collinsd
* Portals now use variable execute buffers. They also
* allocate/deallocate themselves properly now.
*
* 37 14-02-97 11:27a Collinsd
* Added check for portal display code overflow
*
* 36 13/02/97 18:00 Oliverc
* Fixed bug in whole level display when outside map
*
* 35 12/02/97 12:54 Oliverc
* Clears turned on automatically when whole level displayed
*
* 34 11/02/97 21:00 Oliverc
* Now displays all groups when go outside map
*
* 33 2/03/97 5:16p Phillipd
* Translusceny is now controlled by global execute buffers.... which is
* much better...
*
* 32 22-01-97 6:00p Collinsd
* Added debug node cube.
*
* 31 12/27/96 3:38p Phillipd
* Primary.h Secondary.h pickups.h are now clean....
* Still Lots to do though.....
*
* 30 12/27/96 12:34p Phillipd
* all files are not dependant on mydplay.h...just some..
* including it several times in the same files didnt help..
*
* 29 5/12/96 16:17 Oliverc
* Integrated new portal format (portalpolys grouped into logical portals
* and converted into preprojected collisionpolys), BUT bug in portal
* transition routine remains to be fixed
*
* 28 11/21/96 2:37p Phillipd
*
* 27 4/11/96 10:45 Oliverc
* Changed display routines to clip to visible portal boundaries of each
* group
*
* 26 17/10/96 12:40 Collinsd
* Added blend types to all transparent exec lists.
*
* 25 10/15/96 2:21p Phillipd
*
* 24 11/10/96 10:31 Oliverc
* Revised portal clipping to work with odd main window sizes
*
* 23 10/10/96 21:23 Oliverc
* Fixed portal clipping bug (but not entirely sure why it works...)
*
* 22 10/07/96 11:54a Phillipd
*
* 21 10/06/96 5:04p Phillipd
* We now have our own text debug print info thing...
* which speeds up the game by 10%.....
*
* 20 10/05/96 2:02p Phillipd
*
* 19 10/04/96 10:32a Phillipd
*
* 18 9/18/96 5:35p Phillipd
*
* 17 11/09/96 19:00 Collinsd
* Added tests for RAMP emulation ( Although may not work)
* Also fixed gravity effect to be dependent on GLOBAL_SCALE
*
* 16 30/08/96 17:31 Collinsd
* Fixed bug in rgba colours ( Cheers Dan ).
*
* 15 28/08/96 11:55 Collinsd
* Added show skin code + more options for show visi.
*
* 14 28/08/96 11:15 Collinsd
*
* 13 8/16/96 2:43p Phillipd
*
* 12 8/09/96 2:02p Phillipd
*
* 11 8/08/96 5:39p Phillipd
*
* 10 7/29/96 12:16p Phillipd
*
* 9 7/24/96 9:51a Phillipd
*
* 8 23/07/96 18:01 Collinsd
* Added visipoly line mode and group in mode.
*
* 7 7/23/96 2:38p Phillipd
*
* 6 22/07/96 14:47 Collinsd
*
* 5 22/07/96 14:08 Collinsd
* Added visipoly display.
*
* 4 7/21/96 4:27p Phillipd
* added asynchrinus(??) execution ..so lights happen at the same time as
* the last group is being displayed...
*
* 3 19/07/96 12:34 Oliverc
* Changed ship <-> background collision routine
* to track movement of ship through portals
*
* 2 7/16/96 11:11a Phillipd
* moved all visipoly stuff to visi.c and visi.h..
*
* 1 7/16/96 10:58a Phillipd
*/
/*ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
* All routines to do with Visipolys...
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ*/
#include <stdio.h>
#include "typedefs.h"
#include <dplay.h>
#include "new3d.h"
#include "quat.h"
#include "CompObjects.h"
#include "bgobjects.h"
#include "Object.h"
#include "mydplay.h"
#include "mload.h"
#include "camera.h"
#include "visi.h"
#include <float.h>
#include "file.h"
#include "triggers.h"
#include "main.h"
#include "lines.h"
#include "teleport.h"
#include "extforce.h"
#include "goal.h"
#include "title.h"
#include "restart.h"
#include "XMem.h"
#ifdef OPT_ON
#pragma optimize( "gty", on )
#endif
extern void SetViewportError( char *where, D3DVIEWPORT *vp, HRESULT rval );
extern float hfov;
extern float screen_aspect_ratio;
extern int outside_map;
extern BOOL DoClipping;
extern CAMERA CurrentCamera;
/*ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
Externals...
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ*/
extern BOOL CTF;
extern BOOL CaptureTheFlag;
extern MATRIX ProjMatrix;
extern TLOADHEADER Tloadheader;
extern D3DMATRIX proj;
extern D3DMATRIX view;
extern D3DMATRIXHANDLE hProj;
extern D3DMATRIXHANDLE hView;
extern MCLOADHEADER MCloadheader;
extern BOOL UsedStippledAlpha;
extern DWORD CurrentSrcBlend;
extern DWORD CurrentDestBlend;
extern DWORD CurrentTextureBlend;
extern BSP_NODE * OldCollideNode;
extern BOOL PowerVR;
extern BOOL bPolySort;
extern uint16 GroupTris[ MAXGROUPS ];
extern int use_local_data;
extern LINE Lines[ MAXLINES ];
extern char LevelNames[MAXLEVELS][128];
extern MLOADHEADER Mloadheader;
/*ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
Globals...
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ*/
#define MAXPORTVERTS (MAXPORTALSPERGROUP * MAXVERTSPERPORTAL)
#define MAXGROUPSVISIBLE 16
static int GTabRowSize;
#define GROUP2GROUP_OFFSET( G1, G2 ) ( ( (G2) >> 5 ) + ( (G1) * GTabRowSize ) )
#define GROUP2GROUP_MASK( G1, G2 ) ( 1 << ( (G2) & 31 ) )
#define DIV_CEIL( N, D ) ( (int32) ( (N) + (D) - 1 ) / (D) )
typedef struct _GROUPRELATION
{
uint32 *table;
GROUPLIST list[ MAXGROUPS ];
} GROUPRELATION;
static GROUPRELATION ConnectedGroup;
static GROUPRELATION VisibleGroup;
static GROUPRELATION IndirectVisibleGroup;
static BOOL InitConnections = FALSE;
uint16 Num_IndirectVisible = 0;
uint16 IndirectVisible[ MAXGROUPS ];
static VECTOR clip_top, clip_bottom, clip_left, clip_right; // clipping planes
int NumOfVertsConsidered= 0;
int NumOfVertsTouched= 0;
uint16 GroupImIn;
D3DTRANSFORMDATA Data;
DWORD VertexCount = 0;
DWORD Offscreen = 0;
VERT TestVerts[MAXPORTVERTS];
VISPOLVERTEX VisVerts[MAXPORTVERTS];
uint16 CurrentGroupVisible; // the real group thats visible
uint16 GroupInVisibleList; // where it is in the visibility list
uint16 NumGroupsVisible;
uint16 GroupsVisible[MAXGROUPS];
D3DRECT GroupVisibleExtents[MAXGROUPS];
uint16 NumPortalsVisible;
uint16 PortalsVisible[MAXPORTALSPERGROUP];
D3DRECT PortalExtents[MAXGROUPS];
D3DVIEWPORT OldViewPort;
D3DVIEWPORT PresentViewPort;
MATRIX VisPolyMatrix = {
1.0F, 0.0F, 0.0F, 0.0F,
0.0F, 1.0F, 0.0F, 0.0F,
0.0F, 0.0F, 1.0F, 0.0F,
0.0F, 0.0F, 0.0F, 1.0F };
uint16 IsGroupVisible[MAXGROUPS];
struct
{
uint32 tsum;
uint32 tmax;
uint32 tmin;
uint32 visits;
} VisiStats[ MAXGROUPS ];
typedef struct
{
uint16 group;
struct
{
double x, y, z;
} from, to;
} DebugRay;
static void InitDebugRays( void )
{
#if 1
char fname[ 256 ];
FILE *rf;
DebugRay ray;
uint16 line;
Change_Ext( &LevelNames[ LevelNum ][ 0 ], fname, ".ray" );
rf = fopen( fname, "rb" );
if ( !rf )
return;
while ( fread( &ray.group, sizeof( ray.group ), 1, rf ) )
{
if ( !fread( &ray.from, sizeof( double ), 3, rf ) )
break;
if ( !fread( &ray.to, sizeof( double ), 3, rf ) )
break;
line = FindFreeLine();
if ( line != (uint16) -1 )
{
Lines[ line ].StartPos.x = (float) ray.from.x;
Lines[ line ].StartPos.y = (float) ray.from.y;
Lines[ line ].StartPos.z = (float) ray.from.z;
Lines[ line ].EndPos.x = (float) ray.to.x;
Lines[ line ].EndPos.y = (float) ray.to.y;
Lines[ line ].EndPos.z = (float) ray.to.z;
Lines[ line ].StartCol.R = 255;
Lines[ line ].StartCol.G = 64;
Lines[ line ].StartCol.B = 64;
Lines[ line ].EndCol.R = 64;
Lines[ line ].EndCol.G = 255;
Lines[ line ].EndCol.B = 64;
Lines[ line ].StartTrans = 255;
Lines[ line ].EndTrans = 255;
Lines[ line ].Group = ray.group;
}
}
fclose( rf );
#endif
}
void InitVisiStats( MLOADHEADER *m )
{
int j;
GROUP *g;
VECTOR min, max;
for ( j = 0; j < m->num_groups; j++ )
{
VisiStats[ j ].tsum = 0;
VisiStats[ j ].tmax = 0;
VisiStats[ j ].tmin = -1;
VisiStats[ j ].visits = 0;
g = &m->Group[ j ];
min.x = g->center.x - g->half_size.x;
min.y = g->center.y - g->half_size.y;
min.z = g->center.z - g->half_size.z;
max.x = g->center.x + g->half_size.x;
max.y = g->center.y + g->half_size.y;
max.z = g->center.z + g->half_size.z;
// CreateBoundingBox( &min, &max, j );
}
#ifndef FINAL_RELEASE
InitDebugRays();
#endif
}
BOOL OutputVisiStats( MLOADHEADER *m, char *lname )
{
FILE *f;
char fname[ 256 ];
int j, k;
if ( !use_local_data )
return TRUE;
Change_Ext( lname, fname, ".vis" );
f = fopen( fname, "w" );
if ( !f )
return FALSE;
fprintf( f, "Group TMAX TAVG TMIN Visits Name\n" );
for ( j = 0; j < m->num_groups; j++ )
{
fprintf( f, "%s%3d %9d %9d %9d %9d %s\n",
( VisiStats[ j ].tmax > 1000 ) ? "*" : " ",
j, VisiStats[ j ].tmax,
( VisiStats[ j ].visits ) ? VisiStats[ j ].tsum / VisiStats[ j ].visits : 0,
( VisiStats[ j ].visits ) ? VisiStats[ j ].tmin : 0,
VisiStats[ j ].visits,
m->Group[ j ].name );
}
fprintf( f, "\n\nGROUP CONNECTED GROUPS\n" );
for ( j = 0; j < m->num_groups; j++ )
{
fprintf( f, "%-10s %2d", m->Group[ j ].name, ConnectedGroup.list[ j ].groups );
for ( k = 0; k < ConnectedGroup.list[ j ].groups; k++ )
{
fprintf( f, " %s", m->Group[ ConnectedGroup.list[ j ].group[ k ] ].name );
}
fprintf( f, "\n" );
}
fprintf( f, "\n\nGROUP VISIBLE GROUPS\n" );
for ( j = 0; j < m->num_groups; j++ )
{
fprintf( f, "%-10s %2d", m->Group[ j ].name, VisibleGroup.list[ j ].groups );
for ( k = 0; k < VisibleGroup.list[ j ].groups; k++ )
{
fprintf( f, " %s", m->Group[ VisibleGroup.list[ j ].group[ k ] ].name );
}
fprintf( f, "\n" );
}
fclose( f );
return TRUE;
}
static void RelateGroups( GROUPRELATION *rel, uint16 g1, uint16 g2 )
{
uint32 *t, mask;
t = rel->table + GROUP2GROUP_OFFSET( g1, g2 );
mask = GROUP2GROUP_MASK( g1, g2 );
if ( !( *t & mask ) )
{
*t |= mask;
rel->list[ g1 ].groups++;
}
}
static BOOL AreGroupsRelated( GROUPRELATION *rel, uint16 g1, uint16 g2 )
{
uint32 *t, mask;
t = rel->table + GROUP2GROUP_OFFSET( g1, g2 );
mask = GROUP2GROUP_MASK( g1, g2 );
if ( *t & mask )
return TRUE;
else
return FALSE;
}
static void FindGroupsVisible( GROUPRELATION *vis, uint16 from_group, VISTREE *visible )
{
int j;
RelateGroups( vis, from_group, visible->group );
for ( j = 0; j < visible->num_visible; j++ )
{
FindGroupsVisible( vis, from_group, &visible->visible[ j ] );
}
}
BOOL GroupsAreConnected( uint16 g1, uint16 g2 )
{
return AreGroupsRelated( &ConnectedGroup, g1, g2 );
}
BOOL GroupsAreVisible( uint16 g1, uint16 g2 )
{
return AreGroupsRelated( &VisibleGroup, g1, g2 );
}
BOOL GroupsAreIndirectVisible( uint16 g1, uint16 g2 )
{
if ( IndirectVisibleGroup.table )
return AreGroupsRelated( &IndirectVisibleGroup, g1, g2 );
else
{
static int failed = 0;
if ( !failed++ )
Msg( "No IndirectVisible table loaded\n" );
return FALSE;
}
}
BOOL ReadGroupConnections( MLOADHEADER *m, char **pbuf )
{
uint32 tabsize;
uint16 g;
char *buf;
uint16 *buf16;
// initialise data structures to reasonable defaults
for ( g = 0; g < MAXGROUPS; g++ )
{
ConnectedGroup.list[ g ].groups = 0;
ConnectedGroup.list[ g ].group = NULL;
VisibleGroup.list[ g ].groups = 0;
VisibleGroup.list[ g ].group = NULL;
IndirectVisibleGroup.list[ g ].groups = 0;
IndirectVisibleGroup.list[ g ].group = NULL;
}
InitConnections = TRUE;
// allocate tables
GTabRowSize = DIV_CEIL( MAXGROUPS, 32 );
tabsize = m->num_groups * GTabRowSize * sizeof( uint32 );
if ( !ConnectedGroup.table )
{
ConnectedGroup.table = (uint32 *) malloc( tabsize );
if ( !ConnectedGroup.table )
{
Msg( "ReadGroupConnections: failed malloc for ConnectedGroup.table\n" );
return FALSE;
}
}
if ( !VisibleGroup.table )
{
VisibleGroup.table = (uint32 *) malloc( tabsize );
if ( !VisibleGroup.table )
{
Msg( "ReadGroupConnections: failed malloc for VisibleGroup.table\n" );
return FALSE;
}
}
if ( !IndirectVisibleGroup.table )
{
IndirectVisibleGroup.table = (uint32 *) malloc( tabsize );
if ( !IndirectVisibleGroup.table )
{
Msg( "ReadGroupConnections: failed malloc for IndirectVisibleGroup.table\n" );
return FALSE;
}
}
memset( ConnectedGroup.table, 0, tabsize );
memset( VisibleGroup.table, 0, tabsize );
memset( IndirectVisibleGroup.table, 0, tabsize );
buf = *pbuf;
// read connectivity table
memmove( ConnectedGroup.table, buf, tabsize );
buf += tabsize;
buf16 = (uint16 *) buf;
for ( g = 0; g < m->num_groups; g++ )
{
ConnectedGroup.list[ g ].groups = *buf16++;
if ( ConnectedGroup.list[ g ].groups )
{
ConnectedGroup.list[ g ].group = (uint16 *) calloc( ConnectedGroup.list[ g ].groups, sizeof( uint16 ) );
if ( !ConnectedGroup.list[ g ].group )
{
Msg( "ReadGroupConnections: failed X_calloc for ConnectedGroup.list[ %d ]\n", g );
return FALSE;
}
memmove( ConnectedGroup.list[ g ].group, buf16, ConnectedGroup.list[ g ].groups * sizeof( uint16 ) );
buf16 += ConnectedGroup.list[ g ].groups;
}
}
buf = (char *) buf16;
// read visibility summary table
memmove( VisibleGroup.table, buf, tabsize );
buf += tabsize;
buf16 = (uint16 *) buf;
for ( g = 0; g < m->num_groups; g++ )
{
VisibleGroup.list[ g ].groups = *buf16++;
if ( VisibleGroup.list[ g ].groups )
{
VisibleGroup.list[ g ].group = (uint16 *) calloc( VisibleGroup.list[ g ].groups, sizeof( uint16 ) );
if ( !VisibleGroup.list[ g ].group )
{
Msg( "ReadGroupConnections: failed X_calloc for VisibleGroup.list[ %d ]\n", g );
return FALSE;
}
memmove( VisibleGroup.list[ g ].group, buf16, VisibleGroup.list[ g ].groups * sizeof( uint16 ) );
buf16 += VisibleGroup.list[ g ].groups;
}
}
buf = (char *) buf16;
// read indirect visibility summary table
memmove( IndirectVisibleGroup.table, buf, tabsize );
buf += tabsize;
buf16 = (uint16 *) buf;
for ( g = 0; g < m->num_groups; g++ )
{
IndirectVisibleGroup.list[ g ].groups = *buf16++;
if ( IndirectVisibleGroup.list[ g ].groups )
{
IndirectVisibleGroup.list[ g ].group = (uint16 *) calloc( IndirectVisibleGroup.list[ g ].groups, sizeof( uint16 ) );
if ( !IndirectVisibleGroup.list[ g ].group )
{
Msg( "ReadGroupConnections: failed X_calloc for IndirectVisibleGroup.list[ %d ]\n", g );
return FALSE;
}
memmove( IndirectVisibleGroup.list[ g ].group, buf16, IndirectVisibleGroup.list[ g ].groups * sizeof( uint16 ) );
buf16 += IndirectVisibleGroup.list[ g ].groups;
}
}
buf = (char *) buf16;
*pbuf = buf;
return TRUE;
}
BOOL FindGroupConnections( MLOADHEADER *m )
{
uint32 tabsize;
uint16 g, p, g2;
GROUP *group;
PORTAL *portal;
uint16 gnum;
for ( g = 0; g < MAXGROUPS; g++ )
{
ConnectedGroup.list[ g ].groups = 0;
ConnectedGroup.list[ g ].group = NULL;
VisibleGroup.list[ g ].groups = 0;
VisibleGroup.list[ g ].group = NULL;
IndirectVisibleGroup.list[ g ].groups = 0;
IndirectVisibleGroup.list[ g ].group = NULL;
}
InitConnections = TRUE;
GTabRowSize = (int)ceil(MAXGROUPS/32.0F);
tabsize = MAXGROUPS * GTabRowSize * sizeof( uint32 );
if ( !ConnectedGroup.table )
{
ConnectedGroup.table = (uint32 *) malloc( tabsize );
if ( !ConnectedGroup.table )
{
Msg( "FindGroupConnections: failed malloc for ConnectedGroup.table\n" );
return FALSE;
}
}
if ( !VisibleGroup.table )
{
VisibleGroup.table = (uint32 *) malloc( tabsize );
if ( !VisibleGroup.table )
{
Msg( "FindGroupConnections: failed malloc for VisibleGroup.table\n" );
return FALSE;
}
}
IndirectVisibleGroup.table = NULL;
memset( ConnectedGroup.table, 0, tabsize );
memset( VisibleGroup.table, 0, tabsize );
for ( g = 0; g < m->num_groups; g++ )
{
group = &m->Group[ g ];
ConnectedGroup.list[ g ].groups = 0;
VisibleGroup.list[ g ].groups = 0;
// make group visible from itself
RelateGroups( &VisibleGroup, g, g );
for ( p = 0; p < group->num_portals; p++ )
{
portal = &group->Portal[ p ];
// find groups connected to this one
RelateGroups( &ConnectedGroup, g, portal->visible.group );
// find groups visible from this one
FindGroupsVisible( &VisibleGroup, g, &portal->visible );
}
if ( ConnectedGroup.list[ g ].groups )
{
ConnectedGroup.list[ g ].group = (uint16 *) calloc( ConnectedGroup.list[ g ].groups, sizeof( uint16 ) );
if ( !ConnectedGroup.list[ g ].group )
{
Msg( "FindGroupConnections: failed X_calloc for ConnectedGroup.list[ %d ]\n", g );
return FALSE;
}
gnum = 0;
for ( g2 = 0; g2 < m->num_groups; g2++ )
{
if ( GroupsAreConnected( g, g2 ) )
{
ConnectedGroup.list[ g ].group[ gnum++ ] = g2;
}
}
}
else
{
ConnectedGroup.list[ g ].group = NULL;
}
if ( VisibleGroup.list[ g ].groups )
{
VisibleGroup.list[ g ].group = (uint16 *) calloc( VisibleGroup.list[ g ].groups, sizeof( uint16 ) );
if ( !VisibleGroup.list[ g ].group )
{
Msg( "FindGroupConnections: failed X_calloc for VisibleGroup.list[ %d ]\n", g );
return FALSE;
}
gnum = 0;
for ( g2 = 0; g2 < m->num_groups; g2++ )
{
if ( GroupsAreVisible( g, g2 ) )
{
VisibleGroup.list[ g ].group[ gnum++ ] = g2;
}
}
}
else
{
VisibleGroup.list[ g ].group = NULL;
}
}
return TRUE;
}
void FreeGroupConnections( void )
{
int g;
if ( !InitConnections )
return;
for ( g = 0; g < MAXGROUPS; g++ )
{
ConnectedGroup.list[ g ].groups = 0;
if ( ConnectedGroup.list[ g ].group )
{
free( ConnectedGroup.list[ g ].group );
}
ConnectedGroup.list[ g ].group = NULL;
VisibleGroup.list[ g ].groups = 0;
if ( VisibleGroup.list[ g ].group )
{
free( VisibleGroup.list[ g ].group );
}
VisibleGroup.list[ g ].group = NULL;
IndirectVisibleGroup.list[ g ].groups = 0;
if ( IndirectVisibleGroup.list[ g ].group )
{
free( IndirectVisibleGroup.list[ g ].group );
}
IndirectVisibleGroup.list[ g ].group = NULL;
}
if ( ConnectedGroup.table )
{
free( ConnectedGroup.table );
ConnectedGroup.table = NULL;
}
if ( VisibleGroup.table )
{
free( VisibleGroup.table );
VisibleGroup.table = NULL;
}
if ( IndirectVisibleGroup.table )
{
free( IndirectVisibleGroup.table );
IndirectVisibleGroup.table = NULL;
}
}
GROUPLIST *ConnectedGroups( uint16 g )
{
return &ConnectedGroup.list[ g ];
}
GROUPLIST *VisibleGroups( uint16 g )
{
if( g >= Mloadheader.num_groups )
Msg( "VisibleGroups() Group %d out of range", g );
if( VisibleGroup.list[ g ].groups > Mloadheader.num_groups )
Msg( "VisibleGroups.list[%d].groups=%d more than %d groups", g, VisibleGroup.list[ g ].groups, Mloadheader.num_groups );
if( !VisibleGroup.list[ g ].group )
Msg( "VisibleGroups.list[%d].group Invalid", g );
return &VisibleGroup.list[ g ];
}
GROUPLIST *IndirectVisibleGroups( uint16 g )
{
return &IndirectVisibleGroup.list[ g ];
}
int VisibleOverlap( uint16 g1, uint16 g2, uint16 *overlapping_group )
{
uint32 *t1, *t2, overlap;
int j;
int gnum;
GROUPLIST *l1, *l2;
if( g1 > Mloadheader.num_groups || g2 > Mloadheader.num_groups )
return 0;
t1 = VisibleGroup.table + GROUP2GROUP_OFFSET( g1, 0 );
t2 = VisibleGroup.table + GROUP2GROUP_OFFSET( g2, 0 );
overlap = 0;
for ( j = 0; j < GTabRowSize; j++ )
{
overlap |= *t1++ & *t2++;
}
if ( !overlap )
return 0;
if ( !overlapping_group )
return 1;
l1 = &VisibleGroup.list[ g1 ];
l2 = &VisibleGroup.list[ g2 ];
g1 = 0;
g2 = 0;
gnum = 0;
do {
if ( l1->group[ g1 ] < l2->group[ g2 ] )
{
g1++;
}
else if ( l1->group[ g1 ] > l2->group[ g2 ])
{
g2++;
}
else // groups are same
{
overlapping_group[ gnum++ ] = l1->group[ g1 ];
g1++;
g2++;
}
} while ( g1 < l1->groups && g2 < l2->groups );
return gnum;