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.

229 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hl1_basecombatweapon_shared.h"
  8. #include "hl1_player_shared.h"
  9. LINK_ENTITY_TO_CLASS( basehl1combatweapon, CBaseHL1CombatWeapon );
  10. IMPLEMENT_NETWORKCLASS_ALIASED( BaseHL1CombatWeapon , DT_BaseHL1CombatWeapon )
  11. BEGIN_NETWORK_TABLE( CBaseHL1CombatWeapon , DT_BaseHL1CombatWeapon )
  12. END_NETWORK_TABLE()
  13. BEGIN_PREDICTION_DATA( CBaseHL1CombatWeapon )
  14. END_PREDICTION_DATA()
  15. void CBaseHL1CombatWeapon::Spawn( void )
  16. {
  17. Precache();
  18. SetSolid( SOLID_BBOX );
  19. m_flNextEmptySoundTime = 0.0f;
  20. // Weapons won't show up in trace calls if they are being carried...
  21. RemoveEFlags( EFL_USE_PARTITION_WHEN_NOT_SOLID );
  22. m_iState = WEAPON_NOT_CARRIED;
  23. // Assume
  24. m_nViewModelIndex = 0;
  25. // If I use clips, set my clips to the default
  26. if ( UsesClipsForAmmo1() )
  27. {
  28. m_iClip1 = GetDefaultClip1();
  29. }
  30. else
  31. {
  32. SetPrimaryAmmoCount( GetDefaultClip1() );
  33. m_iClip1 = WEAPON_NOCLIP;
  34. }
  35. if ( UsesClipsForAmmo2() )
  36. {
  37. m_iClip2 = GetDefaultClip2();
  38. }
  39. else
  40. {
  41. SetSecondaryAmmoCount( GetDefaultClip2() );
  42. m_iClip2 = WEAPON_NOCLIP;
  43. }
  44. SetModel( GetWorldModel() );
  45. #if !defined( CLIENT_DLL )
  46. FallInit();
  47. SetCollisionGroup( COLLISION_GROUP_WEAPON );
  48. m_takedamage = DAMAGE_EVENTS_ONLY;
  49. SetBlocksLOS( false );
  50. // Default to non-removeable, because we don't want the
  51. // game_weapon_manager entity to remove weapons that have
  52. // been hand-placed by level designers. We only want to remove
  53. // weapons that have been dropped by NPC's.
  54. SetRemoveable( false );
  55. #endif
  56. //Make weapons easier to pick up in MP.
  57. if ( g_pGameRules->IsMultiplayer() )
  58. {
  59. CollisionProp()->UseTriggerBounds( true, 36 );
  60. }
  61. else
  62. {
  63. CollisionProp()->UseTriggerBounds( true, 24 );
  64. }
  65. // Use more efficient bbox culling on the client. Otherwise, it'll setup bones for most
  66. // characters even when they're not in the frustum.
  67. AddEffects( EF_BONEMERGE_FASTCULL );
  68. }
  69. #if defined( CLIENT_DLL )
  70. #define HL1_BOB_CYCLE_MIN 1.0f
  71. #define HL1_BOB_CYCLE_MAX 0.45f
  72. #define HL1_BOB 0.002f
  73. #define HL1_BOB_UP 0.5f
  74. float g_lateralBob;
  75. float g_verticalBob;
  76. static ConVar cl_bobcycle( "cl_bobcycle","0.8" );
  77. static ConVar cl_bob( "cl_bob","0.002" );
  78. static ConVar cl_bobup( "cl_bobup","0.5" );
  79. // Register these cvars if needed for easy tweaking
  80. static ConVar v_iyaw_cycle( "v_iyaw_cycle", "2"/*, FCVAR_UNREGISTERED*/ );
  81. static ConVar v_iroll_cycle( "v_iroll_cycle", "0.5"/*, FCVAR_UNREGISTERED*/ );
  82. static ConVar v_ipitch_cycle( "v_ipitch_cycle", "1"/*, FCVAR_UNREGISTERED*/ );
  83. static ConVar v_iyaw_level( "v_iyaw_level", "0.3"/*, FCVAR_UNREGISTERED*/ );
  84. static ConVar v_iroll_level( "v_iroll_level", "0.1"/*, FCVAR_UNREGISTERED*/ );
  85. static ConVar v_ipitch_level( "v_ipitch_level", "0.3"/*, FCVAR_UNREGISTERED*/ );
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. // Output : float
  89. //-----------------------------------------------------------------------------
  90. float CBaseHL1CombatWeapon::CalcViewmodelBob( void )
  91. {
  92. static float bobtime;
  93. static float lastbobtime;
  94. float cycle;
  95. CBasePlayer *player = ToBasePlayer( GetOwner() );
  96. //Assert( player );
  97. //NOTENOTE: For now, let this cycle continue when in the air, because it snaps badly without it
  98. if ( ( !gpGlobals->frametime ) || ( player == NULL ) )
  99. {
  100. //NOTENOTE: We don't use this return value in our case (need to restructure the calculation function setup!)
  101. return 0.0f;// just use old value
  102. }
  103. //Find the speed of the player
  104. float speed = player->GetLocalVelocity().Length2D();
  105. //FIXME: This maximum speed value must come from the server.
  106. // MaxSpeed() is not sufficient for dealing with sprinting - jdw
  107. speed = clamp( speed, -320, 320 );
  108. float bob_offset = RemapVal( speed, 0, 320, 0.0f, 1.0f );
  109. bobtime += ( gpGlobals->curtime - lastbobtime ) * bob_offset;
  110. lastbobtime = gpGlobals->curtime;
  111. //Calculate the vertical bob
  112. cycle = bobtime - (int)(bobtime/HL1_BOB_CYCLE_MAX)*HL1_BOB_CYCLE_MAX;
  113. cycle /= HL1_BOB_CYCLE_MAX;
  114. if ( cycle < HL1_BOB_UP )
  115. {
  116. cycle = M_PI * cycle / HL1_BOB_UP;
  117. }
  118. else
  119. {
  120. cycle = M_PI + M_PI*(cycle-HL1_BOB_UP)/(1.0 - HL1_BOB_UP);
  121. }
  122. g_verticalBob = speed*0.005f;
  123. g_verticalBob = g_verticalBob*0.3 + g_verticalBob*0.7*sin(cycle);
  124. g_verticalBob = clamp( g_verticalBob, -7.0f, 4.0f );
  125. //Calculate the lateral bob
  126. cycle = bobtime - (int)(bobtime/HL1_BOB_CYCLE_MAX*2)*HL1_BOB_CYCLE_MAX*2;
  127. cycle /= HL1_BOB_CYCLE_MAX*2;
  128. if ( cycle < HL1_BOB_UP )
  129. {
  130. cycle = M_PI * cycle / HL1_BOB_UP;
  131. }
  132. else
  133. {
  134. cycle = M_PI + M_PI*(cycle-HL1_BOB_UP)/(1.0 - HL1_BOB_UP);
  135. }
  136. g_lateralBob = speed*0.005f;
  137. g_lateralBob = g_lateralBob*0.3 + g_lateralBob*0.7*sin(cycle);
  138. g_lateralBob = clamp( g_lateralBob, -7.0f, 4.0f );
  139. //NOTENOTE: We don't use this return value in our case (need to restructure the calculation function setup!)
  140. return 0.0f;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose:
  144. // Input : &origin -
  145. // &angles -
  146. // viewmodelindex -
  147. //-----------------------------------------------------------------------------
  148. void CBaseHL1CombatWeapon::AddViewmodelBob( CBaseViewModel *viewmodel, Vector &origin, QAngle &angles )
  149. {
  150. Vector forward, right;
  151. AngleVectors( angles, &forward, &right, NULL );
  152. CalcViewmodelBob();
  153. // Apply bob, but scaled down to 40%
  154. VectorMA( origin, g_verticalBob * 0.1f, forward, origin );
  155. // Z bob a bit more
  156. origin[2] += g_verticalBob * 0.1f;
  157. // bob the angles
  158. angles[ ROLL ] += g_verticalBob * 0.5f;
  159. angles[ PITCH ] -= g_verticalBob * 0.4f;
  160. angles[ YAW ] -= g_lateralBob * 0.3f;
  161. VectorMA( origin, g_lateralBob * 0.8f, right, origin );
  162. }
  163. #else
  164. Vector CBaseHL1CombatWeapon::GetSoundEmissionOrigin() const
  165. {
  166. if ( gpGlobals->maxClients == 1 || !GetOwner() )
  167. return CBaseCombatWeapon::GetSoundEmissionOrigin();
  168. // Vector vecOwner = GetOwner()->GetSoundEmissionOrigin();
  169. // Vector vecThis = WorldSpaceCenter();
  170. // DevMsg("SoundEmissionOrigin: Owner: %4.1f,%4.1f,%4.1f Default:%4.1f,%4.1f,%4.1f\n",
  171. // vecOwner.x, vecOwner.y, vecOwner.z,
  172. // vecThis.x, vecThis.y, vecThis.z );
  173. // TEMP fix for HL1MP... sounds are sometimes beeing emitted underneath the ground
  174. return GetOwner()->GetSoundEmissionOrigin();
  175. }
  176. #endif