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.

309 lines
8.3 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ====
  2. //
  3. // Purpose: Player for HL2.
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "logic_playerproxy.h"
  8. #include "filters.h"
  9. #if defined HL2_EPISODIC
  10. #include "hl2_player.h"
  11. #endif // HL2_EPISODIC
  12. #if defined ( PORTAL2 )
  13. #include "portal_player.h"
  14. #include "weapon_portalgun.h"
  15. #endif
  16. LINK_ENTITY_TO_CLASS( logic_playerproxy, CLogicPlayerProxy);
  17. BEGIN_DATADESC( CLogicPlayerProxy )
  18. // Base
  19. DEFINE_OUTPUT( m_RequestedPlayerHealth, "PlayerHealth" ),
  20. DEFINE_OUTPUT( m_PlayerDied, "PlayerDied" ),
  21. DEFINE_FIELD( m_hPlayer, FIELD_EHANDLE ),
  22. DEFINE_OUTPUT( m_OnJump, "OnJump" ),
  23. DEFINE_OUTPUT( m_OnDuck, "OnDuck" ),
  24. DEFINE_OUTPUT( m_OnUnDuck, "OnUnDuck" ),
  25. // Portal 2
  26. #ifdef PORTAL2
  27. DEFINE_OUTPUT( m_OnStartSlowingTime, "OnStartSlowingTime" ),
  28. DEFINE_OUTPUT( m_OnStopSlowingTime, "OnStopSlowingTime" ),
  29. DEFINE_OUTPUT( m_OnPrimaryPortalPlaced, "OnPrimaryPortalPlaced" ),
  30. DEFINE_OUTPUT( m_OnSecondaryPortalPlaced, "OnSecondaryPortalPlaced" ),
  31. DEFINE_OUTPUT( m_OnCoopPing, "OnCoopPing" ),
  32. DEFINE_INPUTFUNC( FIELD_VOID, "AddPotatosToPortalgun", InputAddPotatosToPortalgun ),
  33. DEFINE_INPUTFUNC( FIELD_VOID, "RemovePotatosFromPortalgun", InputRemovePotatosFromPortalgun ),
  34. DEFINE_INPUTFUNC( FIELD_BOOLEAN, "SetDropEnabled", InputSetDropEnabled ),
  35. DEFINE_INPUTFUNC( FIELD_VOID, "ForceVMGrabController", InputForceVMGrabController ),
  36. DEFINE_INPUTFUNC( FIELD_VOID, "ForcePhysicsGrabController", InputForcePhysicsGrabController ),
  37. DEFINE_INPUTFUNC( FIELD_VOID, "ResetGrabControllerBehavior", InputResetGrabControllerBehavior ),
  38. DEFINE_INPUTFUNC( FIELD_VOID, "PaintPlayerWithPortalPaint", InputPaintPlayerWithPortalPaint ),
  39. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetMotionBlurAmount", InputSetMotionBlurAmount ),
  40. #endif // PORTAL2
  41. // HL2 / Episodic
  42. #if defined HL2_EPISODIC && !defined( PORTAL2 )
  43. DEFINE_OUTPUT( m_OnFlashlightOn, "OnFlashlightOn" ),
  44. DEFINE_OUTPUT( m_OnFlashlightOff, "OnFlashlightOff" ),
  45. DEFINE_OUTPUT( m_PlayerMissedAR2AltFire, "PlayerMissedAR2AltFire" ),
  46. DEFINE_OUTPUT( m_PlayerHasAmmo, "PlayerHasAmmo" ),
  47. DEFINE_OUTPUT( m_PlayerHasNoAmmo, "PlayerHasNoAmmo" ),
  48. DEFINE_INPUTFUNC( FIELD_VOID, "SetFlashlightSlowDrain", InputSetFlashlightSlowDrain ),
  49. DEFINE_INPUTFUNC( FIELD_VOID, "SetFlashlightNormalDrain", InputSetFlashlightNormalDrain ),
  50. DEFINE_INPUTFUNC( FIELD_VOID, "LowerWeapon", InputLowerWeapon ),
  51. DEFINE_INPUTFUNC( FIELD_STRING, "SetLocatorTargetEntity", InputSetLocatorTargetEntity ),
  52. DEFINE_INPUTFUNC( FIELD_VOID, "RequestPlayerHealth", InputRequestPlayerHealth ),
  53. DEFINE_INPUTFUNC( FIELD_INTEGER, "SetPlayerHealth", InputSetPlayerHealth ),
  54. DEFINE_INPUTFUNC( FIELD_VOID, "RequestAmmoState", InputRequestAmmoState ),
  55. DEFINE_INPUTFUNC( FIELD_VOID, "EnableCappedPhysicsDamage", InputEnableCappedPhysicsDamage ),
  56. DEFINE_INPUTFUNC( FIELD_VOID, "DisableCappedPhysicsDamage", InputDisableCappedPhysicsDamage ),
  57. #endif // HL2_EPISODIC
  58. END_DATADESC()
  59. void CLogicPlayerProxy::Activate( void )
  60. {
  61. BaseClass::Activate();
  62. if ( m_hPlayer == NULL )
  63. {
  64. m_hPlayer = AI_GetSinglePlayer();
  65. }
  66. }
  67. bool CLogicPlayerProxy::PassesDamageFilter( const CTakeDamageInfo &info )
  68. {
  69. if (m_hDamageFilter)
  70. {
  71. CBaseFilter *pFilter = (CBaseFilter *)(m_hDamageFilter.Get());
  72. return pFilter->PassesDamageFilter(info);
  73. }
  74. return true;
  75. }
  76. void CLogicPlayerProxy::InputSetPlayerHealth( inputdata_t &inputdata )
  77. {
  78. if ( m_hPlayer == NULL )
  79. return;
  80. m_hPlayer->SetHealth( inputdata.value.Int() );
  81. }
  82. void CLogicPlayerProxy::InputRequestPlayerHealth( inputdata_t &inputdata )
  83. {
  84. if ( m_hPlayer == NULL )
  85. return;
  86. m_RequestedPlayerHealth.Set( m_hPlayer->GetHealth(), inputdata.pActivator, inputdata.pCaller );
  87. }
  88. #if defined HL2_EPISODIC && !defined( PORTAL2 )
  89. extern ConVar hl2_darkness_flashlight_factor;
  90. void CLogicPlayerProxy::InputSetFlashlightSlowDrain( inputdata_t &inputdata )
  91. {
  92. if( m_hPlayer == NULL )
  93. return;
  94. CHL2_Player *pPlayer = dynamic_cast<CHL2_Player*>(m_hPlayer.Get());
  95. if( pPlayer )
  96. pPlayer->SetFlashlightPowerDrainScale( hl2_darkness_flashlight_factor.GetFloat() );
  97. }
  98. void CLogicPlayerProxy::InputSetFlashlightNormalDrain( inputdata_t &inputdata )
  99. {
  100. if( m_hPlayer == NULL )
  101. return;
  102. CHL2_Player *pPlayer = dynamic_cast<CHL2_Player*>(m_hPlayer.Get());
  103. if( pPlayer )
  104. pPlayer->SetFlashlightPowerDrainScale( 1.0f );
  105. }
  106. void CLogicPlayerProxy::InputLowerWeapon( inputdata_t &inputdata )
  107. {
  108. if( m_hPlayer == NULL )
  109. return;
  110. CHL2_Player *pPlayer = dynamic_cast<CHL2_Player*>(m_hPlayer.Get());
  111. pPlayer->Weapon_Lower();
  112. }
  113. void CLogicPlayerProxy::InputSetLocatorTargetEntity( inputdata_t &inputdata )
  114. {
  115. if( m_hPlayer == NULL )
  116. return;
  117. CBaseEntity *pTarget = NULL; // assume no target
  118. string_t iszTarget = MAKE_STRING( inputdata.value.String() );
  119. if( iszTarget != NULL_STRING )
  120. {
  121. pTarget = gEntList.FindEntityByName( NULL, iszTarget );
  122. }
  123. CHL2_Player *pPlayer = dynamic_cast<CHL2_Player*>(m_hPlayer.Get());
  124. pPlayer->SetLocatorTargetEntity(pTarget);
  125. }
  126. void CLogicPlayerProxy::InputRequestAmmoState( inputdata_t &inputdata )
  127. {
  128. if( m_hPlayer == NULL )
  129. return;
  130. CBasePlayer *pPlayer = ToBasePlayer( m_hPlayer );
  131. for ( int i = 0 ; i < pPlayer->WeaponCount(); ++i )
  132. {
  133. CBaseCombatWeapon* pCheck = pPlayer->GetWeapon( i );
  134. if ( pCheck )
  135. {
  136. if ( pCheck->HasAnyAmmo() && (pCheck->UsesPrimaryAmmo() || pCheck->UsesSecondaryAmmo()))
  137. {
  138. m_PlayerHasAmmo.FireOutput( this, this, 0 );
  139. return;
  140. }
  141. }
  142. }
  143. m_PlayerHasNoAmmo.FireOutput( this, this, 0 );
  144. }
  145. void CLogicPlayerProxy::InputEnableCappedPhysicsDamage( inputdata_t &inputdata )
  146. {
  147. if ( m_hPlayer == NULL )
  148. return;
  149. CHL2_Player *pPlayer = dynamic_cast<CHL2_Player*>(m_hPlayer.Get());
  150. pPlayer->EnableCappedPhysicsDamage();
  151. }
  152. void CLogicPlayerProxy::InputDisableCappedPhysicsDamage( inputdata_t &inputdata )
  153. {
  154. if ( m_hPlayer == NULL )
  155. return;
  156. CHL2_Player *pPlayer = dynamic_cast<CHL2_Player*>(m_hPlayer.Get());
  157. pPlayer->DisableCappedPhysicsDamage();
  158. }
  159. #endif // HL2_EPISODIC
  160. #if defined ( PORTAL2 )
  161. CPortal_Player* GetPortalPlayerFromProxy()
  162. {
  163. if ( GameRules()->IsMultiplayer() )
  164. {
  165. Assert( 0 );
  166. Warning( "Can't use logic player proxy in multiplayer!\n" );
  167. return NULL;
  168. }
  169. return (CPortal_Player*)UTIL_PlayerByIndex( 1 );
  170. }
  171. void CLogicPlayerProxy::InputAddPotatosToPortalgun( inputdata_t &inputdata )
  172. {
  173. CPortal_Player* pPlayer = GetPortalPlayerFromProxy();
  174. if ( pPlayer == NULL )
  175. return;
  176. CWeaponPortalgun *pPortalgun = (CWeaponPortalgun*)pPlayer->GetActiveWeapon();
  177. Assert( pPortalgun );
  178. if ( !pPortalgun )
  179. return;
  180. pPortalgun->SetPotatosOnPortalgun( true );
  181. }
  182. void CLogicPlayerProxy::InputRemovePotatosFromPortalgun( inputdata_t &inputdata )
  183. {
  184. CPortal_Player* pPlayer = GetPortalPlayerFromProxy();
  185. if ( pPlayer == NULL )
  186. return;
  187. CWeaponPortalgun *pPortalgun = (CWeaponPortalgun*)pPlayer->GetActiveWeapon();
  188. Assert( pPortalgun );
  189. if ( !pPortalgun )
  190. return;
  191. pPortalgun->SetPotatosOnPortalgun( false );
  192. }
  193. void CLogicPlayerProxy::InputSetDropEnabled( inputdata_t &inputdata )
  194. {
  195. CPortal_Player* pPlayer = GetPortalPlayerFromProxy();
  196. if( pPlayer == NULL )
  197. {
  198. return;
  199. }
  200. pPlayer->SetDropEnabled( inputdata.value.Bool() );
  201. }
  202. void SetForcedGrabController( ForcedGrabControllerType type )
  203. {
  204. CPortal_Player* pPlayer = GetPortalPlayerFromProxy();
  205. if( pPlayer == NULL )
  206. {
  207. return;
  208. }
  209. pPlayer->SetForcedGrabControllerType( type );
  210. }
  211. void CLogicPlayerProxy::InputForceVMGrabController( inputdata_t &inputdata )
  212. {
  213. SetForcedGrabController( FORCE_GRAB_CONTROLLER_VM );
  214. }
  215. void CLogicPlayerProxy::InputForcePhysicsGrabController( inputdata_t &inputdata )
  216. {
  217. SetForcedGrabController( FORCE_GRAB_CONTROLLER_PHYSICS );
  218. }
  219. void CLogicPlayerProxy::InputResetGrabControllerBehavior( inputdata_t &inputdata )
  220. {
  221. SetForcedGrabController( FORCE_GRAB_CONTROLLER_DEFAULT );
  222. }
  223. void CLogicPlayerProxy::InputPaintPlayerWithPortalPaint( inputdata_t &/*inputdata*/ )
  224. {
  225. CPortal_Player* pPlayer = GetPortalPlayerFromProxy();
  226. if( pPlayer != NULL )
  227. pPlayer->Paint( PORTAL_POWER, vec3_origin );
  228. }
  229. void CLogicPlayerProxy::InputSetMotionBlurAmount( inputdata_t &inputdata )
  230. {
  231. if ( GameRules() && GameRules()->IsMultiplayer() == false )
  232. {
  233. CPortal_Player* pPlayer = GetPortalPlayerFromProxy();
  234. if( pPlayer != NULL )
  235. {
  236. pPlayer->SetMotionBlurAmount( inputdata.value.Float() );
  237. }
  238. }
  239. }
  240. #endif