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.

330 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "util.h"
  8. #include "weapon_tfc_crowbar.h"
  9. #include "decals.h"
  10. #if defined( CLIENT_DLL )
  11. #include "c_tfc_player.h"
  12. #else
  13. #include "tfc_player.h"
  14. #endif
  15. #define KNIFE_BODYHIT_VOLUME 128
  16. #define KNIFE_WALLHIT_VOLUME 512
  17. static ConVar tfc_crowbar_damage_first( "tfc_crowbar_damage_first", "25", 0, "First crowbar hit damage." );
  18. static ConVar tfc_crowbar_damage_next( "tfc_crowbar_damage_next", "12.5", 0, "Crowbar hit damage after first hit." );
  19. static Vector head_hull_mins( -16, -16, -18 );
  20. static Vector head_hull_maxs( 16, 16, 18 );
  21. // ----------------------------------------------------------------------------- //
  22. // CTFCCrowbar tables.
  23. // ----------------------------------------------------------------------------- //
  24. IMPLEMENT_NETWORKCLASS_ALIASED( TFCCrowbar, DT_WeaponCrowbar )
  25. BEGIN_NETWORK_TABLE( CTFCCrowbar, DT_WeaponCrowbar )
  26. END_NETWORK_TABLE()
  27. BEGIN_PREDICTION_DATA( CTFCCrowbar )
  28. END_PREDICTION_DATA()
  29. LINK_ENTITY_TO_CLASS( weapon_crowbar, CTFCCrowbar );
  30. PRECACHE_WEAPON_REGISTER( weapon_crowbar );
  31. #ifndef CLIENT_DLL
  32. BEGIN_DATADESC( CTFCCrowbar )
  33. DEFINE_FUNCTION( Smack )
  34. END_DATADESC()
  35. #endif
  36. // ----------------------------------------------------------------------------- //
  37. // CTFCCrowbar implementation.
  38. // ----------------------------------------------------------------------------- //
  39. CTFCCrowbar::CTFCCrowbar()
  40. {
  41. }
  42. bool CTFCCrowbar::HasPrimaryAmmo()
  43. {
  44. return true;
  45. }
  46. bool CTFCCrowbar::CanBeSelected()
  47. {
  48. return true;
  49. }
  50. void CTFCCrowbar::Precache()
  51. {
  52. BaseClass::Precache();
  53. }
  54. void CTFCCrowbar::Spawn()
  55. {
  56. Precache();
  57. m_iClip1 = -1;
  58. BaseClass::Spawn();
  59. }
  60. bool CTFCCrowbar::Deploy()
  61. {
  62. CPASAttenuationFilter filter( this );
  63. filter.UsePredictionRules();
  64. EmitSound( filter, entindex(), "Weapon_Crowbar.Deploy" );
  65. return BaseClass::Deploy();
  66. }
  67. void CTFCCrowbar::Holster( int skiplocal )
  68. {
  69. GetPlayerOwner()->m_flNextAttack = gpGlobals->curtime + 0.5;
  70. }
  71. void FindHullIntersection( const Vector &vecSrc, trace_t &tr, const Vector &mins, const Vector &maxs, CBaseEntity *pEntity )
  72. {
  73. int i, j, k;
  74. float distance;
  75. Vector minmaxs[2] = {mins, maxs};
  76. trace_t tmpTrace;
  77. Vector vecHullEnd = tr.endpos;
  78. Vector vecEnd;
  79. distance = 1e6f;
  80. vecHullEnd = vecSrc + ((vecHullEnd - vecSrc)*2);
  81. UTIL_TraceLine( vecSrc, vecHullEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
  82. if ( tmpTrace.fraction < 1.0 )
  83. {
  84. tr = tmpTrace;
  85. return;
  86. }
  87. for ( i = 0; i < 2; i++ )
  88. {
  89. for ( j = 0; j < 2; j++ )
  90. {
  91. for ( k = 0; k < 2; k++ )
  92. {
  93. vecEnd.x = vecHullEnd.x + minmaxs[i][0];
  94. vecEnd.y = vecHullEnd.y + minmaxs[j][1];
  95. vecEnd.z = vecHullEnd.z + minmaxs[k][2];
  96. UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
  97. if ( tmpTrace.fraction < 1.0 )
  98. {
  99. float thisDistance = (tmpTrace.endpos - vecSrc).Length();
  100. if ( thisDistance < distance )
  101. {
  102. tr = tmpTrace;
  103. distance = thisDistance;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. void CTFCCrowbar::ItemPostFrame()
  111. {
  112. // Store this off so we can detect if it's our first swing or not later on.
  113. m_flStoredPrimaryAttack = m_flNextPrimaryAttack;
  114. BaseClass::ItemPostFrame();
  115. }
  116. void CTFCCrowbar::PrimaryAttack()
  117. {
  118. CTFCPlayer *pPlayer = GetPlayerOwner();
  119. Vector vForward;
  120. AngleVectors( pPlayer->EyeAngles(), &vForward );
  121. Vector vecSrc = pPlayer->Weapon_ShootPosition();
  122. Vector vecEnd = vecSrc + vForward * 32;
  123. trace_t tr;
  124. UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );
  125. if ( tr.fraction >= 1.0 )
  126. {
  127. UTIL_TraceHull( vecSrc, vecEnd, head_hull_mins, head_hull_maxs, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &tr );
  128. if ( tr.fraction < 1.0 )
  129. {
  130. // Calculate the point of intersection of the line (or hull) and the object we hit
  131. // This is and approximation of the "best" intersection
  132. CBaseEntity *pHit = tr.m_pEnt;
  133. if ( !pHit || pHit->IsBSPModel() )
  134. FindHullIntersection( vecSrc, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, pPlayer );
  135. vecEnd = tr.endpos; // This is the point on the actual surface (the hull could have hit space)
  136. }
  137. }
  138. bool bDidHit = tr.fraction < 1.0f;
  139. #ifndef CLIENT_DLL
  140. bool bFirstSwing = (gpGlobals->curtime - m_flStoredPrimaryAttack) >= 1;
  141. #endif
  142. pPlayer->DoAnimationEvent( PLAYERANIMEVENT_FIRE_GUN );
  143. m_flTimeWeaponIdle = gpGlobals->curtime + 2;
  144. m_flNextPrimaryAttack = gpGlobals->curtime + 0.4f;
  145. if ( bDidHit )
  146. {
  147. SendWeaponAnim( ACT_VM_HITCENTER );
  148. }
  149. else
  150. {
  151. // Allow for there only being hit activities.
  152. if ( !SendWeaponAnim( ACT_VM_MISSCENTER ) )
  153. SendWeaponAnim( ACT_VM_HITCENTER );
  154. // play wiff or swish sound
  155. WeaponSound( MELEE_MISS );
  156. }
  157. bool bPlayImpactEffect = false;
  158. #ifndef CLIENT_DLL
  159. if ( bDidHit )
  160. {
  161. CBaseEntity *pEntity = tr.m_pEnt;
  162. ClearMultiDamage();
  163. float flDamage = 0;
  164. bool bDoEffects = true;
  165. AxeHit( pEntity, bFirstSwing, tr, &flDamage, &bDoEffects );
  166. if ( flDamage != 0 )
  167. {
  168. CTakeDamageInfo info( pPlayer, pPlayer, flDamage, DMG_CLUB | DMG_NEVERGIB );
  169. CalculateMeleeDamageForce( &info, vForward, tr.endpos, 1.0f/flDamage );
  170. pEntity->DispatchTraceAttack( info, vForward, &tr );
  171. ApplyMultiDamage();
  172. }
  173. if ( bDoEffects )
  174. {
  175. if ( pEntity && pEntity->IsPlayer() )
  176. {
  177. WeaponSound( MELEE_HIT );
  178. if ( pEntity->IsAlive() )
  179. bPlayImpactEffect = true; // no blood effect on dead bodies
  180. }
  181. else
  182. {
  183. bPlayImpactEffect = true; // always show impact effects on world objects
  184. }
  185. }
  186. else
  187. {
  188. bDoEffects = false;
  189. }
  190. }
  191. #endif
  192. if ( bPlayImpactEffect )
  193. {
  194. // delay the decal a bit
  195. m_trHit = tr;
  196. // Store the ent in an EHANDLE, just in case it goes away by the time we get into our think function.
  197. m_pTraceHitEnt = tr.m_pEnt;
  198. SetThink( &CTFCCrowbar::Smack );
  199. SetNextThink( gpGlobals->curtime + 0.2f );
  200. }
  201. }
  202. void CTFCCrowbar::Smack()
  203. {
  204. m_trHit.m_pEnt = m_pTraceHitEnt;
  205. UTIL_ImpactTrace( &m_trHit, DMG_CLUB );
  206. surfacedata_t *psurf = physprops->GetSurfaceData( m_trHit.surface.surfaceProps );
  207. if ( psurf->game.material != CHAR_TEX_FLESH && psurf->game.material != CHAR_TEX_BLOODYFLESH )
  208. WeaponSound( MELEE_HIT_WORLD );
  209. }
  210. void CTFCCrowbar::WeaponIdle()
  211. {
  212. //ResetEmptySound();
  213. CTFCPlayer *pPlayer = GetPlayerOwner();
  214. if (m_flTimeWeaponIdle > gpGlobals->curtime)
  215. return;
  216. m_flTimeWeaponIdle = gpGlobals->curtime + 20;
  217. // only idle if the slid isn't back
  218. SendWeaponAnim( ACT_VM_IDLE );
  219. }
  220. bool CTFCCrowbar::CanDrop()
  221. {
  222. return false;
  223. }
  224. TFCWeaponID CTFCCrowbar::GetWeaponID( void ) const
  225. {
  226. return WEAPON_CROWBAR;
  227. }
  228. #ifdef CLIENT_DLL
  229. // ------------------------------------------------------------------------------------------------ //
  230. // ------------------------------------------------------------------------------------------------ //
  231. // CLIENT DLL SPECIFIC CODE
  232. // ------------------------------------------------------------------------------------------------ //
  233. // ------------------------------------------------------------------------------------------------ //
  234. #else
  235. // ------------------------------------------------------------------------------------------------ //
  236. // ------------------------------------------------------------------------------------------------ //
  237. // GAME DLL SPECIFIC CODE
  238. // ------------------------------------------------------------------------------------------------ //
  239. // ------------------------------------------------------------------------------------------------ //
  240. void CTFCCrowbar::AxeHit( CBaseEntity *pHit, bool bFirstSwing, trace_t &tr, float *flDamage, bool *bDoEffects )
  241. {
  242. if ( bFirstSwing )
  243. *flDamage = tfc_crowbar_damage_first.GetFloat();
  244. else
  245. *flDamage = tfc_crowbar_damage_next.GetFloat();
  246. }
  247. #endif