LCOV - code coverage report
Current view: top level - source/simulation2/helpers - VertexPathfinder.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 5 9 55.6 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_VERTEXPATHFINDER
      19             : #define INCLUDED_VERTEXPATHFINDER
      20             : 
      21             : #include "graphics/Overlay.h"
      22             : #include "maths/Fixed.h"
      23             : #include "maths/FixedVector2D.h"
      24             : #include "simulation2/helpers/Pathfinding.h"
      25             : #include "simulation2/system/Component.h"
      26             : 
      27             : #include <atomic>
      28             : #include <vector>
      29             : 
      30             : class ICmpObstructionManager;
      31             : class PathGoal;
      32             : class SceneCollector;
      33             : template <typename T> class Grid;
      34             : 
      35             : // A vertex around the corners of an obstruction
      36             : // (paths will be sequences of these vertexes)
      37           0 : struct Vertex
      38             : {
      39             :     enum
      40             :     {
      41             :         UNEXPLORED,
      42             :         OPEN,
      43             :         CLOSED,
      44             :     };
      45             : 
      46             :     CFixedVector2D p;
      47             :     fixed g, h;
      48             :     std::uint16_t pred = 0;
      49             :     std::uint8_t status;
      50             :     std::uint8_t quadInward : 4; // the quadrant which is inside the shape (or NONE)
      51             :     std::uint8_t quadOutward : 4; // the quadrants of the next point on the path which this vertex must be in, given 'pred'
      52             : };
      53             : 
      54             : // Obstruction edges (paths will not cross any of these).
      55             : // Defines the two points of the edge.
      56             : struct Edge
      57             : {
      58             :     CFixedVector2D p0, p1;
      59             : 
      60             :     Edge() = default;
      61           0 :     Edge(const CFixedVector2D& p0, const CFixedVector2D& p1) : p0(p0), p1(p1) {}
      62             : };
      63             : 
      64             : // Axis-aligned obstruction squares (paths will not cross any of these).
      65             : // Defines the opposing corners of an axis-aligned square
      66             : // (from which four individual edges can be trivially computed), requiring p0 <= p1
      67             : struct Square
      68             : {
      69             :     CFixedVector2D p0, p1;
      70             : 
      71             :     Square() = default;
      72           0 :     Square(const CFixedVector2D& p0, const CFixedVector2D& p1) : p0(p0), p1(p1) {}
      73             : };
      74             : 
      75             : // Axis-aligned obstruction edges.
      76             : // p0 defines one end; c1 is either the X or Y coordinate of the other end,
      77             : // depending on the context in which this is used.
      78             : struct EdgeAA
      79             : {
      80             :     CFixedVector2D p0;
      81             :     fixed c1;
      82             : 
      83             :     EdgeAA() = default;
      84           0 :     EdgeAA(const CFixedVector2D& p0, const fixed& c1) : p0(p0), c1(c1) {}
      85             : };
      86             : 
      87             : class VertexPathfinder
      88             : {
      89             : public:
      90          28 :     VertexPathfinder(const std::uint16_t& gridSize, Grid<NavcellData>* const & terrainOnlyGrid) :
      91          28 :         m_GridSize(gridSize),
      92          28 :         m_TerrainOnlyGrid(terrainOnlyGrid)
      93             :     {}
      94             :     VertexPathfinder(const VertexPathfinder&) = delete;
      95          21 :     VertexPathfinder(VertexPathfinder&& o) : m_GridSize(o.m_GridSize), m_TerrainOnlyGrid(o.m_TerrainOnlyGrid) {}
      96             : 
      97             :     /**
      98             :      * Compute a precise path from the given point to the goal, and return the set of waypoints.
      99             :      * The path is based on the full set of obstructions that pass the filter, such that
     100             :      * a unit of clearance 'clearance' will be able to follow the path with no collisions.
     101             :      * The path is restricted to a box of radius 'range' from the starting point.
     102             :      * Defined in CCmpPathfinder_Vertex.cpp
     103             :      */
     104             :     WaypointPath ComputeShortPath(const ShortPathRequest& request, CmpPtr<ICmpObstructionManager> cmpObstructionManager) const;
     105             : 
     106             : private:
     107             : 
     108             :     // References to the Pathfinder for convenience.
     109             :     const std::uint16_t& m_GridSize;
     110             :     Grid<NavcellData>* const & m_TerrainOnlyGrid;
     111             : 
     112             :     // These vectors are expensive to recreate on every call, so we cache them here.
     113             :     // They are made mutable to allow using them in the otherwise const ComputeShortPath.
     114             : 
     115             :     mutable std::vector<EdgeAA> m_EdgesLeft;
     116             :     mutable std::vector<EdgeAA> m_EdgesRight;
     117             :     mutable std::vector<EdgeAA> m_EdgesBottom;
     118             :     mutable std::vector<EdgeAA> m_EdgesTop;
     119             : 
     120             :     // List of obstruction vertexes (plus start/end points); we'll try to find paths through
     121             :     // the graph defined by these vertexes.
     122             :     mutable std::vector<Vertex> m_Vertexes;
     123             :     // List of collision edges - paths must never cross these.
     124             :     // (Edges are one-sided so intersections are fine in one direction, but not the other direction.)
     125             :     mutable std::vector<Edge> m_EdgesAligned;
     126             :     mutable std::vector<Edge> m_EdgesUnaligned;
     127             :     mutable std::vector<Square> m_EdgeSquares; // Axis-aligned squares; equivalent to 4 edges.
     128             : };
     129             : 
     130             : /**
     131             :  * There are several vertex pathfinders running asynchronously, so their debug output
     132             :  * might conflict. To remain thread-safe, this single class will handle the debug data.
     133             :  * NB: though threadsafe, the way things are setup means you can have a few
     134             :  * more graphs and edges than you'd expect showing up in the rendered graph.
     135             :  */
     136             : class VertexPathfinderDebugOverlay
     137             : {
     138             :     friend class VertexPathfinder;
     139             : public:
     140           7 :     void SetDebugOverlay(bool enabled) { m_DebugOverlay = enabled; }
     141             :     void RenderSubmit(SceneCollector& collector);
     142             : protected:
     143             : 
     144             :     void DebugRenderGoal(const CSimContext& simContext, const PathGoal& goal);
     145             :     void DebugRenderGraph(const CSimContext& simContext, const std::vector<Vertex>& vertexes, const std::vector<Edge>& edges, const std::vector<Square>& edgeSquares);
     146             :     void DebugRenderEdges(const CSimContext& simContext, bool visible, CFixedVector2D curr, CFixedVector2D npos);
     147             : 
     148             :     std::atomic<bool> m_DebugOverlay = false;
     149             :     // The data is double buffered: the first is the 'work-in-progress' state, the second the last RenderSubmit state.
     150             :     std::vector<SOverlayLine> m_DebugOverlayShortPathLines;
     151             :     std::vector<SOverlayLine> m_DebugOverlayShortPathLinesSubmitted;
     152             : };
     153             : 
     154             : extern VertexPathfinderDebugOverlay g_VertexPathfinderDebugOverlay;
     155             : 
     156             : #endif // INCLUDED_VERTEXPATHFINDER

Generated by: LCOV version 1.16