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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "basetfplayer_shared.h"
#include "basetfcombatweapon_shared.h"
#include "weapon_twohandedcontainer.h"
#if !defined( CLIENT_DLL )
#include "soundent.h"
#else
#include "functionproxy.h"
#include "ivrenderview.h"
#include "cl_animevent.h"
#include "fx.h"
#endif
CBaseTFCombatWeapon::CBaseTFCombatWeapon ( void )
{
m_bReflectViewModelAnimations = false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::Precache( void )
{
BaseClass::Precache();
#if !defined( CLIENT_DLL )
// Connect to my CVars
m_pDamageCVar = cvar->FindVar( UTIL_VarArgs( "%s_damage", GetClassname() ) );
m_pRangeCVar = cvar->FindVar( UTIL_VarArgs( "%s_range", GetClassname() ) );
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseTFCombatWeapon::GetPrimaryAmmo( void )
{
// Get the local player
CBasePlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( pPlayer == NULL )
return 0;
return pPlayer->GetAmmoCount( m_iPrimaryAmmoType );
}
//-----------------------------------------------------------------------------
// Purpose: Checks if the owner is EMPed
//-----------------------------------------------------------------------------
bool CBaseTFCombatWeapon::IsOwnerEMPed()
{
CBaseTFPlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ((!pPlayer) || (!pPlayer->GetPlayerClass()))
return false;
return ( pPlayer->HasPowerup(POWERUP_EMP) );
}
//-----------------------------------------------------------------------------
// Purpose: Temporarily remove disguise
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::CheckRemoveDisguise( void )
{
#if !defined( CLIENT_DLL )
CBaseTFPlayer *player = static_cast< CBaseTFPlayer * >( GetOwner());
if ( player )
{
// Always remove camo
player->ClearCamouflage();
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Factor in the player's anim speed to the sequence duration for weapons
//-----------------------------------------------------------------------------
float CBaseTFCombatWeapon::SequenceDuration( int iSequence )
{
float flDuration = BaseClass::SequenceDuration( iSequence );
CBaseTFPlayer *pOwner = (CBaseTFPlayer *)GetOwner();
if ( pOwner )
{
flDuration /= pOwner->GetDefaultAnimSpeed();
}
return flDuration;
}
//-----------------------------------------------------------------------------
// Purpose: Override weapon sound. TF doesn't use ATTN_GUNFIRE for it's weapon attenuations.
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::WeaponSound( WeaponSound_t shoot_type, float soundtime /*= 0.0f*/ )
{
#if !defined( CLIENT_DLL )
// HACKHACK: Force a combat sound to alert NPCs, for antlion prototype
CSoundEnt::InsertSound( SOUND_COMBAT, GetAbsOrigin(), 600, 0.1 );
#endif
BaseClass::WeaponSound( shoot_type, soundtime );
}
//-----------------------------------------------------------------------------
// Purpose: Get the point from which to create the weapon's tracer
//-----------------------------------------------------------------------------
Vector CBaseTFCombatWeapon::GetTracerSrc( Vector &vecSrc, Vector &vecFireDir )
{
QAngle vecAngles;
VectorAngles( vecFireDir, vecAngles );
Vector right;
AngleVectors( vecAngles, NULL, &right, NULL );
return (vecSrc + Vector ( 0,0,-4 ) + right * 2 + vecFireDir * 16);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : activity -
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::PlayAttackAnimation( int activity )
{
SendWeaponAnim( activity );
CBaseTFPlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( pPlayer )
{
pPlayer->SetAnimation( PLAYER_ATTACK1 );
pPlayer->SetLastAttackTime( gpGlobals->curtime );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : sequence -
//-----------------------------------------------------------------------------
bool CBaseTFCombatWeapon::SendWeaponAnim( int iActivity )
{
CBaseTFPlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( !pPlayer )
return false;
CBaseTFCombatWeapon *pOther = NULL;
// See if we are wielding multiple weapons
CWeaponTwoHandedContainer *pContainer = dynamic_cast< CWeaponTwoHandedContainer * >( pPlayer->GetActiveWeapon() );
if ( pContainer )
{
// Make sure they exist
CBaseTFCombatWeapon *left = static_cast< CBaseTFCombatWeapon * >( pContainer->GetLeftWeapon() );
CBaseTFCombatWeapon *right = static_cast< CBaseTFCombatWeapon * >( pContainer->GetRightWeapon() );
if ( !left || !right )
return false;
// Make sure one of them is this!!!
if ( left != this && right != this )
return false;
// Get pointer to other one
pOther = left;
if ( left == this )
{
pOther = right;
}
Assert(pOther);
// Now ask our other weapon if it would like to stomp my animation attempt
iActivity = pOther->ReplaceOtherWeaponsActivity( iActivity );
if ( iActivity == -1 )
return false;
}
// Always pass through to base
BaseClass::SendWeaponAnim( iActivity );
if ( !IsReflectingAnimations() )
return false;
// See if we are wielding multiple weapons
if ( !pContainer )
return false;
Assert(pOther);
// Send our other weapon the activity code
iActivity = GetOtherWeaponsActivity( iActivity );
if ( iActivity != -1 )
{
pOther->SendWeaponAnim( iActivity );
// Remember it for weapon switching
m_iLastReflectedActivity = iActivity;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : reflect -
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::SetReflectViewModelAnimations( bool reflect )
{
m_bReflectViewModelAnimations = reflect;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CBaseTFCombatWeapon::IsReflectingAnimations( void ) const
{
return m_bReflectViewModelAnimations;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CBaseTFCombatWeapon::IsCamouflaged( void )
{
CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)GetOwner();
if ( pPlayer && pPlayer->IsCamouflaged() )
return true;
return false;
}
#if defined( CLIENT_DLL )
static ConVar cl_bobcycle( "cl_bobcycle","0.8" );
static ConVar cl_bob( "cl_bob","0.002" );
static ConVar cl_bobup( "cl_bobup","0.5" );
// Register these cvars if needed for easy tweaking
static ConVar v_iyaw_cycle( "v_iyaw_cycle", "2"/*, FCVAR_UNREGISTERED*/ );
static ConVar v_iroll_cycle( "v_iroll_cycle", "0.5"/*, FCVAR_UNREGISTERED*/ );
static ConVar v_ipitch_cycle( "v_ipitch_cycle", "1"/*, FCVAR_UNREGISTERED*/ );
static ConVar v_iyaw_level( "v_iyaw_level", "0.3"/*, FCVAR_UNREGISTERED*/ );
static ConVar v_iroll_level( "v_iroll_level", "0.1"/*, FCVAR_UNREGISTERED*/ );
static ConVar v_ipitch_level( "v_ipitch_level", "0.3"/*, FCVAR_UNREGISTERED*/ );
//-----------------------------------------------------------------------------
// Purpose:
// Output : float
//-----------------------------------------------------------------------------
float CBaseTFCombatWeapon::CalcViewmodelBob( void )
{
static double bobtime;
static float bob;
float cycle;
CBasePlayer *player = ToBasePlayer( GetOwner() );
if ( !player )
return 0.0f;
if ( ( player->GetGroundEntity() == NULL ) || !gpGlobals->frametime )
{
return bob; // just use old value
}
bobtime += gpGlobals->frametime;
cycle = bobtime - (int)(bobtime/cl_bobcycle.GetFloat())*cl_bobcycle.GetFloat();
cycle /= cl_bobcycle.GetFloat();
if (cycle < cl_bobup.GetFloat())
{
cycle = M_PI * cycle / cl_bobup.GetFloat();
}
else
{
cycle = M_PI + M_PI*(cycle-cl_bobup.GetFloat())/(1.0 - cl_bobup.GetFloat());
}
// bob is proportional to simulated velocity in the xy plane
// (don't count Z, or jumping messes it up)
bob = player->GetAbsVelocity().Length2D() * cl_bob.GetFloat();
bob = bob*0.3 + bob*0.7*sin(cycle);
bob = MIN( 4.0, bob );
bob = MAX( -7.0, bob );
return bob;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : float
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::AddViewmodelBob( CBaseViewModel *viewmodel, Vector &origin, QAngle &angles )
{
float fIdleScale = 2.0f;
// Bias so view models aren't all synced to each other
//float curtime = gpGlobals->curtime + ( viewmodelindex * 2 * M_PI / GetViewModelCount() );
float curtime = gpGlobals->curtime + ( viewmodel->entindex() * 2 * M_PI );
origin[ROLL] -= fIdleScale * sin(curtime*v_iroll_cycle.GetFloat()) * v_iroll_level.GetFloat();
origin[PITCH] -= fIdleScale * sin(curtime*v_ipitch_cycle.GetFloat()) * (v_ipitch_level.GetFloat() * 0.5);
origin[YAW] -= fIdleScale * sin(curtime*v_iyaw_cycle.GetFloat()) * v_iyaw_level.GetFloat();
Vector forward;
AngleVectors( angles, &forward, NULL, NULL );
float flBob = CalcViewmodelBob();
// Apply bob, but scaled down to 40%
VectorMA( origin, flBob * 0.4, forward, origin );
// Z bob a bit more
origin[2] += flBob;
// throw in a little tilt.
angles[ YAW ] -= flBob * 0.6;
angles[ ROLL ] -= flBob * 0.5;
angles[ PITCH ] -= flBob * 0.4;
}
//-----------------------------------------------------------------------------
// Purpose: TF specific weapon anim events
//-----------------------------------------------------------------------------
bool CBaseTFCombatWeapon::OnFireEvent( C_BaseViewModel *pViewModel, const Vector& origin, const QAngle& angles, int event, const char *options )
{
switch( event )
{
case CL_EVENT_MUZZLEFLASH0:
case CL_EVENT_MUZZLEFLASH1:
case CL_EVENT_MUZZLEFLASH2:
case CL_EVENT_MUZZLEFLASH3:
{
int iAttachment = -1;
Vector attachOrigin;
QAngle attachAngles;
// First person muzzle flashes
switch (event)
{
case CL_EVENT_MUZZLEFLASH0:
iAttachment = 0;
break;
case CL_EVENT_MUZZLEFLASH1:
iAttachment = 1;
break;
case CL_EVENT_MUZZLEFLASH2:
iAttachment = 2;
break;
case CL_EVENT_MUZZLEFLASH3:
iAttachment = 3;
break;
}
// Did we find it?
if ( pViewModel->GetAttachment( iAttachment+1, attachOrigin, attachAngles ) )
{
//int iType = atoi( options );
// Is our owner boosted?
CBasePlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( pPlayer && pPlayer->HasPowerup(POWERUP_BOOST) )
{
unsigned char color[3];
color[0] = 50;
color[1] = 255;
color[2] = 50;
FX_MuzzleEffect( attachOrigin, attachAngles, 1.0, GetRefEHandle(), &color[0] );
}
else
{
FX_MuzzleEffect( attachOrigin, attachAngles, 1.0, GetRefEHandle() );
}
return true;
}
}
break;
default:
break;
}
return false;
}
//============================================================================================================
// OWNER BOOSTED PROXY FOR WEAPONS / PLAYERS
//============================================================================================================
class CPlayerBoostedProxy : public CResultProxy
{
public:
bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
void OnBind( void *pC_BaseEntity );
private:
CFloatInput m_Factor;
};
bool CPlayerBoostedProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
if (!CResultProxy::Init( pMaterial, pKeyValues ))
return false;
if (!m_Factor.Init( pMaterial, pKeyValues, "scale", 1 ))
return false;
return true;
}
void CPlayerBoostedProxy::OnBind( void *pRenderable )
{
// Find the view angle between the player and this entity....
IClientRenderable *pRend = (IClientRenderable *)pRenderable;
C_BaseEntity *pEntity = pRend->GetIClientUnknown()->GetBaseEntity();
CBaseTFPlayer *pPlayer = NULL;
C_BaseViewModel *pViewModel = dynamic_cast<C_BaseViewModel*>(pEntity);
if ( pViewModel )
{
pPlayer = C_BaseTFPlayer::GetLocalPlayer();
}
else
{
CBaseTFCombatWeapon *pWeapon = dynamic_cast<CBaseTFCombatWeapon*>(pEntity);
if ( pWeapon )
{
pPlayer = ToBaseTFPlayer( pWeapon->GetOwner() );
}
else
{
pPlayer = dynamic_cast<CBaseTFPlayer*>(pEntity);
}
}
// Find him?
if ( pPlayer )
{
float flBoosted = (int)pPlayer->HasPowerup( POWERUP_BOOST );
SetFloatResult( flBoosted * m_Factor.GetFloat() );
}
}
EXPOSE_INTERFACE( CPlayerBoostedProxy, IMaterialProxy, "PlayerBoosted" IMATERIAL_PROXY_INTERFACE_VERSION );
#else
//-----------------------------------------------------------------------------
// Purpose:
// Output : float
//-----------------------------------------------------------------------------
float CBaseTFCombatWeapon::CalcViewmodelBob( void )
{
return 0.0f;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : float
//-----------------------------------------------------------------------------
void CBaseTFCombatWeapon::AddViewmodelBob( CBaseViewModel *viewmodel, Vector &origin, QAngle &angles )
{
}
#endif
LINK_ENTITY_TO_CLASS( basetfcombatweapon, CBaseTFCombatWeapon );
IMPLEMENT_NETWORKCLASS_ALIASED( BaseTFCombatWeapon , DT_BaseTFCombatWeapon )
BEGIN_NETWORK_TABLE( CBaseTFCombatWeapon , DT_BaseTFCombatWeapon )
#if !defined( CLIENT_DLL )
// Don't network any animation stuff to client
SendPropExclude( "DT_AnimTimeMustBeFirst", "m_flAnimTime" ),
SendPropExclude( "DT_BaseAnimating", "m_flCycle" ),
SendPropInt( SENDINFO( m_bReflectViewModelAnimations ), 1, SPROP_UNSIGNED ),
#else
RecvPropInt( RECVINFO( m_bReflectViewModelAnimations ) ),
#endif
END_NETWORK_TABLE()
BEGIN_PREDICTION_DATA( CBaseTFCombatWeapon )
// If true, reflect and weapon animations to all view models
DEFINE_PRED_FIELD( m_bReflectViewModelAnimations, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE ),
DEFINE_FIELD( m_iLastReflectedActivity, FIELD_INTEGER )
END_PREDICTION_DATA()
|