author | truebrain |
Sun, 15 Jun 2008 22:37:35 +0000 | |
branch | noai |
changeset 10972 | 986675d19245 |
parent 10920 | e33442a2b239 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10249
diff
changeset
|
3 |
/** @file pathfind.cpp Implementation of the oldest supported pathfinder. */ |
9505 | 4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1209
diff
changeset
|
6 |
#include "openttd.h" |
3234
986c30171e92
(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
4984308f9125
(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" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
9 |
#include "tile_cmd.h" |
9599 | 10 |
#include "landscape.h" |
0 | 11 |
#include "pathfind.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
12 |
#include "rail_type.h" |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
13 |
#include "debug.h" |
3154 | 14 |
#include "tunnel_map.h" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
15 |
#include "settings_type.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
16 |
#include "tunnelbridge_map.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
17 |
#include "core/random_func.hpp" |
10181
54df587fef5d
(svn r12712) [NoAI] -Sync with trunk r12672:12711.
rubidium
parents:
9869
diff
changeset
|
18 |
#include "core/alloc_type.hpp" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
19 |
#include "tunnelbridge.h" |
0 | 20 |
|
9505 | 21 |
/* remember which tiles we have already visited so we don't visit them again. */ |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
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
5dd3db2b86d7
(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
5dd3db2b86d7
(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
68ec4a2f2d79
(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
68ec4a2f2d79
(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]; |
|
9505 | 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
5dd3db2b86d7
(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
68ec4a2f2d79
(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
68ec4a2f2d79
(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
5dd3db2b86d7
(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 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
113 |
static void TPFModeShip(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
0 | 114 |
{ |
10920 | 115 |
assert(tpf->tracktype == TRANSPORT_WATER); |
747
e795750b96de
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
116 |
|
10920 | 117 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
118 |
/* wrong track type */ |
|
119 |
if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) return; |
|
120 |
||
121 |
DiagDirection dir = GetTunnelBridgeDirection(tile); |
|
122 |
/* entering tunnel / bridge? */ |
|
123 |
if (dir == direction) { |
|
124 |
TileIndex endtile = GetOtherTunnelBridgeEnd(tile); |
|
125 |
||
126 |
tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1; |
|
127 |
||
128 |
TPFSetTileBit(tpf, tile, 14); |
|
129 |
TPFSetTileBit(tpf, endtile, 14); |
|
130 |
||
131 |
tile = endtile; |
|
132 |
} else { |
|
133 |
/* leaving tunnel / bridge? */ |
|
134 |
if (ReverseDiagDir(dir) != direction) return; |
|
135 |
} |
|
136 |
} |
|
0 | 137 |
|
9505 | 138 |
/* This addition will sometimes overflow by a single tile. |
139 |
* The use of TILE_MASK here makes sure that we still point at a valid |
|
140 |
* tile, and then this tile will be in the sentinel row/col, so GetTileTrackStatus will fail. */ |
|
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4406
diff
changeset
|
141 |
tile = TILE_MASK(tile + TileOffsByDiagDir(direction)); |
0 | 142 |
|
143 |
if (++tpf->rd.cur_length > 50) |
|
144 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
145 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
146 |
TrackBits bits = TrackStatusToTrackBits(GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type)) & DiagdirReachesTracks(direction); |
9732 | 147 |
if (bits == TRACK_BIT_NONE) return; |
0 | 148 |
|
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
913
diff
changeset
|
149 |
assert(TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY()); |
0 | 150 |
|
9732 | 151 |
bool only_one_track = true; |
0 | 152 |
do { |
9732 | 153 |
Track track = RemoveFirstTrack(&bits); |
154 |
if (bits != TRACK_BIT_NONE) only_one_track = false; |
|
10920 | 155 |
RememberData rd = tpf->rd; |
0 | 156 |
|
9505 | 157 |
/* Change direction 4 times only */ |
9732 | 158 |
if (!only_one_track && track != tpf->rd.last_choosen_track) { |
2952 | 159 |
if (++tpf->rd.depth > 4) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
160 |
tpf->rd = rd; |
0 | 161 |
return; |
162 |
} |
|
9732 | 163 |
tpf->rd.last_choosen_track = track; |
0 | 164 |
} |
165 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
166 |
tpf->the_dir = TrackEnterdirToTrackdir(track, direction); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
167 |
|
9732 | 168 |
if (!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length)) { |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
169 |
TPFModeShip(tpf, tile, TrackdirToExitdir(tpf->the_dir)); |
0 | 170 |
} |
171 |
||
172 |
tpf->rd = rd; |
|
9732 | 173 |
} while (bits != TRACK_BIT_NONE); |
0 | 174 |
|
175 |
} |
|
176 |
||
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
177 |
/** |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
178 |
* Checks if any vehicle can enter/leave tile in given diagdir |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
179 |
* Checks only for rail/road depots and road non-drivethrough stations |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
180 |
* @param tile tile to check |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
181 |
* @param side side of tile we are trying to leave/enter |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
182 |
* @param tracktype type of transport |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
183 |
* @pre tile has trackbit at that diagdir |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
184 |
* @return true iff vehicle can enter/leve the tile in given side |
22 | 185 |
*/ |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
186 |
static inline bool CanAccessTileInDir(TileIndex tile, DiagDirection side, TransportType tracktype) |
0 | 187 |
{ |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
188 |
if (tracktype == TRANSPORT_RAIL) { |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
189 |
/* depot from wrong side */ |
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
190 |
if (IsRailDepotTile(tile) && GetRailDepotDirection(tile) != side) return false; |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
191 |
} else if (tracktype == TRANSPORT_ROAD) { |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
192 |
/* depot from wrong side */ |
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
193 |
if (IsRoadDepotTile(tile) && GetRoadDepotDirection(tile) != side) return false; |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
194 |
/* non-driverthrough road station from wrong side */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
195 |
if (IsStandardRoadStopTile(tile) && GetRoadStopDir(tile) != side) return false; |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
196 |
} |
0 | 197 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
198 |
return true; |
0 | 199 |
} |
200 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
201 |
static void TPFModeNormal(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
202 |
{ |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
203 |
const TileIndex tile_org = tile; |
0 | 204 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
205 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
206 |
/* wrong track type */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
207 |
if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) return; |
5668
76262aa5075d
(svn r7620) -Fix: [OPF] signal update was incorrectly propagated:
KUDr
parents:
5573
diff
changeset
|
208 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
209 |
DiagDirection dir = GetTunnelBridgeDirection(tile); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
210 |
/* entering tunnel / bridge? */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
211 |
if (dir == direction) { |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
212 |
TileIndex endtile = GetOtherTunnelBridgeEnd(tile); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
213 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
214 |
tpf->rd.cur_length += GetTunnelBridgeLength(tile, endtile) + 1; |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
215 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
216 |
TPFSetTileBit(tpf, tile, 14); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
217 |
TPFSetTileBit(tpf, endtile, 14); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
218 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
219 |
tile = endtile; |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
220 |
} else { |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
221 |
/* leaving tunnel / bridge? */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
222 |
if (ReverseDiagDir(dir) != direction) return; |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
223 |
} |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
224 |
} else { |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
225 |
/* can we leave tile in this dir? */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
226 |
if (!CanAccessTileInDir(tile, direction, tpf->tracktype)) return; |
752
df3e096dd9db
(svn r1208) -Fix: the owner-check introduced in r1203 now also works correctly for
truelight
parents:
747
diff
changeset
|
227 |
} |
747
e795750b96de
(svn r1203) -Fix: the pathfinder no longer sees rail with an other owner as a
truelight
parents:
679
diff
changeset
|
228 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
229 |
tile += TileOffsByDiagDir(direction); |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
230 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
231 |
/* can we enter tile in this dir? */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
232 |
if (!CanAccessTileInDir(tile, ReverseDiagDir(direction), tpf->tracktype)) return; |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3894
diff
changeset
|
233 |
|
5708
76382881636e
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5707
diff
changeset
|
234 |
/* Check if the new tile is a tunnel or bridge head and that the direction |
76382881636e
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5707
diff
changeset
|
235 |
* and transport type match */ |
76382881636e
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5707
diff
changeset
|
236 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
237 |
if (GetTunnelBridgeDirection(tile) != direction || |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
238 |
GetTunnelBridgeTransportType(tile) != tpf->tracktype) { |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
239 |
return; |
5708
76382881636e
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5707
diff
changeset
|
240 |
} |
76382881636e
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5707
diff
changeset
|
241 |
} |
76382881636e
(svn r7718) -Fix (runknown): When pathfinding onto a bridge or tunnel end from
peter1138
parents:
5707
diff
changeset
|
242 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
243 |
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, tpf->tracktype, tpf->sub_type)); |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
244 |
|
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
245 |
/* Check in case of rail if the owner is the same */ |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
246 |
if (tpf->tracktype == TRANSPORT_RAIL) { |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
247 |
if (trackdirbits != TRACKDIR_BIT_NONE && TrackStatusToTrackdirBits(GetTileTrackStatus(tile_org, TRANSPORT_RAIL, 0)) != TRACKDIR_BIT_NONE) { |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
248 |
if (GetTileOwner(tile_org) != GetTileOwner(tile)) return; |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
249 |
} |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
250 |
} |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
251 |
|
0 | 252 |
tpf->rd.cur_length++; |
253 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
254 |
trackdirbits &= DiagdirReachesTrackdirs(direction); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
255 |
TrackBits bits = TrackdirBitsToTrackBits(trackdirbits); |
0 | 256 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
257 |
if (bits != TRACK_BIT_NONE) { |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
258 |
if (!tpf->disable_tile_hash || (tpf->rd.cur_length <= 64 && (KillFirstBit(bits) == 0 || ++tpf->rd.depth <= 7))) { |
0 | 259 |
do { |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
260 |
Track track = RemoveFirstTrack(&bits); |
0 | 261 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
262 |
tpf->the_dir = TrackEnterdirToTrackdir(track, direction); |
9629 | 263 |
RememberData rd = tpf->rd; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
264 |
|
9732 | 265 |
/* make sure we are not leaving from invalid side */ |
266 |
if (TPFSetTileBit(tpf, tile, tpf->the_dir) && CanAccessTileInDir(tile, TrackdirToExitdir(tpf->the_dir), tpf->tracktype) && |
|
267 |
!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length) ) { |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
268 |
TPFModeNormal(tpf, tile, TrackdirToExitdir(tpf->the_dir)); |
0 | 269 |
} |
270 |
tpf->rd = rd; |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
271 |
} while (bits != TRACK_BIT_NONE); |
0 | 272 |
} |
273 |
} |
|
9629 | 274 |
} |
275 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
276 |
void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data) |
0 | 277 |
{ |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
278 |
assert(IsValidDiagDirection(direction)); |
0 | 279 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
280 |
SmallStackSafeStackAlloc<TrackPathFinder, 1> tpf; |
0 | 281 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
282 |
/* initialize path finder variables */ |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
283 |
tpf->userdata = data; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
284 |
tpf->enum_proc = enum_proc; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
285 |
tpf->new_link = tpf->links; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
286 |
tpf->num_links_left = lengthof(tpf->links); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
287 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
288 |
tpf->rd.cur_length = 0; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
289 |
tpf->rd.depth = 0; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
290 |
tpf->rd.last_choosen_track = INVALID_TRACK; |
0 | 291 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
292 |
tpf->disable_tile_hash = (flags & PATHFIND_FLAGS_DISABLE_TILE_HASH) != 0; |
0 | 293 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
294 |
tpf->tracktype = tt; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
295 |
tpf->sub_type = sub_type; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
296 |
|
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
297 |
if ((flags & PATHFIND_FLAGS_SHIP_MODE) != 0) { |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
298 |
tpf->enum_proc(tile, data, INVALID_TRACKDIR, 0); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
299 |
TPFModeShip(tpf, tile, direction); |
0 | 300 |
} else { |
301 |
/* clear the hash_heads */ |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
302 |
memset(tpf->hash_head, 0, sizeof(tpf->hash_head)); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
303 |
TPFModeNormal(tpf, tile, direction); |
0 | 304 |
} |
305 |
||
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
306 |
if (after_proc != NULL) after_proc(tpf); |
0 | 307 |
} |
308 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6498
diff
changeset
|
309 |
struct StackedItem { |
0 | 310 |
TileIndex tile; |
9505 | 311 |
uint16 cur_length; ///< This is the current length to this tile. |
312 |
uint16 priority; ///< This is the current length + estimated length to the goal. |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
313 |
TrackdirByte track; |
0 | 314 |
byte depth; |
315 |
byte state; |
|
316 |
byte first_track; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6498
diff
changeset
|
317 |
}; |
0 | 318 |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6498
diff
changeset
|
319 |
struct HashLink { |
0 | 320 |
TileIndex tile; |
321 |
uint16 typelength; |
|
322 |
uint16 next; |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6498
diff
changeset
|
323 |
}; |
0 | 324 |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6498
diff
changeset
|
325 |
struct NewTrackPathFinder { |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
326 |
NTPEnumProc *enum_proc; |
0 | 327 |
void *userdata; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
328 |
TileIndex dest; |
0 | 329 |
|
3355
a653b8e47f27
(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
|
330 |
TransportType tracktype; |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
331 |
RailTypes railtypes; |
0 | 332 |
uint maxlength; |
333 |
||
334 |
HashLink *new_link; |
|
335 |
uint num_links_left; |
|
336 |
||
979
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
337 |
uint nstack; |
9505 | 338 |
StackedItem stack[256]; ///< priority queue of stacked items |
0 | 339 |
|
9505 | 340 |
uint16 hash_head[0x400]; ///< hash heads. 0 means unused. 0xFFFC = length, 0x3 = dir |
341 |
TileIndex hash_tile[0x400]; ///< tiles. or links. |
|
0 | 342 |
|
9505 | 343 |
HashLink links[0x400]; ///< hash links |
0 | 344 |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6498
diff
changeset
|
345 |
}; |
0 | 346 |
#define NTP_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links) |
347 |
#define NTP_GET_LINK_PTR(tpf, link_offs) (HashLink*)((byte*)tpf->links + (link_offs)) |
|
348 |
||
349 |
#define ARR(i) tpf->stack[(i)-1] |
|
350 |
||
9505 | 351 |
/** called after a new element was added in the queue at the last index. |
352 |
* move it down to the proper position */ |
|
536 | 353 |
static inline void HeapifyUp(NewTrackPathFinder *tpf) |
0 | 354 |
{ |
355 |
StackedItem si; |
|
356 |
int i = ++tpf->nstack; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
357 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
358 |
while (i != 1 && ARR(i).priority < ARR(i>>1).priority) { |
9505 | 359 |
/* the child element is larger than the parent item. |
360 |
* swap the child item and the parent item. */ |
|
361 |
si = ARR(i); ARR(i) = ARR(i >> 1); ARR(i >> 1) = si; |
|
362 |
i >>= 1; |
|
0 | 363 |
} |
364 |
} |
|
365 |
||
9505 | 366 |
/** called after the element 0 was eaten. fill it with a new element */ |
536 | 367 |
static inline void HeapifyDown(NewTrackPathFinder *tpf) |
0 | 368 |
{ |
369 |
StackedItem si; |
|
370 |
int i = 1, j; |
|
979
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
371 |
int n; |
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
372 |
|
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
373 |
assert(tpf->nstack > 0); |
f12f96116cdd
(svn r1475) Fix some more signed/unsigned comparison warnings
tron
parents:
926
diff
changeset
|
374 |
n = --tpf->nstack; |
0 | 375 |
|
376 |
if (n == 0) return; // heap is empty so nothing to do? |
|
377 |
||
9505 | 378 |
/* copy the last item to index 0. we use it as base for heapify. */ |
379 |
ARR(1) = ARR(n + 1); |
|
0 | 380 |
|
9505 | 381 |
while ((j = i * 2) <= n) { |
382 |
/* figure out which is smaller of the children. */ |
|
383 |
if (j != n && ARR(j).priority > ARR(j + 1).priority) |
|
0 | 384 |
j++; // right item is smaller |
385 |
||
386 |
assert(i <= n && j <= n); |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
387 |
if (ARR(i).priority <= ARR(j).priority) |
0 | 388 |
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
|
389 |
|
9505 | 390 |
/* swap parent with the child */ |
0 | 391 |
si = ARR(i); ARR(i) = ARR(j); ARR(j) = si; |
392 |
i = j; |
|
393 |
} |
|
394 |
} |
|
395 |
||
9505 | 396 |
/** mark a tile as visited and store the length of the path. |
397 |
* if we already had a better path to this tile, return false. |
|
398 |
* otherwise return true. */ |
|
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
399 |
static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir, uint length) |
0 | 400 |
{ |
401 |
uint hash,head; |
|
402 |
HashLink *link, *new_link; |
|
403 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
404 |
assert(length < 16384-1); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
405 |
|
0 | 406 |
hash = PATHFIND_HASH_TILE(tile); |
407 |
||
9505 | 408 |
/* never visited before? */ |
0 | 409 |
if ((head=tpf->hash_head[hash]) == 0) { |
410 |
tpf->hash_tile[hash] = tile; |
|
411 |
tpf->hash_head[hash] = dir | (length << 2); |
|
412 |
return true; |
|
413 |
} |
|
414 |
||
415 |
if (head != 0xffff) { |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
416 |
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
|
417 |
|
9505 | 418 |
/* longer length */ |
0 | 419 |
if (length >= (head >> 2)) return false; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
420 |
|
0 | 421 |
tpf->hash_head[hash] = dir | (length << 2); |
422 |
return true; |
|
423 |
} |
|
9505 | 424 |
/* two tiles with the same hash, need to make a link |
425 |
* allocate a link. if out of links, handle this by returning |
|
426 |
* that a tile was already visisted. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
427 |
if (tpf->num_links_left == 0) { |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
428 |
DEBUG(ntp, 1, "No links left"); |
0 | 429 |
return false; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
430 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
431 |
|
0 | 432 |
tpf->num_links_left--; |
433 |
link = tpf->new_link++; |
|
434 |
||
435 |
/* move the data that was previously in the hash_??? variables |
|
436 |
* to the link struct, and let the hash variables point to the link */ |
|
437 |
link->tile = tpf->hash_tile[hash]; |
|
438 |
tpf->hash_tile[hash] = NTP_GET_LINK_OFFS(tpf, link); |
|
439 |
||
440 |
link->typelength = tpf->hash_head[hash]; |
|
9505 | 441 |
tpf->hash_head[hash] = 0xFFFF; // multi link |
0 | 442 |
link->next = 0xFFFF; |
443 |
} else { |
|
9505 | 444 |
/* a linked list of many tiles, |
445 |
* find the one corresponding to the tile, if it exists. |
|
446 |
* otherwise make a new link */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
447 |
|
0 | 448 |
uint offs = tpf->hash_tile[hash]; |
449 |
do { |
|
450 |
link = NTP_GET_LINK_PTR(tpf, offs); |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
451 |
if (tile == link->tile && (link->typelength & 0x3U) == (uint)dir) { |
3019
a09d8f7b48be
(svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents:
3017
diff
changeset
|
452 |
if (length >= (uint)(link->typelength >> 2)) return false; |
0 | 453 |
link->typelength = dir | (length << 2); |
454 |
return true; |
|
455 |
} |
|
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
2952
diff
changeset
|
456 |
} while ((offs = link->next) != 0xFFFF); |
0 | 457 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
458 |
|
0 | 459 |
/* get here if we need to add a new link to link, |
460 |
* first, allocate a new link, in the same way as before */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
461 |
if (tpf->num_links_left == 0) { |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
462 |
DEBUG(ntp, 1, "No links left"); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
463 |
return false; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
464 |
} |
0 | 465 |
tpf->num_links_left--; |
466 |
new_link = tpf->new_link++; |
|
467 |
||
468 |
/* then fill the link with the new info, and establish a ptr from the old |
|
469 |
* link to the new one */ |
|
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
470 |
new_link->tile = tile; |
0 | 471 |
new_link->typelength = dir | (length << 2); |
472 |
new_link->next = 0xFFFF; |
|
473 |
||
474 |
link->next = NTP_GET_LINK_OFFS(tpf, new_link); |
|
475 |
return true; |
|
476 |
} |
|
477 |
||
2782 | 478 |
/** |
479 |
* Checks if the shortest path to the given tile/dir so far is still the given |
|
480 |
* length. |
|
481 |
* @return true if the length is still the same |
|
482 |
* @pre The given tile/dir combination should be present in the hash, by a |
|
483 |
* previous call to NtpVisit(). |
|
484 |
*/ |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1901
diff
changeset
|
485 |
static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length) |
0 | 486 |
{ |
487 |
uint hash,head,offs; |
|
488 |
HashLink *link; |
|
489 |
||
490 |
hash = PATHFIND_HASH_TILE(tile); |
|
491 |
head=tpf->hash_head[hash]; |
|
492 |
assert(head); |
|
493 |
||
494 |
if (head != 0xffff) { |
|
495 |
assert( tpf->hash_tile[hash] == tile && (head & 3) == dir); |
|
496 |
assert( (head >> 2) <= length); |
|
497 |
return length == (head >> 2); |
|
498 |
} |
|
499 |
||
9505 | 500 |
/* else it's a linked list of many tiles */ |
0 | 501 |
offs = tpf->hash_tile[hash]; |
2952 | 502 |
for (;;) { |
0 | 503 |
link = NTP_GET_LINK_PTR(tpf, offs); |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
2952
diff
changeset
|
504 |
if (tile == link->tile && (link->typelength & 0x3U) == dir) { |
3019
a09d8f7b48be
(svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents:
3017
diff
changeset
|
505 |
assert((uint)(link->typelength >> 2) <= length); |
a09d8f7b48be
(svn r3599) -Fix: added some casts to suppress some more warnings
truelight
parents:
3017
diff
changeset
|
506 |
return length == (uint)(link->typelength >> 2); |
0 | 507 |
} |
508 |
offs = link->next; |
|
509 |
assert(offs != 0xffff); |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
514 |
static uint DistanceMoo(TileIndex t0, TileIndex t1) |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
515 |
{ |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
516 |
const uint dx = Delta(TileX(t0), TileX(t1)); |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
517 |
const uint dy = Delta(TileY(t0), TileY(t1)); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
518 |
|
9505 | 519 |
const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
520 |
/* OPTIMISATION: |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
521 |
* Original: diagTracks = max(dx, dy) - min(dx,dy); |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
522 |
* Proof: |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
523 |
* (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */ |
9505 | 524 |
const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks. |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
525 |
|
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
526 |
return diagTracks*DIAG_FACTOR + straightTracks*STR_FACTOR; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
527 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
528 |
|
9505 | 529 |
/* These has to be small cause the max length of a track |
530 |
* is currently limited to 16384 */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
531 |
|
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
532 |
static const byte _length_of_track[16] = { |
4344
5d0e40cd67b9
(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
|
533 |
DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0, |
5d0e40cd67b9
(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
|
534 |
DIAG_FACTOR, DIAG_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, STR_FACTOR, 0, 0 |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
535 |
}; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
536 |
|
9505 | 537 |
/* new more optimized pathfinder for trains... |
538 |
* Tile is the tile the train is at. |
|
539 |
* direction is the tile the train is moving towards. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
540 |
|
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3132
diff
changeset
|
541 |
static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direction) |
0 | 542 |
{ |
2782 | 543 |
TrackBits bits, allbits; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
544 |
Trackdir track; |
2782 | 545 |
TileIndex tile_org; |
0 | 546 |
StackedItem si; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
547 |
int estimation; |
0 | 548 |
|
2261
3f78323707bb
(svn r2781) Fix some of the issues with variables in .h files.
ludde
parents:
2186
diff
changeset
|
549 |
|
2136
2c9fda706e52
(svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents:
2125
diff
changeset
|
550 |
|
9505 | 551 |
/* Need to have a special case for the start. |
552 |
* We shouldn't call the callback for the current tile. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
553 |
si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value. |
0 | 554 |
si.depth = 0; |
555 |
si.state = 0; |
|
2136
2c9fda706e52
(svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents:
2125
diff
changeset
|
556 |
si.first_track = 0xFF; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
557 |
goto start_at; |
0 | 558 |
|
2952 | 559 |
for (;;) { |
9505 | 560 |
/* Get the next item to search from from the priority queue */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
561 |
do { |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
562 |
if (tpf->nstack == 0) |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
563 |
return; // nothing left? then we're done! |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
564 |
si = tpf->stack[0]; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
565 |
tile = si.tile; |
0 | 566 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
567 |
HeapifyDown(tpf); |
9505 | 568 |
/* Make sure we havn't already visited this tile. */ |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
569 |
} while (!NtpCheck(tpf, tile, ReverseDiagDir(TrackdirToExitdir(ReverseTrackdir(si.track))), si.cur_length)); |
0 | 570 |
|
9505 | 571 |
/* Add the length of this track. */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
572 |
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
|
573 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
574 |
callback_and_continue: |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
575 |
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length)) |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
576 |
return; |
0 | 577 |
|
2136
2c9fda706e52
(svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents:
2125
diff
changeset
|
578 |
assert(si.track <= 13); |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
579 |
direction = TrackdirToExitdir(si.track); |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
580 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
581 |
start_at: |
9505 | 582 |
/* If the tile is the entry tile of a tunnel, and we're not going out of the tunnel, |
583 |
* need to find the exit of the tunnel. */ |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
584 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
585 |
if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) { |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
586 |
/* We are not just driving out of the tunnel/bridge */ |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
587 |
if (GetTunnelBridgeDirection(tile) != direction || |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
588 |
GetTunnelBridgeTransportType(tile) != tpf->tracktype) { |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
589 |
/* We are not driving into the tunnel/bridge, or it is an invalid tunnel/bridge */ |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
590 |
continue; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
591 |
} |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
592 |
if (!HasBit(tpf->railtypes, GetRailType(tile))) { |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
593 |
bits = TRACK_BIT_NONE; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
594 |
break; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
595 |
} |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
596 |
|
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
597 |
TileIndex endtile = GetOtherTunnelBridgeEnd(tile); |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
598 |
si.cur_length += DIAG_FACTOR * (GetTunnelBridgeLength(tile, endtile) + 1); |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
599 |
tile = endtile; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
600 |
/* tile now points to the exit tile of the tunnel/bridge */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
601 |
} |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
602 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
603 |
|
9505 | 604 |
/* This is a special loop used to go through |
605 |
* a rail net and find the first intersection */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
606 |
tile_org = tile; |
2952 | 607 |
for (;;) { |
2136
2c9fda706e52
(svn r2646) Change: [ntp] Fix uninitialized variable and add some more asserts to be able to debug an assert error.
ludde
parents:
2125
diff
changeset
|
608 |
assert(direction <= 3); |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4406
diff
changeset
|
609 |
tile += TileOffsByDiagDir(direction); |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
610 |
|
9505 | 611 |
/* too long search length? bail out. */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
612 |
if (si.cur_length >= tpf->maxlength) { |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
613 |
DEBUG(ntp, 1, "Cur_length too big"); |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
614 |
bits = TRACK_BIT_NONE; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
615 |
break; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
616 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
617 |
|
9505 | 618 |
/* Not a regular rail tile? |
619 |
* Then we can't use the code below, but revert to more general code. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
620 |
if (!IsTileType(tile, MP_RAILWAY) || !IsPlainRailTile(tile)) { |
9505 | 621 |
/* We found a tile which is not a normal railway tile. |
622 |
* Determine which tracks that exist on this tile. */ |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
623 |
bits = TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTrackdirs(direction)); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
624 |
|
9505 | 625 |
/* Check that the tile contains exactly one track */ |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
626 |
if (bits == 0 || KillFirstBit(bits) != 0) break; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
627 |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
628 |
if (!HasBit(tpf->railtypes, GetRailType(tile))) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
629 |
bits = TRACK_BIT_NONE; |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
630 |
break; |
3804
13ee0f2f5cc9
(svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents:
3735
diff
changeset
|
631 |
} |
13ee0f2f5cc9
(svn r4812) -Fix (FS#161) NTP properly checks for railtypes on non-plain-rail-tiles (Rubidium)
celestar
parents:
3735
diff
changeset
|
632 |
|
9505 | 633 |
/******************* |
634 |
* If we reach here, the tile has exactly one track. |
|
635 |
* tile - index to a tile that is not rail tile, but still straight (with optional signals) |
|
636 |
* bits - bitmask of which track that exist on the tile (exactly one bit is set) |
|
637 |
* direction - which direction are we moving in? |
|
638 |
*******************/ |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
639 |
si.track = TrackEnterdirToTrackdir(FindFirstTrack(bits), direction); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
640 |
si.cur_length += _length_of_track[si.track]; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
641 |
goto callback_and_continue; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
642 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
643 |
|
2782 | 644 |
/* Regular rail tile, determine which tracks exist. */ |
3267
591027d10884
(svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents:
3234
diff
changeset
|
645 |
allbits = GetTrackBits(tile); |
2782 | 646 |
/* Which tracks are reachable? */ |
647 |
bits = allbits & DiagdirReachesTracks(direction); |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
648 |
|
2782 | 649 |
/* The tile has no reachable tracks => End of rail segment |
650 |
* or Intersection => End of rail segment. We check this agains all the |
|
651 |
* bits, not just reachable ones, to prevent infinite loops. */ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
652 |
if (bits == TRACK_BIT_NONE || TracksOverlap(allbits)) break; |
2137
0e686b34c3a9
(svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents:
2136
diff
changeset
|
653 |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
654 |
if (!HasBit(tpf->railtypes, GetRailType(tile))) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
655 |
bits = TRACK_BIT_NONE; |
3355
a653b8e47f27
(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
|
656 |
break; |
a653b8e47f27
(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
|
657 |
} |
a653b8e47f27
(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
|
658 |
|
2782 | 659 |
/* If we reach here, the tile has exactly one track, and this |
9601
b499fdd106d5
(svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents:
9599
diff
changeset
|
660 |
track is reachable = > Rail segment continues */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
661 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
662 |
track = TrackEnterdirToTrackdir(FindFirstTrack(bits), direction); |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
663 |
assert(track != INVALID_TRACKDIR); |
2137
0e686b34c3a9
(svn r2647) Fix: [ntp] Fix assertion error introduced in r2635
ludde
parents:
2136
diff
changeset
|
664 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
665 |
si.cur_length += _length_of_track[track]; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
666 |
|
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
667 |
/* Check if this rail is an upwards slope. If it is, then add a penalty. */ |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9732
diff
changeset
|
668 |
if (IsDiagonalTrackdir(track) && IsUphillTrackdir(GetTileSlope(tile, NULL), track)) { |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
669 |
// upwards slope. add some penalty. |
9505 | 670 |
si.cur_length += 4 * DIAG_FACTOR; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
671 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
672 |
|
9505 | 673 |
/* railway tile with signals..? */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
674 |
if (HasSignals(tile)) { |
4631
3005b7b2e18c
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
675 |
if (!HasSignalOnTrackdir(tile, track)) { |
9505 | 676 |
/* if one way signal not pointing towards us, stop going in this direction => End of rail segment. */ |
4631
3005b7b2e18c
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
677 |
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
678 |
bits = TRACK_BIT_NONE; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
679 |
break; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
680 |
} |
4631
3005b7b2e18c
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
681 |
} else if (GetSignalStateByTrackdir(tile, track) == SIGNAL_STATE_GREEN) { |
9505 | 682 |
/* green signal in our direction. either one way or two way. */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
683 |
si.state |= 3; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
684 |
} else { |
9505 | 685 |
/* reached a red signal. */ |
4631
3005b7b2e18c
(svn r6495) -Codechange: removed direct map access in pathfind.c
glx
parents:
4559
diff
changeset
|
686 |
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) { |
9505 | 687 |
/* two way red signal. unless we passed another green signal on the way, |
688 |
* stop going in this direction => End of rail segment. |
|
689 |
* this is to prevent us from going into a full platform. */ |
|
690 |
if (!(si.state & 1)) { |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
691 |
bits = TRACK_BIT_NONE; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
692 |
break; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
693 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
694 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
695 |
if (!(si.state & 2)) { |
9505 | 696 |
/* Is this the first signal we see? And it's red... add penalty */ |
697 |
si.cur_length += 10 * DIAG_FACTOR; |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
698 |
si.state += 2; // remember that we added penalty. |
9505 | 699 |
/* Because we added a penalty, we can't just continue as usual. |
700 |
* Need to get out and let A* do it's job with |
|
701 |
* possibly finding an even shorter path. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
702 |
break; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
703 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
704 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
705 |
|
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
706 |
if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length)) |
9505 | 707 |
return; // Don't process this tile any further |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
708 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
709 |
|
9505 | 710 |
/* continue with the next track */ |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
711 |
direction = TrackdirToExitdir(track); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
712 |
|
9505 | 713 |
/* safety check if we're running around chasing our tail... (infinite loop) */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
714 |
if (tile == tile_org) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
715 |
bits = TRACK_BIT_NONE; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
716 |
break; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
717 |
} |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
718 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
719 |
|
9505 | 720 |
/* There are no tracks to choose between. |
721 |
* Stop searching in this direction */ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
722 |
if (bits == TRACK_BIT_NONE) |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
723 |
continue; |
0 | 724 |
|
9505 | 725 |
/**************** |
726 |
* We got multiple tracks to choose between (intersection). |
|
727 |
* Branch the search space into several branches. |
|
728 |
****************/ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
729 |
|
9505 | 730 |
/* Check if we've already visited this intersection. |
731 |
* If we've already visited it with a better length, then |
|
732 |
* there's no point in visiting it again. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
733 |
if (!NtpVisit(tpf, tile, direction, si.cur_length)) |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
734 |
continue; |
0 | 735 |
|
9505 | 736 |
/* Push all possible alternatives that we can reach from here |
737 |
* onto the priority heap. |
|
738 |
* 'bits' contains the tracks that we can choose between. */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
739 |
|
9505 | 740 |
/* First compute the estimated distance to the target. |
741 |
* This is used to implement A* */ |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
742 |
estimation = 0; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
743 |
if (tpf->dest != 0) |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
744 |
estimation = DistanceMoo(tile, tpf->dest); |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
745 |
|
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
746 |
si.depth++; |
2774
4b7d8beec975
(svn r3321) - Fix: A wrong use of the map m5 bits, where a previously calculated "bits" variable should have been used. This resulted in the pathfinder imagining junctions, which negatively affects performance somewhat (Darkvater).
matthijs
parents:
2548
diff
changeset
|
747 |
if (si.depth == 0) |
9505 | 748 |
continue; // We overflowed our depth. No more searching in this direction. |
0 | 749 |
si.tile = tile; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
750 |
while (bits != TRACK_BIT_NONE) { |
5849
58039c9dc565
(svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents:
5838
diff
changeset
|
751 |
Track track = RemoveFirstTrack(&bits); |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
752 |
si.track = TrackEnterdirToTrackdir(track, direction); |
2782 | 753 |
assert(si.track != 0xFF); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
754 |
si.priority = si.cur_length + estimation; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
755 |
|
9505 | 756 |
/* out of stack items, bail out? */ |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
757 |
if (tpf->nstack >= lengthof(tpf->stack)) { |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5028
diff
changeset
|
758 |
DEBUG(ntp, 1, "Out of stack"); |
0 | 759 |
break; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
760 |
} |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
761 |
|
0 | 762 |
tpf->stack[tpf->nstack] = si; |
763 |
HeapifyUp(tpf); |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
764 |
}; |
0 | 765 |
|
9505 | 766 |
/* If this is the first intersection, we need to fill the first_track member. |
767 |
* so the code outside knows which path is better. |
|
768 |
* also randomize the order in which we search through them. */ |
|
0 | 769 |
if (si.depth == 1) { |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
770 |
assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3); |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
771 |
if (tpf->nstack != 1) { |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
772 |
uint32 r = Random(); |
5984 | 773 |
if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
774 |
if (tpf->nstack != 2) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
775 |
TrackdirByte t = tpf->stack[2].track; |
5984 | 776 |
if (r & 2) Swap(tpf->stack[0].track, t); |
777 |
if (r & 4) Swap(tpf->stack[1].track, t); |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
778 |
tpf->stack[2].first_track = tpf->stack[2].track = t; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
779 |
} |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
780 |
tpf->stack[0].first_track = tpf->stack[0].track; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
781 |
tpf->stack[1].first_track = tpf->stack[1].track; |
0 | 782 |
} |
783 |
} |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
784 |
|
9505 | 785 |
/* Continue with the next from the queue... */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2049
diff
changeset
|
786 |
} |
0 | 787 |
} |
788 |
||
789 |
||
9505 | 790 |
/** new pathfinder for trains. better and faster. */ |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
791 |
void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data) |
0 | 792 |
{ |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
793 |
SmallStackSafeStackAlloc<NewTrackPathFinder, 1> tpf; |
0 | 794 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
795 |
tpf->dest = dest; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
796 |
tpf->userdata = data; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
797 |
tpf->enum_proc = enum_proc; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
798 |
tpf->tracktype = TRANSPORT_RAIL; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
799 |
tpf->railtypes = railtypes; |
10776 | 800 |
tpf->maxlength = min(_settings_game.pf.opf.pf_maxlength * 3, 10000); |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
801 |
tpf->nstack = 0; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
802 |
tpf->new_link = tpf->links; |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
803 |
tpf->num_links_left = lengthof(tpf->links); |
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
804 |
memset(tpf->hash_head, 0, sizeof(tpf->hash_head)); |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2006
diff
changeset
|
805 |
|
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9826
diff
changeset
|
806 |
NTPEnum(tpf, tile, direction); |
0 | 807 |
} |