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.

349 lines
7.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "basetypes.h"
  10. #include "saverestore.h"
  11. #include "saverestore_utlvector.h"
  12. #include "saverestore_utlsymbol.h"
  13. #include "globalstate.h"
  14. #include "igamesystem.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. struct globalentity_t
  18. {
  19. DECLARE_SIMPLE_DATADESC();
  20. CUtlSymbol name;
  21. CUtlSymbol levelName;
  22. GLOBALESTATE state;
  23. int counter;
  24. };
  25. class CGlobalState : public CAutoGameSystem
  26. {
  27. public:
  28. CGlobalState( char const *name ) : CAutoGameSystem( name ), m_disableStateUpdates(false)
  29. {
  30. }
  31. // IGameSystem
  32. virtual void LevelShutdownPreEntity()
  33. {
  34. // don't allow state updates during shutdowns
  35. Assert( !m_disableStateUpdates );
  36. m_disableStateUpdates = true;
  37. }
  38. virtual void LevelShutdownPostEntity()
  39. {
  40. Assert( m_disableStateUpdates );
  41. m_disableStateUpdates = false;
  42. }
  43. void EnableStateUpdates( bool bEnable )
  44. {
  45. m_disableStateUpdates = !bEnable;
  46. }
  47. void SetState( int globalIndex, GLOBALESTATE state )
  48. {
  49. if ( m_disableStateUpdates || !m_list.IsValidIndex(globalIndex) )
  50. return;
  51. m_list[globalIndex].state = state;
  52. }
  53. GLOBALESTATE GetState( int globalIndex )
  54. {
  55. if ( !m_list.IsValidIndex(globalIndex) )
  56. return GLOBAL_OFF;
  57. return m_list[globalIndex].state;
  58. }
  59. void SetCounter( int globalIndex, int counter )
  60. {
  61. if ( m_disableStateUpdates || !m_list.IsValidIndex(globalIndex) )
  62. return;
  63. m_list[globalIndex].counter = counter;
  64. }
  65. int AddToCounter( int globalIndex, int delta )
  66. {
  67. if ( m_disableStateUpdates || !m_list.IsValidIndex(globalIndex) )
  68. return 0;
  69. return ( m_list[globalIndex].counter += delta );
  70. }
  71. int GetCounter( int globalIndex )
  72. {
  73. if ( !m_list.IsValidIndex(globalIndex) )
  74. return 0;
  75. return m_list[globalIndex].counter;
  76. }
  77. void SetMap( int globalIndex, string_t mapname )
  78. {
  79. if ( !m_list.IsValidIndex(globalIndex) )
  80. return;
  81. m_list[globalIndex].levelName = m_nameList.AddString( STRING(mapname) );
  82. }
  83. const char *GetMap( int globalIndex )
  84. {
  85. if ( !m_list.IsValidIndex(globalIndex) )
  86. return NULL;
  87. return m_nameList.String( m_list[globalIndex].levelName );
  88. }
  89. const char *GetName( int globalIndex )
  90. {
  91. if ( !m_list.IsValidIndex(globalIndex) )
  92. return NULL;
  93. return m_nameList.String( m_list[globalIndex].name );
  94. }
  95. int GetIndex( const char *pGlobalname )
  96. {
  97. CUtlSymbol symName = m_nameList.Find( pGlobalname );
  98. if ( symName.IsValid() )
  99. {
  100. for ( int i = m_list.Count() - 1; i >= 0; --i )
  101. {
  102. if ( m_list[i].name == symName )
  103. return i;
  104. }
  105. }
  106. return -1;
  107. }
  108. int AddEntity( const char *pGlobalname, const char *pMapName, GLOBALESTATE state )
  109. {
  110. globalentity_t entity;
  111. entity.name = m_nameList.AddString( pGlobalname );
  112. entity.levelName = m_nameList.AddString( pMapName );
  113. entity.state = state;
  114. int index = GetIndex( m_nameList.String( entity.name ) );
  115. if ( index >= 0 )
  116. return index;
  117. return m_list.AddToTail( entity );
  118. }
  119. int GetNumGlobals( void )
  120. {
  121. return m_list.Count();
  122. }
  123. void Reset( void );
  124. int Save( ISave &save );
  125. int Restore( IRestore &restore );
  126. DECLARE_SIMPLE_DATADESC();
  127. //#ifdef _DEBUG
  128. void DumpGlobals( void );
  129. //#endif
  130. public:
  131. CUtlSymbolTable m_nameList;
  132. private:
  133. bool m_disableStateUpdates;
  134. CUtlVector<globalentity_t> m_list;
  135. };
  136. static CGlobalState gGlobalState( "CGlobalState" );
  137. static CUtlSymbolDataOps g_GlobalSymbolDataOps( gGlobalState.m_nameList );
  138. void GlobalEntity_SetState( int globalIndex, GLOBALESTATE state )
  139. {
  140. gGlobalState.SetState( globalIndex, state );
  141. }
  142. void GlobalEntity_SetCounter( int globalIndex, int counter )
  143. {
  144. gGlobalState.SetCounter( globalIndex, counter );
  145. }
  146. int GlobalEntity_AddToCounter( int globalIndex, int delta )
  147. {
  148. return gGlobalState.AddToCounter( globalIndex, delta );
  149. }
  150. void GlobalEntity_SetFlags( int globalIndex, int flags )
  151. {
  152. // Flags are stored in counter var
  153. gGlobalState.SetCounter( globalIndex, flags );
  154. }
  155. void GlobalEntity_AddFlags( int globalIndex, int flags )
  156. {
  157. // Flags are stored in counter var
  158. gGlobalState.SetCounter( globalIndex, gGlobalState.GetCounter( globalIndex ) | flags );
  159. }
  160. void GlobalEntity_RemoveFlags( int globalIndex, int flags )
  161. {
  162. // Flags are stored in counter var
  163. gGlobalState.SetCounter( globalIndex, gGlobalState.GetCounter( globalIndex ) & ~flags );
  164. }
  165. void GlobalEntity_EnableStateUpdates( bool bEnable )
  166. {
  167. gGlobalState.EnableStateUpdates( bEnable );
  168. }
  169. void GlobalEntity_SetMap( int globalIndex, string_t mapname )
  170. {
  171. gGlobalState.SetMap( globalIndex, mapname );
  172. }
  173. int GlobalEntity_Add( const char *pGlobalname, const char *pMapName, GLOBALESTATE state )
  174. {
  175. return gGlobalState.AddEntity( pGlobalname, pMapName, state );
  176. }
  177. int GlobalEntity_GetIndex( const char *pGlobalname )
  178. {
  179. return gGlobalState.GetIndex( pGlobalname );
  180. }
  181. GLOBALESTATE GlobalEntity_GetState( int globalIndex )
  182. {
  183. return gGlobalState.GetState( globalIndex );
  184. }
  185. int GlobalEntity_GetCounter( int globalIndex )
  186. {
  187. return gGlobalState.GetCounter( globalIndex );
  188. }
  189. int GlobalEntity_GetFlags( int globalIndex )
  190. {
  191. // Flags are stored in counter var
  192. return gGlobalState.GetCounter( globalIndex );
  193. }
  194. const char *GlobalEntity_GetMap( int globalIndex )
  195. {
  196. return gGlobalState.GetMap( globalIndex );
  197. }
  198. const char *GlobalEntity_GetName( int globalIndex )
  199. {
  200. return gGlobalState.GetName( globalIndex );
  201. }
  202. int GlobalEntity_GetNumGlobals( void )
  203. {
  204. return gGlobalState.GetNumGlobals();
  205. }
  206. CON_COMMAND(dump_globals, "Dump all global entities/states")
  207. {
  208. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  209. return;
  210. gGlobalState.DumpGlobals();
  211. }
  212. // This is available all the time now on impulse 104, remove later
  213. //#ifdef _DEBUG
  214. void CGlobalState::DumpGlobals( void )
  215. {
  216. static char *estates[] = { "Off", "On", "Dead" };
  217. Msg( "-- Globals --\n" );
  218. for ( int i = 0; i < m_list.Count(); i++ )
  219. {
  220. Msg( "%s: %s (%s) = %d\n", m_nameList.String( m_list[i].name ), m_nameList.String( m_list[i].levelName ), estates[m_list[i].state], m_list[i].counter );
  221. }
  222. }
  223. //#endif
  224. // Global state Savedata
  225. BEGIN_SIMPLE_DATADESC( CGlobalState )
  226. DEFINE_UTLVECTOR( m_list, FIELD_EMBEDDED ),
  227. // DEFINE_FIELD( m_nameList, CUtlSymbolTable ),
  228. // DEFINE_FIELD( m_disableStateUpdates, FIELD_BOOLEAN ),
  229. END_DATADESC()
  230. BEGIN_SIMPLE_DATADESC( globalentity_t )
  231. DEFINE_CUSTOM_FIELD( name, &g_GlobalSymbolDataOps ),
  232. DEFINE_CUSTOM_FIELD( levelName, &g_GlobalSymbolDataOps ),
  233. DEFINE_FIELD( state, FIELD_INTEGER ),
  234. DEFINE_FIELD( counter, FIELD_INTEGER ),
  235. END_DATADESC()
  236. int CGlobalState::Save( ISave &save )
  237. {
  238. if ( !save.WriteFields( "GLOBAL", this, NULL, m_DataMap.dataDesc, m_DataMap.dataNumFields ) )
  239. return 0;
  240. return 1;
  241. }
  242. int CGlobalState::Restore( IRestore &restore )
  243. {
  244. Reset();
  245. if ( !restore.ReadFields( "GLOBAL", this, NULL, m_DataMap.dataDesc, m_DataMap.dataNumFields ) )
  246. return 0;
  247. return 1;
  248. }
  249. void CGlobalState::Reset( void )
  250. {
  251. m_list.Purge();
  252. m_nameList.RemoveAll();
  253. }
  254. void SaveGlobalState( CSaveRestoreData *pSaveData )
  255. {
  256. CSave saveHelper( pSaveData );
  257. gGlobalState.Save( saveHelper );
  258. }
  259. void RestoreGlobalState( CSaveRestoreData *pSaveData )
  260. {
  261. CRestore restoreHelper( pSaveData );
  262. gGlobalState.Restore( restoreHelper );
  263. }
  264. //-----------------------------------------------------------------------------
  265. // Purpose: This gets called when a level is shut down
  266. //-----------------------------------------------------------------------------
  267. void ResetGlobalState( void )
  268. {
  269. gGlobalState.Reset();
  270. }
  271. void ShowServerGameTime()
  272. {
  273. Msg( "Server game time: %f\n", gpGlobals->curtime );
  274. }
  275. CON_COMMAND(server_game_time, "Gives the game time in seconds (server's curtime)")
  276. {
  277. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  278. return;
  279. ShowServerGameTime();
  280. }