forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticprop.cpp
759 lines (636 loc) · 22.5 KB
/
staticprop.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Places "detail" objects which are client-only renderable things
//
// $Revision: $
// $NoKeywords: $
//=============================================================================//
#include "vbsp.h"
#include "bsplib.h"
#include "utlvector.h"
#include "bspfile.h"
#include "gamebspfile.h"
#include "VPhysics_Interface.h"
#include "Studio.h"
#include "byteswap.h"
#include "UtlBuffer.h"
#include "CollisionUtils.h"
#include <float.h>
#include "CModel.h"
#include "PhysDll.h"
#include "utlsymbol.h"
#include "tier1/strtools.h"
#include "KeyValues.h"
static void SetCurrentModel( studiohdr_t *pStudioHdr );
static void FreeCurrentModelVertexes();
IPhysicsCollision *s_pPhysCollision = NULL;
//-----------------------------------------------------------------------------
// These puppies are used to construct the game lumps
//-----------------------------------------------------------------------------
static CUtlVector<StaticPropDictLump_t> s_StaticPropDictLump;
static CUtlVector<StaticPropLump_t> s_StaticPropLump;
static CUtlVector<StaticPropLeafLump_t> s_StaticPropLeafLump;
//-----------------------------------------------------------------------------
// Used to build the static prop
//-----------------------------------------------------------------------------
struct StaticPropBuild_t
{
char const* m_pModelName;
char const* m_pLightingOrigin;
Vector m_Origin;
QAngle m_Angles;
int m_Solid;
int m_Skin;
int m_Flags;
float m_FadeMinDist;
float m_FadeMaxDist;
bool m_FadesOut;
float m_flForcedFadeScale;
unsigned short m_nMinDXLevel;
unsigned short m_nMaxDXLevel;
int m_LightmapResolutionX;
int m_LightmapResolutionY;
};
//-----------------------------------------------------------------------------
// Used to cache collision model generation
//-----------------------------------------------------------------------------
struct ModelCollisionLookup_t
{
CUtlSymbol m_Name;
CPhysCollide* m_pCollide;
};
static bool ModelLess( ModelCollisionLookup_t const& src1, ModelCollisionLookup_t const& src2 )
{
return src1.m_Name < src2.m_Name;
}
static CUtlRBTree<ModelCollisionLookup_t, unsigned short> s_ModelCollisionCache( 0, 32, ModelLess );
static CUtlVector<int> s_LightingInfo;
//-----------------------------------------------------------------------------
// Gets the keyvalues from a studiohdr
//-----------------------------------------------------------------------------
bool StudioKeyValues( studiohdr_t* pStudioHdr, KeyValues *pValue )
{
if ( !pStudioHdr )
return false;
return pValue->LoadFromBuffer( pStudioHdr->pszName(), pStudioHdr->KeyValueText() );
}
//-----------------------------------------------------------------------------
// Makes sure the studio model is a static prop
//-----------------------------------------------------------------------------
enum isstaticprop_ret
{
RET_VALID,
RET_FAIL_NOT_MARKED_STATIC_PROP,
RET_FAIL_DYNAMIC,
};
isstaticprop_ret IsStaticProp( studiohdr_t* pHdr )
{
if (!(pHdr->flags & STUDIOHDR_FLAGS_STATIC_PROP))
return RET_FAIL_NOT_MARKED_STATIC_PROP;
// If it's got a propdata section in the model's keyvalues, it's not allowed to be a prop_static
KeyValues *modelKeyValues = new KeyValues(pHdr->pszName());
if ( StudioKeyValues( pHdr, modelKeyValues ) )
{
KeyValues *sub = modelKeyValues->FindKey("prop_data");
if ( sub )
{
if ( !(sub->GetInt( "allowstatic", 0 )) )
{
modelKeyValues->deleteThis();
return RET_FAIL_DYNAMIC;
}
}
}
modelKeyValues->deleteThis();
return RET_VALID;
}
//-----------------------------------------------------------------------------
// Add static prop model to the list of models
//-----------------------------------------------------------------------------
static int AddStaticPropDictLump( char const* pModelName )
{
StaticPropDictLump_t dictLump;
strncpy( dictLump.m_Name, pModelName, DETAIL_NAME_LENGTH );
for (int i = s_StaticPropDictLump.Size(); --i >= 0; )
{
if (!memcmp(&s_StaticPropDictLump[i], &dictLump, sizeof(dictLump) ))
return i;
}
return s_StaticPropDictLump.AddToTail( dictLump );
}
//-----------------------------------------------------------------------------
// Load studio model vertex data from a file...
//-----------------------------------------------------------------------------
bool LoadStudioModel( char const* pModelName, char const* pEntityType, CUtlBuffer& buf )
{
if ( !g_pFullFileSystem->ReadFile( pModelName, NULL, buf ) )
return false;
// Check that it's valid
if (strncmp ((const char *) buf.PeekGet(), "IDST", 4) &&
strncmp ((const char *) buf.PeekGet(), "IDAG", 4))
{
return false;
}
studiohdr_t* pHdr = (studiohdr_t*)buf.PeekGet();
Studio_ConvertStudioHdrToNewVersion( pHdr );
if (pHdr->version != STUDIO_VERSION)
{
return false;
}
isstaticprop_ret isStaticProp = IsStaticProp(pHdr);
if ( isStaticProp == RET_FAIL_NOT_MARKED_STATIC_PROP )
{
Warning("Error! To use model \"%s\"\n"
" with %s, it must be compiled with $staticprop!\n", pModelName, pEntityType );
return false;
}
else if ( isStaticProp == RET_FAIL_DYNAMIC && !g_bAllowDynamicPropsAsStatic )
{
Warning("Error! %s using model \"%s\", which must be used on a dynamic entity (i.e. prop_physics). Deleted.\n", pEntityType, pModelName);
return false;
}
// ensure reset
pHdr->pVertexBase = NULL;
pHdr->pIndexBase = NULL;
return true;
}
//-----------------------------------------------------------------------------
// Computes a convex hull from a studio mesh
//-----------------------------------------------------------------------------
static CPhysConvex* ComputeConvexHull( mstudiomesh_t* pMesh )
{
// Generate a list of all verts in the mesh
Vector** ppVerts = (Vector**)stackalloc(pMesh->numvertices * sizeof(Vector*) );
const mstudio_meshvertexdata_t *vertData = pMesh->GetVertexData();
Assert( vertData ); // This can only return NULL on X360 for now
for (int i = 0; i < pMesh->numvertices; ++i)
{
ppVerts[i] = vertData->Position(i);
}
// Generate a convex hull from the verts
return s_pPhysCollision->ConvexFromVerts( ppVerts, pMesh->numvertices );
}
//-----------------------------------------------------------------------------
// Computes a convex hull from the studio model
//-----------------------------------------------------------------------------
CPhysCollide* ComputeConvexHull( studiohdr_t* pStudioHdr )
{
CUtlVector<CPhysConvex*> convexHulls;
for (int body = 0; body < pStudioHdr->numbodyparts; ++body )
{
mstudiobodyparts_t *pBodyPart = pStudioHdr->pBodypart( body );
for( int model = 0; model < pBodyPart->nummodels; ++model )
{
mstudiomodel_t *pStudioModel = pBodyPart->pModel( model );
for( int mesh = 0; mesh < pStudioModel->nummeshes; ++mesh )
{
// Make a convex hull for each mesh
// NOTE: This won't work unless the model has been compiled
// with $staticprop
mstudiomesh_t *pStudioMesh = pStudioModel->pMesh( mesh );
convexHulls.AddToTail( ComputeConvexHull( pStudioMesh ) );
}
}
}
// Convert an array of convex elements to a compiled collision model
// (this deletes the convex elements)
return s_pPhysCollision->ConvertConvexToCollide( convexHulls.Base(), convexHulls.Size() );
}
//-----------------------------------------------------------------------------
// Add, find collision model in cache
//-----------------------------------------------------------------------------
static CPhysCollide* GetCollisionModel( char const* pModelName )
{
// Convert to a common string
char* pTemp = (char*)_alloca(strlen(pModelName) + 1);
strcpy( pTemp, pModelName );
_strlwr( pTemp );
char* pSlash = strchr( pTemp, '\\' );
while( pSlash )
{
*pSlash = '/';
pSlash = strchr( pTemp, '\\' );
}
// Find it in the cache
ModelCollisionLookup_t lookup;
lookup.m_Name = pTemp;
int i = s_ModelCollisionCache.Find( lookup );
if (i != s_ModelCollisionCache.InvalidIndex())
return s_ModelCollisionCache[i].m_pCollide;
// Load the studio model file
CUtlBuffer buf;
if (!LoadStudioModel(pModelName, "prop_static", buf))
{
Warning("Error loading studio model \"%s\"!\n", pModelName );
// This way we don't try to load it multiple times
lookup.m_pCollide = 0;
s_ModelCollisionCache.Insert( lookup );
return 0;
}
// Compute the convex hull of the model...
studiohdr_t* pStudioHdr = (studiohdr_t*)buf.PeekGet();
// necessary for vertex access
SetCurrentModel( pStudioHdr );
lookup.m_pCollide = ComputeConvexHull( pStudioHdr );
s_ModelCollisionCache.Insert( lookup );
if ( !lookup.m_pCollide )
{
Warning("Bad geometry on \"%s\"!\n", pModelName );
}
// Debugging
if (g_DumpStaticProps)
{
static int propNum = 0;
char tmp[128];
sprintf( tmp, "staticprop%03d.txt", propNum );
DumpCollideToGlView( lookup.m_pCollide, tmp );
++propNum;
}
FreeCurrentModelVertexes();
// Insert into cache...
return lookup.m_pCollide;
}
//-----------------------------------------------------------------------------
// Tests a single leaf against the static prop
//-----------------------------------------------------------------------------
static bool TestLeafAgainstCollide( int depth, int* pNodeList,
Vector const& origin, QAngle const& angles, CPhysCollide* pCollide )
{
// Copy the planes in the node list into a list of planes
float* pPlanes = (float*)_alloca(depth * 4 * sizeof(float) );
int idx = 0;
for (int i = depth; --i >= 0; ++idx )
{
int sign = (pNodeList[i] < 0) ? -1 : 1;
int node = (sign < 0) ? - pNodeList[i] - 1 : pNodeList[i];
dnode_t* pNode = &dnodes[node];
dplane_t* pPlane = &dplanes[pNode->planenum];
pPlanes[idx*4] = sign * pPlane->normal[0];
pPlanes[idx*4+1] = sign * pPlane->normal[1];
pPlanes[idx*4+2] = sign * pPlane->normal[2];
pPlanes[idx*4+3] = sign * pPlane->dist;
}
// Make a convex solid out of the planes
CPhysConvex* pPhysConvex = s_pPhysCollision->ConvexFromPlanes( pPlanes, depth, 0.0f );
// This should never happen, but if it does, return no collision
Assert( pPhysConvex );
if (!pPhysConvex)
return false;
CPhysCollide* pLeafCollide = s_pPhysCollision->ConvertConvexToCollide( &pPhysConvex, 1 );
// Collide the leaf solid with the static prop solid
trace_t tr;
s_pPhysCollision->TraceCollide( vec3_origin, vec3_origin, pLeafCollide, vec3_angle,
pCollide, origin, angles, &tr );
s_pPhysCollision->DestroyCollide( pLeafCollide );
return (tr.startsolid != 0);
}
//-----------------------------------------------------------------------------
// Find all leaves that intersect with this bbox + test against the static prop..
//-----------------------------------------------------------------------------
static void ComputeConvexHullLeaves_R( int node, int depth, int* pNodeList,
Vector const& mins, Vector const& maxs,
Vector const& origin, QAngle const& angles, CPhysCollide* pCollide,
CUtlVector<unsigned short>& leafList )
{
Assert( pNodeList && pCollide );
Vector cornermin, cornermax;
while( node >= 0 )
{
dnode_t* pNode = &dnodes[node];
dplane_t* pPlane = &dplanes[pNode->planenum];
// Arbitrary split plane here
for (int i = 0; i < 3; ++i)
{
if (pPlane->normal[i] >= 0)
{
cornermin[i] = mins[i];
cornermax[i] = maxs[i];
}
else
{
cornermin[i] = maxs[i];
cornermax[i] = mins[i];
}
}
if (DotProduct( pPlane->normal, cornermax ) <= pPlane->dist)
{
// Add the node to the list of nodes
pNodeList[depth] = node;
++depth;
node = pNode->children[1];
}
else if (DotProduct( pPlane->normal, cornermin ) >= pPlane->dist)
{
// In this case, we are going in front of the plane. That means that
// this plane must have an outward normal facing in the oppisite direction
// We indicate this be storing a negative node index in the node list
pNodeList[depth] = - node - 1;
++depth;
node = pNode->children[0];
}
else
{
// Here the box is split by the node. First, we'll add the plane as if its
// outward facing normal is in the direction of the node plane, then
// we'll have to reverse it for the other child...
pNodeList[depth] = node;
++depth;
ComputeConvexHullLeaves_R( pNode->children[1],
depth, pNodeList, mins, maxs, origin, angles, pCollide, leafList );
pNodeList[depth - 1] = - node - 1;
ComputeConvexHullLeaves_R( pNode->children[0],
depth, pNodeList, mins, maxs, origin, angles, pCollide, leafList );
return;
}
}
Assert( pNodeList && pCollide );
// Never add static props to solid leaves
if ( (dleafs[-node-1].contents & CONTENTS_SOLID) == 0 )
{
if (TestLeafAgainstCollide( depth, pNodeList, origin, angles, pCollide ))
{
leafList.AddToTail( -node - 1 );
}
}
}
//-----------------------------------------------------------------------------
// Places Static Props in the level
//-----------------------------------------------------------------------------
static void ComputeStaticPropLeaves( CPhysCollide* pCollide, Vector const& origin,
QAngle const& angles, CUtlVector<unsigned short>& leafList )
{
// Compute an axis-aligned bounding box for the collide
Vector mins, maxs;
s_pPhysCollision->CollideGetAABB( &mins, &maxs, pCollide, origin, angles );
// Find all leaves that intersect with the bounds
int tempNodeList[1024];
ComputeConvexHullLeaves_R( 0, 0, tempNodeList, mins, maxs,
origin, angles, pCollide, leafList );
}
//-----------------------------------------------------------------------------
// Computes the lighting origin
//-----------------------------------------------------------------------------
static bool ComputeLightingOrigin( StaticPropBuild_t const& build, Vector& lightingOrigin )
{
for (int i = s_LightingInfo.Count(); --i >= 0; )
{
int entIndex = s_LightingInfo[i];
// Check against all lighting info entities
char const* pTargetName = ValueForKey( &entities[entIndex], "targetname" );
if (!Q_strcmp(pTargetName, build.m_pLightingOrigin))
{
GetVectorForKey( &entities[entIndex], "origin", lightingOrigin );
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Places Static Props in the level
//-----------------------------------------------------------------------------
static void AddStaticPropToLump( StaticPropBuild_t const& build )
{
// Get the collision model
CPhysCollide* pConvexHull = GetCollisionModel( build.m_pModelName );
if (!pConvexHull)
return;
// Compute the leaves the static prop's convex hull hits
CUtlVector< unsigned short > leafList;
ComputeStaticPropLeaves( pConvexHull, build.m_Origin, build.m_Angles, leafList );
if ( !leafList.Count() )
{
Warning( "Static prop %s outside the map (%.2f, %.2f, %.2f)\n", build.m_pModelName, build.m_Origin.x, build.m_Origin.y, build.m_Origin.z );
return;
}
// Insert an element into the lump data...
int i = s_StaticPropLump.AddToTail( );
StaticPropLump_t& propLump = s_StaticPropLump[i];
propLump.m_PropType = AddStaticPropDictLump( build.m_pModelName );
VectorCopy( build.m_Origin, propLump.m_Origin );
VectorCopy( build.m_Angles, propLump.m_Angles );
propLump.m_FirstLeaf = s_StaticPropLeafLump.Count();
propLump.m_LeafCount = leafList.Count();
propLump.m_Solid = build.m_Solid;
propLump.m_Skin = build.m_Skin;
propLump.m_Flags = build.m_Flags;
if (build.m_FadesOut)
{
propLump.m_Flags |= STATIC_PROP_FLAG_FADES;
}
propLump.m_FadeMinDist = build.m_FadeMinDist;
propLump.m_FadeMaxDist = build.m_FadeMaxDist;
propLump.m_flForcedFadeScale = build.m_flForcedFadeScale;
propLump.m_nMinDXLevel = build.m_nMinDXLevel;
propLump.m_nMaxDXLevel = build.m_nMaxDXLevel;
if (build.m_pLightingOrigin && *build.m_pLightingOrigin)
{
if (ComputeLightingOrigin( build, propLump.m_LightingOrigin ))
{
propLump.m_Flags |= STATIC_PROP_USE_LIGHTING_ORIGIN;
}
}
propLump.m_nLightmapResolutionX = build.m_LightmapResolutionX;
propLump.m_nLightmapResolutionY = build.m_LightmapResolutionY;
// Add the leaves to the leaf lump
for (int j = 0; j < leafList.Size(); ++j)
{
StaticPropLeafLump_t insert;
insert.m_Leaf = leafList[j];
s_StaticPropLeafLump.AddToTail( insert );
}
}
//-----------------------------------------------------------------------------
// Places static props in the lump
//-----------------------------------------------------------------------------
static void SetLumpData( )
{
GameLumpHandle_t handle = g_GameLumps.GetGameLumpHandle(GAMELUMP_STATIC_PROPS);
if (handle != g_GameLumps.InvalidGameLump())
g_GameLumps.DestroyGameLump(handle);
int dictsize = s_StaticPropDictLump.Size() * sizeof(StaticPropDictLump_t);
int objsize = s_StaticPropLump.Size() * sizeof(StaticPropLump_t);
int leafsize = s_StaticPropLeafLump.Size() * sizeof(StaticPropLeafLump_t);
int size = dictsize + objsize + leafsize + 3 * sizeof(int);
handle = g_GameLumps.CreateGameLump( GAMELUMP_STATIC_PROPS, size, 0, GAMELUMP_STATIC_PROPS_VERSION );
// Serialize the data
CUtlBuffer buf( g_GameLumps.GetGameLump(handle), size );
buf.PutInt( s_StaticPropDictLump.Size() );
if (dictsize)
buf.Put( s_StaticPropDictLump.Base(), dictsize );
buf.PutInt( s_StaticPropLeafLump.Size() );
if (leafsize)
buf.Put( s_StaticPropLeafLump.Base(), leafsize );
buf.PutInt( s_StaticPropLump.Size() );
if (objsize)
buf.Put( s_StaticPropLump.Base(), objsize );
}
//-----------------------------------------------------------------------------
// Places Static Props in the level
//-----------------------------------------------------------------------------
void EmitStaticProps()
{
CreateInterfaceFn physicsFactory = GetPhysicsFactory();
if ( physicsFactory )
{
s_pPhysCollision = (IPhysicsCollision *)physicsFactory( VPHYSICS_COLLISION_INTERFACE_VERSION, NULL );
if( !s_pPhysCollision )
return;
}
// Generate a list of lighting origins, and strip them out
int i;
for ( i = 0; i < num_entities; ++i)
{
char* pEntity = ValueForKey(&entities[i], "classname");
if (!Q_strcmp(pEntity, "info_lighting"))
{
s_LightingInfo.AddToTail(i);
}
}
// Emit specifically specified static props
for ( i = 0; i < num_entities; ++i)
{
char* pEntity = ValueForKey(&entities[i], "classname");
if (!strcmp(pEntity, "static_prop") || !strcmp(pEntity, "prop_static"))
{
StaticPropBuild_t build;
GetVectorForKey( &entities[i], "origin", build.m_Origin );
GetAnglesForKey( &entities[i], "angles", build.m_Angles );
build.m_pModelName = ValueForKey( &entities[i], "model" );
build.m_Solid = IntForKey( &entities[i], "solid" );
build.m_Skin = IntForKey( &entities[i], "skin" );
build.m_FadeMaxDist = FloatForKey( &entities[i], "fademaxdist" );
build.m_Flags = 0;//IntForKey( &entities[i], "spawnflags" ) & STATIC_PROP_WC_MASK;
if (IntForKey( &entities[i], "ignorenormals" ) == 1)
{
build.m_Flags |= STATIC_PROP_IGNORE_NORMALS;
}
if (IntForKey( &entities[i], "disableshadows" ) == 1)
{
build.m_Flags |= STATIC_PROP_NO_SHADOW;
}
if (IntForKey( &entities[i], "disablevertexlighting" ) == 1)
{
build.m_Flags |= STATIC_PROP_NO_PER_VERTEX_LIGHTING;
}
if (IntForKey( &entities[i], "disableselfshadowing" ) == 1)
{
build.m_Flags |= STATIC_PROP_NO_SELF_SHADOWING;
}
if (IntForKey( &entities[i], "screenspacefade" ) == 1)
{
build.m_Flags |= STATIC_PROP_SCREEN_SPACE_FADE;
}
if (IntForKey( &entities[i], "generatelightmaps") == 0)
{
build.m_Flags |= STATIC_PROP_NO_PER_TEXEL_LIGHTING;
build.m_LightmapResolutionX = 0;
build.m_LightmapResolutionY = 0;
}
else
{
build.m_LightmapResolutionX = IntForKey( &entities[i], "lightmapresolutionx" );
build.m_LightmapResolutionY = IntForKey( &entities[i], "lightmapresolutiony" );
}
const char *pKey = ValueForKey( &entities[i], "fadescale" );
if ( pKey && pKey[0] )
{
build.m_flForcedFadeScale = FloatForKey( &entities[i], "fadescale" );
}
else
{
build.m_flForcedFadeScale = 1;
}
build.m_FadesOut = (build.m_FadeMaxDist > 0);
build.m_pLightingOrigin = ValueForKey( &entities[i], "lightingorigin" );
if (build.m_FadesOut)
{
build.m_FadeMinDist = FloatForKey( &entities[i], "fademindist" );
if (build.m_FadeMinDist < 0)
{
build.m_FadeMinDist = build.m_FadeMaxDist;
}
}
else
{
build.m_FadeMinDist = 0;
}
build.m_nMinDXLevel = (unsigned short)IntForKey( &entities[i], "mindxlevel" );
build.m_nMaxDXLevel = (unsigned short)IntForKey( &entities[i], "maxdxlevel" );
AddStaticPropToLump( build );
// strip this ent from the .bsp file
entities[i].epairs = 0;
}
}
// Strip out lighting origins; has to be done here because they are used when
// static props are made
for ( i = s_LightingInfo.Count(); --i >= 0; )
{
// strip this ent from the .bsp file
entities[s_LightingInfo[i]].epairs = 0;
}
SetLumpData( );
}
static studiohdr_t *g_pActiveStudioHdr;
static void SetCurrentModel( studiohdr_t *pStudioHdr )
{
// track the correct model
g_pActiveStudioHdr = pStudioHdr;
}
static void FreeCurrentModelVertexes()
{
Assert( g_pActiveStudioHdr );
if ( g_pActiveStudioHdr->pVertexBase )
{
free( g_pActiveStudioHdr->pVertexBase );
g_pActiveStudioHdr->pVertexBase = NULL;
}
}
const vertexFileHeader_t * mstudiomodel_t::CacheVertexData( void * pModelData )
{
char fileName[260];
FileHandle_t fileHandle;
vertexFileHeader_t *pVvdHdr;
Assert( pModelData == NULL );
Assert( g_pActiveStudioHdr );
if ( g_pActiveStudioHdr->pVertexBase )
{
return (vertexFileHeader_t *)g_pActiveStudioHdr->pVertexBase;
}
// mandatory callback to make requested data resident
// load and persist the vertex file
strcpy( fileName, "models/" );
strcat( fileName, g_pActiveStudioHdr->pszName() );
Q_StripExtension( fileName, fileName, sizeof( fileName ) );
strcat( fileName, ".vvd" );
// load the model
fileHandle = g_pFileSystem->Open( fileName, "rb" );
if ( !fileHandle )
{
Error( "Unable to load vertex data \"%s\"\n", fileName );
}
// Get the file size
int size = g_pFileSystem->Size( fileHandle );
if (size == 0)
{
g_pFileSystem->Close( fileHandle );
Error( "Bad size for vertex data \"%s\"\n", fileName );
}
pVvdHdr = (vertexFileHeader_t *)malloc(size);
g_pFileSystem->Read( pVvdHdr, size, fileHandle );
g_pFileSystem->Close( fileHandle );
// check header
if (pVvdHdr->id != MODEL_VERTEX_FILE_ID)
{
Error("Error Vertex File %s id %d should be %d\n", fileName, pVvdHdr->id, MODEL_VERTEX_FILE_ID);
}
if (pVvdHdr->version != MODEL_VERTEX_FILE_VERSION)
{
Error("Error Vertex File %s version %d should be %d\n", fileName, pVvdHdr->version, MODEL_VERTEX_FILE_VERSION);
}
if (pVvdHdr->checksum != g_pActiveStudioHdr->checksum)
{
Error("Error Vertex File %s checksum %d should be %d\n", fileName, pVvdHdr->checksum, g_pActiveStudioHdr->checksum);
}
g_pActiveStudioHdr->pVertexBase = (void*)pVvdHdr;
return pVvdHdr;
}