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.

150 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "func_forcefield.h"
  8. #include "tf_shareddefs.h"
  9. #include "tf_gamerules.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. IMPLEMENT_AUTO_LIST( IFuncForceFieldAutoList );
  13. //===========================================================================================================
  14. LINK_ENTITY_TO_CLASS( func_forcefield, CFuncForceField );
  15. BEGIN_DATADESC( CFuncForceField )
  16. END_DATADESC()
  17. IMPLEMENT_SERVERCLASS_ST( CFuncForceField, DT_FuncForceField )
  18. END_SEND_TABLE()
  19. //-----------------------------------------------------------------------------
  20. // Purpose:
  21. //-----------------------------------------------------------------------------
  22. void CFuncForceField::Spawn( void )
  23. {
  24. BaseClass::Spawn();
  25. SetActive( IsOn() );
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose:
  29. //-----------------------------------------------------------------------------
  30. int CFuncForceField::UpdateTransmitState()
  31. {
  32. return SetTransmitState( FL_EDICT_ALWAYS );
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Purpose: Only transmit this entity to clients that aren't in our team
  36. //-----------------------------------------------------------------------------
  37. int CFuncForceField::ShouldTransmit( const CCheckTransmitInfo *pInfo )
  38. {
  39. return FL_EDICT_ALWAYS;
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose:
  43. //-----------------------------------------------------------------------------
  44. void CFuncForceField::TurnOff( void )
  45. {
  46. BaseClass::TurnOff();
  47. SetActive( false );
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose:
  51. //-----------------------------------------------------------------------------
  52. void CFuncForceField::TurnOn( void )
  53. {
  54. BaseClass::TurnOn();
  55. SetActive( true );
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose:
  59. //-----------------------------------------------------------------------------
  60. bool CFuncForceField::ShouldCollide( int collisionGroup, int contentsMask ) const
  61. {
  62. // Force fields are off during a team win
  63. if ( TFGameRules()->State_Get() == GR_STATE_TEAM_WIN )
  64. return false;
  65. if ( GetTeamNumber() == TEAM_UNASSIGNED )
  66. return false;
  67. if ( collisionGroup == COLLISION_GROUP_PLAYER_MOVEMENT )
  68. {
  69. switch ( GetTeamNumber() )
  70. {
  71. case TF_TEAM_BLUE:
  72. if ( !( contentsMask & CONTENTS_BLUETEAM ) )
  73. return false;
  74. break;
  75. case TF_TEAM_RED:
  76. if ( !( contentsMask & CONTENTS_REDTEAM ) )
  77. return false;
  78. break;
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose:
  86. //-----------------------------------------------------------------------------
  87. void CFuncForceField::SetActive( bool bActive )
  88. {
  89. if ( bActive )
  90. {
  91. // We're a trigger, but we want to be solid. Our ShouldCollide() will make
  92. // us non-solid to members of the team that spawns here.
  93. RemoveSolidFlags( FSOLID_TRIGGER );
  94. RemoveSolidFlags( FSOLID_NOT_SOLID );
  95. }
  96. else
  97. {
  98. AddSolidFlags( FSOLID_NOT_SOLID );
  99. AddSolidFlags( FSOLID_TRIGGER );
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. //-----------------------------------------------------------------------------
  105. bool PointsCrossForceField( const Vector& vecStart, const Vector &vecEnd, int nTeamToIgnore )
  106. {
  107. // Setup the ray.
  108. Ray_t ray;
  109. ray.Init( vecStart, vecEnd );
  110. for ( int i = 0; i < IFuncForceFieldAutoList::AutoList().Count(); ++i )
  111. {
  112. CFuncForceField *pEntity = static_cast< CFuncForceField* >( IFuncForceFieldAutoList::AutoList()[i] );
  113. if ( pEntity->m_iDisabled )
  114. continue;
  115. if ( pEntity->GetTeamNumber() == nTeamToIgnore && nTeamToIgnore != TEAM_UNASSIGNED )
  116. continue;
  117. trace_t trace;
  118. enginetrace->ClipRayToEntity( ray, MASK_ALL, pEntity, &trace );
  119. if ( trace.fraction < 1.0f )
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }