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 : #include "precompiled.h" 19 : 20 : #include "Pathfinding.h" 21 : 22 : #include "lib/debug.h" 23 : #include "maths/FixedVector2D.h" 24 : #include "ps/CLogger.h" 25 : #include "simulation2/helpers/Grid.h" 26 : #include "simulation2/system/Component.h" 27 : 28 : #include <string> 29 : 30 : namespace Pathfinding 31 : { 32 11379 : bool CheckLineMovement(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, 33 : pass_class_t passClass, const Grid<NavcellData>& grid) 34 : { 35 : // We shouldn't allow lines between diagonally-adjacent navcells. 36 : // It doesn't matter whether we allow lines precisely along the edge 37 : // of an impassable navcell. 38 : 39 : // To rasterise the line: 40 : // If the line is (e.g.) aiming up-right, then we start at the navcell 41 : // containing the start point and the line must either end in that navcell 42 : // or else exit along the top edge or the right edge (or through the top-right corner, 43 : // which we'll arbitrary treat as the horizontal edge). 44 : // So we jump into the adjacent navcell across that edge, and continue. 45 : 46 : // To handle the special case of units that are stuck on impassable cells, 47 : // we allow them to move from an impassable to a passable cell (but not 48 : // vice versa). 49 : 50 11379 : std::uint16_t i0, j0, i1, j1; 51 11379 : NearestNavcell(x0, z0, i0, j0, grid.m_W, grid.m_H); 52 11379 : NearestNavcell(x1, z1, i1, j1, grid.m_W, grid.m_H); 53 : 54 : // Find which direction the line heads in 55 11379 : int di = (i0 < i1 ? +1 : i1 < i0 ? -1 : 0); 56 11379 : int dj = (j0 < j1 ? +1 : j1 < j0 ? -1 : 0); 57 : 58 11379 : std::uint16_t i = i0; 59 11379 : std::uint16_t j = j0; 60 : 61 11379 : bool currentlyOnImpassable = !IS_PASSABLE(grid.get(i0, j0), passClass); 62 : 63 213993 : while (true) 64 : { 65 : // Make sure we are still in the limits 66 213993 : ENSURE( 67 : ((di > 0 && i0 <= i && i <= i1) || (di < 0 && i1 <= i && i <= i0) || (di == 0 && i == i0)) && 68 : ((dj > 0 && j0 <= j && j <= j1) || (dj < 0 && j1 <= j && j <= j0) || (dj == 0 && j == j0))); 69 : 70 : // Fail if we're moving onto an impassable navcell 71 213993 : bool passable = IS_PASSABLE(grid.get(i, j), passClass); 72 213993 : if (passable) 73 : currentlyOnImpassable = false; 74 0 : else if (!currentlyOnImpassable) 75 11379 : return false; 76 : 77 : // Succeed if we're at the target 78 213993 : if (i == i1 && j == j1) 79 : return true; 80 : 81 : // If we can only move horizontally/vertically, then just move in that direction 82 : // If we are reaching the limits, we can go straight to the end 83 202614 : if (di == 0 || i == i1) 84 : { 85 201348 : j += dj; 86 415341 : continue; 87 : } 88 1266 : else if (dj == 0 || j == j1) 89 : { 90 48 : i += di; 91 48 : continue; 92 : } 93 : 94 : // Otherwise we need to check which cell to move into: 95 : 96 : // Check whether the line intersects the horizontal (top/bottom) edge of 97 : // the current navcell. 98 : // Horizontal edge is (i, j + (dj>0?1:0)) .. (i + 1, j + (dj>0?1:0)) 99 : // Since we already know the line is moving from this navcell into a different 100 : // navcell, we simply need to test that the edge's endpoints are not both on the 101 : // same side of the line. 102 : 103 : // If we are crossing exactly a vertex of the grid, we will get dota or dotb equal 104 : // to 0. In that case we arbitrarily choose to move of dj. 105 : // This only works because we handle the case (i == i1 || j == j1) beforehand. 106 : // Otherwise we could go outside the j limits and never reach the final navcell. 107 : 108 1218 : entity_pos_t xia = entity_pos_t::FromInt(i).Multiply(Pathfinding::NAVCELL_SIZE); 109 1218 : entity_pos_t xib = entity_pos_t::FromInt(i+1).Multiply(Pathfinding::NAVCELL_SIZE); 110 1218 : entity_pos_t zj = entity_pos_t::FromInt(j + (dj+1)/2).Multiply(Pathfinding::NAVCELL_SIZE); 111 : 112 1218 : CFixedVector2D perp = CFixedVector2D(x1 - x0, z1 - z0).Perpendicular(); 113 1218 : entity_pos_t dota = (CFixedVector2D(xia, zj) - CFixedVector2D(x0, z0)).Dot(perp); 114 1218 : entity_pos_t dotb = (CFixedVector2D(xib, zj) - CFixedVector2D(x0, z0)).Dot(perp); 115 : 116 : // If the horizontal edge is fully on one side of the line, so the line doesn't 117 : // intersect it, we should move across the vertical edge instead 118 1218 : if ((dota < entity_pos_t::Zero() && dotb < entity_pos_t::Zero()) || 119 1020 : (dota > entity_pos_t::Zero() && dotb > entity_pos_t::Zero())) 120 276 : i += di; 121 : else 122 942 : j += dj; 123 : } 124 : } 125 : } 126 : 127 51 : PathfinderPassability::PathfinderPassability(pass_class_t mask, const CParamNode& node) : m_Mask(mask) 128 : { 129 51 : if (node.GetChild("MinWaterDepth").IsOk()) 130 15 : m_MinDepth = node.GetChild("MinWaterDepth").ToFixed(); 131 : else 132 36 : m_MinDepth = std::numeric_limits<fixed>::min(); 133 : 134 51 : if (node.GetChild("MaxWaterDepth").IsOk()) 135 22 : m_MaxDepth = node.GetChild("MaxWaterDepth").ToFixed(); 136 : else 137 29 : m_MaxDepth = std::numeric_limits<fixed>::max(); 138 : 139 51 : if (node.GetChild("MaxTerrainSlope").IsOk()) 140 29 : m_MaxSlope = node.GetChild("MaxTerrainSlope").ToFixed(); 141 : else 142 22 : m_MaxSlope = std::numeric_limits<fixed>::max(); 143 : 144 51 : if (node.GetChild("MinShoreDistance").IsOk()) 145 7 : m_MinShore = node.GetChild("MinShoreDistance").ToFixed(); 146 : else 147 44 : m_MinShore = std::numeric_limits<fixed>::min(); 148 : 149 51 : if (node.GetChild("MaxShoreDistance").IsOk()) 150 7 : m_MaxShore = node.GetChild("MaxShoreDistance").ToFixed(); 151 : else 152 44 : m_MaxShore = std::numeric_limits<fixed>::max(); 153 : 154 51 : if (node.GetChild("Clearance").IsOk()) 155 : { 156 16 : m_Clearance = node.GetChild("Clearance").ToFixed(); 157 : 158 : /* According to Philip who designed the original doc (in docs/pathfinder.pdf), 159 : * clearance should usually be integer to ensure consistent behavior when rasterizing 160 : * the passability map. 161 : * This seems doubtful to me and my pathfinder fix makes having a clearance of 0.8 quite convenient 162 : * so I comment out this check, but leave it here for the purpose of documentation should a bug arise. 163 : 164 : if (!(m_Clearance % Pathfinding::NAVCELL_SIZE).IsZero()) 165 : { 166 : // If clearance isn't an integer number of navcells then we'll 167 : // probably get weird behaviour when expanding the navcell grid 168 : // by clearance, vs expanding static obstructions by clearance 169 : LOGWARNING("Pathfinder passability class has clearance {:f}, should be multiple of {:f}", 170 : m_Clearance.ToFloat(), Pathfinding::NAVCELL_SIZE.ToFloat()); 171 : }*/ 172 : } 173 : else 174 35 : m_Clearance = fixed::Zero(); 175 : 176 51 : if (node.GetChild("Obstructions").IsOk()) 177 : { 178 36 : const std::string& obstructions = node.GetChild("Obstructions").ToString(); 179 36 : if (obstructions == "none") 180 12 : m_Obstructions = NONE; 181 24 : else if (obstructions == "pathfinding") 182 16 : m_Obstructions = PATHFINDING; 183 8 : else if (obstructions == "foundation") 184 8 : m_Obstructions = FOUNDATION; 185 : else 186 : { 187 0 : LOGERROR("Invalid value for Obstructions in pathfinder.xml for pass class {}", mask); 188 0 : m_Obstructions = NONE; 189 : } 190 : } 191 : else 192 15 : m_Obstructions = NONE; 193 51 : }