author | tron |
Thu, 14 Jul 2005 06:10:23 +0000 | |
changeset 2054 | e313dd080bd3 |
parent 2049 | ad0d49c916d4 |
child 2125 | 3098398bf7ff |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1209
diff
changeset
|
2 |
#include "openttd.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" |
1209
a1ac96655b79
(svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents:
1035
diff
changeset
|
4 |
#include "tile.h" |
0 | 5 |
#include "pathfind.h" |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
6 |
#include "rail.h" |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
7 |
#include "debug.h" |
0 | 8 |
|
9 |
// remember which tiles we have already visited so we don't visit them again. |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
10 |
static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir) |
0 | 11 |
{ |
12 |
uint hash, val, offs; |
|
13 |
TrackPathFinderLink *link, *new_link; |
|
14 |
uint bits = 1 << dir; |
|
15 |
||
16 |
if (tpf->disable_tile_hash) |
|
17 |
return true; |
|
18 |
||
19 |
hash = PATHFIND_HASH_TILE(tile); |
|
20 |
||
21 |
val = tpf->hash_head[hash]; |
|
22 |
||
23 |
if (val == 0) { |
|
24 |
/* unused hash entry, set the appropriate bit in it and return true |
|
25 |
* to indicate that a bit was set. */ |
|
26 |
tpf->hash_head[hash] = bits; |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
27 |
tpf->hash_tile[hash] = tile; |
0 | 28 |
return true; |
29 |
} else if (!(val & 0x8000)) { |
|
30 |
/* single tile */ |
|
31 |
||
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
32 |
if (tile == tpf->hash_tile[hash]) { |
0 | 33 |
/* found another bit for the same tile, |
34 |
* check if this bit is already set, if so, return false */ |
|
35 |
if (val & bits) |
|
36 |
return false; |
|
37 |
||
38 |
/* otherwise set the bit and return true to indicate that the bit |
|
39 |
* was set */ |
|
40 |
tpf->hash_head[hash] = val | bits; |
|
41 |
return true; |
|
42 |
} else { |
|
43 |
/* 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
|
44 |
|
0 | 45 |
/* allocate a link. if out of links, handle this by returning |
46 |
* that a tile was already visisted. */ |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
47 |
if (tpf->num_links_left == 0) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
48 |
DEBUG(misc, 4) ("[NTP] no links left\n"); |
0 | 49 |
return false; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
50 |
} |
0 | 51 |
tpf->num_links_left--; |
52 |
link = tpf->new_link++; |
|
53 |
||
54 |
/* move the data that was previously in the hash_??? variables |
|
55 |
* to the link struct, and let the hash variables point to the link */ |
|
56 |
link->tile = tpf->hash_tile[hash]; |
|
57 |
tpf->hash_tile[hash] = PATHFIND_GET_LINK_OFFS(tpf, link); |
|
58 |
||
59 |
link->flags = tpf->hash_head[hash]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
60 |
tpf->hash_head[hash] = 0xFFFF; /* multi link */ |
0 | 61 |
|
62 |
link->next = 0xFFFF; |
|
63 |
} |
|
64 |
} else { |
|
65 |
/* a linked list of many tiles, |
|
66 |
* find the one corresponding to the tile, if it exists. |
|
67 |
* otherwise make a new link */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
68 |
|
0 | 69 |
offs = tpf->hash_tile[hash]; |
70 |
do { |
|
71 |
link = PATHFIND_GET_LINK_PTR(tpf, offs); |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
72 |
if (tile == link->tile) { |
0 | 73 |
/* found the tile in the link list, |
74 |
* check if the bit was alrady set, if so return false to indicate that the |
|
75 |
* bit was already set */ |
|
76 |
if (link->flags & bits) |
|
77 |
return false; |
|
78 |
link->flags |= bits; |
|
79 |
return true; |
|
80 |
} |
|
81 |
} while ((offs=link->next) != 0xFFFF); |
|
82 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
83 |
|
0 | 84 |
/* get here if we need to add a new link to link, |
85 |
* first, allocate a new link, in the same way as before */ |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
86 |
if (tpf->num_links_left == 0) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
87 |
DEBUG(misc, 4)("[NTP] no links left\n"); |
0 | 88 |
return false; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
89 |
} |
0 | 90 |
tpf->num_links_left--; |
91 |
new_link = tpf->new_link++; |
|
92 |
||
93 |
/* then fill the link with the new info, and establish a ptr from the old |
|
94 |
* link to the new one */ |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
95 |
new_link->tile = tile; |
0 | 96 |
new_link->flags = bits; |
97 |
new_link->next = 0xFFFF; |
|
98 |
||
99 |
link->next = PATHFIND_GET_LINK_OFFS(tpf, new_link); |
|
100 |
return true; |
|
101 |
} |
|
102 |
||
103 |
static const byte _bits_mask[4] = { |
|
104 |
0x19, |
|
105 |
0x16, |
|
106 |
0x25, |
|
107 |
0x2A, |
|
108 |
}; |
|
109 |
||
110 |
static const byte _tpf_new_direction[14] = { |
|
111 |
0,1,0,1,2,1, 0,0, |
|
112 |
2,3,3,2,3,0, |
|
113 |
}; |
|
114 |
||
115 |
static const byte _tpf_prev_direction[14] = { |
|
116 |
0,1,1,0,1,2, 0,0, |
|
117 |
2,3,2,3,0,3, |
|
118 |
}; |
|
119 |
||
120 |
||
121 |
static const byte _otherdir_mask[4] = { |
|
122 |
0x10, |
|
123 |
0, |
|
124 |
0x5, |
|
125 |
0x2A, |
|
126 |
}; |
|
127 |
||
128 |
#ifdef DEBUG_TILE_PUSH |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
129 |
extern void dbg_push_tile(TileIndex tile, int track); |
0 | 130 |
extern void dbg_pop_tile(); |
131 |
#endif |
|
132 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
133 |
static void TPFMode2(TrackPathFinder *tpf, TileIndex tile, int direction) |
0 | 134 |
{ |
135 |
uint bits; |
|
136 |
int i; |
|
137 |
RememberData rd; |
|
753 | 138 |
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
|
139 |
|
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
140 |
/* XXX: Mode 2 is currently only used for ships, why is this code here? */ |
752
df3e096dd9db
(svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents:
747
diff
changeset
|
141 |
if (tpf->tracktype == TRANSPORT_RAIL) { |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1034
diff
changeset
|
142 |
if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) { |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1891
diff
changeset
|
143 |
owner = GetTileOwner(tile); |
913
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
144 |
/* Check if we are on the middle of a bridge (has no owner) */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
145 |
if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xC0) == 0xC0) |
913
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
146 |
owner = -1; |
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
147 |
} |
752
df3e096dd9db
(svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents:
747
diff
changeset
|
148 |
} |
0 | 149 |
|
150 |
// This addition will sometimes overflow by a single tile. |
|
151 |
// 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
|
152 |
// tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. |
900 | 153 |
tile = TILE_MASK(tile + TileOffsByDir(direction)); |
0 | 154 |
|
747
e795750b96de
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
155 |
/* 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
|
156 |
if (tpf->tracktype == TRANSPORT_RAIL) { |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1034
diff
changeset
|
157 |
if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) |
913
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
158 |
/* Check if we are on the middle of a bridge (has no owner) */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
159 |
if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_m[tile].m5 & 0xC0) != 0xC0) |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1891
diff
changeset
|
160 |
if (owner != -1 && !IsTileOwner(tile, owner)) |
913
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
161 |
return; |
752
df3e096dd9db
(svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents:
747
diff
changeset
|
162 |
} |
747
e795750b96de
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
163 |
|
0 | 164 |
if (++tpf->rd.cur_length > 50) |
165 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
166 |
|
0 | 167 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
168 |
bits = (byte)((bits | (bits >> 8)) & _bits_mask[direction]); |
|
169 |
if (bits == 0) |
|
170 |
return; |
|
171 |
||
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
|
172 |
assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY()); |
0 | 173 |
|
174 |
if ( (bits & (bits - 1)) == 0 ) { |
|
175 |
/* only one direction */ |
|
176 |
i = 0; |
|
177 |
while (!(bits&1)) |
|
178 |
i++, bits>>=1; |
|
179 |
||
180 |
rd = tpf->rd; |
|
181 |
goto continue_here; |
|
182 |
} |
|
183 |
/* several directions */ |
|
184 |
i=0; |
|
185 |
do { |
|
186 |
if (!(bits & 1)) continue; |
|
187 |
rd = tpf->rd; |
|
188 |
||
189 |
// Change direction 4 times only |
|
190 |
if ((byte)i != tpf->rd.pft_var6) { |
|
191 |
if(++tpf->rd.depth > 4) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
192 |
tpf->rd = rd; |
0 | 193 |
return; |
194 |
} |
|
195 |
tpf->rd.pft_var6 = (byte)i; |
|
196 |
} |
|
197 |
||
198 |
continue_here:; |
|
199 |
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
|
200 |
|
0 | 201 |
#ifdef DEBUG_TILE_PUSH |
202 |
dbg_push_tile(tile, tpf->the_dir); |
|
203 |
#endif |
|
204 |
if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, NULL)) { |
|
205 |
TPFMode2(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
206 |
} |
|
207 |
#ifdef DEBUG_TILE_PUSH |
|
208 |
dbg_pop_tile(); |
|
209 |
#endif |
|
210 |
||
211 |
tpf->rd = rd; |
|
212 |
} while (++i, bits>>=1); |
|
213 |
||
214 |
} |
|
215 |
||
216 |
static const int8 _get_tunlen_inc[5] = { -16, 0, 16, 0, -16 }; |
|
217 |
||
22 | 218 |
/* Returns the end tile and the length of a tunnel. The length does not |
219 |
* include the starting tile (entry), it does include the end tile (exit). |
|
220 |
*/ |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
221 |
FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, int direction) |
0 | 222 |
{ |
223 |
FindLengthOfTunnelResult flotr; |
|
224 |
int x,y; |
|
225 |
byte z; |
|
226 |
||
227 |
flotr.length = 0; |
|
228 |
||
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
|
229 |
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
|
230 |
y = TileY(tile) * 16; |
0 | 231 |
|
232 |
z = GetSlopeZ(x+8, y+8); |
|
233 |
||
234 |
for(;;) { |
|
235 |
flotr.length++; |
|
236 |
||
237 |
x += _get_tunlen_inc[direction]; |
|
238 |
y += _get_tunlen_inc[direction+1]; |
|
239 |
||
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
240 |
tile = TileVirtXY(x, y); |
0 | 241 |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1034
diff
changeset
|
242 |
if (IsTileType(tile, MP_TUNNELBRIDGE) && |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
243 |
(_m[tile].m5 & 0xF0) == 0 && // tunnel entrance/exit |
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
244 |
//((_m[tile].m5>>2)&3) == type && // rail/road-tunnel <-- This is not necesary to check, right? |
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
245 |
((_m[tile].m5 & 3)^2) == direction && // entrance towards: 0 = NE, 1 = SE, 2 = SW, 3 = NW |
0 | 246 |
GetSlopeZ(x+8, y+8) == z) |
247 |
break; |
|
248 |
} |
|
249 |
||
250 |
flotr.tile = tile; |
|
251 |
return flotr; |
|
252 |
} |
|
253 |
||
254 |
static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 }; |
|
255 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
256 |
static uint SkipToEndOfTunnel(TrackPathFinder *tpf, TileIndex tile, int direction) |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
257 |
{ |
0 | 258 |
FindLengthOfTunnelResult flotr; |
259 |
TPFSetTileBit(tpf, tile, 14); |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
260 |
flotr = FindLengthOfTunnel(tile, direction); |
0 | 261 |
tpf->rd.cur_length += flotr.length; |
262 |
TPFSetTileBit(tpf, flotr.tile, 14); |
|
263 |
return flotr.tile; |
|
264 |
} |
|
265 |
||
266 |
const byte _ffb_64[128] = { |
|
267 |
0,0,1,0,2,0,1,0, |
|
268 |
3,0,1,0,2,0,1,0, |
|
269 |
4,0,1,0,2,0,1,0, |
|
270 |
3,0,1,0,2,0,1,0, |
|
271 |
5,0,1,0,2,0,1,0, |
|
272 |
3,0,1,0,2,0,1,0, |
|
273 |
4,0,1,0,2,0,1,0, |
|
274 |
3,0,1,0,2,0,1,0, |
|
275 |
||
276 |
0,0,0,2,0,4,4,6, |
|
277 |
0,8,8,10,8,12,12,14, |
|
278 |
0,16,16,18,16,20,20,22, |
|
279 |
16,24,24,26,24,28,28,30, |
|
280 |
0,32,32,34,32,36,36,38, |
|
281 |
32,40,40,42,40,44,44,46, |
|
282 |
32,48,48,50,48,52,52,54, |
|
283 |
48,56,56,58,56,60,60,62, |
|
284 |
}; |
|
285 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
286 |
static void TPFMode1(TrackPathFinder *tpf, TileIndex tile, int direction) |
0 | 287 |
{ |
288 |
uint bits; |
|
289 |
int i; |
|
290 |
RememberData rd; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
291 |
TileIndex tile_org = tile; |
0 | 292 |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
293 |
if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xF0) == 0) { |
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
294 |
if ((_m[tile].m5 & 3) != direction || ((_m[tile].m5>>2)&3) != tpf->tracktype) |
0 | 295 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
296 |
tile = SkipToEndOfTunnel(tpf, tile, direction); |
0 | 297 |
} |
900 | 298 |
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
|
299 |
|
e795750b96de
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
300 |
/* 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
|
301 |
if (tpf->tracktype == TRANSPORT_RAIL) { |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1034
diff
changeset
|
302 |
if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE)) |
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1034
diff
changeset
|
303 |
if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) |
913
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
304 |
/* Check if we are on a bridge (middle parts don't have an owner */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
305 |
if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_m[tile].m5 & 0xC0) != 0xC0) |
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
306 |
if (!IsTileType(tile_org, MP_TUNNELBRIDGE) || (_m[tile_org].m5 & 0xC0) != 0xC0) |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1891
diff
changeset
|
307 |
if (GetTileOwner(tile_org) != GetTileOwner(tile)) |
913
ee326ef51cdb
(svn r1400) -Fix: signal stays red if a track is removed (Darkvater / Tron / TrueLight)
truelight
parents:
905
diff
changeset
|
308 |
return; |
752
df3e096dd9db
(svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents:
747
diff
changeset
|
309 |
} |
747
e795750b96de
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
310 |
|
0 | 311 |
tpf->rd.cur_length++; |
312 |
||
313 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
|
314 |
||
315 |
if ((byte)bits != tpf->var2) { |
|
316 |
bits &= _tpfmode1_and[direction]; |
|
317 |
bits = bits | (bits>>8); |
|
318 |
} |
|
319 |
bits &= 0xBF; |
|
320 |
||
321 |
if (bits != 0) { |
|
322 |
if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KILL_FIRST_BIT(bits) == 0 || ++tpf->rd.depth <= 7))) { |
|
323 |
do { |
|
324 |
i = FIND_FIRST_BIT(bits); |
|
325 |
bits = KILL_FIRST_BIT(bits); |
|
326 |
||
327 |
tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i; |
|
328 |
rd = tpf->rd; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
329 |
|
0 | 330 |
#ifdef DEBUG_TILE_PUSH |
331 |
dbg_push_tile(tile, tpf->the_dir); |
|
332 |
#endif |
|
333 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && |
|
334 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) { |
|
335 |
TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
336 |
} |
|
337 |
#ifdef DEBUG_TILE_PUSH |
|
338 |
dbg_pop_tile(); |
|
339 |
#endif |
|
340 |
tpf->rd = rd; |
|
341 |
} while (bits != 0); |
|
342 |
} |
|
343 |
} |
|
344 |
||
345 |
/* the next is only used when signals are checked. |
|
346 |
* 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
|
347 |
|
0 | 348 |
/* 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
|
349 |
* stack space dramatically. */ |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
350 |
|
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
351 |
/* If we are doing signal setting, we must reverse at evere tile, so we |
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
352 |
* iterate all the tracks in a signal block, even when a normal train would |
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
353 |
* not reach it (for example, when two lines merge */ |
0 | 354 |
if (tpf->hasbit_13) |
355 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
356 |
|
0 | 357 |
tile = tile_org; |
358 |
direction ^= 2; |
|
359 |
||
360 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
|
361 |
bits |= (bits >> 8); |
|
362 |
||
363 |
if ( (byte)bits != tpf->var2) { |
|
364 |
bits &= _bits_mask[direction]; |
|
365 |
} |
|
366 |
||
367 |
bits &= 0xBF; |
|
368 |
if (bits == 0) |
|
369 |
return; |
|
370 |
||
371 |
do { |
|
372 |
i = FIND_FIRST_BIT(bits); |
|
373 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
374 |
|
0 | 375 |
tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i; |
376 |
rd = tpf->rd; |
|
377 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && |
|
378 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) { |
|
379 |
TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
380 |
} |
|
381 |
tpf->rd = rd; |
|
382 |
} while (bits != 0); |
|
383 |
} |
|
384 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
385 |
void FollowTrack(TileIndex tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data) |
0 | 386 |
{ |
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
387 |
TrackPathFinder tpf; |
0 | 388 |
|
389 |
assert(direction < 4); |
|
390 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
391 |
/* initialize path finder variables */ |
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
392 |
tpf.userdata = data; |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
393 |
tpf.enum_proc = enum_proc; |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
394 |
tpf.new_link = tpf.links; |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
395 |
tpf.num_links_left = lengthof(tpf.links); |
0 | 396 |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
397 |
tpf.rd.cur_length = 0; |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
398 |
tpf.rd.depth = 0; |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
399 |
tpf.rd.pft_var6 = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
400 |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
401 |
tpf.var2 = HASBIT(flags, 15) ? 0x43 : 0xFF; /* 0x8000 */ |
0 | 402 |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
403 |
tpf.disable_tile_hash = HASBIT(flags, 12) != 0; /* 0x1000 */ |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
404 |
tpf.hasbit_13 = HASBIT(flags, 13) != 0; /* 0x2000 */ |
0 | 405 |
|
406 |
||
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
407 |
tpf.tracktype = (byte)flags; |
0 | 408 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
409 |
if (HASBIT(flags, 11)) { |
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
410 |
tpf.rd.pft_var6 = 0xFF; |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
411 |
tpf.enum_proc(tile, data, 0, 0, 0); |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
412 |
TPFMode2(&tpf, tile, direction); |
0 | 413 |
} else { |
414 |
/* clear the hash_heads */ |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
415 |
memset(tpf.hash_head, 0, sizeof(tpf.hash_head)); |
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
416 |
TPFMode1(&tpf, tile, direction); |
0 | 417 |
} |
418 |
||
419 |
if (after_proc != NULL) |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
420 |
after_proc(&tpf); |
0 | 421 |
} |
422 |
||
423 |
typedef struct { |
|
424 |
TileIndex tile; |
|
425 |
uint16 cur_length; |
|
426 |
byte track; |
|
427 |
byte depth; |
|
428 |
byte state; |
|
429 |
byte first_track; |
|
430 |
} StackedItem; |
|
431 |
||
432 |
static const byte _new_track[6][4] = { |
|
433 |
{0,0xff,8,0xff,}, |
|
434 |
{0xff,1,0xff,9,}, |
|
435 |
{0xff,2,10,0xff,}, |
|
436 |
{3,0xff,0xff,11,}, |
|
437 |
{12,4,0xff,0xff,}, |
|
438 |
{0xff,0xff,5,13,}, |
|
439 |
}; |
|
440 |
||
441 |
typedef struct HashLink { |
|
442 |
TileIndex tile; |
|
443 |
uint16 typelength; |
|
444 |
uint16 next; |
|
445 |
} HashLink; |
|
446 |
||
447 |
typedef struct { |
|
448 |
TPFEnumProc *enum_proc; |
|
449 |
void *userdata; |
|
450 |
||
451 |
byte tracktype; |
|
452 |
uint maxlength; |
|
453 |
||
454 |
HashLink *new_link; |
|
455 |
uint num_links_left; |
|
456 |
||
979
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
457 |
uint nstack; |
0 | 458 |
StackedItem stack[256]; // priority queue of stacked items |
459 |
||
460 |
uint16 hash_head[0x400]; // hash heads. 0 means unused. 0xFFC0 = length, 0x3F = type |
|
461 |
TileIndex hash_tile[0x400]; // tiles. or links. |
|
462 |
||
463 |
HashLink links[0x400]; // hash links |
|
464 |
||
465 |
} NewTrackPathFinder; |
|
466 |
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links) |
|
467 |
#define NTP_GET_LINK_PTR(tpf, link_offs) (HashLink*)((byte*)tpf->links + (link_offs)) |
|
468 |
||
469 |
#define ARR(i) tpf->stack[(i)-1] |
|
470 |
||
471 |
// called after a new element was added in the queue at the last index. |
|
472 |
// move it down to the proper position |
|
536 | 473 |
static inline void HeapifyUp(NewTrackPathFinder *tpf) |
0 | 474 |
{ |
475 |
StackedItem si; |
|
476 |
int i = ++tpf->nstack; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
477 |
|
0 | 478 |
while (i != 1 && ARR(i).cur_length < ARR(i>>1).cur_length) { |
479 |
// 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
|
480 |
// swap the child item and the parent item. |
0 | 481 |
si = ARR(i); ARR(i) = ARR(i>>1); ARR(i>>1) = si; |
482 |
i>>=1; |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
// called after the element 0 was eaten. fill it with a new element |
|
536 | 487 |
static inline void HeapifyDown(NewTrackPathFinder *tpf) |
0 | 488 |
{ |
489 |
StackedItem si; |
|
490 |
int i = 1, j; |
|
979
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
491 |
int n; |
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
492 |
|
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
493 |
assert(tpf->nstack > 0); |
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
494 |
n = --tpf->nstack; |
0 | 495 |
|
496 |
if (n == 0) return; // heap is empty so nothing to do? |
|
497 |
||
498 |
// copy the last item to index 0. we use it as base for heapify. |
|
499 |
ARR(1) = ARR(n+1); |
|
500 |
||
501 |
while ((j=i*2) <= n) { |
|
502 |
// 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
|
503 |
if (j != n && ARR(j).cur_length > ARR(j+1).cur_length) |
0 | 504 |
j++; // right item is smaller |
505 |
||
506 |
assert(i <= n && j <= n); |
|
507 |
if (ARR(i).cur_length <= ARR(j).cur_length) |
|
508 |
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
|
509 |
|
0 | 510 |
// swap parent with the child |
511 |
si = ARR(i); ARR(i) = ARR(j); ARR(j) = si; |
|
512 |
i = j; |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
// mark a tile as visited and store the length of the path. |
|
517 |
// if we already had a better path to this tile, return false. |
|
518 |
// otherwise return true. |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
519 |
static bool NtpVisit(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length) |
0 | 520 |
{ |
521 |
uint hash,head; |
|
522 |
HashLink *link, *new_link; |
|
523 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
524 |
assert(length < 16384-1); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
525 |
|
0 | 526 |
hash = PATHFIND_HASH_TILE(tile); |
527 |
||
528 |
// never visited before? |
|
529 |
if ((head=tpf->hash_head[hash]) == 0) { |
|
530 |
tpf->hash_tile[hash] = tile; |
|
531 |
tpf->hash_head[hash] = dir | (length << 2); |
|
532 |
return true; |
|
533 |
} |
|
534 |
||
535 |
if (head != 0xffff) { |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
536 |
if (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
|
537 |
|
0 | 538 |
// longer length |
539 |
if (length >= (head >> 2)) return false; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
540 |
|
0 | 541 |
tpf->hash_head[hash] = dir | (length << 2); |
542 |
return true; |
|
543 |
} |
|
544 |
// two tiles with the same hash, need to make a link |
|
545 |
// allocate a link. if out of links, handle this by returning |
|
546 |
// that a tile was already visisted. |
|
547 |
if (tpf->num_links_left == 0) |
|
548 |
return false; |
|
549 |
tpf->num_links_left--; |
|
550 |
link = tpf->new_link++; |
|
551 |
||
552 |
/* move the data that was previously in the hash_??? variables |
|
553 |
* to the link struct, and let the hash variables point to the link */ |
|
554 |
link->tile = tpf->hash_tile[hash]; |
|
555 |
tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link); |
|
556 |
||
557 |
link->typelength = tpf->hash_head[hash]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
558 |
tpf->hash_head[hash] = 0xFFFF; /* multi link */ |
0 | 559 |
link->next = 0xFFFF; |
560 |
} else { |
|
561 |
// a linked list of many tiles, |
|
562 |
// find the one corresponding to the tile, if it exists. |
|
563 |
// otherwise make a new link |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
564 |
|
0 | 565 |
uint offs = tpf->hash_tile[hash]; |
566 |
do { |
|
567 |
link = NTP_GET_LINK_PTR(tpf, offs); |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
568 |
if (tile == link->tile && (uint)(link->typelength & 0x3) == dir) { |
0 | 569 |
if (length >= (uint)(link->typelength >> 2)) return false; |
570 |
link->typelength = dir | (length << 2); |
|
571 |
return true; |
|
572 |
} |
|
573 |
} while ((offs=link->next) != 0xFFFF); |
|
574 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
575 |
|
0 | 576 |
/* get here if we need to add a new link to link, |
577 |
* first, allocate a new link, in the same way as before */ |
|
578 |
if (tpf->num_links_left == 0) |
|
579 |
return false; |
|
580 |
tpf->num_links_left--; |
|
581 |
new_link = tpf->new_link++; |
|
582 |
||
583 |
/* then fill the link with the new info, and establish a ptr from the old |
|
584 |
* link to the new one */ |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
585 |
new_link->tile = tile; |
0 | 586 |
new_link->typelength = dir | (length << 2); |
587 |
new_link->next = 0xFFFF; |
|
588 |
||
589 |
link->next = NTP_GET_LINK_OFFS(tpf, new_link); |
|
590 |
return true; |
|
591 |
} |
|
592 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
593 |
static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length) |
0 | 594 |
{ |
595 |
uint hash,head,offs; |
|
596 |
HashLink *link; |
|
597 |
||
598 |
hash = PATHFIND_HASH_TILE(tile); |
|
599 |
head=tpf->hash_head[hash]; |
|
600 |
assert(head); |
|
601 |
||
602 |
if (head != 0xffff) { |
|
603 |
assert( tpf->hash_tile[hash] == tile && (head & 3) == dir); |
|
604 |
assert( (head >> 2) <= length); |
|
605 |
return length == (head >> 2); |
|
606 |
} |
|
607 |
||
608 |
// else it's a linked list of many tiles |
|
609 |
offs = tpf->hash_tile[hash]; |
|
610 |
for(;;) { |
|
611 |
link = NTP_GET_LINK_PTR(tpf, offs); |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
612 |
if (tile == link->tile && (uint)(link->typelength & 0x3) == dir) { |
0 | 613 |
assert( (uint)(link->typelength >> 2) <= length); |
614 |
return length == (uint)(link->typelength >> 2); |
|
615 |
} |
|
616 |
offs = link->next; |
|
617 |
assert(offs != 0xffff); |
|
618 |
} |
|
619 |
} |
|
620 |
||
621 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
622 |
static const uint16 _is_upwards_slope[15] = { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
623 |
0, // no tileh |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
624 |
(1 << TRACKDIR_DIAG1_SW) | (1 << TRACKDIR_DIAG2_NW), // 1 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
625 |
(1 << TRACKDIR_DIAG1_SW) | (1 << TRACKDIR_DIAG2_SE), // 2 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
626 |
(1 << TRACKDIR_DIAG1_SW), // 3 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
627 |
(1 << TRACKDIR_DIAG1_NE) | (1 << TRACKDIR_DIAG2_SE), // 4 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
628 |
0, // 5 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
629 |
(1 << TRACKDIR_DIAG2_SE), // 6 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
630 |
0, // 7 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
631 |
(1 << TRACKDIR_DIAG1_NE) | (1 << TRACKDIR_DIAG2_NW), // 8, |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
632 |
(1 << TRACKDIR_DIAG2_NW), // 9 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
633 |
0, //10 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
634 |
0, //11, |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
635 |
(1 << TRACKDIR_DIAG1_NE), //12 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
636 |
0, //13 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
637 |
0, //14 |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
638 |
}; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
639 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
640 |
|
0 | 641 |
// new more optimized pathfinder for trains... |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
642 |
static void NTPEnum(NewTrackPathFinder *tpf, TileIndex tile, uint direction) |
0 | 643 |
{ |
644 |
uint bits, tile_org; |
|
645 |
int i; |
|
646 |
StackedItem si; |
|
647 |
FindLengthOfTunnelResult flotr; |
|
648 |
||
649 |
si.cur_length = 0; |
|
650 |
si.depth = 0; |
|
651 |
si.state = 0; |
|
652 |
||
653 |
restart: |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
654 |
if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xF0) == 0) { |
148
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
655 |
/* This is a tunnel tile */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
656 |
if ( (uint)(_m[tile].m5 & 3) != (direction ^ 2)) { /* ^ 2 is reversing the direction */ |
148
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
657 |
/* We are not just driving out of the tunnel */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
658 |
if ( (uint)(_m[tile].m5 & 3) != direction || ((_m[tile].m5>>1)&6) != tpf->tracktype) |
148
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
659 |
/* We are not driving into the tunnel, or it |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
660 |
* is an invalid tunnel */ |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
661 |
goto stop_search; |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
662 |
flotr = FindLengthOfTunnel(tile, direction); |
148
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
663 |
si.cur_length += flotr.length; |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
664 |
tile = flotr.tile; |
6a72b12f5588
(svn r149) -Fix: [997703] Junction after tunnel bug (blathijs)
truelight
parents:
22
diff
changeset
|
665 |
} |
0 | 666 |
} |
667 |
||
668 |
// remember the start tile so we know if we're in an inf loop. |
|
669 |
tile_org = tile; |
|
670 |
||
671 |
for(;;) { |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
672 |
int track; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
673 |
|
900 | 674 |
tile += TileOffsByDir(direction); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
675 |
|
0 | 676 |
// too long search length? bail out. |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
677 |
if (++si.cur_length >= tpf->maxlength) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
678 |
DEBUG(misc,4) ("[NTP] cur_length too big\n"); |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
679 |
goto stop_search; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
680 |
} |
0 | 681 |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
682 |
// Not a regular rail tile? |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
683 |
// Then we can't use the code below, but revert to more general code. |
2045
8ae4fcc273ef
(svn r2554) - Fix: [pathfinding] Change to using some helper functions for checking the railtype.
ludde
parents:
2044
diff
changeset
|
684 |
if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) { |
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
685 |
bits = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _tpfmode1_and[direction]; |
0 | 686 |
bits = (bits | (bits >> 8)) & 0x3F; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
687 |
if (bits == 0) goto stop_search; |
0 | 688 |
break; |
689 |
} |
|
690 |
||
691 |
// regular rail tile, determine the tracks that are actually reachable. |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
692 |
bits = _m[tile].m5 & _bits_mask[direction]; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
693 |
if (bits == 0) goto stop_search; // no tracks there? stop searching. |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
694 |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
695 |
// intersection? then we need to branch the search space, |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
696 |
// can't handle that from here. |
0 | 697 |
if (KILL_FIRST_BIT(bits) != 0) break; |
698 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
699 |
track = _new_track[FIND_FIRST_BIT(bits)][direction]; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
700 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
701 |
// Check if this rail is an upwards slope. If it is, then add a penalty. |
2045
8ae4fcc273ef
(svn r2554) - Fix: [pathfinding] Change to using some helper functions for checking the railtype.
ludde
parents:
2044
diff
changeset
|
702 |
// Small optimization here.. if (track&7)>1 then it can't be a slope so we avoid calling GetTileSlope |
8ae4fcc273ef
(svn r2554) - Fix: [pathfinding] Change to using some helper functions for checking the railtype.
ludde
parents:
2044
diff
changeset
|
703 |
if ((track & 7) <= 1 && (_is_upwards_slope[GetTileSlope(tile, NULL)] & (1 << track)) ) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
704 |
// upwards slope. add some penalty. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
705 |
si.cur_length += 2; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
706 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
707 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
708 |
// railway tile with signals..? |
2045
8ae4fcc273ef
(svn r2554) - Fix: [pathfinding] Change to using some helper functions for checking the railtype.
ludde
parents:
2044
diff
changeset
|
709 |
if (HasSignals(tile)) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
710 |
byte m3; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
711 |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
712 |
m3 = _m[tile].m3; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
713 |
if (!(m3 & SignalAlongTrackdir(track))) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
714 |
// if one way signal not pointing towards us, stop going in this direction. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
715 |
if (m3 & SignalAgainstTrackdir(track)) |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
716 |
goto stop_search; |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2046
diff
changeset
|
717 |
} else if (_m[tile].m2 & SignalAlongTrackdir(track)) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
718 |
// green signal in our direction. either one way or two way. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
719 |
si.state |= 1; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
720 |
} else { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
721 |
// reached a red signal. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
722 |
if (m3 & SignalAgainstTrackdir(track)) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
723 |
// two way red signal. unless we passed another green signal on the way, |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
724 |
// stop going in this direction. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
725 |
// this is to prevent us from going into a full platform. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
726 |
if (!(si.state&1)) |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
727 |
goto stop_search; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
728 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
729 |
// add some penalty since this path has a red signal on it. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
730 |
// only add this penalty max 2 times. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
731 |
if ((si.state & 6) != 4) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
732 |
si.cur_length += 10; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
733 |
si.state += 2; // remember that we added penalty. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
734 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
735 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
736 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
737 |
if (tpf->enum_proc(tile, tpf->userdata, track, si.cur_length, &si.state)) |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
738 |
goto stop_search; // we should stop searching in this direction |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
739 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
740 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
741 |
// continue with the next track |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
742 |
direction = _tpf_new_direction[track]; |
0 | 743 |
assert(direction != 0xFF); |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
744 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
745 |
// check if we're running around chasing our tail... (infinite loop) |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
746 |
if (tile == tile_org) |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
747 |
goto stop_search; |
0 | 748 |
} |
749 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
750 |
// if only one possible track to choose from, just continue |
0 | 751 |
if (KILL_FIRST_BIT(bits) == 0) { |
752 |
i = _new_track[FIND_FIRST_BIT(bits)][direction]; |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
753 |
// call the callback to check if we've reached the destination |
0 | 754 |
if (tpf->enum_proc(tile, tpf->userdata, i, si.cur_length, &si.state)) |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
755 |
goto stop_search; // we should stop searching in this direction. |
0 | 756 |
|
757 |
// we should continue searching. determine new direction. |
|
758 |
direction = _tpf_new_direction[i]; |
|
759 |
goto restart; // use tail recursion optimization. |
|
760 |
} |
|
761 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
762 |
//////////////// |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
763 |
// We got multiple tracks to choose between (intersection). |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
764 |
// Branch the search space into several branches. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
765 |
// Push each alternative on the stack. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
766 |
//////////////// |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
767 |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
768 |
// Increase recursion depth counter, and |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
769 |
// Check so the depth is not too big.. to prevent enourmous slowdown. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
770 |
if (si.depth >= _patches.pf_maxdepth) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
771 |
DEBUG(misc, 4) ("[NTP] depth too big\n"); |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
772 |
goto stop_search; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
773 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
774 |
si.depth++; |
0 | 775 |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
776 |
// Check if we've already visited this intersection. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
777 |
// If we've already visited it with a better length, then |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
778 |
// there's no point in visiting it again. |
0 | 779 |
if (NtpVisit(tpf, tile, direction, si.cur_length)) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
780 |
// Push all possible alternatives that we can reach from here |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
781 |
// onto the priority heap. |
0 | 782 |
si.tile = tile; |
783 |
do { |
|
784 |
si.track = _new_track[FIND_FIRST_BIT(bits)][direction]; |
|
785 |
// out of stack items, bail out? |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
786 |
if (tpf->nstack >= lengthof(tpf->stack)) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
787 |
DEBUG(misc, 4) ("[NTP] out of stack\n"); |
0 | 788 |
break; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
789 |
} |
0 | 790 |
tpf->stack[tpf->nstack] = si; |
791 |
HeapifyUp(tpf); |
|
792 |
} while ((bits = KILL_FIRST_BIT(bits)) != 0); |
|
793 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
794 |
// If this is the first intersection, we need to fill the first_track member. |
0 | 795 |
// so the code outside knows which path is better. |
796 |
// also randomize the order in which we search through them. |
|
797 |
if (si.depth == 1) { |
|
798 |
uint32 r = Random(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
799 |
assert(tpf->nstack == 2 || tpf->nstack == 3); |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
800 |
if (r&1) swap_byte(&tpf->stack[0].track, &tpf->stack[1].track); |
0 | 801 |
if (tpf->nstack != 2) { |
802 |
byte t = tpf->stack[2].track; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
803 |
if (r&2) swap_byte(&tpf->stack[0].track, &t); |
0 | 804 |
if (r&4) swap_byte(&tpf->stack[1].track, &t); |
805 |
tpf->stack[2].first_track = tpf->stack[2].track = t; |
|
806 |
} |
|
807 |
tpf->stack[0].first_track = tpf->stack[0].track; |
|
808 |
tpf->stack[1].first_track = tpf->stack[1].track; |
|
809 |
} |
|
810 |
} |
|
811 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
812 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
813 |
stop_search: |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
814 |
// Now continue searching from the intersection that has the lowest |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
815 |
// cost. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
816 |
// Pop the lowest cost item from the priority heap. |
0 | 817 |
do { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
818 |
if (tpf->nstack == 0) |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
819 |
return; // nothing left? then we're done! |
0 | 820 |
si = tpf->stack[0]; |
821 |
tile = si.tile; |
|
822 |
HeapifyDown(tpf); |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
823 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
824 |
// First check if we've already visited the tile we're just about to continue at. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
825 |
// If we've already visited it, no point in continuing from there. |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
826 |
// Then call the callback. |
0 | 827 |
} while ( |
828 |
!NtpCheck(tpf, tile, _tpf_prev_direction[si.track], si.cur_length) || // already have better path to that tile? |
|
829 |
tpf->enum_proc(tile, tpf->userdata, si.track, si.cur_length, &si.state) |
|
830 |
); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
831 |
|
0 | 832 |
direction = _tpf_new_direction[si.track]; |
833 |
goto restart; |
|
834 |
} |
|
835 |
||
836 |
||
837 |
// new pathfinder for trains. better and faster. |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
838 |
void NewTrainPathfind(TileIndex tile, byte direction, TPFEnumProc *enum_proc, void *data, byte *cache) |
0 | 839 |
{ |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
840 |
NewTrackPathFinder tpf; |
0 | 841 |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
842 |
tpf.userdata = data; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
843 |
tpf.enum_proc = enum_proc; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
844 |
tpf.tracktype = 0; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
845 |
tpf.maxlength = _patches.pf_maxlength; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
846 |
tpf.nstack = 0; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
847 |
tpf.new_link = tpf.links; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
848 |
tpf.num_links_left = lengthof(tpf.links); |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
849 |
memset(tpf.hash_head, 0, sizeof(tpf.hash_head)); |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
850 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
851 |
NTPEnum(&tpf, tile, direction); |
0 | 852 |
} |
853 |