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