forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecals.cpp
345 lines (295 loc) · 8.43 KB
/
decals.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "decals.h"
#include "igamesystem.h"
#include "utlsymbol.h"
#include "utldict.h"
#include "KeyValues.h"
#include "filesystem.h"
#ifdef CLIENT_DLL
#include "iefx.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define DECAL_LIST_FILE "scripts/decals_subrect.txt"
//#define DECAL_LIST_FILE "scripts/decals.txt"
#define TRANSLATION_DATA_SECTION "TranslationData"
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CDecalEmitterSystem : public IDecalEmitterSystem, public CAutoGameSystem
{
public:
CDecalEmitterSystem( char const *name ) : CAutoGameSystem( name )
{
}
virtual bool Init();
virtual void Shutdown();
virtual void LevelInitPreEntity();
// Public interface
virtual int GetDecalIndexForName( char const *decalname );
virtual const char *GetDecalNameForIndex( int nIndex );
virtual char const *TranslateDecalForGameMaterial( char const *decalName, unsigned char gamematerial );
private:
char const *ImpactDecalForGameMaterial( int gamematerial );
void LoadDecalsFromScript( char const *filename );
void Clear();
struct DecalListEntry
{
DecalListEntry()
{
name = UTL_INVAL_SYMBOL;
precache_index = -1;
weight = 1.0f;
}
CUtlSymbol name;
int precache_index;
float weight;
};
struct DecalEntry
{
DecalEntry()
{
}
DecalEntry( const DecalEntry& src )
{
int c = src.indices.Count();
for ( int i = 0; i < c; i++ )
{
indices.AddToTail( src.indices[ i ] );
}
}
DecalEntry& operator = ( const DecalEntry& src )
{
if ( this == &src )
return *this;
int c = src.indices.Count();
for ( int i = 0; i < c; i++ )
{
indices.AddToTail( src.indices[ i ] );
}
return *this;
}
CUtlVector< int > indices;
};
CUtlVector< DecalListEntry > m_AllDecals;
CUtlDict< DecalEntry, int > m_Decals;
CUtlSymbolTable m_DecalFileNames;
CUtlDict< int, int > m_GameMaterialTranslation;
};
static CDecalEmitterSystem g_DecalSystem( "CDecalEmitterSystem" );
IDecalEmitterSystem *decalsystem = &g_DecalSystem;
//-----------------------------------------------------------------------------
// Purpose:
// Input : *decalname -
// Output : int
//-----------------------------------------------------------------------------
int CDecalEmitterSystem::GetDecalIndexForName( char const *decalname )
{
if ( !decalname || !decalname[ 0 ] )
return -1;
int idx = m_Decals.Find( decalname );
if ( idx == m_Decals.InvalidIndex() )
return -1;
DecalEntry *e = &m_Decals[ idx ];
Assert( e );
int count = e->indices.Count();
if ( count <= 0 )
return -1;
float totalweight = 0.0f;
int slot = 0;
for ( int i = 0; i < count; i++ )
{
int idx = e->indices[ i ];
DecalListEntry *item = &m_AllDecals[ idx ];
Assert( item );
if ( !totalweight )
{
slot = idx;
}
// Always assume very first slot will match
totalweight += item->weight;
if ( !totalweight || random->RandomFloat(0,totalweight) < item->weight )
{
slot = idx;
}
}
return m_AllDecals[ slot ].precache_index;
}
const char *CDecalEmitterSystem::GetDecalNameForIndex( int nIndex )
{
for ( int nDecal = 0; nDecal < m_AllDecals.Count(); ++nDecal )
{
if ( m_AllDecals[ nDecal ].precache_index == nIndex )
{
return m_DecalFileNames.String( m_AllDecals[ nDecal ].name );
}
}
return "";
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CDecalEmitterSystem::Init()
{
LoadDecalsFromScript( DECAL_LIST_FILE );
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDecalEmitterSystem::LevelInitPreEntity()
{
// Precache all entries
int c = m_AllDecals.Count();
for ( int i = 0 ; i < c; i++ )
{
DecalListEntry& e = m_AllDecals[ i ];
#if defined( CLIENT_DLL )
e.precache_index = effects->Draw_DecalIndexFromName( (char *)m_DecalFileNames.String( e.name ) );
#else
e.precache_index = engine->PrecacheDecal( m_DecalFileNames.String( e.name ) );
#endif
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *filename -
//-----------------------------------------------------------------------------
void CDecalEmitterSystem::LoadDecalsFromScript( char const *filename )
{
KeyValues *kv = new KeyValues( filename );
Assert( kv );
if ( kv )
{
KeyValues *translation = NULL;
#ifndef _XBOX
if ( kv->LoadFromFile( filesystem, filename ) )
#else
if ( kv->LoadFromFile( filesystem, filename, "GAME" ) )
#endif
{
KeyValues *p = kv;
while ( p )
{
if ( p->GetFirstSubKey() )
{
char const *keyname = p->GetName();
if ( !Q_stricmp( keyname, TRANSLATION_DATA_SECTION ) )
{
translation = p;
}
else
{
DecalEntry entry;
for ( KeyValues *sub = p->GetFirstSubKey(); sub != NULL; sub = sub->GetNextKey() )
{
MEM_ALLOC_CREDIT();
DecalListEntry decal;
decal.precache_index = -1;
decal.name = m_DecalFileNames.AddString( sub->GetName() );
decal.weight = sub->GetFloat();
// Add to global list
int idx = m_AllDecals.AddToTail( decal );
// Add index only to local list
entry.indices.AddToTail( idx );
}
// Add entry to main dictionary
m_Decals.Insert( keyname, entry );
}
}
p = p->GetNextKey();
}
}
else
{
Msg( "CDecalEmitterSystem::LoadDecalsFromScript: Unable to load '%s'\n", filename );
}
if ( !translation )
{
Msg( "CDecalEmitterSystem::LoadDecalsFromScript: Script '%s' missing section '%s'\n",
filename,
TRANSLATION_DATA_SECTION );
}
else
{
// Now parse game material to entry translation table
for ( KeyValues *sub = translation->GetFirstSubKey(); sub != NULL; sub = sub->GetNextKey() )
{
// Don't add NULL string to list
if ( !Q_stricmp( sub->GetString(), "" ) )
continue;
int idx = m_Decals.Find( sub->GetString() );
if ( idx != m_Decals.InvalidIndex() )
{
m_GameMaterialTranslation.Insert( sub->GetName(), idx );
}
else
{
Msg( "CDecalEmitterSystem::LoadDecalsFromScript: Translation for game material type '%s' references unknown decal '%s'\n",
sub->GetName(), sub->GetString() );
}
}
}
kv->deleteThis();
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : gamematerial -
// Output : char const
//-----------------------------------------------------------------------------
char const *CDecalEmitterSystem::ImpactDecalForGameMaterial( int gamematerial )
{
char gm[ 2 ];
gm[0] = (char)gamematerial;
gm[1] = 0;
int idx = m_GameMaterialTranslation.Find( gm );
if ( idx == m_GameMaterialTranslation.InvalidIndex() )
return NULL;
return m_Decals.GetElementName( m_GameMaterialTranslation.Element(idx) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDecalEmitterSystem::Shutdown()
{
Clear();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDecalEmitterSystem::Clear()
{
m_DecalFileNames.RemoveAll();
m_Decals.Purge();
m_AllDecals.Purge();
m_GameMaterialTranslation.Purge();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *decalName -
// gamematerial -
// Output : char const
//-----------------------------------------------------------------------------
char const *CDecalEmitterSystem::TranslateDecalForGameMaterial( char const *decalName, unsigned char gamematerial )
{
if ( gamematerial == CHAR_TEX_CONCRETE )
return decalName;
if ( !Q_stricmp( decalName, "Impact.Concrete" ) )
{
if ( gamematerial == '-' )
return "";
char const *d = ImpactDecalForGameMaterial( gamematerial );
if ( d )
{
return d;
}
}
return decalName;
}