Team Fortress 2 Source Code as on 22/4/2020
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.

269 lines
8.0 KiB

  1. //========= Copyright 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. #ifdef 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. CBasePlayer* pPlayer = UTIL_PlayerByIndex(CBaseEntity::m_nDebugPlayer);
  127. if (!pPlayer)
  128. {
  129. return;
  130. }
  131. Vector pRight;
  132. pPlayer->EyeVectors( NULL, &pRight, NULL );
  133. #ifdef _WIN32
  134. Vector topPos = NWCEdit::AirNodePlacementPosition();
  135. #else
  136. Vector pForward;
  137. pPlayer->EyeVectors( &pForward );
  138. Vector floorVec = pForward;
  139. floorVec.z = 0;
  140. VectorNormalize( floorVec );
  141. VectorNormalize( pForward );
  142. float cosAngle = DotProduct(floorVec,pForward);
  143. float lookDist = g_pAINetworkManager->GetEditOps()->m_flAirEditDistance/cosAngle;
  144. Vector topPos = pPlayer->EyePosition()+pForward * lookDist;
  145. #endif
  146. Vector bottomPos = topPos;
  147. bottomPos.z = GetLongFloorZ(bottomPos);
  148. // Make sure we can see the target pos
  149. trace_t tr;
  150. Vector endPos;
  151. UTIL_TraceLine(pPlayer->EyePosition(), topPos, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  152. if (tr.fraction == 1.0)
  153. {
  154. Vector rightTrace = topPos + pRight*400;
  155. float traceLen = (topPos - rightTrace).Length();
  156. UTIL_TraceLine(topPos, rightTrace, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  157. endPos = topPos+(pRight*traceLen*tr.fraction);
  158. NDebugOverlay::DrawTickMarkedLine(topPos, endPos, 24.0, 5, 255,0,0,false,0);
  159. Vector leftTrace = topPos - pRight*400;
  160. traceLen = (topPos - leftTrace).Length();
  161. UTIL_TraceLine(topPos, leftTrace, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  162. endPos = topPos-(pRight*traceLen*tr.fraction);
  163. NDebugOverlay::DrawTickMarkedLine(topPos, endPos, 24.0, 5, 255,0,0,false,0);
  164. Vector upTrace = topPos + Vector(0,0,1)*400;
  165. traceLen = (topPos - upTrace).Length();
  166. UTIL_TraceLine(topPos, upTrace, MASK_NPCSOLID_BRUSHONLY, pPlayer, COLLISION_GROUP_NONE, &tr);
  167. endPos = topPos+(Vector(0,0,1)*traceLen*tr.fraction);
  168. NDebugOverlay::DrawTickMarkedLine(bottomPos, endPos, 24.0, 5, 255,0,0,false,0);
  169. // Draw white cross at center
  170. NDebugOverlay::Cross3D(topPos, Vector(-2,-2,-2), Vector(2,2,2), 255,255,255, true, 0);
  171. }
  172. else
  173. {
  174. // Draw warning cross at center
  175. NDebugOverlay::Cross3D(topPos, Vector(-2,-2,-2), Vector(2,2,2), 255,100,100, true, 0 );
  176. }
  177. /* Don't show distance for now.
  178. char text[25];
  179. Q_snprintf(text,sizeof(text),"%3.0f",flCrossDistance/12.0);
  180. Vector textPos = topPos - pRight*16 + Vector(0,0,10);
  181. NDebugOverlay::Text( textPos, text, true, 0 );
  182. */
  183. }
  184. //------------------------------------------------------------------------------
  185. // Purpose : Draw all overlay lines in the list
  186. //------------------------------------------------------------------------------
  187. void UTIL_DrawOverlayLines(void)
  188. {
  189. if (m_nDebugOverlayIndex != -1)
  190. {
  191. for (int i=0;i<NUM_DEBUG_OVERLAY_LINES;i++)
  192. {
  193. if (m_debugOverlayLine[i]->draw)
  194. {
  195. NDebugOverlay::Line(m_debugOverlayLine[i]->origin,
  196. m_debugOverlayLine[i]->dest,
  197. m_debugOverlayLine[i]->r,
  198. m_debugOverlayLine[i]->g,
  199. m_debugOverlayLine[i]->b,
  200. m_debugOverlayLine[i]->noDepthTest,0);
  201. }
  202. }
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose: Add an overlay line with padding on the start and end
  207. //-----------------------------------------------------------------------------
  208. void DebugDrawLine( const Vector& vecAbsStart, const Vector& vecAbsEnd, int r, int g, int b, bool test, float duration )
  209. {
  210. NDebugOverlay::Line( vecAbsStart + Vector( 0,0,0.1), vecAbsEnd + Vector( 0,0,0.1), r,g,b, test, duration );
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose: Allow all debug overlays to be cleared at once
  214. //-----------------------------------------------------------------------------
  215. CON_COMMAND( clear_debug_overlays, "clears debug overlays" )
  216. {
  217. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  218. return;
  219. CBaseEntity *pEntity = gEntList.FirstEnt();
  220. // Clear all entities of their debug overlays
  221. while ( pEntity )
  222. {
  223. pEntity->m_debugOverlays = 0;
  224. // UNDONE: Clear out / expire timed overlays?
  225. pEntity = gEntList.NextEnt( pEntity );
  226. }
  227. // Clear all engine overlays
  228. if ( debugoverlay )
  229. {
  230. debugoverlay->ClearAllOverlays();
  231. }
  232. }