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.

198 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "func_no_build.h"
  10. #include "tf_team.h"
  11. #include "ndebugoverlay.h"
  12. #include "tf_obj.h"
  13. #include "triggers.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. DECLARE_AUTO_LIST( IFuncNoBuildAutoList );
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Defines an area where objects cannot be built
  19. //-----------------------------------------------------------------------------
  20. class CFuncNoBuild : public CBaseTrigger, public IFuncNoBuildAutoList
  21. {
  22. DECLARE_CLASS( CFuncNoBuild, CBaseTrigger );
  23. public:
  24. CFuncNoBuild();
  25. DECLARE_DATADESC();
  26. virtual void Spawn( void );
  27. virtual void Precache( void );
  28. virtual void Activate( void );
  29. // Inputs
  30. void InputSetActive( inputdata_t &inputdata );
  31. void InputSetInactive( inputdata_t &inputdata );
  32. void InputToggleActive( inputdata_t &inputdata );
  33. void SetActive( bool bActive );
  34. bool GetActive() const;
  35. //bool IsEmpty( void );
  36. bool PreventsBuildOf( int iObjectType );
  37. private:
  38. bool m_bActive;
  39. bool m_bAllowSentry;
  40. bool m_bAllowDispenser;
  41. bool m_bAllowTeleporters;
  42. };
  43. LINK_ENTITY_TO_CLASS( func_nobuild, CFuncNoBuild);
  44. BEGIN_DATADESC( CFuncNoBuild )
  45. // inputs
  46. DEFINE_INPUTFUNC( FIELD_VOID, "SetActive", InputSetActive ),
  47. DEFINE_INPUTFUNC( FIELD_VOID, "SetInactive", InputSetInactive ),
  48. DEFINE_INPUTFUNC( FIELD_VOID, "ToggleActive", InputToggleActive ),
  49. DEFINE_KEYFIELD( m_bAllowSentry, FIELD_BOOLEAN, "AllowSentry" ),
  50. DEFINE_KEYFIELD( m_bAllowDispenser, FIELD_BOOLEAN, "AllowDispenser" ),
  51. DEFINE_KEYFIELD( m_bAllowTeleporters, FIELD_BOOLEAN, "AllowTeleporters" ),
  52. END_DATADESC()
  53. PRECACHE_REGISTER( func_nobuild );
  54. IMPLEMENT_AUTO_LIST( IFuncNoBuildAutoList );
  55. //-----------------------------------------------------------------------------
  56. // Purpose:
  57. //-----------------------------------------------------------------------------
  58. CFuncNoBuild::CFuncNoBuild()
  59. : m_bAllowSentry( false )
  60. , m_bAllowDispenser( false )
  61. , m_bAllowTeleporters( false )
  62. {
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: Initializes the resource zone
  66. //-----------------------------------------------------------------------------
  67. void CFuncNoBuild::Spawn( void )
  68. {
  69. BaseClass::Spawn();
  70. InitTrigger();
  71. m_bActive = true;
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose:
  75. //-----------------------------------------------------------------------------
  76. void CFuncNoBuild::Precache( void )
  77. {
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. //-----------------------------------------------------------------------------
  82. void CFuncNoBuild::Activate( void )
  83. {
  84. BaseClass::Activate();
  85. SetActive( true );
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose:
  89. //-----------------------------------------------------------------------------
  90. void CFuncNoBuild::InputSetActive( inputdata_t &inputdata )
  91. {
  92. SetActive( true );
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. //-----------------------------------------------------------------------------
  97. void CFuncNoBuild::InputSetInactive( inputdata_t &inputdata )
  98. {
  99. SetActive( false );
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose:
  103. //-----------------------------------------------------------------------------
  104. void CFuncNoBuild::InputToggleActive( inputdata_t &inputdata )
  105. {
  106. if ( m_bActive )
  107. {
  108. SetActive( false );
  109. }
  110. else
  111. {
  112. SetActive( true );
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose:
  117. //-----------------------------------------------------------------------------
  118. void CFuncNoBuild::SetActive( bool bActive )
  119. {
  120. m_bActive = bActive;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose:
  124. //-----------------------------------------------------------------------------
  125. bool CFuncNoBuild::GetActive() const
  126. {
  127. return m_bActive;
  128. }
  129. bool CFuncNoBuild::PreventsBuildOf( int iObjectType )
  130. {
  131. if ( iObjectType == OBJ_SENTRYGUN && m_bAllowSentry )
  132. return false;
  133. if ( iObjectType == OBJ_DISPENSER && m_bAllowDispenser )
  134. return false;
  135. if ( iObjectType == OBJ_TELEPORTER && m_bAllowTeleporters )
  136. return false;
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose: Does a nobuild zone prevent us from building?
  141. //-----------------------------------------------------------------------------
  142. bool PointInNoBuild( const Vector &vecBuildOrigin, const CBaseObject* pObj )
  143. {
  144. Assert( pObj );
  145. if ( !pObj )
  146. return false;
  147. for ( int i = 0; i < IFuncNoBuildAutoList::AutoList().Count(); ++i )
  148. {
  149. CFuncNoBuild *pNoBuild = static_cast< CFuncNoBuild* >( IFuncNoBuildAutoList::AutoList()[i] );
  150. // Are we within this no build?
  151. if ( pNoBuild->GetActive()
  152. && pNoBuild->PointIsWithin( vecBuildOrigin )
  153. && pNoBuild->PreventsBuildOf( pObj->GetType() ) )
  154. {
  155. // See if this ent's on the team we're set to deny
  156. int nDenyTeam = pNoBuild->GetTeamNumber();
  157. if ( !nDenyTeam || nDenyTeam == pObj->GetTeamNumber() )
  158. return true;
  159. }
  160. }
  161. return false; // Building should be ok.
  162. }