summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_weapon_grenade_nail.cpp
blob: 5f1c2b2fa6a32cd4b2ea0a04ee0d906164aab97b (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: TF Nail Grenade.
//
//=============================================================================//
#include "cbase.h"
#include "tf_weaponbase.h"
#include "tf_gamerules.h"
#include "npcevent.h"
#include "engine/IEngineSound.h"
#include "tf_weapon_grenade_nail.h"

// Server specific.
#ifdef GAME_DLL
#include "tf_player.h"
#include "items.h"
#include "tf_weaponbase_grenadeproj.h"
#include "soundent.h"
#include "KeyValues.h"
#include "tf_projectile_nail.h"
#include "physics_saverestore.h"
#include "phys_controller.h"
#endif

#define GRENADE_NAIL_TIMER	3.0f //Seconds

//=============================================================================
//
// TF Nail Grenade tables.
//

IMPLEMENT_NETWORKCLASS_ALIASED( TFGrenadeNail, DT_TFGrenadeNail )

BEGIN_NETWORK_TABLE( CTFGrenadeNail, DT_TFGrenadeNail )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CTFGrenadeNail )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( tf_weapon_grenade_nail, CTFGrenadeNail );
PRECACHE_WEAPON_REGISTER( tf_weapon_grenade_nail );

//=============================================================================
//
// TF Nail Grenade functions.
//

// Server specific.
#ifdef GAME_DLL

BEGIN_DATADESC( CTFGrenadeNail )
END_DATADESC()

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CTFWeaponBaseGrenadeProj *CTFGrenadeNail::EmitGrenade( Vector vecSrc, QAngle vecAngles, Vector vecVel, 
							        AngularImpulse angImpulse, CBasePlayer *pPlayer, float flTime, int iflags )
{
	return CTFGrenadeNailProjectile::Create( vecSrc, vecAngles, vecVel, angImpulse, 
		                              pPlayer, GetTFWpnData(), flTime );
}

#endif

//=============================================================================
//
// TF Nail Grenade Projectile functions (Server specific).
//
#ifdef GAME_DLL

BEGIN_DATADESC( CTFGrenadeNailProjectile )
	DEFINE_THINKFUNC( EmitNails ),
	DEFINE_EMBEDDED( m_GrenadeController ),
	DEFINE_PHYSPTR( m_pMotionController ),
END_DATADESC()

#define GRENADE_MODEL "models/weapons/w_models/w_grenade_nail.mdl"

LINK_ENTITY_TO_CLASS( tf_weapon_grenade_nail_projectile, CTFGrenadeNailProjectile );
PRECACHE_WEAPON_REGISTER( tf_weapon_grenade_nail_projectile );

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CTFGrenadeNailProjectile* CTFGrenadeNailProjectile::Create( const Vector &position, const QAngle &angles, 
																const Vector &velocity, const AngularImpulse &angVelocity, 
																CBaseCombatCharacter *pOwner, const CTFWeaponInfo &weaponInfo, float timer, int iFlags )
{
	// Nail grenades are always thrown like discs
	QAngle vecCustomAngles = angles;
	vecCustomAngles.x = clamp( vecCustomAngles.x, -10,10 );
	vecCustomAngles.z = clamp( vecCustomAngles.x, -10,10 );
	Vector vecCustomAngVelocity = vec3_origin;
	vecCustomAngVelocity.z = RandomFloat( -600, 600 );
	CTFGrenadeNailProjectile *pGrenade = static_cast<CTFGrenadeNailProjectile*>( CTFWeaponBaseGrenadeProj::Create( "tf_weapon_grenade_nail_projectile", position, vecCustomAngles, velocity, vecCustomAngVelocity, pOwner, weaponInfo, timer, iFlags ) );
	return pGrenade;
}

CTFGrenadeNailProjectile::~CTFGrenadeNailProjectile()
{
	if ( m_pMotionController != NULL )
	{
		physenv->DestroyMotionController( m_pMotionController );
		m_pMotionController = NULL;
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFGrenadeNailProjectile::Spawn()
{
	SetModel( GRENADE_MODEL );

	m_pMotionController = NULL;

	UseClientSideAnimation();
		
	BaseClass::Spawn();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFGrenadeNailProjectile::Precache()
{
	PrecacheModel( GRENADE_MODEL );

	PrecacheScriptSound( "Weapon_Grenade_Nail.Launch" );

	BaseClass::Precache();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFGrenadeNailProjectile::BounceSound( void )
{
	EmitSound( "Weapon_Grenade_Nail.Bounce" );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTFGrenadeNailProjectile::Detonate()
{
	if ( ShouldNotDetonate() )
	{
		RemoveGrenade();
		return;
	}

	StartEmittingNails();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CTFGrenadeNailProjectile::OnTakeDamage( const CTakeDamageInfo &info )
{
	if ( m_pMotionController != NULL )
	{
		// motioncontroller is animating us, dont take hits that will disorient us
		return 0;
	}

	return BaseClass::OnTakeDamage( info );
}

void CTFGrenadeNailProjectile::StartEmittingNails( void )
{
	// 0.4 seconds later, emit nails
	IPhysicsObject *pPhysicsObject = VPhysicsGetObject();

	if ( pPhysicsObject )
	{
		m_pMotionController = physenv->CreateMotionController( &m_GrenadeController );
		m_pMotionController->AttachObject( pPhysicsObject, true );

		pPhysicsObject->EnableGravity( false );

		pPhysicsObject->Wake();
	}

	QAngle ang(0,0,0);
	Vector pos = GetAbsOrigin();
	pos.z += 32;
	m_GrenadeController.SetDesiredPosAndOrientation( pos, ang );

	m_flNailAngle = 0;
	m_iNumNailBurstsLeft = 40;

	int animDesired = SelectWeightedSequence( ACT_RANGE_ATTACK1 );
	ResetSequence( animDesired );
	SetPlaybackRate( 1.0 );

	Vector soundPosition = GetAbsOrigin() + Vector( 0, 0, 5 );
	CPASAttenuationFilter filter( soundPosition );
	EmitSound( filter, entindex(), "Weapon_Grenade_Nail.Launch" );

#ifdef GAME_DLL
	SetThink( &CTFGrenadeNailProjectile::EmitNails );
	SetNextThink( gpGlobals->curtime + 0.4 );
#endif
}

void CTFGrenadeNailProjectile::EmitNails( void )
{
	m_iNumNailBurstsLeft--;

	if ( m_iNumNailBurstsLeft < 0 )
	{
		BaseClass::Detonate();
		return;
	}

	Vector forward, up;
	float flAngleToAdd = random->RandomFloat( 30, 40 );

	// else release some nails
	for ( int i=0; i < 4 ;i++ )
	{
		m_flNailAngle = UTIL_AngleMod( m_flNailAngle + flAngleToAdd );

		QAngle angNail( random->RandomFloat( -3, 3 ), m_flNailAngle, 0 );

		// Emit a nail
		CTFProjectile_Nail *pNail = CTFProjectile_Nail::Create( GetAbsOrigin(), angNail, this, GetThrower() );	
		if ( pNail )
		{
			pNail->SetDamage( 18 );
		}
	}

	SetNextThink( gpGlobals->curtime + 0.1 );
}

IMotionEvent::simresult_e CNailGrenadeController::Simulate( IPhysicsMotionController *pController, IPhysicsObject *pObject, float deltaTime, Vector &linear, AngularImpulse &angular )
{
	// Try to get to m_vecDesiredPosition

	// Try to orient ourselves to m_angDesiredOrientation

	Vector currentPos;
	QAngle currentAng;

	pObject->GetPosition( &currentPos, &currentAng );

	Vector vecVel;
	AngularImpulse angVel;
	pObject->GetVelocity( &vecVel, &angVel );

	linear.Init();
	angular.Init();

	if ( m_bReachedPos )
	{
		// Lock at this height
		if ( vecVel.Length() > 1.0 )
		{
			AngularImpulse nil( 0,0,0 );
			pObject->SetVelocityInstantaneous( &vec3_origin, &nil );

			// For now teleport to the proper orientation
			currentAng.x = 0;
			currentAng.y = 0;
			currentAng.z = 0;
			pObject->SetPosition( currentPos, currentAng, true );
		}
	}
	else
	{
		// not at the right height yet, keep moving up
		linear.z =  50 * ( m_vecDesiredPosition.z - currentPos.z );

		if ( currentPos.z > m_vecDesiredPosition.z )
		{
			// lock into position
			m_bReachedPos = true;
		}

		// Start rotating in the right direction
		// we'll lock angles once we reach proper height to stop the oscillating
		matrix3x4_t matrix;
		// get the object's local to world transform
		pObject->GetPositionMatrix( &matrix );

		Vector m_worldGoalAxis(0,0,1);

		// Get the alignment axis in object space
		Vector currentLocalTargetAxis;
		VectorIRotate( m_worldGoalAxis, matrix, currentLocalTargetAxis );

		float invDeltaTime = (1/deltaTime);
		float m_angularLimit = 10;

		angular = ComputeRotSpeedToAlignAxes( m_worldGoalAxis, currentLocalTargetAxis, angVel, 1.0, invDeltaTime * invDeltaTime, m_angularLimit * invDeltaTime );
	}

	return SIM_GLOBAL_ACCELERATION;
}

void CNailGrenadeController::SetDesiredPosAndOrientation( Vector pos, QAngle orientation )
{
	m_vecDesiredPosition = pos;
	m_angDesiredOrientation = orientation;

	m_bReachedPos = false;
	m_bReachedOrientation = false;
}

BEGIN_SIMPLE_DATADESC( CNailGrenadeController )
END_DATADESC()

#endif