home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / Game_Log.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  5KB  |  229 lines

  1.  
  2. #include "../idlib/precompiled.h"
  3. #pragma hdrstop
  4.  
  5. #include "Game_local.h"
  6. #include "Game_Log.h"
  7.  
  8. /*
  9. ===============================================================================
  10.  
  11.     rvGameLog
  12.  
  13. ===============================================================================
  14. */
  15.  
  16. rvGameLogLocal    gameLogLocal;
  17. rvGameLog*        gameLog = &gameLogLocal;
  18.  
  19. /*
  20. ================
  21. rvGameLogLocal::rvGameLogLocal
  22. ================
  23. */
  24. rvGameLogLocal::rvGameLogLocal ( ) {
  25.     initialized    = false;
  26. }
  27.  
  28. /*
  29. ================
  30. rvGameLogLocal::Init
  31. ================
  32. */
  33. void rvGameLogLocal::Init ( void ) {
  34.     file        = NULL;
  35.     indexCount  = 0;
  36.     initialized    = true;
  37.     
  38.     index.Clear ( );
  39.     frame.Clear ( );
  40.     oldframe.Clear ( );
  41. }
  42.  
  43. /*
  44. ================
  45. rvGameLogLocal::Shutdown
  46. ================
  47. */
  48. void rvGameLogLocal::Shutdown    ( void ) {
  49.     index.Clear ( );
  50.     frame.Clear ( );
  51.     oldframe.Clear ( );
  52.  
  53.     if ( initialized && file ) {
  54.         const char* out;
  55.         out = va(":%d %d", gameLocal.time, gameLocal.framenum );
  56.         file->Write ( out, strlen ( out ) );
  57.         file->Flush ( );
  58.         fileSystem->CloseFile ( file );
  59.         file = NULL;
  60.     }
  61.     initialized    = false;
  62. }
  63.  
  64. /*
  65. ================
  66. rvGameLogLocal::BeginFrame
  67. ================
  68. */
  69. void rvGameLogLocal::BeginFrame    ( int time ) {
  70.     // See if logging has been turned on or not
  71.     if ( g_gamelog.GetBool ( ) != initialized ) {
  72.         if ( initialized ) {
  73.             Shutdown ( );
  74.             return;
  75.         } else { 
  76.             Init ( );
  77.         }
  78.     } else if ( !g_gamelog.GetBool ( ) ) {
  79.         return;
  80.     }        
  81. }
  82.  
  83. /*
  84. ================
  85. rvGameLogLocal::EndFrame
  86. ================
  87. */
  88. void rvGameLogLocal::EndFrame ( void ) {
  89.     int            i;
  90.     const char* out;
  91.     bool        wroteTime;
  92.  
  93.     // Dont do anything if not logging
  94.     if ( !g_gamelog.GetBool ( ) ) {
  95.         return;
  96.     }
  97.  
  98.     // When not in multiplayer, log the players approx origin and viewangles
  99.     if ( !gameLocal.isMultiplayer ) {
  100.         idPlayer* player;
  101.         player = gameLocal.GetLocalPlayer ( );
  102.         if ( player ) {
  103.             Set ( "player0_origin", va("%d %d %d", (int)player->GetPhysics()->GetOrigin()[0], (int)player->GetPhysics()->GetOrigin()[1], (int)player->GetPhysics()->GetOrigin()[2] ) );
  104.             Set ( "player0_angles_yaw", va("%g", (float)player->viewAngles[YAW] ) );
  105.             Set ( "player0_angles_pitch", va("%g", (float)player->viewAngles[PITCH] ) );
  106.             Set ( "player0_buttons", player->usercmd.buttons );
  107.             Set ( "player0_health", player->health );
  108.             Set ( "player0_armor", player->inventory.armor );
  109.         }
  110.     }    
  111.  
  112.     if ( !file ) {
  113.         idStr mapName;
  114.         idStr filename;
  115.         mapName = gameLocal.serverInfo.GetString( "si_map" );
  116.         mapName.StripFileExtension ( );
  117.         filename = "logs/" + mapName + "/" + cvarSystem->GetCVarString("win_username") + "_";
  118.  
  119.         // Find a unique filename
  120.         for ( i = 0; fileSystem->ReadFile( filename + va("%06d.log", i ), NULL, NULL ) > 0; i ++ );        
  121.  
  122.         // Actually open the file now 
  123.         file = fileSystem->OpenFileWrite ( filename + va("%06d.log", i ), "fs_cdpath" );                
  124.         if ( !file ) {
  125.             return;
  126.         }
  127.         
  128.         timer_fps.Stop( );
  129.         timer_fps.Clear ( );
  130.         timer_fps.Start ( );
  131.     } else {
  132.         static int        fpsIndex;
  133.         static float    fpsValue[4];
  134.                 
  135.         timer_fps.Stop ( );    
  136.         fpsValue[(fpsIndex++)%4] = 1000.0f / (timer_fps.Milliseconds ( ) + 1);
  137.         if ( fpsIndex >= 4 ) {
  138.             GAMELOG_SET ( "fps", Min(60,(int)((int)(fpsValue[0] + fpsValue[1] + fpsValue[2] + fpsValue[3]) / 40.0f) * 10) );
  139.         }
  140.         timer_fps.Clear ( );
  141.         timer_fps.Start ( );
  142.     }
  143.     
  144.     // Write out any new indexes that were added this frame
  145.     for ( ; indexCount < index.Num(); indexCount ++ ) {
  146.         const char* out;
  147.         out = va("#%d ", indexCount );
  148.         file->Write ( out, strlen ( out ) );
  149.         file->Write ( index[indexCount].c_str(), index[indexCount].Length() );    
  150.         file->Write ( "\r\n", 2 );
  151.     }
  152.         
  153.     // Write out any data that was added this frame    
  154.     wroteTime = false;
  155.     for ( i = frame.Num() - 1; i >= 0; i -- ) {
  156.         // TODO: filter
  157.         if ( oldframe[i] != frame[i] ) {
  158.             if ( !wroteTime ) {
  159.                 out = va(":%d %d", gameLocal.time, gameLocal.framenum );
  160.                 file->Write ( out, strlen ( out ) );
  161.                 wroteTime = true;
  162.             }        
  163.             out = va(" %d \"", i );
  164.             file->Write ( out, strlen(out) );
  165.             file->Write ( frame[i].c_str(), frame[i].Length ( ) );
  166.             file->Write ( "\"", 1 );
  167.             oldframe[i] = frame[i];
  168.         }        
  169.     }        
  170.  
  171.     if ( wroteTime ) {
  172.         file->Write ( "\r\n", 2 );    
  173.         file->Flush ( );
  174.     }
  175.  
  176.     // Clear the frame for next time
  177.     for ( i = index.Num() - 1; i >= 0; i -- ) {
  178.         frame[i] = "";
  179.     }
  180. }
  181.  
  182. /*
  183. ================
  184. rvGameLogLocal::Set
  185. ================
  186. */
  187. void rvGameLogLocal::Set ( const char* keyword, const char* value ) {    
  188.     int i;
  189.     i = index.AddUnique ( keyword );
  190.     frame.SetNum ( index.Num(), true );
  191.     oldframe.SetNum ( index.Num(), true );
  192.     frame[i] = value;
  193. }
  194.  
  195. void rvGameLogLocal::Set ( const char* keyword, int value ) {
  196.     Set ( keyword, va("%d", value ) );    
  197. }
  198.  
  199. void rvGameLogLocal::Set ( const char* keyword, float value ) {
  200.     Set ( keyword, va("%g", value ) );
  201. }
  202.  
  203. void rvGameLogLocal::Set ( const char* keyword, bool value ) {
  204.     Set ( keyword, va("%d", (int)value ) );
  205. }
  206.     
  207. /*
  208. ================
  209. rvGameLogLocal::Add
  210. ================
  211. */
  212. void rvGameLogLocal::Add ( const char* keyword, int value ) {
  213.     int i;
  214.     i = index.AddUnique ( keyword );
  215.     frame.SetNum ( index.Num(), true );
  216.     oldframe.SetNum ( index.Num(), true );
  217.     frame[i] = va("%d",atoi(frame[i].c_str()) + value );
  218. }
  219.  
  220. void rvGameLogLocal::Add ( const char* keyword, float value ) {
  221.     int i;
  222.     i = index.AddUnique ( keyword );
  223.     frame.SetNum ( index.Num(), true );
  224.     oldframe.SetNum ( index.Num(), true );
  225.     frame[i] = va("%g",atof(frame[i].c_str()) + value );
  226. }
  227.  
  228.  
  229.