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.

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