Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "weapon_knife.h"
  8. #include "cs_gamerules.h"
  9. #if defined( CLIENT_DLL )
  10. #include "c_cs_player.h"
  11. #else
  12. #include "cs_player.h"
  13. #include "ilagcompensationmanager.h"
  14. #include "cs_gamestats.h"
  15. #endif
  16. #define KNIFE_BODYHIT_VOLUME 128
  17. #define KNIFE_WALLHIT_VOLUME 512
  18. Vector head_hull_mins( -16, -16, -18 );
  19. Vector head_hull_maxs( 16, 16, 18 );
  20. #ifndef CLIENT_DLL
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Only send to local player if this weapon is the active weapon
  23. // Input : *pStruct -
  24. // *pVarData -
  25. // *pRecipients -
  26. // objectID -
  27. // Output : void*
  28. //-----------------------------------------------------------------------------
  29. void* SendProxy_SendActiveLocalKnifeDataTable( const SendProp *pProp, const void *pStruct, const void *pVarData, CSendProxyRecipients *pRecipients, int objectID )
  30. {
  31. // Get the weapon entity
  32. CBaseCombatWeapon *pWeapon = (CBaseCombatWeapon*)pVarData;
  33. if ( pWeapon )
  34. {
  35. // Only send this chunk of data to the player carrying this weapon
  36. CBasePlayer *pPlayer = ToBasePlayer( pWeapon->GetOwner() );
  37. if ( pPlayer /*&& pPlayer->GetActiveWeapon() == pWeapon*/ )
  38. {
  39. pRecipients->SetOnly( pPlayer->GetClientIndex() );
  40. return (void*)pVarData;
  41. }
  42. }
  43. return NULL;
  44. }
  45. REGISTER_SEND_PROXY_NON_MODIFIED_POINTER( SendProxy_SendActiveLocalKnifeDataTable );
  46. #endif
  47. // ----------------------------------------------------------------------------- //
  48. // CKnife tables.
  49. // ----------------------------------------------------------------------------- //
  50. IMPLEMENT_NETWORKCLASS_ALIASED( Knife, DT_WeaponKnife )
  51. BEGIN_NETWORK_TABLE_NOBASE( CKnife, DT_LocalActiveWeaponKnifeData )
  52. #if !defined( CLIENT_DLL )
  53. SendPropTime( SENDINFO( m_flSmackTime ) ),
  54. #else
  55. RecvPropTime( RECVINFO( m_flSmackTime ) ),
  56. #endif
  57. END_NETWORK_TABLE()
  58. BEGIN_NETWORK_TABLE( CKnife, DT_WeaponKnife )
  59. #if !defined( CLIENT_DLL )
  60. SendPropDataTable("LocalActiveWeaponKnifeData", 0, &REFERENCE_SEND_TABLE(DT_LocalActiveWeaponKnifeData), SendProxy_SendActiveLocalKnifeDataTable ),
  61. #else
  62. RecvPropDataTable("LocalActiveWeaponKnifeData", 0, 0, &REFERENCE_RECV_TABLE(DT_LocalActiveWeaponKnifeData)),
  63. #endif
  64. END_NETWORK_TABLE()
  65. #if defined CLIENT_DLL
  66. BEGIN_PREDICTION_DATA( CKnife )
  67. DEFINE_PRED_FIELD( m_flSmackTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  68. END_PREDICTION_DATA()
  69. #endif
  70. LINK_ENTITY_TO_CLASS( weapon_knife, CKnife );
  71. PRECACHE_WEAPON_REGISTER( weapon_knife );
  72. #ifndef CLIENT_DLL
  73. BEGIN_DATADESC( CKnife )
  74. DEFINE_THINKFUNC( Smack )
  75. END_DATADESC()
  76. #endif
  77. // ----------------------------------------------------------------------------- //
  78. // CKnife implementation.
  79. // ----------------------------------------------------------------------------- //
  80. CKnife::CKnife()
  81. {
  82. }
  83. bool CKnife::HasPrimaryAmmo()
  84. {
  85. return true;
  86. }
  87. bool CKnife::CanBeSelected()
  88. {
  89. return true;
  90. }
  91. void CKnife::Precache()
  92. {
  93. BaseClass::Precache();
  94. PrecacheScriptSound( "Weapon_Knife.Deploy" );
  95. PrecacheScriptSound( "Weapon_Knife.Slash" );
  96. PrecacheScriptSound( "Weapon_Knife.Stab" );
  97. PrecacheScriptSound( "Weapon_Knife.Hit" );
  98. }
  99. void CKnife::Spawn()
  100. {
  101. Precache();
  102. m_iClip1 = -1;
  103. BaseClass::Spawn();
  104. }
  105. bool CKnife::Deploy()
  106. {
  107. CPASAttenuationFilter filter( this );
  108. filter.UsePredictionRules();
  109. EmitSound( filter, entindex(), "Weapon_Knife.Deploy" );
  110. return BaseClass::Deploy();
  111. }
  112. void CKnife::Holster( int skiplocal )
  113. {
  114. if ( GetPlayerOwner() )
  115. {
  116. GetPlayerOwner()->m_flNextAttack = gpGlobals->curtime + 0.5;
  117. }
  118. }
  119. void CKnife::WeaponAnimation ( int iAnimation )
  120. {
  121. /*
  122. int flag;
  123. #if defined( CLIENT_WEAPONS )
  124. flag = FEV_NOTHOST;
  125. #else
  126. flag = 0;
  127. #endif
  128. PLAYBACK_EVENT_FULL( flag, pPlayer->edict(), m_usKnife,
  129. 0.0, (float *)&g_vecZero, (float *)&g_vecZero,
  130. 0.0,
  131. 0.0,
  132. iAnimation, 2, 3, 4 );
  133. */
  134. }
  135. void FindHullIntersection( const Vector &vecSrc, trace_t &tr, const Vector &mins, const Vector &maxs, CBaseEntity *pEntity )
  136. {
  137. int i, j, k;
  138. float distance;
  139. Vector minmaxs[2] = {mins, maxs};
  140. trace_t tmpTrace;
  141. Vector vecHullEnd = tr.endpos;
  142. Vector vecEnd;
  143. distance = 1e6f;
  144. vecHullEnd = vecSrc + ((vecHullEnd - vecSrc)*2);
  145. UTIL_TraceLine( vecSrc, vecHullEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
  146. if ( tmpTrace.fraction < 1.0 )
  147. {
  148. tr = tmpTrace;
  149. return;
  150. }
  151. for ( i = 0; i < 2; i++ )
  152. {
  153. for ( j = 0; j < 2; j++ )
  154. {
  155. for ( k = 0; k < 2; k++ )
  156. {
  157. vecEnd.x = vecHullEnd.x + minmaxs[i][0];
  158. vecEnd.y = vecHullEnd.y + minmaxs[j][1];
  159. vecEnd.z = vecHullEnd.z + minmaxs[k][2];
  160. UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
  161. if ( tmpTrace.fraction < 1.0 )
  162. {
  163. float thisDistance = (tmpTrace.endpos - vecSrc).Length();
  164. if ( thisDistance < distance )
  165. {
  166. tr = tmpTrace;
  167. distance = thisDistance;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. void CKnife::PrimaryAttack()
  175. {
  176. CCSPlayer *pPlayer = GetPlayerOwner();
  177. if ( pPlayer )
  178. {
  179. #if !defined (CLIENT_DLL)
  180. // Move other players back to history positions based on local player's lag
  181. lagcompensation->StartLagCompensation( pPlayer, pPlayer->GetCurrentCommand() );
  182. #endif
  183. SwingOrStab( false );
  184. #if !defined (CLIENT_DLL)
  185. lagcompensation->FinishLagCompensation( pPlayer );
  186. #endif
  187. }
  188. }
  189. void CKnife::SecondaryAttack()
  190. {
  191. CCSPlayer *pPlayer = GetPlayerOwner();
  192. if ( pPlayer && !pPlayer->m_bIsDefusing && !CSGameRules()->IsFreezePeriod() )
  193. {
  194. #if !defined (CLIENT_DLL)
  195. // Move other players back to history positions based on local player's lag
  196. lagcompensation->StartLagCompensation( pPlayer, pPlayer->GetCurrentCommand() );
  197. #endif
  198. SwingOrStab( true );
  199. #if !defined (CLIENT_DLL)
  200. lagcompensation->FinishLagCompensation( pPlayer );
  201. #endif
  202. }
  203. }
  204. #include "effect_dispatch_data.h"
  205. void CKnife::Smack( void )
  206. {
  207. if ( !GetPlayerOwner() )
  208. return;
  209. m_trHit.m_pEnt = m_pTraceHitEnt;
  210. if ( !m_trHit.m_pEnt || (m_trHit.surface.flags & SURF_SKY) )
  211. return;
  212. if ( m_trHit.fraction == 1.0 )
  213. return;
  214. if ( m_trHit.m_pEnt )
  215. {
  216. CPASAttenuationFilter filter( this );
  217. filter.UsePredictionRules();
  218. if( m_trHit.m_pEnt->IsPlayer() )
  219. {
  220. EmitSound( filter, entindex(), m_bStab?"Weapon_Knife.Stab":"Weapon_Knife.Hit" );
  221. }
  222. else
  223. {
  224. EmitSound( filter, entindex(), "Weapon_Knife.HitWall" );
  225. }
  226. }
  227. CEffectData data;
  228. data.m_vOrigin = m_trHit.endpos;
  229. data.m_vStart = m_trHit.startpos;
  230. data.m_nSurfaceProp = m_trHit.surface.surfaceProps;
  231. data.m_nDamageType = DMG_SLASH;
  232. data.m_nHitBox = m_trHit.hitbox;
  233. #ifdef CLIENT_DLL
  234. data.m_hEntity = m_trHit.m_pEnt->GetRefEHandle();
  235. #else
  236. data.m_nEntIndex = m_trHit.m_pEnt->entindex();
  237. #endif
  238. CPASFilter filter( data.m_vOrigin );
  239. #ifndef CLIENT_DLL
  240. filter.RemoveRecipient( GetPlayerOwner() );
  241. #endif
  242. data.m_vAngles = GetPlayerOwner()->GetAbsAngles();
  243. data.m_fFlags = 0x1; //IMPACT_NODECAL;
  244. te->DispatchEffect( filter, 0.0, data.m_vOrigin, "KnifeSlash", data );
  245. }
  246. void CKnife::WeaponIdle()
  247. {
  248. if (m_flTimeWeaponIdle > gpGlobals->curtime)
  249. return;
  250. CCSPlayer *pPlayer = GetPlayerOwner();
  251. if ( !pPlayer )
  252. return;
  253. if ( pPlayer->IsShieldDrawn() )
  254. return;
  255. SetWeaponIdleTime( gpGlobals->curtime + 20 );
  256. // only idle if the slid isn't back
  257. SendWeaponAnim( ACT_VM_IDLE );
  258. }
  259. //=============================================================================
  260. // HPE_BEGIN:
  261. // [tj] Hacky cheat code to control knife damage
  262. //=============================================================================
  263. #ifndef CLIENT_DLL
  264. ConVar KnifeDamageScale("knife_damage_scale", "100", FCVAR_DEVELOPMENTONLY);
  265. #endif
  266. //=============================================================================
  267. // HPE_END
  268. //=============================================================================
  269. bool CKnife::SwingOrStab( bool bStab )
  270. {
  271. CCSPlayer *pPlayer = GetPlayerOwner();
  272. if ( !pPlayer )
  273. return false;
  274. float fRange = bStab ? 32 : 48; // knife range
  275. Vector vForward; AngleVectors( pPlayer->EyeAngles(), &vForward );
  276. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  277. Vector vecEnd = vecSrc + vForward * fRange;
  278. trace_t tr;
  279. UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );
  280. //check for hitting glass - TODO - fix this hackiness, doesn't always line up with what FindHullIntersection returns
  281. #ifndef CLIENT_DLL
  282. CTakeDamageInfo glassDamage( pPlayer, pPlayer, 42.0f, DMG_BULLET | DMG_NEVERGIB );
  283. TraceAttackToTriggers( glassDamage, tr.startpos, tr.endpos, vForward );
  284. #endif
  285. if ( tr.fraction >= 1.0 )
  286. {
  287. UTIL_TraceHull( vecSrc, vecEnd, head_hull_mins, head_hull_maxs, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );
  288. if ( tr.fraction < 1.0 )
  289. {
  290. // Calculate the point of intersection of the line (or hull) and the object we hit
  291. // This is and approximation of the "best" intersection
  292. CBaseEntity *pHit = tr.m_pEnt;
  293. if ( !pHit || pHit->IsBSPModel() )
  294. FindHullIntersection( vecSrc, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, pPlayer );
  295. vecEnd = tr.endpos; // This is the point on the actual surface (the hull could have hit space)
  296. }
  297. }
  298. bool bDidHit = tr.fraction < 1.0f;
  299. #ifndef CLIENT_DLL
  300. bool bFirstSwing = (m_flNextPrimaryAttack + 0.4) < gpGlobals->curtime;
  301. #endif
  302. float fPrimDelay, fSecDelay;
  303. if ( bStab )
  304. {
  305. SendWeaponAnim( bDidHit ? ACT_VM_HITCENTER : ACT_VM_MISSCENTER );
  306. fPrimDelay = fSecDelay = bDidHit ? 1.1f : 1.0f;
  307. pPlayer->DoAnimationEvent( PLAYERANIMEVENT_FIRE_GUN_PRIMARY );
  308. }
  309. else // swing
  310. {
  311. SendWeaponAnim( bDidHit ? ACT_VM_HITCENTER : ACT_VM_MISSCENTER );
  312. fPrimDelay = bDidHit ? 0.5f : 0.4f;
  313. fSecDelay = bDidHit ? 0.5f : 0.5f;
  314. pPlayer->DoAnimationEvent( PLAYERANIMEVENT_FIRE_GUN_SECONDARY );
  315. }
  316. if ( pPlayer->HasShield() )
  317. {
  318. fPrimDelay += 0.7f; // 0.7 seconds slower if we carry a shield
  319. fSecDelay += 0.7f;
  320. }
  321. m_flNextPrimaryAttack = gpGlobals->curtime + fPrimDelay;
  322. m_flNextSecondaryAttack = gpGlobals->curtime + fSecDelay;
  323. SetWeaponIdleTime( gpGlobals->curtime + 2 );
  324. if ( !bDidHit )
  325. {
  326. // play wiff or swish sound
  327. CPASAttenuationFilter filter( this );
  328. filter.UsePredictionRules();
  329. EmitSound( filter, entindex(), "Weapon_Knife.Slash" );
  330. }
  331. #ifndef CLIENT_DLL
  332. float flDamage = 0.0f;
  333. if ( bDidHit )
  334. {
  335. // play thwack, smack, or dong sound
  336. CBaseEntity *pEntity = tr.m_pEnt;
  337. // player "shoot" animation
  338. pPlayer->SetAnimation( PLAYER_ATTACK1 );
  339. ClearMultiDamage();
  340. flDamage = 42.0f;
  341. if ( bStab )
  342. {
  343. flDamage = 65.0f;
  344. if ( pEntity && pEntity->IsPlayer() )
  345. {
  346. Vector vTragetForward;
  347. AngleVectors( pEntity->GetAbsAngles(), &vTragetForward );
  348. Vector2D vecLOS = (pEntity->GetAbsOrigin() - pPlayer->GetAbsOrigin()).AsVector2D();
  349. Vector2DNormalize( vecLOS );
  350. float flDot = vecLOS.Dot( vTragetForward.AsVector2D() );
  351. //Triple the damage if we are stabbing them in the back.
  352. if ( flDot > 0.80f )
  353. flDamage *= 3;
  354. }
  355. }
  356. else
  357. {
  358. if ( bFirstSwing )
  359. {
  360. // first swing does full damage
  361. flDamage = 20;
  362. }
  363. else
  364. {
  365. // subsequent swings do less
  366. flDamage = 15;
  367. }
  368. }
  369. //=============================================================================
  370. // HPE_BEGIN:
  371. // [tj] Hacky cheat to lower knife damage for testing
  372. //=============================================================================
  373. flDamage *= (KnifeDamageScale.GetInt() / 100.0f);
  374. //=============================================================================
  375. // HPE_END
  376. //=============================================================================
  377. CTakeDamageInfo info( pPlayer, pPlayer, flDamage, DMG_BULLET | DMG_NEVERGIB );
  378. CalculateMeleeDamageForce( &info, vForward, tr.endpos, 1.0f/flDamage );
  379. pEntity->DispatchTraceAttack( info, vForward, &tr );
  380. ApplyMultiDamage();
  381. }
  382. CCS_GameStats.Event_KnifeUse( pPlayer, bStab, flDamage );
  383. #endif
  384. if ( bDidHit )
  385. {
  386. // delay the decal a bit
  387. m_trHit = tr;
  388. // Store the ent in an EHANDLE, just in case it goes away by the time we get into our think function.
  389. m_pTraceHitEnt = tr.m_pEnt;
  390. m_bStab = bStab; //store this so we know what hit sound to play
  391. m_flSmackTime = gpGlobals->curtime + (bStab?0.2f:0.1f);
  392. }
  393. return bDidHit;
  394. }
  395. void CKnife::ItemPostFrame( void )
  396. {
  397. if( m_flSmackTime > 0 && gpGlobals->curtime > m_flSmackTime )
  398. {
  399. Smack();
  400. m_flSmackTime = -1;
  401. }
  402. BaseClass::ItemPostFrame();
  403. }
  404. bool CKnife::CanDrop()
  405. {
  406. return false;
  407. }