author | truelight |
Wed, 15 Dec 2004 22:06:47 +0000 | |
changeset 678 | f62cae2abc0c |
parent 536 | 03d80fecb999 |
child 679 | 04ca2cd69420 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "pathfind.h" |
|
4 |
||
5 |
// remember which tiles we have already visited so we don't visit them again. |
|
6 |
static bool TPFSetTileBit(TrackPathFinder *tpf, uint tile, int dir) |
|
7 |
{ |
|
8 |
uint hash, val, offs; |
|
9 |
TrackPathFinderLink *link, *new_link; |
|
10 |
uint bits = 1 << dir; |
|
11 |
||
12 |
if (tpf->disable_tile_hash) |
|
13 |
return true; |
|
14 |
||
15 |
hash = PATHFIND_HASH_TILE(tile); |
|
16 |
||
17 |
val = tpf->hash_head[hash]; |
|
18 |
||
19 |
if (val == 0) { |
|
20 |
/* unused hash entry, set the appropriate bit in it and return true |
|
21 |
* to indicate that a bit was set. */ |
|
22 |
tpf->hash_head[hash] = bits; |
|
23 |
tpf->hash_tile[hash] = (TileIndex)tile; |
|
24 |
return true; |
|
25 |
} else if (!(val & 0x8000)) { |
|
26 |
/* single tile */ |
|
27 |
||
28 |
if ( (TileIndex)tile == tpf->hash_tile[hash] ) { |
|
29 |
/* found another bit for the same tile, |
|
30 |
* check if this bit is already set, if so, return false */ |
|
31 |
if (val & bits) |
|
32 |
return false; |
|
33 |
||
34 |
/* otherwise set the bit and return true to indicate that the bit |
|
35 |
* was set */ |
|
36 |
tpf->hash_head[hash] = val | bits; |
|
37 |
return true; |
|
38 |
} else { |
|
39 |
/* two tiles with the same hash, need to make a link */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
40 |
|
0 | 41 |
/* allocate a link. if out of links, handle this by returning |
42 |
* that a tile was already visisted. */ |
|
43 |
if (tpf->num_links_left == 0) |
|
44 |
return false; |
|
45 |
tpf->num_links_left--; |
|
46 |
link = tpf->new_link++; |
|
47 |
||
48 |
/* move the data that was previously in the hash_??? variables |
|
49 |
* to the link struct, and let the hash variables point to the link */ |
|
50 |
link->tile = tpf->hash_tile[hash]; |
|
51 |
tpf->hash_tile[hash] = PATHFIND_GET_LINK_OFFS(tpf, link); |
|
52 |
||
53 |
link->flags = tpf->hash_head[hash]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
54 |
tpf->hash_head[hash] = 0xFFFF; /* multi link */ |
0 | 55 |
|
56 |
link->next = 0xFFFF; |
|
57 |
} |
|
58 |
} else { |
|
59 |
/* a linked list of many tiles, |
|
60 |
* find the one corresponding to the tile, if it exists. |
|
61 |
* otherwise make a new link */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
62 |
|
0 | 63 |
offs = tpf->hash_tile[hash]; |
64 |
do { |
|
65 |
link = PATHFIND_GET_LINK_PTR(tpf, offs); |
|
66 |
if ( (TileIndex)tile == link->tile) { |
|
67 |
/* found the tile in the link list, |
|
68 |
* check if the bit was alrady set, if so return false to indicate that the |
|
69 |
* bit was already set */ |
|
70 |
if (link->flags & bits) |
|
71 |
return false; |
|
72 |
link->flags |= bits; |
|
73 |
return true; |
|
74 |
} |
|
75 |
} while ((offs=link->next) != 0xFFFF); |
|
76 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
77 |
|
0 | 78 |
/* get here if we need to add a new link to link, |
79 |
* first, allocate a new link, in the same way as before */ |
|
80 |
if (tpf->num_links_left == 0) |
|
81 |
return false; |
|
82 |
tpf->num_links_left--; |
|
83 |
new_link = tpf->new_link++; |
|
84 |
||
85 |
/* then fill the link with the new info, and establish a ptr from the old |
|
86 |
* link to the new one */ |
|
87 |
new_link->tile = (TileIndex)tile; |
|
88 |
new_link->flags = bits; |
|
89 |
new_link->next = 0xFFFF; |
|
90 |
||
91 |
link->next = PATHFIND_GET_LINK_OFFS(tpf, new_link); |
|
92 |
return true; |
|
93 |
} |
|
94 |
||
95 |
static const byte _bits_mask[4] = { |
|
96 |
0x19, |
|
97 |
0x16, |
|
98 |
0x25, |
|
99 |
0x2A, |
|
100 |
}; |
|
101 |
||
102 |
static const byte _tpf_new_direction[14] = { |
|
103 |
0,1,0,1,2,1, 0,0, |
|
104 |
2,3,3,2,3,0, |
|
105 |
}; |
|
106 |
||
107 |
static const byte _tpf_prev_direction[14] = { |
|
108 |
0,1,1,0,1,2, 0,0, |
|
109 |
2,3,2,3,0,3, |
|
110 |
}; |
|
111 |
||
112 |
||
113 |
static const byte _otherdir_mask[4] = { |
|
114 |
0x10, |
|
115 |
0, |
|
116 |
0x5, |
|
117 |
0x2A, |
|
118 |
}; |
|
119 |
||
120 |
#ifdef DEBUG_TILE_PUSH |
|
121 |
extern void dbg_push_tile(uint tile, int track); |
|
122 |
extern void dbg_pop_tile(); |
|
123 |
#endif |
|
124 |
||
410 | 125 |
static void TPFMode2(TrackPathFinder *tpf, uint tile, int direction) |
0 | 126 |
{ |
127 |
uint bits; |
|
128 |
int i; |
|
129 |
RememberData rd; |
|
130 |
||
131 |
// This addition will sometimes overflow by a single tile. |
|
132 |
// The use of TILE_MASK here makes sure that we still point at a valid |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
133 |
// tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. |
0 | 134 |
tile = TILE_MASK(tile + _tileoffs_by_dir[direction]); |
135 |
||
136 |
if (++tpf->rd.cur_length > 50) |
|
137 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
138 |
|
0 | 139 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
140 |
bits = (byte)((bits | (bits >> 8)) & _bits_mask[direction]); |
|
141 |
if (bits == 0) |
|
142 |
return; |
|
143 |
||
144 |
assert(GET_TILE_X(tile) != 255 && GET_TILE_Y(tile) != 255); |
|
145 |
||
146 |
if ( (bits & (bits - 1)) == 0 ) { |
|
147 |
/* only one direction */ |
|
148 |
i = 0; |
|
149 |
while (!(bits&1)) |
|
150 |
i++, bits>>=1; |
|
151 |
||
152 |
rd = tpf->rd; |
|
153 |
goto continue_here; |
|
154 |
} |
|
155 |
/* several directions */ |
|
156 |
i=0; |
|
157 |
do { |
|
158 |
if (!(bits & 1)) continue; |
|
159 |
rd = tpf->rd; |
|
160 |
||
161 |
// Change direction 4 times only |
|
162 |
if ((byte)i != tpf->rd.pft_var6) { |
|
163 |
if(++tpf->rd.depth > 4) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
164 |
tpf->rd = rd; |
0 | 165 |
return; |
166 |
} |
|
167 |
tpf->rd.pft_var6 = (byte)i; |
|
168 |
} |
|
169 |
||
170 |
continue_here:; |
|
171 |
tpf->the_dir = HASBIT(_otherdir_mask[direction],i) ? (i+8) : i; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
172 |
|
0 | 173 |
#ifdef DEBUG_TILE_PUSH |
174 |
dbg_push_tile(tile, tpf->the_dir); |
|
175 |
#endif |
|
176 |
if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, NULL)) { |
|
177 |
TPFMode2(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
178 |
} |
|
179 |
#ifdef DEBUG_TILE_PUSH |
|
180 |
dbg_pop_tile(); |
|
181 |
#endif |
|
182 |
||
183 |
tpf->rd = rd; |
|
184 |
} while (++i, bits>>=1); |
|
185 |
||
186 |
} |
|
187 |
||
188 |
static const int8 _get_tunlen_inc[5] = { -16, 0, 16, 0, -16 }; |
|
189 |
||
22 | 190 |
/* Returns the end tile and the length of a tunnel. The length does not |
191 |
* include the starting tile (entry), it does include the end tile (exit). |
|
192 |
*/ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
193 |
FindLengthOfTunnelResult FindLengthOfTunnel(uint tile, int direction) |
0 | 194 |
{ |
195 |
FindLengthOfTunnelResult flotr; |
|
196 |
int x,y; |
|
197 |
byte z; |
|
198 |
||
199 |
flotr.length = 0; |
|
200 |
||
201 |
x = GET_TILE_X(tile) * 16; |
|
202 |
y = GET_TILE_Y(tile) * 16; |
|
203 |
||
204 |
z = GetSlopeZ(x+8, y+8); |
|
205 |
||
206 |
for(;;) { |
|
207 |
flotr.length++; |
|
208 |
||
209 |
x += _get_tunlen_inc[direction]; |
|
210 |
y += _get_tunlen_inc[direction+1]; |
|
211 |
||
212 |
tile = TILE_FROM_XY(x,y); |
|
213 |
||
214 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && |
|
241
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
215 |
(_map5[tile] & 0xF0) == 0 && // tunnel entrance/exit |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
216 |
//((_map5[tile]>>2)&3) == type && // rail/road-tunnel <-- This is not necesary to check, right? |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
217 |
((_map5[tile] & 3)^2) == direction && // entrance towards: 0 = NE, 1 = SE, 2 = SW, 3 = NW |
0 | 218 |
GetSlopeZ(x+8, y+8) == z) |
219 |
break; |
|
220 |
} |
|
221 |
||
222 |
flotr.tile = tile; |
|
223 |
return flotr; |
|
224 |
} |
|
225 |
||
226 |
static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 }; |
|
227 |
||
228 |
static uint SkipToEndOfTunnel(TrackPathFinder *tpf, uint tile, int direction) { |
|
229 |
FindLengthOfTunnelResult flotr; |
|
230 |
TPFSetTileBit(tpf, tile, 14); |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
231 |
flotr = FindLengthOfTunnel(tile, direction); |
0 | 232 |
tpf->rd.cur_length += flotr.length; |
233 |
TPFSetTileBit(tpf, flotr.tile, 14); |
|
234 |
return flotr.tile; |
|
235 |
} |
|
236 |
||
237 |
const byte _ffb_64[128] = { |
|
238 |
0,0,1,0,2,0,1,0, |
|
239 |
3,0,1,0,2,0,1,0, |
|
240 |
4,0,1,0,2,0,1,0, |
|
241 |
3,0,1,0,2,0,1,0, |
|
242 |
5,0,1,0,2,0,1,0, |
|
243 |
3,0,1,0,2,0,1,0, |
|
244 |
4,0,1,0,2,0,1,0, |
|
245 |
3,0,1,0,2,0,1,0, |
|
246 |
||
247 |
0,0,0,2,0,4,4,6, |
|
248 |
0,8,8,10,8,12,12,14, |
|
249 |
0,16,16,18,16,20,20,22, |
|
250 |
16,24,24,26,24,28,28,30, |
|
251 |
0,32,32,34,32,36,36,38, |
|
252 |
32,40,40,42,40,44,44,46, |
|
253 |
32,48,48,50,48,52,52,54, |
|
254 |
48,56,56,58,56,60,60,62, |
|
255 |
}; |
|
256 |
||
410 | 257 |
static void TPFMode1(TrackPathFinder *tpf, uint tile, int direction) |
0 | 258 |
{ |
259 |
uint bits; |
|
260 |
int i; |
|
261 |
RememberData rd; |
|
262 |
uint tile_org = tile; |
|
263 |
||
264 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xF0)==0) { |
|
241
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
265 |
if ((_map5[tile] & 3) != direction || ((_map5[tile]>>2)&3) != tpf->tracktype) |
0 | 266 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
267 |
tile = SkipToEndOfTunnel(tpf, tile, direction); |
0 | 268 |
} |
269 |
tile += _tileoffs_by_dir[direction]; |
|
270 |
tpf->rd.cur_length++; |
|
271 |
||
272 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
|
273 |
||
274 |
if ((byte)bits != tpf->var2) { |
|
275 |
bits &= _tpfmode1_and[direction]; |
|
276 |
bits = bits | (bits>>8); |
|
277 |
} |
|
278 |
bits &= 0xBF; |
|
279 |
||
280 |
if (bits != 0) { |
|
281 |
if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KILL_FIRST_BIT(bits) == 0 || ++tpf->rd.depth <= 7))) { |
|
282 |
do { |
|
283 |
i = FIND_FIRST_BIT(bits); |
|
284 |
bits = KILL_FIRST_BIT(bits); |
|
285 |
||
286 |
tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i; |
|
287 |
rd = tpf->rd; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
288 |
|
0 | 289 |
#ifdef DEBUG_TILE_PUSH |
290 |
dbg_push_tile(tile, tpf->the_dir); |
|
291 |
#endif |
|
292 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && |
|
293 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) { |
|
294 |
TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
295 |
} |
|
296 |
#ifdef DEBUG_TILE_PUSH |
|
297 |
dbg_pop_tile(); |
|
298 |
#endif |
|
299 |
tpf->rd = rd; |
|
300 |
} while (bits != 0); |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
/* the next is only used when signals are checked. |
|
305 |
* seems to go in 2 directions simultaneously */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
306 |
|
0 | 307 |
/* if i can get rid of this, tail end recursion can be used to minimize |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
308 |
* stack space dramatically. */ |
0 | 309 |
if (tpf->hasbit_13) |
310 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
311 |
|
0 | 312 |
tile = tile_org; |
313 |
direction ^= 2; |
|
314 |
||
315 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
|
316 |
bits |= (bits >> 8); |
|
317 |
||
318 |
if ( (byte)bits != tpf->var2) { |
|
319 |
bits &= _bits_mask[direction]; |
|
320 |
} |
|
321 |
||
322 |
bits &= 0xBF; |
|
323 |
if (bits == 0) |
|
324 |
return; |
|
325 |
||
326 |
do { |
|
327 |
i = FIND_FIRST_BIT(bits); |
|
328 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
329 |
|
0 | 330 |
tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i; |
331 |
rd = tpf->rd; |
|
332 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && |
|
333 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) { |
|
334 |
TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
335 |
} |
|
336 |
tpf->rd = rd; |
|
337 |
} while (bits != 0); |
|
338 |
} |
|
339 |
||
340 |
void FollowTrack(uint tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data) |
|
341 |
{ |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
342 |
TrackPathFinder tpf; |
0 | 343 |
|
344 |
assert(direction < 4); |
|
345 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
346 |
/* initialize path finder variables */ |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
347 |
tpf.userdata = data; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
348 |
tpf.enum_proc = enum_proc; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
349 |
tpf.new_link = tpf.links; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
350 |
tpf.num_links_left = lengthof(tpf.links); |
0 | 351 |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
352 |
tpf.rd.cur_length = 0; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
353 |
tpf.rd.depth = 0; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
354 |
tpf.rd.pft_var6 = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
355 |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
356 |
tpf.var2 = HASBIT(flags, 15) ? 0x43 : 0xFF; /* 0x8000 */ |
0 | 357 |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
358 |
tpf.disable_tile_hash = HASBIT(flags, 12) != 0; /* 0x1000 */ |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
359 |
tpf.hasbit_13 = HASBIT(flags, 13) != 0; /* 0x2000 */ |
0 | 360 |
|
361 |
||
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
362 |
tpf.tracktype = (byte)flags; |
0 | 363 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
364 |
if (HASBIT(flags, 11)) { |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
365 |
tpf.rd.pft_var6 = 0xFF; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
366 |
tpf.enum_proc(tile, data, 0, 0, 0); |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
367 |
TPFMode2(&tpf, tile, direction); |
0 | 368 |
} else { |
369 |
/* clear the hash_heads */ |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
370 |
memset(tpf.hash_head, 0, sizeof(tpf.hash_head)); |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
371 |
TPFMode1(&tpf, tile, direction); |
0 | 372 |
} |
373 |
||
374 |
if (after_proc != NULL) |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
375 |
after_proc(&tpf); |
0 | 376 |
} |
377 |
||
378 |
typedef struct { |
|
379 |
TileIndex tile; |
|
380 |
uint16 cur_length; |
|
381 |
byte track; |
|
382 |
byte depth; |
|
383 |
byte state; |
|
384 |
byte first_track; |
|
385 |
} StackedItem; |
|
386 |
||
387 |
static const byte _new_dir[6][4] = { |
|
388 |
{0,0xff,2,0xff,}, |
|
389 |
{0xff,1,0xff,3,}, |
|
390 |
{0xff,0,3,0xff,}, |
|
391 |
{1,0xff,0xff,2,}, |
|
392 |
{3,2,0xff,0xff,}, |
|
393 |
{0xff,0xff,1,0,}, |
|
394 |
}; |
|
395 |
||
396 |
static const byte _new_track[6][4] = { |
|
397 |
{0,0xff,8,0xff,}, |
|
398 |
{0xff,1,0xff,9,}, |
|
399 |
{0xff,2,10,0xff,}, |
|
400 |
{3,0xff,0xff,11,}, |
|
401 |
{12,4,0xff,0xff,}, |
|
402 |
{0xff,0xff,5,13,}, |
|
403 |
}; |
|
404 |
||
405 |
typedef struct HashLink { |
|
406 |
TileIndex tile; |
|
407 |
uint16 typelength; |
|
408 |
uint16 next; |
|
409 |
} HashLink; |
|
410 |
||
411 |
typedef struct { |
|
412 |
TPFEnumProc *enum_proc; |
|
413 |
void *userdata; |
|
414 |
||
415 |
byte tracktype; |
|
416 |
uint maxlength; |
|
417 |
||
418 |
HashLink *new_link; |
|
419 |
uint num_links_left; |
|
420 |
||
421 |
int nstack; |
|
422 |
StackedItem stack[256]; // priority queue of stacked items |
|
423 |
||
424 |
uint16 hash_head[0x400]; // hash heads. 0 means unused. 0xFFC0 = length, 0x3F = type |
|
425 |
TileIndex hash_tile[0x400]; // tiles. or links. |
|
426 |
||
427 |
HashLink links[0x400]; // hash links |
|
428 |
||
429 |
} NewTrackPathFinder; |
|
430 |
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links) |
|
431 |
#define NTP_GET_LINK_PTR(tpf, link_offs) (HashLink*)((byte*)tpf->links + (link_offs)) |
|
432 |
||
433 |
#define ARR(i) tpf->stack[(i)-1] |
|
434 |
||
435 |
// called after a new element was added in the queue at the last index. |
|
436 |
// move it down to the proper position |
|
536 | 437 |
static inline void HeapifyUp(NewTrackPathFinder *tpf) |
0 | 438 |
{ |
439 |
StackedItem si; |
|
440 |
int i = ++tpf->nstack; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
441 |
|
0 | 442 |
while (i != 1 && ARR(i).cur_length < ARR(i>>1).cur_length) { |
443 |
// the child element is larger than the parent item. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
444 |
// swap the child item and the parent item. |
0 | 445 |
si = ARR(i); ARR(i) = ARR(i>>1); ARR(i>>1) = si; |
446 |
i>>=1; |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
// called after the element 0 was eaten. fill it with a new element |
|
536 | 451 |
static inline void HeapifyDown(NewTrackPathFinder *tpf) |
0 | 452 |
{ |
453 |
StackedItem si; |
|
454 |
int i = 1, j; |
|
455 |
int n = --tpf->nstack; |
|
456 |
||
457 |
if (n == 0) return; // heap is empty so nothing to do? |
|
458 |
||
459 |
// copy the last item to index 0. we use it as base for heapify. |
|
460 |
ARR(1) = ARR(n+1); |
|
461 |
||
462 |
while ((j=i*2) <= n) { |
|
463 |
// figure out which is smaller of the children. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
464 |
if (j != n && ARR(j).cur_length > ARR(j+1).cur_length) |
0 | 465 |
j++; // right item is smaller |
466 |
||
467 |
assert(i <= n && j <= n); |
|
468 |
if (ARR(i).cur_length <= ARR(j).cur_length) |
|
469 |
break; // base elem smaller than smallest, done! |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
470 |
|
0 | 471 |
// swap parent with the child |
472 |
si = ARR(i); ARR(i) = ARR(j); ARR(j) = si; |
|
473 |
i = j; |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
// mark a tile as visited and store the length of the path. |
|
478 |
// if we already had a better path to this tile, return false. |
|
479 |
// otherwise return true. |
|
480 |
static bool NtpVisit(NewTrackPathFinder *tpf, uint tile, uint dir, uint length) |
|
481 |
{ |
|
482 |
uint hash,head; |
|
483 |
HashLink *link, *new_link; |
|
484 |
||
485 |
assert(length < 1024); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
486 |
|
0 | 487 |
hash = PATHFIND_HASH_TILE(tile); |
488 |
||
489 |
// never visited before? |
|
490 |
if ((head=tpf->hash_head[hash]) == 0) { |
|
491 |
tpf->hash_tile[hash] = tile; |
|
492 |
tpf->hash_head[hash] = dir | (length << 2); |
|
493 |
return true; |
|
494 |
} |
|
495 |
||
496 |
if (head != 0xffff) { |
|
497 |
if ( (TileIndex)tile == tpf->hash_tile[hash] && (head & 0x3) == dir ) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
498 |
|
0 | 499 |
// longer length |
500 |
if (length >= (head >> 2)) return false; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
501 |
|
0 | 502 |
tpf->hash_head[hash] = dir | (length << 2); |
503 |
return true; |
|
504 |
} |
|
505 |
// two tiles with the same hash, need to make a link |
|
506 |
// allocate a link. if out of links, handle this by returning |
|
507 |
// that a tile was already visisted. |
|
508 |
if (tpf->num_links_left == 0) |
|
509 |
return false; |
|
510 |
tpf->num_links_left--; |
|
511 |
link = tpf->new_link++; |
|
512 |
||
513 |
/* move the data that was previously in the hash_??? variables |
|
514 |
* to the link struct, and let the hash variables point to the link */ |
|
515 |
link->tile = tpf->hash_tile[hash]; |
|
516 |
tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link); |
|
517 |
||
518 |
link->typelength = tpf->hash_head[hash]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
519 |
tpf->hash_head[hash] = 0xFFFF; /* multi link */ |
0 | 520 |
link->next = 0xFFFF; |
521 |
} else { |
|
522 |
// a linked list of many tiles, |
|
523 |
// find the one corresponding to the tile, if it exists. |
|
524 |
// otherwise make a new link |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
525 |
|
0 | 526 |
uint offs = tpf->hash_tile[hash]; |
527 |
do { |
|
528 |
link = NTP_GET_LINK_PTR(tpf, offs); |
|
529 |
if ( (TileIndex)tile == link->tile && (uint)(link->typelength & 0x3) == dir) { |
|
530 |
if (length >= (uint)(link->typelength >> 2)) return false; |
|
531 |
link->typelength = dir | (length << 2); |
|
532 |
return true; |
|
533 |
} |
|
534 |
} while ((offs=link->next) != 0xFFFF); |
|
535 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
536 |
|
0 | 537 |
/* get here if we need to add a new link to link, |
538 |
* first, allocate a new link, in the same way as before */ |
|
539 |
if (tpf->num_links_left == 0) |
|
540 |
return false; |
|
541 |
tpf->num_links_left--; |
|
542 |
new_link = tpf->new_link++; |
|
543 |
||
544 |
/* then fill the link with the new info, and establish a ptr from the old |
|
545 |
* link to the new one */ |
|
546 |
new_link->tile = (TileIndex)tile; |
|
547 |
new_link->typelength = dir | (length << 2); |
|
548 |
new_link->next = 0xFFFF; |
|
549 |
||
550 |
link->next = NTP_GET_LINK_OFFS(tpf, new_link); |
|
551 |
return true; |
|
552 |
} |
|
553 |
||
554 |
static bool NtpCheck(NewTrackPathFinder *tpf, uint tile, uint dir, uint length) |
|
555 |
{ |
|
556 |
uint hash,head,offs; |
|
557 |
HashLink *link; |
|
558 |
||
559 |
hash = PATHFIND_HASH_TILE(tile); |
|
560 |
head=tpf->hash_head[hash]; |
|
561 |
assert(head); |
|
562 |
||
563 |
if (head != 0xffff) { |
|
564 |
assert( tpf->hash_tile[hash] == tile && (head & 3) == dir); |
|
565 |
assert( (head >> 2) <= length); |
|
566 |
return length == (head >> 2); |
|
567 |
} |
|
568 |
||
569 |
// else it's a linked list of many tiles |
|
570 |
offs = tpf->hash_tile[hash]; |
|
571 |
for(;;) { |
|
572 |
link = NTP_GET_LINK_PTR(tpf, offs); |
|
573 |
if ( (TileIndex)tile == link->tile && (uint)(link->typelength & 0x3) == dir) { |
|
574 |
assert( (uint)(link->typelength >> 2) <= length); |
|
575 |
return length == (uint)(link->typelength >> 2); |
|
576 |
} |
|
577 |
offs = link->next; |
|
578 |
assert(offs != 0xffff); |
|
579 |
} |
|
580 |
} |
|
581 |
||
582 |
||
583 |
// new more optimized pathfinder for trains... |
|
410 | 584 |
static void NTPEnum(NewTrackPathFinder *tpf, uint tile, uint direction) |
0 | 585 |
{ |
586 |
uint bits, tile_org; |
|
587 |
int i; |
|
588 |
StackedItem si; |
|
589 |
FindLengthOfTunnelResult flotr; |
|
590 |
||
591 |
si.cur_length = 0; |
|
592 |
si.depth = 0; |
|
593 |
si.state = 0; |
|
594 |
||
595 |
restart: |
|
596 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xF0)==0) { |
|
148
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
597 |
/* This is a tunnel tile */ |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
598 |
if ( (uint)(_map5[tile] & 3) != (direction ^ 2)) { /* ^ 2 is reversing the direction */ |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
599 |
/* We are not just driving out of the tunnel */ |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
600 |
if ( (uint)(_map5[tile] & 3) != direction || ((_map5[tile]>>1)&6) != tpf->tracktype) |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
601 |
/* We are not driving into the tunnel, or it |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
602 |
* is an invalid tunnel */ |
0 | 603 |
goto popnext; |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
604 |
flotr = FindLengthOfTunnel(tile, direction); |
148
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
605 |
si.cur_length += flotr.length; |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
606 |
tile = flotr.tile; |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
607 |
} |
0 | 608 |
} |
609 |
||
610 |
// remember the start tile so we know if we're in an inf loop. |
|
611 |
tile_org = tile; |
|
612 |
||
613 |
for(;;) { |
|
614 |
tile += _tileoffs_by_dir[direction]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
615 |
|
0 | 616 |
// too long search length? bail out. |
617 |
if (++si.cur_length >= tpf->maxlength) |
|
618 |
goto popnext; |
|
619 |
||
620 |
// not a regular rail tile? |
|
621 |
if (!IS_TILETYPE(tile, MP_RAILWAY) || (bits = _map5[tile]) & 0xC0) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
622 |
bits = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _tpfmode1_and[direction]; |
0 | 623 |
bits = (bits | (bits >> 8)) & 0x3F; |
624 |
break; |
|
625 |
} |
|
626 |
||
627 |
// regular rail tile, determine the tracks that are actually reachable. |
|
628 |
bits &= _bits_mask[direction]; |
|
629 |
if (bits == 0) goto popnext; // no tracks there? stop searching. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
630 |
|
0 | 631 |
// complex tile?, let the generic handler handle that.. |
632 |
if (KILL_FIRST_BIT(bits) != 0) break; |
|
633 |
||
634 |
// don't bother calling the callback when we have regular tracks only. |
|
635 |
// it's usually not needed anyway. that will speed up things. |
|
636 |
direction = _new_dir[FIND_FIRST_BIT(bits)][direction]; |
|
637 |
assert(direction != 0xFF); |
|
638 |
if (tile == tile_org) goto popnext; // detect infinite loop.. |
|
639 |
} |
|
640 |
||
641 |
if (!bits) goto popnext; |
|
642 |
||
643 |
// if only one reachable track, use tail recursion optimization. |
|
644 |
if (KILL_FIRST_BIT(bits) == 0) { |
|
645 |
i = _new_track[FIND_FIRST_BIT(bits)][direction]; |
|
646 |
// call the callback |
|
647 |
if (tpf->enum_proc(tile, tpf->userdata, i, si.cur_length, &si.state)) |
|
648 |
goto popnext; // we should stop searching in this direction. |
|
649 |
||
650 |
// we should continue searching. determine new direction. |
|
651 |
direction = _tpf_new_direction[i]; |
|
652 |
goto restart; // use tail recursion optimization. |
|
653 |
} |
|
654 |
||
655 |
// too high recursion depth.. bail out.. |
|
656 |
if (si.depth >= _patches.pf_maxdepth) |
|
657 |
goto popnext; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
658 |
|
0 | 659 |
si.depth++; // increase recursion depth. |
660 |
||
661 |
// see if this tile was already visited..? |
|
662 |
if (NtpVisit(tpf, tile, direction, si.cur_length)) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
663 |
// push all possible alternatives |
0 | 664 |
si.tile = tile; |
665 |
do { |
|
666 |
si.track = _new_track[FIND_FIRST_BIT(bits)][direction]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
667 |
|
0 | 668 |
// out of stack items, bail out? |
669 |
if (tpf->nstack >= lengthof(tpf->stack)) |
|
670 |
break; |
|
671 |
tpf->stack[tpf->nstack] = si; |
|
672 |
HeapifyUp(tpf); |
|
673 |
} while ((bits = KILL_FIRST_BIT(bits)) != 0); |
|
674 |
||
675 |
// if this is the first recursion step, we need to fill the first_track member. |
|
676 |
// so the code outside knows which path is better. |
|
677 |
// also randomize the order in which we search through them. |
|
678 |
if (si.depth == 1) { |
|
679 |
uint32 r = Random(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
680 |
assert(tpf->nstack == 2 || tpf->nstack == 3); |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
681 |
if (r&1) swap_byte(&tpf->stack[0].track, &tpf->stack[1].track); |
0 | 682 |
if (tpf->nstack != 2) { |
683 |
byte t = tpf->stack[2].track; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
684 |
if (r&2) swap_byte(&tpf->stack[0].track, &t); |
0 | 685 |
if (r&4) swap_byte(&tpf->stack[1].track, &t); |
686 |
tpf->stack[2].first_track = tpf->stack[2].track = t; |
|
687 |
} |
|
688 |
tpf->stack[0].first_track = tpf->stack[0].track; |
|
689 |
tpf->stack[1].first_track = tpf->stack[1].track; |
|
690 |
} |
|
691 |
} |
|
692 |
||
693 |
popnext: |
|
694 |
// where to continue. |
|
695 |
do { |
|
696 |
if (tpf->nstack == 0) return; // nothing left? |
|
697 |
si = tpf->stack[0]; |
|
698 |
tile = si.tile; |
|
699 |
HeapifyDown(tpf); |
|
700 |
} while ( |
|
701 |
!NtpCheck(tpf, tile, _tpf_prev_direction[si.track], si.cur_length) || // already have better path to that tile? |
|
702 |
tpf->enum_proc(tile, tpf->userdata, si.track, si.cur_length, &si.state) |
|
703 |
); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
704 |
|
0 | 705 |
direction = _tpf_new_direction[si.track]; |
706 |
goto restart; |
|
707 |
} |
|
708 |
||
709 |
||
710 |
// new pathfinder for trains. better and faster. |
|
711 |
void NewTrainPathfind(uint tile, byte direction, TPFEnumProc *enum_proc, void *data, byte *cache) |
|
712 |
{ |
|
713 |
if (!_patches.new_pathfinding) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
714 |
FollowTrack(tile, 0x3000 | TRANSPORT_RAIL, direction, enum_proc, NULL, data); |
0 | 715 |
} else { |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
716 |
NewTrackPathFinder tpf; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
717 |
tpf.userdata = data; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
718 |
tpf.enum_proc = enum_proc; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
719 |
tpf.tracktype = 0; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
720 |
tpf.maxlength = _patches.pf_maxlength; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
721 |
tpf.nstack = 0; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
722 |
tpf.new_link = tpf.links; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
723 |
tpf.num_links_left = lengthof(tpf.links); |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
724 |
memset(tpf.hash_head, 0, sizeof(tpf.hash_head)); |
0 | 725 |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
726 |
NTPEnum(&tpf, tile, direction); |
0 | 727 |
} |
728 |
} |
|
729 |