author | rubidium |
Thu, 01 Feb 2007 15:49:12 +0000 | |
changeset 5893 | 7e431a4abebb |
parent 5733 | 388bb9dcb79b |
child 6012 | 065d7234a7a9 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1209
diff
changeset
|
4 |
#include "openttd.h" |
3234
a2791a480b71
(svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents:
3184
diff
changeset
|
5 |
#include "bridge_map.h" |
3900
2c84ed52709d
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3894
diff
changeset
|
6 |
#include "station_map.h" |
2c84ed52709d
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3894
diff
changeset
|
7 |
#include "depot.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2153
diff
changeset
|
8 |
#include "functions.h" |
679
04ca2cd69420
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
536
diff
changeset
|
9 |
#include "map.h" |
1209
2e00193652b2
(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
|
10 |
#include "tile.h" |
0 | 11 |
#include "pathfind.h" |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
12 |
#include "rail.h" |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
13 |
#include "debug.h" |
3154 | 14 |
#include "tunnel_map.h" |
2153
ecfc674410b4
(svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents:
2137
diff
changeset
|
15 |
#include "variables.h" |
3735
76dacda0b4da
(svn r4715) - Fix: (FS#109) ? Wrongfully bad signal - Don't allow OPF to enter train depot from the back
KUDr
parents:
3618
diff
changeset
|
16 |
#include "depot.h" |
0 | 17 |
|
18 |
// remember which tiles we have already visited so we don't visit them again. |
|
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
19 |
static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir) |
0 | 20 |
{ |
21 |
uint hash, val, offs; |
|
22 |
TrackPathFinderLink *link, *new_link; |
|
23 |
uint bits = 1 << dir; |
|
24 |
||
25 |
if (tpf->disable_tile_hash) |
|
26 |
return true; |
|
27 |
||
28 |
hash = PATHFIND_HASH_TILE(tile); |
|
29 |
||
30 |
val = tpf->hash_head[hash]; |
|
31 |
||
32 |
if (val == 0) { |
|
33 |
/* unused hash entry, set the appropriate bit in it and return true |
|
34 |
* to indicate that a bit was set. */ |
|
35 |
tpf->hash_head[hash] = bits; |
|
1986
fcc849a38ae6
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
36 |
tpf->hash_tile[hash] = tile; |
0 | 37 |
return true; |
38 |
} else if (!(val & 0x8000)) { |
|
39 |
/* single tile */ |
|
40 |
||
1986
fcc849a38ae6
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
41 |
if (tile == tpf->hash_tile[hash]) { |
0 | 42 |
/* found another bit for the same tile, |
43 |
* check if this bit is already set, if so, return false */ |
|
44 |
if (val & bits) |
|
45 |
return false; |
|
46 |
||
47 |
/* otherwise set the bit and return true to indicate that the bit |
|
48 |
* was set */ |
|
49 |
tpf->hash_head[hash] = val | bits; |
|
50 |
return true; |
|
51 |
} else { |
|
52 |
/* 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
|
53 |
|
0 | 54 |
/* allocate a link. if out of links, handle this by returning |
55 |
* that a tile was already visisted. */ |
|
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
56 |
if (tpf->num_links_left == 0) { |
0 | 57 |
return false; |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
58 |
} |
0 | 59 |
tpf->num_links_left--; |
60 |
link = tpf->new_link++; |
|
61 |
||
62 |
/* move the data that was previously in the hash_??? variables |
|
63 |
* to the link struct, and let the hash variables point to the link */ |
|
64 |
link->tile = tpf->hash_tile[hash]; |
|
65 |
tpf->hash_tile[hash] = PATHFIND_GET_LINK_OFFS(tpf, link); |
|
66 |
||
67 |
link->flags = tpf->hash_head[hash]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
68 |
tpf->hash_head[hash] = 0xFFFF; /* multi link */ |
0 | 69 |
|
70 |
link->next = 0xFFFF; |
|
71 |
} |
|
72 |
} else { |
|
73 |
/* a linked list of many tiles, |
|
74 |
* find the one corresponding to the tile, if it exists. |
|
75 |
* otherwise make a new link */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
76 |
|
0 | 77 |
offs = tpf->hash_tile[hash]; |
78 |
do { |
|
79 |
link = PATHFIND_GET_LINK_PTR(tpf, offs); |
|
1986
fcc849a38ae6
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
80 |
if (tile == link->tile) { |
0 | 81 |
/* found the tile in the link list, |
82 |
* check if the bit was alrady set, if so return false to indicate that the |
|
83 |
* bit was already set */ |
|
84 |
if (link->flags & bits) |
|
85 |
return false; |
|
86 |
link->flags |= bits; |
|
87 |
return true; |
|
88 |
} |
|
89 |
} while ((offs=link->next) != 0xFFFF); |
|
90 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
91 |
|
0 | 92 |
/* get here if we need to add a new link to link, |
93 |
* first, allocate a new link, in the same way as before */ |
|
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
94 |
if (tpf->num_links_left == 0) { |
0 | 95 |
return false; |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
96 |
} |
0 | 97 |
tpf->num_links_left--; |
98 |
new_link = tpf->new_link++; |
|
99 |
||
100 |
/* then fill the link with the new info, and establish a ptr from the old |
|
101 |
* link to the new one */ |
|
1986
fcc849a38ae6
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
102 |
new_link->tile = tile; |
0 | 103 |
new_link->flags = bits; |
104 |
new_link->next = 0xFFFF; |
|
105 |
||
106 |
link->next = PATHFIND_GET_LINK_OFFS(tpf, new_link); |
|
107 |
return true; |
|
108 |
} |
|
109 |
||
110 |
static const byte _bits_mask[4] = { |
|
111 |
0x19, |
|
112 |
0x16, |
|
113 |
0x25, |
|
114 |
0x2A, |
|
115 |
}; |
|
116 |
||
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
117 |
static const DiagDirection _tpf_new_direction[14] = { |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
118 |
DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
119 |
INVALID_DIAGDIR, INVALID_DIAGDIR, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
120 |
DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, |
0 | 121 |
}; |
122 |
||
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
123 |
static const DiagDirection _tpf_prev_direction[14] = { |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
124 |
DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
125 |
INVALID_DIAGDIR, INVALID_DIAGDIR, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
126 |
DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, |
0 | 127 |
}; |
128 |
||
129 |
||
130 |
static const byte _otherdir_mask[4] = { |
|
131 |
0x10, |
|
132 |
0, |
|
133 |
0x5, |
|
134 |
0x2A, |
|
135 |
}; |
|
136 |
||
3153
e83501906eae
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
137 |
static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
0 | 138 |
{ |
139 |
uint bits; |
|
140 |
int i; |
|
141 |
RememberData rd; |
|
747
4bf34cf669d0
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
142 |
|
3618
5cf2fa302933
(svn r4515) -Codechange: TPFMode2 is currently only used for TRANSPORT_WATER. So remove all stuff that deals with other transport types and assert TRANSPORT_WATER
celestar
parents:
3420
diff
changeset
|
143 |
assert(tpf->tracktype == TRANSPORT_WATER); |
0 | 144 |
|
145 |
// This addition will sometimes overflow by a single tile. |
|
146 |
// 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
|
147 |
// tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. |
4559
aa0c13e39840
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4406
diff
changeset
|
148 |
tile = TILE_MASK(tile + TileOffsByDiagDir(direction)); |
0 | 149 |
|
150 |
if (++tpf->rd.cur_length > 50) |
|
151 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
152 |
|
0 | 153 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
154 |
bits = (byte)((bits | (bits >> 8)) & _bits_mask[direction]); |
|
155 |
if (bits == 0) |
|
156 |
return; |
|
157 |
||
926
a6d140a6a4de
(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
|
158 |
assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY()); |
0 | 159 |
|
160 |
if ( (bits & (bits - 1)) == 0 ) { |
|
161 |
/* only one direction */ |
|
162 |
i = 0; |
|
163 |
while (!(bits&1)) |
|
164 |
i++, bits>>=1; |
|
165 |
||
166 |
rd = tpf->rd; |
|
167 |
goto continue_here; |
|
168 |
} |
|
169 |
/* several directions */ |
|
170 |
i=0; |
|
171 |
do { |
|
172 |
if (!(bits & 1)) continue; |
|
173 |
rd = tpf->rd; |
|
174 |
||
175 |
// Change direction 4 times only |
|
176 |
if ((byte)i != tpf->rd.pft_var6) { |
|
2952 | 177 |
if (++tpf->rd.depth > 4) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
178 |
tpf->rd = rd; |
0 | 179 |
return; |
180 |
} |
|
181 |
tpf->rd.pft_var6 = (byte)i; |
|
182 |
} |
|
183 |
||
184 |
continue_here:; |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
185 |
tpf->the_dir = (Trackdir)(i + (HASBIT(_otherdir_mask[direction], i) ? 8 : 0)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
186 |
|
0 | 187 |
if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, NULL)) { |
188 |
TPFMode2(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
189 |
} |
|
190 |
||
191 |
tpf->rd = rd; |
|
192 |
} while (++i, bits>>=1); |
|
193 |
||
194 |
} |
|
195 |
||
196 |
||
22 | 197 |
/* Returns the end tile and the length of a tunnel. The length does not |
198 |
* include the starting tile (entry), it does include the end tile (exit). |
|
199 |
*/ |
|
3420 | 200 |
FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection dir) |
0 | 201 |
{ |
4559
aa0c13e39840
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4406
diff
changeset
|
202 |
TileIndexDiff delta = TileOffsByDiagDir(dir); |
3420 | 203 |
uint z = GetTileZ(tile); |
0 | 204 |
FindLengthOfTunnelResult flotr; |
205 |
||
206 |
flotr.length = 0; |
|
207 |
||
3420 | 208 |
dir = ReverseDiagDir(dir); |
209 |
do { |
|
0 | 210 |
flotr.length++; |
3420 | 211 |
tile += delta; |
212 |
} while( |
|
213 |
!IsTunnelTile(tile) || |
|
214 |
GetTunnelDirection(tile) != dir || |
|
215 |
GetTileZ(tile) != z |
|
216 |
); |
|
0 | 217 |
|
218 |
flotr.tile = tile; |
|
219 |
return flotr; |
|
220 |
} |
|
221 |
||
222 |
static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 }; |
|
223 |
||
3157
3f35e2d9c8e3
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
224 |
static uint SkipToEndOfTunnel(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
225 |
{ |
0 | 226 |
FindLengthOfTunnelResult flotr; |
227 |
TPFSetTileBit(tpf, tile, 14); |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
148
diff
changeset
|
228 |
flotr = FindLengthOfTunnel(tile, direction); |
0 | 229 |
tpf->rd.cur_length += flotr.length; |
230 |
TPFSetTileBit(tpf, flotr.tile, 14); |
|
231 |
return flotr.tile; |
|
232 |
} |
|
233 |
||
234 |
const byte _ffb_64[128] = { |
|
4344
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
235 |
0, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
236 |
3, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
237 |
4, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
238 |
3, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
239 |
5, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
240 |
3, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
241 |
4, 0, 1, 0, 2, 0, 1, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
242 |
3, 0, 1, 0, 2, 0, 1, 0, |
0 | 243 |
|
4344
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
244 |
0, 0, 0, 2, 0, 4, 4, 6, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
245 |
0, 8, 8, 10, 8, 12, 12, 14, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
246 |
0, 16, 16, 18, 16, 20, 20, 22, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
247 |
16, 24, 24, 26, 24, 28, 28, 30, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
248 |
0, 32, 32, 34, 32, 36, 36, 38, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
249 |
32, 40, 40, 42, 40, 44, 44, 46, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
250 |
32, 48, 48, 50, 48, 52, 52, 54, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
251 |
48, 56, 56, 58, 56, 60, 60, 62, |
0 | 252 |
}; |
253 |
||
3153
e83501906eae
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
254 |
static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
0 | 255 |
{ |
5455
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
256 |
uint bits; |
0 | 257 |
int i; |
258 |
RememberData rd; |
|
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
259 |
TileIndex tile_org = tile; |
0 | 260 |
|
5385
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
261 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
262 |
if (IsTunnel(tile)) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
263 |
if (GetTunnelDirection(tile) != direction || |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
264 |
GetTunnelTransportType(tile) != tpf->tracktype) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
265 |
return; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
266 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
267 |
tile = SkipToEndOfTunnel(tpf, tile, direction); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
268 |
} else { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
269 |
TileIndex tile_end; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
270 |
if (GetBridgeRampDirection(tile) != direction || |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
271 |
GetBridgeTransportType(tile) != tpf->tracktype) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
272 |
return; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
273 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
274 |
//fprintf(stderr, "%s: Planning over bridge\n", __func__); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
275 |
// TODO doesn't work - WHAT doesn't work? |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
276 |
TPFSetTileBit(tpf, tile, 14); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
277 |
tile_end = GetOtherBridgeEnd(tile); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
278 |
tpf->rd.cur_length += DistanceManhattan(tile, tile_end); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
279 |
tile = tile_end; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
280 |
TPFSetTileBit(tpf, tile, 14); |
2493
f6b4300cc2b0
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2486
diff
changeset
|
281 |
} |
0 | 282 |
} |
4559
aa0c13e39840
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4406
diff
changeset
|
283 |
tile += TileOffsByDiagDir(direction); |
747
4bf34cf669d0
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
284 |
|
5455
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
285 |
/* Check in case of rail if the owner is the same */ |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
286 |
if (tpf->tracktype == TRANSPORT_RAIL) { |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
287 |
// don't enter train depot from the back |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
288 |
if (IsTileDepotType(tile, TRANSPORT_RAIL) && GetRailDepotDirection(tile) == direction) return; |
5417
a0224e6cedd1
(svn r7620) -Fix: [OPF] signal update was incorrectly propagated:
KUDr
parents:
5385
diff
changeset
|
289 |
|
5455
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
290 |
if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE)) |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
291 |
if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
292 |
if (GetTileOwner(tile_org) != GetTileOwner(tile)) return; |
752
4e4ec58ba45f
(svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents:
747
diff
changeset
|
293 |
} |
747
4bf34cf669d0
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
294 |
|
5455
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
295 |
// check if the new tile can be entered from that direction |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
296 |
if (tpf->tracktype == TRANSPORT_ROAD) { |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
297 |
// road stops and depots now have a track (r4419) |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
298 |
// don't enter road stop from the back |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
299 |
if (IsRoadStopTile(tile) && ReverseDiagDir(GetRoadStopDir(tile)) != direction) return; |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
300 |
// don't enter road depot from the back |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
301 |
if (IsTileDepotType(tile, TRANSPORT_ROAD) && ReverseDiagDir(GetRoadDepotDirection(tile)) != direction) return; |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
302 |
} |
3900
2c84ed52709d
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3894
diff
changeset
|
303 |
|
5457
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
304 |
/* Check if the new tile is a tunnel or bridge head and that the direction |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
305 |
* and transport type match */ |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
306 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
307 |
if (IsTunnel(tile)) { |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
308 |
if (GetTunnelDirection(tile) != direction || |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
309 |
GetTunnelTransportType(tile) != tpf->tracktype) { |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
310 |
return; |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
311 |
} |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
312 |
} else if (IsBridge(tile)) { |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
313 |
if (GetBridgeRampDirection(tile) != direction || |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
314 |
GetBridgeTransportType(tile) != tpf->tracktype) { |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
315 |
return; |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
316 |
} |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
317 |
} |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
318 |
} |
a0591c7d9db8
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5456
diff
changeset
|
319 |
|
0 | 320 |
tpf->rd.cur_length++; |
321 |
||
5455
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
322 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
b3ceb5649239
(svn r7716) -Revert r7620: Changes introduced more problems than they fixed (and a goto?)
peter1138
parents:
5417
diff
changeset
|
323 |
|
0 | 324 |
if ((byte)bits != tpf->var2) { |
325 |
bits &= _tpfmode1_and[direction]; |
|
326 |
bits = bits | (bits>>8); |
|
327 |
} |
|
328 |
bits &= 0xBF; |
|
329 |
||
330 |
if (bits != 0) { |
|
331 |
if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KILL_FIRST_BIT(bits) == 0 || ++tpf->rd.depth <= 7))) { |
|
332 |
do { |
|
333 |
i = FIND_FIRST_BIT(bits); |
|
334 |
bits = KILL_FIRST_BIT(bits); |
|
335 |
||
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
336 |
tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i); |
0 | 337 |
rd = tpf->rd; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
338 |
|
0 | 339 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && |
340 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) { |
|
341 |
TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
342 |
} |
|
343 |
tpf->rd = rd; |
|
344 |
} while (bits != 0); |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
/* the next is only used when signals are checked. |
|
349 |
* 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
|
350 |
|
0 | 351 |
/* 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
|
352 |
* stack space dramatically. */ |
2006
9d5d7fd428c2
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
353 |
|
9d5d7fd428c2
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
354 |
/* If we are doing signal setting, we must reverse at evere tile, so we |
9d5d7fd428c2
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
355 |
* iterate all the tracks in a signal block, even when a normal train would |
9d5d7fd428c2
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1986
diff
changeset
|
356 |
* not reach it (for example, when two lines merge */ |
0 | 357 |
if (tpf->hasbit_13) |
358 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
359 |
|
3153
e83501906eae
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
360 |
direction = ReverseDiagDir(direction); |
5456
6ae660a4f3a5
(svn r7717) -Fix (runknown): When following path for signals, don't skip back to the
peter1138
parents:
5455
diff
changeset
|
361 |
tile += TileOffsByDiagDir(direction); |
0 | 362 |
|
363 |
bits = GetTileTrackStatus(tile, tpf->tracktype); |
|
364 |
bits |= (bits >> 8); |
|
365 |
||
366 |
if ( (byte)bits != tpf->var2) { |
|
367 |
bits &= _bits_mask[direction]; |
|
368 |
} |
|
369 |
||
370 |
bits &= 0xBF; |
|
371 |
if (bits == 0) |
|
372 |
return; |
|
373 |
||
374 |
do { |
|
375 |
i = FIND_FIRST_BIT(bits); |
|
376 |
bits = KILL_FIRST_BIT(bits); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
377 |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
378 |
tpf->the_dir = (Trackdir)((_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i); |
0 | 379 |
rd = tpf->rd; |
380 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && |
|
381 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) { |
|
382 |
TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]); |
|
383 |
} |
|
384 |
tpf->rd = rd; |
|
385 |
} while (bits != 0); |
|
386 |
} |
|
387 |
||
3153
e83501906eae
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
388 |
void FollowTrack(TileIndex tile, uint16 flags, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data) |
0 | 389 |
{ |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
390 |
TrackPathFinder tpf; |
0 | 391 |
|
392 |
assert(direction < 4); |
|
393 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
394 |
/* initialize path finder variables */ |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
395 |
tpf.userdata = data; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
396 |
tpf.enum_proc = enum_proc; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
397 |
tpf.new_link = tpf.links; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
398 |
tpf.num_links_left = lengthof(tpf.links); |
0 | 399 |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
400 |
tpf.rd.cur_length = 0; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
401 |
tpf.rd.depth = 0; |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
402 |
tpf.rd.pft_var6 = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
403 |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
404 |
tpf.var2 = HASBIT(flags, 15) ? 0x43 : 0xFF; /* 0x8000 */ |
0 | 405 |
|
3132
4ecd7f40fc76
(svn r3747) Change HASBIT() to return 0/1 instead of 0/value of tested bit, because the name suggests it does the former and current behavior broke in some places in very subtle ways (for example HASBIT(x, 0) != HASBIT(y, 1) doesn't work, returning a bool after HASBIT(x, 9) neither)
tron
parents:
3102
diff
changeset
|
406 |
tpf.disable_tile_hash = HASBIT(flags, 12); /* 0x1000 */ |
4ecd7f40fc76
(svn r3747) Change HASBIT() to return 0/1 instead of 0/value of tested bit, because the name suggests it does the former and current behavior broke in some places in very subtle ways (for example HASBIT(x, 0) != HASBIT(y, 1) doesn't work, returning a bool after HASBIT(x, 9) neither)
tron
parents:
3102
diff
changeset
|
407 |
tpf.hasbit_13 = HASBIT(flags, 13); /* 0x2000 */ |
0 | 408 |
|
409 |
||
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
410 |
tpf.tracktype = (TransportType)(flags & 0xFF); |
0 | 411 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
412 |
if (HASBIT(flags, 11)) { |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
413 |
tpf.rd.pft_var6 = 0xFF; |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
414 |
tpf.enum_proc(tile, data, INVALID_TRACKDIR, 0, 0); |
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
415 |
TPFMode2(&tpf, tile, direction); |
0 | 416 |
} else { |
417 |
/* clear the hash_heads */ |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
418 |
memset(tpf.hash_head, 0, sizeof(tpf.hash_head)); |
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
419 |
TPFMode1(&tpf, tile, direction); |
0 | 420 |
} |
421 |
||
422 |
if (after_proc != NULL) |
|
318
648afd1ab9a7
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
241
diff
changeset
|
423 |
after_proc(&tpf); |
0 | 424 |
} |
425 |
||
426 |
typedef struct { |
|
427 |
TileIndex tile; |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
428 |
uint16 cur_length; // This is the current length to this tile. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
429 |
uint16 priority; // This is the current length + estimated length to the goal. |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
430 |
TrackdirByte track; |
0 | 431 |
byte depth; |
432 |
byte state; |
|
433 |
byte first_track; |
|
434 |
} StackedItem; |
|
435 |
||
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
436 |
static const Trackdir _new_trackdir[6][4] = { |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
437 |
{TRACKDIR_X_NE, INVALID_TRACKDIR, TRACKDIR_X_SW, INVALID_TRACKDIR,}, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
438 |
{INVALID_TRACKDIR, TRACKDIR_Y_SE, INVALID_TRACKDIR, TRACKDIR_Y_NW,}, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
439 |
{INVALID_TRACKDIR, TRACKDIR_UPPER_E, TRACKDIR_UPPER_W, INVALID_TRACKDIR,}, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
440 |
{TRACKDIR_LOWER_E, INVALID_TRACKDIR, INVALID_TRACKDIR, TRACKDIR_LOWER_W,}, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
441 |
{TRACKDIR_LEFT_N, TRACKDIR_LEFT_S, INVALID_TRACKDIR, INVALID_TRACKDIR,}, |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
442 |
{INVALID_TRACKDIR, INVALID_TRACKDIR, TRACKDIR_RIGHT_S, TRACKDIR_RIGHT_N,}, |
0 | 443 |
}; |
444 |
||
445 |
typedef struct HashLink { |
|
446 |
TileIndex tile; |
|
447 |
uint16 typelength; |
|
448 |
uint16 next; |
|
449 |
} HashLink; |
|
450 |
||
451 |
typedef struct { |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
452 |
NTPEnumProc *enum_proc; |
0 | 453 |
void *userdata; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
454 |
TileIndex dest; |
0 | 455 |
|
3355
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
456 |
TransportType tracktype; |
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
457 |
RailTypeMask railtypes; |
0 | 458 |
uint maxlength; |
459 |
||
460 |
HashLink *new_link; |
|
461 |
uint num_links_left; |
|
462 |
||
979
11ea18598e16
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
463 |
uint nstack; |
0 | 464 |
StackedItem stack[256]; // priority queue of stacked items |
465 |
||
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
466 |
uint16 hash_head[0x400]; // hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir |
0 | 467 |
TileIndex hash_tile[0x400]; // tiles. or links. |
468 |
||
469 |
HashLink links[0x400]; // hash links |
|
470 |
||
471 |
} NewTrackPathFinder; |
|
472 |
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links) |
|
473 |
#define NTP_GET_LINK_PTR(tpf, link_offs) (HashLink*)((byte*)tpf->links + (link_offs)) |
|
474 |
||
475 |
#define ARR(i) tpf->stack[(i)-1] |
|
476 |
||
477 |
// called after a new element was added in the queue at the last index. |
|
478 |
// move it down to the proper position |
|
536 | 479 |
static inline void HeapifyUp(NewTrackPathFinder *tpf) |
0 | 480 |
{ |
481 |
StackedItem si; |
|
482 |
int i = ++tpf->nstack; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
483 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
484 |
while (i != 1 && ARR(i).priority < ARR(i>>1).priority) { |
0 | 485 |
// 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
|
486 |
// swap the child item and the parent item. |
0 | 487 |
si = ARR(i); ARR(i) = ARR(i>>1); ARR(i>>1) = si; |
488 |
i>>=1; |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
// called after the element 0 was eaten. fill it with a new element |
|
536 | 493 |
static inline void HeapifyDown(NewTrackPathFinder *tpf) |
0 | 494 |
{ |
495 |
StackedItem si; |
|
496 |
int i = 1, j; |
|
979
11ea18598e16
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
497 |
int n; |
11ea18598e16
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
498 |
|
11ea18598e16
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
499 |
assert(tpf->nstack > 0); |
11ea18598e16
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
500 |
n = --tpf->nstack; |
0 | 501 |
|
502 |
if (n == 0) return; // heap is empty so nothing to do? |
|
503 |
||
504 |
// copy the last item to index 0. we use it as base for heapify. |
|
505 |
ARR(1) = ARR(n+1); |
|
506 |
||
507 |
while ((j=i*2) <= n) { |
|
508 |
// figure out which is smaller of the children. |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
509 |
if (j != n && ARR(j).priority > ARR(j+1).priority) |
0 | 510 |
j++; // right item is smaller |
511 |
||
512 |
assert(i <= n && j <= n); |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
513 |
if (ARR(i).priority <= ARR(j).priority) |
0 | 514 |
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
|
515 |
|
0 | 516 |
// swap parent with the child |
517 |
si = ARR(i); ARR(i) = ARR(j); ARR(j) = si; |
|
518 |
i = j; |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
// mark a tile as visited and store the length of the path. |
|
523 |
// if we already had a better path to this tile, return false. |
|
524 |
// otherwise return true. |
|
3153
e83501906eae
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
525 |
static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir, uint length) |
0 | 526 |
{ |
527 |
uint hash,head; |
|
528 |
HashLink *link, *new_link; |
|
529 |
||
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
530 |
assert(length < 16384-1); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
531 |
|
0 | 532 |
hash = PATHFIND_HASH_TILE(tile); |
533 |
||
534 |
// never visited before? |
|
535 |
if ((head=tpf->hash_head[hash]) == 0) { |
|
536 |
tpf->hash_tile[hash] = tile; |
|
537 |
tpf->hash_head[hash] = dir | (length << 2); |
|
538 |
return true; |
|
539 |
} |
|
540 |
||
541 |
if (head != 0xffff) { |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
542 |
if (tile == tpf->hash_tile[hash] && (head & 0x3) == (uint)dir) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
543 |
|
0 | 544 |
// longer length |
545 |
if (length >= (head >> 2)) return false; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
546 |
|
0 | 547 |
tpf->hash_head[hash] = dir | (length << 2); |
548 |
return true; |
|
549 |
} |
|
550 |
// two tiles with the same hash, need to make a link |
|
551 |
// allocate a link. if out of links, handle this by returning |
|
552 |
// that a tile was already visisted. |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
553 |
if (tpf->num_links_left == 0) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
554 |
DEBUG(ntp, 1, "No links left"); |
0 | 555 |
return false; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
556 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
557 |
|
0 | 558 |
tpf->num_links_left--; |
559 |
link = tpf->new_link++; |
|
560 |
||
561 |
/* move the data that was previously in the hash_??? variables |
|
562 |
* to the link struct, and let the hash variables point to the link */ |
|
563 |
link->tile = tpf->hash_tile[hash]; |
|
564 |
tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link); |
|
565 |
||
566 |
link->typelength = tpf->hash_head[hash]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
567 |
tpf->hash_head[hash] = 0xFFFF; /* multi link */ |
0 | 568 |
link->next = 0xFFFF; |
569 |
} else { |
|
570 |
// a linked list of many tiles, |
|
571 |
// find the one corresponding to the tile, if it exists. |
|
572 |
// otherwise make a new link |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
573 |
|
0 | 574 |
uint offs = tpf->hash_tile[hash]; |
575 |
do { |
|
576 |
link = NTP_GET_LINK_PTR(tpf, offs); |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
577 |
if (tile == link->tile && (link->typelength & 0x3U) == (uint)dir) { |
3019
9d641b5e630b
(svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents:
3017
diff
changeset
|
578 |
if (length >= (uint)(link->typelength >> 2)) return false; |
0 | 579 |
link->typelength = dir | (length << 2); |
580 |
return true; |
|
581 |
} |
|
3017
a75caf4efa2d
(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
|
582 |
} while ((offs = link->next) != 0xFFFF); |
0 | 583 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
584 |
|
0 | 585 |
/* get here if we need to add a new link to link, |
586 |
* first, allocate a new link, in the same way as before */ |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
587 |
if (tpf->num_links_left == 0) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
588 |
DEBUG(ntp, 1, "No links left"); |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
589 |
return false; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
590 |
} |
0 | 591 |
tpf->num_links_left--; |
592 |
new_link = tpf->new_link++; |
|
593 |
||
594 |
/* then fill the link with the new info, and establish a ptr from the old |
|
595 |
* link to the new one */ |
|
1986
fcc849a38ae6
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
596 |
new_link->tile = tile; |
0 | 597 |
new_link->typelength = dir | (length << 2); |
598 |
new_link->next = 0xFFFF; |
|
599 |
||
600 |
link->next = NTP_GET_LINK_OFFS(tpf, new_link); |
|
601 |
return true; |
|
602 |
} |
|
603 |
||
2782 | 604 |
/** |
605 |
* Checks if the shortest path to the given tile/dir so far is still the given |
|
606 |
* length. |
|
607 |
* @return true if the length is still the same |
|
608 |
* @pre The given tile/dir combination should be present in the hash, by a |
|
609 |
* previous call to NtpVisit(). |
|
610 |
*/ |
|
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
611 |
static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length) |
0 | 612 |
{ |
613 |
uint hash,head,offs; |
|
614 |
HashLink *link; |
|
615 |
||
616 |
hash = PATHFIND_HASH_TILE(tile); |
|
617 |
head=tpf->hash_head[hash]; |
|
618 |
assert(head); |
|
619 |
||
620 |
if (head != 0xffff) { |
|
621 |
assert( tpf->hash_tile[hash] == tile && (head & 3) == dir); |
|
622 |
assert( (head >> 2) <= length); |
|
623 |
return length == (head >> 2); |
|
624 |
} |
|
625 |
||
626 |
// else it's a linked list of many tiles |
|
627 |
offs = tpf->hash_tile[hash]; |
|
2952 | 628 |
for (;;) { |
0 | 629 |
link = NTP_GET_LINK_PTR(tpf, offs); |
3017
a75caf4efa2d
(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
|
630 |
if (tile == link->tile && (link->typelength & 0x3U) == dir) { |
3019
9d641b5e630b
(svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents:
3017
diff
changeset
|
631 |
assert((uint)(link->typelength >> 2) <= length); |
9d641b5e630b
(svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents:
3017
diff
changeset
|
632 |
return length == (uint)(link->typelength >> 2); |
0 | 633 |
} |
634 |
offs = link->next; |
|
635 |
assert(offs != 0xffff); |
|
636 |
} |
|
637 |
} |
|
638 |
||
639 |
||
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
640 |
static const uint16 _is_upwards_slope[15] = { |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
641 |
0, // no tileh |
3102
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
642 |
(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_NW), // 1 |
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
643 |
(1 << TRACKDIR_X_SW) | (1 << TRACKDIR_Y_SE), // 2 |
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
644 |
(1 << TRACKDIR_X_SW), // 3 |
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
645 |
(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_SE), // 4 |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
646 |
0, // 5 |
3102
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
647 |
(1 << TRACKDIR_Y_SE), // 6 |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
648 |
0, // 7 |
3102
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
649 |
(1 << TRACKDIR_X_NE) | (1 << TRACKDIR_Y_NW), // 8, |
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
650 |
(1 << TRACKDIR_Y_NW), // 9 |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
651 |
0, //10 |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
652 |
0, //11, |
3102
f9ca4379db9d
(svn r3697) Rename DIAG1/DIAG2 to X resp. Y as this conveys a bit better in which direction a pieces of rail goes
tron
parents:
3019
diff
changeset
|
653 |
(1 << TRACKDIR_X_NE), //12 |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
654 |
0, //13 |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
655 |
0, //14 |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
656 |
}; |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
657 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
658 |
static uint DistanceMoo(TileIndex t0, TileIndex t1) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
659 |
{ |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
660 |
const uint dx = delta(TileX(t0), TileX(t1)); |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
661 |
const uint dy = delta(TileY(t0), TileY(t1)); |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
662 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
663 |
const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */ |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
664 |
/* OPTIMISATION: |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
665 |
* Original: diagTracks = max(dx, dy) - min(dx,dy); |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
666 |
* Proof: |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
667 |
* (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */ |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
668 |
const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */ |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
669 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
670 |
return diagTracks*DIAG_FACTOR + straightTracks*STR_FACTOR; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
671 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
672 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
673 |
// These has to be small cause the max length of a track |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
674 |
// is currently limited to 16384 |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
675 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
676 |
static const byte _length_of_track[16] = { |
4344
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
677 |
DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4081
diff
changeset
|
678 |
DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0 |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
679 |
}; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
680 |
|
0 | 681 |
// new more optimized pathfinder for trains... |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
682 |
// Tile is the tile the train is at. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
683 |
// direction is the tile the train is moving towards. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
684 |
|
3153
e83501906eae
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
685 |
static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
0 | 686 |
{ |
2782 | 687 |
TrackBits bits, allbits; |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
688 |
Trackdir track; |
2782 | 689 |
TileIndex tile_org; |
0 | 690 |
StackedItem si; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
691 |
int estimation; |
0 | 692 |
|
2261
d3554e5d3e86
(svn r2781) Fix some of the issues with variables in .h files.
ludde
parents:
2186
diff
changeset
|
693 |
|
2136
c87ed6a3c15f
(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
|
694 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
695 |
// Need to have a special case for the start. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
696 |
// We shouldn't call the callback for the current tile. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
697 |
si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value. |
0 | 698 |
si.depth = 0; |
699 |
si.state = 0; |
|
2136
c87ed6a3c15f
(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
|
700 |
si.first_track = 0xFF; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
701 |
goto start_at; |
0 | 702 |
|
2952 | 703 |
for (;;) { |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
704 |
// Get the next item to search from from the priority queue |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
705 |
do { |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
706 |
if (tpf->nstack == 0) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
707 |
return; // nothing left? then we're done! |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
708 |
si = tpf->stack[0]; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
709 |
tile = si.tile; |
0 | 710 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
711 |
HeapifyDown(tpf); |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
712 |
// Make sure we havn't already visited this tile. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
713 |
} while (!NtpCheck(tpf, tile, _tpf_prev_direction[si.track], si.cur_length)); |
0 | 714 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
715 |
// Add the length of this track. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
716 |
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
|
717 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
718 |
callback_and_continue: |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
719 |
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length)) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
720 |
return; |
0 | 721 |
|
2136
c87ed6a3c15f
(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
|
722 |
assert(si.track <= 13); |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
723 |
direction = _tpf_new_direction[si.track]; |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
724 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
725 |
start_at: |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
726 |
// If the tile is the entry tile of a tunnel, and we're not going out of the tunnel, |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
727 |
// need to find the exit of the tunnel. |
5385
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
728 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
729 |
if (IsTunnel(tile)) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
730 |
if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
731 |
FindLengthOfTunnelResult flotr; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
732 |
|
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
733 |
/* We are not just driving out of the tunnel */ |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
734 |
if (GetTunnelDirection(tile) != direction || |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
735 |
GetTunnelTransportType(tile) != tpf->tracktype) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
736 |
// We are not driving into the tunnel, or it is an invalid tunnel |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
737 |
continue; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
738 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
739 |
if (!HASBIT(tpf->railtypes, GetRailType(tile))) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
740 |
bits = TRACK_BIT_NONE; |
5385
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
741 |
break; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
742 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
743 |
flotr = FindLengthOfTunnel(tile, direction); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
744 |
si.cur_length += flotr.length * DIAG_FACTOR; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
745 |
tile = flotr.tile; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
746 |
// tile now points to the exit tile of the tunnel |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
747 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
748 |
} else { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
749 |
TileIndex tile_end; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
750 |
if (GetBridgeRampDirection(tile) != ReverseDiagDir(direction)) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
751 |
// We are not just leaving the bridge |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
752 |
if (GetBridgeRampDirection(tile) != direction || |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
753 |
GetBridgeTransportType(tile) != tpf->tracktype) { |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
754 |
// Not entering the bridge or not compatible |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
755 |
continue; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
756 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
757 |
} |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
758 |
tile_end = GetOtherBridgeEnd(tile); |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
759 |
si.cur_length += DistanceManhattan(tile, tile_end) * DIAG_FACTOR; |
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
760 |
tile = tile_end; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
761 |
} |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
762 |
} |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
763 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
764 |
// This is a special loop used to go through |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
765 |
// a rail net and find the first intersection |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
766 |
tile_org = tile; |
2952 | 767 |
for (;;) { |
2136
c87ed6a3c15f
(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
|
768 |
assert(direction <= 3); |
4559
aa0c13e39840
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4406
diff
changeset
|
769 |
tile += TileOffsByDiagDir(direction); |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
770 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
771 |
// too long search length? bail out. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
772 |
if (si.cur_length >= tpf->maxlength) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
773 |
DEBUG(ntp, 1, "Cur_length too big"); |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
774 |
bits = TRACK_BIT_NONE; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
775 |
break; |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
776 |
} |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
777 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
778 |
// Not a regular rail tile? |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
779 |
// Then we can't use the code below, but revert to more general code. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
780 |
if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) { |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
781 |
// We found a tile which is not a normal railway tile. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
782 |
// Determine which tracks that exist on this tile. |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
783 |
uint32 ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _tpfmode1_and[direction]; |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
784 |
bits = TrackdirBitsToTrackBits((TrackdirBits)(ts & TRACKDIR_BIT_MASK)); |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
785 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
786 |
// Check that the tile contains exactly one track |
2782 | 787 |
if (bits == 0 || KILL_FIRST_BIT(bits) != 0) break; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
788 |
|
5385
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
789 |
if (!HASBIT(tpf->railtypes, IsTileType(tile, MP_STREET) ? GetRailTypeCrossing(tile) : GetRailType(tile))) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
790 |
bits = TRACK_BIT_NONE; |
5385
3868f2e6db9b
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5380
diff
changeset
|
791 |
break; |
3804
53ad7c93c3d5
(svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents:
3735
diff
changeset
|
792 |
} |
53ad7c93c3d5
(svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents:
3735
diff
changeset
|
793 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
794 |
/////////////////// |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
795 |
// If we reach here, the tile has exactly one track. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
796 |
// tile - index to a tile that is not rail tile, but still straight (with optional signals) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
797 |
// bits - bitmask of which track that exist on the tile (exactly one bit is set) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
798 |
// direction - which direction are we moving in? |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
799 |
/////////////////// |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
800 |
si.track = _new_trackdir[FIND_FIRST_BIT(bits)][direction]; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
801 |
si.cur_length += _length_of_track[si.track]; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
802 |
goto callback_and_continue; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
803 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
804 |
|
2782 | 805 |
/* Regular rail tile, determine which tracks exist. */ |
3267
feff95208a9f
(svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents:
3234
diff
changeset
|
806 |
allbits = GetTrackBits(tile); |
2782 | 807 |
/* Which tracks are reachable? */ |
808 |
bits = allbits & DiagdirReachesTracks(direction); |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
809 |
|
2782 | 810 |
/* The tile has no reachable tracks => End of rail segment |
811 |
* or Intersection => End of rail segment. We check this agains all the |
|
812 |
* bits, not just reachable ones, to prevent infinite loops. */ |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
813 |
if (bits == TRACK_BIT_NONE || TracksOverlap(allbits)) break; |
2137
bbf04957feec
(svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents:
2136
diff
changeset
|
814 |
|
3355
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
815 |
if (!HASBIT(tpf->railtypes, GetRailType(tile))) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
816 |
bits = TRACK_BIT_NONE; |
3355
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
817 |
break; |
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
818 |
} |
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
819 |
|
2782 | 820 |
/* If we reach here, the tile has exactly one track, and this |
821 |
track is reachable => Rail segment continues */ |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
822 |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
823 |
track = _new_trackdir[FIND_FIRST_BIT(bits)][direction]; |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
824 |
assert(track != INVALID_TRACKDIR); |
2137
bbf04957feec
(svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents:
2136
diff
changeset
|
825 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
826 |
si.cur_length += _length_of_track[track]; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
827 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
828 |
// Check if this rail is an upwards slope. If it is, then add a penalty. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
829 |
// Small optimization here.. if (track&7)>1 then it can't be a slope so we avoid calling GetTileSlope |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
830 |
if ((track & 7) <= 1 && (_is_upwards_slope[GetTileSlope(tile, NULL)] & (1 << track)) ) { |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
831 |
// upwards slope. add some penalty. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
832 |
si.cur_length += 4*DIAG_FACTOR; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
833 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
834 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
835 |
// railway tile with signals..? |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
836 |
if (HasSignals(tile)) { |
4631
584a6da986b0
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
837 |
if (!HasSignalOnTrackdir(tile, track)) { |
2782 | 838 |
// if one way signal not pointing towards us, stop going in this direction => End of rail segment. |
4631
584a6da986b0
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
839 |
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
840 |
bits = TRACK_BIT_NONE; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
841 |
break; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
842 |
} |
4631
584a6da986b0
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
843 |
} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) { |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
844 |
// green signal in our direction. either one way or two way. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
845 |
si.state |= 3; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
846 |
} else { |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
847 |
// reached a red signal. |
4631
584a6da986b0
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
848 |
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) { |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
849 |
// two way red signal. unless we passed another green signal on the way, |
2782 | 850 |
// stop going in this direction => End of rail segment. |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
851 |
// this is to prevent us from going into a full platform. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
852 |
if (!(si.state&1)) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
853 |
bits = TRACK_BIT_NONE; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
854 |
break; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
855 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
856 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
857 |
if (!(si.state & 2)) { |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
858 |
// Is this the first signal we see? And it's red... add penalty |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
859 |
si.cur_length += 10*DIAG_FACTOR; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
860 |
si.state += 2; // remember that we added penalty. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
861 |
// Because we added a penalty, we can't just continue as usual. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
862 |
// Need to get out and let A* do it's job with |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
863 |
// possibly finding an even shorter path. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
864 |
break; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
865 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
866 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
867 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
868 |
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length)) |
2782 | 869 |
return; /* Don't process this tile any further */ |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
870 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
871 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
872 |
// continue with the next track |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
873 |
direction = _tpf_new_direction[track]; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
874 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
875 |
// safety check if we're running around chasing our tail... (infinite loop) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
876 |
if (tile == tile_org) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
877 |
bits = TRACK_BIT_NONE; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
878 |
break; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
879 |
} |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
880 |
} |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
881 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
882 |
// There are no tracks to choose between. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
883 |
// Stop searching in this direction |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
884 |
if (bits == TRACK_BIT_NONE) |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
885 |
continue; |
0 | 886 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
887 |
//////////////// |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
888 |
// We got multiple tracks to choose between (intersection). |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
889 |
// Branch the search space into several branches. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
890 |
//////////////// |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
891 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
892 |
// Check if we've already visited this intersection. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
893 |
// If we've already visited it with a better length, then |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
894 |
// there's no point in visiting it again. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
895 |
if (!NtpVisit(tpf, tile, direction, si.cur_length)) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
896 |
continue; |
0 | 897 |
|
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
898 |
// Push all possible alternatives that we can reach from here |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
899 |
// onto the priority heap. |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
900 |
// 'bits' contains the tracks that we can choose between. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
901 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
902 |
// First compute the estimated distance to the target. |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
903 |
// This is used to implement A* |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
904 |
estimation = 0; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
905 |
if (tpf->dest != 0) |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
906 |
estimation = DistanceMoo(tile, tpf->dest); |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
907 |
|
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
908 |
si.depth++; |
2774
8ab8627b5ba1
(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
|
909 |
if (si.depth == 0) |
8ab8627b5ba1
(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
|
910 |
continue; /* We overflowed our depth. No more searching in this direction. */ |
0 | 911 |
si.tile = tile; |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
912 |
while (bits != TRACK_BIT_NONE) { |
5598
2fadbd43709d
(svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents:
5587
diff
changeset
|
913 |
Track track = RemoveFirstTrack(&bits); |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
914 |
si.track = _new_trackdir[track][direction]; |
2782 | 915 |
assert(si.track != 0xFF); |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
916 |
si.priority = si.cur_length + estimation; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
917 |
|
0 | 918 |
// out of stack items, bail out? |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
919 |
if (tpf->nstack >= lengthof(tpf->stack)) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
920 |
DEBUG(ntp, 1, "Out of stack"); |
0 | 921 |
break; |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
922 |
} |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
923 |
|
0 | 924 |
tpf->stack[tpf->nstack] = si; |
925 |
HeapifyUp(tpf); |
|
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
926 |
}; |
0 | 927 |
|
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
928 |
// If this is the first intersection, we need to fill the first_track member. |
0 | 929 |
// so the code outside knows which path is better. |
930 |
// also randomize the order in which we search through them. |
|
931 |
if (si.depth == 1) { |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
932 |
assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3); |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
933 |
if (tpf->nstack != 1) { |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
934 |
uint32 r = Random(); |
5733 | 935 |
if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track); |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
936 |
if (tpf->nstack != 2) { |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
937 |
TrackdirByte t = tpf->stack[2].track; |
5733 | 938 |
if (r & 2) Swap(tpf->stack[0].track, t); |
939 |
if (r & 4) Swap(tpf->stack[1].track, t); |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
940 |
tpf->stack[2].first_track = tpf->stack[2].track = t; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
941 |
} |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
942 |
tpf->stack[0].first_track = tpf->stack[0].track; |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
943 |
tpf->stack[1].first_track = tpf->stack[1].track; |
0 | 944 |
} |
945 |
} |
|
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
946 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
947 |
// Continue with the next from the queue... |
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
948 |
} |
0 | 949 |
} |
950 |
||
951 |
||
952 |
// new pathfinder for trains. better and faster. |
|
3355
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
953 |
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypeMask railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data) |
0 | 954 |
{ |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
955 |
NewTrackPathFinder tpf; |
0 | 956 |
|
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
957 |
tpf.dest = dest; |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
958 |
tpf.userdata = data; |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
959 |
tpf.enum_proc = enum_proc; |
3355
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
960 |
tpf.tracktype = TRANSPORT_RAIL; |
e414a0b104a6
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3267
diff
changeset
|
961 |
tpf.railtypes = railtypes; |
2125
edc17858f9f6
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
962 |
tpf.maxlength = min(_patches.pf_maxlength * 3, 10000); |
2044
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
963 |
tpf.nstack = 0; |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
964 |
tpf.new_link = tpf.links; |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
965 |
tpf.num_links_left = lengthof(tpf.links); |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
966 |
memset(tpf.hash_head, 0, sizeof(tpf.hash_head)); |
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
967 |
|
df63b9a7dec3
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
968 |
NTPEnum(&tpf, tile, direction); |
0 | 969 |
} |