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.

250 lines
7.6 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A camera entity that's used by the -makedevshots system to take
  4. // dev screenshots everytime the map is checked into source control.
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "tier0/icommandline.h"
  10. #include "igamesystem.h"
  11. #include "filesystem.h"
  12. #include <keyvalues.h>
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. int g_iDevShotCameraCount = 0;
  16. #define DEVSHOT_INITIAL_WAIT 5 // Time after the level spawn before the first devshot camera takes it's shot
  17. #define DEVSHOT_INTERVAL 5 // Time between each devshot camera taking it's shot
  18. //-----------------------------------------------------------------------------
  19. // Purpose: A camera entity that's used by the -makedevshots system to take
  20. // dev screenshots everytime the map is checked into source control.
  21. //-----------------------------------------------------------------------------
  22. class CPointDevShotCamera : public CBaseEntity
  23. {
  24. DECLARE_CLASS( CPointDevShotCamera, CBaseEntity );
  25. public:
  26. DECLARE_DATADESC();
  27. void Spawn( void );
  28. void DevShotThink_Setup( void );
  29. void DevShotThink_TakeShot( void );
  30. void DevShotThink_PostShot( void );
  31. // Always transmit to clients so they know where to move the view to
  32. virtual int UpdateTransmitState();
  33. private:
  34. string_t m_iszCameraName;
  35. int m_iFOV;
  36. };
  37. BEGIN_DATADESC( CPointDevShotCamera )
  38. DEFINE_FUNCTION( DevShotThink_Setup ),
  39. DEFINE_FUNCTION( DevShotThink_TakeShot ),
  40. DEFINE_FUNCTION( DevShotThink_PostShot ),
  41. DEFINE_KEYFIELD( m_iszCameraName, FIELD_STRING, "cameraname" ),
  42. DEFINE_KEYFIELD( m_iFOV, FIELD_INTEGER, "FOV" ),
  43. END_DATADESC()
  44. LINK_ENTITY_TO_CLASS( point_devshot_camera, CPointDevShotCamera );
  45. //-----------------------------------------------------------------------------
  46. // Purpose:
  47. //-----------------------------------------------------------------------------
  48. void CPointDevShotCamera::Spawn( void )
  49. {
  50. BaseClass::Spawn();
  51. // Remove this entity immediately if we're not making devshots
  52. if ( !CommandLine()->FindParm("-makedevshots") )
  53. {
  54. UTIL_Remove( this );
  55. return;
  56. }
  57. // Take a screenshot when it's my turn
  58. SetThink( &CPointDevShotCamera::DevShotThink_Setup );
  59. SetNextThink( gpGlobals->curtime + DEVSHOT_INITIAL_WAIT + (g_iDevShotCameraCount * DEVSHOT_INTERVAL) );
  60. g_iDevShotCameraCount++;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. int CPointDevShotCamera::UpdateTransmitState()
  66. {
  67. // always transmit if currently used by a monitor
  68. return SetTransmitState( FL_EDICT_ALWAYS );
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. //-----------------------------------------------------------------------------
  73. void CPointDevShotCamera::DevShotThink_Setup( void )
  74. {
  75. // Move the player to the devshot camera
  76. CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost();
  77. if ( !pPlayer )
  78. return;
  79. // Hide stuff
  80. engine->ClientCommand( pPlayer->edict(), "developer 0" );
  81. engine->ClientCommand( pPlayer->edict(), "cl_drawhud 0" );
  82. engine->ClientCommand( pPlayer->edict(), "sv_cheats 1" );
  83. engine->ClientCommand( pPlayer->edict(), "god" );
  84. engine->ClientCommand( pPlayer->edict(), "notarget" );
  85. pPlayer->AddSolidFlags( FSOLID_NOT_SOLID );
  86. pPlayer->EnableControl(FALSE);
  87. pPlayer->SetViewEntity( this );
  88. pPlayer->SetFOV( this, m_iFOV );
  89. // Hide the player's viewmodel
  90. if ( pPlayer->GetActiveWeapon() )
  91. {
  92. pPlayer->GetActiveWeapon()->AddEffects( EF_NODRAW );
  93. }
  94. DispatchUpdateTransmitState();
  95. // Now take the shot next frame
  96. SetThink( &CPointDevShotCamera::DevShotThink_TakeShot );
  97. SetNextThink( gpGlobals->curtime );
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. //-----------------------------------------------------------------------------
  102. void CPointDevShotCamera::DevShotThink_TakeShot( void )
  103. {
  104. // Take the screenshot
  105. CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost();
  106. if ( !pPlayer )
  107. return;
  108. engine->ClientCommand( pPlayer->edict(), "devshots_screenshot \"%s\"", STRING(m_iszCameraName) );
  109. // Now take the shot next frame
  110. SetThink( &CPointDevShotCamera::DevShotThink_PostShot );
  111. SetNextThink( gpGlobals->curtime + (DEVSHOT_INTERVAL - 1) );
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose:
  115. //-----------------------------------------------------------------------------
  116. void CPointDevShotCamera::DevShotThink_PostShot( void )
  117. {
  118. // Take the screenshot
  119. CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost();
  120. if ( !pPlayer )
  121. return;
  122. pPlayer->SetFOV( this, 0 );
  123. // If all cameras have taken their shots, move to the next map
  124. g_iDevShotCameraCount--;
  125. if ( !g_iDevShotCameraCount )
  126. {
  127. engine->ClientCommand( pPlayer->edict(), "devshots_nextmap" );
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose: Game system to detect maps without cameras in them, and move on
  132. //-----------------------------------------------------------------------------
  133. class CDevShotSystem : public CAutoGameSystemPerFrame
  134. {
  135. public:
  136. CDevShotSystem( char const *name ) : CAutoGameSystemPerFrame( name )
  137. {
  138. }
  139. virtual void LevelInitPreEntity()
  140. {
  141. m_bIssuedNextMapCommand = false;
  142. g_iDevShotCameraCount = 0;
  143. m_bParsedMapFile = false;
  144. }
  145. virtual void SafeRemoveIfDesired( void )
  146. {
  147. // If we're not making devshots, remove this system immediately
  148. if ( !CommandLine()->FindParm("-makedevshots") )
  149. {
  150. Remove( this );
  151. return;
  152. }
  153. }
  154. virtual void FrameUpdatePostEntityThink( void )
  155. {
  156. // Wait until we're all spawned in
  157. if ( gpGlobals->curtime < 5 )
  158. return;
  159. if ( m_bIssuedNextMapCommand )
  160. return;
  161. if ( !m_bParsedMapFile )
  162. {
  163. m_bParsedMapFile = true;
  164. // See if we've got a camera file to import cameras from
  165. char szFullName[512];
  166. Q_snprintf(szFullName,sizeof(szFullName), "maps/%s.txt", STRING( gpGlobals->mapname ));
  167. KeyValues *pkvMapCameras = new KeyValues( "MapCameras" );
  168. if ( pkvMapCameras->LoadFromFile( filesystem, szFullName, "MOD" ) )
  169. {
  170. Warning( "Devshots: Loading point_devshot_camera positions from %s. \n", szFullName );
  171. // Get each camera, and add it to our list
  172. KeyValues *pkvCamera = pkvMapCameras->GetFirstSubKey();
  173. while ( pkvCamera )
  174. {
  175. // Get camera name
  176. const char *pCameraName = pkvCamera->GetName();
  177. // Make a camera, and move it to the position specified
  178. CPointDevShotCamera *pCamera = (CPointDevShotCamera*)CreateEntityByName( "point_devshot_camera" );
  179. Assert( pCamera );
  180. pCamera->KeyValue( "cameraname", pCameraName );
  181. pCamera->KeyValue( "origin", pkvCamera->GetString( "origin", "0 0 0" ) );
  182. pCamera->KeyValue( "angles", pkvCamera->GetString( "angles", "0 0 0" ) );
  183. pCamera->KeyValue( "FOV", pkvCamera->GetString( "FOV", "75" ) );
  184. DispatchSpawn( pCamera );
  185. pCamera->Activate();
  186. // Move to next camera
  187. pkvCamera = pkvCamera->GetNextKey();
  188. }
  189. }
  190. if ( !g_iDevShotCameraCount )
  191. {
  192. Warning( "Devshots: No point_devshot_camera in %s. Moving to next map.\n", STRING( gpGlobals->mapname ) );
  193. CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost();
  194. if ( pPlayer )
  195. {
  196. engine->ClientCommand( pPlayer->edict(), "devshots_nextmap" );
  197. m_bIssuedNextMapCommand = true;
  198. return;
  199. }
  200. }
  201. }
  202. }
  203. private:
  204. bool m_bIssuedNextMapCommand;
  205. bool m_bParsedMapFile;
  206. };
  207. CDevShotSystem DevShotSystem( "CDevShotSystem" );