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