forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
3416 lines (2875 loc) · 95.4 KB
/
map.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "vbsp.h"
#include "map_shared.h"
#include "disp_vbsp.h"
#include "tier1/strtools.h"
#include "builddisp.h"
#include "tier0/icommandline.h"
#include "KeyValues.h"
#include "materialsub.h"
#include "fgdlib/fgdlib.h"
#include "manifest.h"
#include "mathlib/vmatrix.h"
#ifdef VSVMFIO
#include "VmfImport.h"
#endif // VSVMFIO
// undefine to make plane finding use linear sort
#define USE_HASHING
#define RENDER_NORMAL_EPSILON 0.00001
#define RENDER_DIST_EPSILON 0.01f
#define BRUSH_CLIP_EPSILON 0.01f // this should probably be the same
// as clip epsilon, but it is 0.1f and I
// currently don't know how that number was
// come to (cab) - this is 0.01 of an inch
// for clipping brush solids
struct LoadSide_t
{
mapbrush_t *pBrush;
side_t *pSide;
int nSideIndex;
int nBaseFlags;
int nBaseContents;
Vector planepts[3];
brush_texture_t td;
};
extern qboolean onlyents;
CUtlVector< CMapFile * > g_Maps;
CMapFile *g_MainMap = NULL;
CMapFile *g_LoadingMap = NULL;
char CMapFile::m_InstancePath[ MAX_PATH ] = "";
int CMapFile::m_InstanceCount = 0;
int CMapFile::c_areaportals = 0;
void CMapFile::Init( void )
{
entity_num = 0;
num_entities = 0;
nummapplanes = 0;
memset( mapplanes, 0, sizeof( mapplanes ) );
nummapbrushes = 0;
memset( mapbrushes, 0, sizeof( mapbrushes ) );
nummapbrushsides = 0;
memset( brushsides, 0, sizeof( brushsides ) );
memset( side_brushtextures, 0, sizeof( side_brushtextures ) );
memset( planehash, 0, sizeof( planehash ) );
m_ConnectionPairs = NULL;
m_StartMapOverlays = g_aMapOverlays.Count();
m_StartMapWaterOverlays = g_aMapWaterOverlays.Count();
c_boxbevels = 0;
c_edgebevels = 0;
c_clipbrushes = 0;
g_ClipTexinfo = -1;
}
// All the brush sides referenced by info_no_dynamic_shadow entities.
CUtlVector<int> g_NoDynamicShadowSides;
void TestExpandBrushes (void);
ChunkFileResult_t LoadDispDistancesCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispDistancesKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispInfoCallback(CChunkFile *pFile, mapdispinfo_t **ppMapDispInfo );
ChunkFileResult_t LoadDispInfoKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispNormalsCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispNormalsKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispOffsetsCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispOffsetsKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispAlphasCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispAlphasKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispTriangleTagsCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispTriangleTagsKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
#ifdef VSVMFIO
ChunkFileResult_t LoadDispOffsetNormalsCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo);
ChunkFileResult_t LoadDispOffsetNormalsKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo);
#endif // VSVMFIO
ChunkFileResult_t LoadEntityCallback(CChunkFile *pFile, int nParam);
ChunkFileResult_t LoadEntityKeyCallback(const char *szKey, const char *szValue, LoadEntity_t *pLoadEntity);
ChunkFileResult_t LoadConnectionsCallback(CChunkFile *pFile, LoadEntity_t *pLoadEntity);
ChunkFileResult_t LoadConnectionsKeyCallback(const char *szKey, const char *szValue, LoadEntity_t *pLoadEntity);
ChunkFileResult_t LoadSolidCallback(CChunkFile *pFile, LoadEntity_t *pLoadEntity);
ChunkFileResult_t LoadSolidKeyCallback(const char *szKey, const char *szValue, mapbrush_t *pLoadBrush);
ChunkFileResult_t LoadSideCallback(CChunkFile *pFile, LoadSide_t *pSideInfo);
ChunkFileResult_t LoadSideKeyCallback(const char *szKey, const char *szValue, LoadSide_t *pSideInfo);
/*
=============================================================================
PLANE FINDING
=============================================================================
*/
/*
=================
PlaneTypeForNormal
=================
*/
int PlaneTypeForNormal (Vector& normal)
{
vec_t ax, ay, az;
// NOTE: should these have an epsilon around 1.0?
if (normal[0] == 1.0 || normal[0] == -1.0)
return PLANE_X;
if (normal[1] == 1.0 || normal[1] == -1.0)
return PLANE_Y;
if (normal[2] == 1.0 || normal[2] == -1.0)
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;
}
/*
================
PlaneEqual
================
*/
qboolean PlaneEqual (plane_t *p, Vector& normal, vec_t dist, float normalEpsilon, float distEpsilon)
{
#if 1
if (
fabs(p->normal[0] - normal[0]) < normalEpsilon
&& fabs(p->normal[1] - normal[1]) < normalEpsilon
&& fabs(p->normal[2] - normal[2]) < normalEpsilon
&& fabs(p->dist - dist) < distEpsilon )
return true;
#else
if (p->normal[0] == normal[0]
&& p->normal[1] == normal[1]
&& p->normal[2] == normal[2]
&& p->dist == dist)
return true;
#endif
return false;
}
/*
================
AddPlaneToHash
================
*/
void CMapFile::AddPlaneToHash (plane_t *p)
{
int hash;
hash = (int)fabs(p->dist) / 8;
hash &= (PLANE_HASHES-1);
p->hash_chain = planehash[hash];
planehash[hash] = p;
}
/*
================
CreateNewFloatPlane
================
*/
int CMapFile::CreateNewFloatPlane (Vector& normal, vec_t dist)
{
plane_t *p, temp;
if (VectorLength(normal) < 0.5)
g_MapError.ReportError ("FloatPlane: bad normal");
// create a new plane
if (nummapplanes+2 > MAX_MAP_PLANES)
g_MapError.ReportError ("MAX_MAP_PLANES");
p = &mapplanes[nummapplanes];
VectorCopy (normal, p->normal);
p->dist = dist;
p->type = (p+1)->type = PlaneTypeForNormal (p->normal);
VectorSubtract (vec3_origin, normal, (p+1)->normal);
(p+1)->dist = -dist;
nummapplanes += 2;
// allways put axial planes facing positive first
if (p->type < 3)
{
if (p->normal[0] < 0 || p->normal[1] < 0 || p->normal[2] < 0)
{
// flip order
temp = *p;
*p = *(p+1);
*(p+1) = temp;
AddPlaneToHash (p);
AddPlaneToHash (p+1);
return nummapplanes - 1;
}
}
AddPlaneToHash (p);
AddPlaneToHash (p+1);
return nummapplanes - 2;
}
/*
==============
SnapVector
==============
*/
bool SnapVector (Vector& normal)
{
int i;
for (i=0 ; i<3 ; i++)
{
if ( fabs(normal[i] - 1) < RENDER_NORMAL_EPSILON )
{
VectorClear (normal);
normal[i] = 1;
return true;
}
if ( fabs(normal[i] - -1) < RENDER_NORMAL_EPSILON )
{
VectorClear (normal);
normal[i] = -1;
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Snaps normal to axis-aligned if it is within an epsilon of axial.
// Rounds dist to integer if it is within an epsilon of integer.
// Input : normal - Plane normal vector (assumed to be unit length).
// dist - Plane constant.
//-----------------------------------------------------------------------------
void SnapPlane(Vector &normal, vec_t &dist)
{
SnapVector(normal);
if (fabs(dist - RoundInt(dist)) < RENDER_DIST_EPSILON)
{
dist = RoundInt(dist);
}
}
//-----------------------------------------------------------------------------
// Purpose: Snaps normal to axis-aligned if it is within an epsilon of axial.
// Recalculates dist if the normal was snapped. Rounds dist to integer
// if it is within an epsilon of integer.
// Input : normal - Plane normal vector (assumed to be unit length).
// dist - Plane constant.
// p0, p1, p2 - Three points on the plane.
//-----------------------------------------------------------------------------
void SnapPlane(Vector &normal, vec_t &dist, const Vector &p0, const Vector &p1, const Vector &p2)
{
if (SnapVector(normal))
{
//
// Calculate a new plane constant using the snapped normal. Use the
// centroid of the three plane points to minimize error. This is like
// rotating the plane around the centroid.
//
Vector p3 = (p0 + p1 + p2) / 3.0f;
dist = normal.Dot(p3);
if ( g_snapAxialPlanes )
{
dist = RoundInt(dist);
}
}
if (fabs(dist - RoundInt(dist)) < RENDER_DIST_EPSILON)
{
dist = RoundInt(dist);
}
}
/*
=============
FindFloatPlane
=============
*/
#ifndef USE_HASHING
int CMapFile::FindFloatPlane (Vector& normal, vec_t dist)
{
int i;
plane_t *p;
SnapPlane(normal, dist);
for (i=0, p=mapplanes ; i<nummapplanes ; i++, p++)
{
if (PlaneEqual (p, normal, dist, RENDER_NORMAL_EPSILON, RENDER_DIST_EPSILON))
return i;
}
return CreateNewFloatPlane (normal, dist);
}
#else
int CMapFile::FindFloatPlane (Vector& normal, vec_t dist)
{
int i;
plane_t *p;
int hash, h;
SnapPlane(normal, dist);
hash = (int)fabs(dist) / 8;
hash &= (PLANE_HASHES-1);
// search the border bins as well
for (i=-1 ; i<=1 ; i++)
{
h = (hash+i)&(PLANE_HASHES-1);
for (p = planehash[h] ; p ; p=p->hash_chain)
{
if (PlaneEqual (p, normal, dist, RENDER_NORMAL_EPSILON, RENDER_DIST_EPSILON))
return p-mapplanes;
}
}
return CreateNewFloatPlane (normal, dist);
}
#endif
//-----------------------------------------------------------------------------
// Purpose: Builds a plane normal and distance from three points on the plane.
// If the normal is nearly axial, it will be snapped to be axial. Looks
// up the plane in the unique planes.
// Input : p0, p1, p2 - Three points on the plane.
// Output : Returns the index of the plane in the planes list.
//-----------------------------------------------------------------------------
int CMapFile::PlaneFromPoints(const Vector &p0, const Vector &p1, const Vector &p2)
{
Vector t1, t2, normal;
vec_t dist;
VectorSubtract (p0, p1, t1);
VectorSubtract (p2, p1, t2);
CrossProduct (t1, t2, normal);
VectorNormalize (normal);
dist = DotProduct (p0, normal);
SnapPlane(normal, dist, p0, p1, p2);
return FindFloatPlane (normal, dist);
}
/*
===========
BrushContents
===========
*/
int BrushContents (mapbrush_t *b)
{
int contents;
int unionContents = 0;
side_t *s;
int i;
s = &b->original_sides[0];
contents = s->contents;
unionContents = contents;
for (i=1 ; i<b->numsides ; i++, s++)
{
s = &b->original_sides[i];
unionContents |= s->contents;
#if 0
if (s->contents != contents)
{
Msg("Brush %i: mixed face contents\n", b->id);
break;
}
#endif
}
// NOTE: we're making slime translucent so that it doesn't block lighting on things floating on its surface
int transparentContents = unionContents & (CONTENTS_WINDOW|CONTENTS_GRATE|CONTENTS_WATER|CONTENTS_SLIME);
if ( transparentContents )
{
contents |= transparentContents | CONTENTS_TRANSLUCENT;
contents &= ~CONTENTS_SOLID;
}
return contents;
}
//============================================================================
bool IsAreaPortal( char const *pClassName )
{
// If the class name starts with "func_areaportal", then it's considered an area portal.
char const *pBaseName = "func_areaportal";
char const *pCur = pBaseName;
while( *pCur && *pClassName )
{
if( *pCur != *pClassName )
break;
++pCur;
++pClassName;
}
return *pCur == 0;
}
/*
=================
AddBrushBevels
Adds any additional planes necessary to allow the brush to be expanded
against axial bounding boxes
=================
*/
void CMapFile::AddBrushBevels (mapbrush_t *b)
{
int axis, dir;
int i, j, k, l, order;
side_t sidetemp;
brush_texture_t tdtemp;
side_t *s, *s2;
Vector normal;
float dist;
winding_t *w, *w2;
Vector vec, vec2;
float d;
//
// add the axial planes
//
order = 0;
for (axis=0 ; axis <3 ; axis++)
{
for (dir=-1 ; dir <= 1 ; dir+=2, order++)
{
// see if the plane is allready present
for (i=0, s=b->original_sides ; i<b->numsides ; i++,s++)
{
if (mapplanes[s->planenum].normal[axis] == dir)
break;
}
if (i == b->numsides)
{ // add a new side
if (nummapbrushsides == MAX_MAP_BRUSHSIDES)
g_MapError.ReportError ("MAX_MAP_BRUSHSIDES");
nummapbrushsides++;
b->numsides++;
VectorClear (normal);
normal[axis] = dir;
if (dir == 1)
dist = b->maxs[axis];
else
dist = -b->mins[axis];
s->planenum = FindFloatPlane (normal, dist);
s->texinfo = b->original_sides[0].texinfo;
s->contents = b->original_sides[0].contents;
s->bevel = true;
c_boxbevels++;
}
// if the plane is not in it canonical order, swap it
if (i != order)
{
sidetemp = b->original_sides[order];
b->original_sides[order] = b->original_sides[i];
b->original_sides[i] = sidetemp;
j = b->original_sides - brushsides;
tdtemp = side_brushtextures[j+order];
side_brushtextures[j+order] = side_brushtextures[j+i];
side_brushtextures[j+i] = tdtemp;
}
}
}
//
// add the edge bevels
//
if (b->numsides == 6)
return; // pure axial
// test the non-axial plane edges
for (i=6 ; i<b->numsides ; i++)
{
s = b->original_sides + i;
w = s->winding;
if (!w)
continue;
for (j=0 ; j<w->numpoints ; j++)
{
k = (j+1)%w->numpoints;
VectorSubtract (w->p[j], w->p[k], vec);
if (VectorNormalize (vec) < 0.5)
continue;
SnapVector (vec);
for (k=0 ; k<3 ; k++)
if ( vec[k] == -1 || vec[k] == 1)
break; // axial
if (k != 3)
continue; // only test non-axial edges
// try the six possible slanted axials from this edge
for (axis=0 ; axis <3 ; axis++)
{
for (dir=-1 ; dir <= 1 ; dir+=2)
{
// construct a plane
VectorClear (vec2);
vec2[axis] = dir;
CrossProduct (vec, vec2, normal);
if (VectorNormalize (normal) < 0.5)
continue;
dist = DotProduct (w->p[j], normal);
// if all the points on all the sides are
// behind this plane, it is a proper edge bevel
for (k=0 ; k<b->numsides ; k++)
{
// if this plane has allready been used, skip it
// NOTE: Use a larger tolerance for collision planes than for rendering planes
if ( PlaneEqual(&mapplanes[b->original_sides[k].planenum], normal, dist, 0.01f, 0.01f ) )
break;
w2 = b->original_sides[k].winding;
if (!w2)
continue;
for (l=0 ; l<w2->numpoints ; l++)
{
d = DotProduct (w2->p[l], normal) - dist;
if (d > 0.1)
break; // point in front
}
if (l != w2->numpoints)
break;
}
if (k != b->numsides)
continue; // wasn't part of the outer hull
// add this plane
if (nummapbrushsides == MAX_MAP_BRUSHSIDES)
g_MapError.ReportError ("MAX_MAP_BRUSHSIDES");
nummapbrushsides++;
s2 = &b->original_sides[b->numsides];
s2->planenum = FindFloatPlane (normal, dist);
s2->texinfo = b->original_sides[0].texinfo;
s2->contents = b->original_sides[0].contents;
s2->bevel = true;
c_edgebevels++;
b->numsides++;
}
}
}
}
}
/*
================
MakeBrushWindings
makes basewindigs for sides and mins / maxs for the brush
================
*/
qboolean CMapFile::MakeBrushWindings (mapbrush_t *ob)
{
int i, j;
winding_t *w;
side_t *side;
plane_t *plane;
ClearBounds (ob->mins, ob->maxs);
for (i=0 ; i<ob->numsides ; i++)
{
plane = &mapplanes[ob->original_sides[i].planenum];
w = BaseWindingForPlane (plane->normal, plane->dist);
for (j=0 ; j<ob->numsides && w; j++)
{
if (i == j)
continue;
if (ob->original_sides[j].bevel)
continue;
plane = &mapplanes[ob->original_sides[j].planenum^1];
// ChopWindingInPlace (&w, plane->normal, plane->dist, 0); //CLIP_EPSILON);
// adding an epsilon here, due to precision issues creating complex
// displacement surfaces (cab)
ChopWindingInPlace( &w, plane->normal, plane->dist, BRUSH_CLIP_EPSILON );
}
side = &ob->original_sides[i];
side->winding = w;
if (w)
{
side->visible = true;
for (j=0 ; j<w->numpoints ; j++)
AddPointToBounds (w->p[j], ob->mins, ob->maxs);
}
}
for (i=0 ; i<3 ; i++)
{
if (ob->mins[i] < MIN_COORD_INTEGER || ob->maxs[i] > MAX_COORD_INTEGER)
Msg("Brush %i: bounds out of range\n", ob->id);
if (ob->mins[i] > MAX_COORD_INTEGER || ob->maxs[i] < MIN_COORD_INTEGER)
Msg("Brush %i: no visible sides on brush\n", ob->id);
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Takes all of the brushes from the current entity and adds them to the
// world's brush list. Used by func_detail and func_areaportal.
// THIS ROUTINE MAY ONLY BE USED DURING ENTITY LOADING.
// Input : mapent - Entity whose brushes are to be moved to the world.
//-----------------------------------------------------------------------------
void CMapFile::MoveBrushesToWorld( entity_t *mapent )
{
int newbrushes;
int worldbrushes;
mapbrush_t *temp;
int i;
// this is pretty gross, because the brushes are expected to be
// in linear order for each entity
newbrushes = mapent->numbrushes;
worldbrushes = entities[0].numbrushes;
temp = (mapbrush_t *)malloc(newbrushes*sizeof(mapbrush_t));
memcpy (temp, mapbrushes + mapent->firstbrush, newbrushes*sizeof(mapbrush_t));
#if 0 // let them keep their original brush numbers
for (i=0 ; i<newbrushes ; i++)
temp[i].entitynum = 0;
#endif
// make space to move the brushes (overlapped copy)
memmove (mapbrushes + worldbrushes + newbrushes,
mapbrushes + worldbrushes,
sizeof(mapbrush_t) * (nummapbrushes - worldbrushes - newbrushes) );
// copy the new brushes down
memcpy (mapbrushes + worldbrushes, temp, sizeof(mapbrush_t) * newbrushes);
// fix up indexes
entities[0].numbrushes += newbrushes;
for (i=1 ; i<num_entities ; i++)
entities[i].firstbrush += newbrushes;
free (temp);
mapent->numbrushes = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Takes all of the brushes from the current entity and adds them to the
// world's brush list. Used by func_detail and func_areaportal.
// Input : mapent - Entity whose brushes are to be moved to the world.
//-----------------------------------------------------------------------------
void CMapFile::MoveBrushesToWorldGeneral( entity_t *mapent )
{
int newbrushes;
int worldbrushes;
mapbrush_t *temp;
int i;
for( i = 0; i < nummapdispinfo; i++ )
{
if ( mapdispinfo[ i ].entitynum == ( mapent - entities ) )
{
mapdispinfo[ i ].entitynum = 0;
}
}
// this is pretty gross, because the brushes are expected to be
// in linear order for each entity
newbrushes = mapent->numbrushes;
worldbrushes = entities[0].numbrushes;
temp = (mapbrush_t *)malloc(newbrushes*sizeof(mapbrush_t));
memcpy (temp, mapbrushes + mapent->firstbrush, newbrushes*sizeof(mapbrush_t));
#if 0 // let them keep their original brush numbers
for (i=0 ; i<newbrushes ; i++)
temp[i].entitynum = 0;
#endif
// make space to move the brushes (overlapped copy)
memmove (mapbrushes + worldbrushes + newbrushes,
mapbrushes + worldbrushes,
sizeof(mapbrush_t) * (mapent->firstbrush - worldbrushes) );
// wwwxxxmmyyy
// copy the new brushes down
memcpy (mapbrushes + worldbrushes, temp, sizeof(mapbrush_t) * newbrushes);
// fix up indexes
entities[0].numbrushes += newbrushes;
for (i=1 ; i<num_entities ; i++)
{
if ( entities[ i ].firstbrush < mapent->firstbrush ) // if we use <=, then we'll remap the passed in ent, which we don't want to
{
entities[ i ].firstbrush += newbrushes;
}
}
free (temp);
mapent->numbrushes = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Iterates the sides of brush and removed CONTENTS_DETAIL from each side
// Input : *brush -
//-----------------------------------------------------------------------------
void RemoveContentsDetailFromBrush( mapbrush_t *brush )
{
// Only valid on non-world brushes
Assert( brush->entitynum != 0 );
side_t *s;
int i;
s = &brush->original_sides[0];
for ( i=0 ; i<brush->numsides ; i++, s++ )
{
if ( s->contents & CONTENTS_DETAIL )
{
s->contents &= ~CONTENTS_DETAIL;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Iterates all brushes in an entity and removes CONTENTS_DETAIL from all brushes
// Input : *mapent -
//-----------------------------------------------------------------------------
void CMapFile::RemoveContentsDetailFromEntity( entity_t *mapent )
{
int i;
for ( i = 0; i < mapent->numbrushes; i++ )
{
int brushnum = mapent->firstbrush + i;
mapbrush_t *brush = &mapbrushes[ brushnum ];
RemoveContentsDetailFromBrush( brush );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pFile -
// *pDisp -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t LoadDispDistancesCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo)
{
return(pFile->ReadChunk((KeyHandler_t)LoadDispDistancesKeyCallback, pMapDispInfo));
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : szKey -
// szValue -
// pDisp -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t LoadDispDistancesKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo)
{
if (!strnicmp(szKey, "row", 3))
{
char szBuf[MAX_KEYVALUE_LEN];
strcpy(szBuf, szValue);
int nCols = (1 << pMapDispInfo->power) + 1;
int nRow = atoi(&szKey[3]);
char *pszNext = strtok(szBuf, " ");
int nIndex = nRow * nCols;
while (pszNext != NULL)
{
pMapDispInfo->dispDists[nIndex] = (float)atof(pszNext);
pszNext = strtok(NULL, " ");
nIndex++;
}
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose: load in the displacement info "chunk" from the .map file into the
// vbsp map displacement info data structure
// Output : return the index of the map displacement info
//-----------------------------------------------------------------------------
ChunkFileResult_t LoadDispInfoCallback(CChunkFile *pFile, mapdispinfo_t **ppMapDispInfo )
{
//
// check to see if we exceeded the maximum displacement info list size
//
if (nummapdispinfo > MAX_MAP_DISPINFO)
{
g_MapError.ReportError( "ParseDispInfoChunk: nummapdispinfo > MAX_MAP_DISPINFO" );
}
// get a pointer to the next available displacement info slot
mapdispinfo_t *pMapDispInfo = &mapdispinfo[nummapdispinfo];
nummapdispinfo++;
//
// Set up handlers for the subchunks that we are interested in.
//
CChunkHandlerMap Handlers;
Handlers.AddHandler("normals", (ChunkHandler_t)LoadDispNormalsCallback, pMapDispInfo);
Handlers.AddHandler("distances", (ChunkHandler_t)LoadDispDistancesCallback, pMapDispInfo);
Handlers.AddHandler("offsets", (ChunkHandler_t)LoadDispOffsetsCallback, pMapDispInfo);
Handlers.AddHandler("alphas", (ChunkHandler_t)LoadDispAlphasCallback, pMapDispInfo);
Handlers.AddHandler("triangle_tags", (ChunkHandler_t)LoadDispTriangleTagsCallback, pMapDispInfo);
#ifdef VSVMFIO
Handlers.AddHandler("offset_normals", (ChunkHandler_t)LoadDispOffsetNormalsCallback, pMapDispInfo);
#endif // VSVMFIO
//
// Read the displacement chunk.
//
pFile->PushHandlers(&Handlers);
ChunkFileResult_t eResult = pFile->ReadChunk((KeyHandler_t)LoadDispInfoKeyCallback, pMapDispInfo);
pFile->PopHandlers();
if (eResult == ChunkFile_Ok)
{
// return a pointer to the displacement info
*ppMapDispInfo = pMapDispInfo;
}
return(eResult);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *szKey -
// *szValue -
// *mapent -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t LoadDispInfoKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo)
{
if (!stricmp(szKey, "power"))
{
CChunkFile::ReadKeyValueInt(szValue, pMapDispInfo->power);
}
#ifdef VSVMFIO
else if (!stricmp(szKey, "elevation"))
{
CChunkFile::ReadKeyValueFloat(szValue, pMapDispInfo->m_elevation);
}
#endif // VSVMFIO
else if (!stricmp(szKey, "uaxis"))
{
CChunkFile::ReadKeyValueVector3(szValue, pMapDispInfo->uAxis);
}
else if (!stricmp(szKey, "vaxis"))
{
CChunkFile::ReadKeyValueVector3(szValue, pMapDispInfo->vAxis);
}
else if( !stricmp( szKey, "startposition" ) )
{
CChunkFile::ReadKeyValueVector3( szValue, pMapDispInfo->startPosition );
}
else if( !stricmp( szKey, "flags" ) )
{
CChunkFile::ReadKeyValueInt( szValue, pMapDispInfo->flags );
}
#if 0 // old data
else if (!stricmp( szKey, "alpha" ) )
{
CChunkFile::ReadKeyValueVector4( szValue, pMapDispInfo->alphaValues );
}
#endif
else if (!stricmp(szKey, "mintess"))
{
CChunkFile::ReadKeyValueInt(szValue, pMapDispInfo->minTess);
}
else if (!stricmp(szKey, "smooth"))
{
CChunkFile::ReadKeyValueFloat(szValue, pMapDispInfo->smoothingAngle);
}
return(ChunkFile_Ok);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pFile -
// *pDisp -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t LoadDispNormalsCallback(CChunkFile *pFile, mapdispinfo_t *pMapDispInfo)
{
return(pFile->ReadChunk((KeyHandler_t)LoadDispNormalsKeyCallback, pMapDispInfo));
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *szKey -
// *szValue -
// *pDisp -
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t LoadDispNormalsKeyCallback(const char *szKey, const char *szValue, mapdispinfo_t *pMapDispInfo)
{
if (!strnicmp(szKey, "row", 3))
{
char szBuf[MAX_KEYVALUE_LEN];
strcpy(szBuf, szValue);
int nCols = (1 << pMapDispInfo->power) + 1;
int nRow = atoi(&szKey[3]);
char *pszNext0 = strtok(szBuf, " ");
char *pszNext1 = strtok(NULL, " ");
char *pszNext2 = strtok(NULL, " ");
int nIndex = nRow * nCols;
while ((pszNext0 != NULL) && (pszNext1 != NULL) && (pszNext2 != NULL))
{
pMapDispInfo->vectorDisps[nIndex][0] = (float)atof(pszNext0);
pMapDispInfo->vectorDisps[nIndex][1] = (float)atof(pszNext1);
pMapDispInfo->vectorDisps[nIndex][2] = (float)atof(pszNext2);
pszNext0 = strtok(NULL, " ");
pszNext1 = strtok(NULL, " ");
pszNext2 = strtok(NULL, " ");