Counter Strike : Global Offensive Source Code
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.

179 lines
4.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "player_pickup.h"
  10. #ifdef PORTAL
  11. #include "portal_player.h"
  12. #endif
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. // player pickup utility routine
  16. void Pickup_ForcePlayerToDropThisObject( CBaseEntity *pTarget )
  17. {
  18. if ( pTarget == NULL )
  19. return;
  20. IPhysicsObject *pPhysics = pTarget->VPhysicsGetObject();
  21. if ( pPhysics == NULL )
  22. return;
  23. if ( pPhysics->GetGameFlags() & FVPHYSICS_PLAYER_HELD )
  24. {
  25. CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
  26. pPlayer->ForceDropOfCarriedPhysObjects( pTarget );
  27. }
  28. }
  29. void Pickup_OnPhysGunDrop( CBaseEntity *pDroppedObject, CBasePlayer *pPlayer, PhysGunDrop_t Reason )
  30. {
  31. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pDroppedObject);
  32. if ( pPickup )
  33. {
  34. pPickup->OnPhysGunDrop( pPlayer, Reason );
  35. }
  36. }
  37. void Pickup_OnPhysGunPickup( CBaseEntity *pPickedUpObject, CBasePlayer *pPlayer, PhysGunPickup_t reason )
  38. {
  39. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pPickedUpObject);
  40. if ( pPickup )
  41. {
  42. pPickup->OnPhysGunPickup( pPlayer, reason );
  43. }
  44. // send phys gun pickup item event, but only in single player
  45. if ( !g_pGameRules->IsMultiplayer() )
  46. {
  47. IGameEvent *event = gameeventmanager->CreateEvent( "physgun_pickup" );
  48. if ( event )
  49. {
  50. event->SetInt( "entindex", pPickedUpObject->entindex() );
  51. gameeventmanager->FireEvent( event );
  52. }
  53. }
  54. }
  55. bool Pickup_OnAttemptPhysGunPickup( CBaseEntity *pPickedUpObject, CBasePlayer *pPlayer, PhysGunPickup_t reason )
  56. {
  57. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pPickedUpObject);
  58. if ( pPickup )
  59. {
  60. return pPickup->OnAttemptPhysGunPickup( pPlayer, reason );
  61. }
  62. return true;
  63. }
  64. CBaseEntity *Pickup_OnFailedPhysGunPickup( CBaseEntity *pPickedUpObject, Vector vPhysgunPos )
  65. {
  66. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pPickedUpObject);
  67. if ( pPickup )
  68. {
  69. return pPickup->OnFailedPhysGunPickup( vPhysgunPos );
  70. }
  71. return NULL;
  72. }
  73. bool Pickup_GetPreferredCarryAngles( CBaseEntity *pObject, CBasePlayer *pPlayer, matrix3x4_t &localToWorld, QAngle &outputAnglesWorldSpace )
  74. {
  75. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pObject);
  76. if ( pPickup )
  77. {
  78. if ( pPickup->HasPreferredCarryAnglesForPlayer( pPlayer ) )
  79. {
  80. outputAnglesWorldSpace = TransformAnglesToWorldSpace( pPickup->PreferredCarryAngles(), localToWorld );
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. bool Pickup_ForcePhysGunOpen( CBaseEntity *pObject, CBasePlayer *pPlayer )
  87. {
  88. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pObject);
  89. if ( pPickup )
  90. {
  91. return pPickup->ForcePhysgunOpen( pPlayer );
  92. }
  93. return false;
  94. }
  95. AngularImpulse Pickup_PhysGunLaunchAngularImpulse( CBaseEntity *pObject, PhysGunForce_t reason )
  96. {
  97. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pObject);
  98. if ( pPickup != NULL && pPickup->ShouldPuntUseLaunchForces( reason ) )
  99. {
  100. return pPickup->PhysGunLaunchAngularImpulse();
  101. }
  102. return RandomAngularImpulse( -600, 600 );
  103. }
  104. Vector Pickup_DefaultPhysGunLaunchVelocity( const Vector &vecForward, float flMass )
  105. {
  106. #ifdef HL2_DLL
  107. // Calculate the velocity based on physcannon rules
  108. float flForceMax = physcannon_maxforce.GetFloat();
  109. float flForce = flForceMax;
  110. float mass = flMass;
  111. if ( mass > 100 )
  112. {
  113. mass = MIN( mass, 1000 );
  114. float flForceMin = physcannon_minforce.GetFloat();
  115. flForce = SimpleSplineRemapValClamped( mass, 100, 600, flForceMax, flForceMin );
  116. }
  117. return ( vecForward * flForce );
  118. #endif
  119. // Do the simple calculation
  120. return ( vecForward * flMass );
  121. }
  122. Vector Pickup_PhysGunLaunchVelocity( CBaseEntity *pObject, const Vector &vecForward, PhysGunForce_t reason )
  123. {
  124. // The object must be valid
  125. if ( pObject == NULL )
  126. {
  127. Assert( 0 );
  128. return vec3_origin;
  129. }
  130. // Shouldn't ever get here with a non-vphysics object.
  131. IPhysicsObject *pPhysicsObject = pObject->VPhysicsGetObject();
  132. if ( pPhysicsObject == NULL )
  133. {
  134. Assert( 0 );
  135. return vec3_origin;
  136. }
  137. // Call the pickup entity's callback
  138. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pObject);
  139. if ( pPickup != NULL && pPickup->ShouldPuntUseLaunchForces( reason ) )
  140. return pPickup->PhysGunLaunchVelocity( vecForward, pPhysicsObject->GetMass() );
  141. // Do our default behavior
  142. return Pickup_DefaultPhysGunLaunchVelocity( vecForward, pPhysicsObject->GetMass() );
  143. }
  144. bool Pickup_ShouldPuntUseLaunchForces( CBaseEntity *pObject, PhysGunForce_t reason )
  145. {
  146. IPlayerPickupVPhysics *pPickup = dynamic_cast<IPlayerPickupVPhysics *>(pObject);
  147. if ( pPickup )
  148. {
  149. return pPickup->ShouldPuntUseLaunchForces( reason );
  150. }
  151. return false;
  152. }