1 /**
2 * @file
3 * @brief grid pathfinding and routing
4 */
5
6 /*
7 All original material Copyright (C) 2002-2013 UFO: Alien Invasion.
8
9 Copyright (C) 1997-2001 Id Software, Inc.
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
20 See the GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 */
27
28 #include "common.h"
29 #include "routing.h"
30
31 /*
32 ===============================================================================
33 MAP TRACING DEBUGGING TABLES
34 ===============================================================================
35 */
36
37 bool debugTrace = false;
38
39 /*
40 ==========================================================
41 LOCAL CONSTANTS
42 ==========================================================
43 */
44
45 #define RT_NO_OPENING -1
46
47 /* Width of the box required to stand in a cell by an actor's feet. */
48 #define halfMicrostepSize (PATHFINDING_MICROSTEP_SIZE / 2 - DIST_EPSILON)
49 /* This is a template for the extents of the box used by an actor's feet. */
50 static const AABB footBox(-halfMicrostepSize, -halfMicrostepSize, 0, halfMicrostepSize, halfMicrostepSize, 0);
51
52 /* Width of the box required to stand in a cell by an actor's torso. */
53 #define half1x1Width (UNIT_SIZE * 1 / 2 - WALL_SIZE - DIST_EPSILON)
54 #define half2x2Width (UNIT_SIZE * 2 / 2 - WALL_SIZE - DIST_EPSILON)
55 /* These are templates for the extents of the box used by an actor's torso. */
56 static const AABB actor1x1Box(-half1x1Width, -half1x1Width, 0, half1x1Width, half1x1Width, 0);
57 static const AABB actor2x2Box(-half2x2Width, -half2x2Width, 0, half2x2Width, half2x2Width, 0);
58
59 /*
60 ==========================================================
61 LOCAL TYPES
62 ==========================================================
63 */
64
65 /**
66 * @brief RT_data_s contains the essential data that is passed to most of the RT_* functions
67 */
68 class RoutingData
69 {
70 public:
71 mapTiles_t* mapTiles;
72 Routing &routing; /**< The routing tables */
73 actorSizeEnum_t actorSize; /**< The size of the actor, in cells */
74 const char** list; /**< The local models list */
75
76 RoutingData (mapTiles_t* mapTiles, Routing &r, actorSizeEnum_t actorSize, const char** list);
77 };
78
RoutingData(mapTiles_t * mapTiles,Routing & r,actorSizeEnum_t actorSize,const char ** list)79 RoutingData::RoutingData (mapTiles_t* mapTiles, Routing &r, actorSizeEnum_t actorSize, const char** list) : routing(r)
80 {
81 this->mapTiles = mapTiles;
82 this->actorSize = actorSize;
83 this->list = list;
84 }
85
RT_StepupSet(RoutingData * rtd,const int x,const int y,const int z,const int dir,const int val)86 static inline void RT_StepupSet (RoutingData *rtd, const int x, const int y, const int z, const int dir, const int val)
87 {
88 rtd->routing.setStepup(rtd->actorSize, x, y, z, dir, val);
89 }
90
RT_ConnSetNoGo(RoutingData * rtd,const int x,const int y,const int z,const int dir)91 static inline void RT_ConnSetNoGo (RoutingData *rtd, const int x, const int y, const int z, const int dir)
92 {
93 rtd->routing.setConn(rtd->actorSize, x, y, z, dir, 0);
94 rtd->routing.setStepup(rtd->actorSize, x, y, z, dir, PATHFINDING_NO_STEPUP);
95 }
96
97 /**
98 * @brief A 'place' is a part of a grid column where an actor can exist
99 * Unlike for a grid-cell, floor and ceiling are absolute values
100 */
101 typedef struct place_s {
102 pos3_t cell; /**< coordinates of the grid-cell this was derived from. */
103 int floor; /**< The floor of the place, given in absolute QUANTs */
104 int ceiling; /**< The ceiling of it, given in absolute QUANTs. */
105 int floorZ; /**< The level (0-7) of the floor. */
106 bool usable; /**< does an actor fit in here ? */
107
isUsableplace_s108 inline bool isUsable (void) const
109 {
110 return usable;
111 }
112 } place_t;
113
RT_PlaceInit(const Routing & routing,const actorSizeEnum_t actorSize,place_t * p,const int x,const int y,const int z)114 static inline void RT_PlaceInit (const Routing &routing, const actorSizeEnum_t actorSize, place_t* p, const int x, const int y, const int z)
115 {
116 p->cell[0] = x;
117 p->cell[1] = y;
118 p->cell[2] = z;
119 const int relCeiling = routing.getCeiling(actorSize, p->cell);
120 p->floor = routing.getFloor(actorSize, x, y, z) + z * CELL_HEIGHT;
121 p->ceiling = relCeiling + z * CELL_HEIGHT;
122 p->floorZ = std::max(0, p->floor / CELL_HEIGHT) ;
123 p->usable = (relCeiling && p->floor > -1 && p->ceiling - p->floor >= PATHFINDING_MIN_OPENING) ? true : false;
124 }
125
RT_PlaceDoesIntersectEnough(const place_t * p,const place_t * other)126 static inline bool RT_PlaceDoesIntersectEnough (const place_t* p, const place_t* other)
127 {
128 return (std::min(p->ceiling, other->ceiling) - std::max(p->floor, other->floor) >= PATHFINDING_MIN_OPENING);
129 }
130
131 /**
132 * @brief This function detects a special stairway situation, where one place is right
133 * in front of a stairway and has a floor at eg. 1 and a ceiling at eg. 16.
134 * The other place has the beginning of the stairway, so the floor is at eg. 6
135 * and the ceiling is that of the higher level, eg. 32.
136 */
RT_PlaceIsShifted(const place_t * p,const place_t * other)137 static inline int RT_PlaceIsShifted (const place_t* p, const place_t* other)
138 {
139 if (!p->isUsable() || !other->isUsable())
140 return 0;
141 if (p->floor < other->floor && p->ceiling < other->ceiling)
142 return 1; /* stepping up */
143 if (p->floor > other->floor && p->ceiling > other->ceiling)
144 return 2; /* stepping down */
145 return 0;
146 }
147
148 /**
149 * @brief An 'opening' describes the connection between two adjacent spaces where an actor can exist in a cell
150 * @note Note that if size is @c 0, the other members are undefined. They may contain reasonable values, though
151 */
152 typedef struct opening_s {
153 int size; /**< The opening size (max actor height) that can travel this passage. */
154 int base; /**< The base height of the opening, given in abs QUANTs */
155 int stepup; /**< The stepup needed to travel through this passage in this direction. */
156 int invstepup; /**< The stepup needed to travel through this passage in the opposite direction. */
157 } opening_t;
158
159 /*
160 ==========================================================
161 GRID ORIENTED MOVEMENT AND SCANNING
162 ==========================================================
163 */
164
165 #ifdef DEBUG
166 /**
167 * @brief Dumps contents of a map to console for inspection.
168 * @param[in] routing The routing maps (either server or client map)
169 * @param[in] actorSize The size of the actor along the X and Y axis in cell units
170 * @param[in] lx The low end of the x range updated
171 * @param[in] ly The low end of the y range updated
172 * @param[in] lz The low end of the z range updated
173 * @param[in] hx The high end of the x range updated
174 * @param[in] hy The high end of the y range updated
175 * @param[in] hz The high end of the z range updated
176 */
RT_DumpMap(const Routing & routing,actorSizeEnum_t actorSize,int lx,int ly,int lz,int hx,int hy,int hz)177 static void RT_DumpMap (const Routing &routing, actorSizeEnum_t actorSize, int lx, int ly, int lz, int hx, int hy, int hz)
178 {
179 int x, y, z;
180
181 Com_Printf("\nRT_DumpMap (%i %i %i) (%i %i %i)\n", lx, ly, lz, hx, hy, hz);
182 for (z = hz; z >= lz; --z) {
183 Com_Printf("\nLayer %i:\n ", z);
184 for (x = lx; x <= hx; ++x) {
185 Com_Printf("%9i", x);
186 }
187 Com_Printf("\n");
188 for (y = hy; y >= ly; --y) {
189 Com_Printf("%3i ", y);
190 for (x = lx; x <= hx; ++x) {
191 Com_Printf("%s%s%s%s "
192 , RT_CONN_NX(routing, actorSize, x, y, z) ? "w" : " "
193 , RT_CONN_PY(routing, actorSize, x, y, z) ? "n" : " "
194 , RT_CONN_NY(routing, actorSize, x, y, z) ? "s" : " "
195 , RT_CONN_PX(routing, actorSize, x, y, z) ? "e" : " "
196 );
197 }
198 Com_Printf("\n");
199 }
200 }
201 }
202
203 /**
204 * @brief Dumps contents of the entire client map to console for inspection.
205 * @param[in] map A pointer to the map being dumped
206 */
RT_DumpWholeMap(mapTiles_t * mapTiles,const Routing & routing)207 void RT_DumpWholeMap (mapTiles_t* mapTiles, const Routing &routing)
208 {
209 AABB box;
210 vec3_t normal, origin;
211 pos3_t start, end, test;
212 trace_t trace;
213 int i;
214
215 /* Initialize start, end, and normal */
216 VectorClear(start);
217 VectorSet(end, PATHFINDING_WIDTH - 1, PATHFINDING_WIDTH - 1, PATHFINDING_HEIGHT - 1);
218 VectorSet(normal, UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_HEIGHT / 2);
219 VectorClear(origin);
220
221 for (i = 0; i < 3; i++) {
222 /* Lower positive boundary */
223 while (end[i] > start[i]) {
224 /* Adjust ceiling */
225 VectorCopy(start, test);
226 test[i] = end[i] - 1; /* test is now one floor lower than end */
227 /* Prep boundary box */
228 PosToVec(test, box.mins);
229 VectorSubtract(box.mins, normal, box.mins);
230 PosToVec(end, box.maxs);
231 VectorAdd(box.maxs, normal, box.maxs);
232 /* Test for stuff in a small box, if there is something then exit while */
233 trace = RT_COMPLETEBOXTRACE_SIZE(mapTiles, &box, nullptr);
234 if (trace.fraction < 1.0)
235 break;
236 /* There is nothing, lower the boundary. */
237 end[i]--;
238 }
239
240 /* Raise negative boundary */
241 while (end[i] > start[i]) {
242 /* Adjust ceiling */
243 VectorCopy(end, test);
244 test[i] = start[i] + 1; /* test is now one floor lower than end */
245 /* Prep boundary box */
246 PosToVec(start, box.mins);
247 VectorSubtract(box.mins, normal, box.mins);
248 PosToVec(test, box.maxs);
249 VectorAdd(box.maxs, normal, box.maxs);
250 /* Test for stuff in a small box, if there is something then exit while */
251 trace = RT_COMPLETEBOXTRACE_SIZE(mapTiles, &box, nullptr);
252 if (trace.fraction < 1.0)
253 break;
254 /* There is nothing, raise the boundary. */
255 start[i]++;
256 }
257 }
258
259 /* Dump the client map */
260 RT_DumpMap(routing, 0, start[0], start[1], start[2], end[0], end[1], end[2]);
261 }
262 #endif
263
264 /**
265 * @brief Check if an actor can stand(up) in the cell given by pos
266 */
RT_CanActorStandHere(const Routing & routing,const int actorSize,const pos3_t pos)267 bool RT_CanActorStandHere (const Routing &routing, const int actorSize, const pos3_t pos)
268 {
269 if (routing.getCeiling(actorSize, pos) - routing.getFloor(actorSize, pos) >= PLAYER_STANDING_HEIGHT / QUANT)
270 return true;
271 else
272 return false;
273 }
274
275 /**
276 * @brief Calculate the map size via model data and store grid size
277 * in map_min and map_max. This is done with every new map load
278 * @param[in] mapTiles List of tiles the current (RMA-)map is composed of
279 * @param[out] map_min The lower extents of the current map.
280 * @param[out] map_max The upper extents of the current map.
281 * @sa CMod_LoadRouting
282 * @sa DoRouting
283 */
RT_GetMapSize(mapTiles_t * mapTiles,vec3_t map_min,vec3_t map_max)284 void RT_GetMapSize (mapTiles_t* mapTiles, vec3_t map_min, vec3_t map_max)
285 {
286 AABB box;
287 const vec3_t normal = {UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_HEIGHT / 2};
288 pos3_t start, end, test;
289 vec3_t origin;
290 int i;
291
292 /* Initialize start, end, and normal */
293 VectorSet(start, 0, 0, 0);
294 VectorSet(end, PATHFINDING_WIDTH - 1, PATHFINDING_WIDTH - 1, PATHFINDING_HEIGHT - 1);
295 VectorCopy(vec3_origin, origin);
296
297 for (i = 0; i < 3; i++) {
298 /* Lower positive boundary */
299 while (end[i] > start[i]) {
300 /* Adjust ceiling */
301 VectorCopy(start, test);
302 test[i] = end[i]; /* the box from test to end is now one cell high */
303 /* Prep boundary box */
304 PosToVec(test, box.mins);
305 VectorSubtract(box.mins, normal, box.mins);
306 PosToVec(end, box.maxs);
307 VectorAdd(box.maxs, normal, box.maxs);
308 /* Test for stuff in a small box, if there is something then exit while */
309 const trace_t trace = RT_COMPLETEBOXTRACE_SIZE(mapTiles, &box, nullptr);
310 if (trace.fraction < 1.0)
311 break;
312 /* There is nothing, lower the boundary. */
313 end[i]--;
314 }
315
316 /* Raise negative boundary */
317 while (end[i] > start[i]) {
318 /* Adjust ceiling */
319 VectorCopy(end, test);
320 test[i] = start[i]; /* the box from start to test is now one cell high */
321 /* Prep boundary box */
322 PosToVec(start, box.mins);
323 VectorSubtract(box.mins, normal, box.mins);
324 PosToVec(test, box.maxs);
325 VectorAdd(box.maxs, normal, box.maxs);
326 /* Test for stuff in a small box, if there is something then exit while */
327 const trace_t trace = RT_COMPLETEBOXTRACE_SIZE(mapTiles, &box, nullptr);
328 if (trace.fraction < 1.0)
329 break;
330 /* There is nothing, raise the boundary. */
331 start[i]++;
332 }
333 }
334
335 /* Com_Printf("Extents: (%i, %i, %i) to (%i, %i, %i)\n", start[0], start[1], start[2], end[0], end[1], end[2]); */
336
337 /* convert to vectors */
338 PosToVec(start, map_min);
339 PosToVec(end, map_max);
340
341 /* Stretch to the exterior edges of our extents */
342 VectorSubtract(map_min, normal, map_min);
343 VectorAdd(map_max, normal, map_max);
344 }
345
346
347 /*
348 ===============================================================================
349 NEW MAP TRACING FUNCTIONS
350 ===============================================================================
351 */
352
353 /**
354 * @brief Check if pos is on solid ground
355 * @param[in] routing The map's routing data
356 * @param[in] actorSize The size of the actor along the X and Y axis in cell units
357 * @param[in] pos The position to check below
358 * @return true if solid
359 * @sa CL_AddTargetingBox
360 * @todo see CL_ActorMoveMouse
361 */
RT_AllCellsBelowAreFilled(const Routing & routing,const int actorSize,const pos3_t pos)362 bool RT_AllCellsBelowAreFilled (const Routing &routing, const int actorSize, const pos3_t pos)
363 {
364 int i;
365
366 /* the -1 level is considered solid ground */
367 if (pos[2] == 0)
368 return true;
369
370 for (i = 0; i < pos[2]; i++) {
371 if (routing.getCeiling(actorSize, pos[0], pos[1], i) != 0)
372 return false;
373 }
374 return true;
375 }
376
377 /**
378 * @brief This function looks to see if an actor of a given size can occupy a cell(s) and if so
379 * identifies the floor and ceiling for that cell. If the cell has no floor, the floor will be negative
380 * with 0 indicating the base for the cell(s). If there is no ceiling in the cell, the first ceiling
381 * found above the current cell will be used. If there is no ceiling above the cell, the ceiling will
382 * be the top of the model. This function will also adjust all floor and ceiling values for all cells
383 * between the found floor and ceiling.
384 * @param[in] mapTiles List of tiles the current (RMA-)map is composed of
385 * @param[in] routing The map's routing data
386 * @param[in] actorSize The size of the actor along the X and Y axis in cell units
387 * @param[in] x The x position in the routing arrays (0 - PATHFINDING_WIDTH-1)
388 * @param[in] y The y position in the routing arrays (0 - PATHFINDING_WIDTH-1)
389 * @param[in] z The z position in the routing arrays (0 - PATHFINDING_HEIGHT-1)
390 * @param[in] list The local models list (a local model has a name starting with * followed by the model number)
391 * @return The z value of the next cell to scan, usually the cell with the ceiling.
392 * @sa Grid_RecalcRouting
393 */
RT_CheckCell(mapTiles_t * mapTiles,Routing & routing,const int actorSize,const int x,const int y,const int z,const char ** list)394 int RT_CheckCell (mapTiles_t* mapTiles, Routing &routing, const int actorSize, const int x, const int y, const int z, const char** list)
395 {
396 /* Width of the box required to stand in a cell by an actor's torso. */
397 const float halfActorWidth = UNIT_SIZE * actorSize / 2 - WALL_SIZE - DIST_EPSILON;
398 /* This is a template for the extents of the box used by an actor's legs. */
399 const AABB legBox(-halfMicrostepSize, -halfMicrostepSize, 0,
400 halfMicrostepSize, halfMicrostepSize, QuantToModel(PATHFINDING_LEGROOMHEIGHT) - DIST_EPSILON * 2);
401 /* This is a template for the extents of the box used by an actor's torso. */
402 const AABB torsoBox(-halfActorWidth, -halfActorWidth, QuantToModel(PATHFINDING_LEGROOMHEIGHT),
403 halfActorWidth, halfActorWidth, QuantToModel(PATHFINDING_MIN_OPENING) - DIST_EPSILON * 2);
404 /* This is a template for the ceiling trace after an actor's torso space has been found. */
405 const AABB ceilBox(-halfActorWidth, -halfActorWidth, 0,
406 halfActorWidth, halfActorWidth, 0);
407
408 vec3_t start, end; /* Start and end of the downward traces. */
409 vec3_t tstart, tend; /* Start and end of the upward traces. */
410 AABB box; /* Holds the exact bounds to be traced for legs and torso. */
411 pos3_t pos;
412 float bottom, top; /* Floor and ceiling model distances from the cell base. (in mapunits) */
413 #ifdef DEBUG
414 float initial; /* Cell floor and ceiling z coordinate. */
415 #endif
416 int bottomQ, topQ; /* The floor and ceiling in QUANTs */
417 int i;
418 int fz, cz; /* Floor and ceiling Z cell coordinates */
419
420 assert(actorSize > ACTOR_SIZE_INVALID && actorSize <= ACTOR_MAX_SIZE);
421 /* x and y cannot exceed PATHFINDING_WIDTH - actorSize */
422 assert((x >= 0) && (x <= PATHFINDING_WIDTH - actorSize));
423 assert((y >= 0) && (y <= PATHFINDING_WIDTH - actorSize));
424 assert(z < PATHFINDING_HEIGHT);
425
426 /* calculate tracing coordinates */
427 VectorSet(pos, x, y, z);
428 SizedPosToVec(pos, actorSize, end); /* end is now at the center of the cells the actor occupies. */
429
430 /* prepare fall down check */
431 VectorCopy(end, start);
432 /*
433 * Adjust these points so that start to end is from the top of the cell to the bottom of the model.
434 */
435 #ifdef DEBUG
436 initial = start[2] + UNIT_HEIGHT / 2; /* This is the top-most starting point in this cell. */
437 #endif
438 start[2] += UNIT_HEIGHT / 2 - QUANT; /* This one QUANT unit below initial. */
439 end[2] = -UNIT_HEIGHT * 2; /* To the bottom of the model! (Plus some for good measure) */
440
441 /*
442 * Trace for a floor. Steps:
443 * 1. Start at the top of the designated cell and scan toward the model's base.
444 * 2. If we do not find a brush, then this cell is bottomless and not enterable.
445 * 3. We have found an upward facing brush. Scan up PATHFINDING_LEGROOMHEIGHT height.
446 * 4. If we find anything, then this space is too small of an opening. Restart just below our current floor.
447 * 5. Trace up towards the model ceiling with a box as large as the actor. The first obstruction encountered
448 * marks the ceiling. If there are no obstructions, the model ceiling is the ceiling.
449 * 6. If the opening between the floor and the ceiling is not at least PATHFINDING_MIN_OPENING tall, then
450 * restart below the current floor.
451 */
452 for (;;) { /* Loop forever, we will exit if we hit the model bottom or find a valid floor. */
453 trace_t tr = RT_COMPLETEBOXTRACE_PASSAGE(mapTiles, Line(start, end), &footBox, list);
454 if (tr.fraction >= 1.0) { /* If there is no brush underneath this starting point, */
455 routing.setFilled(actorSize, x, y, 0, z); /* mark all cells to the model base as filled. */
456 return 0; /* return (a z-value of)0 to indicate we just scanned the model bottom. */
457 }
458
459 /* We have hit a brush that faces up and can be stood on. A potential floor. Look for a ceiling. */
460 bottom = tr.endpos[2]; /* record the floor position. */
461
462 #ifdef DEBUG
463 assert(initial > bottom);
464 #endif
465
466 /* Record the hit position in tstart for later use. */
467 VectorCopy(tr.endpos, tstart);
468
469 /* Prep the start and end of the "leg room" test. */
470 box.set(legBox);
471 box.shift(tstart); /* Now box has the lower and upper required foot space extent */
472
473 tr = RT_COMPLETEBOXTRACE_PASSAGE(mapTiles, Line(), &box, list);
474 if (tr.fraction < 1.0) {
475 /*
476 * There is a premature obstruction. We can't use this as a floor.
477 * Check under start. We need to have at least the minimum amount of clearance from our ceiling,
478 * So start at that point.
479 */
480 start[2] = bottom - QuantToModel(PATHFINDING_MIN_OPENING);
481 /* Check in case we are trying to scan too close to the bottom of the model. */
482 if (start[2] <= QuantToModel(PATHFINDING_MIN_OPENING)) {
483 /* There is no useable brush underneath this starting point. */
484 routing.setFilled(actorSize, x, y, 0, z); /* mark all cells to the model base as filled. */
485 return 0; /* return (a z-value of)0 to indicate we just scanned the model bottom. */
486 }
487 /* Restart with the new start[] value */
488 continue;
489 }
490
491 /* Prep the start and end of the "torso room" test. */
492 box.set(torsoBox);
493 box.shift(tstart); /* Now box has the lower and upper required torso space extent */
494
495 tr = RT_COMPLETEBOXTRACE_PASSAGE(mapTiles, Line(), &box, list);
496 if (tr.fraction < 1.0) {
497 /*
498 * There is a premature obstruction. We can't use this as a floor.
499 * Check under start. We need to have at least the minimum amount of clearance from our ceiling,
500 * So start at that point.
501 */
502 start[2] = bottom - QuantToModel(PATHFINDING_MIN_OPENING);
503 /* Check in case we are trying to scan too close to the bottom of the model. */
504 if (start[2] <= QuantToModel(PATHFINDING_MIN_OPENING)) {
505 /* There is no useable brush underneath this starting point. */
506 routing.setFilled(actorSize, x, y, 0, z); /* mark all cells to the model base as filled. */
507 return 0; /* return 0 to indicate we just scanned the model bottom. */
508 }
509 /* Restart */
510 continue;
511 }
512
513 /*
514 * If we are here, then the immediate floor is unobstructed MIN_OPENING units high.
515 * This is a valid floor. Find the actual ceiling.
516 */
517
518 tstart[2] = box.maxs[2]; /* The box trace for height starts at the top of the last trace. */
519 VectorCopy(tstart, tend);
520 tend[2] = PATHFINDING_HEIGHT * UNIT_HEIGHT; /* tend now reaches the model ceiling. */
521
522 tr = RT_COMPLETEBOXTRACE_PASSAGE(mapTiles, Line(tstart, tend), &ceilBox, list);
523
524 /* We found the ceiling. */
525 top = tr.endpos[2];
526
527 /*
528 * There is one last possibility:
529 * If our found ceiling is above the cell we started the scan in, then we may have scanned up through another
530 * floor (one sided brush). If this is the case, we set the ceiling to QUANT below the floor of the above
531 * ceiling if it is lower than our found ceiling.
532 */
533 if (tr.endpos[2] > (z + 1) * UNIT_HEIGHT) {
534 const float topf = (z + 1) * UNIT_HEIGHT + QuantToModel(routing.getFloor(actorSize, x, y, z + 1) - 1);
535 top = std::min(tr.endpos[2], topf);
536 }
537
538 /* We found the ceiling. */
539 top = tr.endpos[2];
540
541 /* exit the infinite while loop */
542 break;
543 }
544
545 UFO_assert(bottom <= top, "\nassert(bottom <= top): x=%i y=%i bottom=%f top=%f\n", x, y, bottom, top);
546
547 /* top and bottom are absolute model heights. Find the actual cell z coordinates for these heights.
548 * ...but before rounding, give back the DIST_EPSILON that was added by the trace.
549 * Actually, we have to give back two DIST_EPSILON to prevent rounding issues */
550 bottom -= 2 * DIST_EPSILON;
551 top += 2 * DIST_EPSILON;
552 bottomQ = ModelFloorToQuant(bottom); /* Convert to QUANT units to ensure the floor is rounded up to the correct value. */
553 topQ = ModelCeilingToQuant(top); /* Convert to QUANT units to ensure the floor is rounded down to the correct value. */
554 fz = floorf(bottomQ / CELL_HEIGHT); /* Ensure we round down to get the bottom-most affected cell */
555 /** @note Remember that ceiling values of 1-16 belong to a cell. We need to adjust topQ by 1 to round to the correct z value. */
556 cz = std::min(z, (int)(floorf((topQ - 1) / CELL_HEIGHT))); /* Use the lower of z or the calculated ceiling */
557
558 assert(fz <= cz);
559
560 /* Last, update the floors and ceilings of cells from (x, y, fz) to (x, y, cz) */
561 for (i = fz; i <= cz; i++) {
562 /* Round up floor to keep feet out of model. */
563 routing.setFloor(actorSize, x, y, i, bottomQ - i * CELL_HEIGHT);
564 /* Round down ceiling to heep head out of model. Also offset by floor and max at 255. */
565 routing.setCeiling(actorSize, x, y, i, topQ - i * CELL_HEIGHT);
566 }
567
568 /* Also, update the floors of any filled cells immediately above the ceiling up to our original cell. */
569 routing.setFilled(actorSize, x, y, cz + 1, z);
570
571 /* Return the lowest z coordinate that we updated floors for. */
572 return fz;
573 }
574
575
576 /**
577 * @brief Performs traces to find a passage between two points given an upper and lower bound.
578 * @param[in] rtd The essential routing data with map, actorsize, ents
579 * @param[in] dir Direction of movement
580 * @param[in] x Starting x coordinate
581 * @param[in] y Starting y coordinate
582 * @param[in] z Starting z coordinate
583 * @param[in] openingSize Absolute height in QUANT units of the opening.
584 * @param[in] openingBase Absolute height in QUANT units of the bottom of the opening.
585 * @param[in] stepup Required stepup to travel in this direction.
586 */
RT_FillPassageData(RoutingData * rtd,const int dir,const int x,const int y,const int z,const int openingSize,const int openingBase,const int stepup)587 static int RT_FillPassageData (RoutingData *rtd, const int dir, const int x, const int y, const int z, const int openingSize, const int openingBase, const int stepup)
588 {
589 const int openingTop = openingBase + openingSize;
590 int fz, cz; /**< Floor and ceiling Z cell coordinates */
591 int i;
592
593 /* Final interpretation:
594 * We now have the floor and the ceiling of the passage traveled between the two cells.
595 * This span may cover many cells vertically. We can use this to our advantage:
596 * +Like in the floor tracing, we can assign the direction value for multiple cells and
597 * skip some scans.
598 * +The value of each current cell will list the max allowed height of an actor in the passageway,
599 * which also can be used to see if an actor can fly upward.
600 * +The allowed height will be based off the floor in the cell or the bottom of the cell; we do not
601 * want super tall characters to fly through ceilings.
602 * +To see if an actor can fly down, we check the cells on level down to see if the diagonal movement
603 * can be made and that both have ceilings above the current level.
604 */
605
606 fz = z;
607 cz = ceil((float)openingTop / CELL_HEIGHT) - 1;
608 cz = std::min(PATHFINDING_HEIGHT - 1, cz);
609
610 /* last chance- if cz < z, then bail (and there is an error with the ceiling data somewhere */
611 if (cz < z) {
612 /* We can't go this way. */
613 RT_ConnSetNoGo(rtd, x, y, z, dir);
614 if (debugTrace)
615 Com_Printf("Passage found but below current cell, opening_base=%i, opening_top=%i, z = %i, cz = %i.\n", openingBase, openingTop, z, cz);
616 return z;
617 }
618
619 if (debugTrace)
620 Com_Printf("Passage found, opening_base=%i, opening_size=%i, opening_top=%i, stepup=%i. (%i to %i)\n", openingBase, openingSize, openingTop, stepup, fz, cz);
621
622 assert(fz <= z && z <= cz);
623
624 /* Last, update the connections of cells from (x, y, fz) to (x, y, cz) for direction dir */
625 for (i = fz; i <= cz; i++) {
626 int oh;
627 /* Offset from the floor or the bottom of the current cell, whichever is higher. */
628 oh = openingTop - std::max(openingBase, i * CELL_HEIGHT);
629 /* Only if > 0 */
630 assert (oh >= 0);
631 rtd->routing.setConn(rtd->actorSize, x, y, i, dir, oh);
632 /* The stepup is 0 for all cells that are not at the floor. */
633 RT_StepupSet(rtd, x, y, i, dir, 0);
634 if (debugTrace) {
635 Com_Printf("getConn for (%i, %i, %i) as:%i dir:%i = %i\n", x, y, i, rtd->actorSize, dir, rtd->routing.getConn(rtd->actorSize, x, y, i, dir));
636 }
637 }
638
639 RT_StepupSet(rtd, x, y, z, dir, stepup);
640 if (debugTrace) {
641 Com_Printf("Final STEPUP for (%i, %i, %i) as:%i dir:%i = %i\n", x, y, z, rtd->actorSize, dir, stepup);
642 }
643
644 /*
645 * Return the highest z coordinate scanned- cz if fz==cz, z==cz, or the floor in cz is negative.
646 * Otherwise cz - 1 to recheck cz in case there is a floor in cz with its own ceiling.
647 */
648 if (fz == cz || z == cz || rtd->routing.getFloor(rtd->actorSize, x, y, cz) < 0)
649 return cz;
650 return cz - 1;
651 }
652
653 /**
654 * @brief Helper function to trace for walls
655 * @param[in] rtd The essential routing data with map, actorsize, ents
656 * @param[in] traceLine The starting point of the trace is at the FLOOR'S CENTER. The end point of the trace is centered x and y at the destination but at the same height as start.
657 * @param[in] hi The upper height ABOVE THE FLOOR of the bounding box.
658 * @param[in] lo The lower height ABOVE THE FLOOR of the bounding box.
659 */
RT_ObstructedTrace(const RoutingData * rtd,const Line & traceLine,int hi,int lo)660 static trace_t RT_ObstructedTrace (const RoutingData *rtd, const Line &traceLine, int hi, int lo)
661 {
662 AABB box; /**< Tracing box extents */
663 const float halfActorWidth = UNIT_SIZE * rtd->actorSize / 2 - WALL_SIZE - DIST_EPSILON;
664
665 /* Configure the box trace extents. The box is relative to the original floor. */
666 VectorSet(box.maxs, halfActorWidth, halfActorWidth, QuantToModel(hi) - DIST_EPSILON);
667 VectorSet(box.mins, -halfActorWidth, -halfActorWidth, QuantToModel(lo) + DIST_EPSILON);
668
669 /* perform the trace, then return true if the trace was obstructed. */
670 return RT_COMPLETEBOXTRACE_PASSAGE(rtd->mapTiles, traceLine, &box, rtd->list);
671 }
672
673
674 /**
675 * @brief Performs a trace to find the floor of a passage a fraction of the way from start to end.
676 * @param[in] rtd The essential routing data with map, actorsize, ents
677 * @param[in] start The starting coordinate to search for a floor from.
678 * @param[in] end The starting coordinate to search for a floor from.
679 * @param[in] frac The fraction of the distance traveled from start to end, using (0.0 to 1.0).
680 * @param[in] startingHeight The starting height for this upward trace.
681 * @return The absolute height of the found floor in QUANT units.
682 */
RT_FindOpeningFloorFrac(const RoutingData * rtd,const vec3_t start,const vec3_t end,const float frac,const int startingHeight)683 static int RT_FindOpeningFloorFrac (const RoutingData *rtd, const vec3_t start, const vec3_t end, const float frac, const int startingHeight)
684 {
685 vec3_t mstart, mend; /**< Midpoint line to trace across */ /**< Tracing box extents */
686 const AABB* box = (rtd->actorSize == ACTOR_SIZE_NORMAL ? &actor1x1Box : &actor2x2Box);
687
688 /* Position mstart and mend at the fraction point */
689 VectorInterpolation(start, end, frac, mstart);
690 VectorCopy(mstart, mend);
691 mstart[2] = QuantToModel(startingHeight) + (QUANT / 2); /* Set at the starting height, plus a little more to keep us off a potential surface. */
692 mend[2] = -QUANT; /* Set below the model. */
693
694 const trace_t tr = RT_COMPLETEBOXTRACE_PASSAGE(rtd->mapTiles, Line(mstart, mend), box, rtd->list);
695
696 if (debugTrace)
697 Com_Printf("Brush found at %f.\n", tr.endpos[2]);
698
699 /* OK, now we have the floor height value in tr.endpos[2].
700 * Divide by QUANT and round up.
701 */
702 return ModelFloorToQuant(tr.endpos[2] - DIST_EPSILON);
703 }
704
705
706 /**
707 * @brief Performs a trace to find the ceiling of a passage a fraction of the way from start to end.
708 * @param[in] rtd The essential routing data with map, actorsize, ents
709 * @param[in] start The starting coordinate to search for a ceiling from.
710 * @param[in] end The starting coordinate to search for a ceiling from.
711 * @param[in] frac The fraction of the distance traveled from start to end, using (0.0 to 1.0).
712 * @param[in] startingHeight The starting height for this upward trace.
713 * @return The absolute height of the found ceiling in QUANT units.
714 */
RT_FindOpeningCeilingFrac(const RoutingData * rtd,const vec3_t start,const vec3_t end,const float frac,const int startingHeight)715 static int RT_FindOpeningCeilingFrac (const RoutingData *rtd, const vec3_t start, const vec3_t end, const float frac, const int startingHeight)
716 {
717 vec3_t mstart, mend; /**< Midpoint line to trace across */
718 const AABB* box = (rtd->actorSize == ACTOR_SIZE_NORMAL ? &actor1x1Box : &actor2x2Box); /**< Tracing box extents */
719
720 /* Position mstart and mend at the midpoint */
721 VectorInterpolation(start, end, frac, mstart);
722 VectorCopy(mstart, mend);
723 mstart[2] = QuantToModel(startingHeight) - (QUANT / 2); /* Set at the starting height, minus a little more to keep us off a potential surface. */
724 mend[2] = UNIT_HEIGHT * PATHFINDING_HEIGHT + QUANT; /* Set above the model. */
725
726 const trace_t tr = RT_COMPLETEBOXTRACE_PASSAGE(rtd->mapTiles, Line(mstart, mend), box, rtd->list);
727
728 if (debugTrace)
729 Com_Printf("Brush found at %f.\n", tr.endpos[2]);
730
731 /* OK, now we have the floor height value in tr.endpos[2].
732 * Divide by QUANT and round down. */
733 return ModelCeilingToQuant(tr.endpos[2] + DIST_EPSILON);
734 }
735
736
737 /**
738 * @brief Performs traces to find the approximate floor of a passage.
739 * @param[in] rtd The essential routing data with map, actorsize, ents
740 * @param[in] start The starting coordinate to search for a floor from.
741 * @param[in] end The starting coordinate to search for a floor from.
742 * @param[in] startingHeight The starting height for this downward trace.
743 * @param[in] floorLimit The lowest limit of the found floor.
744 * @return The absolute height of the found floor in QUANT units.
745 */
RT_FindOpeningFloor(const RoutingData * rtd,const vec3_t start,const vec3_t end,const int startingHeight,const int floorLimit)746 static int RT_FindOpeningFloor (const RoutingData *rtd, const vec3_t start, const vec3_t end, const int startingHeight, const int floorLimit)
747 {
748 /* Look for additional space below init_bottom, down to lowest_bottom. */
749 int midfloor;
750
751 if (start[0] == end[0] || start[1] == end[1]) {
752 /* For orthogonal dirs, find the height at the midpoint. */
753 midfloor = RT_FindOpeningFloorFrac(rtd, start, end, 0.5, startingHeight);
754 } else {
755 int midfloor2;
756
757 /* If this is diagonal, trace the 1/3 and 2/3 points instead. */
758 /* 1/3 point */
759 midfloor = RT_FindOpeningFloorFrac(rtd, start, end, 0.33, startingHeight);
760
761 /* 2/3 point */
762 midfloor2 = RT_FindOpeningFloorFrac(rtd, start, end, 0.66, startingHeight);
763 midfloor = std::max(midfloor, midfloor2);
764 }
765
766 /* return the highest floor. */
767 return std::max(floorLimit, midfloor);
768 }
769
770
771 /**
772 * @brief Performs traces to find the approximate ceiling of a passage.
773 * @param[in] rtd The essential routing data with map, actorsize, ents
774 * @param[in] start The starting coordinate to search for a ceiling from.
775 * @param[in] end The starting coordinate to search for a ceiling from.
776 * @param[in] startingHeight The starting height for this upward trace.
777 * @param[in] ceilLimit The highest the ceiling may be.
778 * @return The absolute height of the found ceiling in QUANT units.
779 */
RT_FindOpeningCeiling(const RoutingData * rtd,const vec3_t start,const vec3_t end,const int startingHeight,const int ceilLimit)780 static int RT_FindOpeningCeiling (const RoutingData *rtd, const vec3_t start, const vec3_t end, const int startingHeight, const int ceilLimit)
781 {
782 int midceil;
783
784 if (start[0] == end[0] || start[1] == end[1]) {
785 /* For orthogonal dirs, find the height at the midpoint. */
786 midceil = RT_FindOpeningCeilingFrac(rtd, start, end, 0.5, startingHeight);
787 } else {
788 int midceil2;
789
790 /* If this is diagonal, trace the 1/3 and 2/3 points instead. */
791 /* 1/3 point */
792 midceil = RT_FindOpeningCeilingFrac(rtd, start, end, 0.33, startingHeight);
793
794 /* 2/3 point */
795 midceil2 = RT_FindOpeningCeilingFrac(rtd, start, end, 0.66, startingHeight);
796 midceil = std::min(midceil, midceil2);
797 }
798
799 /* return the lowest ceiling. */
800 return std::min(ceilLimit, midceil);
801 }
802
803
RT_CalcNewZ(const RoutingData * rtd,const int ax,const int ay,const int top,const int hi)804 static int RT_CalcNewZ (const RoutingData *rtd, const int ax, const int ay, const int top, const int hi)
805 {
806 int temp_z, adj_lo;
807
808 temp_z = floorf((hi - 1) / CELL_HEIGHT);
809 temp_z = std::min(temp_z, PATHFINDING_HEIGHT - 1);
810 adj_lo = rtd->routing.getFloor(rtd->actorSize, ax, ay, temp_z) + temp_z * CELL_HEIGHT;
811 if (adj_lo > hi) {
812 temp_z--;
813 adj_lo = rtd->routing.getFloor(rtd->actorSize, ax, ay, temp_z) + temp_z * CELL_HEIGHT;
814 }
815 /**
816 * @note Return a value only if there is a floor for the adjacent cell.
817 * Also the found adjacent lo must be at lease MIN_OPENING-MIN_STEPUP below
818 * the top.
819 */
820 if (adj_lo >= 0 && top - adj_lo >= PATHFINDING_MIN_OPENING - PATHFINDING_MIN_STEPUP) {
821 if (debugTrace)
822 Com_Printf("Found floor in destination cell: %i (%i).\n", adj_lo, temp_z);
823 return floorf(adj_lo / CELL_HEIGHT);
824 }
825 if (debugTrace)
826 Com_Printf("Skipping found floor in destination cell- not enough opening: %i (%i).\n", adj_lo, temp_z);
827
828 return RT_NO_OPENING;
829 }
830
831
832 /**
833 * @brief Performs actual trace to find a passage between two points given an upper and lower bound.
834 * @param[in] rtd The essential routing data with map, actorsize, ents
835 * @param[in] start Starting trace coordinate
836 * @param[in] end Ending trace coordinate
837 * @param[in] ax Ending x coordinate
838 * @param[in] ay Ending y coordinate
839 * @param[in] bottom Actual height of the starting floor.
840 * @param[in] top Actual height of the starting ceiling.
841 * @param[in] lo Actual height of the bottom of the slice trace.
842 * @param[in] hi Actual height of the top of the slice trace.
843 * @param[out] foundLow Actual height of the bottom of the found passage.
844 * @param[out] foundHigh Actual height of the top of the found passage.
845 * @return The new z value of the actor after traveling in this direction from the starting location.
846 */
RT_TraceOpening(const RoutingData * rtd,const vec3_t start,const vec3_t end,const int ax,const int ay,const int bottom,const int top,int lo,int hi,int * foundLow,int * foundHigh)847 static int RT_TraceOpening (const RoutingData *rtd, const vec3_t start, const vec3_t end, const int ax, const int ay, const int bottom, const int top, int lo, int hi, int* foundLow, int* foundHigh)
848 {
849 const trace_t tr = RT_ObstructedTrace(rtd, Line(start, end), hi, lo);
850 if (tr.fraction >= 1.0) {
851 lo = RT_FindOpeningFloor(rtd, start, end, lo, bottom);
852 hi = RT_FindOpeningCeiling(rtd, start, end, hi, top);
853 if (hi - lo >= PATHFINDING_MIN_OPENING) {
854 int tempZ;
855 if (lo == -1) {
856 /* Bailing- no floor in destination cell. */
857 *foundLow = *foundHigh = 0;
858 return RT_NO_OPENING;
859 }
860 /* This opening works, use it! */
861 *foundLow = lo;
862 *foundHigh = hi;
863 /* Find the floor for the highest adjacent cell in this passage. */
864 tempZ = RT_CalcNewZ(rtd, ax, ay, top, hi);
865 if (tempZ != RT_NO_OPENING)
866 return tempZ;
867 }
868 }
869 *foundLow = *foundHigh = hi;
870 return RT_NO_OPENING;
871 }
872
873
874 /**
875 * @brief Performs traces to find a passage between two points given an upper and lower bound.
876 * @param[in] rtd The essential routing data with map, actorsize, ents
877 * @param[in] from Starting place
878 * @param[in] ax Ending x coordinate
879 * @param[in] ay Ending y coordinate
880 * @param[in] bottom Actual height of the starting floor.
881 * @param[in] top Actual height of the starting ceiling.
882 * @param[out] foundLow Actual height of the bottom of the found passage.
883 * @param[out] foundHigh Actual height of the top of the found passage.
884 * @return The new z value of the actor after traveling in this direction from the starting location.
885 */
RT_FindOpening(RoutingData * rtd,const place_t * from,const int ax,const int ay,const int bottom,const int top,int * foundLow,int * foundHigh)886 static int RT_FindOpening (RoutingData *rtd, const place_t* from, const int ax, const int ay, const int bottom, const int top, int* foundLow, int* foundHigh)
887 {
888 vec3_t start, end;
889 pos3_t pos;
890 int tempZ;
891
892 if (bottom == -1) {
893 /* Bailing- no floor in current cell. */
894 *foundLow = *foundHigh = 0;
895 return RT_NO_OPENING;
896 }
897
898 /* Initialize the starting vector */
899 SizedPosToVec(from->cell, rtd->actorSize, start);
900
901 /* Initialize the ending vector */
902 VectorSet(pos, ax, ay, from->cell[2]);
903 SizedPosToVec(pos, rtd->actorSize, end);
904
905 /* Initialize the z component of both vectors */
906 start[2] = end[2] = 0;
907
908 /* ----- sky trace ------ */
909 /* shortcut: if both ceilings are the sky, we can check for walls
910 * AND determine the bottom of the passage in just one trace */
911 if (from->ceiling >= PATHFINDING_HEIGHT * CELL_HEIGHT
912 && from->cell[2] * CELL_HEIGHT + rtd->routing.getCeiling(rtd->actorSize, ax, ay, from->cell[2]) >= PATHFINDING_HEIGHT * CELL_HEIGHT) {
913 vec3_t sky, earth;
914 const AABB* box = (rtd->actorSize == ACTOR_SIZE_NORMAL ? &actor1x1Box : &actor2x2Box);
915 int tempBottom;
916
917 VectorInterpolation(start, end, 0.5, sky); /* center it halfway between the cells */
918 VectorCopy(sky, earth);
919 sky[2] = UNIT_HEIGHT * PATHFINDING_HEIGHT; /* Set to top of model. */
920 earth[2] = QuantToModel(bottom);
921
922 const trace_t tr = RT_COMPLETEBOXTRACE_PASSAGE(rtd->mapTiles, Line(sky, earth), box, rtd->list);
923 tempBottom = ModelFloorToQuant(tr.endpos[2]);
924 if (tempBottom <= bottom + PATHFINDING_MIN_STEPUP) {
925 const int hi = bottom + PATHFINDING_MIN_OPENING;
926 /* Found opening with sky trace. */
927 *foundLow = tempBottom;
928 *foundHigh = CELL_HEIGHT * PATHFINDING_HEIGHT;
929 return RT_CalcNewZ(rtd, ax, ay, top, hi);
930 }
931 }
932 /* Warning: never try to make this an 'else if', or 'arched entry' situations will fail !! */
933
934 /* ----- guaranteed opening trace ------ */
935 /* Now calculate the "guaranteed" opening, if any. If the opening from
936 * the floor to the ceiling is not too tall, there must be a section that
937 * will always be vacant if there is a usable passage of any size and at
938 * any height. */
939 if (top - bottom < PATHFINDING_MIN_OPENING * 2) {
940 const int lo = top - PATHFINDING_MIN_OPENING;
941 const int hi = bottom + PATHFINDING_MIN_OPENING;
942
943 tempZ = RT_TraceOpening(rtd, start, end, ax, ay, bottom, top, lo, hi, foundLow, foundHigh);
944 } else {
945 /* ----- brute force trace ------ */
946 /* There is no "guaranteed" opening, brute force search. */
947 int lo = bottom;
948 tempZ = 0;
949 while (lo <= top - PATHFINDING_MIN_OPENING) {
950 /* Check for a 1 QUANT opening. */
951 tempZ = RT_TraceOpening(rtd, start, end, ax, ay, bottom, top, lo, lo + 1, foundLow, foundHigh);
952 if (tempZ != RT_NO_OPENING)
953 break;
954 /* Credit to Duke: We skip the minimum opening, as if there is a
955 * viable opening, even one slice above, that opening would be open. */
956 lo += PATHFINDING_MIN_OPENING;
957 }
958 }
959 return tempZ;
960 }
961
962
963 /**
964 * @brief Performs small traces to find places when an actor can step up.
965 * @param[in] rtd The essential routing data with map, actorsize, ents
966 * @param[in] from Starting place
967 * @param[in] ax Ending x coordinate
968 * @param[in] ay Ending y coordinate
969 * @param[in] az Ending z coordinate
970 * @param[in] stairwaySituation whether we are standing in front of a stairway
971 * @param[out] opening descriptor of the opening found, if any
972 * @return The change in floor height in QUANT units because of the additional trace.
973 */
RT_MicroTrace(RoutingData * rtd,const place_t * from,const int ax,const int ay,const int az,const int stairwaySituation,opening_t * opening)974 static int RT_MicroTrace (RoutingData *rtd, const place_t* from, const int ax, const int ay, const int az, const int stairwaySituation, opening_t* opening)
975 {
976 /* OK, now we have a viable shot across. Run microstep tests now. */
977 /* Now calculate the stepup at the floor using microsteps. */
978 int top = opening->base + opening->size;
979 signed char bases[UNIT_SIZE / PATHFINDING_MICROSTEP_SIZE + 1];
980 float sx, sy, ex, ey;
981 /* Shortcut the value of UNIT_SIZE / PATHFINDING_MICROSTEP_SIZE. */
982 const int steps = UNIT_SIZE / PATHFINDING_MICROSTEP_SIZE;
983 int i, current_h, highest_h, highest_i = 0, skipped, newBottom;
984 vec3_t start, end;
985 pos3_t pos;
986
987 /* First prepare the two known end values. */
988 bases[0] = from->floor;
989 const int floorVal = rtd->routing.getFloor(rtd->actorSize, ax, ay, az);
990 bases[steps] = std::max(0, floorVal) + az * CELL_HEIGHT;
991
992 /* Initialize the starting vector */
993 SizedPosToVec(from->cell, rtd->actorSize, start);
994
995 /* Initialize the ending vector */
996 VectorSet(pos, ax, ay, az);
997 SizedPosToVec(pos, rtd->actorSize, end);
998
999 /* Now prep the z values for start and end. */
1000 start[2] = QuantToModel(opening->base) + 1; /**< Just above the bottom of the found passage */
1001 end[2] = -QUANT;
1002
1003 /* Memorize the start and end x,y points */
1004 sx = start[0];
1005 sy = start[1];
1006 ex = end[0];
1007 ey = end[1];
1008
1009 newBottom = std::max(bases[0], bases[steps]);
1010 /* Now calculate the rest of the microheights. */
1011 for (i = 1; i < steps; i++) {
1012 start[0] = end[0] = sx + (ex - sx) * (i / (float)steps);
1013 start[1] = end[1] = sy + (ey - sy) * (i / (float)steps);
1014
1015 /* perform the trace, then return true if the trace was obstructed. */
1016 const trace_t tr = RT_COMPLETEBOXTRACE_PASSAGE(rtd->mapTiles, Line(start, end), &footBox, rtd->list);
1017 if (tr.fraction >= 1.0) {
1018 bases[i] = -1;
1019 } else {
1020 bases[i] = ModelFloorToQuant(tr.endpos[2] - DIST_EPSILON);
1021 /* Walking through glass fix:
1022 * It is possible to have an obstruction that can be skirted around diagonally
1023 * because the microtraces are so tiny. But, we have a full size trace in opening->base
1024 * that apporoximates where legroom ends. If the found floor of the middle microtrace is
1025 * too low, then set it to the worst case scenario floor based on base->opening.
1026 */
1027 if (i == floor(steps / 2.0) && bases[i] < opening->base - PATHFINDING_MIN_STEPUP) {
1028 if (debugTrace)
1029 Com_Printf("Adjusting middle trace- the known base is too high. \n");
1030 bases[i] = opening->base - PATHFINDING_MIN_STEPUP;
1031 }
1032 }
1033
1034 if (debugTrace)
1035 Com_Printf("Microstep %i from (%f, %f, %f) to (%f, %f, %f) = %i [%f]\n",
1036 i, start[0], start[1], start[2], end[0], end[1], end[2], bases[i], tr.endpos[2]);\
1037
1038 newBottom = std::max(newBottom, (int)bases[i]);
1039 }
1040
1041 if (debugTrace)
1042 Com_Printf("z:%i az:%i bottom:%i new_bottom:%i top:%i bases[0]:%i bases[%i]:%i\n", from->cell[2], az, opening->base, newBottom, top, bases[0], steps, bases[steps]);
1043
1044
1045 /** @note This for loop is bi-directional: i may be decremented to retrace prior steps. */
1046 /* Now find the maximum stepup moving from (x, y) to (ax, ay). */
1047 /* Initialize stepup. */
1048 current_h = bases[0];
1049 highest_h = -1;
1050 highest_i = 1;
1051 opening->stepup = 0; /**< Was originally -CELL_HEIGHT, but stepup is needed to go UP, not down. */
1052 skipped = 0;
1053 for (i = 1; i <= steps; i++) {
1054 if (debugTrace)
1055 Com_Printf("Tracing forward i:%i h:%i\n", i, current_h);
1056 /* If there is a rise, use it. */
1057 if (bases[i] >= current_h || ++skipped > PATHFINDING_MICROSTEP_SKIP) {
1058 if (skipped == PATHFINDING_MICROSTEP_SKIP) {
1059 i = highest_i;
1060 if (debugTrace)
1061 Com_Printf(" Skipped too many steps, reverting to i:%i\n", i);
1062 }
1063 opening->stepup = std::max(opening->stepup, bases[i] - current_h);
1064 current_h = bases[i];
1065 highest_h = -2;
1066 highest_i = i + 1;
1067 skipped = 0;
1068 if (debugTrace)
1069 Com_Printf(" Advancing b:%i stepup:%i\n", bases[i], opening->stepup);
1070 } else {
1071 /* We are skipping this step in case the actor can step over this lower step. */
1072 /* Record the step in case it is the highest of the low steps. */
1073 if (bases[i] > highest_h) {
1074 highest_h = bases[i];
1075 highest_i = i;
1076 }
1077 if (debugTrace)
1078 Com_Printf(" Skipped because we are falling, skip:%i.\n", skipped);
1079 /* If this is the last iteration, make sure we go back and get our last stepup tests. */
1080 if (i == steps) {
1081 skipped = PATHFINDING_MICROSTEP_SKIP;
1082 i = highest_i - 1;
1083 if (debugTrace)
1084 Com_Printf(" Tripping skip counter to perform last tests.\n");
1085 }
1086 }
1087 }
1088
1089 /** @note This for loop is bi-directional: i may be decremented to retrace prior steps. */
1090 /* Now find the maximum stepup moving from (x, y) to (ax, ay). */
1091 /* Initialize stepup. */
1092 current_h = bases[steps];
1093 highest_h = -1;
1094 highest_i = steps - 1; /**< Note that for this part of the code, this is the LOWEST i. */
1095 opening->invstepup = 0; /**< Was originally -CELL_HEIGHT, but stepup is needed to go UP, not down. */
1096 skipped = 0;
1097 for (i = steps - 1; i >= 0; i--) {
1098 if (debugTrace)
1099 Com_Printf("Tracing backward i:%i h:%i\n", i, current_h);
1100 /* If there is a rise, use it. */
1101 if (bases[i] >= current_h || ++skipped > PATHFINDING_MICROSTEP_SKIP) {
1102 if (skipped == PATHFINDING_MICROSTEP_SKIP) {
1103 i = highest_i;
1104 if (debugTrace)
1105 Com_Printf(" Skipped too many steps, reverting to i:%i\n", i);
1106 }
1107 opening->invstepup = std::max(opening->invstepup, bases[i] - current_h);
1108 current_h = bases[i];
1109 highest_h = -2;
1110 highest_i = i - 1;
1111 skipped = 0;
1112 if (debugTrace)
1113 Com_Printf(" Advancing b:%i stepup:%i\n", bases[i], opening->invstepup);
1114 } else {
1115 /* We are skipping this step in case the actor can step over this lower step. */
1116 /* Record the step in case it is the highest of the low steps. */
1117 if (bases[i] > highest_h) {
1118 highest_h = bases[i];
1119 highest_i = i;
1120 }
1121 if (debugTrace)
1122 Com_Printf(" Skipped because we are falling, skip:%i.\n", skipped);
1123 /* If this is the last iteration, make sure we go back and get our last stepup tests. */
1124 if (i == 0) {
1125 skipped = PATHFINDING_MICROSTEP_SKIP;
1126 i = highest_i + 1;
1127 if (debugTrace)
1128 Com_Printf(" Tripping skip counter to perform last tests.\n");
1129 }
1130 }
1131 }
1132
1133 if (stairwaySituation) {
1134 const int middle = bases[4]; /* terrible hack by Duke. This relies on PATHFINDING_MICROSTEP_SIZE being set to 4 !! */
1135
1136 if (stairwaySituation == 1) { /* stepping up */
1137 if (bases[1] <= middle && /* if nothing in the 1st part of the passage is higher than what's at the border */
1138 bases[2] <= middle &&
1139 bases[3] <= middle ) {
1140 if (debugTrace)
1141 Com_Printf("Addition granted by ugly stair hack-stepping up.\n");
1142 return opening->base - middle;
1143 }
1144 } else if (stairwaySituation == 2) {/* stepping down */
1145 if (bases[5] <= middle && /* same for the 2nd part of the passage */
1146 bases[6] <= middle &&
1147 bases[7] <= middle )
1148 if (debugTrace)
1149 Com_Printf("Addition granted by ugly stair hack-stepping down.\n");
1150 return opening->base - middle;
1151 }
1152 }
1153
1154 /* Return the confirmed passage opening. */
1155 return opening->base - newBottom;
1156 }
1157
1158
1159 /**
1160 * @brief Performs traces to find a passage between two points given an upper and lower bound.
1161 * @param[in] rtd The essential routing data with map, actorsize, ents
1162 * @param[in] from Starting place
1163 * @param[in] to Ending place
1164 * @param[out] opening descriptor of the opening found, if any
1165 * @return The size in QUANT units of the detected opening.
1166 */
RT_TraceOnePassage(RoutingData * rtd,const place_t * from,const place_t * to,opening_t * opening)1167 static int RT_TraceOnePassage (RoutingData *rtd, const place_t* from, const place_t* to, opening_t* opening)
1168 {
1169 int hi; /**< absolute ceiling of the passage found. */
1170 const int z = from->cell[2];
1171 int az; /**< z height of the actor after moving in this direction. */
1172 const int lower = std::max(from->floor, to->floor);
1173 const int upper = std::min(from->ceiling, to->ceiling);
1174 const int ax = to->cell[0];
1175 const int ay = to->cell[1];
1176
1177 RT_FindOpening(rtd, from, ax, ay, lower, upper, &opening->base, &hi);
1178 /* calc opening found so far and set stepup */
1179 opening->size = hi - opening->base;
1180 az = to->floorZ;
1181
1182 /* We subtract MIN_STEPUP because that is foot space-
1183 * the opening there only needs to be the microtrace
1184 * wide and not the usual dimensions.
1185 */
1186 if (az != RT_NO_OPENING && opening->size >= PATHFINDING_MIN_OPENING - PATHFINDING_MIN_STEPUP) {
1187 const int srcFloor = from->floor;
1188 const int dstFloor = rtd->routing.getFloor(rtd->actorSize, ax, ay, az) + az * CELL_HEIGHT;
1189 /* if we already have enough headroom, try to skip microtracing */
1190 if (opening->size < ACTOR_MAX_HEIGHT
1191 || abs(srcFloor - opening->base) > PATHFINDING_MIN_STEPUP
1192 || abs(dstFloor - opening->base) > PATHFINDING_MIN_STEPUP) {
1193 int stairway = RT_PlaceIsShifted(from, to);
1194 /* This returns the total opening height, as the
1195 * microtrace may reveal more passage height from the foot space. */
1196 const int bonusSize = RT_MicroTrace(rtd, from, ax, ay, az, stairway, opening);
1197 opening->base -= bonusSize;
1198 opening->size = hi - opening->base; /* re-calculate */
1199 } else {
1200 /* Skipping microtracing, just set the stepup values. */
1201 opening->stepup = std::max(0, opening->base - srcFloor);
1202 opening->invstepup = std::max(0, opening->base - dstFloor);
1203 }
1204
1205 /* Now place an upper bound on stepup */
1206 if (opening->stepup > PATHFINDING_MAX_STEPUP) {
1207 opening->stepup = PATHFINDING_NO_STEPUP;
1208 } else {
1209 /* Add rise/fall bit as needed. */
1210 if (az < z && opening->invstepup <= PATHFINDING_MAX_STEPUP)
1211 /* BIG_STEPDOWN indicates 'walking down', don't set it if we're 'falling' */
1212 opening->stepup |= PATHFINDING_BIG_STEPDOWN;
1213 else if (az > z)
1214 opening->stepup |= PATHFINDING_BIG_STEPUP;
1215 }
1216
1217 /* Now place an upper bound on stepup */
1218 if (opening->invstepup > PATHFINDING_MAX_STEPUP) {
1219 opening->invstepup = PATHFINDING_NO_STEPUP;
1220 } else {
1221 /* Add rise/fall bit as needed. */
1222 if (az > z)
1223 opening->invstepup |= PATHFINDING_BIG_STEPDOWN;
1224 else if (az < z)
1225 opening->invstepup |= PATHFINDING_BIG_STEPUP;
1226 }
1227
1228 if (opening->size >= PATHFINDING_MIN_OPENING) {
1229 return opening->size;
1230 }
1231 }
1232
1233 if (debugTrace)
1234 Com_Printf(" No opening found.\n");
1235 opening->stepup = PATHFINDING_NO_STEPUP;
1236 opening->invstepup = PATHFINDING_NO_STEPUP;
1237 return 0;
1238 }
1239
1240 /**
1241 * @brief Performs traces to find a passage between two points.
1242 * @param[in] rtd The essential routing data with map, actorsize, ents
1243 * @param[in] x Starting x coordinate
1244 * @param[in] y Starting y coordinate
1245 * @param[in] z Starting z coordinate
1246 * @param[in] ax Ending x coordinate
1247 * @param[in] ay Ending y coordinate
1248 * @param[out] opening descriptor of the opening found, if any
1249 */
RT_TracePassage(RoutingData * rtd,const int x,const int y,const int z,const int ax,const int ay,opening_t * opening)1250 static void RT_TracePassage (RoutingData *rtd, const int x, const int y, const int z, const int ax, const int ay, opening_t* opening)
1251 {
1252 int aboveCeil, lowCeil;
1253 /** we don't need the cell below the adjacent cell because we should have already checked it */
1254 place_t from, to, above;
1255 const place_t* placeToCheck = nullptr;
1256
1257 RT_PlaceInit(rtd->routing, rtd->actorSize, &from, x, y, z);
1258 RT_PlaceInit(rtd->routing, rtd->actorSize, &to, ax, ay, z);
1259
1260 aboveCeil = (z < PATHFINDING_HEIGHT - 1) ? rtd->routing.getCeiling(rtd->actorSize, ax, ay, z + 1) + (z + 1) * CELL_HEIGHT : to.ceiling;
1261 lowCeil = std::min(from.ceiling, (rtd->routing.getCeiling(rtd->actorSize, ax, ay, z) == 0 || to.ceiling - from.floor < PATHFINDING_MIN_OPENING) ? aboveCeil : to.ceiling);
1262
1263 /*
1264 * First check the ceiling for the cell beneath the adjacent floor to see
1265 * if there is a potential opening. The difference between the
1266 * ceiling and the floor is at least PATHFINDING_MIN_OPENING tall, then
1267 * scan it to see if we can use it. If we can, then one of two things
1268 * will happen:
1269 * - The actual adjacent cell has no floor of its own, and we will walk
1270 * or fall into the cell below the adjacent cell anyway.
1271 * - There is a floor in the adjacent cell, but we will not be able to
1272 * walk into it anyway because there cannot be any steps if there is
1273 * a passage. An actor can walk down into the cell ONLY IF it's
1274 * negative stepup meets or exceeds the change in floor height.
1275 * No actors will be allowed to fall because they cannot temporarily
1276 * occupy the space beneath the floor in the adjacent cell to fall
1277 * (all actors in the cell must be ON TOP of the floor in the cell).
1278 * If there is no passage, then the obstruction may be used as steps to
1279 * climb up to the adjacent floor.
1280 */
1281 if (to.isUsable() && RT_PlaceDoesIntersectEnough(&from, &to)) {
1282 placeToCheck = &to;
1283 } else if (z < PATHFINDING_HEIGHT - 1) {
1284 RT_PlaceInit(rtd->routing, rtd->actorSize, &above, ax, ay, z + 1);
1285 if (above.isUsable() && RT_PlaceDoesIntersectEnough(&from, &above)) {
1286 placeToCheck = &above;
1287 }
1288 }
1289 if (!placeToCheck) {
1290 if (debugTrace)
1291 Com_Printf(" No opening found. c:%i lc:%i.\n", from.ceiling, lowCeil);
1292 /* If we got here, then there is no opening from floor to ceiling. */
1293 opening->stepup = PATHFINDING_NO_STEPUP;
1294 opening->invstepup = PATHFINDING_NO_STEPUP;
1295 opening->base = lowCeil;
1296 opening->size = 0;
1297 return;
1298 }
1299
1300 /*
1301 * Now that we got here, we know that either the opening between the
1302 * ceiling below the adjacent cell and the current floor is too small or
1303 * obstructed. Try to move onto the adjacent floor.
1304 */
1305 if (debugTrace)
1306 Com_Printf(" Testing up c:%i lc:%i.\n", from.ceiling, lowCeil);
1307
1308 RT_TraceOnePassage(rtd, &from, placeToCheck, opening);
1309 if (opening->size < PATHFINDING_MIN_OPENING) {
1310 if (debugTrace)
1311 Com_Printf(" No opening found.\n");
1312 /* If we got here, then there is no useable opening from floor to ceiling. */
1313 opening->stepup = PATHFINDING_NO_STEPUP;
1314 opening->invstepup = PATHFINDING_NO_STEPUP;
1315 opening->base = lowCeil;
1316 opening->size = 0;
1317 }
1318 }
1319
1320
1321 /**
1322 * @brief Routing Function to update the connection between two fields
1323 * @param[in] rtd The essential routing data with map, actorsize, ents
1324 * @param[in] x The x position in the routing arrays (0 to PATHFINDING_WIDTH - actorSize)
1325 * @param[in] y The y position in the routing arrays (0 to PATHFINDING_WIDTH - actorSize)
1326 * @param[in] ax The x of the adjacent cell
1327 * @param[in] ay The y of the adjacent cell
1328 * @param[in] z The z position in the routing arrays (0 to PATHFINDING_HEIGHT - 1)
1329 * @param[in] dir The direction to test for a connection through
1330 */
RT_UpdateConnection(RoutingData * rtd,const int x,const int y,const int ax,const int ay,const int z,const int dir)1331 static int RT_UpdateConnection (RoutingData *rtd, const int x, const int y, const int ax, const int ay, const int z, const int dir)
1332 {
1333 const int ceiling = rtd->routing.getCeiling(rtd->actorSize, x, y, z);
1334 const int adjCeiling = rtd->routing.getCeiling(rtd->actorSize, ax, ay, z);
1335 const int extAdjCeiling = (z < PATHFINDING_HEIGHT - 1) ? rtd->routing.getCeiling(rtd->actorSize, ax, ay, z + 1) : adjCeiling;
1336 const int absCeiling = ceiling + z * CELL_HEIGHT;
1337 const int absAdjCeiling = adjCeiling + z * CELL_HEIGHT;
1338 const int absExtAdjCeiling = (z < PATHFINDING_HEIGHT - 1) ? adjCeiling + (z + 1) * CELL_HEIGHT : absCeiling;
1339 const int absFloor = rtd->routing.getFloor(rtd->actorSize, x, y, z) + z * CELL_HEIGHT;
1340 const int absAdjFloor = rtd->routing.getFloor(rtd->actorSize, ax, ay, z) + z * CELL_HEIGHT;
1341 opening_t opening; /** the opening between the two cells */
1342 int new_z1, az = z;
1343 #if RT_IS_BIDIRECTIONAL == 1
1344 int new_z2;
1345 #endif
1346
1347 if (debugTrace)
1348 Com_Printf("\n(%i, %i, %i) to (%i, %i, %i) as:%i\n", x, y, z, ax, ay, z, rtd->actorSize);
1349
1350 /** test if the adjacent cell and the cell above it are blocked by a loaded model */
1351 if (adjCeiling == 0 && (extAdjCeiling == 0 || ceiling == 0)) {
1352 /* We can't go this way. */
1353 RT_ConnSetNoGo(rtd, x, y, z, dir);
1354 #if RT_IS_BIDIRECTIONAL == 1
1355 RT_ConnSetNoGo(rtd, ax, ay, z, dir ^ 1);
1356 #endif
1357 if (debugTrace)
1358 Com_Printf("Current cell filled. c:%i ac:%i\n", rtd->routing.getCeiling(rtd->actorSize, x, y, z), rtd->routing.getCeiling(rtd->actorSize, ax, ay, z));
1359 return z;
1360 }
1361
1362 #if RT_IS_BIDIRECTIONAL == 1
1363 /** In case the adjacent floor has no ceiling, swap the current and adjacent cells. */
1364 if (ceiling == 0 && adjCeiling != 0) {
1365 return RT_UpdateConnection(rtd, ax, ay, x, y, z, dir ^ 1);
1366 }
1367 #endif
1368
1369 /**
1370 * @note OK, simple test here. We know both cells have a ceiling, so they are both open.
1371 * If the absolute ceiling of one is below the absolute floor of the other, then there is no intersection.
1372 */
1373 if (absCeiling < absAdjFloor || absExtAdjCeiling < absFloor) {
1374 /* We can't go this way. */
1375 RT_ConnSetNoGo(rtd, x, y, z, dir);
1376 #if RT_IS_BIDIRECTIONAL == 1
1377 RT_ConnSetNoGo(rtd, ax, ay, z, dir ^ 1);
1378 #endif
1379 if (debugTrace)
1380 Com_Printf("Ceiling lower than floor. f:%i c:%i af:%i ac:%i\n", absFloor, absCeiling, absAdjFloor, absAdjCeiling);
1381 return z;
1382 }
1383
1384 /** Find an opening. */
1385 RT_TracePassage(rtd, x, y, z, ax, ay, &opening);
1386 if (debugTrace) {
1387 Com_Printf("Final STEPUP for (%i, %i, %i) as:%i dir:%i = %i\n", x, y, z, rtd->actorSize, dir, opening.stepup);
1388 }
1389 /** Apply the data to the routing table.
1390 * We always call the fill function. If the passage cannot be traveled, the
1391 * function fills it in as unpassable. */
1392 new_z1 = RT_FillPassageData(rtd, dir, x, y, z, opening.size, opening.base, opening.stepup);
1393
1394 if (opening.stepup & PATHFINDING_BIG_STEPUP) {
1395 /* ^ 1 reverses the direction of dir */
1396 #if RT_IS_BIDIRECTIONAL == 1
1397 RT_ConnSetNoGo(rtd, ax, ay, z, dir ^ 1);
1398 #endif
1399 az++;
1400 } else if (opening.stepup & PATHFINDING_BIG_STEPDOWN) {
1401 az--;
1402 }
1403 #if RT_IS_BIDIRECTIONAL == 1
1404 new_z2 = RT_FillPassageData(rtd, dir ^ 1, ax, ay, az, opening.size, opening.base, opening.invstepup);
1405 if (new_z2 == az && az < z)
1406 new_z2++;
1407 return std::min(new_z1, new_z2);
1408 #else
1409 return new_z1;
1410 #endif
1411 }
1412
1413
1414 /**
1415 * @brief Routing Function to update the connection between two fields
1416 * @param[in] mapTiles List of tiles the current (RMA-)map is composed of
1417 * @param[in] routing Routing table of the current loaded map
1418 * @param[in] actorSize The size of the actor, in units
1419 * @param[in] x The x position in the routing arrays (0 to PATHFINDING_WIDTH - actorSize)
1420 * @param[in] y The y position in the routing arrays (0 to PATHFINDING_WIDTH - actorSize)
1421 * @param[in] dir The direction to test for a connection through
1422 * @param[in] list The local models list (a local model has a name starting with * followed by the model number)
1423 */
RT_UpdateConnectionColumn(mapTiles_t * mapTiles,Routing & routing,const int actorSize,const int x,const int y,const int dir,const char ** list)1424 void RT_UpdateConnectionColumn (mapTiles_t* mapTiles, Routing &routing, const int actorSize, const int x, const int y, const int dir, const char** list)
1425 {
1426 int z = 0; /**< The current z value that we are testing. */
1427 /* the essential data passed down the calltree */
1428 RoutingData rtd(mapTiles, routing, actorSize, list);
1429
1430 /* get the neighbor cell's coordinates */
1431 const int ax = x + dvecs[dir][0];
1432 const int ay = y + dvecs[dir][1];
1433
1434 assert(actorSize > ACTOR_SIZE_INVALID && actorSize <= ACTOR_MAX_SIZE);
1435 assert((x >= 0) && (x <= PATHFINDING_WIDTH - actorSize));
1436 assert((y >= 0) && (y <= PATHFINDING_WIDTH - actorSize));
1437
1438 #ifdef DEBUG
1439 /** @todo remove me */
1440 /* just a place to place a breakpoint */
1441 if (x == 126 && y == 129 && dir == 2) {
1442 z = 7;
1443 }
1444 #endif
1445
1446 /* if our destination cell is out of bounds, bail. */
1447 if (ax < 0 || ax > PATHFINDING_WIDTH - actorSize || ay < 0 || y > PATHFINDING_WIDTH - actorSize) {
1448 /* We can't go this way. */
1449 RT_ConnSetNoGo(&rtd, x, y, z, dir);
1450 /* There is only one entry here: There is no inverse cell to store data for. */
1451 if (debugTrace)
1452 Com_Printf("Destination cell non-existant.\n");
1453 return;
1454 }
1455
1456 /* Main loop */
1457 for (z = 0; z < PATHFINDING_HEIGHT; z++) {
1458 /* The last z value processed by the tracing function. */
1459 const int new_z = RT_UpdateConnection(&rtd, x, y, ax, ay, z, dir);
1460 assert(new_z >= z);
1461 z = new_z;
1462 }
1463 }
1464
RT_WriteCSVFiles(const Routing & routing,const char * baseFilename,const ipos3_t mins,const ipos3_t maxs)1465 void RT_WriteCSVFiles (const Routing &routing, const char* baseFilename, const ipos3_t mins, const ipos3_t maxs)
1466 {
1467 char filename[MAX_OSPATH], ext[MAX_OSPATH];
1468 int x, y, z;
1469
1470 /* An elevation files- dumps the floor and ceiling levels relative to each cell. */
1471 for (int i = 1; i <= ACTOR_MAX_SIZE; i++) {
1472 strncpy(filename, baseFilename, sizeof(filename) - 1);
1473 sprintf(ext, ".%i.elevation.csv", i);
1474 Com_DefaultExtension(filename, sizeof(filename), ext);
1475 ScopedFile f;
1476 FS_OpenFile(filename, &f, FILE_WRITE);
1477 if (!f)
1478 Sys_Error("Could not open file %s.", filename);
1479 FS_Printf(&f, ",");
1480 for (x = mins[0]; x <= maxs[0] - i + 1; x++)
1481 FS_Printf(&f, "x:%i,", x);
1482 FS_Printf(&f, "\n");
1483 for (z = maxs[2]; z >= mins[2]; z--) {
1484 for (y = maxs[1]; y >= mins[1] - i + 1; y--) {
1485 FS_Printf(&f, "z:%i y:%i,", z ,y);
1486 for (x = mins[0]; x <= maxs[0] - i + 1; x++) {
1487 /* compare results */
1488 FS_Printf(&f, "h:%i c:%i,", routing.getFloor(i, x, y, z), routing.getCeiling(i, x, y, z));
1489 }
1490 FS_Printf(&f, "\n");
1491 }
1492 FS_Printf(&f, "\n");
1493 }
1494 }
1495
1496 /* Output the walls/passage files. */
1497 for (int i = 1; i <= ACTOR_MAX_SIZE; i++) {
1498 strncpy(filename, baseFilename, sizeof(filename) - 1);
1499 sprintf(ext, ".%i.walls.csv", i);
1500 Com_DefaultExtension(filename, sizeof(filename), ext);
1501 ScopedFile f;
1502 FS_OpenFile(filename, &f, FILE_WRITE);
1503 if (!f)
1504 Sys_Error("Could not open file %s.", filename);
1505 FS_Printf(&f, ",");
1506 for (x = mins[0]; x <= maxs[0] - i + 1; x++)
1507 FS_Printf(&f, "x:%i,", x);
1508 FS_Printf(&f, "\n");
1509 for (z = maxs[2]; z >= mins[2]; z--) {
1510 for (y = maxs[1]; y >= mins[1] - i + 1; y--) {
1511 FS_Printf(&f, "z:%i y:%i,", z ,y);
1512 for (x = mins[0]; x <= maxs[0] - i + 1; x++) {
1513 /* compare results */
1514 FS_Printf(&f, "\"");
1515
1516 /* NW corner */
1517 FS_Printf(&f, "%3i-%3i ", RT_CONN_NX_PY(routing, i, x, y, z), RT_STEPUP_NX_PY(routing, i, x, y, z));
1518
1519 /* N side */
1520 FS_Printf(&f, "%3i-%3i ", RT_CONN_PY(routing, i, x, y, z), RT_STEPUP_PY(routing, i, x, y, z));
1521
1522 /* NE corner */
1523 FS_Printf(&f, "%3i-%3i ", RT_CONN_PX_PY(routing, i, x, y, z), RT_STEPUP_PX_PY(routing, i, x, y, z));
1524
1525 FS_Printf(&f, "\n");
1526
1527 /* W side */
1528 FS_Printf(&f, "%3i-%3i ", RT_CONN_NX(routing, i, x, y, z), RT_STEPUP_NX(routing, i, x, y, z));
1529
1530 /* Center - display floor height */
1531 FS_Printf(&f, "_%+2i_ ", routing.getFloor(i, x, y, z));
1532
1533 /* E side */
1534 FS_Printf(&f, "%3i-%3i ", RT_CONN_PX(routing, i, x, y, z), RT_STEPUP_PX(routing, i, x, y, z));
1535
1536 FS_Printf(&f, "\n");
1537
1538 /* SW corner */
1539 FS_Printf(&f, "%3i-%3i ", RT_CONN_NX_NY(routing, i, x, y, z), RT_STEPUP_NX_NY(routing, i, x, y, z));
1540
1541 /* S side */
1542 FS_Printf(&f, "%3i-%3i ", RT_CONN_NY(routing, i, x, y, z), RT_STEPUP_NY(routing, i, x, y, z));
1543
1544 /* SE corner */
1545 FS_Printf(&f, "%3i-%3i ", RT_CONN_PX_NY(routing, i, x, y, z), RT_STEPUP_PX_NY(routing, i, x, y, z));
1546
1547 FS_Printf(&f, "\",");
1548 }
1549 FS_Printf(&f, "\n");
1550 }
1551 FS_Printf(&f, "\n");
1552 }
1553 }
1554 }
1555
1556 #ifdef DEBUG
1557 /**
1558 * @brief A debug function to be called from CL_DebugPath_f
1559 * @param[in] mapTiles List of tiles the current (RMA-)map is composed of
1560 * @param[in] routing Routing table of the current loaded map
1561 * @param[in] actorSize The size of the actor, in units
1562 * @param[in] x The x position in the routing arrays (0 to PATHFINDING_WIDTH - actorSize)
1563 * @param[in] y The y position in the routing arrays (0 to PATHFINDING_WIDTH - actorSize)
1564 * @param[in] dir The direction to test for a connection through
1565 * @param[in] list The local models list (a local model has a name starting with * followed by the model number)
1566 */
RT_DebugSpecial(mapTiles_t * mapTiles,Routing & routing,const int actorSize,const int x,const int y,const int dir,const char ** list)1567 int RT_DebugSpecial (mapTiles_t* mapTiles, Routing &routing, const int actorSize, const int x, const int y, const int dir, const char** list)
1568 {
1569 int z = 0; /**< The current z value that we are testing. */
1570 int new_z; /**< The last z value processed by the tracing function. */
1571 RoutingData rtd(mapTiles, routing, actorSize, list); /* the essential data passed down the calltree */
1572
1573 /* get the neighbor cell's coordinates */
1574 const int ax = x + dvecs[dir][0];
1575 const int ay = y + dvecs[dir][1];
1576
1577 new_z = RT_UpdateConnection(&rtd, x, y, ax, ay, z, dir);
1578 return new_z;
1579 }
1580
1581 /**
1582 * @brief display pathfinding info to the console. Also useful to
1583 * directly use the debugger on some vital pathfinding functions.
1584 * Will probably be removed for the release.
1585 */
RT_DebugPathDisplay(Routing & routing,actorSizeEnum_t actorSize,int x,int y,int z)1586 void RT_DebugPathDisplay (Routing &routing, actorSizeEnum_t actorSize, int x, int y, int z)
1587 {
1588 Com_Printf("data at cursor XYZ(%i, %i, %i) Floor(%i) Ceiling(%i)\n", x, y, z,
1589 routing.getFloor(actorSize, x, y, z),
1590 routing.getCeiling(actorSize, x, y, z) );
1591 Com_Printf("connections ortho: (PX=%i, NX=%i, PY=%i, NY=%i))\n",
1592 RT_CONN_PX(routing, actorSize, x, y, z), /* dir = 0 */
1593 RT_CONN_NX(routing, actorSize, x, y, z), /* 1 */
1594 RT_CONN_PY(routing, actorSize, x, y, z), /* 2 */
1595 RT_CONN_NY(routing, actorSize, x, y, z) ); /* 3 */
1596 Com_Printf("connections diago: (PX_PY=%i, NX_NY=%i, NX_PY=%i, PX_NY=%i))\n",
1597 RT_CONN_PX_PY(routing, actorSize, x, y, z), /* dir = 4 */
1598 RT_CONN_NX_NY(routing, actorSize, x, y, z), /* 5 */
1599 RT_CONN_NX_PY(routing, actorSize, x, y, z), /* 6 */
1600 RT_CONN_PX_NY(routing, actorSize, x, y, z) ); /* 7 */
1601 Com_Printf("stepup ortho: (PX=%i, NX=%i, PY=%i, NY=%i))\n",
1602 RT_STEPUP_PX(routing, actorSize, x, y, z), /* dir = 0 */
1603 RT_STEPUP_NX(routing, actorSize, x, y, z), /* 1 */
1604 RT_STEPUP_PY(routing, actorSize, x, y, z), /* 2 */
1605 RT_STEPUP_NY(routing, actorSize, x, y, z) ); /* 3 */
1606 }
1607
1608 #endif
1609