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.

191 lines
4.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "basecombatweapon.h"
  9. #include "explode.h"
  10. #include "eventqueue.h"
  11. #include "gamerules.h"
  12. #include "ammodef.h"
  13. #include "in_buttons.h"
  14. #include "soundent.h"
  15. #include "ndebugoverlay.h"
  16. #include "vstdlib/random.h"
  17. #include "engine/IEngineSound.h"
  18. #include "game.h"
  19. #include "player.h"
  20. #include "entitylist.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. // Spawnflags
  24. #define SF_MESSAGE_DISABLED 1
  25. //-----------------------------------------------------------------------------
  26. // Purpose:
  27. //-----------------------------------------------------------------------------
  28. class CMessageEntity : public CPointEntity
  29. {
  30. DECLARE_CLASS( CMessageEntity, CPointEntity );
  31. public:
  32. void Spawn( void );
  33. void Activate( void );
  34. void Think( void );
  35. void DrawOverlays(void);
  36. virtual void UpdateOnRemove();
  37. void InputEnable( inputdata_t &inputdata );
  38. void InputDisable( inputdata_t &inputdata );
  39. DECLARE_DATADESC();
  40. protected:
  41. int m_radius;
  42. string_t m_messageText;
  43. bool m_drawText;
  44. bool m_bDeveloperOnly;
  45. bool m_bEnabled;
  46. };
  47. LINK_ENTITY_TO_CLASS( point_message, CMessageEntity );
  48. BEGIN_DATADESC( CMessageEntity )
  49. DEFINE_KEYFIELD( m_radius, FIELD_INTEGER, "radius" ),
  50. DEFINE_KEYFIELD( m_messageText, FIELD_STRING, "message" ),
  51. DEFINE_KEYFIELD( m_bDeveloperOnly, FIELD_BOOLEAN, "developeronly" ),
  52. DEFINE_FIELD( m_drawText, FIELD_BOOLEAN ),
  53. DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
  54. // Inputs
  55. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  56. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  57. END_DATADESC()
  58. static CUtlVector< CHandle< CMessageEntity > > g_MessageEntities;
  59. //-----------------------------------------
  60. // Spawn
  61. //-----------------------------------------
  62. void CMessageEntity::Spawn( void )
  63. {
  64. SetNextThink( gpGlobals->curtime + 0.1f );
  65. m_drawText = false;
  66. m_bDeveloperOnly = false;
  67. m_bEnabled = !HasSpawnFlags( SF_MESSAGE_DISABLED );
  68. //m_debugOverlays |= OVERLAY_TEXT_BIT; // make sure we always show the text
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. //-----------------------------------------------------------------------------
  73. void CMessageEntity::Activate( void )
  74. {
  75. BaseClass::Activate();
  76. CHandle< CMessageEntity > h;
  77. h = this;
  78. g_MessageEntities.AddToTail( h );
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. //-----------------------------------------------------------------------------
  83. void CMessageEntity::UpdateOnRemove()
  84. {
  85. BaseClass::UpdateOnRemove();
  86. CHandle< CMessageEntity > h;
  87. h = this;
  88. g_MessageEntities.FindAndRemove( h );
  89. BaseClass::UpdateOnRemove();
  90. }
  91. //-----------------------------------------
  92. // Think
  93. //-----------------------------------------
  94. void CMessageEntity::Think( void )
  95. {
  96. SetNextThink( gpGlobals->curtime + 0.1f );
  97. // check for player distance
  98. CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
  99. if ( !pPlayer || ( pPlayer->GetFlags() & FL_NOTARGET ) )
  100. return;
  101. Vector worldTargetPosition = pPlayer->EyePosition();
  102. // bail if player is too far away
  103. if ( (worldTargetPosition - GetAbsOrigin()).Length() > m_radius )
  104. {
  105. m_drawText = false;
  106. return;
  107. }
  108. // turn on text
  109. m_drawText = true;
  110. }
  111. //-------------------------------------------
  112. //-------------------------------------------
  113. void CMessageEntity::DrawOverlays(void)
  114. {
  115. if ( !m_drawText )
  116. return;
  117. if ( m_bDeveloperOnly && !g_pDeveloper->GetInt() )
  118. return;
  119. if ( !m_bEnabled )
  120. return;
  121. // display text if they are within range
  122. char tempstr[512];
  123. Q_snprintf( tempstr, sizeof(tempstr), "%s", STRING(m_messageText) );
  124. EntityText( 0, tempstr, 0);
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Purpose:
  128. //-----------------------------------------------------------------------------
  129. void CMessageEntity::InputEnable( inputdata_t &inputdata )
  130. {
  131. m_bEnabled = true;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Purpose:
  135. //-----------------------------------------------------------------------------
  136. void CMessageEntity::InputDisable( inputdata_t &inputdata )
  137. {
  138. m_bEnabled = false;
  139. }
  140. // This is a hack to make point_message stuff appear in developer 0 release builds
  141. // for now
  142. void DrawMessageEntities()
  143. {
  144. int c = g_MessageEntities.Count();
  145. for ( int i = c - 1; i >= 0; i-- )
  146. {
  147. CMessageEntity *me = g_MessageEntities[ i ];
  148. if ( !me )
  149. {
  150. g_MessageEntities.Remove( i );
  151. continue;
  152. }
  153. me->DrawOverlays();
  154. }
  155. }