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

  1. //----------------------------------------------------------------
  2. // Game_Debug.cpp
  3. //
  4. // Copyright 2002-2004 Raven Software
  5. //----------------------------------------------------------------
  6.  
  7. #include "../idlib/precompiled.h"
  8. #pragma hdrstop
  9.  
  10. #include "Game_local.h"
  11.  
  12. rvGameDebug    gameDebug;
  13.  
  14. /*
  15. ===============================================================================
  16.  
  17.     rvGameDebug
  18.  
  19. ===============================================================================
  20. */
  21.  
  22. /*
  23. ================
  24. rvGameDebug::rvGameDebug
  25. ================
  26. */
  27. rvGameDebug::rvGameDebug ( void ) {
  28. }
  29.  
  30. /*
  31. ================
  32. rvGameDebug::Init
  33. ================
  34. */
  35. void rvGameDebug::Init ( void ) {
  36.     focusEntity        = NULL;
  37.     overrideEntity    = NULL;
  38.     currentHud        = NULL;
  39.     memset ( &hud, 0, sizeof(hud) );
  40.  
  41.     jumpIndex = -1;
  42.     jumpPoints.Clear ( );
  43. }
  44.  
  45. /*
  46. ================
  47. rvGameDebug::Shutdown
  48. ================
  49. */
  50. void rvGameDebug::Shutdown ( void ) {
  51.     nonGameState.Clear ( );
  52.     gameStats.Clear ( );
  53.  
  54.     currentHud        = NULL;
  55.     focusEntity        = NULL;
  56.     overrideEntity    = NULL;
  57.  
  58.     memset ( &hud, 0, sizeof(hud) );
  59. }    
  60.  
  61. /*
  62. ================
  63. rvGameDebug::Think
  64. ================
  65. */
  66. void rvGameDebug::BeginFrame ( void ) {
  67.     int hudIndex;
  68.  
  69.     inFrame = true;
  70.  
  71.     hudIndex = g_showDebugHud.GetInteger();
  72.     if ( hudIndex <= 0 ) {
  73.         focusEntity = NULL;
  74.         currentHud     = NULL;
  75.         return;
  76.     }
  77.  
  78.     // Update the current debug hud if the cvar has changed
  79.     if ( g_showDebugHud.IsModified() || !currentHud ) {
  80.         if ( hudIndex > DBGHUD_MAX ) {
  81.             g_showDebugHud.SetInteger( 0 );
  82.             focusEntity = NULL;
  83.             currentHud  = NULL;
  84.             return;
  85.         }
  86.     
  87.         g_showDebugHud.ClearModified( );
  88.  
  89.         // If the debug hud hasnt been loaded yet then load it now
  90.         if ( !hud[hudIndex] ) {
  91.             hud[hudIndex] = uiManager->FindGui( va("guis/debug/hud%d.gui",hudIndex), true, true, true );
  92.             
  93.             // If the hud wasnt found auto-generate one
  94.             if ( !hud[hudIndex] ) {
  95.                 hud[hudIndex] = uiManager->Alloc();
  96.             }
  97.         }
  98.  
  99.         // Cache the debug hud state.
  100.         currentHud = hud[hudIndex];    
  101.     }
  102.     
  103.     currentHud->ClearState ( );
  104.         
  105.     // IF there is an override entity just use that, otherwise find one that
  106.     // is in front of the players crosshair    
  107.     if ( overrideEntity ) {
  108.         focusEntity = overrideEntity;
  109.         overrideEntity = NULL;
  110.     } else {
  111.         idPlayer*    player;
  112.         idVec3        start;
  113.         idVec3        end;
  114.         trace_t        tr;
  115.  
  116.         player = gameLocal.GetLocalPlayer ( );
  117.         start  = player->GetEyePosition();
  118.         end    = start + player->viewAngles.ToForward() * 4096.0f;
  119.               
  120.         gameLocal.TracePoint( player, tr, start, end, MASK_SHOT_BOUNDINGBOX, player );
  121.           if ( tr.fraction < 1.0 && tr.c.entityNum != ENTITYNUM_WORLD ) {
  122.               focusEntity = static_cast<idEntity*>(gameLocal.entities[ tr.c.entityNum ]);        
  123.           } else {
  124.               focusEntity = NULL;
  125.           }
  126.     }
  127.  
  128.     // Automatically add some basic entity information
  129.     if ( focusEntity )    {
  130.         SetInt ( "entityNumber", focusEntity->entityNumber );
  131.         SetInt ( "entityHealth", focusEntity->health );
  132.         SetString ( "entityName", focusEntity->name );
  133.         SetString ( "entityClass", focusEntity->GetClassname ( ) );
  134.     }
  135.  
  136.     // General map information    
  137.     SetString ( "mapname", gameLocal.GetMapName ( ) );
  138.     SetString ( "version", cvarSystem->GetCVarString ( "si_version" ) );
  139.     if ( gameLocal.GetLocalPlayer() ) {
  140.         SetString ( "viewpos", gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin().ToString() );
  141.     }
  142. }
  143.  
  144. /*
  145. ================
  146. rvGameDebug::EndFrame
  147. ================
  148. */
  149. void rvGameDebug::EndFrame ( void ) {
  150.     inFrame = false;
  151. }
  152.  
  153. /*
  154. ================
  155. rvGameDebug::DrawHud
  156. ================
  157. */
  158. void rvGameDebug::DrawHud ( void ) {
  159.     if ( !currentHud ) {
  160.         return;
  161.     }
  162.  
  163.     // The scratch hud displays key value pairs in a list so
  164.     // we need to push the keys into the list
  165.     if ( IsHudActive ( DBGHUD_SCRATCH ) ) {
  166.         int        index;
  167.         idDict    tempState;
  168.         
  169.         tempState.Copy ( currentHud->State() );
  170.         currentHud->ClearState ( );
  171.         
  172.         for ( index = 0; index < tempState.GetNumKeyVals(); index ++ ) {
  173.             const idKeyValue* kv;
  174.         
  175.             kv = tempState.GetKeyVal ( index );
  176.             SetString ( va("scratchKey_item_%d", index ), kv->GetKey() );
  177.             SetString ( va("scratchValue_item_%d", index ), kv->GetValue() );
  178.         }
  179.     }
  180.     else {
  181.         int index;
  182.         for ( index = 0; index < nonGameState.GetNumKeyVals(); index ++ ) {
  183.             const idKeyValue* kv;        
  184.             kv = nonGameState.GetKeyVal ( index );
  185.             currentHud->SetStateString ( kv->GetKey(), kv->GetValue() );            
  186.         }
  187.     }    
  188.     
  189.     // Activate the hud to ensure lists get updated and redraw it
  190.     currentHud->StateChanged ( gameLocal.time );
  191.     currentHud->Redraw( gameLocal.time );            
  192. }
  193.  
  194. /*
  195. ================
  196. rvGameDebug::AppendList
  197. ================
  198. */
  199. void rvGameDebug::AppendList ( const char* listname, const char* value ) {
  200.     if ( !currentHud ) {
  201.         return;
  202.     }
  203.     
  204.     int        count;
  205.     char    countName[1024];
  206.     char    itemName[1024];
  207.  
  208.     idStr::snPrintf ( countName, 1023, "%sCount", listname );
  209.     count = GetInt ( countName );
  210.  
  211.     idStr::snPrintf ( itemName, 1023, "%s_item_%d", listname, count );    
  212.     SetString ( va("%s_item_%d", listname, count ), value );
  213.     
  214.     SetInt ( countName, count + 1 );    
  215. }
  216.  
  217. /*
  218. ================
  219. rvGameDebug::Set
  220. ================
  221. */
  222. void rvGameDebug::SetInt ( const char* key, int value ) {
  223.     if ( inFrame ) {
  224.         if ( currentHud ) {
  225.             currentHud->SetStateInt( key, value );
  226.         }
  227.     } else  {
  228.         nonGameState.SetInt ( key, value );
  229.     }
  230. }
  231.  
  232. void rvGameDebug::SetFloat ( const char* key, float value ) {
  233.     if ( inFrame ) {
  234.         if ( currentHud ) {
  235.             currentHud->SetStateFloat( key, value );
  236.         }
  237.     } else {
  238.         nonGameState.SetFloat ( key, value );
  239.     }    
  240. }
  241.  
  242. void rvGameDebug::SetString ( const char* key, const char* value ) {
  243.     if ( inFrame )    {
  244.         if ( currentHud ) {
  245.             currentHud->SetStateString( key, value );
  246.         }
  247.     } else {
  248.         nonGameState.Set ( key, value );
  249.     }
  250. }
  251.  
  252. /*
  253. ================
  254. rvGameDebug::Get
  255. ================
  256. */
  257. int rvGameDebug::GetInt ( const char* key ) { 
  258.     return currentHud ? currentHud->State().GetInt ( key ) : 0;
  259. }
  260.  
  261. float rvGameDebug::GetFloat ( const char* key ) {
  262.     return currentHud ? currentHud->State().GetFloat ( key ) : 0.0f;
  263. }
  264.  
  265. const char* rvGameDebug::GetString ( const char* key ) {
  266.     return currentHud ? currentHud->State().GetString  ( key ) : "";
  267. }
  268.  
  269. /*
  270. ================
  271. rvGameDebug::SetStat
  272. ================
  273. */
  274. void rvGameDebug::SetStatInt ( const char* key, int value ) {
  275.     gameStats.SetInt ( key, value );
  276. }
  277.  
  278. void rvGameDebug::SetStatFloat ( const char* key, float value ) {
  279.     gameStats.SetFloat ( key, value );
  280. }
  281.  
  282. void rvGameDebug::SetStatString ( const char* key, const char* value ) {
  283.     gameStats.Set ( key, value );
  284. }
  285.  
  286. /*
  287. ================
  288. rvGameDebug::GetStat
  289. ================
  290. */
  291. int rvGameDebug::GetStatInt ( const char* key ) { 
  292.     return gameStats.GetInt ( key );
  293. }
  294.  
  295. float rvGameDebug::GetStatFloat ( const char* key ) {
  296.     return gameStats.GetFloat ( key );
  297. }
  298.  
  299. const char* rvGameDebug::GetStatString ( const char* key ) {
  300.     return gameStats.GetString ( key );
  301. }
  302.  
  303.  
  304. /*
  305. ================
  306. rvGameDebug::JumpAdd
  307. ================
  308. */
  309. void rvGameDebug::JumpAdd ( const char* name, const idVec3& origin, const idAngles& angles ) {
  310.     debugJumpPoint_t jump;
  311.     jump.name = name;
  312.     jump.origin = origin;
  313.     jump.angles = angles;
  314.     jumpPoints.Append ( jump );
  315. }
  316.  
  317. /*
  318. ================
  319. rvGameDebug::JumpTo
  320. ================
  321. */
  322. void rvGameDebug::JumpTo ( const char* name ) {
  323.     int index;
  324.     for ( index = 0; index < jumpPoints.Num(); index ++ ) {
  325.         if ( !jumpPoints[index].name.Icmp ( name ) ) {
  326.             JumpTo ( index );
  327.             return;
  328.         }
  329.     }
  330. }
  331.  
  332. /*
  333. ================
  334. rvGameDebug::JumpTo
  335. ================
  336. */
  337. void rvGameDebug::JumpTo ( int jumpIndex ) {
  338.     if ( jumpIndex >= jumpPoints.Num() ) { 
  339.         return;
  340.     }
  341.  
  342.     jumpIndex = jumpIndex;
  343.  
  344.     idPlayer* player = gameLocal.GetLocalPlayer();
  345.     if( player ) {
  346.         player->Teleport( jumpPoints[jumpIndex].origin, jumpPoints[jumpIndex].angles, NULL );
  347.     }    
  348. }
  349.  
  350. /*
  351. ================
  352. rvGameDebug::JumpNext
  353. ================
  354. */
  355. void rvGameDebug::JumpNext ( void ) {
  356.     if ( !jumpPoints.Num() ) {
  357.         return;
  358.     }
  359.     JumpTo ( ( jumpIndex + 1 ) % jumpPoints.Num() );
  360. }
  361.  
  362. /*
  363. ================
  364. rvGameDebug::JumpPrev
  365. ================
  366. */
  367. void rvGameDebug::JumpPrev ( void ) {
  368.     if ( !jumpPoints.Num() ) {
  369.         return;
  370.     }
  371.     JumpTo ( ( jumpIndex + jumpPoints.Num() - 1 ) % jumpPoints.Num() );
  372. }
  373.