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.

271 lines
8.1 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Namespace for functions dealing with Debug Overlays
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "cbase.h"
  10. #include "mathlib/mathlib.h"
  11. #include "player.h"
  12. #include "ndebugoverlay.h"
  13. #include "wcedit.h"
  14. #if defined( DEDICATED ) || defined( _PS3 ) || defined( POSIX )
  15. #include "ai_basenpc.h"
  16. #include "ai_network.h"
  17. #include "ai_networkmanager.h"
  18. #endif
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. #define NUM_DEBUG_OVERLAY_LINES 20
  22. int m_nDebugOverlayIndex = -1;
  23. OverlayLine_t* m_debugOverlayLine[NUM_DEBUG_OVERLAY_LINES];
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Return the old overlay line from the overlay pool
  26. //-----------------------------------------------------------------------------
  27. OverlayLine_t* GetDebugOverlayLine(void)
  28. {
  29. // Make casing pool if not initialized
  30. if (m_nDebugOverlayIndex == -1)
  31. {
  32. for (int i=0;i<NUM_DEBUG_OVERLAY_LINES;i++)
  33. {
  34. m_debugOverlayLine[i] = new OverlayLine_t;
  35. m_debugOverlayLine[i]->noDepthTest = true;
  36. m_debugOverlayLine[i]->draw = false;
  37. }
  38. m_nDebugOverlayIndex = 0;
  39. }
  40. int id;
  41. m_nDebugOverlayIndex++;
  42. if (m_nDebugOverlayIndex == NUM_DEBUG_OVERLAY_LINES)
  43. {
  44. m_nDebugOverlayIndex = 0;
  45. id = NUM_DEBUG_OVERLAY_LINES-1;
  46. }
  47. else
  48. {
  49. id = m_nDebugOverlayIndex-1;
  50. }
  51. return m_debugOverlayLine[id];
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Adds a debug line to be drawn on the screen
  55. // Input : If testLOS is true, color is based on line of sight test
  56. // Output :
  57. //-----------------------------------------------------------------------------
  58. void UTIL_AddDebugLine(const Vector &startPos, const Vector &endPos, bool noDepthTest, bool testLOS)
  59. {
  60. OverlayLine_t* debugLine = GetDebugOverlayLine();
  61. debugLine->origin = startPos;
  62. debugLine->dest = endPos;
  63. debugLine->noDepthTest = noDepthTest;
  64. debugLine->draw = true;
  65. if (testLOS)
  66. {
  67. trace_t tr;
  68. UTIL_TraceLine ( debugLine->origin, debugLine->dest, MASK_BLOCKLOS, NULL, COLLISION_GROUP_NONE, &tr );
  69. if (tr.startsolid || tr.fraction < 1.0)
  70. {
  71. debugLine->r = 255;
  72. debugLine->g = 0;
  73. debugLine->b = 0;
  74. return;
  75. }
  76. }
  77. debugLine->r = 255;
  78. debugLine->g = 255;
  79. debugLine->b = 255;
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose: Returns z value of floor below given point (up to 384 inches below)
  83. //-----------------------------------------------------------------------------
  84. float GetLongFloorZ(const Vector &origin)
  85. {
  86. // trace to the ground, then pop up 8 units and place node there to make it
  87. // easier for them to connect (think stairs, chairs, and bumps in the floor).
  88. // After the routing is done, push them back down.
  89. //
  90. trace_t tr;
  91. UTIL_TraceLine ( origin,
  92. origin - Vector ( 0, 0, 2048 ),
  93. MASK_NPCSOLID_BRUSHONLY,
  94. NULL,
  95. COLLISION_GROUP_NONE,
  96. &tr );
  97. // This trace is ONLY used if we hit an entity flagged with FL_WORLDBRUSH
  98. trace_t trEnt;
  99. UTIL_TraceLine ( origin,
  100. origin - Vector ( 0, 0, 2048 ),
  101. MASK_NPCSOLID,
  102. NULL,
  103. COLLISION_GROUP_NONE,
  104. &trEnt );
  105. // Did we hit something closer than the floor?
  106. if ( trEnt.fraction < tr.fraction )
  107. {
  108. // If it was a world brush entity, copy the node location
  109. if ( trEnt.m_pEnt )
  110. {
  111. CBaseEntity *e = trEnt.m_pEnt;
  112. if ( e && (e->GetFlags() & FL_WORLDBRUSH) )
  113. {
  114. tr.endpos = trEnt.endpos;
  115. }
  116. }
  117. }
  118. return tr.endpos.z;
  119. }
  120. //------------------------------------------------------------------------------
  121. // Purpose : Draws a large crosshair at flCrossDistance from the debug player
  122. // with tick marks
  123. //------------------------------------------------------------------------------
  124. void UTIL_DrawPositioningOverlay( float flCrossDistance )
  125. {
  126. #ifndef SWDS
  127. CBasePlayer* pPlayer = UTIL_PlayerByIndex(CBaseEntity::m_nDebugPlayer);
  128. if (!pPlayer)
  129. {
  130. return;
  131. }
  132. Vector pRight;
  133. pPlayer->EyeVectors( NULL, &pRight, NULL );
  134. #ifdef _WIN32
  135. Vector topPos = NWCEdit::AirNodePlacementPosition();
  136. #else
  137. Vector pForward;
  138. pPlayer->EyeVectors( &pForward );
  139. Vector floorVec = pForward;
  140. floorVec.z = 0;
  141. VectorNormalize( floorVec );
  142. VectorNormalize( pForward );
  143. float cosAngle = DotProduct(floorVec,pForward);
  144. float lookDist = g_pAINetworkManager->GetEditOps()->m_flAirEditDistance/cosAngle;
  145. Vector topPos = pPlayer->EyePosition()+pForward * lookDist;
  146. #endif
  147. Vector bottomPos = topPos;
  148. bottomPos.z = GetLongFloorZ(bottomPos);
  149. // Make sure we can see the target pos
  150. trace_t tr;
  151. Vector endPos;
  152. UTIL_TraceLine(pPlayer->EyePosition(), topPos, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  153. if (tr.fraction == 1.0)
  154. {
  155. Vector rightTrace = topPos + pRight*400;
  156. float traceLen = (topPos - rightTrace).Length();
  157. UTIL_TraceLine(topPos, rightTrace, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  158. endPos = topPos+(pRight*traceLen*tr.fraction);
  159. NDebugOverlay::DrawTickMarkedLine(topPos, endPos, 24.0, 5, 255,0,0,false,0);
  160. Vector leftTrace = topPos - pRight*400;
  161. traceLen = (topPos - leftTrace).Length();
  162. UTIL_TraceLine(topPos, leftTrace, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  163. endPos = topPos-(pRight*traceLen*tr.fraction);
  164. NDebugOverlay::DrawTickMarkedLine(topPos, endPos, 24.0, 5, 255,0,0,false,0);
  165. Vector upTrace = topPos + Vector(0,0,1)*400;
  166. traceLen = (topPos - upTrace).Length();
  167. UTIL_TraceLine(topPos, upTrace, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  168. endPos = topPos+(Vector(0,0,1)*traceLen*tr.fraction);
  169. NDebugOverlay::DrawTickMarkedLine(bottomPos, endPos, 24.0, 5, 255,0,0,false,0);
  170. // Draw white cross at center
  171. NDebugOverlay::Cross3D(topPos, Vector(-2,-2,-2), Vector(2,2,2), 255,255,255, true, 0);
  172. }
  173. else
  174. {
  175. // Draw warning cross at center
  176. NDebugOverlay::Cross3D(topPos, Vector(-2,-2,-2), Vector(2,2,2), 255,100,100, true, 0 );
  177. }
  178. /* Don't show distance for now.
  179. char text[25];
  180. Q_snprintf(text,sizeof(text),"%3.0f",flCrossDistance/12.0);
  181. Vector textPos = topPos - pRight*16 + Vector(0,0,10);
  182. NDebugOverlay::Text( textPos, text, true, 0 );
  183. */
  184. #endif
  185. }
  186. //------------------------------------------------------------------------------
  187. // Purpose : Draw all overlay lines in the list
  188. //------------------------------------------------------------------------------
  189. void UTIL_DrawOverlayLines(void)
  190. {
  191. if (m_nDebugOverlayIndex != -1)
  192. {
  193. for (int i=0;i<NUM_DEBUG_OVERLAY_LINES;i++)
  194. {
  195. if (m_debugOverlayLine[i]->draw)
  196. {
  197. NDebugOverlay::Line(m_debugOverlayLine[i]->origin,
  198. m_debugOverlayLine[i]->dest,
  199. m_debugOverlayLine[i]->r,
  200. m_debugOverlayLine[i]->g,
  201. m_debugOverlayLine[i]->b,
  202. m_debugOverlayLine[i]->noDepthTest,0);
  203. }
  204. }
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose: Add an overlay line with padding on the start and end
  209. //-----------------------------------------------------------------------------
  210. void DebugDrawLine( const Vector& vecAbsStart, const Vector& vecAbsEnd, int r, int g, int b, bool test, float duration )
  211. {
  212. NDebugOverlay::Line( vecAbsStart + Vector( 0,0,0.1), vecAbsEnd + Vector( 0,0,0.1), r,g,b, test, duration );
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Purpose: Allow all debug overlays to be cleared at once
  216. //-----------------------------------------------------------------------------
  217. CON_COMMAND( clear_debug_overlays, "clears debug overlays" )
  218. {
  219. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  220. return;
  221. CBaseEntity *pEntity = gEntList.FirstEnt();
  222. // Clear all entities of their debug overlays
  223. while ( pEntity )
  224. {
  225. pEntity->m_debugOverlays = 0;
  226. // UNDONE: Clear out / expire timed overlays?
  227. pEntity = gEntList.NextEnt( pEntity );
  228. }
  229. // Clear all engine overlays
  230. if ( debugoverlay )
  231. {
  232. debugoverlay->ClearAllOverlays();
  233. }
  234. }