author | tron |
Sun, 26 Mar 2006 11:08:44 +0000 | |
changeset 3333 | 41f8abe65d1e |
parent 3315 | 1f65f8260092 |
child 3355 | a653b8e47f27 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
1247 | 3 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1857
diff
changeset
|
4 |
#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
|
5 |
#include "bridge_map.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1248
diff
changeset
|
6 |
#include "debug.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2115
diff
changeset
|
7 |
#include "functions.h" |
1247 | 8 |
#include "npf.h" |
9 |
#include "aystar.h" |
|
10 |
#include "macros.h" |
|
11 |
#include "pathfind.h" |
|
12 |
#include "station.h" |
|
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3309
diff
changeset
|
13 |
#include "station_map.h" |
1247 | 14 |
#include "tile.h" |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
15 |
#include "depot.h" |
3154 | 16 |
#include "tunnel_map.h" |
1247 | 17 |
|
1983 | 18 |
static AyStar _npf_aystar; |
1247 | 19 |
|
20 |
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH, |
|
21 |
* the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071 |
|
22 |
*/ |
|
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
23 |
#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH) |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
24 |
static const uint _trackdir_length[TRACKDIR_END] = { |
1463
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
25 |
NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, |
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
26 |
0, 0, |
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
27 |
NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH |
1247 | 28 |
}; |
29 |
||
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
30 |
/** |
2403
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
31 |
* Calculates the minimum distance traveled to get from t0 to t1 when only |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
32 |
* using tracks (ie, only making 45 degree turns). Returns the distance in the |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
33 |
* NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
34 |
* prevent rounding. |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
35 |
*/ |
2416 | 36 |
static uint NPFDistanceTrack(TileIndex t0, TileIndex t1) |
2403
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
37 |
{ |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
38 |
const uint dx = abs(TileX(t0) - TileX(t1)); |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
39 |
const uint dy = abs(TileY(t0) - TileY(t1)); |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
40 |
|
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
41 |
const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */ |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
42 |
/* OPTIMISATION: |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
43 |
* Original: diagTracks = max(dx, dy) - min(dx,dy); |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
44 |
* Proof: |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
45 |
* (dx+dy) - straightTracks == (min + max) - straightTracks = min + max - 2 * min = max - min */ |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
46 |
const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */ |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
47 |
|
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
48 |
/* Don't factor out NPF_TILE_LENGTH below, this will round values and lose |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
49 |
* precision */ |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
50 |
return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH; |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
51 |
} |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
52 |
|
2951 | 53 |
|
2075
7f0ca01392db
(svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents:
2049
diff
changeset
|
54 |
#if 0 |
1983 | 55 |
static uint NTPHash(uint key1, uint key2) |
1247 | 56 |
{ |
1661
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
57 |
/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */ |
1247 | 58 |
return PATHFIND_HASH_TILE(key1); |
59 |
} |
|
2075
7f0ca01392db
(svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents:
2049
diff
changeset
|
60 |
#endif |
1247 | 61 |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
62 |
/** |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
63 |
* Calculates a hash value for use in the NPF. |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
64 |
* @param key1 The TileIndex of the tile to hash |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
65 |
* @param key1 The Trackdir of the track on the tile. |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
66 |
* |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
67 |
* @todo Think of a better hash. |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
68 |
*/ |
1983 | 69 |
static uint NPFHash(uint key1, uint key2) |
1661
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
70 |
{ |
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
71 |
/* TODO: think of a better hash? */ |
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
72 |
uint part1 = TileX(key1) & NPF_HASH_HALFMASK; |
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
73 |
uint part2 = TileY(key1) & NPF_HASH_HALFMASK; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
74 |
|
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
75 |
assert(IsValidTrackdir(key2)); |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
76 |
assert(IsValidTile(key1)); |
2951 | 77 |
return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE; |
1661
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
78 |
} |
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
79 |
|
1983 | 80 |
static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent) |
81 |
{ |
|
1247 | 82 |
return 0; |
83 |
} |
|
84 |
||
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
85 |
/* Calcs the tile of given station that is closest to a given tile |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
86 |
* for this we assume the station is a rectangle, |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
87 |
* as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h) |
1247 | 88 |
*/ |
1983 | 89 |
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile) |
90 |
{ |
|
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
91 |
const Station* st = GetStation(station); |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
92 |
|
1983 | 93 |
uint minx = TileX(st->train_tile); // topmost corner of station |
94 |
uint miny = TileY(st->train_tile); |
|
95 |
uint maxx = minx + st->trainst_w - 1; // lowermost corner of station |
|
96 |
uint maxy = miny + st->trainst_h - 1; |
|
97 |
uint x; |
|
98 |
uint y; |
|
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
99 |
|
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
100 |
// we are going the aim for the x coordinate of the closest corner |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
101 |
// but if we are between those coordinates, we will aim for our own x coordinate |
1983 | 102 |
x = clamp(TileX(tile), minx, maxx); |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
103 |
|
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
104 |
// same for y coordinate, see above comment |
1983 | 105 |
y = clamp(TileY(tile), miny, maxy); |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
106 |
|
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
107 |
// return the tile of our target coordinates |
1983 | 108 |
return TileXY(x, y); |
2182 | 109 |
} |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
110 |
|
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
111 |
/* Calcs the heuristic to the target station or tile. For train stations, it |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
112 |
* takes into account the direction of approach. |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
113 |
*/ |
1983 | 114 |
static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent) |
115 |
{ |
|
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
116 |
NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
117 |
NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
118 |
TileIndex from = current->tile; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
119 |
TileIndex to = fstd->dest_coords; |
1453 | 120 |
uint dist; |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
121 |
|
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
122 |
// for train-stations, we are going to aim for the closest station tile |
3135 | 123 |
if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION) |
1453 | 124 |
to = CalcClosestStationTile(fstd->station_index, from); |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
125 |
|
2951 | 126 |
if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) { |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
127 |
/* Since roads only have diagonal pieces, we use manhattan distance here */ |
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
128 |
dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH; |
2951 | 129 |
} else { |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
130 |
/* Ships and trains can also go diagonal, so the minimum distance is shorter */ |
2403
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
131 |
dist = NPFDistanceTrack(from, to); |
2951 | 132 |
} |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
133 |
|
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
134 |
DEBUG(npf, 4)("Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist); |
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
135 |
|
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
136 |
if (dist < ftd->best_bird_dist) { |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
137 |
ftd->best_bird_dist = dist; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
138 |
ftd->best_trackdir = current->user_data[NPF_TRACKDIR_CHOICE]; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
139 |
} |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
140 |
return dist; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
141 |
} |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
142 |
|
2951 | 143 |
|
1247 | 144 |
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to |
145 |
* get here, either getting it from the current choice or from the parent's |
|
146 |
* choice */ |
|
1983 | 147 |
static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent) |
1247 | 148 |
{ |
149 |
if (parent->path.parent == NULL) { |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
150 |
Trackdir trackdir = (Trackdir)current->direction; |
1247 | 151 |
/* This is a first order decision, so we'd better save the |
152 |
* direction we chose */ |
|
153 |
current->user_data[NPF_TRACKDIR_CHOICE] = trackdir; |
|
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
154 |
DEBUG(npf, 6)("Saving trackdir: %#x", trackdir); |
1247 | 155 |
} else { |
2951 | 156 |
/* We've already made the decision, so just save our parent's decision */ |
1247 | 157 |
current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE]; |
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/* Will return the cost of the tunnel. If it is an entry, it will return the |
|
162 |
* cost of that tile. If the tile is an exit, it will return the tunnel length |
|
163 |
* including the exit tile. Requires that this is a Tunnel tile */ |
|
1983 | 164 |
static uint NPFTunnelCost(AyStarNode* current) |
165 |
{ |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
166 |
DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction); |
1247 | 167 |
TileIndex tile = current->tile; |
3154 | 168 |
if (GetTunnelDirection(tile) == ReverseDiagDir(exitdir)) { |
1247 | 169 |
/* We just popped out if this tunnel, since were |
170 |
* facing the tunnel exit */ |
|
171 |
FindLengthOfTunnelResult flotr; |
|
3147
0a09ce6d651a
(svn r3767) Move all direction related enums and functions to a separate header
tron
parents:
3135
diff
changeset
|
172 |
flotr = FindLengthOfTunnel(tile, ReverseDiagDir(exitdir)); |
1247 | 173 |
return flotr.length * NPF_TILE_LENGTH; |
174 |
//TODO: Penalty for tunnels? |
|
175 |
} else { |
|
176 |
/* We are entering the tunnel, the enter tile is just a |
|
177 |
* straight track */ |
|
178 |
return NPF_TILE_LENGTH; |
|
179 |
} |
|
180 |
} |
|
181 |
||
1983 | 182 |
static uint NPFSlopeCost(AyStarNode* current) |
183 |
{ |
|
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
184 |
TileIndex next = current->tile + TileOffsByDir(TrackdirToExitdir(current->direction)); |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
185 |
int x,y; |
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
186 |
int8 z1,z2; |
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
187 |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
188 |
x = TileX(current->tile) * TILE_SIZE; |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
189 |
y = TileY(current->tile) * TILE_SIZE; |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
190 |
/* get the height of the center of the current tile */ |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
191 |
z1 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT); |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
192 |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
193 |
x = TileX(next) * TILE_SIZE; |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
194 |
y = TileY(next) * TILE_SIZE; |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
195 |
/* get the height of the center of the next tile */ |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
196 |
z2 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT); |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
197 |
|
2951 | 198 |
if (z2 - z1 > 1) { |
1247 | 199 |
/* Slope up */ |
200 |
return _patches.npf_rail_slope_penalty; |
|
201 |
} |
|
202 |
return 0; |
|
203 |
/* Should we give a bonus for slope down? Probably not, we |
|
204 |
* could just substract that bonus from the penalty, because |
|
205 |
* there is only one level of steepness... */ |
|
206 |
} |
|
207 |
||
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
208 |
/* Mark tiles by mowing the grass when npf debug level >= 1 */ |
1983 | 209 |
static void NPFMarkTile(TileIndex tile) |
210 |
{ |
|
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
211 |
#ifdef NO_DEBUG_MESSAGES |
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
212 |
return; |
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
213 |
#else |
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
|
214 |
if (_debug_npf_level < 1) return; |
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
|
215 |
switch (GetTileType(tile)) { |
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
|
216 |
case MP_RAILWAY: |
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
|
217 |
/* DEBUG: mark visited tiles by mowing the grass under them ;-) */ |
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
|
218 |
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) { |
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
|
219 |
SB(_m[tile].m2, 0, 4, 0); |
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
|
220 |
MarkTileDirtyByTile(tile); |
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
|
221 |
} |
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
|
222 |
break; |
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
|
223 |
|
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
|
224 |
case MP_STREET: |
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
|
225 |
if (!IsTileDepotType(tile, TRANSPORT_ROAD)) { |
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
|
226 |
SB(_m[tile].m4, 4, 3, 0); |
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
|
227 |
MarkTileDirtyByTile(tile); |
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
|
228 |
} |
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
|
229 |
break; |
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
|
230 |
|
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
|
231 |
default: |
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
|
232 |
break; |
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
|
233 |
} |
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
234 |
#endif |
1247 | 235 |
} |
236 |
||
1983 | 237 |
static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) |
238 |
{ |
|
1247 | 239 |
//TileIndex tile = current->tile; |
240 |
int32 cost = 0; |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
241 |
Trackdir trackdir = (Trackdir)current->direction; |
1247 | 242 |
|
243 |
cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ |
|
244 |
||
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
245 |
if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) |
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
246 |
cost += _patches.npf_buoy_penalty; /* A small penalty for going over buoys */ |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
247 |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
248 |
if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) |
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
249 |
cost += _patches.npf_water_curve_penalty; |
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
250 |
|
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
251 |
/* TODO More penalties? */ |
1247 | 252 |
|
253 |
return cost; |
|
254 |
} |
|
255 |
||
256 |
/* Determine the cost of this node, for road tracks */ |
|
1983 | 257 |
static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) |
258 |
{ |
|
1247 | 259 |
TileIndex tile = current->tile; |
260 |
int32 cost = 0; |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
261 |
|
1247 | 262 |
/* Determine base length */ |
263 |
switch (GetTileType(tile)) { |
|
264 |
case MP_TUNNELBRIDGE: |
|
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
265 |
if (IsTunnel(tile)) { |
1247 | 266 |
cost = NPFTunnelCost(current); |
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
267 |
} else { |
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
268 |
cost = NPF_TILE_LENGTH; |
1247 | 269 |
} |
2605
d9d7eecdd1f1
(svn r3142) Remove a now invalid case fall-through and use helper function instead of map access.
peter1138
parents:
2493
diff
changeset
|
270 |
break; |
2951 | 271 |
|
1247 | 272 |
case MP_STREET: |
273 |
cost = NPF_TILE_LENGTH; |
|
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
274 |
/* Increase the cost for level crossings */ |
2605
d9d7eecdd1f1
(svn r3142) Remove a now invalid case fall-through and use helper function instead of map access.
peter1138
parents:
2493
diff
changeset
|
275 |
if (IsLevelCrossing(tile)) |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
276 |
cost += _patches.npf_crossing_penalty; |
1247 | 277 |
break; |
2951 | 278 |
|
1247 | 279 |
default: |
280 |
break; |
|
281 |
} |
|
282 |
||
283 |
/* Determine extra costs */ |
|
284 |
||
285 |
/* Check for slope */ |
|
286 |
cost += NPFSlopeCost(current); |
|
287 |
||
1941
b1cb02c0401c
(svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents:
1927
diff
changeset
|
288 |
/* Check for turns. Road vehicles only really drive diagonal, turns are |
b1cb02c0401c
(svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents:
1927
diff
changeset
|
289 |
* represented by non-diagonal tracks */ |
b1cb02c0401c
(svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents:
1927
diff
changeset
|
290 |
if (!IsDiagonalTrackdir(current->direction)) |
b1cb02c0401c
(svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents:
1927
diff
changeset
|
291 |
cost += _patches.npf_road_curve_penalty; |
1247 | 292 |
|
293 |
NPFMarkTile(tile); |
|
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
294 |
DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); |
1247 | 295 |
return cost; |
296 |
} |
|
297 |
||
298 |
||
299 |
/* Determine the cost of this node, for railway tracks */ |
|
1983 | 300 |
static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) |
301 |
{ |
|
1247 | 302 |
TileIndex tile = current->tile; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
303 |
Trackdir trackdir = (Trackdir)current->direction; |
1247 | 304 |
int32 cost = 0; |
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
|
305 |
/* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */ |
1617
55878ca5ada9
(svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents:
1503
diff
changeset
|
306 |
OpenListNode new_node; |
55878ca5ada9
(svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents:
1503
diff
changeset
|
307 |
|
1247 | 308 |
/* Determine base length */ |
309 |
switch (GetTileType(tile)) { |
|
310 |
case MP_TUNNELBRIDGE: |
|
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
311 |
if (IsTunnel(tile)) { |
1247 | 312 |
cost = NPFTunnelCost(current); |
313 |
break; |
|
314 |
} |
|
315 |
/* Fall through if above if is false, it is a bridge |
|
316 |
* then. We treat that as ordinary rail */ |
|
2951 | 317 |
|
1247 | 318 |
case MP_RAILWAY: |
319 |
cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ |
|
320 |
break; |
|
2951 | 321 |
|
1247 | 322 |
case MP_STREET: /* Railway crossing */ |
323 |
cost = NPF_TILE_LENGTH; |
|
324 |
break; |
|
2951 | 325 |
|
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
326 |
case MP_STATION: |
2951 | 327 |
/* We give a station tile a penalty. Logically we would only want to give |
328 |
* station tiles that are not our destination this penalty. This would |
|
329 |
* discourage trains to drive through busy stations. But, we can just |
|
330 |
* give any station tile a penalty, because every possible route will get |
|
331 |
* this penalty exactly once, on its end tile (if it's a station) and it |
|
332 |
* will therefore not make a difference. */ |
|
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
333 |
cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty; |
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
334 |
break; |
2951 | 335 |
|
1247 | 336 |
default: |
337 |
break; |
|
338 |
} |
|
339 |
||
340 |
/* Determine extra costs */ |
|
341 |
||
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
342 |
/* Check for signals */ |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
343 |
if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir)) { |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
344 |
/* Ordinary track with signals */ |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
345 |
if (GetSignalState(tile, trackdir) == SIGNAL_STATE_RED) { |
1247 | 346 |
/* Signal facing us is red */ |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
347 |
if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) { |
1247 | 348 |
/* Penalize the first signal we |
349 |
* encounter, if it is red */ |
|
1643
d38655053062
(svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents:
1617
diff
changeset
|
350 |
|
d38655053062
(svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents:
1617
diff
changeset
|
351 |
/* Is this a presignal exit or combo? */ |
3238
a100405fe221
(svn r3912) Move the signal type enum and GetSignalType() to rail_map.h; also add SetSignalType() and use the functions
tron
parents:
3235
diff
changeset
|
352 |
SignalType sigtype = GetSignalType(tile); |
2951 | 353 |
if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) { |
1643
d38655053062
(svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents:
1617
diff
changeset
|
354 |
/* Penalise exit and combo signals differently (heavier) */ |
d38655053062
(svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents:
1617
diff
changeset
|
355 |
cost += _patches.npf_rail_firstred_exit_penalty; |
2951 | 356 |
} else { |
1643
d38655053062
(svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents:
1617
diff
changeset
|
357 |
cost += _patches.npf_rail_firstred_penalty; |
2951 | 358 |
} |
1247 | 359 |
} |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
360 |
/* Record the state of this signal */ |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
361 |
NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true); |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
362 |
} else { |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
363 |
/* Record the state of this signal */ |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
364 |
NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false); |
1247 | 365 |
} |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
366 |
NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true); |
1247 | 367 |
} |
368 |
||
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
369 |
/* Penalise the tile if it is a target tile and the last signal was |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
370 |
* red */ |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
371 |
/* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
372 |
* of course... */ |
1617
55878ca5ada9
(svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents:
1503
diff
changeset
|
373 |
new_node.path.node = *current; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
374 |
if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
375 |
cost += _patches.npf_rail_lastred_penalty; |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
376 |
|
1247 | 377 |
/* Check for slope */ |
378 |
cost += NPFSlopeCost(current); |
|
379 |
||
380 |
/* Check for turns */ |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
381 |
if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) |
1460 | 382 |
cost += _patches.npf_rail_curve_penalty; |
383 |
//TODO, with realistic acceleration, also the amount of straight track between |
|
384 |
// curves should be taken into account, as this affects the speed limit. |
|
1247 | 385 |
|
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
386 |
/* Check for reverse in depot */ |
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
387 |
if (IsTileDepotType(tile, TRANSPORT_RAIL) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) { |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
388 |
/* Penalise any depot tile that is not the last tile in the path. This |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
389 |
* _should_ penalise every occurence of reversing in a depot (and only |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
390 |
* that) */ |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
391 |
cost += _patches.npf_rail_depot_reverse_penalty; |
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
392 |
} |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
393 |
|
1247 | 394 |
/* Check for occupied track */ |
395 |
//TODO |
|
396 |
||
397 |
NPFMarkTile(tile); |
|
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
398 |
DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); |
1247 | 399 |
return cost; |
400 |
} |
|
401 |
||
402 |
/* Will find any depot */ |
|
1983 | 403 |
static int32 NPFFindDepot(AyStar* as, OpenListNode *current) |
404 |
{ |
|
1617
55878ca5ada9
(svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents:
1503
diff
changeset
|
405 |
TileIndex tile = current->path.node.tile; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
406 |
|
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
407 |
/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below, |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
408 |
* since checking the cache not that much faster than the actual check */ |
2951 | 409 |
if (IsTileDepotType(tile, as->user_data[NPF_TYPE])) { |
1247 | 410 |
return AYSTAR_FOUND_END_NODE; |
2951 | 411 |
} else { |
1247 | 412 |
return AYSTAR_DONE; |
2951 | 413 |
} |
1247 | 414 |
} |
415 |
||
416 |
/* Will find a station identified using the NPFFindStationOrTileData */ |
|
1983 | 417 |
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current) |
418 |
{ |
|
1464
266d3b0ee2c8
(svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents:
1463
diff
changeset
|
419 |
NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; |
1617
55878ca5ada9
(svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents:
1503
diff
changeset
|
420 |
AyStarNode *node = ¤t->path.node; |
1464
266d3b0ee2c8
(svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents:
1463
diff
changeset
|
421 |
TileIndex tile = node->tile; |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
422 |
|
1247 | 423 |
/* If GetNeighbours said we could get here, we assume the station type |
424 |
* is correct */ |
|
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
425 |
if ( |
3135 | 426 |
(fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */ |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3309
diff
changeset
|
427 |
(IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */ |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
428 |
) { |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
429 |
return AYSTAR_FOUND_END_NODE; |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
430 |
} else { |
1247 | 431 |
return AYSTAR_DONE; |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
432 |
} |
1247 | 433 |
} |
434 |
||
435 |
/* To be called when current contains the (shortest route to) the target node. |
|
436 |
* Will fill the contents of the NPFFoundTargetData using |
|
437 |
* AyStarNode[NPF_TRACKDIR_CHOICE]. |
|
438 |
*/ |
|
1983 | 439 |
static void NPFSaveTargetData(AyStar* as, OpenListNode* current) |
440 |
{ |
|
1247 | 441 |
NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
442 |
ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE]; |
1247 | 443 |
ftd->best_path_dist = current->g; |
444 |
ftd->best_bird_dist = 0; |
|
445 |
ftd->node = current->path.node; |
|
446 |
} |
|
447 |
||
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
448 |
/** |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
449 |
* Finds out if a given player's vehicles are allowed to enter a given tile. |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
450 |
* @param owner The owner of the vehicle. |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
451 |
* @param tile The tile that is about to be entered. |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
452 |
* @param enterdir The direction from which the vehicle wants to enter the tile. |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
453 |
* @return true if the vehicle can enter the tile. |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
454 |
* @todo This function should be used in other places than just NPF, |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
455 |
* maybe moved to another file too. |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
456 |
*/ |
1983 | 457 |
static bool VehicleMayEnterTile(Owner owner, TileIndex tile, DiagDirection enterdir) |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
458 |
{ |
2951 | 459 |
if (IsTileType(tile, MP_RAILWAY) || /* Rail tile (also rail depot) */ |
460 |
IsTrainStationTile(tile) || /* Rail station tile */ |
|
461 |
IsTileDepotType(tile, TRANSPORT_ROAD) || /* Road depot tile */ |
|
462 |
IsRoadStationTile(tile) || /* Road station tile */ |
|
463 |
IsTileDepotType(tile, TRANSPORT_WATER)) { /* Water depot tile */ |
|
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
464 |
return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */ |
2951 | 465 |
} |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
466 |
|
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
467 |
switch (GetTileType(tile)) { |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
468 |
case MP_STREET: |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
469 |
/* rail-road crossing : are we looking at the railway part? */ |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
470 |
if (IsLevelCrossing(tile) && GetCrossingTransportType(tile, TrackdirToTrack(DiagdirToDiagTrackdir(enterdir))) == TRANSPORT_RAIL) |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
471 |
return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */ |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
472 |
break; |
2951 | 473 |
|
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
474 |
case MP_TUNNELBRIDGE: |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
475 |
#if 0 |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
476 |
/* OPTIMISATION: If we are on the middle of a bridge, we will not do the cpu |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
477 |
* intensive owner check, instead we will just assume that if the vehicle |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
478 |
* managed to get on the bridge, it is probably allowed to :-) |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
479 |
*/ |
2493
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2416
diff
changeset
|
480 |
if ((_m[tile].m5 & 0xC6) == 0xC0 && GB(_m[tile].m5, 0, 1) == (enterdir & 0x1)) { |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
481 |
/* on the middle part of a railway bridge: find bridge ending */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2029
diff
changeset
|
482 |
while (IsTileType(tile, MP_TUNNELBRIDGE) && !((_m[tile].m5 & 0xC6) == 0x80)) { |
2493
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2416
diff
changeset
|
483 |
tile += TileOffsByDir(GB(_m[tile].m5, 0, 1)); |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
484 |
} |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
485 |
} |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
486 |
/* if we were on a railway middle part, we are now at a railway bridge ending */ |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
487 |
#endif |
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
488 |
if ((IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_RAIL) || |
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
|
489 |
(IsBridge(tile) && ( |
3235
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
490 |
( |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
491 |
IsBridgeRamp(tile) && |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
492 |
GetBridgeTransportType(tile) == TRANSPORT_RAIL |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
493 |
) || ( |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
494 |
IsBridgeMiddle(tile) && |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
495 |
IsTransportUnderBridge(tile) && |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
496 |
GetTransportTypeUnderBridge(tile) == TRANSPORT_RAIL && |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
497 |
GetBridgeAxis(tile) != DiagDirToAxis(enterdir) |
0917155ed14f
(svn r3908) Fix last commit (I'm saying that way too often *sigh*) (Pointed out by Celestar)
tron
parents:
3234
diff
changeset
|
498 |
) |
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
|
499 |
))) { |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
500 |
return IsTileOwner(tile, owner); |
2951 | 501 |
} |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
502 |
break; |
2951 | 503 |
|
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
504 |
default: |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
505 |
break; |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
506 |
} |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
507 |
|
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
508 |
return true; /* no need to check */ |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
509 |
} |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
510 |
|
1247 | 511 |
/* Will just follow the results of GetTileTrackStatus concerning where we can |
512 |
* go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and |
|
513 |
* an argument to GetTileTrackStatus. Will skip tunnels, meaning that the |
|
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
514 |
* entry and exit are neighbours. Will fill |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
515 |
* AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
516 |
* copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */ |
1983 | 517 |
static void NPFFollowTrack(AyStar* aystar, OpenListNode* current) |
518 |
{ |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
519 |
Trackdir src_trackdir = (Trackdir)current->path.node.direction; |
1247 | 520 |
TileIndex src_tile = current->path.node.tile; |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
521 |
DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir); |
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
522 |
TileIndex dst_tile = INVALID_TILE; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
523 |
int i; |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
524 |
TrackdirBits trackdirbits, ts; |
1247 | 525 |
TransportType type = aystar->user_data[NPF_TYPE]; |
526 |
/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */ |
|
527 |
aystar->num_neighbours = 0; |
|
1678
838dd6f46081
(svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents:
1677
diff
changeset
|
528 |
DEBUG(npf, 4)("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); |
1247 | 529 |
|
530 |
/* Find dest tile */ |
|
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
531 |
if (IsTunnelTile(src_tile) && GetTunnelDirection(src_tile) == src_exitdir) { |
1247 | 532 |
/* This is a tunnel. We know this tunnel is our type, |
533 |
* otherwise we wouldn't have got here. It is also facing us, |
|
534 |
* so we should skip it's body */ |
|
3179 | 535 |
dst_tile = GetOtherTunnelEnd(src_tile); |
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
536 |
} else if (type != TRANSPORT_WATER && (IsRoadStationTile(src_tile) || IsTileDepotType(src_tile, type))) { |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
537 |
/* This is a road station or a train or road depot. We can enter and exit |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
538 |
* those from one side only. Trackdirs don't support that (yet), so we'll |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
539 |
* do this here. */ |
1247 | 540 |
|
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
541 |
DiagDirection exitdir; |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
542 |
/* Find out the exit direction first */ |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
543 |
if (IsRoadStationTile(src_tile)) { |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
544 |
exitdir = GetRoadStationDir(src_tile); |
3333 | 545 |
} else { /* Train or road depot */ |
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
546 |
exitdir = GetDepotDirection(src_tile, type); |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
547 |
} |
1247 | 548 |
|
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
549 |
/* Let's see if were headed the right way into the depot */ |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
550 |
if (src_trackdir == DiagdirToDiagTrackdir(ReverseDiagDir(exitdir))) { |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
551 |
/* We are headed inwards. We cannot go through the back of the depot. |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
552 |
* For rail, we can now reverse. Reversing for road vehicles is never |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
553 |
* useful, since you cannot take paths you couldn't take before |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
554 |
* reversing (as with rail). */ |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
555 |
if (type == TRANSPORT_RAIL) { |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
556 |
/* We can only reverse here, so we'll not consider this direction, but |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
557 |
* jump ahead to the reverse direction. It would be nicer to return |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
558 |
* one neighbour here (the reverse trackdir of the one we are |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
559 |
* considering now) and then considering that one to return the tracks |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
560 |
* outside of the depot. But, because the code layout is cleaner this |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
561 |
* way, we will just pretend we are reversed already */ |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
562 |
src_trackdir = ReverseTrackdir(src_trackdir); |
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
563 |
dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(exitdir)); |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
564 |
} else { |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
565 |
dst_tile = INVALID_TILE; /* Road vehicle heading inwards: dead end */ |
2951 | 566 |
} |
3309
dc08a15ad54e
(svn r4071) - Fix: [NPF] Trains & busses were unable to find a route when leaving a depot or bus stop. Small omission from r4023 (fix by glx)
matthijs
parents:
3295
diff
changeset
|
567 |
} else { |
dc08a15ad54e
(svn r4071) - Fix: [NPF] Trains & busses were unable to find a route when leaving a depot or bus stop. Small omission from r4023 (fix by glx)
matthijs
parents:
3295
diff
changeset
|
568 |
dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(exitdir)); |
1247 | 569 |
} |
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
570 |
} else { |
1247 | 571 |
/* This a normal tile, a bridge, a tunnel exit, etc. */ |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
572 |
dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(TrackdirToExitdir(src_trackdir))); |
3295
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
573 |
} |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
574 |
if (dst_tile == INVALID_TILE) { |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
575 |
/* We reached the border of the map */ |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
576 |
/* TODO Nicer control flow for this */ |
36c287fd4ceb
(svn r4023) - Fix: [ 1453646 NPF ] Road vehicles planning through the back of depots and stations.
matthijs
parents:
3238
diff
changeset
|
577 |
return; |
1247 | 578 |
} |
579 |
||
1965
ef27280abb9a
(svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents:
1950
diff
changeset
|
580 |
/* I can't enter a tunnel entry/exit tile from a tile above the tunnel. Note |
ef27280abb9a
(svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents:
1950
diff
changeset
|
581 |
* that I can enter the tunnel from a tile below the tunnel entrance. This |
ef27280abb9a
(svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents:
1950
diff
changeset
|
582 |
* solves the problem of vehicles wanting to drive off a tunnel entrance */ |
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3179
diff
changeset
|
583 |
if (IsTunnelTile(dst_tile) && GetTileZ(dst_tile) < GetTileZ(src_tile)) { |
1965
ef27280abb9a
(svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents:
1950
diff
changeset
|
584 |
return; |
2493
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2416
diff
changeset
|
585 |
} |
1965
ef27280abb9a
(svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents:
1950
diff
changeset
|
586 |
|
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
587 |
/* check correct rail type (mono, maglev, etc) */ |
1749
bb7fa90dd0cb
(svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents:
1700
diff
changeset
|
588 |
if (type == TRANSPORT_RAIL) { |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
589 |
RailType dst_type = GetTileRailType(dst_tile, src_trackdir); |
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
590 |
if (!IsCompatibleRail(aystar->user_data[NPF_RAILTYPE], dst_type)) |
1749
bb7fa90dd0cb
(svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents:
1700
diff
changeset
|
591 |
return; |
bb7fa90dd0cb
(svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents:
1700
diff
changeset
|
592 |
} |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
593 |
|
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
594 |
/* Check the owner of the tile */ |
1967
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
595 |
if (!VehicleMayEnterTile(aystar->user_data[NPF_OWNER], dst_tile, TrackdirToExitdir(src_trackdir))) { |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
596 |
return; |
c57192f4c121
(svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents:
1965
diff
changeset
|
597 |
} |
1247 | 598 |
|
599 |
/* Determine available tracks */ |
|
1655
f45015d2df03
(svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents:
1650
diff
changeset
|
600 |
if (type != TRANSPORT_WATER && (IsRoadStationTile(dst_tile) || IsTileDepotType(dst_tile, type))){ |
f45015d2df03
(svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents:
1650
diff
changeset
|
601 |
/* Road stations and road and train depots return 0 on GTTS, so we have to do this by hand... */ |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
602 |
DiagDirection exitdir; |
2951 | 603 |
if (IsRoadStationTile(dst_tile)) { |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
604 |
exitdir = GetRoadStationDir(dst_tile); |
2951 | 605 |
} else { /* Road or train depot */ |
1650
12a20779af79
(svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents:
1644
diff
changeset
|
606 |
exitdir = GetDepotDirection(dst_tile, type); |
2951 | 607 |
} |
1650
12a20779af79
(svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents:
1644
diff
changeset
|
608 |
/* Find the trackdirs that are available for a depot or station with this |
12a20779af79
(svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents:
1644
diff
changeset
|
609 |
* orientation. They are only "inwards", since we are reaching this tile |
12a20779af79
(svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents:
1644
diff
changeset
|
610 |
* from some other tile. This prevents vehicles driving into depots from |
12a20779af79
(svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents:
1644
diff
changeset
|
611 |
* the back */ |
3147
0a09ce6d651a
(svn r3767) Move all direction related enums and functions to a separate header
tron
parents:
3135
diff
changeset
|
612 |
ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagDir(exitdir))); |
1247 | 613 |
} else { |
614 |
ts = GetTileTrackStatus(dst_tile, type); |
|
615 |
} |
|
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
616 |
trackdirbits = ts & TRACKDIR_BIT_MASK; /* Filter out signal status and the unused bits */ |
1247 | 617 |
|
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
618 |
DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits); |
1247 | 619 |
/* Select only trackdirs we can reach from our current trackdir */ |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
620 |
trackdirbits &= TrackdirReachesTrackdirs(src_trackdir); |
1247 | 621 |
if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */ |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
622 |
trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir); |
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
623 |
|
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
624 |
DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirbits); |
1247 | 625 |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
626 |
i = 0; |
1247 | 627 |
/* Enumerate possible track */ |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
628 |
while (trackdirbits != 0) { |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
629 |
Trackdir dst_trackdir; |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
630 |
dst_trackdir = FindFirstBit2x64(trackdirbits); |
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
631 |
trackdirbits = KillFirstBit2x64(trackdirbits); |
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
632 |
DEBUG(npf, 5)("Expanded into trackdir: %d, remaining trackdirs: %#x", dst_trackdir, trackdirbits); |
1247 | 633 |
|
634 |
/* Check for oneway signal against us */ |
|
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
635 |
if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TYPE_SIGNALS) { |
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
636 |
if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir)) |
1247 | 637 |
// if one way signal not pointing towards us, stop going in this direction. |
1944
012fa5e69118
(svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents:
1942
diff
changeset
|
638 |
break; |
1247 | 639 |
} |
640 |
{ |
|
641 |
/* We've found ourselves a neighbour :-) */ |
|
642 |
AyStarNode* neighbour = &aystar->neighbours[i]; |
|
643 |
neighbour->tile = dst_tile; |
|
644 |
neighbour->direction = dst_trackdir; |
|
645 |
/* Save user data */ |
|
646 |
neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS]; |
|
647 |
NPFFillTrackdirChoice(neighbour, current); |
|
648 |
} |
|
649 |
i++; |
|
650 |
} |
|
651 |
aystar->num_neighbours = i; |
|
652 |
} |
|
653 |
||
654 |
/* |
|
655 |
* Plan a route to the specified target (which is checked by target_proc), |
|
656 |
* from start1 and if not NULL, from start2 as well. The type of transport we |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
657 |
* are checking is in type. reverse_penalty is applied to all routes that |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
658 |
* originate from the second start node. |
1247 | 659 |
* When we are looking for one specific target (optionally multiple tiles), we |
660 |
* should use a good heuristic to perform aystar search. When we search for |
|
661 |
* multiple targets that are spread around, we should perform a breadth first |
|
662 |
* search by specifiying CalcZero as our heuristic. |
|
663 |
*/ |
|
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
664 |
static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner, RailType railtype, uint reverse_penalty) |
1983 | 665 |
{ |
1247 | 666 |
int r; |
667 |
NPFFoundTargetData result; |
|
668 |
||
669 |
/* Initialize procs */ |
|
670 |
_npf_aystar.CalculateH = heuristic_proc; |
|
671 |
_npf_aystar.EndNodeCheck = target_proc; |
|
672 |
_npf_aystar.FoundEndNode = NPFSaveTargetData; |
|
673 |
_npf_aystar.GetNeighbours = NPFFollowTrack; |
|
674 |
if (type == TRANSPORT_RAIL) |
|
675 |
_npf_aystar.CalculateG = NPFRailPathCost; |
|
676 |
else if (type == TRANSPORT_ROAD) |
|
677 |
_npf_aystar.CalculateG = NPFRoadPathCost; |
|
678 |
else if (type == TRANSPORT_WATER) |
|
679 |
_npf_aystar.CalculateG = NPFWaterPathCost; |
|
680 |
else |
|
681 |
assert(0); |
|
682 |
||
683 |
/* Initialize Start Node(s) */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
684 |
start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 685 |
start1->user_data[NPF_NODE_FLAGS] = 0; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
686 |
_npf_aystar.addstart(&_npf_aystar, start1, 0); |
1247 | 687 |
if (start2) { |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
688 |
start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
689 |
start2->user_data[NPF_NODE_FLAGS] = 0; |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
690 |
NPFSetFlag(start2, NPF_FLAG_REVERSE, true); |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
691 |
_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty); |
1247 | 692 |
} |
693 |
||
694 |
/* Initialize result */ |
|
695 |
result.best_bird_dist = (uint)-1; |
|
696 |
result.best_path_dist = (uint)-1; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
697 |
result.best_trackdir = INVALID_TRACKDIR; |
1247 | 698 |
_npf_aystar.user_path = &result; |
699 |
||
700 |
/* Initialize target */ |
|
701 |
_npf_aystar.user_target = target; |
|
702 |
||
703 |
/* Initialize user_data */ |
|
704 |
_npf_aystar.user_data[NPF_TYPE] = type; |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
705 |
_npf_aystar.user_data[NPF_OWNER] = owner; |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
706 |
_npf_aystar.user_data[NPF_RAILTYPE] = railtype; |
1247 | 707 |
|
708 |
/* GO! */ |
|
709 |
r = AyStarMain_Main(&_npf_aystar); |
|
710 |
assert(r != AYSTAR_STILL_BUSY); |
|
711 |
||
712 |
if (result.best_bird_dist != 0) { |
|
3053
708bd89b6acb
(svn r3642) There is a 'npf' debug level for a reason, use that instead of bastardizing 'misc'. Also, pointers are checked against NULL in an if-statement
Darkvater
parents:
3017
diff
changeset
|
713 |
if (target != NULL) { |
708bd89b6acb
(svn r3642) There is a 'npf' debug level for a reason, use that instead of bastardizing 'misc'. Also, pointers are checked against NULL in an if-statement
Darkvater
parents:
3017
diff
changeset
|
714 |
DEBUG(npf, 1) ("Could not find route to tile 0x%x from 0x%x.", target->dest_coords, start1->tile); |
1247 | 715 |
} else { |
716 |
/* Assumption: target == NULL, so we are looking for a depot */ |
|
3053
708bd89b6acb
(svn r3642) There is a 'npf' debug level for a reason, use that instead of bastardizing 'misc'. Also, pointers are checked against NULL in an if-statement
Darkvater
parents:
3017
diff
changeset
|
717 |
DEBUG(npf, 1) ("Could not find route to a depot from tile 0x%x.", start1->tile); |
1247 | 718 |
} |
719 |
||
720 |
} |
|
721 |
return result; |
|
722 |
} |
|
723 |
||
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
724 |
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype) |
1983 | 725 |
{ |
1247 | 726 |
AyStarNode start1; |
727 |
AyStarNode start2; |
|
728 |
||
729 |
start1.tile = tile1; |
|
730 |
start2.tile = tile2; |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
731 |
/* We set this in case the target is also the start tile, we will just |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
732 |
* return a not found then */ |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
733 |
start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 734 |
start1.direction = trackdir1; |
735 |
start2.direction = trackdir2; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
736 |
start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 737 |
|
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
738 |
return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, railtype, 0); |
1247 | 739 |
} |
740 |
||
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
741 |
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype) |
1983 | 742 |
{ |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
743 |
return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, 0, target, type, owner, railtype); |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
744 |
} |
1247 | 745 |
|
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
746 |
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, Owner owner, RailType railtype, uint reverse_penalty) |
1983 | 747 |
{ |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
748 |
AyStarNode start1; |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
749 |
AyStarNode start2; |
1247 | 750 |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
751 |
start1.tile = tile1; |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
752 |
start2.tile = tile2; |
1247 | 753 |
/* We set this in case the target is also the start tile, we will just |
754 |
* return a not found then */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
755 |
start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
756 |
start1.direction = trackdir1; |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
757 |
start2.direction = trackdir2; |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
758 |
start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 759 |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
760 |
/* perform a breadth first search. Target is NULL, |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
761 |
* since we are just looking for any depot...*/ |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2878
diff
changeset
|
762 |
return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, railtype, reverse_penalty); |
1247 | 763 |
} |
764 |
||
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
765 |
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype) |
1983 | 766 |
{ |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
767 |
return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, 0, type, owner, railtype, 0); |
1247 | 768 |
} |
769 |
||
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
770 |
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype) |
1983 | 771 |
{ |
1247 | 772 |
/* Okay, what we're gonna do. First, we look at all depots, calculate |
773 |
* the manhatten distance to get to each depot. We then sort them by |
|
774 |
* distance. We start by trying to plan a route to the closest, then |
|
775 |
* the next closest, etc. We stop when the best route we have found so |
|
776 |
* far, is shorter than the manhattan distance. This will obviously |
|
777 |
* always find the closest depot. It will probably be most efficient |
|
778 |
* for ships, since the heuristic will not be to far off then. I hope. |
|
779 |
*/ |
|
780 |
Queue depots; |
|
781 |
int r; |
|
782 |
NPFFoundTargetData best_result; |
|
783 |
NPFFoundTargetData result; |
|
784 |
NPFFindStationOrTileData target; |
|
785 |
AyStarNode start; |
|
786 |
Depot* current; |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
787 |
Depot *depot; |
1247 | 788 |
|
789 |
init_InsSort(&depots); |
|
790 |
/* Okay, let's find all depots that we can use first */ |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
791 |
FOR_ALL_DEPOTS(depot) { |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
792 |
/* Check if this is really a valid depot, it is of the needed type and |
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
793 |
* owner */ |
1338 | 794 |
if (IsValidDepot(depot) && IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner)) |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
795 |
/* If so, let's add it to the queue, sorted by distance */ |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
796 |
depots.push(&depots, depot, DistanceManhattan(tile, depot->xy)); |
1247 | 797 |
} |
798 |
||
799 |
/* Now, let's initialise the aystar */ |
|
800 |
||
801 |
/* Initialize procs */ |
|
802 |
_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic; |
|
803 |
_npf_aystar.EndNodeCheck = NPFFindStationOrTile; |
|
804 |
_npf_aystar.FoundEndNode = NPFSaveTargetData; |
|
805 |
_npf_aystar.GetNeighbours = NPFFollowTrack; |
|
806 |
if (type == TRANSPORT_RAIL) |
|
807 |
_npf_aystar.CalculateG = NPFRailPathCost; |
|
808 |
else if (type == TRANSPORT_ROAD) |
|
809 |
_npf_aystar.CalculateG = NPFRoadPathCost; |
|
810 |
else if (type == TRANSPORT_WATER) |
|
811 |
_npf_aystar.CalculateG = NPFWaterPathCost; |
|
812 |
else |
|
813 |
assert(0); |
|
814 |
||
815 |
/* Initialize target */ |
|
3135 | 816 |
target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */ |
1247 | 817 |
_npf_aystar.user_target = ⌖ |
818 |
||
819 |
/* Initialize user_data */ |
|
820 |
_npf_aystar.user_data[NPF_TYPE] = type; |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
821 |
_npf_aystar.user_data[NPF_OWNER] = owner; |
1247 | 822 |
|
823 |
/* Initialize Start Node */ |
|
824 |
start.tile = tile; |
|
825 |
start.direction = trackdir; /* We will initialize user_data inside the loop below */ |
|
826 |
||
827 |
/* Initialize Result */ |
|
828 |
_npf_aystar.user_path = &result; |
|
829 |
best_result.best_path_dist = (uint)-1; |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
830 |
best_result.best_bird_dist = (uint)-1; |
1247 | 831 |
|
832 |
/* Just iterate the depots in order of increasing distance */ |
|
833 |
while ((current = depots.pop(&depots))) { |
|
834 |
/* Check to see if we already have a path shorter than this |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
835 |
* depot's manhattan distance. HACK: We call DistanceManhattan |
1247 | 836 |
* again, we should probably modify the queue to give us that |
837 |
* value... */ |
|
838 |
if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist) |
|
839 |
break; |
|
840 |
||
841 |
/* Initialize Start Node */ |
|
842 |
/* We set this in case the target is also the start tile, we will just |
|
843 |
* return a not found then */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
844 |
start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 845 |
start.user_data[NPF_NODE_FLAGS] = 0; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1753
diff
changeset
|
846 |
_npf_aystar.addstart(&_npf_aystar, &start, 0); |
1247 | 847 |
|
848 |
/* Initialize result */ |
|
849 |
result.best_bird_dist = (uint)-1; |
|
850 |
result.best_path_dist = (uint)-1; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
851 |
result.best_trackdir = INVALID_TRACKDIR; |
1247 | 852 |
|
853 |
/* Initialize target */ |
|
854 |
target.dest_coords = current->xy; |
|
855 |
||
856 |
/* GO! */ |
|
857 |
r = AyStarMain_Main(&_npf_aystar); |
|
858 |
assert(r != AYSTAR_STILL_BUSY); |
|
859 |
||
860 |
/* This depot is closer */ |
|
861 |
if (result.best_path_dist < best_result.best_path_dist) |
|
862 |
best_result = result; |
|
863 |
} |
|
864 |
if (result.best_bird_dist != 0) { |
|
3053
708bd89b6acb
(svn r3642) There is a 'npf' debug level for a reason, use that instead of bastardizing 'misc'. Also, pointers are checked against NULL in an if-statement
Darkvater
parents:
3017
diff
changeset
|
865 |
DEBUG(npf, 1) ("Could not find route to any depot from tile 0x%x.", tile); |
1247 | 866 |
} |
867 |
return best_result; |
|
868 |
} |
|
869 |
||
870 |
void InitializeNPF(void) |
|
871 |
{ |
|
1661
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
872 |
init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE); |
1463
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
873 |
_npf_aystar.loops_per_tick = 0; |
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
874 |
_npf_aystar.max_path_cost = 0; |
1700
b8ecf0494fdd
(svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents:
1678
diff
changeset
|
875 |
//_npf_aystar.max_search_nodes = 0; |
b8ecf0494fdd
(svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents:
1678
diff
changeset
|
876 |
/* We will limit the number of nodes for now, until we have a better |
b8ecf0494fdd
(svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents:
1678
diff
changeset
|
877 |
* solution to really fix performance */ |
b8ecf0494fdd
(svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents:
1678
diff
changeset
|
878 |
_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes; |
1247 | 879 |
} |
880 |
||
1983 | 881 |
void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) |
882 |
{ |
|
1247 | 883 |
/* Ships don't really reach their stations, but the tile in front. So don't |
884 |
* save the station id for ships. For roadvehs we don't store it either, |
|
885 |
* because multistop depends on vehicles actually reaching the exact |
|
886 |
* dest_tile, not just any stop of that station. |
|
887 |
* So only for train orders to stations we fill fstd->station_index, for all |
|
888 |
* others only dest_coords */ |
|
2951 | 889 |
if (v->current_order.type == OT_GOTO_STATION && v->type == VEH_Train) { |
1247 | 890 |
fstd->station_index = v->current_order.station; |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
891 |
/* Let's take the closest tile of the station as our target for trains */ |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
892 |
fstd->dest_coords = CalcClosestStationTile(v->current_order.station, v->tile); |
1247 | 893 |
} else { |
894 |
fstd->dest_coords = v->dest_tile; |
|
3135 | 895 |
fstd->station_index = INVALID_STATION; |
1247 | 896 |
} |
897 |
} |