summaryrefslogtreecommitdiff
path: root/game/client/tf2/c_objectsentrygun.cpp
blob: 9d47a76315277945e7198b942d89d2be4774b5a3 (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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Client's CObjectSentrygun
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "bone_setup.h"
#include "CommanderOverlay.h"
#include "c_baseobject.h"
#include "C_Obj_SentryGun.h"
#include "tf_shareddefs.h"
#include "c_basetfplayer.h"
#include "ObjectControlPanel.h"
#include <vgui_controls/Button.h>

inline float UTIL_AngleMod(float a)
{
	return anglemod(a);
}

//-----------------------------------------------------------------------------
// Purpose: Base Sentrygun
//-----------------------------------------------------------------------------
BEGIN_RECV_TABLE_NOBASE(C_ObjectSentrygun, DT_SentrygunTeamOnlyVars)
	RecvPropInt(RECVINFO( m_iAmmo )),
END_RECV_TABLE()

IMPLEMENT_CLIENTCLASS_DT(C_ObjectSentrygun, DT_ObjectSentrygun, CObjectSentrygun)
	RecvPropInt( RECVINFO( m_iBaseTurnRate ) ),
	RecvPropEHandle(RECVINFO(m_hEnemy)),
	RecvPropDataTable( "teamonly", 0, 0, &REFERENCE_RECV_TABLE( DT_SentrygunTeamOnlyVars )),
	RecvPropInt(RECVINFO(m_bTurtled)),
 	RecvPropInt( RECVINFO( m_nAnimationParity ) ),
	RecvPropInt( RECVINFO( m_nOrientationParity ) ),
END_RECV_TABLE()

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_ObjectSentrygun::C_ObjectSentrygun()
{
	m_fBoneXRotator = 0;
	m_fBoneYRotator = 0;
	m_iAmmo = 0;
	m_bTurtled = false;
	m_flStartedTurtlingAt = 0;
	m_flStartedUnTurtlingAt = 0;

	SetViewOffset( Vector(0,0,22) );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::SetDormant( bool bDormant )
{
	BaseClass::SetDormant( bDormant );
	ENTITY_PANEL_ACTIVATE( "sentrygun", !bDormant );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int C_ObjectSentrygun::DrawModel( int flags )
{
	float flRealOriginZ = GetLocalOrigin().z;

	// If we're turtling, slide the model into the ground
	if ( m_bTurtled )
	{
		// How far down are we?
		float flTime = MIN( gpGlobals->curtime - m_flStartedTurtlingAt, SENTRY_TURTLE_TIME );
		float flPercent = 1 - (SENTRY_TURTLE_TIME - flTime) / SENTRY_TURTLE_TIME;

		// FIXME: This is totally wrong!!!
		Vector vNewOrigin = GetLocalOrigin();
		vNewOrigin.z -= (CollisionProp()->OBBSize().z * flPercent);
		SetLocalOrigin( vNewOrigin );
		InvalidateBoneCache();
	}
	else if ( !m_bTurtled )
	{
		if ( m_flStartedUnTurtlingAt )
		{
			float flTime = MIN( gpGlobals->curtime - m_flStartedUnTurtlingAt, SENTRY_TURTLE_TIME );
			float flPercent = (SENTRY_TURTLE_TIME - flTime) / SENTRY_TURTLE_TIME;
			
		// FIXME: This is totally wrong!!!
			Vector vNewOrigin = GetLocalOrigin();
			vNewOrigin.z -= (CollisionProp()->OBBSize().z * flPercent);
			SetLocalOrigin( vNewOrigin );
			InvalidateBoneCache();

			// Fully unturtled?
			if ( flTime >= SENTRY_TURTLE_TIME )
			{
				m_flStartedUnTurtlingAt = 0;
			}
		}
	}

	int drawn = BaseClass::DrawModel( flags );
	SetLocalOriginDim( Z_INDEX, flRealOriginZ );

	return drawn;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::PreDataUpdate( DataUpdateType_t updateType )
{
	BaseClass::PreDataUpdate( updateType );

	m_bLastTurtled = m_bTurtled;
	m_nLastAnimationParity = m_nAnimationParity;
	m_angPrevLocalAngles = GetLocalAngles();
	m_nPrevOrientationParity = 	m_nOrientationParity;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::PostDataUpdate( DataUpdateType_t updateType )
{
	BaseClass::PostDataUpdate( updateType );

	if ( m_bLastTurtled != m_bTurtled )
	{
		if ( m_bTurtled )
		{
			m_flStartedTurtlingAt = gpGlobals->curtime;
			m_flStartedUnTurtlingAt = 0;
		}
		else
		{
			m_flStartedUnTurtlingAt = gpGlobals->curtime;
			m_flStartedTurtlingAt = 0;
		}
	}

	if ( m_nLastAnimationParity != m_nAnimationParity )
 	{
 		SetCycle( 0.0f );
 	}

	bool changed = false;
	QAngle angleDiff;
	angleDiff = ( GetAbsAngles() - m_angPrevLocalAngles );
	for (int i = 0;i < 3; i++ )
	{
		angleDiff[i] = UTIL_AngleMod( angleDiff[ i ] );
	}

	if ( angleDiff.Length() > 0.1f )
	{
		changed = true;
	}
	if ( updateType == DATA_UPDATE_CREATED || changed )
	{
		// Orient it
		m_vecCurAngles.y = UTIL_AngleMod( GetLocalAngles().y );
		RecomputeOrientation();
	}
	else if ( m_nPrevOrientationParity != m_nOrientationParity )
	{
		if ( changed )
		{
			RecomputeOrientation();
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::OnDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnDataChanged( updateType );

	if ( updateType == DATA_UPDATE_CREATED )
	{
		// Start thinking (Baseclass stops it)
		SetNextClientThink( CLIENT_THINK_ALWAYS );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::FinishedBuilding( void )
{
	BaseClass::FinishedBuilding();

	EmitSound( "ObjectSentrygun.Activate" );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::GetBoneControllers(float controllers[MAXSTUDIOBONECTRLS])
{
	studiohdr_t *pModel = modelinfo->GetStudiomodel( GetModel() );

	// When yaw preview is on,
	if (!IsPreviewingYaw())
	{
		Studio_SetController(pModel, 0, m_fBoneXRotator, controllers[0]);
	}
	else
	{
		// Bone rotation == 0 here to make it exactly match the preview
		Studio_SetController(pModel, 0, 0, controllers[0]);
	}

	Studio_SetController(pModel, 1, m_fBoneYRotator, controllers[1]);
	Studio_SetController(pModel, 2, m_fBoneYRotator, controllers[2]);
	Studio_SetController(pModel, 3, m_fBoneYRotator, controllers[3]);
}

//-----------------------------------------------------------------------------
// Purpose: This is called to get the initial builder yaw...
//-----------------------------------------------------------------------------
float C_ObjectSentrygun::GetInitialBuilderYaw()
{
	// Take the current rotation into account
	return GetAbsAngles().y + m_fBoneXRotator;
}

//-----------------------------------------------------------------------------
// Called when a rotation happens
//-----------------------------------------------------------------------------
void C_ObjectSentrygun::RecomputeOrientation( )
{
	m_iRightBound = UTIL_AngleMod( m_vecCurAngles.y - 50);
	m_iLeftBound = UTIL_AngleMod( m_vecCurAngles.y + 50);
	if ( m_iRightBound > m_iLeftBound )
	{
		m_iRightBound = m_iLeftBound;
		m_iLeftBound = UTIL_AngleMod( m_vecCurAngles.y - 50);
	}

	// Start it rotating
	m_vecGoalAngles.y = m_iRightBound;;
	m_vecGoalAngles.x = m_vecCurAngles.x = 0;

	m_fBoneXRotator = 0.0f;
	m_fBoneYRotator = 0.0f;

	m_bTurningRight = true;
}


//-----------------------------------------------------------------------------
// Purpose: Handle movement of the turret
//-----------------------------------------------------------------------------
bool C_ObjectSentrygun::MoveTurret(void)
{
	bool bMoved = 0;

	float turnrate = (float)(m_iBaseTurnRate) * 10.0f;
	turnrate *= gpGlobals->frametime;

	// any x movement?
	if ( m_vecCurAngles.x != m_vecGoalAngles.x )
	{
		float flDir = m_vecGoalAngles.x > m_vecCurAngles.x ? 1 : -1 ;

		m_vecCurAngles.x += 0.1 * (turnrate * 5) * flDir;

		// if we started below the goal, and now we're past, peg to goal
		if (flDir == 1)
		{
			if (m_vecCurAngles.x > m_vecGoalAngles.x)
				m_vecCurAngles.x = m_vecGoalAngles.x;
		} 
		else
		{
			if (m_vecCurAngles.x < m_vecGoalAngles.x)
				m_vecCurAngles.x = m_vecGoalAngles.x;
		}

		m_fBoneYRotator = m_vecCurAngles.x;

		bMoved = 1;
	}

	if ( m_vecCurAngles.y != m_vecGoalAngles.y )
	{
		float flDir = m_vecGoalAngles.y > m_vecCurAngles.y ? 1 : -1 ;
		float flDist = fabs(m_vecGoalAngles.y - m_vecCurAngles.y);
		bool bReversed = false;
		
		if (flDist > 180)
		{
			flDist = 360 - flDist;
			flDir = -flDir;
			bReversed = true;
		}

		if (m_hEnemy == NULL )
		{
			if (flDist > 30)
			{
				if (m_fTurnRate < turnrate * 20)
				{
					m_fTurnRate += turnrate;
				}
			}
			else
			{
				// Slow down
				if ( m_fTurnRate > (turnrate * 5) )
					m_fTurnRate -= turnrate;
			}
		}
		else
		{
			// When tracking enemies, move faster and don't slow
			if (flDist > 30)
			{
				if (m_fTurnRate < turnrate * 30)
				{
					m_fTurnRate += turnrate * 3;
				}
			}
		}

		m_vecCurAngles.y += 0.1 * m_fTurnRate * flDir;

		// if we passed over the goal, peg right to it now
		if (flDir == -1)
		{
			if ( (bReversed == false && m_vecGoalAngles.y > m_vecCurAngles.y) || (bReversed == true && m_vecGoalAngles.y < m_vecCurAngles.y) )
				m_vecCurAngles.y = m_vecGoalAngles.y;
		} 
		else
		{
			if ( (bReversed == false && m_vecGoalAngles.y < m_vecCurAngles.y) || (bReversed == true && m_vecGoalAngles.y > m_vecCurAngles.y) )
				m_vecCurAngles.y = m_vecGoalAngles.y;
		}

		if (m_vecCurAngles.y < 0)
			m_vecCurAngles.y += 360;
		else if (m_vecCurAngles.y >= 360)
			m_vecCurAngles.y -= 360;

		if (flDist < (0.05 * turnrate))
			m_vecCurAngles.y = m_vecGoalAngles.y;

		m_fBoneXRotator = m_vecCurAngles.y - UTIL_AngleMod( GetAbsAngles().y );

		bMoved = 1;
	}

	if ( !bMoved || !m_fTurnRate )
		m_fTurnRate = turnrate;

	if ( bMoved )
	{
		NetworkStateChanged();
	}

	return bMoved;
}

void C_ObjectSentrygun::ClientThink( void )
{
	// Turtling sentryguns don't think
	if ( IsTurtled() )
		return;

	if ( IsPlacing() || IsBuilding() )
		return;


	if ( m_hEnemy != NULL )
	{
		// Figure out where we're firing at
		Vector vecMid = EyePosition();
		Vector vecFireTarget = m_hEnemy->WorldSpaceCenter(); //  + vecMid; // BodyTarget( vecMid );
		Vector vecDirToEnemy = vecFireTarget - vecMid;
		QAngle angToTarget;
		VectorAngles(vecDirToEnemy, angToTarget);
		
		angToTarget.y = UTIL_AngleMod( angToTarget.y );
		if (angToTarget.x < -180)
			angToTarget.x += 360;
		if (angToTarget.x > 180)
			angToTarget.x -= 360;

		// now all numbers should be in [1...360]
		// pin to turret limitations to [-50...50]
		if (angToTarget.x > 50)
			angToTarget.x = 50;
		else if (angToTarget.x < -50)
			angToTarget.x = -50;

		m_vecGoalAngles.y = angToTarget.y;
		m_vecGoalAngles.x = angToTarget.x;

		MoveTurret();
		return;
	}

	// Rotate
	if ( !MoveTurret() )
	{
		// Play a sound occasionally
		if ( random->RandomFloat(0, 1) < 0.02 )
		{
			EmitSound( "ObjectSentrygun.Idle" );
		}

		// Switch rotation direction
		if (m_bTurningRight)
		{
			m_bTurningRight = false;
			m_vecGoalAngles.y = m_iLeftBound;
		}
		else
		{
			m_bTurningRight = true;
			m_vecGoalAngles.y = m_iRightBound;
		}

		// Randomly look up and down a bit
		if ( random->RandomFloat(0, 1) < 0.3 )
		{
			m_vecGoalAngles.x = (int)random->RandomFloat(-10,10);
		}
	}
}


//-----------------------------------------------------------------------------
// Control screen 
//-----------------------------------------------------------------------------
class CSentrygunControlPanel : public CRotatingObjectControlPanel
{
	DECLARE_CLASS( CSentrygunControlPanel, CRotatingObjectControlPanel );

public:
	CSentrygunControlPanel( vgui::Panel *parent, const char *panelName );
	virtual bool Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData );
	virtual void OnTick();
	virtual void OnCommand( const char *command );

	void		 AddAmmo( void );

private:
	vgui::Label		*m_pAmmoLabel;
};


DECLARE_VGUI_SCREEN_FACTORY( CSentrygunControlPanel, "sentrygun_control_panel" );


//-----------------------------------------------------------------------------
// Constructor: 
//-----------------------------------------------------------------------------
CSentrygunControlPanel::CSentrygunControlPanel( vgui::Panel *parent, const char *panelName )
	: BaseClass( parent, "CSentrygunControlPanel" ) 
{
}

//-----------------------------------------------------------------------------
// Initialization 
//-----------------------------------------------------------------------------
bool CSentrygunControlPanel::Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData )
{
	m_pAmmoLabel = new vgui::Label( this, "AmmoReadout", "" );

	if (!BaseClass::Init(pKeyValues, pInitData))
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Frame-based update
//-----------------------------------------------------------------------------
void CSentrygunControlPanel::OnTick()
{
	BaseClass::OnTick();
	
	C_BaseObject *pObj = GetOwningObject();
	if (!pObj)
		return;

	Assert( dynamic_cast<C_ObjectSentrygun*>(pObj) );
	C_ObjectSentrygun *pSentrygun = static_cast<C_ObjectSentrygun*>(pObj);

	char buf[256];
	int iAmmo = pSentrygun->GetAmmoLeft();
	if (iAmmo > 0)
	{
		Q_snprintf( buf, sizeof( buf ), "%d rounds left", iAmmo );
	}
	else
	{
		Q_snprintf( buf, sizeof( buf ), "OUT OF AMMO" );
	}
	m_pAmmoLabel->SetText( buf );
}

//-----------------------------------------------------------------------------
// Purpose: Handle ammo input to the sentrygun
//-----------------------------------------------------------------------------
void CSentrygunControlPanel::AddAmmo( void )
{
	C_BaseObject *pObj = GetOwningObject();
	if (pObj)
	{
		pObj->SendClientCommand( "addammo" );
	}
}

//-----------------------------------------------------------------------------
// Button click handlers
//-----------------------------------------------------------------------------
void CSentrygunControlPanel::OnCommand( const char *command )
{
	if (!Q_strnicmp(command, "AddAmmo", 7))
	{
		AddAmmo();
		return;
	}

	BaseClass::OnCommand(command);
}



//======================================================================================================
// SENTRYGUN TYPES
//======================================================================================================
// Purpose: Plasma sentrygun
//-----------------------------------------------------------------------------
IMPLEMENT_CLIENTCLASS_DT(C_ObjectSentrygunPlasma, DT_ObjectSentrygunPlasma, CObjectSentrygunPlasma)
END_RECV_TABLE()

C_ObjectSentrygunPlasma::C_ObjectSentrygunPlasma()
{
}

//-----------------------------------------------------------------------------
// Purpose: Rocket launcher sentrygun
//-----------------------------------------------------------------------------
IMPLEMENT_CLIENTCLASS_DT(C_ObjectSentrygunRocketlauncher, DT_ObjectSentrygunRocketlauncher, CObjectSentrygunRocketlauncher)
END_RECV_TABLE()

C_ObjectSentrygunRocketlauncher::C_ObjectSentrygunRocketlauncher()
{
}