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