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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// TF Entity Spawner
//
//=============================================================================
#include "cbase.h"
#include "tf_entity_spawner.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// CEntitySpawnManager
//-----------------------------------------------------------------------------
BEGIN_DATADESC( CEntitySpawnManager )
// Fields
DEFINE_KEYFIELD( m_iszEntityName, FIELD_STRING, "entity_name" ),
DEFINE_KEYFIELD( m_iEntityCount, FIELD_INTEGER, "entity_count" ),
DEFINE_KEYFIELD( m_iRespawnTime, FIELD_INTEGER, "respawn_time" ),
DEFINE_KEYFIELD( m_bDropToGround, FIELD_BOOLEAN, "drop_to_ground" ),
DEFINE_KEYFIELD( m_bRandomRotation, FIELD_BOOLEAN, "random_rotation" ),
// Outputs
// ON RESPAWN ?
END_DATADESC()
LINK_ENTITY_TO_CLASS( entity_spawn_manager, CEntitySpawnManager );
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CEntitySpawnManager::Spawn( void )
{
BaseClass::Spawn();
SetNextThink( TICK_NEVER_THINK );
SetThink( NULL );
}
//-----------------------------------------------------------------------------
// Stores related spawn points.
//-----------------------------------------------------------------------------
void CEntitySpawnManager::RegisterSpawnPoint( CEntitySpawnPoint* pNewPoint )
{
m_SpawnPoints.AddToHead( pNewPoint );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CEntitySpawnManager::Activate( void )
{
BaseClass::Activate();
// AssertMsg1( 0, ("entity_spawn_manager active with %i points!"), m_SpawnPoints.Count() );
// Don't spawn more objects than we have points.
m_iMaxSpawnedEntities = MIN( m_SpawnPoints.Count(), m_iEntityCount );
if ( !m_iszEntityName || (m_iMaxSpawnedEntities == 0) )
{
AssertMsg1( 0, ("entity_spawn_manager %s active with nothing to spawn!"), GetEntityName().ToCStr() );
return;
}
// Perform initial spawn.
SpawnAllEntities();
}
//-----------------------------------------------------------------------------
// Populates a random set of spawn points with objects, up to our max.
//-----------------------------------------------------------------------------
void CEntitySpawnManager::SpawnAllEntities()
{
int iNumUsed = 0;
for ( int i=0; i<m_SpawnPoints.Count(); ++i )
{
if ( m_SpawnPoints[i]->IsUsed() )
{
iNumUsed++;
}
}
int iNumToSpawn = m_iMaxSpawnedEntities - iNumUsed;
if ( iNumToSpawn <= 0 )
return;
while ( iNumToSpawn )
{
SpawnEntity();
iNumToSpawn--;
}
}
//-----------------------------------------------------------------------------
// Spawns a single entity at a random location.
//-----------------------------------------------------------------------------
bool CEntitySpawnManager::SpawnEntity()
{
return SpawnEntityAt( GetRandomUnusedIndex() );
}
//-----------------------------------------------------------------------------
// Returns a random unused point.
//-----------------------------------------------------------------------------
int CEntitySpawnManager::GetRandomUnusedIndex()
{
int iStartIndex = rand() % m_SpawnPoints.Count();
for ( int i=0; i<m_SpawnPoints.Count(); ++i )
{
int index = i+iStartIndex;
if ( index >= m_SpawnPoints.Count() )
index -= m_SpawnPoints.Count();
CEntitySpawnPoint *pPoint = m_SpawnPoints[index];
if ( !pPoint || pPoint->IsUsed() )
continue;
else
return index;
}
return -1;
}
//-----------------------------------------------------------------------------
// Creates the entity.
//-----------------------------------------------------------------------------
bool CEntitySpawnManager::SpawnEntityAt( int iIndex )
{
if ( iIndex == -1 )
return false;
CEntitySpawnPoint *pPoint = m_SpawnPoints[iIndex];
if ( !pPoint )
return false;
CBaseEntity *pEnt = CreateEntityByName( m_iszEntityName.ToCStr() );
if ( !pEnt )
return false;
Vector origin = pPoint->GetAbsOrigin();
Vector mins, maxs, mins_o, maxs_o;
pEnt->CollisionProp()->WorldSpaceAABB( &mins, &maxs );
mins_o = origin + mins;
maxs_o = origin + mins;
if ( !UTIL_IsSpaceEmpty( pEnt, mins_o, maxs_o ) )
{
UTIL_Remove( pEnt );
return false;
}
if ( m_bDropToGround )
{
trace_t trace;
UTIL_TraceHull( origin, origin + Vector( 0, 0, -500 ), mins, maxs, MASK_SOLID, pEnt, COLLISION_GROUP_NONE, &trace );
origin = trace.endpos;
}
pEnt->SetAbsOrigin( origin );
DispatchSpawn( pEnt );
if ( m_bRandomRotation )
{
pEnt->SetAbsAngles( QAngle( 0, random->RandomFloat( 0, 360 ), 0 ) );
}
else
{
pEnt->SetAbsAngles( pPoint->GetAbsAngles() );
}
pPoint->SetEntity( pEnt );
return true;
}
//-----------------------------------------------------------------------------
// CEntitySpawnPoint
//-----------------------------------------------------------------------------
BEGIN_DATADESC( CEntitySpawnPoint )
// Fields
DEFINE_KEYFIELD( m_iszSpawnManagerName, FIELD_STRING, "spawn_manager_name" ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( entity_spawn_point, CEntitySpawnPoint );
//-----------------------------------------------------------------------------
// Initializes the spawn point and registers it with its manager.
//-----------------------------------------------------------------------------
void CEntitySpawnPoint::Spawn( void )
{
BaseClass::Spawn();
SetNextThink( TICK_NEVER_THINK );
SetThink( NULL );
if ( !m_iszSpawnManagerName )
{
AssertMsg( 0, ("entity_spawn_point with no spawn_manager_name!") );
return;
}
m_hSpawnManager = dynamic_cast<CEntitySpawnManager*>( gEntList.FindEntityByName( NULL, m_iszSpawnManagerName ) );
if ( !m_hSpawnManager )
{
AssertMsg2( 0, ("entity_spawn_point %s unable to find spawn_manager_name %s!"), GetEntityName().ToCStr(), m_iszSpawnManagerName.ToCStr() );
return;
}
m_hSpawnManager->RegisterSpawnPoint( this );
gEntList.AddListenerEntity( this );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CEntitySpawnPoint::UpdateOnRemove(void)
{
gEntList.RemoveListenerEntity( this );
BaseClass::UpdateOnRemove();
}
//-----------------------------------------------------------------------------
// When our entity is deleted, we become responsible for indicating when a new one should spawn.
//-----------------------------------------------------------------------------
void CEntitySpawnPoint::OnEntityDeleted( CBaseEntity *pEntity )
{
if ( !m_hSpawnManager )
return;
if ( pEntity == m_hMyEntity )
{
m_flNodeFree = gpGlobals->curtime + 10.f;
m_hMyEntity = NULL;
SetNextThink( gpGlobals->curtime + m_hSpawnManager->GetRespawnTime() );
SetThink( &CEntitySpawnPoint::RespawnNotifyThink );
}
}
void CEntitySpawnPoint::RespawnNotifyThink( void )
{
if ( (gpGlobals->curtime > m_flNodeFree) && m_hSpawnManager->SpawnEntity() )
{
SetThink( NULL );
SetNextThink( TICK_NEVER_THINK );
}
else
{
SetNextThink( gpGlobals->curtime + 5.f );
SetThink( &CEntitySpawnPoint::RespawnNotifyThink );
}
}
|