summaryrefslogtreecommitdiff
path: root/game/shared/dod/weapon_dodsniper.cpp
blob: 70d35687f399044ece74a0ec8e9c7aca50e86bfc (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h" 
#include "fx_dod_shared.h"
#include "weapon_dodsniper.h"

#ifdef CLIENT_DLL
	#include "prediction.h"
#endif

IMPLEMENT_NETWORKCLASS_ALIASED( DODSniperWeapon, DT_SniperWeapon )

#ifdef CLIENT_DLL

	void RecvProxy_AnimStart( const CRecvProxyData *pData, void *pStruct, void *pOut )
	{
		CDODSniperWeapon *pWpn = (CDODSniperWeapon *) pStruct;
		pWpn->m_bDoViewAnim =  ( pData->m_Value.m_Int > 0 );
	}

#endif

BEGIN_NETWORK_TABLE( CDODSniperWeapon, DT_SniperWeapon )
	#ifdef CLIENT_DLL
		RecvPropBool( RECVINFO( m_bZoomed ) ),
		RecvPropInt( RECVINFO( m_bDoViewAnim ), 0, RecvProxy_AnimStart )
	#else
		SendPropBool( SENDINFO( m_bZoomed ) ),
		SendPropBool( SENDINFO( m_bDoViewAnim ) )
	#endif
END_NETWORK_TABLE()


#ifdef CLIENT_DLL

	BEGIN_PREDICTION_DATA( CDODSniperWeapon )
		DEFINE_PRED_FIELD( m_bZoomed, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE )
	END_PREDICTION_DATA()

#else

	BEGIN_DATADESC( CDODSniperWeapon )
	END_DATADESC()

#endif


CDODSniperWeapon::CDODSniperWeapon()
{
}

void CDODSniperWeapon::Spawn( void )
{
	BaseClass::Spawn();

	ResetTimers();

#ifdef CLIENT_DLL
	m_bDoViewAnimCache = false;
	m_flZoomPercent = 0.0f;
#endif

	m_bShouldRezoomAfterShot = true;
}

void CDODSniperWeapon::ResetTimers( void )
{
	m_flUnzoomTime = -1;
	m_flRezoomTime = -1;
	m_flZoomOutTime = -1;
	m_flZoomInTime = -1;
	m_bRezoomAfterShot = false;
	m_bRezoomAfterReload = false;
}

bool CDODSniperWeapon::Holster( CBaseCombatWeapon *pSwitchingTo )
{
#ifndef CLIENT_DLL
	ZoomOut();	

	ResetTimers();
#endif

	return BaseClass::Holster( pSwitchingTo );
}

void CDODSniperWeapon::PrimaryAttack( void )
{
	BaseClass::PrimaryAttack();

	CDODPlayer *pPlayer = ToDODPlayer( GetOwner() );

	if ( IsZoomed() && ShouldZoomOutBetweenShots() )
	{
		//If we have more bullets, zoom out, play the bolt animation and zoom back in
		if( m_iClip1 > 0 && m_bShouldRezoomAfterShot && ( pPlayer && pPlayer->ShouldAutoRezoom() ) )
		{
			SetRezoom( true, 0.5f );	// zoom out in 0.5 seconds, then rezoom
		}
		else	//just zoom out
		{
			SetRezoom( false, 0.5f );	// just zoom out in 0.5 seconds
		}
	}

	// overwrite the next secondary attack, so we can zoom sooned after we've fired
	if ( m_bRezoomAfterShot && m_iClip1 > 0 )
		m_flNextSecondaryAttack = gpGlobals->curtime + 2.0;
	else
		m_flNextSecondaryAttack = SequenceDuration();
}

void CDODSniperWeapon::SecondaryAttack( void )
{
	CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );

	Assert( pPlayer );

	if ( !pPlayer )
		return;

	ToggleZoom();
}

bool CDODSniperWeapon::Reload( void )
{
	bool bSuccess = BaseClass::Reload();

	if ( bSuccess && IsZoomed() )
	{
		//if ( ShouldRezoomAfterReload() )
		//	m_bRezoomAfterReload = true;

		ZoomOut();
	}

	if ( !bSuccess )
		m_bRezoomAfterReload = false;

	return bSuccess;
}

void CDODSniperWeapon::FinishReload( void )
{
	BaseClass::FinishReload();

	if ( m_bRezoomAfterReload )
	{
		ZoomIn();
		m_bRezoomAfterReload = false;
	}
}

float CDODSniperWeapon::GetWeaponAccuracy( float flPlayerSpeed )
{
	//snipers and deployable weapons inherit this and override when we need a different accuracy

	float flSpread = m_pWeaponInfo->m_flAccuracy;

	if ( IsZoomed() && ( gpGlobals->curtime - m_flZoomChangeTime ) > DOD_SNIPER_ZOOM_CHANGE_TIME )
	{
		flSpread = m_pWeaponInfo->m_flSecondaryAccuracy;
	}

	if( flPlayerSpeed > 45 )
		flSpread += m_pWeaponInfo->m_flAccuracyMovePenalty;

	return flSpread;
}

bool CDODSniperWeapon::IsZoomed( void )
{
	// check the player?
	return IsSniperZoomed();
}

bool CDODSniperWeapon::ShouldDrawCrosshair( void )
{
	//if ( IsFullyZoomed() )
	//	return false;

	// don't draw if we are zoomed at all
	if ( m_bZoomed )
		return false;

	return BaseClass::ShouldDrawCrosshair();
}

#include "in_buttons.h"

void CDODSniperWeapon::ItemPostFrame( void )
{
	if ( m_flUnzoomTime > 0 && gpGlobals->curtime > m_flUnzoomTime )
	{
		if ( m_bRezoomAfterShot )
		{
			ZoomOutIn();
			m_bRezoomAfterShot = false;
		}
		else
		{
			ZoomOut();
		}

		m_flUnzoomTime = -1;
	}

	CDODPlayer *pPlayer = ToDODPlayer( GetPlayerOwner() );
	Assert( pPlayer );

	if ( m_flRezoomTime > 0 )
	{
		// if the player is sprinting and moving at all, cancel the rezoom
		if ( ( pPlayer->m_nButtons & IN_SPEED ) > 0 &&
			pPlayer->GetAbsVelocity().Length2D() > 20 )
		{
			m_flRezoomTime = -1;
		}
		else if ( gpGlobals->curtime > m_flRezoomTime )
		{
            ZoomIn();
			m_flRezoomTime = -1;
		}
	}

	if ( m_flZoomInTime > 0 && gpGlobals->curtime > m_flZoomInTime )
	{
#ifndef CLIENT_DLL
		CDODPlayer *pPlayer = ToDODPlayer( GetPlayerOwner() );
		Assert( pPlayer );
		pPlayer->SetFOV( pPlayer, GetZoomedFOV(), 0.1f );
#endif
		m_flZoomInTime = -1;
		m_flZoomOutTime = -1;
	}

	if ( m_flZoomInTime > 0 && (pPlayer->m_nButtons & IN_ATTACK) )
	{
		m_bInAttack = true;
	}

	BaseClass::ItemPostFrame();
}

void CDODSniperWeapon::Drop( const Vector &vecVelocity )
{
	// If a player is killed while deployed, this resets the weapon state
	if ( IsZoomed() )
		ZoomOut();

	ResetTimers();

	BaseClass::Drop( vecVelocity );
}

void CDODSniperWeapon::ToggleZoom( void )
{
	CDODPlayer *pPlayer = GetDODPlayerOwner();

	if ( !pPlayer->m_Shared.IsJumping() )
	{
		if( !IsZoomed() )
		{
			if ( CanAttack() )
			{
#ifndef CLIENT_DLL
				pPlayer->RemoveHintTimer( m_iAltFireHint );
#endif

				ZoomIn();
			}
		}
		else
		{
			ZoomOut();
		}
	}	
}

void CDODSniperWeapon::ZoomIn( void )
{
	m_flZoomInTime = gpGlobals->curtime + DOD_SNIPER_ZOOM_CHANGE_TIME;

#ifndef CLIENT_DLL
	SetZoomed( true );

	m_bDoViewAnim = !m_bDoViewAnim;
#endif

	m_flNextPrimaryAttack = MAX( gpGlobals->curtime + 0.5, m_flNextPrimaryAttack );
	m_flNextSecondaryAttack = MAX( gpGlobals->curtime + 0.5, m_flNextSecondaryAttack );
	m_flTimeWeaponIdle = gpGlobals->curtime + m_pWeaponInfo->m_flTimeToIdleAfterFire;

	m_flZoomChangeTime = gpGlobals->curtime;
}

void CDODSniperWeapon::ZoomOut( void )
{
	bool bWasZoomed = IsZoomed();

#ifndef CLIENT_DLL
	CDODPlayer *pPlayer = ToDODPlayer( GetPlayerOwner() );
	Assert( pPlayer );

	pPlayer->SetFOV( pPlayer, 90, 0.1f );
	SetZoomed( false );

	m_bDoViewAnim = !m_bDoViewAnim;
#endif	

	if ( bWasZoomed )
	{
		m_flNextPrimaryAttack = MAX( gpGlobals->curtime + 0.5, m_flNextPrimaryAttack );
		m_flNextSecondaryAttack = MAX( gpGlobals->curtime + 0.5, m_flNextSecondaryAttack );
		m_flTimeWeaponIdle = gpGlobals->curtime + m_pWeaponInfo->m_flTimeToIdleAfterFire;
	}

	m_flZoomChangeTime = gpGlobals->curtime;

	// if we are thinking about zooming, cancel it
	m_flZoomInTime = -1;
	m_flUnzoomTime = -1;
	m_flRezoomTime = -1;
}

// reduce rezoome time, to account for fade in we're replacing with anim
#define REZOOM_TIME	1.2f

void CDODSniperWeapon::ZoomOutIn( void )
{
	ZoomOut();

	m_flRezoomTime = gpGlobals->curtime + 1.0;
}

void CDODSniperWeapon::SetRezoom( bool bRezoom, float flDelay )
{
	m_flUnzoomTime = gpGlobals->curtime + flDelay;

	m_bRezoomAfterShot = bRezoom;
}

bool CDODSniperWeapon::IsFullyZoomed( void )
{
	return ( IsZoomed() == true &&
		(( gpGlobals->curtime - m_flZoomChangeTime ) > DOD_SNIPER_ZOOM_CHANGE_TIME) );
}

bool CDODSniperWeapon::IsZoomingIn( void )
{
	return ( m_flZoomInTime > gpGlobals->curtime );
}

#ifdef CLIENT_DLL

	float CDODSniperWeapon::GetZoomedPercentage( void )
	{
		return m_flZoomPercent;
	}

	Vector CDODSniperWeapon::GetDesiredViewModelOffset( C_DODPlayer *pOwner )
	{
		static Vector vecLastResult = vec3_origin;

		if ( prediction->InPrediction() && !prediction->IsFirstTimePredicted() )
		{
			return vecLastResult;
		}

		if ( m_bDoViewAnim != m_bDoViewAnimCache )
		{
			// start the anim timer 
			m_flViewAnimTimer = gpGlobals->curtime + DOD_SNIPER_ZOOM_CHANGE_TIME;
			m_bDoViewAnimCache = m_bDoViewAnim.m_Value;
		}

		Vector viewOffset = pOwner->GetViewOffset();

		float flPercent = clamp( ( viewOffset.z - VEC_PRONE_VIEW_SCALED( pOwner ).z ) / ( VEC_VIEW_SCALED( pOwner ).z - VEC_PRONE_VIEW_SCALED( pOwner ).z ), 0.0, 1.0 );

		Vector offset = ( flPercent * GetDODWpnData().m_vecViewNormalOffset +
			( 1.0 - flPercent ) * GetDODWpnData().m_vecViewProneOffset );

		
		// how long since we changed iron sight mode
		float flZoomAnimPercent = clamp( ( (m_flViewAnimTimer - gpGlobals->curtime) / ( DOD_SNIPER_ZOOM_CHANGE_TIME ) ), 0.0, 1.0 );

		m_flZoomPercent = ( IsZoomed() ? ( 1.0 - flZoomAnimPercent ) : flZoomAnimPercent );

		//Msg( "(%.1f) zoom %.2f %.2f\n", gpGlobals->curtime, m_flViewAnimTimer - gpGlobals->curtime, m_flZoomPercent );

		// use that percent to interp to iron sight position
		Vector vecResult = ( m_flZoomPercent * GetDODWpnData().m_vecIronSightOffset + 
			( 1.0 - m_flZoomPercent ) * offset );

		// store this value to use when called in prediction
		vecLastResult = vecResult;

		return vecResult;
	}

	float CDODSniperWeapon::GetViewModelSwayScale( void )
	{
		if ( IsFullyZoomed() )
			return 0;

		return BaseClass::GetViewModelSwayScale();
	}

#endif	//CLIENT_DLL