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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "tf_gamerules.h"
#include "entity_bird.h"
#include "KeyValues.h"
#include "filesystem.h"
#include "tf_fx.h"
LINK_ENTITY_TO_CLASS( entity_bird, CEntityBird );
#define ENTITYBIRD_MODEL "models/props_forest/bird.mdl"
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEntityBird::Spawn( void )
{
Precache();
BaseClass::Spawn();
SetMoveType( MOVETYPE_NONE );
SetSolid( SOLID_BBOX );
SetModel( ENTITYBIRD_MODEL );
SetSize( -Vector(8,8,0), Vector(8,8,16) );
SetSequence(0);
m_takedamage = DAMAGE_YES;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEntityBird::Precache( void )
{
BaseClass::Precache();
// We deliberately allow late precaches here. It'll cause a hitch, but it'll ensure
// we don't load the model on any map that doesn't have birds, and we'll make sure
// we don't accidentally keep loading it after the birds are supposed to go away.
bool bAllowPrecache = CBaseEntity::IsPrecacheAllowed();
CBaseEntity::SetAllowPrecache( true );
PrecacheModel( ENTITYBIRD_MODEL );
CBaseEntity::SetAllowPrecache( bAllowPrecache );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEntityBird::Touch( CBaseEntity *pOther )
{
BaseClass::Touch( pOther );
// If we're touched by anything, we pop!
Explode();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CEntityBird::OnTakeDamage( const CTakeDamageInfo &info )
{
Explode();
return 1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEntityBird::Explode( void )
{
Vector vecOrigin = WorldSpaceCenter();
CPVSFilter filter( vecOrigin );
TE_TFExplosion( filter, 0.0f, vecOrigin, Vector(0,0,1), TF_WEAPON_NONE, 0 );
UTIL_Remove( this );
}
//-----------------------------------------------------------------------------
// Purpose: Spawn random birds at locations on certain maps.
//-----------------------------------------------------------------------------
void CEntityBird::SpawnRandomBirds( void )
{
const char *pszMapName = STRING( gpGlobals->mapname );
if ( !pszMapName || !pszMapName[0] )
return;
KeyValues *pFileKV = new KeyValues( "birds" );
if ( !pFileKV->LoadFromFile( g_pFullFileSystem, "scripts/birds.txt", "MOD" ) )
return;
// Build a list of birds in the map already, and make sure we don't spawn any on those spots again
CUtlVector<Vector> vecExistingBirds;
CBaseEntity *pBird = gEntList.FindEntityByClassname( NULL, "entity_bird" );
while( pBird )
{
vecExistingBirds.AddToTail( pBird->GetAbsOrigin() );
pBird = gEntList.FindEntityByClassname( pBird, "entity_bird" );
}
// See if we have an entry for this map
KeyValues *pMapKV = pFileKV->FindKey( pszMapName );
if ( pMapKV )
{
CUtlVector<Vector> vecLocations;
KeyValues *pkvLoc = pMapKV->GetFirstSubKey();
while ( pkvLoc )
{
const char *pszLocation = pkvLoc->GetString();
int iIdx = vecLocations.AddToTail();
UTIL_StringToVector( vecLocations[iIdx].Base(), pszLocation );
pkvLoc = pkvLoc->GetNextKey();
}
if ( vecLocations.Count() )
{
//int iLocation = RandomInt(0,vecLocations.Count()-1);
for ( int i = 0; i < vecLocations.Count(); i++ )
{
bool bExists = false;
// Make sure there isn't a bird here already
FOR_EACH_VEC( vecExistingBirds, iBird )
{
if ( vecLocations[i].DistToSqr(vecExistingBirds[iBird]) < (32*32) )
{
bExists = true;
break;
}
}
if ( !bExists )
{
CBaseEntity::Create( "entity_bird", vecLocations[i], QAngle(0,RandomFloat(0,360),0) );
}
}
}
}
pFileKV->deleteThis();
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#define ENTITY_FLYING_BIRD_SPEED_MIN 200
#define ENTITY_FLYING_BIRD_SPEED_MAX 500
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void SpawnClientsideFlyingBird( Vector &vecSpawn )
{
float flyAngle = RandomFloat( -M_PI, M_PI );
float flyAngleRate = RandomFloat( -1.5f, 1.5f );
float accelZ = RandomFloat( 0.5f, 2.0f );
float speed = RandomFloat( ENTITY_FLYING_BIRD_SPEED_MIN, ENTITY_FLYING_BIRD_SPEED_MAX );
float flGlideTime = RandomFloat( 0.25f, 1.0f );
bool bAllowPrecache = CBaseEntity::IsPrecacheAllowed();
CBaseEntity::SetAllowPrecache( true );
CBaseEntity::PrecacheModel( "models/props_forest/dove.mdl" );
CBaseEntity::SetAllowPrecache( bAllowPrecache );
CPVSFilter filter( vecSpawn );
UserMessageBegin( filter, "SpawnFlyingBird" );
WRITE_VEC3COORD( vecSpawn );
WRITE_FLOAT( flyAngle );
WRITE_FLOAT( flyAngleRate );
WRITE_FLOAT( accelZ );
WRITE_FLOAT( speed );
WRITE_FLOAT( flGlideTime );
MessageEnd();
}
|