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.

93 lines
2.4 KiB

  1. //===== Copyright � Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "cs_nav_mesh.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. const char* g_pszPostActivateSetupThink = "PostActivateSetupThink";
  11. class CPointHidingSpot : public CServerOnlyPointEntity
  12. {
  13. DECLARE_CLASS( CPointHidingSpot, CServerOnlyPointEntity )
  14. public:
  15. CPointHidingSpot();
  16. DECLARE_DATADESC();
  17. virtual void Activate() OVERRIDE;
  18. virtual void UpdateOnRemove() OVERRIDE;
  19. void PostActivateSetupThink();
  20. void DetachFromHidingSpot();
  21. protected:
  22. CNavArea *m_pNavArea; // nav our hiding spot is associated with
  23. HidingSpot *m_pSpot;
  24. };
  25. BEGIN_DATADESC( CPointHidingSpot )
  26. END_DATADESC()
  27. LINK_ENTITY_TO_CLASS( point_hiding_spot, CPointHidingSpot );
  28. CPointHidingSpot::CPointHidingSpot() :
  29. m_pNavArea( NULL ),
  30. m_pSpot( NULL )
  31. {
  32. }
  33. void CPointHidingSpot::Activate()
  34. {
  35. BaseClass::Activate();
  36. // Do setup after all activates have run and we're in game simulation. Nav gets loaded/setup during server
  37. // activate so we can't guarantee this ent will be before or after nav load.
  38. SetContextThink( &CPointHidingSpot::PostActivateSetupThink, gpGlobals->curtime + 0.1f, g_pszPostActivateSetupThink );
  39. }
  40. void CPointHidingSpot::DetachFromHidingSpot()
  41. {
  42. m_pSpot = NULL;
  43. }
  44. void CPointHidingSpot::UpdateOnRemove()
  45. {
  46. if ( m_pNavArea && m_pSpot )
  47. {
  48. m_pNavArea->RemoveHidingSpot( m_pSpot );
  49. bool bSuccess = TheHidingSpots.FindAndRemove( m_pSpot );
  50. Assert( bSuccess );
  51. NOTE_UNUSED( bSuccess );
  52. delete m_pSpot;
  53. }
  54. BaseClass::UpdateOnRemove();
  55. }
  56. void CPointHidingSpot::PostActivateSetupThink()
  57. {
  58. if ( !m_pNavArea )
  59. {
  60. m_pNavArea = TheNavMesh->GetNearestNavArea( this, 0, MAX_COORD_FLOAT );
  61. if ( !m_pNavArea )
  62. {
  63. Warning( "Warning: point_hiding_spot (%s) couldn't find a nearby nav. Removing.", GetDebugName() );
  64. UTIL_Remove( this );
  65. return;
  66. }
  67. Vector vecPointOnGround;
  68. m_pNavArea->GetClosestPointOnArea( GetAbsOrigin(), &vecPointOnGround );
  69. m_pSpot = TheNavMesh->CreateHidingSpot();
  70. m_pSpot->SetSaved( false );
  71. m_pSpot->SetPosition( vecPointOnGround );
  72. m_pSpot->SetFlags( HidingSpot::IN_COVER | HidingSpot::GOOD_SNIPER_SPOT | HidingSpot::IDEAL_SNIPER_SPOT );
  73. m_pSpot->SetArea( m_pNavArea );
  74. m_pNavArea->AddHidingSpot( m_pSpot );
  75. SetContextThink( NULL, TICK_NEVER_THINK, g_pszPostActivateSetupThink );
  76. }
  77. }