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.

183 lines
5.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "info_camera_link.h"
  8. #include "point_camera.h"
  9. #include "utllinkedlist.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. // Link between entities and cameras
  14. //-----------------------------------------------------------------------------
  15. class CInfoCameraLink : public CLogicalEntity
  16. {
  17. DECLARE_CLASS( CInfoCameraLink, CLogicalEntity );
  18. DECLARE_DATADESC();
  19. public:
  20. CInfoCameraLink();
  21. ~CInfoCameraLink();
  22. virtual void Activate();
  23. private:
  24. void InputSetCamera(inputdata_t &inputdata);
  25. void InputSetTargetEntity(inputdata_t &inputdata);
  26. void SetCameraByName(const char *szName);
  27. CHandle<CPointCamera> m_hCamera;
  28. EHANDLE m_hTargetEntity;
  29. string_t m_strPointCamera;
  30. friend CBaseEntity *CreateInfoCameraLink( CBaseEntity *pTarget, CPointCamera *pCamera );
  31. friend void PointCameraSetupVisibility( CBaseEntity *pPlayer, int area, unsigned char *pvs, int pvssize );
  32. };
  33. //-----------------------------------------------------------------------------
  34. // List of all info camera links
  35. //-----------------------------------------------------------------------------
  36. CUtlFixedLinkedList<CInfoCameraLink *> g_InfoCameraLinkList;
  37. //-----------------------------------------------------------------------------
  38. // Save/load
  39. //-----------------------------------------------------------------------------
  40. BEGIN_DATADESC( CInfoCameraLink )
  41. DEFINE_KEYFIELD( m_strPointCamera, FIELD_STRING, "PointCamera" ),
  42. DEFINE_FIELD( m_hCamera, FIELD_EHANDLE ),
  43. DEFINE_FIELD( m_hTargetEntity, FIELD_EHANDLE ),
  44. // Outputs
  45. DEFINE_INPUTFUNC( FIELD_STRING, "SetCamera", InputSetCamera ),
  46. END_DATADESC()
  47. LINK_ENTITY_TO_CLASS( info_camera_link, CInfoCameraLink );
  48. //-----------------------------------------------------------------------------
  49. // Constructor, destructor
  50. //-----------------------------------------------------------------------------
  51. CInfoCameraLink::CInfoCameraLink()
  52. {
  53. g_InfoCameraLinkList.AddToTail( this );
  54. }
  55. CInfoCameraLink::~CInfoCameraLink()
  56. {
  57. g_InfoCameraLinkList.FindAndRemove( this );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose: Called after all entities have spawned and after a load game.
  61. //-----------------------------------------------------------------------------
  62. void CInfoCameraLink::Activate()
  63. {
  64. BaseClass::Activate();
  65. // Checks necessary to prevent interference with CreateInfoCameraLink
  66. if ( !m_hCamera )
  67. {
  68. SetCameraByName( STRING(m_strPointCamera) );
  69. }
  70. if ( !m_hTargetEntity )
  71. {
  72. m_hTargetEntity = gEntList.FindEntityByName( NULL, STRING(m_target) );
  73. }
  74. }
  75. void CInfoCameraLink::SetCameraByName(const char *szName)
  76. {
  77. CBaseEntity *pBaseEnt = gEntList.FindEntityByName( NULL, szName );
  78. if( pBaseEnt )
  79. {
  80. m_hCamera = dynamic_cast<CPointCamera *>( pBaseEnt );
  81. if ( m_hCamera )
  82. {
  83. // Keep the camera name consistent for save/load
  84. m_strPointCamera = MAKE_STRING( szName );
  85. }
  86. }
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. void CInfoCameraLink::InputSetCamera(inputdata_t &inputdata)
  92. {
  93. SetCameraByName( inputdata.value.String() );
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose:
  97. //-----------------------------------------------------------------------------
  98. CBaseEntity *CreateInfoCameraLink( CBaseEntity *pTarget, CPointCamera *pCamera )
  99. {
  100. CInfoCameraLink *pInfoCameraLink = (CInfoCameraLink*)CreateEntityByName( "info_camera_link" );
  101. if ( !pInfoCameraLink )
  102. return NULL;
  103. pInfoCameraLink->m_hCamera = pCamera;
  104. pInfoCameraLink->m_hTargetEntity = pTarget;
  105. pInfoCameraLink->Spawn();
  106. return pInfoCameraLink;
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Sets up visibility
  110. //-----------------------------------------------------------------------------
  111. void PointCameraSetupVisibility( CBaseEntity *pPlayer, int area, unsigned char *pvs, int pvssize )
  112. {
  113. for ( CPointCamera *pCameraEnt = GetPointCameraList(); pCameraEnt != NULL; pCameraEnt = pCameraEnt->m_pNext )
  114. {
  115. pCameraEnt->SetActive( false );
  116. }
  117. int nNext;
  118. for ( int i = g_InfoCameraLinkList.Head(); i != g_InfoCameraLinkList.InvalidIndex(); i = nNext )
  119. {
  120. nNext = g_InfoCameraLinkList.Next( i );
  121. CBaseEntity *pTargetEnt = g_InfoCameraLinkList[i]->m_hTargetEntity;
  122. if ( !pTargetEnt )
  123. {
  124. UTIL_Remove( g_InfoCameraLinkList[i] );
  125. continue;
  126. }
  127. // Don't bother if it's not visible
  128. if ( pTargetEnt->IsEffectActive( EF_NODRAW ) )
  129. continue;
  130. if ( !pTargetEnt->NetworkProp()->IsInPVS( pPlayer->edict(), pvs, pvssize ) )
  131. continue;
  132. if ( engine->CheckAreasConnected( area, pTargetEnt->NetworkProp()->AreaNum() ) )
  133. {
  134. CPointCamera *pCameraEnt = g_InfoCameraLinkList[i]->m_hCamera;
  135. if ( pCameraEnt )
  136. {
  137. engine->AddOriginToPVS( pCameraEnt->GetAbsOrigin() );
  138. pCameraEnt->SetActive( true );
  139. }
  140. }
  141. }
  142. }