UFO: Alien Invasion
Loading...
Searching...
No Matches
routing.cpp
Go to the documentation of this file.
1
4
5/*
6All original material Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8Copyright (C) 1997-2001 Id Software, Inc.
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
19See the GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25*/
26
27
28#include "bsp.h"
30#include "levels.h"
31
32static Routing Nmap;
33
36
37
41static int CheckUnit (unsigned int unitnum)
42{
43 int new_z;
44
45 /* get coordinates of that unit */
46 const int z = unitnum % PATHFINDING_HEIGHT;
47 const int y = (unitnum / PATHFINDING_HEIGHT) % PATHFINDING_WIDTH;
48 const int x = (unitnum / PATHFINDING_HEIGHT / PATHFINDING_WIDTH) % PATHFINDING_WIDTH;
49 const int actorSize = unitnum / PATHFINDING_HEIGHT / PATHFINDING_WIDTH / PATHFINDING_WIDTH;
50
51 /* test bounds - the size adjustment is needed because large actor cells occupy multiple cell units. */
52 if (x > wpMaxs[0] - actorSize || y > wpMaxs[1] - actorSize
53 || x < wpMins[0] || y < wpMins[1]) {
54 /* don't enter - outside world */
55 return 0;
56 }
57
58 /* Call the common CheckUnit function */
59 new_z = RT_CheckCell(&mapTiles, Nmap, actorSize + 1, x, y, z, nullptr);
60
61 /* new_z should never be above z. */
62 assert(new_z <= z);
63
64 /* Adjust unitnum if this check adjusted multiple cells. */
65 return new_z - z;
66}
67
72static void CheckUnitThread (unsigned int unitnum)
73{
74 const int basenum = unitnum * PATHFINDING_HEIGHT;
75 int newnum;
76 for (newnum = basenum + PATHFINDING_HEIGHT - 1; newnum >= basenum; newnum--)
77 newnum += CheckUnit(newnum);
78}
79
80
84static void CheckConnectionsThread (unsigned int unitnum)
85{
86 /* get coordinates of that unit */
87 const int numDirs = CORE_DIRECTIONS / (1 + RT_IS_BIDIRECTIONAL);
88 const int dir = (unitnum % numDirs) * (RT_IS_BIDIRECTIONAL ? 2 : 1);
89 const int y = (unitnum / numDirs) % PATHFINDING_WIDTH;
90 const int x = (unitnum / numDirs / PATHFINDING_WIDTH) % PATHFINDING_WIDTH;
91 const int actorSize = unitnum / numDirs / PATHFINDING_WIDTH / PATHFINDING_WIDTH;
92
93 /* test bounds - the size adjustment is needed because large actor cells occupy multiple cell units. */
94 if (x > wpMaxs[0] - actorSize || y > wpMaxs[1] - actorSize
95 || x < wpMins[0] || y < wpMins[1] ) {
96 /* don't enter - outside world */
97 /* Com_Printf("x%i y%i z%i dir%i size%i (%i, %i, %i) (%i, %i, %i)\n", x, y, z, dir, size, wpMins[0], wpMins[1], wpMins[2], wpMaxs[0], wpMaxs[1], wpMaxs[2]); */
98 return;
99 }
100
101 Verb_Printf(VERB_EXTRA, "%i %i %i %i (%i, %i, %i) (%i, %i, %i)\n", x, y, dir, actorSize, wpMins[0], wpMins[1], wpMins[2], wpMaxs[0], wpMaxs[1], wpMaxs[2]);
102
103 RT_UpdateConnectionColumn(&mapTiles, Nmap, actorSize + 1, x, y, dir);
104
105 /* Com_Printf("z:%i nz:%i\n", z, new_z); */
106}
107
108
115void DoRouting (void)
116{
117 int i;
118 byte* data;
119 pos3_t pos;
120
121 /* Turn on trace debugging if requested. */
122 if (config.generateDebugTrace)
123 debugTrace = true;
124
125 /* Record the current mapTiles[0] state so we can remove all CLIPS when done. */
126 PushInfo();
127
128 /* build tracing structure */
129 EmitBrushes();
130 EmitPlanes();
133
134 /* Reset the whole block of map data to 0 */
135 Nmap.init();
136
137 /* get world bounds for optimizing */
138 AABB tileBox;
139 RT_GetMapSize(&mapTiles, tileBox);
140 VecToPos(tileBox.mins, wpMins);
141 VecToPos(tileBox.maxs, wpMaxs);
142
143 /* wpMaxs is on a 32 boundary. This causes VecToPos to calculate a pos *above* wpMAxs. We have to compensate. */
144 wpMaxs[0]--;
145 wpMaxs[1]--;
146 /* Note that VecToPos already caps the z value to PATHFINDING_HEIGHT - 1 */
147 if (tileBox.maxs[2] - 1 < UNIT_HEIGHT * (PATHFINDING_HEIGHT - 1))
148 wpMaxs[2]--;
149
150 /* Verify the world extents are not lopsided. */
151 assert(wpMins[0] <= wpMaxs[0]);
152 assert(wpMins[1] <= wpMaxs[1]);
153 assert(wpMins[2] <= wpMaxs[2]);
154
155 /* scan area heights */
157
158 /* scan connections */
160
161 /* Try to shrink the world bounds along the x and y coordinates */
162 for (i = 0; i < 2; i++) { /* for x and y, but not z */
163 int j = i ^ 1; /* if i points to x, j points to y and vice versa */
164 /* Increase the mins */
165 while (wpMaxs[j] > wpMins[j]) {
166 VectorSet(pos, wpMins[0], wpMins[1], wpMaxs[2]);
167 for (pos[i] = wpMins[i]; pos[i] <= wpMaxs[i]; pos[i]++) { /* for all cells in an x or y row */
168 if (Nmap.getFloor(1, pos[0], pos[1], wpMaxs[2]) + wpMaxs[2] * CELL_HEIGHT != -1) /* no floor ? */
169 break;
170 }
171 if (pos[i] <= wpMaxs[i]) /* found a floor before the end of the row ? */
172 break; /* break while */
173 wpMins[j]++; /* if it was an x-row, increase y-value of mins and vice versa */
174 }
175 /* Decrease the maxs */
176 while (wpMaxs[j] > wpMins[j]) {
177 VectorCopy(wpMaxs, pos);
178 for (pos[i] = wpMins[i]; pos[i] <= wpMaxs[i]; pos[i]++) {
179 if (Nmap.getFloor(1, pos[0], pos[1], wpMaxs[2]) + wpMaxs[2] * CELL_HEIGHT != -1)
180 break;
181 }
182 if (pos[i] <= wpMaxs[i])
183 break;
184 wpMaxs[j]--;
185 }
186 }
187
188 /* Output the floor trace file if set */
189 if (config.generateTraceFile) {
191 }
192
193 /* store the data */
194 data = curTile->routedata;
195 for (i = 0; i < 3; i++)
197 data = CompressRouting((byte*)wpMins, data, sizeof(wpMins));
198 for (i = 0; i < 3; i++)
200 data = CompressRouting((byte*)wpMaxs, data, sizeof(wpMaxs));
201 data = CompressRouting((byte*)&Nmap, data, sizeof(Nmap));
202
203 curTile->routedatasize = data - curTile->routedata;
204
205 /* Ensure that we did not exceed our allotment of memory for this data. */
206 assert(curTile->routedatasize <= MAX_MAP_ROUTING);
207
208 /* Remove the CLIPS fom the tracing structure by resetting it. */
209 PopInfo();
210}
void EmitBrushes(void)
Writes the brush list to the bsp.
Definition writebsp.cpp:236
void EmitPlanes(void)
Emits planes to the bsp file.
Definition writebsp.cpp:41
void MakeTracingNodes(int levels)
Use the bsp node structure to reconstruct efficient tracing structures that are used for fast visibil...
Definition trace.cpp:38
byte * CompressRouting(byte *dataStart, byte *destStart, int l)
Compress the routing data of a map.
Definition bspfile.cpp:37
#define LittleLong(X)
Definition byte.h:37
Definition aabb.h:42
vec3_t maxs
Definition aabb.h:258
vec3_t mins
Definition aabb.h:257
void RT_GetMapSize(mapTiles_t *mapTiles, AABB &mapBox)
Calculate the map size via model data and store grid size in map_min and map_max. This is done with e...
Definition routing.cpp:253
int RT_CheckCell(mapTiles_t *mapTiles, Routing &routing, const int actorSize, const int x, const int y, const int z, const char **list)
This function looks to see if an actor of a given size can occupy a cell(s) and if so identifies the ...
Definition routing.cpp:360
bool debugTrace
Definition routing.cpp:38
void RT_WriteCSVFiles(const Routing &routing, const char *baseFilename, const GridBox &box)
Definition routing.cpp:1330
void RT_UpdateConnectionColumn(mapTiles_t *mapTiles, Routing &routing, const int actorSize, const int x, const int y, const int dir, const char **list, const int minZ, const int maxZ)
Routing Function to update the connection between two fields.
Definition routing.cpp:1292
#define MAX_MAP_ROUTING
Definition defines.h:150
#define ACTOR_MAX_SIZE
Definition defines.h:305
#define PATHFINDING_WIDTH
absolute max
Definition defines.h:292
#define UNIT_HEIGHT
Definition defines.h:122
#define CELL_HEIGHT
A cell's height in QUANT sized units.
Definition defines.h:296
#define PATHFINDING_HEIGHT
15 max, adjusting above 8 will require a rewrite to the DV code
Definition defines.h:294
#define LEVEL_ACTORCLIP
Definition defines.h:352
void PopInfo(void)
Definition levels.cpp:48
void PushInfo(void)
Definition levels.cpp:33
#define CORE_DIRECTIONS
Definition mathlib.h:88
#define VecToPos(v, p)
Map boundary is +/- MAX_WORLD_WIDTH - to get into the positive area we add the possible max negative ...
Definition mathlib.h:100
QGL_EXTERN GLsizei const GLvoid * data
Definition r_gl.h:89
QGL_EXTERN GLint i
Definition r_gl.h:113
grid pathfinding and routing
#define RT_IS_BIDIRECTIONAL
Definition routing.h:40
static config_t config
Definition test_all.cpp:43
static mapTiles_t mapTiles
dMapTile_t * curTile
Definition bsp.cpp:32
void Verb_Printf(const verbosityLevel_t importance, const char *format,...) __attribute__((format(__printf__
void RunSingleThreadOn(void(*func)(unsigned int), unsigned int workcount, bool progress, const char *id)
@ VERB_NORMAL
Definition shared.h:45
@ VERB_EXTRA
Definition shared.h:46
char baseFilename[MAX_OSPATH]
Definition ufo2map.cpp:55
static void CheckConnectionsThread(unsigned int unitnum)
Definition routing.cpp:84
static ipos3_t wpMins
world min and max values converted from vec to pos
Definition routing.cpp:35
static void CheckUnitThread(unsigned int unitnum)
A wrapper for CheckUnit that is thread safe.
Definition routing.cpp:72
static int CheckUnit(unsigned int unitnum)
Definition routing.cpp:41
static ipos3_t wpMaxs
Definition routing.cpp:35
static Routing Nmap
Definition routing.cpp:32
void DoRouting(void)
Calculates the routing of a map.
Definition routing.cpp:115
pos_t pos3_t[3]
Definition ufotypes.h:58
ipos_t ipos3_t[3]
Definition ufotypes.h:70
#define VectorCopy(src, dest)
Definition vector.h:51
#define VectorSet(v, x, y, z)
Definition vector.h:59