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