-
Notifications
You must be signed in to change notification settings - Fork 33
/
aas_store.c
1080 lines (1019 loc) · 32.5 KB
/
aas_store.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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "qbsp.h"
#include "botlib/aasfile.h"
#include "aas_file.h"
#include "aas_store.h"
#include "aas_create.h"
#include "aas_cfg.h"
//#define NOTHREEVERTEXFACES
#define STOREPLANESDOUBLE
#define VERTEX_EPSILON 0.1 //NOTE: changed from 0.5
#define DIST_EPSILON 0.05 //NOTE: changed from 0.9
#define NORMAL_EPSILON 0.0001 //NOTE: changed from 0.005
#define INTEGRAL_EPSILON 0.01
#define VERTEX_HASHING
#define VERTEX_HASH_SHIFT 7
#define VERTEX_HASH_SIZE ((MAX_MAP_BOUNDS>>(VERTEX_HASH_SHIFT-1))+1) //was 64
//
#define PLANE_HASHING
#define PLANE_HASH_SIZE 1024 //must be power of 2
//
#define EDGE_HASHING
#define EDGE_HASH_SIZE 1024 //must be power of 2
aas_t aasworld;
//vertex hash
int *aas_vertexchain; // the next vertex in a hash chain
int aas_hashverts[VERTEX_HASH_SIZE*VERTEX_HASH_SIZE]; // a vertex number, or 0 for no verts
//plane hash
int *aas_planechain;
int aas_hashplanes[PLANE_HASH_SIZE];
//edge hash
int *aas_edgechain;
int aas_hashedges[EDGE_HASH_SIZE];
int allocatedaasmem = 0;
int groundfacesonly = false;//true;
//
typedef struct max_aas_s
{
int max_bboxes;
int max_vertexes;
int max_planes;
int max_edges;
int max_edgeindexsize;
int max_faces;
int max_faceindexsize;
int max_areas;
int max_areasettings;
int max_reachabilitysize;
int max_nodes;
int max_portals;
int max_portalindexsize;
int max_clusters;
} max_aas_t;
//maximums of everything
max_aas_t max_aas;
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int AAS_CountTmpNodes(tmp_node_t *tmpnode)
{
if (!tmpnode) return 0;
return AAS_CountTmpNodes(tmpnode->children[0]) +
AAS_CountTmpNodes(tmpnode->children[1]) + 1;
} //end of the function AAS_CountTmpNodes
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AAS_InitMaxAAS(void)
{
int numfaces, numpoints, numareas;
tmp_face_t *f;
tmp_area_t *a;
numpoints = 0;
numfaces = 0;
for (f = tmpaasworld.faces; f; f = f->l_next)
{
numfaces++;
if (f->winding) numpoints += f->winding->numpoints;
} //end for
//
numareas = 0;
for (a = tmpaasworld.areas; a; a = a->l_next)
{
numareas++;
} //end for
max_aas.max_bboxes = AAS_MAX_BBOXES;
max_aas.max_vertexes = numpoints + 1;
max_aas.max_planes = nummapplanes;
max_aas.max_edges = numpoints + 1;
max_aas.max_edgeindexsize = (numpoints + 1) * 3;
max_aas.max_faces = numfaces + 10;
max_aas.max_faceindexsize = (numfaces + 10) * 2;
max_aas.max_areas = numareas + 10;
max_aas.max_areasettings = numareas + 10;
max_aas.max_reachabilitysize = 0;
max_aas.max_nodes = AAS_CountTmpNodes(tmpaasworld.nodes) + 10;
max_aas.max_portals = 0;
max_aas.max_portalindexsize = 0;
max_aas.max_clusters = 0;
} //end of the function AAS_InitMaxAAS
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AAS_AllocMaxAAS(void)
{
int i;
AAS_InitMaxAAS();
//bounding boxes
aasworld.numbboxes = 0;
aasworld.bboxes = (aas_bbox_t *) GetClearedMemory(max_aas.max_bboxes * sizeof(aas_bbox_t));
allocatedaasmem += max_aas.max_bboxes * sizeof(aas_bbox_t);
//vertexes
aasworld.numvertexes = 0;
aasworld.vertexes = (aas_vertex_t *) GetClearedMemory(max_aas.max_vertexes * sizeof(aas_vertex_t));
allocatedaasmem += max_aas.max_vertexes * sizeof(aas_vertex_t);
//planes
aasworld.numplanes = 0;
aasworld.planes = (aas_plane_t *) GetClearedMemory(max_aas.max_planes * sizeof(aas_plane_t));
allocatedaasmem += max_aas.max_planes * sizeof(aas_plane_t);
//edges
aasworld.numedges = 0;
aasworld.edges = (aas_edge_t *) GetClearedMemory(max_aas.max_edges * sizeof(aas_edge_t));
allocatedaasmem += max_aas.max_edges * sizeof(aas_edge_t);
//edge index
aasworld.edgeindexsize = 0;
aasworld.edgeindex = (aas_edgeindex_t *) GetClearedMemory(max_aas.max_edgeindexsize * sizeof(aas_edgeindex_t));
allocatedaasmem += max_aas.max_edgeindexsize * sizeof(aas_edgeindex_t);
//faces
aasworld.numfaces = 0;
aasworld.faces = (aas_face_t *) GetClearedMemory(max_aas.max_faces * sizeof(aas_face_t));
allocatedaasmem += max_aas.max_faces * sizeof(aas_face_t);
//face index
aasworld.faceindexsize = 0;
aasworld.faceindex = (aas_faceindex_t *) GetClearedMemory(max_aas.max_faceindexsize * sizeof(aas_faceindex_t));
allocatedaasmem += max_aas.max_faceindexsize * sizeof(aas_faceindex_t);
//convex areas
aasworld.numareas = 0;
aasworld.areas = (aas_area_t *) GetClearedMemory(max_aas.max_areas * sizeof(aas_area_t));
allocatedaasmem += max_aas.max_areas * sizeof(aas_area_t);
//convex area settings
aasworld.numareasettings = 0;
aasworld.areasettings = (aas_areasettings_t *) GetClearedMemory(max_aas.max_areasettings * sizeof(aas_areasettings_t));
allocatedaasmem += max_aas.max_areasettings * sizeof(aas_areasettings_t);
//reachablity list
aasworld.reachabilitysize = 0;
aasworld.reachability = (aas_reachability_t *) GetClearedMemory(max_aas.max_reachabilitysize * sizeof(aas_reachability_t));
allocatedaasmem += max_aas.max_reachabilitysize * sizeof(aas_reachability_t);
//nodes of the bsp tree
aasworld.numnodes = 0;
aasworld.nodes = (aas_node_t *) GetClearedMemory(max_aas.max_nodes * sizeof(aas_node_t));
allocatedaasmem += max_aas.max_nodes * sizeof(aas_node_t);
//cluster portals
aasworld.numportals = 0;
aasworld.portals = (aas_portal_t *) GetClearedMemory(max_aas.max_portals * sizeof(aas_portal_t));
allocatedaasmem += max_aas.max_portals * sizeof(aas_portal_t);
//cluster portal index
aasworld.portalindexsize = 0;
aasworld.portalindex = (aas_portalindex_t *) GetClearedMemory(max_aas.max_portalindexsize * sizeof(aas_portalindex_t));
allocatedaasmem += max_aas.max_portalindexsize * sizeof(aas_portalindex_t);
//cluster
aasworld.numclusters = 0;
aasworld.clusters = (aas_cluster_t *) GetClearedMemory(max_aas.max_clusters * sizeof(aas_cluster_t));
allocatedaasmem += max_aas.max_clusters * sizeof(aas_cluster_t);
//
Log_Print("allocated ");
PrintMemorySize(allocatedaasmem);
Log_Print(" of AAS memory\n");
//reset the has stuff
aas_vertexchain = (int *) GetClearedMemory(max_aas.max_vertexes * sizeof(int));
aas_planechain = (int *) GetClearedMemory(max_aas.max_planes * sizeof(int));
aas_edgechain = (int *) GetClearedMemory(max_aas.max_edges * sizeof(int));
//
for (i = 0; i < max_aas.max_vertexes; i++) aas_vertexchain[i] = -1;
for (i = 0; i < VERTEX_HASH_SIZE * VERTEX_HASH_SIZE; i++) aas_hashverts[i] = -1;
//
for (i = 0; i < max_aas.max_planes; i++) aas_planechain[i] = -1;
for (i = 0; i < PLANE_HASH_SIZE; i++) aas_hashplanes[i] = -1;
//
for (i = 0; i < max_aas.max_edges; i++) aas_edgechain[i] = -1;
for (i = 0; i < EDGE_HASH_SIZE; i++) aas_hashedges[i] = -1;
} //end of the function AAS_AllocMaxAAS
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AAS_FreeMaxAAS(void)
{
//bounding boxes
if (aasworld.bboxes) FreeMemory(aasworld.bboxes);
aasworld.bboxes = NULL;
aasworld.numbboxes = 0;
//vertexes
if (aasworld.vertexes) FreeMemory(aasworld.vertexes);
aasworld.vertexes = NULL;
aasworld.numvertexes = 0;
//planes
if (aasworld.planes) FreeMemory(aasworld.planes);
aasworld.planes = NULL;
aasworld.numplanes = 0;
//edges
if (aasworld.edges) FreeMemory(aasworld.edges);
aasworld.edges = NULL;
aasworld.numedges = 0;
//edge index
if (aasworld.edgeindex) FreeMemory(aasworld.edgeindex);
aasworld.edgeindex = NULL;
aasworld.edgeindexsize = 0;
//faces
if (aasworld.faces) FreeMemory(aasworld.faces);
aasworld.faces = NULL;
aasworld.numfaces = 0;
//face index
if (aasworld.faceindex) FreeMemory(aasworld.faceindex);
aasworld.faceindex = NULL;
aasworld.faceindexsize = 0;
//convex areas
if (aasworld.areas) FreeMemory(aasworld.areas);
aasworld.areas = NULL;
aasworld.numareas = 0;
//convex area settings
if (aasworld.areasettings) FreeMemory(aasworld.areasettings);
aasworld.areasettings = NULL;
aasworld.numareasettings = 0;
//reachablity list
if (aasworld.reachability) FreeMemory(aasworld.reachability);
aasworld.reachability = NULL;
aasworld.reachabilitysize = 0;
//nodes of the bsp tree
if (aasworld.nodes) FreeMemory(aasworld.nodes);
aasworld.nodes = NULL;
aasworld.numnodes = 0;
//cluster portals
if (aasworld.portals) FreeMemory(aasworld.portals);
aasworld.portals = NULL;
aasworld.numportals = 0;
//cluster portal index
if (aasworld.portalindex) FreeMemory(aasworld.portalindex);
aasworld.portalindex = NULL;
aasworld.portalindexsize = 0;
//clusters
if (aasworld.clusters) FreeMemory(aasworld.clusters);
aasworld.clusters = NULL;
aasworld.numclusters = 0;
Log_Print("freed ");
PrintMemorySize(allocatedaasmem);
Log_Print(" of AAS memory\n");
allocatedaasmem = 0;
//
if (aas_vertexchain) FreeMemory(aas_vertexchain);
aas_vertexchain = NULL;
if (aas_planechain) FreeMemory(aas_planechain);
aas_planechain = NULL;
if (aas_edgechain) FreeMemory(aas_edgechain);
aas_edgechain = NULL;
} //end of the function AAS_FreeMaxAAS
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
unsigned AAS_HashVec(vec3_t vec)
{
int x, y;
x = (MAX_MAP_BOUNDS + (int)(vec[0]+0.5)) >> VERTEX_HASH_SHIFT;
y = (MAX_MAP_BOUNDS + (int)(vec[1]+0.5)) >> VERTEX_HASH_SHIFT;
if (x < 0 || x >= VERTEX_HASH_SIZE || y < 0 || y >= VERTEX_HASH_SIZE)
{
Log_Print("WARNING! HashVec: point %f %f %f outside valid range\n", vec[0], vec[1], vec[2]);
Log_Print("This should never happen!\n");
return -1;
} //end if
return y*VERTEX_HASH_SIZE + x;
} //end of the function AAS_HashVec
//===========================================================================
// returns true if the vertex was found in the list
// stores the vertex number in *vnum
// stores a new vertex if not stored already
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_GetVertex(vec3_t v, int *vnum)
{
int i;
#ifndef VERTEX_HASHING
float diff;
#endif //VERTEX_HASHING
#ifdef VERTEX_HASHING
int h, vn;
vec3_t vert;
for (i = 0; i < 3; i++)
{
if ( fabs(v[i] - Q_rint(v[i])) < INTEGRAL_EPSILON)
vert[i] = Q_rint(v[i]);
else
vert[i] = v[i];
} //end for
h = AAS_HashVec(vert);
//if the vertex was outside the valid range
if (h == -1)
{
*vnum = -1;
return true;
} //end if
for (vn = aas_hashverts[h]; vn >= 0; vn = aas_vertexchain[vn])
{
if (fabs(aasworld.vertexes[vn][0] - vert[0]) < VERTEX_EPSILON
&& fabs(aasworld.vertexes[vn][1] - vert[1]) < VERTEX_EPSILON
&& fabs(aasworld.vertexes[vn][2] - vert[2]) < VERTEX_EPSILON)
{
*vnum = vn;
return true;
} //end if
} //end for
#else //VERTEX_HASHING
//check if the vertex is already stored
//stupid linear search
for (i = 0; i < aasworld.numvertexes; i++)
{
diff = vert[0] - aasworld.vertexes[i][0];
if (diff < VERTEX_EPSILON && diff > -VERTEX_EPSILON)
{
diff = vert[1] - aasworld.vertexes[i][1];
if (diff < VERTEX_EPSILON && diff > -VERTEX_EPSILON)
{
diff = vert[2] - aasworld.vertexes[i][2];
if (diff < VERTEX_EPSILON && diff > -VERTEX_EPSILON)
{
*vnum = i;
return true;
} //end if
} //end if
} //end if
} //end for
#endif //VERTEX_HASHING
if (aasworld.numvertexes >= max_aas.max_vertexes)
{
Error("AAS_MAX_VERTEXES = %d", max_aas.max_vertexes);
} //end if
VectorCopy(vert, aasworld.vertexes[aasworld.numvertexes]);
*vnum = aasworld.numvertexes;
#ifdef VERTEX_HASHING
aas_vertexchain[aasworld.numvertexes] = aas_hashverts[h];
aas_hashverts[h] = aasworld.numvertexes;
#endif //VERTEX_HASHING
aasworld.numvertexes++;
return false;
} //end of the function AAS_GetVertex
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
unsigned AAS_HashEdge(int v1, int v2)
{
int vnum1, vnum2;
//
if (v1 < v2)
{
vnum1 = v1;
vnum2 = v2;
} //end if
else
{
vnum1 = v2;
vnum2 = v1;
} //end else
return (vnum1 + vnum2) & (EDGE_HASH_SIZE-1);
} //end of the function AAS_HashVec
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AAS_AddEdgeToHash(int edgenum)
{
int hash;
aas_edge_t *edge;
edge = &aasworld.edges[edgenum];
hash = AAS_HashEdge(edge->v[0], edge->v[1]);
aas_edgechain[edgenum] = aas_hashedges[hash];
aas_hashedges[hash] = edgenum;
} //end of the function AAS_AddEdgeToHash
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_FindHashedEdge(int v1num, int v2num, int *edgenum)
{
int e, hash;
aas_edge_t *edge;
hash = AAS_HashEdge(v1num, v2num);
for (e = aas_hashedges[hash]; e >= 0; e = aas_edgechain[e])
{
edge = &aasworld.edges[e];
if (edge->v[0] == v1num)
{
if (edge->v[1] == v2num)
{
*edgenum = e;
return true;
} //end if
} //end if
else if (edge->v[1] == v1num)
{
if (edge->v[0] == v2num)
{
//negative for a reversed edge
*edgenum = -e;
return true;
} //end if
} //end else
} //end for
return false;
} //end of the function AAS_FindHashedPlane
//===========================================================================
// returns true if the edge was found
// stores the edge number in *edgenum (negative if reversed edge)
// stores new edge if not stored already
// returns zero when the edge is degenerate
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_GetEdge(vec3_t v1, vec3_t v2, int *edgenum)
{
int v1num, v2num;
qboolean found;
//the first edge is a dummy
if (aasworld.numedges == 0) aasworld.numedges = 1;
found = AAS_GetVertex(v1, &v1num);
found &= AAS_GetVertex(v2, &v2num);
//if one of the vertexes was outside the valid range
if (v1num == -1 || v2num == -1)
{
*edgenum = 0;
return true;
} //end if
//if both vertexes are the same or snapped onto each other
if (v1num == v2num)
{
*edgenum = 0;
return true;
} //end if
//if both vertexes where already stored
if (found)
{
#ifdef EDGE_HASHING
if (AAS_FindHashedEdge(v1num, v2num, edgenum)) return true;
#else
int i;
for (i = 1; i < aasworld.numedges; i++)
{
if (aasworld.edges[i].v[0] == v1num)
{
if (aasworld.edges[i].v[1] == v2num)
{
*edgenum = i;
return true;
} //end if
} //end if
else if (aasworld.edges[i].v[1] == v1num)
{
if (aasworld.edges[i].v[0] == v2num)
{
//negative for a reversed edge
*edgenum = -i;
return true;
} //end if
} //end else
} //end for
#endif //EDGE_HASHING
} //end if
if (aasworld.numedges >= max_aas.max_edges)
{
Error("AAS_MAX_EDGES = %d", max_aas.max_edges);
} //end if
aasworld.edges[aasworld.numedges].v[0] = v1num;
aasworld.edges[aasworld.numedges].v[1] = v2num;
*edgenum = aasworld.numedges;
#ifdef EDGE_HASHING
AAS_AddEdgeToHash(*edgenum);
#endif //EDGE_HASHING
aasworld.numedges++;
return false;
} //end of the function AAS_GetEdge
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int AAS_PlaneTypeForNormal(vec3_t normal)
{
vec_t ax, ay, az;
//NOTE: epsilon used
if ( (normal[0] >= 1.0 -NORMAL_EPSILON) ||
(normal[0] <= -1.0 + NORMAL_EPSILON)) return PLANE_X;
if ( (normal[1] >= 1.0 -NORMAL_EPSILON) ||
(normal[1] <= -1.0 + NORMAL_EPSILON)) return PLANE_Y;
if ( (normal[2] >= 1.0 -NORMAL_EPSILON) ||
(normal[2] <= -1.0 + NORMAL_EPSILON)) return PLANE_Z;
ax = fabs(normal[0]);
ay = fabs(normal[1]);
az = fabs(normal[2]);
if (ax >= ay && ax >= az) return PLANE_ANYX;
if (ay >= ax && ay >= az) return PLANE_ANYY;
return PLANE_ANYZ;
} //end of the function AAS_PlaneTypeForNormal
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AAS_AddPlaneToHash(int planenum)
{
int hash;
aas_plane_t *plane;
plane = &aasworld.planes[planenum];
hash = (int)fabs(plane->dist) / 8;
hash &= (PLANE_HASH_SIZE-1);
aas_planechain[planenum] = aas_hashplanes[hash];
aas_hashplanes[hash] = planenum;
} //end of the function AAS_AddPlaneToHash
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int AAS_PlaneEqual(vec3_t normal, float dist, int planenum)
{
float diff;
diff = dist - aasworld.planes[planenum].dist;
if (diff > -DIST_EPSILON && diff < DIST_EPSILON)
{
diff = normal[0] - aasworld.planes[planenum].normal[0];
if (diff > -NORMAL_EPSILON && diff < NORMAL_EPSILON)
{
diff = normal[1] - aasworld.planes[planenum].normal[1];
if (diff > -NORMAL_EPSILON && diff < NORMAL_EPSILON)
{
diff = normal[2] - aasworld.planes[planenum].normal[2];
if (diff > -NORMAL_EPSILON && diff < NORMAL_EPSILON)
{
return true;
} //end if
} //end if
} //end if
} //end if
return false;
} //end of the function AAS_PlaneEqual
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_FindPlane(vec3_t normal, float dist, int *planenum)
{
int i;
for (i = 0; i < aasworld.numplanes; i++)
{
if (AAS_PlaneEqual(normal, dist, i))
{
*planenum = i;
return true;
} //end if
} //end for
return false;
} //end of the function AAS_FindPlane
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_FindHashedPlane(vec3_t normal, float dist, int *planenum)
{
int i, p;
int hash, h;
hash = (int)fabs(dist) / 8;
hash &= (PLANE_HASH_SIZE-1);
//search the border bins as well
for (i = -1; i <= 1; i++)
{
h = (hash+i)&(PLANE_HASH_SIZE-1);
for (p = aas_hashplanes[h]; p >= 0; p = aas_planechain[p])
{
if (AAS_PlaneEqual(normal, dist, p))
{
*planenum = p;
return true;
} //end if
} //end for
} //end for
return false;
} //end of the function AAS_FindHashedPlane
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_GetPlane(vec3_t normal, vec_t dist, int *planenum)
{
aas_plane_t *plane, temp;
//if (AAS_FindPlane(normal, dist, planenum)) return true;
if (AAS_FindHashedPlane(normal, dist, planenum)) return true;
if (aasworld.numplanes >= max_aas.max_planes-1)
{
Error("AAS_MAX_PLANES = %d", max_aas.max_planes);
} //end if
#ifdef STOREPLANESDOUBLE
plane = &aasworld.planes[aasworld.numplanes];
VectorCopy(normal, plane->normal);
plane->dist = dist;
plane->type = (plane+1)->type = PlaneTypeForNormal(plane->normal);
VectorCopy(normal, (plane+1)->normal);
VectorNegate((plane+1)->normal, (plane+1)->normal);
(plane+1)->dist = -dist;
aasworld.numplanes += 2;
//allways put axial planes facing positive first
if (plane->type < 3)
{
if (plane->normal[0] < 0 || plane->normal[1] < 0 || plane->normal[2] < 0)
{
// flip order
temp = *plane;
*plane = *(plane+1);
*(plane+1) = temp;
*planenum = aasworld.numplanes - 1;
return false;
} //end if
} //end if
*planenum = aasworld.numplanes - 2;
//add the planes to the hash
AAS_AddPlaneToHash(aasworld.numplanes - 1);
AAS_AddPlaneToHash(aasworld.numplanes - 2);
return false;
#else
plane = &aasworld.planes[aasworld.numplanes];
VectorCopy(normal, plane->normal);
plane->dist = dist;
plane->type = AAS_PlaneTypeForNormal(normal);
*planenum = aasworld.numplanes;
aasworld.numplanes++;
//add the plane to the hash
AAS_AddPlaneToHash(aasworld.numplanes - 1);
return false;
#endif //STOREPLANESDOUBLE
} //end of the function AAS_GetPlane
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean AAS_GetFace(winding_t *w, plane_t *p, int side, int *facenum)
{
int edgenum, i, j;
aas_face_t *face;
//face zero is a dummy, because of the face index with negative numbers
if (aasworld.numfaces == 0) aasworld.numfaces = 1;
if (aasworld.numfaces >= max_aas.max_faces)
{
Error("AAS_MAX_FACES = %d", max_aas.max_faces);
} //end if
face = &aasworld.faces[aasworld.numfaces];
AAS_GetPlane(p->normal, p->dist, &face->planenum);
face->faceflags = 0;
face->firstedge = aasworld.edgeindexsize;
face->frontarea = 0;
face->backarea = 0;
face->numedges = 0;
for (i = 0; i < w->numpoints; i++)
{
if (aasworld.edgeindexsize >= max_aas.max_edgeindexsize)
{
Error("AAS_MAX_EDGEINDEXSIZE = %d", max_aas.max_edgeindexsize);
} //end if
j = (i+1) % w->numpoints;
AAS_GetEdge(w->p[i], w->p[j], &edgenum);
//if the edge wasn't degenerate
if (edgenum)
{
aasworld.edgeindex[aasworld.edgeindexsize++] = edgenum;
face->numedges++;
} //end if
else if (verbose)
{
Log_Write("AAS_GetFace: face %d had degenerate edge %d-%d\r\n",
aasworld.numfaces, i, j);
} //end else
} //end for
if (face->numedges < 1
#ifdef NOTHREEVERTEXFACES
|| face->numedges < 3
#endif //NOTHREEVERTEXFACES
)
{
memset(&aasworld.faces[aasworld.numfaces], 0, sizeof(aas_face_t));
Log_Write("AAS_GetFace: face %d was tiny\r\n", aasworld.numfaces);
return false;
} //end if
*facenum = aasworld.numfaces;
aasworld.numfaces++;
return true;
} //end of the function AAS_GetFace
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
/*
qboolean AAS_GetFace(winding_t *w, plane_t *p, int side, int *facenum)
{
aas_edgeindex_t edges[1024];
int planenum, numedges, i;
int j, edgenum;
qboolean foundplane, foundedges;
aas_face_t *face;
//face zero is a dummy, because of the face index with negative numbers
if (aasworld.numfaces == 0) aasworld.numfaces = 1;
foundplane = AAS_GetPlane(p->normal, p->dist, &planenum);
foundedges = true;
numedges = w->numpoints;
for (i = 0; i < w->numpoints; i++)
{
if (i >= 1024) Error("AAS_GetFace: more than %d edges\n", 1024);
foundedges &= AAS_GetEdge(w->p[i], w->p[(i+1 >= w->numpoints ? 0 : i+1)], &edges[i]);
} //end for
//FIXME: use portal number instead of a search
//if the plane and all edges already existed
if (foundplane && foundedges)
{
for (i = 0; i < aasworld.numfaces; i++)
{
face = &aasworld.faces[i];
if (planenum == face->planenum)
{
if (numedges == face->numedges)
{
for (j = 0; j < numedges; j++)
{
edgenum = abs(aasworld.edgeindex[face->firstedge + j]);
if (abs(edges[i]) != edgenum) break;
} //end for
if (j == numedges)
{
//jippy found the face
*facenum = -i;
return true;
} //end if
} //end if
} //end if
} //end for
} //end if
if (aasworld.numfaces >= max_aas.max_faces)
{
Error("AAS_MAX_FACES = %d", max_aas.max_faces);
} //end if
face = &aasworld.faces[aasworld.numfaces];
face->planenum = planenum;
face->faceflags = 0;
face->numedges = numedges;
face->firstedge = aasworld.edgeindexsize;
face->frontarea = 0;
face->backarea = 0;
for (i = 0; i < numedges; i++)
{
if (aasworld.edgeindexsize >= max_aas.max_edgeindexsize)
{
Error("AAS_MAX_EDGEINDEXSIZE = %d", max_aas.max_edgeindexsize);
} //end if
aasworld.edgeindex[aasworld.edgeindexsize++] = edges[i];
} //end for
*facenum = aasworld.numfaces;
aasworld.numfaces++;
return false;
} //end of the function AAS_GetFace*/
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void AAS_StoreAreaSettings(tmp_areasettings_t *tmpareasettings)
{
aas_areasettings_t *areasettings;
if (aasworld.numareasettings == 0) aasworld.numareasettings = 1;
areasettings = &aasworld.areasettings[aasworld.numareasettings++];
areasettings->areaflags = tmpareasettings->areaflags;
areasettings->presencetype = tmpareasettings->presencetype;
areasettings->contents = tmpareasettings->contents;
if (tmpareasettings->modelnum > AREACONTENTS_MAXMODELNUM)
Log_Print("WARNING: more than %d mover models\n", AREACONTENTS_MAXMODELNUM);
areasettings->contents |= (tmpareasettings->modelnum & AREACONTENTS_MAXMODELNUM) << AREACONTENTS_MODELNUMSHIFT;
} //end of the function AAS_StoreAreaSettings
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int AAS_StoreArea(tmp_area_t *tmparea)
{
int side, edgenum, i;
plane_t *plane;
tmp_face_t *tmpface;
aas_area_t *aasarea;
aas_edge_t *edge;
aas_face_t *aasface;
aas_faceindex_t aasfacenum;
vec3_t facecenter;
winding_t *w;
//when the area is merged go to the merged area
//FIXME: this isn't necessary anymore because the tree
// is refreshed after area merging
while(tmparea->mergedarea) tmparea = tmparea->mergedarea;
//
if (tmparea->invalid) Error("AAS_StoreArea: tried to store invalid area");
//if there is an aas area already stored for this tmp area
if (tmparea->aasareanum) return -tmparea->aasareanum;
//
if (aasworld.numareas >= max_aas.max_areas)
{
Error("AAS_MAX_AREAS = %d", max_aas.max_areas);
} //end if
//area zero is a dummy
if (aasworld.numareas == 0) aasworld.numareas = 1;
//create an area from this leaf
aasarea = &aasworld.areas[aasworld.numareas];
aasarea->areanum = aasworld.numareas;
aasarea->numfaces = 0;
aasarea->firstface = aasworld.faceindexsize;
ClearBounds(aasarea->mins, aasarea->maxs);
VectorClear(aasarea->center);
//
// Log_Write("tmparea %d became aasarea %d\r\n", tmparea->areanum, aasarea->areanum);
//store the aas area number at the tmp area
tmparea->aasareanum = aasarea->areanum;
//
for (tmpface = tmparea->tmpfaces; tmpface; tmpface = tmpface->next[side])
{
side = tmpface->frontarea != tmparea;
//if there's an aas face created for the tmp face already
if (tmpface->aasfacenum)
{
//we're at the back of the face so use a negative index
aasfacenum = -tmpface->aasfacenum;
#ifdef DEBUG
if (tmpface->aasfacenum < 0 || tmpface->aasfacenum > max_aas.max_faces)
{
Error("AAS_CreateTree_r: face number out of range");
} //end if
#endif //DEBUG
aasface = &aasworld.faces[tmpface->aasfacenum];
aasface->backarea = aasarea->areanum;
} //end if
else
{
plane = &mapplanes[tmpface->planenum ^ side];
if (side)
{
w = tmpface->winding;
tmpface->winding = ReverseWinding(tmpface->winding);
} //end if
if (!AAS_GetFace(tmpface->winding, plane, 0, &aasfacenum)) continue;
if (side)
{
FreeWinding(tmpface->winding);
tmpface->winding = w;
} //end if
aasface = &aasworld.faces[aasfacenum];
aasface->frontarea = aasarea->areanum;
aasface->backarea = 0;
aasface->faceflags = tmpface->faceflags;
//set the face number at the tmp face
tmpface->aasfacenum = aasfacenum;
} //end else
//add face points to the area bounds and
//calculate the face 'center'
VectorClear(facecenter);
for (edgenum = 0; edgenum < aasface->numedges; edgenum++)
{
edge = &aasworld.edges[abs(aasworld.edgeindex[aasface->firstedge + edgenum])];
for (i = 0; i < 2; i++)
{
AddPointToBounds(aasworld.vertexes[edge->v[i]], aasarea->mins, aasarea->maxs);
VectorAdd(aasworld.vertexes[edge->v[i]], facecenter, facecenter);
} //end for
} //end for
VectorScale(facecenter, 1.0 / (aasface->numedges * 2.0), facecenter);
//add the face 'center' to the area 'center'
VectorAdd(aasarea->center, facecenter, aasarea->center);
//
if (aasworld.faceindexsize >= max_aas.max_faceindexsize)
{
Error("AAS_MAX_FACEINDEXSIZE = %d", max_aas.max_faceindexsize);
} //end if
aasworld.faceindex[aasworld.faceindexsize++] = aasfacenum;
aasarea->numfaces++;
} //end for
//if the area has no faces at all (return 0, = solid leaf)
if (!aasarea->numfaces) return 0;