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

#include "cbase.h"
#include "weapon_dodbasebomb.h"


#ifdef CLIENT_DLL
	#include "c_dod_player.h"
#else
	#include "dod_player.h"
	#include "dod_bombtarget.h"
	#include "collisionutils.h"
	#include "in_buttons.h"
#endif


IMPLEMENT_NETWORKCLASS_ALIASED( DODBaseBombWeapon, DT_BaseBombWeapon )


BEGIN_NETWORK_TABLE( CDODBaseBombWeapon, DT_BaseBombWeapon )

#ifdef CLIENT_DLL
	//RecvPropBool( RECVINFO(m_bDeployed) )
#else
	//SendPropBool( SENDINFO(m_bDeployed) )
#endif

END_NETWORK_TABLE()


BEGIN_PREDICTION_DATA( CDODBaseBombWeapon )
END_PREDICTION_DATA()

#ifndef CLIENT_DLL

BEGIN_DATADESC( CDODBaseBombWeapon )
END_DATADESC()

#endif

LINK_ENTITY_TO_CLASS( weapon_basebomb, CDODBaseBombWeapon );
PRECACHE_WEAPON_REGISTER( weapon_basebomb );

acttable_t CDODBaseBombWeapon::m_acttable[] = 
{
	{ ACT_PRONE_IDLE,						ACT_DOD_PRONEWALK_IDLE_PISTOL,			false },	//?
	{ ACT_PRONE_FORWARD,					ACT_DOD_PRONEWALK_IDLE_PISTOL,			false },	//?
	{ ACT_DOD_STAND_IDLE,					ACT_DOD_STAND_IDLE_TNT,					false },
	{ ACT_DOD_CROUCH_IDLE,					ACT_DOD_CROUCH_IDLE_TNT,				false },	//?
	{ ACT_DOD_CROUCHWALK_IDLE,				ACT_DOD_CROUCHWALK_IDLE_TNT,			false },
	{ ACT_DOD_WALK_IDLE,					ACT_DOD_WALK_IDLE_TNT,					false },
	{ ACT_DOD_RUN_IDLE,						ACT_DOD_RUN_IDLE_TNT,					false },
	{ ACT_SPRINT,							ACT_DOD_SPRINT_IDLE_TNT,				false },

	// Hand Signals
	{ ACT_DOD_HS_IDLE,						ACT_DOD_HS_IDLE_PISTOL,					false },
	{ ACT_DOD_HS_CROUCH,					ACT_DOD_HS_CROUCH_PISTOL,				false },
};

IMPLEMENT_ACTTABLE( CDODBaseBombWeapon );

CDODBaseBombWeapon::CDODBaseBombWeapon()
{
}

void CDODBaseBombWeapon::Spawn( )
{
	WEAPON_FILE_INFO_HANDLE	hWpnInfo = LookupWeaponInfoSlot( GetClassname() );

	Assert( hWpnInfo != GetInvalidWeaponInfoHandle() );

	CDODWeaponInfo *pWeaponInfo = dynamic_cast< CDODWeaponInfo* >( GetFileWeaponInfoFromHandle( hWpnInfo ) );

	Assert( pWeaponInfo && "Failed to get CDODWeaponInfo in weapon spawn" );

	m_pWeaponInfo = pWeaponInfo;

	SetPlanting( false );

	BaseClass::Spawn();
}

void CDODBaseBombWeapon::Precache()
{
	BaseClass::Precache();
}

bool CDODBaseBombWeapon::Deploy( )
{
#ifdef CLIENT_DLL
	CDODPlayer *pPlayer = GetDODPlayerOwner();

	if ( pPlayer )
	{
		pPlayer->HintMessage( HINT_BOMB_FIRST_SELECT );
	}
#endif

	return DefaultDeploy( (char*)GetViewModel(), (char*)GetWorldModel(), GetDrawActivity(), (char*)GetAnimPrefix() );
}

void CDODBaseBombWeapon::PrimaryAttack()
{
#ifndef CLIENT_DLL
	if ( IsPlanting() )
	{
		CDODBombTarget *pTarget = (CDODBombTarget *)m_hBombTarget.Get();
		CBasePlayer *pPlayer = GetPlayerOwner();

		if ( !pTarget || !pPlayer )
		{
			CancelPlanting();
			return;
		}

		if ( pTarget->CanPlantHere( GetDODPlayerOwner() ) == false )
		{
			// if the target is not active anymore, cancel ( someone planted there already? )
			CancelPlanting();
		}
		else if ( ( pTarget->GetAbsOrigin() - pPlayer->WorldSpaceCenter() ).Length() > DOD_BOMB_PLANT_RADIUS )
		{
			// if we're too far away, cancel
			CancelPlanting();			
		}
		else if ( IsLookingAtBombTarget( pPlayer, pTarget ) == false || ( pPlayer->GetFlags() & FL_ONGROUND ) == 0 )
		{
			// not looking at the target anymore
			CancelPlanting();
		}
		else if ( gpGlobals->curtime > m_flPlantCompleteTime )
		{
			// we finished the plant
			CompletePlant();
		}
		else
		{
			m_flNextPlantCheck = gpGlobals->curtime + 0.2;
		}

		return;
	}

	// find nearby, visible bomb targets
	CBaseEntity *pEnt = NULL;
	CDODBombTarget *pBestTarget = NULL;

	float flBestDist = FLT_MAX;

	CDODPlayer *pPlayer = GetDODPlayerOwner();

	if ( !pPlayer )
		return;
	
	while( ( pEnt = gEntList.FindEntityByClassname( pEnt, "dod_bomb_target" ) ) != NULL )
	{
		CDODBombTarget *pTarget = static_cast<CDODBombTarget *>( pEnt );

		if ( !pTarget->CanPlantHere( pPlayer ) )
			continue;

		Vector pos = pPlayer->WorldSpaceCenter();

		float flDist = ( pos - pTarget->GetAbsOrigin() ).Length();

		// if we are looking directly at a bomb target and it is within our radius, that automatically wins
		if ( flDist < flBestDist &&
			flDist < DOD_BOMB_PLANT_RADIUS &&
			IsLookingAtBombTarget( pPlayer, pTarget ) &&
			( pPlayer->GetFlags() & FL_ONGROUND ) )
		{
			flBestDist = flDist;
			pBestTarget = pTarget;
		}
	}

	if ( pBestTarget )
	{
		StartPlanting( pBestTarget );
	}

	m_flNextPlantCheck = gpGlobals->curtime + 0.2;

	// true if the player is not holding primary attack
	m_bUsePlant = !( pPlayer->m_nButtons & (IN_ATTACK|IN_ATTACK2) );
#endif
}

void CDODBaseBombWeapon::SecondaryAttack()
{
	PrimaryAttack();
}

#ifndef CLIENT_DLL
void CDODBaseBombWeapon::StartPlanting( CDODBombTarget *pTarget )
{
	// we have already checked that we can plant here

	// store a pointer to the target we're bombing
	m_hBombTarget = pTarget;

	// must do this after setting the bomb target as we tell the planter
	// what target they are at
	SetPlanting( true );

	// set the timer for when we'll be done
	m_flPlantCompleteTime = gpGlobals->curtime + DOD_BOMB_PLANT_TIME;

	// play the planting animation
	SendWeaponAnim( ACT_VM_PRIMARYATTACK );

	CDODPlayer *pPlayer = GetDODPlayerOwner();

	if ( pPlayer )
	{
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_PLANT_TNT );

		pPlayer->SetMaxSpeed( 1 );

		pPlayer->SetProgressBarTime( DOD_BOMB_PLANT_TIME );
	}
}

bool CDODBaseBombWeapon::CancelPlanting( void )
{
	bool bHolster = false;

	SetPlanting( false );

	// play a stop animation
	SendWeaponAnim( ACT_VM_IDLE );

	CDODPlayer *pPlayer = GetDODPlayerOwner();

	if ( pPlayer )
	{
		pPlayer->DoAnimationEvent( PLAYERANIMEVENT_CANCEL_GESTURES );

		// restore player speed
		pPlayer->SetMaxSpeed( 600 );

		pPlayer->ResetProgressBar();

		if ( m_bUsePlant )
		{
			pPlayer->SelectLastItem();

			bHolster = true;
		}
	}

	return bHolster;
}

void CDODBaseBombWeapon::CompletePlant( void )
{
	CDODPlayer *pPlayer = ToDODPlayer( GetPlayerOwner() );

	if ( pPlayer )
	{
		SetPlanting( false );

		// restore player speed
		GetDODPlayerOwner()->SetMaxSpeed( 600 );

		// Tell the target that we finished planting the bomb
		((CDODBombTarget *)m_hBombTarget.Get())->CompletePlanting( pPlayer );

		// destroy the bomb weapon
		pPlayer->Weapon_Drop( this, NULL, NULL );
		UTIL_Remove(this);

		pPlayer->ResetProgressBar();

		pPlayer->SelectLastItem();
	}
}

bool CDODBaseBombWeapon::IsLookingAtBombTarget( CBasePlayer *pPlayer, CDODBombTarget *pTarget )
{
	Vector forward;
	AngleVectors( pPlayer->EyeAngles(), &forward );

	Vector toBomb = pTarget->GetAbsOrigin() - pPlayer->EyePosition();
	toBomb.NormalizeInPlace();

	return ( DotProduct( forward, toBomb ) >= 0.8 );
}

#endif

void CDODBaseBombWeapon::ItemPostFrame()
{
#ifndef CLIENT_DLL
	CBasePlayer *pPlayer = GetPlayerOwner();

	if ( !pPlayer )
		return;

	if ( pPlayer->m_nButtons & (IN_ATTACK|IN_ATTACK2) )
	{
		PrimaryAttack();
	}
	// Only use the time check if we are planting with the +use key
	// adds a slight lag to breaking the player lock otherwise
	else if ( !m_bUsePlant || m_flNextPlantCheck < gpGlobals->curtime )
	{
		if ( IsPlanting() )
		{
			// reset all planting
			bool bHolster = CancelPlanting();

			// sometimes after canceling we put the weapon away and switch
			// to our last weapon. In that case, we don't want to send any more
			// anim calls b/c it confuses the client.
			if ( bHolster )
			{
				// we've put this weapon away, stop everything
				return;
			}

			// anim now
			m_flTimeWeaponIdle = 0;
		}
		
		// idle
		if (m_flTimeWeaponIdle > gpGlobals->curtime)
			return;

		SendWeaponAnim( GetIdleActivity() );

		m_flTimeWeaponIdle = gpGlobals->curtime + SequenceDuration();

		// if we're not planting, why do we have the bomb out?
		// switch to our next best weapon
		pPlayer->SelectLastItem();
	}
	
#endif	// CLIENT_DLL
}

bool CDODBaseBombWeapon::Holster( CBaseCombatWeapon *pSwitchingTo )
{
#ifndef CLIENT_DLL
	if ( IsPlanting() )
		CancelPlanting();
#endif

	return BaseClass::Holster( pSwitchingTo );
}

void CDODBaseBombWeapon::SetPlanting( bool bPlanting )
{
	m_bPlanting = bPlanting;

#ifndef CLIENT_DLL
	CDODPlayer *pPlayer = GetDODPlayerOwner();

	if ( pPlayer )
	{
		pPlayer->SetPlanting( m_bPlanting ? (CDODBombTarget *)m_hBombTarget.Get() : NULL );
	}
#endif
}

bool CDODBaseBombWeapon::IsPlanting( void )
{
	return m_bPlanting;
}