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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Point entity used to create templates out of other entities or groups of entities
//
//=============================================================================//
#include "cbase.h"
#include "entityinput.h"
#include "entityoutput.h"
#include "TemplateEntities.h"
#include "point_template.h"
#include "saverestore_utlvector.h"
#include "mapentities.h"
#include "tier0/icommandline.h"
#include "mapentities_shared.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define SF_POINTTEMPLATE_DONTREMOVETEMPLATEENTITIES 0x0001
// Level designers can suppress the uniquification of the spawned entity
// names with a spawnflag, provided they guarantee that only one instance
// of the entities will ever be spawned at a time.
#define SF_POINTTEMPLATE_PRESERVE_NAMES 0x0002
LINK_ENTITY_TO_CLASS(point_template, CPointTemplate);
BEGIN_SIMPLE_DATADESC( template_t )
DEFINE_FIELD( iTemplateIndex, FIELD_INTEGER ),
DEFINE_FIELD( matEntityToTemplate, FIELD_VMATRIX ),
END_DATADESC()
BEGIN_DATADESC( CPointTemplate )
// Keys
// Silence, Classcheck!
// DEFINE_ARRAY( m_iszTemplateEntityNames, FIELD_STRING, MAX_NUM_TEMPLATES ),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[0], FIELD_STRING, "Template01"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[1], FIELD_STRING, "Template02"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[2], FIELD_STRING, "Template03"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[3], FIELD_STRING, "Template04"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[4], FIELD_STRING, "Template05"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[5], FIELD_STRING, "Template06"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[6], FIELD_STRING, "Template07"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[7], FIELD_STRING, "Template08"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[8], FIELD_STRING, "Template09"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[9], FIELD_STRING, "Template10"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[10], FIELD_STRING, "Template11"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[11], FIELD_STRING, "Template12"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[12], FIELD_STRING, "Template13"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[13], FIELD_STRING, "Template14"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[14], FIELD_STRING, "Template15"),
DEFINE_KEYFIELD( m_iszTemplateEntityNames[15], FIELD_STRING, "Template16"),
DEFINE_UTLVECTOR( m_hTemplateEntities, FIELD_CLASSPTR ),
DEFINE_UTLVECTOR( m_hTemplates, FIELD_EMBEDDED ),
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "ForceSpawn", InputForceSpawn ),
// Outputs
DEFINE_OUTPUT( m_pOutputOnSpawned, "OnEntitySpawned" ),
END_DATADESC()
//-----------------------------------------------------------------------------
// Purpose: A simple system to help precache point_template entities ... ywb
//-----------------------------------------------------------------------------
class CPointTemplatePrecacher : public CAutoGameSystem
{
public:
CPointTemplatePrecacher( char const *name ) : CAutoGameSystem( name )
{
}
void AddToPrecache( CBaseEntity *ent )
{
m_Ents.AddToTail( EHANDLE( ent ) );
}
virtual void LevelInitPreEntity()
{
m_Ents.RemoveAll();
}
virtual void Shutdown()
{
m_Ents.RemoveAll();
}
void Precache()
{
int c = m_Ents.Count();
for ( int i = 0 ; i < c; ++i )
{
CPointTemplate *ent = m_Ents[ i ].Get();
if ( ent )
{
ent->PerformPrecache();
}
}
m_Ents.RemoveAll();
}
private:
CUtlVector< CHandle< CPointTemplate > > m_Ents;
};
CPointTemplatePrecacher g_PointTemplatePrecacher( "CPointTemplatePrecacher" );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void PrecachePointTemplates()
{
g_PointTemplatePrecacher.Precache();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPointTemplate::Spawn( void )
{
Precache();
}
void CPointTemplate::Precache()
{
// We can't call precache right when we instance the template, we need to defer it until after all map entities have
// been loaded, so add this template to a list which is cleared after map entity parsing is completed.
g_PointTemplatePrecacher.AddToPrecache( this );
}
//-----------------------------------------------------------------------------
// Purpose: Level designers can suppress the uniquification of the spawned entity
// names with a spawnflag, provided they guarantee that only one instance
// of the entities will ever be spawned at a time.
//-----------------------------------------------------------------------------
bool CPointTemplate::AllowNameFixup()
{
return !HasSpawnFlags( SF_POINTTEMPLATE_PRESERVE_NAMES );
}
//-----------------------------------------------------------------------------
// Purpose: Called at the start of template initialization for this point_template.
// Find all the entities referenced by this point_template, which will
// then be turned into templates by the map-parsing code.
//-----------------------------------------------------------------------------
void CPointTemplate::StartBuildingTemplates( void )
{
// Build our list of template entities
for ( int i = 0; i < MAX_NUM_TEMPLATES; i++ )
{
if ( m_iszTemplateEntityNames[i] != NULL_STRING )
{
CBaseEntity *pEntity = NULL;
int iOldNum = m_hTemplateEntities.Count();
// Add all the entities with the matching targetname
while ( (pEntity = gEntList.FindEntityByName( pEntity, STRING(m_iszTemplateEntityNames[i]) )) != NULL )
{
m_hTemplateEntities.AddToTail( pEntity );
}
// Useful mapmaker warning
if ( iOldNum == m_hTemplateEntities.Count() )
{
Warning( "Couldn't find any entities named %s, which point_template %s is specifying.\n", STRING(m_iszTemplateEntityNames[i]), STRING(GetEntityName()) );
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Called at the end of template initialization for this point_template.
// All of our referenced entities have now been destroyed.
//-----------------------------------------------------------------------------
void CPointTemplate::FinishBuildingTemplates( void )
{
// Our template entities are now gone, deleted by the server post turning them into templates.
m_hTemplateEntities.Purge();
// Now tell the template system to hook up all the Entity I/O connections within our set of templates.
Templates_ReconnectIOForGroup( this );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPointTemplate::AddTemplate( CBaseEntity *pEntity, const char *pszMapData, int nLen )
{
// Add it to the template list
int iIndex = Templates_Add( pEntity, pszMapData, nLen );
if ( iIndex == -1 )
{
Warning( "point_template %s failed to add template.\n", STRING(GetEntityName()) );
return;
}
template_t newTemplate;
newTemplate.iTemplateIndex = iIndex;
// Store the entity's origin & angles in a matrix in the template's local space
VMatrix matTemplateToWorld, matWorldToTemplate, matEntityToWorld, matEntityToTemplate;
matTemplateToWorld.SetupMatrixOrgAngles( GetAbsOrigin(), GetAbsAngles() );
matTemplateToWorld.InverseTR( matWorldToTemplate );
matEntityToWorld.SetupMatrixOrgAngles( pEntity->GetAbsOrigin(), pEntity->GetAbsAngles() );
MatrixMultiply( matWorldToTemplate, matEntityToWorld, matEntityToTemplate );
newTemplate.matEntityToTemplate = matEntityToTemplate;
m_hTemplates.AddToTail( newTemplate );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CPointTemplate::ShouldRemoveTemplateEntities( void )
{
return ( !(m_spawnflags & SF_POINTTEMPLATE_DONTREMOVETEMPLATEENTITIES) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CPointTemplate::GetNumTemplates( void )
{
return m_hTemplates.Count();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CPointTemplate::GetTemplateIndexForTemplate( int iTemplate )
{
Assert( iTemplate < m_hTemplates.Count() );
return m_hTemplates[iTemplate].iTemplateIndex;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CPointTemplate::GetNumTemplateEntities( void )
{
return m_hTemplateEntities.Count();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBaseEntity *CPointTemplate::GetTemplateEntity( int iTemplateNumber )
{
Assert( iTemplateNumber < m_hTemplateEntities.Count() );
return m_hTemplateEntities[iTemplateNumber];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPointTemplate::PerformPrecache()
{
// Go through all our templated map data and precache all the entities in it
int iTemplates = m_hTemplates.Count();
if ( !iTemplates )
{
Msg("Precache called on a point_template that has no templates: %s\n", STRING(GetEntityName()) );
return;
}
// Tell the template system we're about to start a new template
Templates_StartUniqueInstance();
//HierarchicalSpawn_t *pSpawnList = (HierarchicalSpawn_t*)stackalloc( iTemplates * sizeof(HierarchicalSpawn_t) );
int i;
for ( i = 0; i < iTemplates; i++ )
{
//CBaseEntity *pEntity = NULL;
char *pMapData;
int iTemplateIndex = m_hTemplates[i].iTemplateIndex;
// Some templates have Entity I/O connecting the entities within the template.
// Unique versions of these templates need to be created whenever they're instanced.
int nStringSize;
if ( AllowNameFixup() && Templates_IndexRequiresEntityIOFixup( iTemplateIndex ) )
{
// This template requires instancing.
// Create a new mapdata block and ask the template system to fill it in with
// a unique version (with fixed Entity I/O connections).
pMapData = Templates_GetEntityIOFixedMapData( iTemplateIndex );
}
else
{
// Use the unmodified mapdata
pMapData = (char*)STRING( Templates_FindByIndex( iTemplateIndex ) );
}
nStringSize = Templates_GetStringSize( iTemplateIndex );
// Create the entity from the mapdata
MapEntity_PrecacheEntity( pMapData, nStringSize );
}
}
//-----------------------------------------------------------------------------
// Purpose: Spawn the entities I contain
// Input : &vecOrigin -
// &vecAngles -
// pEntities -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CPointTemplate::CreateInstance( const Vector &vecOrigin, const QAngle &vecAngles, CUtlVector<CBaseEntity*> *pEntities )
{
// Go through all our templated map data and spawn all the entities in it
int iTemplates = m_hTemplates.Count();
if ( !iTemplates )
{
Msg("CreateInstance called on a point_template that has no templates: %s\n", STRING(GetEntityName()) );
return false;
}
// Tell the template system we're about to start a new template
Templates_StartUniqueInstance();
HierarchicalSpawn_t *pSpawnList = (HierarchicalSpawn_t*)stackalloc( iTemplates * sizeof(HierarchicalSpawn_t) );
int i;
for ( i = 0; i < iTemplates; i++ )
{
CBaseEntity *pEntity = NULL;
char *pMapData;
int iTemplateIndex = m_hTemplates[i].iTemplateIndex;
// Some templates have Entity I/O connecting the entities within the template.
// Unique versions of these templates need to be created whenever they're instanced.
if ( AllowNameFixup() && Templates_IndexRequiresEntityIOFixup( iTemplateIndex ) )
{
// This template requires instancing.
// Create a new mapdata block and ask the template system to fill it in with
// a unique version (with fixed Entity I/O connections).
pMapData = Templates_GetEntityIOFixedMapData( iTemplateIndex );
}
else
{
// Use the unmodified mapdata
pMapData = (char*)STRING( Templates_FindByIndex( iTemplateIndex ) );
}
// Create the entity from the mapdata
MapEntity_ParseEntity( pEntity, pMapData, NULL );
if ( pEntity == NULL )
{
Msg("Failed to initialize templated entity with mapdata: %s\n", pMapData );
return false;
}
// Get a matrix that'll convert from world to the new local space
VMatrix matNewTemplateToWorld, matStoredLocalToWorld;
matNewTemplateToWorld.SetupMatrixOrgAngles( vecOrigin, vecAngles );
MatrixMultiply( matNewTemplateToWorld, m_hTemplates[i].matEntityToTemplate, matStoredLocalToWorld );
// Get the world origin & angles from the stored local coordinates
Vector vecNewOrigin;
QAngle vecNewAngles;
vecNewOrigin = matStoredLocalToWorld.GetTranslation();
MatrixToAngles( matStoredLocalToWorld, vecNewAngles );
// Set its origin & angles
pEntity->SetAbsOrigin( vecNewOrigin );
pEntity->SetAbsAngles( vecNewAngles );
pSpawnList[i].m_pEntity = pEntity;
pSpawnList[i].m_nDepth = 0;
pSpawnList[i].m_pDeferredParent = NULL;
}
SpawnHierarchicalList( iTemplates, pSpawnList, true );
for ( i = 0; i < iTemplates; ++i )
{
if ( pSpawnList[i].m_pEntity )
{
pEntities->AddToTail( pSpawnList[i].m_pEntity );
}
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : &inputdata -
//-----------------------------------------------------------------------------
void CPointTemplate::InputForceSpawn( inputdata_t &inputdata )
{
// Spawn our template
CUtlVector<CBaseEntity*> hNewEntities;
if ( !CreateInstance( GetAbsOrigin(), GetAbsAngles(), &hNewEntities ) )
return;
// Fire our output
m_pOutputOnSpawned.FireOutput( this, this );
}
|