LCOV - code coverage report
Current view: top level - source/simulation2/helpers - VertexPathfinder.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 503 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 12 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             : /**
      19             :  * @file
      20             :  * Vertex-based algorithm for CCmpPathfinder.
      21             :  * Computes paths around the corners of rectangular obstructions.
      22             :  *
      23             :  * Useful search term for this algorithm: "points of visibility".
      24             :  *
      25             :  * Since we sometimes want to use this for avoiding moving units, there is no
      26             :  * pre-computation - the whole visibility graph is effectively regenerated for
      27             :  * each path, and it does A* over that graph.
      28             :  *
      29             :  * This scales very poorly in the number of obstructions, so it should be used
      30             :  * with a limited range and not exceedingly frequently.
      31             :  */
      32             : 
      33             : #include "precompiled.h"
      34             : 
      35             : #include "VertexPathfinder.h"
      36             : 
      37             : #include "graphics/Color.h"
      38             : #include "lib/code_generation.h"
      39             : #include "lib/debug.h"
      40             : #include "maths/MathUtil.h"
      41             : #include "ps/Profiler2.h"
      42             : #include "renderer/Scene.h"
      43             : #include "simulation2/components/ICmpObstructionManager.h"
      44             : #include "simulation2/helpers/Grid.h"
      45             : #include "simulation2/helpers/PathGoal.h"
      46             : #include "simulation2/helpers/Position.h"
      47             : #include "simulation2/helpers/PriorityQueue.h"
      48             : #include "simulation2/helpers/Render.h"
      49             : 
      50             : #include <algorithm>
      51             : #include <cmath>
      52             : #include <cstddef>
      53             : #include <mutex>
      54             : #include <numbers>
      55             : #include <queue>
      56             : 
      57             : namespace
      58             : {
      59             : static std::mutex g_DebugMutex;
      60             : }
      61             : 
      62             : VertexPathfinderDebugOverlay g_VertexPathfinderDebugOverlay;
      63             : 
      64             : /* Quadrant optimisation:
      65             :  * (loosely based on GPG2 "Optimizing Points-of-Visibility Pathfinding")
      66             :  *
      67             :  * Consider the vertex ("@") at a corner of an axis-aligned rectangle ("#"):
      68             :  *
      69             :  * TL  :  TR
      70             :  *     :
      71             :  * ####@ - - -
      72             :  * #####
      73             :  * #####
      74             :  * BL ##  BR
      75             :  *
      76             :  * The area around the vertex is split into TopLeft, BottomRight etc quadrants.
      77             :  *
      78             :  * If the shortest path reaches this vertex, it cannot continue to a vertex in
      79             :  * the BL quadrant (it would be blocked by the shape).
      80             :  * Since the shortest path is wrapped tightly around the edges of obstacles,
      81             :  * if the path approached this vertex from the TL quadrant,
      82             :  * it cannot continue to the TL or TR quadrants (the path could be shorter if it
      83             :  * skipped this vertex).
      84             :  * Therefore it must continue to a vertex in the BR quadrant (so this vertex is in
      85             :  * *that* vertex's TL quadrant).
      86             :  *
      87             :  * That lets us significantly reduce the search space by quickly discarding vertexes
      88             :  * from the wrong quadrants.
      89             :  *
      90             :  * (This causes badness if the path starts from inside the shape, so we add some hacks
      91             :  * for that case.)
      92             :  *
      93             :  * (For non-axis-aligned rectangles it's harder to do this computation, so we'll
      94             :  * not bother doing any discarding for those.)
      95             :  */
      96             : static const std::uint8_t QUADRANT_NONE = 0;
      97             : static const std::uint8_t QUADRANT_BL = 1;
      98             : static const std::uint8_t QUADRANT_TR = 2;
      99             : static const std::uint8_t QUADRANT_TL = 4;
     100             : static const std::uint8_t QUADRANT_BR = 8;
     101             : static const std::uint8_t QUADRANT_BLTR = QUADRANT_BL|QUADRANT_TR;
     102             : static const std::uint8_t QUADRANT_TLBR = QUADRANT_TL|QUADRANT_BR;
     103             : static const std::uint8_t QUADRANT_ALL = QUADRANT_BLTR|QUADRANT_TLBR;
     104             : 
     105             : // When computing vertexes to insert into the search graph,
     106             : // add a small delta so that the vertexes of an edge don't get interpreted
     107             : // as crossing the edge (given minor numerical inaccuracies)
     108             : static const entity_pos_t EDGE_EXPAND_DELTA = entity_pos_t::FromInt(1)/16;
     109             : 
     110             : /**
     111             :  * Check whether a ray from 'a' to 'b' crosses any of the edges.
     112             :  * (Edges are one-sided so it's only considered a cross if going from front to back.)
     113             :  */
     114           0 : inline static bool CheckVisibility(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& abn, const std::vector<Edge>& edges)
     115             : {
     116             :     // Edges of general non-axis-aligned shapes
     117           0 :     for (size_t i = 0; i < edges.size(); ++i)
     118             :     {
     119           0 :         CFixedVector2D p0 = edges[i].p0;
     120           0 :         CFixedVector2D p1 = edges[i].p1;
     121             : 
     122           0 :         CFixedVector2D d = (p1 - p0).Perpendicular();
     123             : 
     124             :         // If 'a' is behind the edge, we can't cross
     125           0 :         fixed q = (a - p0).Dot(d);
     126           0 :         if (q < fixed::Zero())
     127           0 :             continue;
     128             : 
     129             :         // If 'b' is in front of the edge, we can't cross
     130           0 :         fixed r = (b - p0).Dot(d);
     131           0 :         if (r > fixed::Zero())
     132           0 :             continue;
     133             : 
     134             :         // The ray is crossing the infinitely-extended edge from in front to behind.
     135             :         // Check the finite edge is crossing the infinitely-extended ray too.
     136             :         // (Given the previous tests, it can only be crossing in one direction.)
     137           0 :         fixed s = (p0 - a).Dot(abn);
     138           0 :         if (s > fixed::Zero())
     139           0 :             continue;
     140             : 
     141           0 :         fixed t = (p1 - a).Dot(abn);
     142           0 :         if (t < fixed::Zero())
     143           0 :             continue;
     144             : 
     145           0 :         return false;
     146             :     }
     147             : 
     148             :     return true;
     149             : }
     150             : 
     151             : // Handle the axis-aligned shape edges separately (for performance):
     152             : // (These are specialised versions of the general unaligned edge code.
     153             : // They assume the caller has already excluded edges for which 'a' is
     154             : // on the wrong side.)
     155             : 
     156           0 : inline static bool CheckVisibilityLeft(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& abn, const std::vector<EdgeAA>& edges)
     157             : {
     158           0 :     if (a.X >= b.X)
     159             :         return true;
     160             : 
     161           0 :     for (size_t i = 0; i < edges.size(); ++i)
     162             :     {
     163           0 :         if (b.X < edges[i].p0.X)
     164           0 :             continue;
     165             : 
     166           0 :         CFixedVector2D p0 (edges[i].p0.X, edges[i].c1);
     167           0 :         fixed s = (p0 - a).Dot(abn);
     168           0 :         if (s > fixed::Zero())
     169           0 :             continue;
     170             : 
     171           0 :         CFixedVector2D p1 (edges[i].p0.X, edges[i].p0.Y);
     172           0 :         fixed t = (p1 - a).Dot(abn);
     173           0 :         if (t < fixed::Zero())
     174           0 :             continue;
     175             : 
     176           0 :         return false;
     177             :     }
     178             : 
     179             :     return true;
     180             : }
     181             : 
     182           0 : inline static bool CheckVisibilityRight(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& abn, const std::vector<EdgeAA>& edges)
     183             : {
     184           0 :     if (a.X <= b.X)
     185             :         return true;
     186             : 
     187           0 :     for (size_t i = 0; i < edges.size(); ++i)
     188             :     {
     189           0 :         if (b.X > edges[i].p0.X)
     190           0 :             continue;
     191             : 
     192           0 :         CFixedVector2D p0 (edges[i].p0.X, edges[i].c1);
     193           0 :         fixed s = (p0 - a).Dot(abn);
     194           0 :         if (s > fixed::Zero())
     195           0 :             continue;
     196             : 
     197           0 :         CFixedVector2D p1 (edges[i].p0.X, edges[i].p0.Y);
     198           0 :         fixed t = (p1 - a).Dot(abn);
     199           0 :         if (t < fixed::Zero())
     200           0 :             continue;
     201             : 
     202           0 :         return false;
     203             :     }
     204             : 
     205             :     return true;
     206             : }
     207             : 
     208           0 : inline static bool CheckVisibilityBottom(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& abn, const std::vector<EdgeAA>& edges)
     209             : {
     210           0 :     if (a.Y >= b.Y)
     211             :         return true;
     212             : 
     213           0 :     for (size_t i = 0; i < edges.size(); ++i)
     214             :     {
     215           0 :         if (b.Y < edges[i].p0.Y)
     216           0 :             continue;
     217             : 
     218           0 :         CFixedVector2D p0 (edges[i].p0.X, edges[i].p0.Y);
     219           0 :         fixed s = (p0 - a).Dot(abn);
     220           0 :         if (s > fixed::Zero())
     221           0 :             continue;
     222             : 
     223           0 :         CFixedVector2D p1 (edges[i].c1, edges[i].p0.Y);
     224           0 :         fixed t = (p1 - a).Dot(abn);
     225           0 :         if (t < fixed::Zero())
     226           0 :             continue;
     227             : 
     228           0 :         return false;
     229             :     }
     230             : 
     231             :     return true;
     232             : }
     233             : 
     234           0 : inline static bool CheckVisibilityTop(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& abn, const std::vector<EdgeAA>& edges)
     235             : {
     236           0 :     if (a.Y <= b.Y)
     237             :         return true;
     238             : 
     239           0 :     for (size_t i = 0; i < edges.size(); ++i)
     240             :     {
     241           0 :         if (b.Y > edges[i].p0.Y)
     242           0 :             continue;
     243             : 
     244           0 :         CFixedVector2D p0 (edges[i].p0.X, edges[i].p0.Y);
     245           0 :         fixed s = (p0 - a).Dot(abn);
     246           0 :         if (s > fixed::Zero())
     247           0 :             continue;
     248             : 
     249           0 :         CFixedVector2D p1 (edges[i].c1, edges[i].p0.Y);
     250           0 :         fixed t = (p1 - a).Dot(abn);
     251           0 :         if (t < fixed::Zero())
     252           0 :             continue;
     253             : 
     254           0 :         return false;
     255             :     }
     256             : 
     257             :     return true;
     258             : }
     259             : 
     260             : typedef PriorityQueueHeap<std::uint16_t, fixed, fixed> VertexPriorityQueue;
     261             : 
     262             : /**
     263             :  * Add edges and vertexes to represent the boundaries between passable and impassable
     264             :  * navcells (for impassable terrain).
     265             :  * Navcells i0 <= i <= i1, j0 <= j <= j1 will be considered.
     266             :  */
     267           0 : static void AddTerrainEdges(std::vector<Edge>& edgesAligned, std::vector<Edge>& edgesUnaligned, std::vector<Vertex>& vertexes,
     268             :     int i0, int j0, int i1, int j1,
     269             :     pass_class_t passClass, const Grid<NavcellData>& grid)
     270             : {
     271             : 
     272             :     // Clamp the coordinates so we won't attempt to sample outside of the grid.
     273             :     // (This assumes the outermost ring of navcells (which are always impassable)
     274             :     // won't have a boundary with any passable navcells. TODO: is that definitely
     275             :     // safe enough?)
     276             : 
     277           0 :     i0 = Clamp(i0, 1, grid.m_W-2);
     278           0 :     j0 = Clamp(j0, 1, grid.m_H-2);
     279           0 :     i1 = Clamp(i1, 1, grid.m_W-2);
     280           0 :     j1 = Clamp(j1, 1, grid.m_H-2);
     281             : 
     282           0 :     for (int j = j0; j <= j1; ++j)
     283             :     {
     284           0 :         for (int i = i0; i <= i1; ++i)
     285             :         {
     286           0 :             if (IS_PASSABLE(grid.get(i, j), passClass))
     287           0 :                 continue;
     288             : 
     289           0 :             if (IS_PASSABLE(grid.get(i+1, j), passClass) && IS_PASSABLE(grid.get(i, j+1), passClass) && IS_PASSABLE(grid.get(i+1, j+1), passClass))
     290             :             {
     291           0 :                 Vertex vert;
     292           0 :                 vert.status = Vertex::UNEXPLORED;
     293           0 :                 vert.quadOutward = QUADRANT_ALL;
     294           0 :                 vert.quadInward = QUADRANT_BL;
     295           0 :                 vert.p = CFixedVector2D(fixed::FromInt(i+1)+EDGE_EXPAND_DELTA, fixed::FromInt(j+1)+EDGE_EXPAND_DELTA).Multiply(Pathfinding::NAVCELL_SIZE);
     296           0 :                 vertexes.push_back(vert);
     297             :             }
     298             : 
     299           0 :             if (IS_PASSABLE(grid.get(i-1, j), passClass) && IS_PASSABLE(grid.get(i, j+1), passClass) && IS_PASSABLE(grid.get(i-1, j+1), passClass))
     300             :             {
     301           0 :                 Vertex vert;
     302           0 :                 vert.status = Vertex::UNEXPLORED;
     303           0 :                 vert.quadOutward = QUADRANT_ALL;
     304           0 :                 vert.quadInward = QUADRANT_BR;
     305           0 :                 vert.p = CFixedVector2D(fixed::FromInt(i)-EDGE_EXPAND_DELTA, fixed::FromInt(j+1)+EDGE_EXPAND_DELTA).Multiply(Pathfinding::NAVCELL_SIZE);
     306           0 :                 vertexes.push_back(vert);
     307             :             }
     308             : 
     309           0 :             if (IS_PASSABLE(grid.get(i+1, j), passClass) && IS_PASSABLE(grid.get(i, j-1), passClass) && IS_PASSABLE(grid.get(i+1, j-1), passClass))
     310             :             {
     311           0 :                 Vertex vert;
     312           0 :                 vert.status = Vertex::UNEXPLORED;
     313           0 :                 vert.quadOutward = QUADRANT_ALL;
     314           0 :                 vert.quadInward = QUADRANT_TL;
     315           0 :                 vert.p = CFixedVector2D(fixed::FromInt(i+1)+EDGE_EXPAND_DELTA, fixed::FromInt(j)-EDGE_EXPAND_DELTA).Multiply(Pathfinding::NAVCELL_SIZE);
     316           0 :                 vertexes.push_back(vert);
     317             :             }
     318             : 
     319           0 :             if (IS_PASSABLE(grid.get(i-1, j), passClass) && IS_PASSABLE(grid.get(i, j-1), passClass) && IS_PASSABLE(grid.get(i-1, j-1), passClass))
     320             :             {
     321           0 :                 Vertex vert;
     322           0 :                 vert.status = Vertex::UNEXPLORED;
     323           0 :                 vert.quadOutward = QUADRANT_ALL;
     324           0 :                 vert.quadInward = QUADRANT_TR;
     325           0 :                 vert.p = CFixedVector2D(fixed::FromInt(i)-EDGE_EXPAND_DELTA, fixed::FromInt(j)-EDGE_EXPAND_DELTA).Multiply(Pathfinding::NAVCELL_SIZE);
     326           0 :                 vertexes.push_back(vert);
     327             :             }
     328             :         }
     329             :     }
     330             : 
     331             :     // XXX rewrite this stuff
     332           0 :     std::vector<std::uint16_t> segmentsR;
     333           0 :     std::vector<std::uint16_t> segmentsL;
     334           0 :     for (int j = j0; j < j1; ++j)
     335             :     {
     336           0 :         segmentsR.clear();
     337           0 :         segmentsL.clear();
     338           0 :         for (int i = i0; i <= i1; ++i)
     339             :         {
     340           0 :             bool a = IS_PASSABLE(grid.get(i, j+1), passClass);
     341           0 :             bool b = IS_PASSABLE(grid.get(i, j), passClass);
     342           0 :             if (a && !b)
     343           0 :                 segmentsL.push_back(i);
     344           0 :             if (b && !a)
     345           0 :                 segmentsR.push_back(i);
     346             :         }
     347             : 
     348           0 :         if (!segmentsR.empty())
     349             :         {
     350           0 :             segmentsR.push_back(0); // sentinel value to simplify the loop
     351           0 :             std::uint16_t ia = segmentsR[0];
     352           0 :             std::uint16_t ib = ia + 1;
     353           0 :             for (size_t n = 1; n < segmentsR.size(); ++n)
     354             :             {
     355           0 :                 if (segmentsR[n] == ib)
     356           0 :                     ++ib;
     357             :                 else
     358             :                 {
     359           0 :                     CFixedVector2D v0 = CFixedVector2D(fixed::FromInt(ia), fixed::FromInt(j+1)).Multiply(Pathfinding::NAVCELL_SIZE);
     360           0 :                     CFixedVector2D v1 = CFixedVector2D(fixed::FromInt(ib), fixed::FromInt(j+1)).Multiply(Pathfinding::NAVCELL_SIZE);
     361             : 
     362           0 :                     if (v0.X == v1.X || v0.Y == v1.Y)
     363           0 :                         edgesAligned.emplace_back(v0, v1);
     364             :                     else
     365             :                         edgesUnaligned.emplace_back(v0, v1);
     366             : 
     367           0 :                     ia = segmentsR[n];
     368           0 :                     ib = ia + 1;
     369             :                 }
     370             :             }
     371             :         }
     372             : 
     373           0 :         if (!segmentsL.empty())
     374             :         {
     375           0 :             segmentsL.push_back(0); // sentinel value to simplify the loop
     376           0 :             std::uint16_t ia = segmentsL[0];
     377           0 :             std::uint16_t ib = ia + 1;
     378           0 :             for (size_t n = 1; n < segmentsL.size(); ++n)
     379             :             {
     380           0 :                 if (segmentsL[n] == ib)
     381           0 :                     ++ib;
     382             :                 else
     383             :                 {
     384           0 :                     CFixedVector2D v0 = CFixedVector2D(fixed::FromInt(ib), fixed::FromInt(j+1)).Multiply(Pathfinding::NAVCELL_SIZE);
     385           0 :                     CFixedVector2D v1 = CFixedVector2D(fixed::FromInt(ia), fixed::FromInt(j+1)).Multiply(Pathfinding::NAVCELL_SIZE);
     386             : 
     387           0 :                     if (v0.X == v1.X || v0.Y == v1.Y)
     388           0 :                         edgesAligned.emplace_back(v0, v1);
     389             :                     else
     390             :                         edgesUnaligned.emplace_back(v0, v1);
     391             : 
     392           0 :                     ia = segmentsL[n];
     393           0 :                     ib = ia + 1;
     394             :                 }
     395             :             }
     396             :         }
     397             :     }
     398           0 :     std::vector<std::uint16_t> segmentsU;
     399           0 :     std::vector<std::uint16_t> segmentsD;
     400           0 :     for (int i = i0; i < i1; ++i)
     401             :     {
     402           0 :         segmentsU.clear();
     403           0 :         segmentsD.clear();
     404           0 :         for (int j = j0; j <= j1; ++j)
     405             :         {
     406           0 :             bool a = IS_PASSABLE(grid.get(i+1, j), passClass);
     407           0 :             bool b = IS_PASSABLE(grid.get(i, j), passClass);
     408           0 :             if (a && !b)
     409           0 :                 segmentsU.push_back(j);
     410           0 :             if (b && !a)
     411           0 :                 segmentsD.push_back(j);
     412             :         }
     413             : 
     414           0 :         if (!segmentsU.empty())
     415             :         {
     416           0 :             segmentsU.push_back(0); // sentinel value to simplify the loop
     417           0 :             std::uint16_t ja = segmentsU[0];
     418           0 :             std::uint16_t jb = ja + 1;
     419           0 :             for (size_t n = 1; n < segmentsU.size(); ++n)
     420             :             {
     421           0 :                 if (segmentsU[n] == jb)
     422           0 :                     ++jb;
     423             :                 else
     424             :                 {
     425           0 :                     CFixedVector2D v0 = CFixedVector2D(fixed::FromInt(i+1), fixed::FromInt(ja)).Multiply(Pathfinding::NAVCELL_SIZE);
     426           0 :                     CFixedVector2D v1 = CFixedVector2D(fixed::FromInt(i+1), fixed::FromInt(jb)).Multiply(Pathfinding::NAVCELL_SIZE);
     427             : 
     428           0 :                     if (v0.X == v1.X || v0.Y == v1.Y)
     429           0 :                         edgesAligned.emplace_back(v0, v1);
     430             :                     else
     431             :                         edgesUnaligned.emplace_back(v0, v1);
     432             : 
     433           0 :                     ja = segmentsU[n];
     434           0 :                     jb = ja + 1;
     435             :                 }
     436             :             }
     437             :         }
     438             : 
     439           0 :         if (!segmentsD.empty())
     440             :         {
     441           0 :             segmentsD.push_back(0); // sentinel value to simplify the loop
     442           0 :             std::uint16_t ja = segmentsD[0];
     443           0 :             std::uint16_t jb = ja + 1;
     444           0 :             for (size_t n = 1; n < segmentsD.size(); ++n)
     445             :             {
     446           0 :                 if (segmentsD[n] == jb)
     447           0 :                     ++jb;
     448             :                 else
     449             :                 {
     450           0 :                     CFixedVector2D v0 = CFixedVector2D(fixed::FromInt(i+1), fixed::FromInt(jb)).Multiply(Pathfinding::NAVCELL_SIZE);
     451           0 :                     CFixedVector2D v1 = CFixedVector2D(fixed::FromInt(i+1), fixed::FromInt(ja)).Multiply(Pathfinding::NAVCELL_SIZE);
     452             : 
     453           0 :                     if (v0.X == v1.X || v0.Y == v1.Y)
     454           0 :                         edgesAligned.emplace_back(v0, v1);
     455             :                     else
     456             :                         edgesUnaligned.emplace_back(v0, v1);
     457             : 
     458           0 :                     ja = segmentsD[n];
     459           0 :                     jb = ja + 1;
     460             :                 }
     461             :             }
     462             :         }
     463             :     }
     464           0 : }
     465             : 
     466           0 : static void SplitAAEdges(const CFixedVector2D& a,
     467             :         const std::vector<Edge>& edgesAligned,
     468             :         const std::vector<Square>& squares,
     469             :         std::vector<EdgeAA>& edgesLeft, std::vector<EdgeAA>& edgesRight,
     470             :         std::vector<EdgeAA>& edgesBottom, std::vector<EdgeAA>& edgesTop)
     471             : {
     472           0 :     for (const Square& square : squares)
     473             :     {
     474           0 :         if (a.X <= square.p0.X)
     475           0 :             edgesLeft.emplace_back(square.p0, square.p1.Y);
     476           0 :         if (a.X >= square.p1.X)
     477           0 :             edgesRight.emplace_back(square.p1, square.p0.Y);
     478           0 :         if (a.Y <= square.p0.Y)
     479           0 :             edgesBottom.emplace_back(square.p0, square.p1.X);
     480           0 :         if (a.Y >= square.p1.Y)
     481           0 :             edgesTop.emplace_back(square.p1, square.p0.X);
     482             :     }
     483             : 
     484             :     // Process aligned edges
     485           0 :     for (const Edge& edge : edgesAligned)
     486             :     {
     487           0 :         if (edge.p0.X == edge.p1.X)
     488             :         {
     489           0 :             if (edge.p1.Y < edge.p0.Y)
     490             :             {
     491           0 :                 if (!(a.X <= edge.p0.X))
     492           0 :                     continue;
     493           0 :                 edgesLeft.emplace_back(edge.p1, edge.p0.Y);
     494             :             }
     495             :             else
     496             :             {
     497           0 :                 if (!(a.X >= edge.p0.X))
     498           0 :                     continue;
     499           0 :                 edgesRight.emplace_back(edge.p1, edge.p0.Y);
     500             :             }
     501             :         }
     502           0 :         else if (edge.p0.Y == edge.p1.Y)
     503             :         {
     504           0 :             if (edge.p0.X < edge.p1.X)
     505             :             {
     506           0 :                 if (!(a.Y <= edge.p0.Y))
     507           0 :                     continue;
     508           0 :                 edgesBottom.emplace_back(edge.p0, edge.p1.X);
     509             :             }
     510             :             else
     511             :             {
     512           0 :                 if (!(a.Y >= edge.p0.Y))
     513           0 :                     continue;
     514           0 :                 edgesTop.emplace_back(edge.p0, edge.p1.X);
     515             :             }
     516             :         }
     517             :     }
     518           0 : }
     519             : 
     520             : /**
     521             :  * Functor for sorting edge-squares by approximate proximity to a fixed point.
     522             :  */
     523             : struct SquareSort
     524             : {
     525             :     CFixedVector2D src;
     526           0 :     SquareSort(CFixedVector2D src) : src(src) { }
     527           0 :     bool operator()(const Square& a, const Square& b) const
     528             :     {
     529           0 :         return (a.p0 - src).CompareLength(b.p0 - src) < 0;
     530             :     }
     531             : };
     532             : 
     533             : /**
     534             :  * Functor for sorting unaligned edges by approximate proximity to a fixed point.
     535             :  */
     536             : struct UnalignedEdgesSort
     537             : {
     538             :     CFixedVector2D src;
     539           0 :     UnalignedEdgesSort(CFixedVector2D src) : src(src) { }
     540           0 :     bool operator()(const Edge& a, const Edge& b) const
     541             :     {
     542           0 :         return (a.p0 - src).CompareLength(b.p0 - src) < 0;
     543             :     }
     544             : };
     545             : 
     546           0 : WaypointPath VertexPathfinder::ComputeShortPath(const ShortPathRequest& request, CmpPtr<ICmpObstructionManager> cmpObstructionManager) const
     547             : {
     548           0 :     PROFILE2("ComputeShortPath");
     549             : 
     550           0 :     g_VertexPathfinderDebugOverlay.DebugRenderGoal(cmpObstructionManager->GetSimContext(), request.goal);
     551             : 
     552             :     // Create impassable edges at the max-range boundary, so we can't escape the region
     553             :     // where we're meant to be searching
     554             : 
     555           0 :     fixed rangeXMin = request.x0 - request.range;
     556           0 :     fixed rangeXMax = request.x0 + request.range;
     557           0 :     fixed rangeZMin = request.z0 - request.range;
     558           0 :     fixed rangeZMax = request.z0 + request.range;
     559             : 
     560             :     // If the goal is outside the bounds, move the center of the search-space towards it slightly,
     561             :     // as the vertex pathfinder tends to be used to get around entities in front of us
     562             :     // (this makes it possible to use smaller search ranges, but still find good paths).
     563             :     // Don't do this for the largest ranges: it makes it harder to backtrack, and large search domains
     564             :     // indicate a rather stuck unit, which means having to backtrack is probable.
     565             :     // (keep this in sync, slightly below unitMotion's max-search range).
     566             :     // (this also ensures symmetrical behaviour for goals inside/outside the max search range).
     567           0 :     CFixedVector2D toGoal = CFixedVector2D(request.goal.x, request.goal.z) - CFixedVector2D(request.x0, request.z0);
     568           0 :     if (toGoal.CompareLength(request.range) >= 0 && request.range < Pathfinding::NAVCELL_SIZE * 46)
     569             :     {
     570           0 :         fixed toGoalLength = toGoal.Length();
     571           0 :         fixed inv = fixed::FromInt(1) / toGoalLength;
     572           0 :         rangeXMin += (toGoal.Multiply(std::min(toGoalLength / 2, request.range * 3 / 5)).Multiply(inv)).X;
     573           0 :         rangeXMax += (toGoal.Multiply(std::min(toGoalLength / 2, request.range * 3 / 5)).Multiply(inv)).X;
     574           0 :         rangeZMin += (toGoal.Multiply(std::min(toGoalLength / 2, request.range * 3 / 5)).Multiply(inv)).Y;
     575           0 :         rangeZMax += (toGoal.Multiply(std::min(toGoalLength / 2, request.range * 3 / 5)).Multiply(inv)).Y;
     576             :     }
     577             : 
     578             :     // Add domain edges
     579             :     // (Inside-out square, so edges are in reverse from the usual direction.)
     580           0 :     m_EdgesAligned.emplace_back(CFixedVector2D(rangeXMin, rangeZMin), CFixedVector2D(rangeXMin, rangeZMax));
     581           0 :     m_EdgesAligned.emplace_back(CFixedVector2D(rangeXMin, rangeZMax), CFixedVector2D(rangeXMax, rangeZMax));
     582           0 :     m_EdgesAligned.emplace_back(CFixedVector2D(rangeXMax, rangeZMax), CFixedVector2D(rangeXMax, rangeZMin));
     583           0 :     m_EdgesAligned.emplace_back(CFixedVector2D(rangeXMax, rangeZMin), CFixedVector2D(rangeXMin, rangeZMin));
     584             : 
     585             : 
     586             :     // Add the start point to the graph
     587           0 :     CFixedVector2D posStart(request.x0, request.z0);
     588           0 :     fixed hStart = (posStart - request.goal.NearestPointOnGoal(posStart)).Length();
     589           0 :     Vertex start = { posStart, fixed::Zero(), hStart, 0, Vertex::OPEN, QUADRANT_NONE, QUADRANT_ALL };
     590           0 :     m_Vertexes.push_back(start);
     591           0 :     const size_t START_VERTEX_ID = 0;
     592             : 
     593             :     // Add the goal vertex to the graph.
     594             :     // Since the goal isn't always a point, this a special magic virtual vertex which moves around - whenever
     595             :     // we look at it from another vertex, it is moved to be the closest point on the goal shape to that vertex.
     596           0 :     Vertex end = { CFixedVector2D(request.goal.x, request.goal.z), fixed::Zero(), fixed::Zero(), 0, Vertex::UNEXPLORED, QUADRANT_NONE, QUADRANT_ALL };
     597           0 :     m_Vertexes.push_back(end);
     598           0 :     const size_t GOAL_VERTEX_ID = 1;
     599             : 
     600             :     // Find all the obstruction squares that might affect us
     601           0 :     std::vector<ICmpObstructionManager::ObstructionSquare> squares;
     602           0 :     size_t staticShapesNb = 0;
     603           0 :     ControlGroupMovementObstructionFilter filter(request.avoidMovingUnits, request.group);
     604           0 :     cmpObstructionManager->GetStaticObstructionsInRange(filter, rangeXMin - request.clearance, rangeZMin - request.clearance, rangeXMax + request.clearance, rangeZMax + request.clearance, squares);
     605           0 :     staticShapesNb = squares.size();
     606           0 :     cmpObstructionManager->GetUnitObstructionsInRange(filter, rangeXMin - request.clearance, rangeZMin - request.clearance, rangeXMax + request.clearance, rangeZMax + request.clearance, squares);
     607             : 
     608             :     // Change array capacities to reduce reallocations
     609           0 :     m_Vertexes.reserve(m_Vertexes.size() + squares.size()*4);
     610           0 :     m_EdgeSquares.reserve(m_EdgeSquares.size() + squares.size()); // (assume most squares are AA)
     611             : 
     612           0 :     entity_pos_t pathfindClearance = request.clearance;
     613             : 
     614             :     // Convert each obstruction square into collision edges and search graph vertexes
     615           0 :     for (size_t i = 0; i < squares.size(); ++i)
     616             :     {
     617           0 :         CFixedVector2D center(squares[i].x, squares[i].z);
     618           0 :         CFixedVector2D u = squares[i].u;
     619           0 :         CFixedVector2D v = squares[i].v;
     620             : 
     621           0 :         if (i >= staticShapesNb)
     622           0 :             pathfindClearance = request.clearance - entity_pos_t::FromInt(1)/2;
     623             : 
     624             :         // Expand the vertexes by the moving unit's collision radius, to find the
     625             :         // closest we can get to it
     626             : 
     627           0 :         CFixedVector2D hd0(squares[i].hw + pathfindClearance + EDGE_EXPAND_DELTA,   squares[i].hh + pathfindClearance + EDGE_EXPAND_DELTA);
     628           0 :         CFixedVector2D hd1(squares[i].hw + pathfindClearance + EDGE_EXPAND_DELTA, -(squares[i].hh + pathfindClearance + EDGE_EXPAND_DELTA));
     629             : 
     630             :         // Check whether this is an axis-aligned square
     631           0 :         bool aa = (u.X == fixed::FromInt(1) && u.Y == fixed::Zero() && v.X == fixed::Zero() && v.Y == fixed::FromInt(1));
     632             : 
     633           0 :         Vertex vert;
     634           0 :         vert.status = Vertex::UNEXPLORED;
     635           0 :         vert.quadInward = QUADRANT_NONE;
     636           0 :         vert.quadOutward = QUADRANT_ALL;
     637             : 
     638           0 :         vert.p.X = center.X - hd0.Dot(u);
     639           0 :         vert.p.Y = center.Y + hd0.Dot(v);
     640           0 :         if (aa)
     641             :         {
     642           0 :             vert.quadInward = QUADRANT_BR;
     643           0 :             vert.quadOutward = (~vert.quadInward) & 0xF;
     644             :         }
     645           0 :         if (vert.p.X >= rangeXMin && vert.p.Y >= rangeZMin && vert.p.X <= rangeXMax && vert.p.Y <= rangeZMax)
     646           0 :             m_Vertexes.push_back(vert);
     647             : 
     648           0 :         vert.p.X = center.X - hd1.Dot(u);
     649           0 :         vert.p.Y = center.Y + hd1.Dot(v);
     650           0 :         if (aa)
     651             :         {
     652           0 :             vert.quadInward = QUADRANT_TR;
     653           0 :             vert.quadOutward = (~vert.quadInward) & 0xF;
     654             :         }
     655           0 :         if (vert.p.X >= rangeXMin && vert.p.Y >= rangeZMin && vert.p.X <= rangeXMax && vert.p.Y <= rangeZMax)
     656           0 :             m_Vertexes.push_back(vert);
     657             : 
     658           0 :         vert.p.X = center.X + hd0.Dot(u);
     659           0 :         vert.p.Y = center.Y - hd0.Dot(v);
     660           0 :         if (aa)
     661             :         {
     662           0 :             vert.quadInward = QUADRANT_TL;
     663           0 :             vert.quadOutward = (~vert.quadInward) & 0xF;
     664             :         }
     665           0 :         if (vert.p.X >= rangeXMin && vert.p.Y >= rangeZMin && vert.p.X <= rangeXMax && vert.p.Y <= rangeZMax)
     666           0 :             m_Vertexes.push_back(vert);
     667             : 
     668           0 :         vert.p.X = center.X + hd1.Dot(u);
     669           0 :         vert.p.Y = center.Y - hd1.Dot(v);
     670           0 :         if (aa)
     671             :         {
     672           0 :             vert.quadInward = QUADRANT_BL;
     673           0 :             vert.quadOutward = (~vert.quadInward) & 0xF;
     674             :         }
     675           0 :         if (vert.p.X >= rangeXMin && vert.p.Y >= rangeZMin && vert.p.X <= rangeXMax && vert.p.Y <= rangeZMax)
     676           0 :             m_Vertexes.push_back(vert);
     677             : 
     678             :         // Add the edges:
     679             : 
     680           0 :         CFixedVector2D h0(squares[i].hw + pathfindClearance, squares[i].hh + pathfindClearance);
     681           0 :         CFixedVector2D h1(squares[i].hw + pathfindClearance, -(squares[i].hh + pathfindClearance));
     682             : 
     683           0 :         CFixedVector2D ev0(center.X - h0.Dot(u), center.Y + h0.Dot(v));
     684           0 :         CFixedVector2D ev1(center.X - h1.Dot(u), center.Y + h1.Dot(v));
     685           0 :         CFixedVector2D ev2(center.X + h0.Dot(u), center.Y - h0.Dot(v));
     686           0 :         CFixedVector2D ev3(center.X + h1.Dot(u), center.Y - h1.Dot(v));
     687           0 :         if (aa)
     688           0 :             m_EdgeSquares.emplace_back(ev1, ev3);
     689             :         else
     690             :         {
     691             :             // For non-axis-aligned edges, add them to unaligned collection
     692           0 :             m_EdgesUnaligned.emplace_back(ev0, ev1);
     693           0 :             m_EdgesUnaligned.emplace_back(ev1, ev2);
     694           0 :             m_EdgesUnaligned.emplace_back(ev2, ev3);
     695           0 :             m_EdgesUnaligned.emplace_back(ev3, ev0);
     696             :         }
     697             : 
     698             :     }
     699             : 
     700             :     // Add terrain obstructions
     701           0 :     {
     702           0 :         std::uint16_t i0, j0, i1, j1;
     703           0 :         Pathfinding::NearestNavcell(rangeXMin, rangeZMin, i0, j0, m_GridSize, m_GridSize);
     704           0 :         Pathfinding::NearestNavcell(rangeXMax, rangeZMax, i1, j1, m_GridSize, m_GridSize);
     705           0 :         AddTerrainEdges(m_EdgesAligned, m_EdgesUnaligned, m_Vertexes, i0, j0, i1, j1, request.passClass, *m_TerrainOnlyGrid);
     706             :     }
     707             : 
     708             :     // Clip out vertices that are inside an edgeSquare (i.e. trivially unreachable)
     709           0 :     for (size_t i = 2; i < m_EdgeSquares.size(); ++i)
     710             :     {
     711             :         // If the start point is inside the square, ignore it
     712           0 :         if (start.p.X >= m_EdgeSquares[i].p0.X &&
     713           0 :             start.p.Y >= m_EdgeSquares[i].p0.Y &&
     714           0 :             start.p.X <= m_EdgeSquares[i].p1.X &&
     715           0 :             start.p.Y <= m_EdgeSquares[i].p1.Y)
     716           0 :             continue;
     717             : 
     718             :         // Remove every non-start/goal vertex that is inside an edgeSquare;
     719             :         // since remove() would be inefficient, just mark it as closed instead.
     720           0 :         for (size_t j = 2; j < m_Vertexes.size(); ++j)
     721           0 :             if (m_Vertexes[j].p.X >= m_EdgeSquares[i].p0.X &&
     722           0 :                 m_Vertexes[j].p.Y >= m_EdgeSquares[i].p0.Y &&
     723           0 :                 m_Vertexes[j].p.X <= m_EdgeSquares[i].p1.X &&
     724           0 :                 m_Vertexes[j].p.Y <= m_EdgeSquares[i].p1.Y)
     725           0 :                 m_Vertexes[j].status = Vertex::CLOSED;
     726             :     }
     727             : 
     728           0 :     ENSURE(m_Vertexes.size() < 65536); // We store array indexes as std::uint16_t.
     729             : 
     730           0 :     g_VertexPathfinderDebugOverlay.DebugRenderGraph(cmpObstructionManager->GetSimContext(), m_Vertexes, m_EdgesAligned, m_EdgeSquares);
     731             : 
     732             :     // Do an A* search over the vertex/visibility graph:
     733             : 
     734             :     // Since we are just measuring Euclidean distance the heuristic is admissible,
     735             :     // so we never have to re-examine a node once it's been moved to the closed set.
     736             : 
     737             :     // Add a weight, as we vastly prefer speed to optimality.
     738             :     // In general, if we're running the vertex pathfinder,
     739             :     // it's that we're already in a suboptimal case on a meta level.
     740             :     // The path found will be at most this many times worse than the optimal path, which is likely OK.
     741           0 :     fixed heuristicWeight = fixed::FromInt(1);
     742             :     // If we have a lot of edges to check, relax the constraint more.
     743           0 :     if (m_EdgesAligned.size() + m_EdgesUnaligned.size() > 100 || m_EdgeSquares.size() > 100)
     744             :         heuristicWeight = heuristicWeight.MulDiv(fixed::FromInt(5), fixed::FromInt(3));
     745             :     else
     746             :         heuristicWeight = heuristicWeight.MulDiv(fixed::FromInt(4), fixed::FromInt(3));
     747             : 
     748             :     // To save time in common cases, we don't precompute a graph of valid edges between vertexes;
     749             :     // we do it lazily instead. When the search algorithm reaches a vertex, we examine every other
     750             :     // vertex and see if we can reach it without hitting any collision edges, and ignore the ones
     751             :     // we can't reach. Since the algorithm can only reach a vertex once (and then it'll be marked
     752             :     // as closed), we won't be doing any redundant visibility computations.
     753             : 
     754           0 :     VertexPriorityQueue open;
     755           0 :     VertexPriorityQueue::Item qiStart = { START_VERTEX_ID, start.h, start.h };
     756           0 :     open.push(qiStart);
     757             : 
     758             :     std::uint16_t idBest = START_VERTEX_ID;
     759             :     fixed hBest = start.h;
     760             : 
     761           0 :     while (!open.empty())
     762             :     {
     763             :         // Move best tile from open to closed
     764           0 :         VertexPriorityQueue::Item curr = open.pop();
     765           0 :         m_Vertexes[curr.id].status = Vertex::CLOSED;
     766             : 
     767             :         // If we've reached the destination, stop
     768           0 :         if (curr.id == GOAL_VERTEX_ID)
     769             :         {
     770           0 :             idBest = curr.id;
     771           0 :             break;
     772             :         }
     773             : 
     774             :         // Sort the edges by distance in order to check those first that have a high probability of blocking a ray.
     775             :         // The heuristic based on distance is very rough, especially for squares that are further away;
     776             :         // we're also only really interested in the closest squares since they are the only ones that block a lot of rays.
     777             :         // Thus we only do a partial sort; the threshold is just a somewhat reasonable value.
     778           0 :         if (m_EdgeSquares.size() > 8)
     779           0 :             std::partial_sort(m_EdgeSquares.begin(), m_EdgeSquares.begin() + 8, m_EdgeSquares.end(), SquareSort(m_Vertexes[curr.id].p));
     780             : 
     781           0 :         m_EdgesLeft.clear();
     782           0 :         m_EdgesRight.clear();
     783           0 :         m_EdgesBottom.clear();
     784           0 :         m_EdgesTop.clear();
     785           0 :         SplitAAEdges(m_Vertexes[curr.id].p, m_EdgesAligned, m_EdgeSquares, m_EdgesLeft, m_EdgesRight, m_EdgesBottom, m_EdgesTop);
     786             : 
     787             :         // Do the same partial sort for unaligned edges, which helps in e.g. forests.
     788             :         // (Higher values because here we need ot account for all 4 edges,
     789             :         // and checking for non-aligned visibility is very slow so it's likely worth it).
     790           0 :         if (m_EdgesUnaligned.size() > 28)
     791           0 :             std::partial_sort(m_EdgesUnaligned.begin(), m_EdgesUnaligned.begin() + 28, m_EdgesUnaligned.end(), UnalignedEdgesSort(m_Vertexes[curr.id].p));
     792             : 
     793             :         // Check the lines to every other vertex
     794           0 :         for (size_t n = 0; n < m_Vertexes.size(); ++n)
     795             :         {
     796           0 :             if (m_Vertexes[n].status == Vertex::CLOSED)
     797           0 :                 continue;
     798             : 
     799             :             // If this is the magical goal vertex, move it to near the current vertex
     800           0 :             CFixedVector2D npos;
     801           0 :             if (n == GOAL_VERTEX_ID)
     802             :             {
     803           0 :                 npos = request.goal.NearestPointOnGoal(m_Vertexes[curr.id].p);
     804             : 
     805             :                 // To prevent integer overflows later on, we need to ensure all vertexes are
     806             :                 // 'close' to the source. The goal might be far away (not a good idea but
     807             :                 // sometimes it happens), so clamp it to the current search range
     808           0 :                 npos.X = Clamp(npos.X, rangeXMin + EDGE_EXPAND_DELTA, rangeXMax - EDGE_EXPAND_DELTA);
     809           0 :                 npos.Y = Clamp(npos.Y, rangeZMin + EDGE_EXPAND_DELTA, rangeZMax - EDGE_EXPAND_DELTA);
     810             :             }
     811             :             else
     812           0 :                 npos = m_Vertexes[n].p;
     813             : 
     814             :             // Work out which quadrant(s) we're approaching the new vertex from
     815           0 :             std::uint8_t quad = 0;
     816           0 :             if (m_Vertexes[curr.id].p.X <= npos.X && m_Vertexes[curr.id].p.Y <= npos.Y) quad |= QUADRANT_BL;
     817           0 :             if (m_Vertexes[curr.id].p.X >= npos.X && m_Vertexes[curr.id].p.Y >= npos.Y) quad |= QUADRANT_TR;
     818           0 :             if (m_Vertexes[curr.id].p.X <= npos.X && m_Vertexes[curr.id].p.Y >= npos.Y) quad |= QUADRANT_TL;
     819           0 :             if (m_Vertexes[curr.id].p.X >= npos.X && m_Vertexes[curr.id].p.Y <= npos.Y) quad |= QUADRANT_BR;
     820             : 
     821             :             // Check that the new vertex is in the right quadrant for the old vertex
     822           0 :             if (!(m_Vertexes[curr.id].quadOutward & quad) && curr.id != START_VERTEX_ID)
     823             :             {
     824             :                 // Hack: Always head towards the goal if possible, to avoid missing it if it's
     825             :                 // inside another unit
     826           0 :                 if (n != GOAL_VERTEX_ID)
     827           0 :                     continue;
     828             :             }
     829             : 
     830             :             // Skip visibility checks for paths that cannot improve the known cost.
     831           0 :             if (m_Vertexes[n].status == Vertex::OPEN)
     832             :             {
     833           0 :                 if (m_Vertexes[curr.id].g >= m_Vertexes[n].g)
     834           0 :                     continue;
     835             : 
     836           0 :                 const fixed remaining{m_Vertexes[n].g - m_Vertexes[curr.id].g};
     837           0 :                 if ((m_Vertexes[curr.id].p - npos).CompareLength(remaining) >= 0)
     838           0 :                     continue;
     839             :             }
     840             : 
     841           0 :             const CFixedVector2D abn = (npos - m_Vertexes[curr.id].p).Perpendicular();
     842             : 
     843           0 :             bool visible =
     844           0 :                 CheckVisibilityLeft(m_Vertexes[curr.id].p, npos, abn, m_EdgesLeft) &&
     845           0 :                 CheckVisibilityRight(m_Vertexes[curr.id].p, npos, abn, m_EdgesRight) &&
     846           0 :                 CheckVisibilityBottom(m_Vertexes[curr.id].p, npos, abn, m_EdgesBottom) &&
     847           0 :                 CheckVisibilityTop(m_Vertexes[curr.id].p, npos, abn, m_EdgesTop) &&
     848           0 :                 CheckVisibility(m_Vertexes[curr.id].p, npos, abn, m_EdgesUnaligned);
     849             : 
     850             :             // Render the edges that we examine.
     851           0 :             g_VertexPathfinderDebugOverlay.DebugRenderEdges(cmpObstructionManager->GetSimContext(), visible, m_Vertexes[curr.id].p, npos);
     852             : 
     853           0 :             if (visible)
     854             :             {
     855           0 :                 fixed g = m_Vertexes[curr.id].g + (m_Vertexes[curr.id].p - npos).Length();
     856             : 
     857             :                 // If this is a new tile, compute the heuristic distance
     858           0 :                 if (m_Vertexes[n].status == Vertex::UNEXPLORED)
     859             :                 {
     860             :                     // Add it to the open list:
     861           0 :                     m_Vertexes[n].status = Vertex::OPEN;
     862           0 :                     m_Vertexes[n].g = g;
     863           0 :                     m_Vertexes[n].h = request.goal.DistanceToPoint(npos).Multiply(heuristicWeight);
     864           0 :                     m_Vertexes[n].pred = curr.id;
     865             : 
     866           0 :                     if (n == GOAL_VERTEX_ID)
     867           0 :                         m_Vertexes[n].p = npos; // remember the new best goal position
     868             : 
     869           0 :                     VertexPriorityQueue::Item t = { static_cast<std::uint16_t>(n),
     870           0 :                         g + m_Vertexes[n].h, m_Vertexes[n].h };
     871           0 :                     open.push(t);
     872             : 
     873             :                     // Remember the heuristically best vertex we've seen so far, in case we never actually reach the target
     874           0 :                     if (m_Vertexes[n].h < hBest)
     875             :                     {
     876           0 :                         idBest = static_cast<std::uint16_t>(n);
     877           0 :                         hBest = m_Vertexes[n].h;
     878             :                     }
     879             :                 }
     880             :                 else // must be OPEN with a better g cost as we check that above
     881             :                 {
     882             :                     // We have a better path, so replace the old one with the new cost/parent
     883           0 :                     fixed gprev = m_Vertexes[n].g;
     884           0 :                     m_Vertexes[n].g = g;
     885           0 :                     m_Vertexes[n].pred = curr.id;
     886             : 
     887           0 :                     if (n == GOAL_VERTEX_ID)
     888           0 :                         m_Vertexes[n].p = npos; // remember the new best goal position
     889             : 
     890           0 :                     open.promote(static_cast<std::uint16_t>(n), gprev + m_Vertexes[n].h,
     891           0 :                         g + m_Vertexes[n].h, m_Vertexes[n].h);
     892             :                 }
     893             :             }
     894             :         }
     895             :     }
     896             : 
     897             :     // Reconstruct the path (in reverse)
     898           0 :     WaypointPath path;
     899           0 :     for (std::uint16_t id = idBest; id != START_VERTEX_ID; id = m_Vertexes[id].pred)
     900           0 :         path.m_Waypoints.emplace_back(Waypoint{ m_Vertexes[id].p.X, m_Vertexes[id].p.Y });
     901             : 
     902           0 :     m_EdgesAligned.clear();
     903           0 :     m_EdgesUnaligned.clear();
     904           0 :     m_EdgeSquares.clear();
     905           0 :     m_Vertexes.clear();
     906             : 
     907           0 :     m_EdgesLeft.clear();
     908           0 :     m_EdgesRight.clear();
     909           0 :     m_EdgesBottom.clear();
     910           0 :     m_EdgesTop.clear();
     911             : 
     912           0 :     return path;
     913           0 : }
     914             : 
     915           0 : void VertexPathfinderDebugOverlay::DebugRenderGoal(const CSimContext& simContext, const PathGoal& goal)
     916             : {
     917           0 :     if (!m_DebugOverlay)
     918           0 :         return;
     919             : 
     920           0 :     std::lock_guard<std::mutex> lock(g_DebugMutex);
     921             : 
     922           0 :     g_VertexPathfinderDebugOverlay.m_DebugOverlayShortPathLines.clear();
     923             : 
     924             :     // Render the goal shape
     925           0 :     m_DebugOverlayShortPathLines.push_back(SOverlayLine());
     926           0 :     m_DebugOverlayShortPathLines.back().m_Color = CColor(1, 0, 0, 1);
     927           0 :     switch (goal.type)
     928             :     {
     929           0 :         case PathGoal::POINT:
     930           0 :         {
     931           0 :             SimRender::ConstructCircleOnGround(simContext, goal.x.ToFloat(), goal.z.ToFloat(), 0.2f, m_DebugOverlayShortPathLines.back(), true);
     932             :             break;
     933             :         }
     934           0 :         case PathGoal::CIRCLE:
     935           0 :         case PathGoal::INVERTED_CIRCLE:
     936           0 :         {
     937           0 :             SimRender::ConstructCircleOnGround(simContext, goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat(), m_DebugOverlayShortPathLines.back(), true);
     938             :             break;
     939             :         }
     940           0 :         case PathGoal::SQUARE:
     941           0 :         case PathGoal::INVERTED_SQUARE:
     942           0 :         {
     943           0 :             float a = atan2f(goal.v.X.ToFloat(), goal.v.Y.ToFloat());
     944           0 :             SimRender::ConstructSquareOnGround(simContext, goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat()*2, goal.hh.ToFloat()*2, a, m_DebugOverlayShortPathLines.back(), true);
     945             :             break;
     946             :         }
     947             :     }
     948           0 : }
     949             : 
     950           0 : void VertexPathfinderDebugOverlay::DebugRenderGraph(const CSimContext& simContext, const std::vector<Vertex>& vertexes, const std::vector<Edge>& edges, const std::vector<Square>& edgeSquares)
     951             : {
     952           0 :     if (!m_DebugOverlay)
     953           0 :         return;
     954             : 
     955           0 :     std::lock_guard<std::mutex> lock(g_DebugMutex);
     956             : 
     957             : #define PUSH_POINT(p) STMT(xz.push_back(p.X.ToFloat()); xz.push_back(p.Y.ToFloat()))
     958             :     // Render the vertexes as little Pac-Man shapes to indicate quadrant direction
     959           0 :     for (size_t i = 0; i < vertexes.size(); ++i)
     960             :     {
     961           0 :         m_DebugOverlayShortPathLines.emplace_back();
     962           0 :         m_DebugOverlayShortPathLines.back().m_Color = CColor(1, 1, 0, 1);
     963             : 
     964           0 :         float x = vertexes[i].p.X.ToFloat();
     965           0 :         float z = vertexes[i].p.Y.ToFloat();
     966             : 
     967           0 :         float a0 = 0, a1 = 0;
     968             :         // Get arc start/end angles depending on quadrant (if any)
     969           0 :         if      (vertexes[i].quadInward == QUADRANT_BL) { a0 = -0.25f; a1 = 0.50f; }
     970           0 :         else if (vertexes[i].quadInward == QUADRANT_TR) { a0 =  0.25f; a1 = 1.00f; }
     971           0 :         else if (vertexes[i].quadInward == QUADRANT_TL) { a0 = -0.50f; a1 = 0.25f; }
     972           0 :         else if (vertexes[i].quadInward == QUADRANT_BR) { a0 =  0.00f; a1 = 0.75f; }
     973             : 
     974           0 :         if (a0 == a1)
     975           0 :             SimRender::ConstructCircleOnGround(simContext, x, z, 0.5f,
     976           0 :                                                m_DebugOverlayShortPathLines.back(), true);
     977             :         else
     978           0 :             SimRender::ConstructClosedArcOnGround(simContext, x, z, 0.5f,
     979             :                                                   a0 * (2.0f * std::numbers::pi_v<float>),
     980             :                                                   a1 * (2.0f * std::numbers::pi_v<float>),
     981           0 :                                                   m_DebugOverlayShortPathLines.back(), true);
     982             :     }
     983             : 
     984             :     // Render the edges
     985           0 :     for (size_t i = 0; i < edges.size(); ++i)
     986             :     {
     987           0 :         m_DebugOverlayShortPathLines.emplace_back();
     988           0 :         m_DebugOverlayShortPathLines.back().m_Color = CColor(0, 1, 1, 1);
     989           0 :         std::vector<float> xz;
     990           0 :         PUSH_POINT(edges[i].p0);
     991           0 :         PUSH_POINT(edges[i].p1);
     992             : 
     993             :         // Add an arrowhead to indicate the direction
     994           0 :         CFixedVector2D d = edges[i].p1 - edges[i].p0;
     995           0 :         d.Normalize(fixed::FromInt(1)/8);
     996           0 :         CFixedVector2D p2 = edges[i].p1 - d*2;
     997           0 :         CFixedVector2D p3 = p2 + d.Perpendicular();
     998           0 :         CFixedVector2D p4 = p2 - d.Perpendicular();
     999           0 :         PUSH_POINT(p3);
    1000           0 :         PUSH_POINT(p4);
    1001           0 :         PUSH_POINT(edges[i].p1);
    1002             : 
    1003           0 :         SimRender::ConstructLineOnGround(simContext, xz, m_DebugOverlayShortPathLines.back(), true);
    1004           0 :     }
    1005             : #undef PUSH_POINT
    1006             : 
    1007             :     // Render the axis-aligned squares
    1008           0 :     for (size_t i = 0; i < edgeSquares.size(); ++i)
    1009             :     {
    1010           0 :         m_DebugOverlayShortPathLines.push_back(SOverlayLine());
    1011           0 :         m_DebugOverlayShortPathLines.back().m_Color = CColor(0, 1, 1, 1);
    1012           0 :         std::vector<float> xz;
    1013           0 :         Square s = edgeSquares[i];
    1014           0 :         xz.push_back(s.p0.X.ToFloat());
    1015           0 :         xz.push_back(s.p0.Y.ToFloat());
    1016           0 :         xz.push_back(s.p0.X.ToFloat());
    1017           0 :         xz.push_back(s.p1.Y.ToFloat());
    1018           0 :         xz.push_back(s.p1.X.ToFloat());
    1019           0 :         xz.push_back(s.p1.Y.ToFloat());
    1020           0 :         xz.push_back(s.p1.X.ToFloat());
    1021           0 :         xz.push_back(s.p0.Y.ToFloat());
    1022           0 :         xz.push_back(s.p0.X.ToFloat());
    1023           0 :         xz.push_back(s.p0.Y.ToFloat());
    1024           0 :         SimRender::ConstructLineOnGround(simContext, xz, m_DebugOverlayShortPathLines.back(), true);
    1025           0 :     }
    1026           0 : }
    1027             : 
    1028           0 : void VertexPathfinderDebugOverlay::DebugRenderEdges(const CSimContext&, bool /*visible*/,
    1029             :     CFixedVector2D /*curr*/, CFixedVector2D /*npos*/)
    1030             : {
    1031           0 :     if (!m_DebugOverlay)
    1032           0 :         return;
    1033             : 
    1034           0 :     std::lock_guard<std::mutex> lock(g_DebugMutex);
    1035             : 
    1036             :     // Disabled by default.
    1037             :     /*
    1038             :     m_DebugOverlayShortPathLines.push_back(SOverlayLine());
    1039             :     m_DebugOverlayShortPathLines.back().m_Color = visible ? CColor(0, 1, 0, 0.5) : CColor(1, 0, 0, 0.5);
    1040             :     m_DebugOverlayShortPathLines.push_back(SOverlayLine());
    1041             :     m_DebugOverlayShortPathLines.back().m_Color = visible ? CColor(0, 1, 0, 0.5) : CColor(1, 0, 0, 0.5);
    1042             :     std::vector<float> xz;
    1043             :     xz.push_back(curr.X.ToFloat());
    1044             :     xz.push_back(curr.Y.ToFloat());
    1045             :     xz.push_back(npos.X.ToFloat());
    1046             :     xz.push_back(npos.Y.ToFloat());
    1047             :     SimRender::ConstructLineOnGround(simContext, xz, m_DebugOverlayShortPathLines.back(), false);
    1048             :     SimRender::ConstructLineOnGround(simContext, xz, m_DebugOverlayShortPathLines.back(), false);
    1049             :     */
    1050           0 : }
    1051             : 
    1052           0 : void VertexPathfinderDebugOverlay::RenderSubmit(SceneCollector& collector)
    1053             : {
    1054           0 :     if (!m_DebugOverlay)
    1055           0 :         return;
    1056             : 
    1057           0 :     std::lock_guard<std::mutex> lock(g_DebugMutex);
    1058           0 :     m_DebugOverlayShortPathLines.swap(m_DebugOverlayShortPathLinesSubmitted);
    1059           0 :     for (size_t i = 0; i < m_DebugOverlayShortPathLinesSubmitted.size(); ++i)
    1060           0 :         collector.Submit(&m_DebugOverlayShortPathLinesSubmitted[i]);
    1061           0 : }

Generated by: LCOV version 1.16