Line data Source code
1 : /* Copyright (C) 2025 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 "PathGoal.h"
21 :
22 : #include "lib/code_annotation.h"
23 : #include "maths/MathUtil.h"
24 : #include "simulation2/helpers/Geometry.h"
25 : #include "simulation2/helpers/Pathfinding.h"
26 :
27 : #include <algorithm>
28 :
29 463724 : static bool NavcellContainsCircle(int i, int j, fixed x, fixed z, fixed r, bool inside)
30 : {
31 : // Accept any navcell (i,j) that contains a point which is inside[/outside]
32 : // (or on the edge of) the circle
33 :
34 : // Get world-space bounds of navcell
35 463724 : entity_pos_t x0 = entity_pos_t::FromInt(i).Multiply(Pathfinding::NAVCELL_SIZE);
36 463724 : entity_pos_t z0 = entity_pos_t::FromInt(j).Multiply(Pathfinding::NAVCELL_SIZE);
37 463724 : entity_pos_t x1 = x0 + Pathfinding::NAVCELL_SIZE;
38 463724 : entity_pos_t z1 = z0 + Pathfinding::NAVCELL_SIZE;
39 :
40 463724 : if (inside)
41 : {
42 : // Get the point inside the navcell closest to (x,z)
43 339590 : entity_pos_t nx = Clamp(x, x0, x1);
44 339590 : entity_pos_t nz = Clamp(z, z0, z1);
45 : // Check if that point is inside the circle
46 673284 : return (CFixedVector2D(nx, nz) - CFixedVector2D(x, z)).CompareLength(r) <= 0;
47 : }
48 : else
49 : {
50 : // If any corner of the navcell is outside the circle, return true.
51 : // Otherwise, since the circle is convex, there cannot be any other point
52 : // in the navcell that is outside the circle.
53 124134 : return (
54 124134 : (CFixedVector2D(x0, z0) - CFixedVector2D(x, z)).CompareLength(r) >= 0
55 94317 : || (CFixedVector2D(x1, z0) - CFixedVector2D(x, z)).CompareLength(r) >= 0
56 93501 : || (CFixedVector2D(x0, z1) - CFixedVector2D(x, z)).CompareLength(r) >= 0
57 124541 : || (CFixedVector2D(x1, z1) - CFixedVector2D(x, z)).CompareLength(r) >= 0
58 : );
59 : }
60 : }
61 :
62 2771 : static bool NavcellContainsSquare(int i, int j,
63 : fixed x, fixed z, CFixedVector2D u, CFixedVector2D v, fixed hw, fixed hh,
64 : bool inside)
65 : {
66 : // Accept any navcell (i,j) that contains a point which is inside[/outside]
67 : // (or on the edge of) the square
68 :
69 : // Get world-space bounds of navcell
70 2771 : entity_pos_t x0 = entity_pos_t::FromInt(i).Multiply(Pathfinding::NAVCELL_SIZE);
71 2771 : entity_pos_t z0 = entity_pos_t::FromInt(j).Multiply(Pathfinding::NAVCELL_SIZE);
72 2771 : entity_pos_t x1 = x0 + Pathfinding::NAVCELL_SIZE;
73 2771 : entity_pos_t z1 = z0 + Pathfinding::NAVCELL_SIZE;
74 :
75 2771 : if (inside)
76 : {
77 : // Get the point inside the navcell closest to (x,z)
78 2771 : entity_pos_t nx = Clamp(x, x0, x1);
79 2771 : entity_pos_t nz = Clamp(z, z0, z1);
80 : // Check if that point is inside the circle
81 5542 : return Geometry::PointIsInSquare(CFixedVector2D(nx - x, nz - z), u, v, CFixedVector2D(hw, hh));
82 : }
83 : else
84 : {
85 : // If any corner of the navcell is outside the square, return true.
86 : // Otherwise, since the square is convex, there cannot be any other point
87 : // in the navcell that is outside the square.
88 0 : return (
89 0 : !Geometry::PointIsInSquare(CFixedVector2D(x0 - x, z0 - z), u, v, CFixedVector2D(hw, hh))
90 0 : || !Geometry::PointIsInSquare(CFixedVector2D(x1 - x, z0 - z), u, v, CFixedVector2D(hw, hh))
91 0 : || !Geometry::PointIsInSquare(CFixedVector2D(x0 - x, z1 - z), u, v, CFixedVector2D(hw, hh))
92 0 : || !Geometry::PointIsInSquare(CFixedVector2D(x1 - x, z1 - z), u, v, CFixedVector2D(hw, hh))
93 : );
94 : }
95 : }
96 :
97 625773 : bool PathGoal::NavcellContainsGoal(int i, int j) const
98 : {
99 625773 : switch (type)
100 : {
101 164409 : case POINT:
102 164409 : {
103 : // Only accept a single navcell
104 164409 : int gi = (x >> Pathfinding::NAVCELL_SIZE_LOG2).ToInt_RoundToNegInfinity();
105 164409 : int gj = (z >> Pathfinding::NAVCELL_SIZE_LOG2).ToInt_RoundToNegInfinity();
106 164409 : return gi == i && gj == j;
107 : }
108 336030 : case CIRCLE:
109 336030 : return NavcellContainsCircle(i, j, x, z, hw, true);
110 122566 : case INVERTED_CIRCLE:
111 122566 : return NavcellContainsCircle(i, j, x, z, hw, false);
112 2768 : case SQUARE:
113 2768 : return NavcellContainsSquare(i, j, x, z, u, v, hw, hh, true);
114 0 : case INVERTED_SQUARE:
115 0 : return NavcellContainsSquare(i, j, x, z, u, v, hw, hh, false);
116 0 : NODEFAULT;
117 : }
118 : }
119 :
120 0 : bool PathGoal::NavcellRectContainsGoal(int i0, int j0, int i1, int j1, int* gi, int* gj) const
121 : {
122 : // Get min/max to simplify range checks
123 0 : int imin = std::min(i0, i1);
124 0 : int imax = std::max(i0, i1);
125 0 : int jmin = std::min(j0, j1);
126 0 : int jmax = std::max(j0, j1);
127 :
128 : // Direction to iterate from (i0,j0) towards (i1,j1)
129 0 : int di = i1 < i0 ? -1 : +1;
130 0 : int dj = j1 < j0 ? -1 : +1;
131 :
132 0 : switch (type)
133 : {
134 0 : case POINT:
135 0 : {
136 : // Calculate the navcell that contains the point goal
137 0 : int i = (x >> Pathfinding::NAVCELL_SIZE_LOG2).ToInt_RoundToNegInfinity();
138 0 : int j = (z >> Pathfinding::NAVCELL_SIZE_LOG2).ToInt_RoundToNegInfinity();
139 : // If that goal navcell is in the given range, return it
140 0 : if (imin <= i && i <= imax && jmin <= j && j <= jmax)
141 : {
142 0 : if (gi)
143 0 : *gi = i;
144 0 : if (gj)
145 0 : *gj = j;
146 0 : return true;
147 : }
148 : return false;
149 : }
150 :
151 : case CIRCLE:
152 : {
153 : // Loop over all navcells in the given range (starting at (i0,j0) since
154 : // this function is meant to find the goal navcell nearest to there
155 : // assuming jmin==jmax || imin==imax),
156 : // and check whether any point in each navcell is within the goal circle.
157 : // (TODO: this is pretty inefficient.)
158 0 : for (int j = j0; jmin <= j && j <= jmax; j += dj)
159 : {
160 0 : for (int i = i0; imin <= i && i <= imax; i += di)
161 : {
162 0 : if (NavcellContainsCircle(i, j, x, z, hw, true))
163 : {
164 0 : if (gi)
165 0 : *gi = i;
166 0 : if (gj)
167 0 : *gj = j;
168 0 : return true;
169 : }
170 : }
171 : }
172 : return false;
173 : }
174 :
175 : case INVERTED_CIRCLE:
176 : {
177 : // Loop over all navcells in the given range (starting at (i0,j0) since
178 : // this function is meant to find the goal navcell nearest to there
179 : // assuming jmin==jmax || imin==imax),
180 : // and check whether any point in each navcell is outside the goal circle.
181 : // (TODO: this is pretty inefficient.)
182 0 : for (int j = j0; jmin <= j && j <= jmax; j += dj)
183 : {
184 0 : for (int i = i0; imin <= i && i <= imax; i += di)
185 : {
186 0 : if (NavcellContainsCircle(i, j, x, z, hw, false))
187 : {
188 0 : if (gi)
189 0 : *gi = i;
190 0 : if (gj)
191 0 : *gj = j;
192 0 : return true;
193 : }
194 : }
195 : }
196 : return false;
197 : }
198 :
199 : case SQUARE:
200 : {
201 : // Loop over all navcells in the given range (starting at (i0,j0) since
202 : // this function is meant to find the goal navcell nearest to there
203 : // assuming jmin==jmax || imin==imax),
204 : // and check whether any point in each navcell is within the goal square.
205 : // (TODO: this is pretty inefficient.)
206 0 : for (int j = j0; jmin <= j && j <= jmax; j += dj)
207 : {
208 0 : for (int i = i0; imin <= i && i <= imax; i += di)
209 : {
210 0 : if (NavcellContainsSquare(i, j, x, z, u, v, hw, hh, true))
211 : {
212 0 : if (gi)
213 0 : *gi = i;
214 0 : if (gj)
215 0 : *gj = j;
216 0 : return true;
217 : }
218 : }
219 : }
220 : return false;
221 : }
222 :
223 : case INVERTED_SQUARE:
224 : {
225 : // Loop over all navcells in the given range (starting at (i0,j0) since
226 : // this function is meant to find the goal navcell nearest to there
227 : // assuming jmin==jmax || imin==imax),
228 : // and check whether any point in each navcell is outside the goal square.
229 : // (TODO: this is pretty inefficient.)
230 0 : for (int j = j0; jmin <= j && j <= jmax; j += dj)
231 : {
232 0 : for (int i = i0; imin <= i && i <= imax; i += di)
233 : {
234 0 : if (NavcellContainsSquare(i, j, x, z, u, v, hw, hh, false))
235 : {
236 0 : if (gi)
237 0 : *gi = i;
238 0 : if (gj)
239 0 : *gj = j;
240 0 : return true;
241 : }
242 : }
243 : }
244 : return false;
245 : }
246 :
247 0 : NODEFAULT;
248 : }
249 : }
250 :
251 23 : bool PathGoal::RectContainsGoal(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1) const
252 : {
253 23 : switch (type)
254 : {
255 5 : case POINT:
256 5 : return x0 <= x && x <= x1 && z0 <= z && z <= z1;
257 :
258 4 : case CIRCLE:
259 4 : {
260 4 : entity_pos_t nx = Clamp(x, x0, x1);
261 4 : entity_pos_t nz = Clamp(z, z0, z1);
262 5 : return (CFixedVector2D(nx, nz) - CFixedVector2D(x, z)).CompareLength(hw) <= 0;
263 : }
264 :
265 4 : case INVERTED_CIRCLE:
266 4 : {
267 4 : return (
268 4 : (CFixedVector2D(x0, z0) - CFixedVector2D(x, z)).CompareLength(hw) >= 0
269 1 : || (CFixedVector2D(x1, z0) - CFixedVector2D(x, z)).CompareLength(hw) >= 0
270 1 : || (CFixedVector2D(x0, z1) - CFixedVector2D(x, z)).CompareLength(hw) >= 0
271 4 : || (CFixedVector2D(x1, z1) - CFixedVector2D(x, z)).CompareLength(hw) >= 0
272 : );
273 : }
274 :
275 5 : case SQUARE:
276 5 : {
277 5 : entity_pos_t nx = Clamp(x, x0, x1);
278 5 : entity_pos_t nz = Clamp(z, z0, z1);
279 10 : return Geometry::PointIsInSquare(CFixedVector2D(nx - x, nz - z), u, v, CFixedVector2D(hw, hh));
280 : }
281 :
282 5 : case INVERTED_SQUARE:
283 5 : {
284 5 : return (
285 7 : !Geometry::PointIsInSquare(CFixedVector2D(x0 - x, z0 - z), u, v, CFixedVector2D(hw, hh))
286 4 : || !Geometry::PointIsInSquare(CFixedVector2D(x1 - x, z0 - z), u, v, CFixedVector2D(hw, hh))
287 4 : || !Geometry::PointIsInSquare(CFixedVector2D(x0 - x, z1 - z), u, v, CFixedVector2D(hw, hh))
288 7 : || !Geometry::PointIsInSquare(CFixedVector2D(x1 - x, z1 - z), u, v, CFixedVector2D(hw, hh))
289 : );
290 : }
291 :
292 0 : NODEFAULT;
293 : }
294 : }
295 :
296 2461 : fixed PathGoal::DistanceToPoint(CFixedVector2D pos) const
297 : {
298 2461 : CFixedVector2D d(pos.X - x, pos.Y - z);
299 :
300 2461 : switch (type)
301 : {
302 782 : case POINT:
303 782 : return d.Length();
304 :
305 1442 : case CIRCLE:
306 2463 : return d.CompareLength(hw) <= 0 ? fixed::Zero() : d.Length() - hw;
307 :
308 62 : case INVERTED_CIRCLE:
309 62 : return d.CompareLength(hw) >= 0 ? fixed::Zero() : hw - d.Length();
310 :
311 173 : case SQUARE:
312 173 : {
313 173 : CFixedVector2D halfSize(hw, hh);
314 213 : return Geometry::PointIsInSquare(d, u, v, halfSize) ?
315 133 : fixed::Zero() : Geometry::DistanceToSquare(d, u, v, halfSize);
316 : }
317 :
318 2 : case INVERTED_SQUARE:
319 2 : {
320 2 : CFixedVector2D halfSize(hw, hh);
321 2 : return !Geometry::PointIsInSquare(d, u, v, halfSize) ?
322 1 : fixed::Zero() : Geometry::DistanceToSquare(d, u, v, halfSize);
323 : }
324 :
325 0 : NODEFAULT;
326 : }
327 : }
328 :
329 7990 : CFixedVector2D PathGoal::NearestPointOnGoal(CFixedVector2D pos) const
330 : {
331 7990 : CFixedVector2D g(x, z);
332 :
333 7990 : switch (type)
334 : {
335 3020 : case POINT:
336 3020 : return g;
337 :
338 3794 : case CIRCLE:
339 3794 : {
340 3794 : CFixedVector2D d(pos.X - x, pos.Y - z);
341 3794 : if (d.CompareLength(hw) <= 0)
342 1 : return pos;
343 :
344 3793 : d.Normalize(hw);
345 3794 : return g + d;
346 : }
347 :
348 830 : case INVERTED_CIRCLE:
349 830 : {
350 830 : CFixedVector2D d(pos.X - x, pos.Y - z);
351 830 : if (d.CompareLength(hw) >= 0)
352 1 : return pos;
353 :
354 829 : if (d.IsZero())
355 0 : d = CFixedVector2D(fixed::FromInt(1), fixed::Zero()); // some arbitrary direction
356 829 : d.Normalize(hw);
357 830 : return g + d;
358 : }
359 :
360 344 : case SQUARE:
361 344 : {
362 344 : CFixedVector2D halfSize(hw, hh);
363 344 : CFixedVector2D d(pos.X - x, pos.Y - z);
364 344 : return Geometry::PointIsInSquare(d, u, v, halfSize) ?
365 687 : pos : g + Geometry::NearestPointOnSquare(d, u, v, halfSize);
366 : }
367 :
368 2 : case INVERTED_SQUARE:
369 2 : {
370 2 : CFixedVector2D halfSize(hw, hh);
371 2 : CFixedVector2D d(pos.X - x, pos.Y - z);
372 2 : return !Geometry::PointIsInSquare(d, u, v, halfSize) ?
373 3 : pos : g + Geometry::NearestPointOnSquare(d, u, v, halfSize);
374 : }
375 :
376 0 : NODEFAULT;
377 : }
378 : }
|