author | skidd13 |
Mon, 16 Jun 2008 17:05:17 +0000 | |
changeset 10979 | 13c4100d8757 |
parent 10775 | 7061477bfbcf |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
10429
1b99254f9607
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
10222
diff
changeset
|
3 |
/** @file npf.cpp Implementation of the NPF pathfinder. */ |
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
4 |
|
1247 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1857
diff
changeset
|
6 |
#include "openttd.h" |
8635
3bbb6f87fced
(svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents:
8627
diff
changeset
|
7 |
#include "npf.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
|
8 |
#include "bridge_map.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1248
diff
changeset
|
9 |
#include "debug.h" |
8615
6b91ca653bad
(svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents:
8609
diff
changeset
|
10 |
#include "tile_cmd.h" |
6b91ca653bad
(svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents:
8609
diff
changeset
|
11 |
#include "bridge.h" |
6949
72d11a1e1e60
(svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents:
6674
diff
changeset
|
12 |
#include "landscape.h" |
1247 | 13 |
#include "aystar.h" |
14 |
#include "pathfind.h" |
|
9281
d8cd9ac52a68
(svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents:
9112
diff
changeset
|
15 |
#include "station_base.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
|
16 |
#include "station_map.h" |
10222
b6919c94cc77
(svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents:
10221
diff
changeset
|
17 |
#include "depot_base.h" |
b6919c94cc77
(svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents:
10221
diff
changeset
|
18 |
#include "depot_map.h" |
3154 | 19 |
#include "tunnel_map.h" |
5720
cc0ceeafaa55
(svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
5573
diff
changeset
|
20 |
#include "network/network.h" |
3957
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
21 |
#include "water_map.h" |
8579
3efbb430092e
(svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents:
8466
diff
changeset
|
22 |
#include "tunnelbridge_map.h" |
8627
448ebf3a8291
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents:
8615
diff
changeset
|
23 |
#include "functions.h" |
8640
1e93b81e96d2
(svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents:
8635
diff
changeset
|
24 |
#include "vehicle_base.h" |
8707
55835d8fbfcd
(svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents:
8693
diff
changeset
|
25 |
#include "settings_type.h" |
8894
1e5b2d4380b8
(svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents:
8732
diff
changeset
|
26 |
#include "tunnelbridge.h" |
1247 | 27 |
|
1983 | 28 |
static AyStar _npf_aystar; |
1247 | 29 |
|
30 |
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH, |
|
31 |
* the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071 |
|
32 |
*/ |
|
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
33 |
#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
|
34 |
static const uint _trackdir_length[TRACKDIR_END] = { |
1463
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
35 |
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
|
36 |
0, 0, |
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
37 |
NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH |
1247 | 38 |
}; |
39 |
||
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
|
40 |
/** |
2403
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
41 |
* 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
|
42 |
* 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
|
43 |
* 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
|
44 |
* prevent rounding. |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
45 |
*/ |
2416 | 46 |
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
|
47 |
{ |
8466
9ce95e16f9f9
(svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents:
8424
diff
changeset
|
48 |
const uint dx = Delta(TileX(t0), TileX(t1)); |
9ce95e16f9f9
(svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents:
8424
diff
changeset
|
49 |
const uint dy = Delta(TileY(t0), TileY(t1)); |
2403
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
50 |
|
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
51 |
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
|
52 |
/* OPTIMISATION: |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
53 |
* 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
|
54 |
* Proof: |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
55 |
* (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
|
56 |
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
|
57 |
|
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
58 |
/* 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
|
59 |
* precision */ |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
60 |
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
|
61 |
} |
f339737b38bc
(svn r2929) * Move DistanceTrack from map.c to npf.c and rename to NPFDistanceTrack.
matthijs
parents:
2186
diff
changeset
|
62 |
|
2951 | 63 |
|
2075
7f0ca01392db
(svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents:
2049
diff
changeset
|
64 |
#if 0 |
1983 | 65 |
static uint NTPHash(uint key1, uint key2) |
1247 | 66 |
{ |
1661
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
67 |
/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */ |
1247 | 68 |
return PATHFIND_HASH_TILE(key1); |
69 |
} |
|
2075
7f0ca01392db
(svn r2585) - Fix [Makefile]: some small cleanups, remove warnings, and add mersenne to makefile (Luca)
Darkvater
parents:
2049
diff
changeset
|
70 |
#endif |
1247 | 71 |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
72 |
/** |
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
73 |
* Calculates a hash value for use in the NPF. |
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4424
diff
changeset
|
74 |
* @param key1 The TileIndex of the tile to hash |
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4424
diff
changeset
|
75 |
* @param key2 The Trackdir of the track on the tile. |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
76 |
* |
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4424
diff
changeset
|
77 |
* @todo Think of a better hash. |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
78 |
*/ |
1983 | 79 |
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
|
80 |
{ |
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
81 |
/* 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
|
82 |
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
|
83 |
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
|
84 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
85 |
assert(IsValidTrackdir((Trackdir)key2)); |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
86 |
assert(IsValidTile(key1)); |
2951 | 87 |
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
|
88 |
} |
6af0c4416154
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1655
diff
changeset
|
89 |
|
1983 | 90 |
static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent) |
91 |
{ |
|
1247 | 92 |
return 0; |
93 |
} |
|
94 |
||
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
95 |
/* 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
|
96 |
* 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
|
97 |
* as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h) |
1247 | 98 |
*/ |
1983 | 99 |
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile) |
100 |
{ |
|
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
101 |
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
|
102 |
|
1983 | 103 |
uint minx = TileX(st->train_tile); // topmost corner of station |
104 |
uint miny = TileY(st->train_tile); |
|
105 |
uint maxx = minx + st->trainst_w - 1; // lowermost corner of station |
|
106 |
uint maxy = miny + st->trainst_h - 1; |
|
107 |
uint x; |
|
108 |
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
|
109 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
110 |
/* we are going the aim for the x coordinate of the closest corner |
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
111 |
* but if we are between those coordinates, we will aim for our own x coordinate */ |
8418
b49fc6be1ab9
(svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents:
8260
diff
changeset
|
112 |
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
|
113 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
114 |
/* same for y coordinate, see above comment */ |
8418
b49fc6be1ab9
(svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents:
8260
diff
changeset
|
115 |
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
|
116 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
117 |
/* return the tile of our target coordinates */ |
1983 | 118 |
return TileXY(x, y); |
2182 | 119 |
} |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
120 |
|
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
121 |
/* 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
|
122 |
* 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
|
123 |
*/ |
1983 | 124 |
static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent) |
125 |
{ |
|
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
TileIndex to = fstd->dest_coords; |
1453 | 130 |
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
|
131 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
132 |
/* for train-stations, we are going to aim for the closest station tile */ |
3135 | 133 |
if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION) |
1453 | 134 |
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
|
135 |
|
2951 | 136 |
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
|
137 |
/* 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
|
138 |
dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH; |
2951 | 139 |
} else { |
1677
c18884ca76d5
(svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents:
1661
diff
changeset
|
140 |
/* 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
|
141 |
dist = NPFDistanceTrack(from, to); |
2951 | 142 |
} |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
143 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
144 |
DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist); |
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
|
145 |
|
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
|
146 |
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
|
147 |
ftd->best_bird_dist = dist; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
148 |
ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE]; |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
149 |
} |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
150 |
return dist; |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
151 |
} |
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
152 |
|
2951 | 153 |
|
1247 | 154 |
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to |
155 |
* get here, either getting it from the current choice or from the parent's |
|
156 |
* choice */ |
|
1983 | 157 |
static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent) |
1247 | 158 |
{ |
159 |
if (parent->path.parent == NULL) { |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
160 |
Trackdir trackdir = (Trackdir)current->direction; |
1247 | 161 |
/* This is a first order decision, so we'd better save the |
162 |
* direction we chose */ |
|
163 |
current->user_data[NPF_TRACKDIR_CHOICE] = trackdir; |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
164 |
DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir); |
1247 | 165 |
} else { |
2951 | 166 |
/* We've already made the decision, so just save our parent's decision */ |
1247 | 167 |
current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE]; |
168 |
} |
|
169 |
} |
|
170 |
||
171 |
/* Will return the cost of the tunnel. If it is an entry, it will return the |
|
172 |
* cost of that tile. If the tile is an exit, it will return the tunnel length |
|
173 |
* including the exit tile. Requires that this is a Tunnel tile */ |
|
1983 | 174 |
static uint NPFTunnelCost(AyStarNode* current) |
175 |
{ |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
176 |
DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction); |
1247 | 177 |
TileIndex tile = current->tile; |
8579
3efbb430092e
(svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents:
8466
diff
changeset
|
178 |
if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) { |
1247 | 179 |
/* We just popped out if this tunnel, since were |
180 |
* facing the tunnel exit */ |
|
8894
1e5b2d4380b8
(svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents:
8732
diff
changeset
|
181 |
return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1); |
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
182 |
/* @todo: Penalty for tunnels? */ |
1247 | 183 |
} else { |
184 |
/* We are entering the tunnel, the enter tile is just a |
|
185 |
* straight track */ |
|
186 |
return NPF_TILE_LENGTH; |
|
187 |
} |
|
188 |
} |
|
189 |
||
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
190 |
static inline uint NPFBridgeCost(AyStarNode *current) |
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
191 |
{ |
8894
1e5b2d4380b8
(svn r11968) -Codechange: remove redundant FindLengthOfTunnel(), use GetTunnelBridgeLength() and/or GetOtherTunnelEnd() instead
smatz
parents:
8732
diff
changeset
|
192 |
return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile)); |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
193 |
} |
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
194 |
|
1983 | 195 |
static uint NPFSlopeCost(AyStarNode* current) |
196 |
{ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
197 |
TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir((Trackdir)current->direction)); |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
198 |
|
8260
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
199 |
/* Get center of tiles */ |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
200 |
int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2; |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
201 |
int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2; |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
202 |
int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2; |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
203 |
int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2; |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
204 |
|
8260
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
205 |
int dx4 = (x2 - x1) / 4; |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
206 |
int dy4 = (y2 - y1) / 4; |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
207 |
|
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
208 |
/* Get the height on both sides of the tile edge. |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
209 |
* Avoid testing the height on the tile-center. This will fail for halftile-foundations. |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
210 |
*/ |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
211 |
int z1 = GetSlopeZ(x1 + dx4, y1 + dy4); |
c7d75cebe3f6
(svn r11313) -Codechange: prepare several pieces of code so the can handle some new slopes. Patch by frosch.
rubidium
parents:
7866
diff
changeset
|
212 |
int z2 = GetSlopeZ(x2 - dx4, y2 - dy4); |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
213 |
|
2951 | 214 |
if (z2 - z1 > 1) { |
1247 | 215 |
/* Slope up */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
216 |
return _settings_game.pf.npf.npf_rail_slope_penalty; |
1247 | 217 |
} |
218 |
return 0; |
|
219 |
/* Should we give a bonus for slope down? Probably not, we |
|
220 |
* could just substract that bonus from the penalty, because |
|
221 |
* there is only one level of steepness... */ |
|
222 |
} |
|
223 |
||
3532
446072c37643
(svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents:
3531
diff
changeset
|
224 |
/** |
446072c37643
(svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents:
3531
diff
changeset
|
225 |
* Mark tiles by mowing the grass when npf debug level >= 1. |
446072c37643
(svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents:
3531
diff
changeset
|
226 |
* Will not work for multiplayer games, since it can (will) cause desyncs. |
446072c37643
(svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents:
3531
diff
changeset
|
227 |
*/ |
1983 | 228 |
static void NPFMarkTile(TileIndex tile) |
229 |
{ |
|
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
230 |
#ifndef NO_DEBUG_MESSAGES |
3532
446072c37643
(svn r4389) -Fix: [NPF] Don't mark tiles when debugging in multiplayer, this will cause desyncs.
matthijs
parents:
3531
diff
changeset
|
231 |
if (_debug_npf_level < 1 || _networking) return; |
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
|
232 |
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
|
233 |
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
|
234 |
/* DEBUG: mark visited tiles by mowing the grass under them ;-) */ |
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
235 |
if (!IsRailDepot(tile)) { |
3531
07fb46c7d469
(svn r4387) -Codechange: Use map accessors even for debugging purposes (npf).
celestar
parents:
3520
diff
changeset
|
236 |
SetRailGroundType(tile, RAIL_GROUND_BARREN); |
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
|
237 |
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
|
238 |
} |
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
|
239 |
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
|
240 |
|
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7586
diff
changeset
|
241 |
case MP_ROAD: |
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
242 |
if (!IsRoadDepot(tile)) { |
4048 | 243 |
SetRoadside(tile, ROADSIDE_BARREN); |
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
|
244 |
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
|
245 |
} |
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
|
246 |
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
|
247 |
|
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
|
248 |
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
|
249 |
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
|
250 |
} |
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
|
251 |
#endif |
1247 | 252 |
} |
253 |
||
1983 | 254 |
static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) |
255 |
{ |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
256 |
/* TileIndex tile = current->tile; */ |
1247 | 257 |
int32 cost = 0; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
258 |
Trackdir trackdir = (Trackdir)current->direction; |
1247 | 259 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
260 |
cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks |
1247 | 261 |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
262 |
if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
263 |
cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys |
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
264 |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
265 |
if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
266 |
cost += _settings_game.pf.npf.npf_water_curve_penalty; |
1751
954dd2900ac9
(svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents:
1749
diff
changeset
|
267 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
268 |
/* @todo More penalties? */ |
1247 | 269 |
|
270 |
return cost; |
|
271 |
} |
|
272 |
||
273 |
/* Determine the cost of this node, for road tracks */ |
|
1983 | 274 |
static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) |
275 |
{ |
|
1247 | 276 |
TileIndex tile = current->tile; |
277 |
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
|
278 |
|
1247 | 279 |
/* Determine base length */ |
280 |
switch (GetTileType(tile)) { |
|
281 |
case MP_TUNNELBRIDGE: |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
282 |
cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current); |
2605
d9d7eecdd1f1
(svn r3142) Remove a now invalid case fall-through and use helper function instead of map access.
peter1138
parents:
2493
diff
changeset
|
283 |
break; |
2951 | 284 |
|
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7586
diff
changeset
|
285 |
case MP_ROAD: |
1247 | 286 |
cost = NPF_TILE_LENGTH; |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1983
diff
changeset
|
287 |
/* Increase the cost for level crossings */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
288 |
if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty; |
1247 | 289 |
break; |
2951 | 290 |
|
6338
0fb4f452873c
(svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents:
5849
diff
changeset
|
291 |
case MP_STATION: |
0fb4f452873c
(svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents:
5849
diff
changeset
|
292 |
cost = NPF_TILE_LENGTH; |
0fb4f452873c
(svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents:
5849
diff
changeset
|
293 |
/* Increase the cost for drive-through road stops */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
294 |
if (IsDriveThroughStopTile(tile)) cost += _settings_game.pf.npf.npf_road_drive_through_penalty; |
6338
0fb4f452873c
(svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents:
5849
diff
changeset
|
295 |
break; |
0fb4f452873c
(svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents:
5849
diff
changeset
|
296 |
|
1247 | 297 |
default: |
298 |
break; |
|
299 |
} |
|
300 |
||
301 |
/* Determine extra costs */ |
|
302 |
||
303 |
/* Check for slope */ |
|
304 |
cost += NPFSlopeCost(current); |
|
305 |
||
1941
b1cb02c0401c
(svn r2447) * Add: [NPF] Penalty for road vehicles making turns.
matthijs
parents:
1927
diff
changeset
|
306 |
/* 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
|
307 |
* represented by non-diagonal tracks */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
308 |
if (!IsDiagonalTrackdir((Trackdir)current->direction)) |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
309 |
cost += _settings_game.pf.npf.npf_road_curve_penalty; |
1247 | 310 |
|
311 |
NPFMarkTile(tile); |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
312 |
DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); |
1247 | 313 |
return cost; |
314 |
} |
|
315 |
||
316 |
||
317 |
/* Determine the cost of this node, for railway tracks */ |
|
1983 | 318 |
static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) |
319 |
{ |
|
1247 | 320 |
TileIndex tile = current->tile; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
321 |
Trackdir trackdir = (Trackdir)current->direction; |
1247 | 322 |
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
|
323 |
/* 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
|
324 |
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
|
325 |
|
1247 | 326 |
/* Determine base length */ |
327 |
switch (GetTileType(tile)) { |
|
328 |
case MP_TUNNELBRIDGE: |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
329 |
cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current); |
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
330 |
break; |
2951 | 331 |
|
1247 | 332 |
case MP_RAILWAY: |
333 |
cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ |
|
334 |
break; |
|
2951 | 335 |
|
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7586
diff
changeset
|
336 |
case MP_ROAD: /* Railway crossing */ |
1247 | 337 |
cost = NPF_TILE_LENGTH; |
338 |
break; |
|
2951 | 339 |
|
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
340 |
case MP_STATION: |
2951 | 341 |
/* We give a station tile a penalty. Logically we would only want to give |
342 |
* station tiles that are not our destination this penalty. This would |
|
343 |
* discourage trains to drive through busy stations. But, we can just |
|
344 |
* give any station tile a penalty, because every possible route will get |
|
345 |
* this penalty exactly once, on its end tile (if it's a station) and it |
|
346 |
* will therefore not make a difference. */ |
|
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
347 |
cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty; |
1503
be35a76c7555
(svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents:
1502
diff
changeset
|
348 |
break; |
2951 | 349 |
|
1247 | 350 |
default: |
351 |
break; |
|
352 |
} |
|
353 |
||
354 |
/* Determine extra costs */ |
|
355 |
||
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
356 |
/* 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
|
357 |
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
|
358 |
/* Ordinary track with signals */ |
3520
092efede26b3
(svn r4376) -Codechange Renamed GetSignalState to GetSignalStateByTrackdir
celestar
parents:
3498
diff
changeset
|
359 |
if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) { |
1247 | 360 |
/* 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
|
361 |
if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) { |
1247 | 362 |
/* Penalize the first signal we |
363 |
* 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
|
364 |
|
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
|
365 |
/* Is this a presignal exit or combo? */ |
7266
4b75e7b9fa91
(svn r10006) -Fix (FS#362): allow different signal types on one tile
glx
parents:
7179
diff
changeset
|
366 |
SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir)); |
2951 | 367 |
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
|
368 |
/* Penalise exit and combo signals differently (heavier) */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
369 |
cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty; |
2951 | 370 |
} else { |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
371 |
cost += _settings_game.pf.npf.npf_rail_firstred_penalty; |
2951 | 372 |
} |
1247 | 373 |
} |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
374 |
/* 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
|
375 |
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
|
376 |
} else { |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
377 |
/* 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
|
378 |
NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false); |
1247 | 379 |
} |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
380 |
NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true); |
1247 | 381 |
} |
382 |
||
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
383 |
/* 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
|
384 |
* red */ |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
385 |
/* 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
|
386 |
* 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
|
387 |
new_node.path.node = *current; |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
388 |
if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
389 |
cost += _settings_game.pf.npf.npf_rail_lastred_penalty; |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
390 |
|
1247 | 391 |
/* Check for slope */ |
392 |
cost += NPFSlopeCost(current); |
|
393 |
||
394 |
/* Check for turns */ |
|
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
395 |
if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
396 |
cost += _settings_game.pf.npf.npf_rail_curve_penalty; |
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
397 |
/*TODO, with realistic acceleration, also the amount of straight track between |
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
398 |
* curves should be taken into account, as this affects the speed limit. */ |
1247 | 399 |
|
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
|
400 |
/* Check for reverse in depot */ |
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
401 |
if (IsRailDepotTile(tile) && 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
|
402 |
/* 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
|
403 |
* _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
|
404 |
* that) */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
405 |
cost += _settings_game.pf.npf.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
|
406 |
} |
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
|
407 |
|
1247 | 408 |
/* Check for occupied track */ |
409 |
//TODO |
|
410 |
||
411 |
NPFMarkTile(tile); |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
412 |
DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); |
1247 | 413 |
return cost; |
414 |
} |
|
415 |
||
416 |
/* Will find any depot */ |
|
1983 | 417 |
static int32 NPFFindDepot(AyStar* as, OpenListNode *current) |
418 |
{ |
|
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
|
419 |
/* 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
|
420 |
* since checking the cache not that much faster than the actual check */ |
10213
23cfd330ccac
(svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents:
9336
diff
changeset
|
421 |
return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ? |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
422 |
AYSTAR_FOUND_END_NODE : AYSTAR_DONE; |
1247 | 423 |
} |
424 |
||
425 |
/* Will find a station identified using the NPFFindStationOrTileData */ |
|
1983 | 426 |
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current) |
427 |
{ |
|
1464
266d3b0ee2c8
(svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents:
1463
diff
changeset
|
428 |
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
|
429 |
AyStarNode *node = ¤t->path.node; |
1464
266d3b0ee2c8
(svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents:
1463
diff
changeset
|
430 |
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
|
431 |
|
1247 | 432 |
/* If GetNeighbours said we could get here, we assume the station type |
433 |
* 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
|
434 |
if ( |
3135 | 435 |
(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
|
436 |
(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
|
437 |
) { |
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
438 |
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
|
439 |
} else { |
1247 | 440 |
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
|
441 |
} |
1247 | 442 |
} |
443 |
||
444 |
/* To be called when current contains the (shortest route to) the target node. |
|
445 |
* Will fill the contents of the NPFFoundTargetData using |
|
446 |
* AyStarNode[NPF_TRACKDIR_CHOICE]. |
|
447 |
*/ |
|
1983 | 448 |
static void NPFSaveTargetData(AyStar* as, OpenListNode* current) |
449 |
{ |
|
1247 | 450 |
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
|
451 |
ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE]; |
1247 | 452 |
ftd->best_path_dist = current->g; |
453 |
ftd->best_bird_dist = 0; |
|
454 |
ftd->node = current->path.node; |
|
455 |
} |
|
456 |
||
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
|
457 |
/** |
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 |
* 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
|
459 |
* @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
|
460 |
* @param tile The tile that is about to be entered. |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
461 |
* @param enterdir The direction in which the vehicle wants to enter 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
|
462 |
* @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
|
463 |
* @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
|
464 |
* 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
|
465 |
*/ |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
466 |
static bool CanEnterTileOwnerCheck(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
|
467 |
{ |
10221
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
468 |
if (IsTileType(tile, MP_RAILWAY) || /* Rail tile (also rail depot) */ |
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
469 |
IsRailwayStationTile(tile) || /* Rail station tile */ |
faef62283f5f
(svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents:
10213
diff
changeset
|
470 |
IsRoadDepotTile(tile) || /* Road depot tile */ |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
471 |
IsStandardRoadStopTile(tile)) { /* Road station tile (but not drive-through stops) */ |
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
|
472 |
return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */ |
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 |
|
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 |
switch (GetTileType(tile)) { |
7866
e19fda04e8d3
(svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents:
7586
diff
changeset
|
476 |
case MP_ROAD: |
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
|
477 |
/* rail-road crossing : are we looking at the railway part? */ |
3498
3441bc83c28a
(svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents:
3442
diff
changeset
|
478 |
if (IsLevelCrossing(tile) && |
3441bc83c28a
(svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents:
3442
diff
changeset
|
479 |
DiagDirToAxis(enterdir) != GetCrossingRoadAxis(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
|
480 |
return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */ |
3498
3441bc83c28a
(svn r4349) Remove GetCrossingTransportType(), it's slightly overkill
tron
parents:
3442
diff
changeset
|
481 |
} |
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
|
482 |
break; |
2951 | 483 |
|
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 |
case MP_TUNNELBRIDGE: |
8584
a8b6dffead63
(svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents:
8579
diff
changeset
|
485 |
if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { |
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
|
486 |
return IsTileOwner(tile, owner); |
2951 | 487 |
} |
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
|
488 |
break; |
2951 | 489 |
|
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
|
490 |
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
|
491 |
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
|
492 |
} |
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
|
493 |
|
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
494 |
return true; // no need to check |
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
|
495 |
} |
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
|
496 |
|
3957
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
497 |
|
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
498 |
/** |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
499 |
* Returns the direction the exit of the depot on the given tile is facing. |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
500 |
*/ |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
501 |
static DiagDirection GetDepotDirection(TileIndex tile, TransportType type) |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
502 |
{ |
10213
23cfd330ccac
(svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents:
9336
diff
changeset
|
503 |
assert(IsDepotTypeTile(tile, type)); |
3957
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
504 |
|
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
505 |
switch (type) { |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
506 |
case TRANSPORT_RAIL: return GetRailDepotDirection(tile); |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
507 |
case TRANSPORT_ROAD: return GetRoadDepotDirection(tile); |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
508 |
case TRANSPORT_WATER: return GetShipDepotDirection(tile); |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
509 |
default: return INVALID_DIAGDIR; /* Not reached */ |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
510 |
} |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
511 |
} |
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
512 |
|
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
513 |
/** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
514 |
static DiagDirection GetSingleTramBit(TileIndex tile) |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
515 |
{ |
9059
04edde3eb0c6
(svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents:
9006
diff
changeset
|
516 |
if (IsNormalRoadTile(tile)) { |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
517 |
RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
518 |
switch (rb) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
519 |
case ROAD_NW: return DIAGDIR_NW; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
520 |
case ROAD_SW: return DIAGDIR_SW; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
521 |
case ROAD_SE: return DIAGDIR_SE; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
522 |
case ROAD_NE: return DIAGDIR_NE; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
523 |
default: break; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
524 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
525 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
526 |
return INVALID_DIAGDIR; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
527 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
528 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
529 |
/** |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
530 |
* Tests if a tile can be entered or left only from one side. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
531 |
* |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
532 |
* Depots, non-drive-through roadstops, and tiles with single trambits are tested. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
533 |
* |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
534 |
* @param tile The tile of interest. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
535 |
* @param type The transporttype of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
536 |
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
537 |
* @return The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
538 |
*/ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
539 |
static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype) |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
540 |
{ |
10213
23cfd330ccac
(svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents:
9336
diff
changeset
|
541 |
if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type); |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
542 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
543 |
if (type == TRANSPORT_ROAD) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
544 |
if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
545 |
if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
546 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
547 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
548 |
return INVALID_DIAGDIR; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
549 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
550 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
551 |
/** |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
552 |
* Tests if a vehicle must reverse on a tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
553 |
* |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
554 |
* @param tile The tile of interest. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
555 |
* @param dir The direction in which the vehicle drives on a tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
556 |
* @param type The transporttype of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
557 |
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
558 |
* @return true iff the vehicle must reverse on the tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
559 |
*/ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
560 |
static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype) |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
561 |
{ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
562 |
DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
563 |
return single_entry != INVALID_DIAGDIR && single_entry != dir; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
564 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
565 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
566 |
/** |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
567 |
* Tests if a vehicle can enter a tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
568 |
* |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
569 |
* @param tile The tile of interest. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
570 |
* @param dir The direction in which the vehicle drives onto a tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
571 |
* @param type The transporttype of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
572 |
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
573 |
* @param railtypes For TRANSPORT_RAIL the compatible RailTypes of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
574 |
* @param owner The owner of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
575 |
* @return true iff the vehicle can enter the tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
576 |
*/ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
577 |
static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner) |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
578 |
{ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
579 |
/* Check tunnel entries and bridge ramps */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
580 |
if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
581 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
582 |
/* Test ownership */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
583 |
if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
584 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
585 |
/* check correct rail type (mono, maglev, etc) */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
586 |
if (type == TRANSPORT_RAIL) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
587 |
RailType rail_type = GetTileRailType(tile); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
588 |
if (!HasBit(railtypes, rail_type)) return false; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
589 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
590 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
591 |
/* Depots, standard roadstops and single tram bits can only be entered from one direction */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
592 |
DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
593 |
if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
594 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
595 |
return true; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
596 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
597 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
598 |
/** |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
599 |
* Returns the driveable Trackdirs on a tile. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
600 |
* |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
601 |
* One-way-roads are taken into account. Signals are not tested. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
602 |
* |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
603 |
* @param dst_tile The tile of interest. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
604 |
* @param src_trackdir The direction the vehicle is currently moving. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
605 |
* @param type The transporttype of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
606 |
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
607 |
* @return The Trackdirs the vehicle can continue moving on. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
608 |
*/ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
609 |
static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype) |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
610 |
{ |
9112
ec6800eaa340
(svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents:
9059
diff
changeset
|
611 |
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype)); |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
612 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
613 |
if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
614 |
/* GetTileTrackStatus() returns 0 for single tram bits. |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
615 |
* As we cannot change it there (easily) without breaking something, change it here */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
616 |
switch (GetSingleTramBit(dst_tile)) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
617 |
case DIAGDIR_NE: |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
618 |
case DIAGDIR_SW: |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
619 |
trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
620 |
break; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
621 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
622 |
case DIAGDIR_NW: |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
623 |
case DIAGDIR_SE: |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
624 |
trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
625 |
break; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
626 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
627 |
default: break; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
628 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
629 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
630 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
631 |
DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
632 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
633 |
/* Select only trackdirs we can reach from our current trackdir */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
634 |
trackdirbits &= TrackdirReachesTrackdirs(src_trackdir); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
635 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
636 |
/* Filter out trackdirs that would make 90 deg turns for trains */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
637 |
if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir); |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
638 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
639 |
DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
640 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
641 |
return trackdirbits; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
642 |
} |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
643 |
|
3957
3c39694becd2
(svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents:
3933
diff
changeset
|
644 |
|
1247 | 645 |
/* Will just follow the results of GetTileTrackStatus concerning where we can |
646 |
* go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and |
|
647 |
* 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
|
648 |
* 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
|
649 |
* 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
|
650 |
* copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */ |
1983 | 651 |
static void NPFFollowTrack(AyStar* aystar, OpenListNode* current) |
652 |
{ |
|
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
653 |
/* We leave src_tile on track src_trackdir in direction src_exitdir */ |
1950
6e4d384034d9
(svn r2456) * Prettyfied npf.c using enums and wrappers from rail.h.
matthijs
parents:
1945
diff
changeset
|
654 |
Trackdir src_trackdir = (Trackdir)current->path.node.direction; |
1247 | 655 |
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
|
656 |
DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir); |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
657 |
|
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
658 |
/* Is src_tile valid, and can be used? |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
659 |
* When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir. |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
660 |
* But we must not check the validity of this move, as src_tile is totally unrelated to the move, if a roadvehicle reversed on a junction. */ |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
661 |
bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(¤t->path.node, NPF_FLAG_IGNORE_START_TILE)); |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
662 |
|
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
663 |
/* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
664 |
TransportType type = (TransportType)aystar->user_data[NPF_TYPE]; |
7179
3e123c2b7c93
(svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents:
6949
diff
changeset
|
665 |
uint subtype = aystar->user_data[NPF_SUB_TYPE]; |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
666 |
|
1247 | 667 |
/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */ |
668 |
aystar->num_neighbours = 0; |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
669 |
DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); |
1247 | 670 |
|
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
671 |
/* We want to determine the tile we arrive, and which choices we have there */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
672 |
TileIndex dst_tile; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
673 |
TrackdirBits trackdirbits; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
674 |
|
1247 | 675 |
/* Find dest tile */ |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
676 |
if (ignore_src_tile) { |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
677 |
/* Do not perform any checks that involve src_tile */ |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
678 |
dst_tile = src_tile + TileOffsByDiagDir(src_exitdir); |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
679 |
trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype); |
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
680 |
} else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
681 |
/* We drive through the wormhole and arrive on the other side */ |
8693
3d721429cc4f
(svn r11760) -Codechange: unify the way how other end of a tunnel/bridge is determined at some places
smatz
parents:
8640
diff
changeset
|
682 |
dst_tile = GetOtherTunnelBridgeEnd(src_tile); |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
683 |
trackdirbits = TrackdirToTrackdirBits(src_trackdir); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
684 |
} else if (ForceReverse(src_tile, src_exitdir, type, subtype)) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
685 |
/* We can only reverse on this tile */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
686 |
dst_tile = src_tile; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
687 |
src_trackdir = ReverseTrackdir(src_trackdir); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
688 |
trackdirbits = TrackdirToTrackdirBits(src_trackdir); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
689 |
} else { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
690 |
/* We leave src_tile in src_exitdir and reach dst_tile */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
691 |
dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir)); |
1247 | 692 |
|
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
693 |
if (dst_tile != INVALID_TILE && !CanEnterTile(dst_tile, src_exitdir, type, subtype, (RailTypes)aystar->user_data[NPF_RAILTYPES], (Owner)aystar->user_data[NPF_OWNER])) dst_tile = INVALID_TILE; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
694 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
695 |
if (dst_tile == INVALID_TILE) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
696 |
/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
697 |
if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
698 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
699 |
dst_tile = src_tile; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
700 |
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
|
701 |
} |
1247 | 702 |
|
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
703 |
trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
704 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
705 |
if (trackdirbits == 0) { |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
706 |
/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */ |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
707 |
if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
708 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
709 |
dst_tile = src_tile; |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
710 |
src_trackdir = ReverseTrackdir(src_trackdir); |
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
711 |
|
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
712 |
trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype); |
1247 | 713 |
} |
2493
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2416
diff
changeset
|
714 |
} |
1965
ef27280abb9a
(svn r2471) - Fix: [ 1221249 ] [NPF] Vehicles try to drive into a tunnel entrance from above.
matthijs
parents:
1950
diff
changeset
|
715 |
|
1247 | 716 |
/* Enumerate possible track */ |
8979
4db3e50e402a
(svn r12058) -Fix: Teach NPF where road vehicles and trams can reverse.
frosch
parents:
8894
diff
changeset
|
717 |
uint i = 0; |
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
|
718 |
while (trackdirbits != 0) { |
5849
58039c9dc565
(svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents:
5838
diff
changeset
|
719 |
Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits); |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
720 |
DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits); |
1247 | 721 |
|
722 |
/* Check for oneway signal against us */ |
|
3792
2eb8388731bf
(svn r4788) - Codechange: RAILTYPE_{NORMAL,ELECTRIC,...} and RAIL_TYPE_{NORMAL,SIGNAL,...} have nearly the same name, rename RAIL_TYPE_* to RAIL_TILE_* of extra clarity
rubidium
parents:
3630
diff
changeset
|
723 |
if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_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
|
724 |
if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir)) |
6674
64f4781b4653
(svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents:
6585
diff
changeset
|
725 |
/* 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
|
726 |
break; |
1247 | 727 |
} |
728 |
{ |
|
729 |
/* We've found ourselves a neighbour :-) */ |
|
730 |
AyStarNode* neighbour = &aystar->neighbours[i]; |
|
731 |
neighbour->tile = dst_tile; |
|
732 |
neighbour->direction = dst_trackdir; |
|
733 |
/* Save user data */ |
|
734 |
neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS]; |
|
735 |
NPFFillTrackdirChoice(neighbour, current); |
|
736 |
} |
|
737 |
i++; |
|
738 |
} |
|
739 |
aystar->num_neighbours = i; |
|
740 |
} |
|
741 |
||
742 |
/* |
|
743 |
* Plan a route to the specified target (which is checked by target_proc), |
|
744 |
* 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
|
745 |
* 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
|
746 |
* originate from the second start node. |
1247 | 747 |
* When we are looking for one specific target (optionally multiple tiles), we |
748 |
* should use a good heuristic to perform aystar search. When we search for |
|
749 |
* multiple targets that are spread around, we should perform a breadth first |
|
750 |
* search by specifiying CalcZero as our heuristic. |
|
751 |
*/ |
|
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
752 |
static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, bool ignore_start_tile1, AyStarNode* start2, bool ignore_start_tile2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty) |
1983 | 753 |
{ |
1247 | 754 |
int r; |
755 |
NPFFoundTargetData result; |
|
756 |
||
757 |
/* Initialize procs */ |
|
758 |
_npf_aystar.CalculateH = heuristic_proc; |
|
759 |
_npf_aystar.EndNodeCheck = target_proc; |
|
760 |
_npf_aystar.FoundEndNode = NPFSaveTargetData; |
|
761 |
_npf_aystar.GetNeighbours = NPFFollowTrack; |
|
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
762 |
switch (type) { |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
763 |
default: NOT_REACHED(); |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
764 |
case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break; |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
765 |
case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break; |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
766 |
case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break; |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3993
diff
changeset
|
767 |
} |
1247 | 768 |
|
769 |
/* Initialize Start Node(s) */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
770 |
start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 771 |
start1->user_data[NPF_NODE_FLAGS] = 0; |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
772 |
NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1); |
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
|
773 |
_npf_aystar.addstart(&_npf_aystar, start1, 0); |
1247 | 774 |
if (start2) { |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
775 |
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
|
776 |
start2->user_data[NPF_NODE_FLAGS] = 0; |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
777 |
NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2); |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1453
diff
changeset
|
778 |
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
|
779 |
_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty); |
1247 | 780 |
} |
781 |
||
782 |
/* Initialize result */ |
|
783 |
result.best_bird_dist = (uint)-1; |
|
784 |
result.best_path_dist = (uint)-1; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
785 |
result.best_trackdir = INVALID_TRACKDIR; |
1247 | 786 |
_npf_aystar.user_path = &result; |
787 |
||
788 |
/* Initialize target */ |
|
789 |
_npf_aystar.user_target = target; |
|
790 |
||
791 |
/* Initialize user_data */ |
|
792 |
_npf_aystar.user_data[NPF_TYPE] = type; |
|
7179
3e123c2b7c93
(svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents:
6949
diff
changeset
|
793 |
_npf_aystar.user_data[NPF_SUB_TYPE] = sub_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
|
794 |
_npf_aystar.user_data[NPF_OWNER] = owner; |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3333
diff
changeset
|
795 |
_npf_aystar.user_data[NPF_RAILTYPES] = railtypes; |
1247 | 796 |
|
797 |
/* GO! */ |
|
798 |
r = AyStarMain_Main(&_npf_aystar); |
|
799 |
assert(r != AYSTAR_STILL_BUSY); |
|
800 |
||
801 |
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
|
802 |
if (target != NULL) { |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
803 |
DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile); |
1247 | 804 |
} else { |
805 |
/* Assumption: target == NULL, so we are looking for a depot */ |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
806 |
DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile); |
1247 | 807 |
} |
808 |
||
809 |
} |
|
810 |
return result; |
|
811 |
} |
|
812 |
||
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
813 |
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) |
1983 | 814 |
{ |
1247 | 815 |
AyStarNode start1; |
816 |
AyStarNode start2; |
|
817 |
||
818 |
start1.tile = tile1; |
|
819 |
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
|
820 |
/* 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
|
821 |
* return a not found then */ |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
822 |
start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 823 |
start1.direction = trackdir1; |
824 |
start2.direction = trackdir2; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
825 |
start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 826 |
|
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
827 |
return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0); |
1247 | 828 |
} |
829 |
||
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
830 |
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) |
1983 | 831 |
{ |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
832 |
return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes); |
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
|
833 |
} |
1247 | 834 |
|
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
835 |
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty) |
1983 | 836 |
{ |
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
|
837 |
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
|
838 |
AyStarNode start2; |
1247 | 839 |
|
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
|
840 |
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
|
841 |
start2.tile = tile2; |
1247 | 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 |
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
|
845 |
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
|
846 |
start2.direction = trackdir2; |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
847 |
start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 848 |
|
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
|
849 |
/* 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
|
850 |
* since we are just looking for any depot...*/ |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
851 |
return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty); |
1247 | 852 |
} |
853 |
||
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
854 |
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) |
1983 | 855 |
{ |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
856 |
return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, type, sub_type, owner, railtypes, 0); |
1247 | 857 |
} |
858 |
||
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
859 |
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) |
1983 | 860 |
{ |
1247 | 861 |
/* Okay, what we're gonna do. First, we look at all depots, calculate |
862 |
* the manhatten distance to get to each depot. We then sort them by |
|
863 |
* distance. We start by trying to plan a route to the closest, then |
|
864 |
* the next closest, etc. We stop when the best route we have found so |
|
865 |
* far, is shorter than the manhattan distance. This will obviously |
|
866 |
* always find the closest depot. It will probably be most efficient |
|
867 |
* for ships, since the heuristic will not be to far off then. I hope. |
|
868 |
*/ |
|
869 |
Queue depots; |
|
870 |
int r; |
|
4424 | 871 |
NPFFoundTargetData best_result = {(uint)-1, (uint)-1, INVALID_TRACKDIR, {INVALID_TILE, 0, {0, 0}}}; |
1247 | 872 |
NPFFoundTargetData result; |
873 |
NPFFindStationOrTileData target; |
|
874 |
AyStarNode start; |
|
875 |
Depot* current; |
|
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
876 |
Depot *depot; |
1247 | 877 |
|
878 |
init_InsSort(&depots); |
|
879 |
/* 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
|
880 |
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
|
881 |
/* 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
|
882 |
* owner */ |
10213
23cfd330ccac
(svn r12745) -Codechange: a bit of naming conventions, introduce Is*DepotTile()
smatz
parents:
9336
diff
changeset
|
883 |
if (IsDepotTypeTile(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
|
884 |
/* 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
|
885 |
depots.push(&depots, depot, DistanceManhattan(tile, depot->xy)); |
1247 | 886 |
} |
887 |
||
888 |
/* Now, let's initialise the aystar */ |
|
889 |
||
890 |
/* Initialize procs */ |
|
891 |
_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic; |
|
892 |
_npf_aystar.EndNodeCheck = NPFFindStationOrTile; |
|
893 |
_npf_aystar.FoundEndNode = NPFSaveTargetData; |
|
894 |
_npf_aystar.GetNeighbours = NPFFollowTrack; |
|
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
895 |
switch (type) { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
896 |
default: NOT_REACHED(); |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
897 |
case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
898 |
case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
899 |
case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4048
diff
changeset
|
900 |
} |
1247 | 901 |
|
902 |
/* Initialize target */ |
|
3135 | 903 |
target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */ |
1247 | 904 |
_npf_aystar.user_target = ⌖ |
905 |
||
906 |
/* Initialize user_data */ |
|
907 |
_npf_aystar.user_data[NPF_TYPE] = type; |
|
7179
3e123c2b7c93
(svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents:
6949
diff
changeset
|
908 |
_npf_aystar.user_data[NPF_SUB_TYPE] = sub_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
|
909 |
_npf_aystar.user_data[NPF_OWNER] = owner; |
1247 | 910 |
|
911 |
/* Initialize Start Node */ |
|
912 |
start.tile = tile; |
|
913 |
start.direction = trackdir; /* We will initialize user_data inside the loop below */ |
|
914 |
||
915 |
/* Initialize Result */ |
|
916 |
_npf_aystar.user_path = &result; |
|
917 |
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
|
918 |
best_result.best_bird_dist = (uint)-1; |
1247 | 919 |
|
920 |
/* Just iterate the depots in order of increasing distance */ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
921 |
while ((current = (Depot*)depots.pop(&depots))) { |
1247 | 922 |
/* 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
|
923 |
* depot's manhattan distance. HACK: We call DistanceManhattan |
1247 | 924 |
* again, we should probably modify the queue to give us that |
925 |
* value... */ |
|
926 |
if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist) |
|
927 |
break; |
|
928 |
||
929 |
/* Initialize Start Node */ |
|
930 |
/* We set this in case the target is also the start tile, we will just |
|
931 |
* return a not found then */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
932 |
start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; |
1247 | 933 |
start.user_data[NPF_NODE_FLAGS] = 0; |
9006
f68ac871be5c
(svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents:
8979
diff
changeset
|
934 |
NPFSetFlag(&start, NPF_FLAG_IGNORE_START_TILE, ignore_start_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
|
935 |
_npf_aystar.addstart(&_npf_aystar, &start, 0); |
1247 | 936 |
|
937 |
/* Initialize result */ |
|
938 |
result.best_bird_dist = (uint)-1; |
|
939 |
result.best_path_dist = (uint)-1; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1941
diff
changeset
|
940 |
result.best_trackdir = INVALID_TRACKDIR; |
1247 | 941 |
|
942 |
/* Initialize target */ |
|
943 |
target.dest_coords = current->xy; |
|
944 |
||
945 |
/* GO! */ |
|
946 |
r = AyStarMain_Main(&_npf_aystar); |
|
947 |
assert(r != AYSTAR_STILL_BUSY); |
|
948 |
||
949 |
/* This depot is closer */ |
|
950 |
if (result.best_path_dist < best_result.best_path_dist) |
|
951 |
best_result = result; |
|
952 |
} |
|
953 |
if (result.best_bird_dist != 0) { |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4561
diff
changeset
|
954 |
DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile); |
1247 | 955 |
} |
956 |
return best_result; |
|
957 |
} |
|
958 |
||
6573 | 959 |
void InitializeNPF() |
1247 | 960 |
{ |
7586
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
961 |
static bool first_init = true; |
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
962 |
if (first_init) { |
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
963 |
first_init = false; |
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
964 |
init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE); |
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
965 |
} else { |
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
966 |
AyStarMain_Clear(&_npf_aystar); |
0ee2ad46cd14
(svn r10357) -Fix: and when we're at it... yet another memory leak, but only when you start a new game; can't find any other memory leaks we can fix.
rubidium
parents:
7266
diff
changeset
|
967 |
} |
1463
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
968 |
_npf_aystar.loops_per_tick = 0; |
a9b4664cef34
(svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents:
1460
diff
changeset
|
969 |
_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
|
970 |
//_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
|
971 |
/* 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
|
972 |
* solution to really fix performance */ |
10775
7061477bfbcf
(svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents:
10703
diff
changeset
|
973 |
_npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes; |
1247 | 974 |
} |
975 |
||
1983 | 976 |
void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) |
977 |
{ |
|
1247 | 978 |
/* Ships don't really reach their stations, but the tile in front. So don't |
979 |
* save the station id for ships. For roadvehs we don't store it either, |
|
980 |
* because multistop depends on vehicles actually reaching the exact |
|
981 |
* dest_tile, not just any stop of that station. |
|
982 |
* So only for train orders to stations we fill fstd->station_index, for all |
|
983 |
* others only dest_coords */ |
|
9332
2e120d0bd632
(svn r12584) -Codechange: do not access the order type directly.
rubidium
parents:
9281
diff
changeset
|
984 |
if (v->current_order.IsType(OT_GOTO_STATION) && v->type == VEH_TRAIN) { |
9336
3f75a2c5e0d3
(svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents:
9332
diff
changeset
|
985 |
fstd->station_index = v->current_order.GetDestination(); |
1452
9285e482f984
(svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents:
1339
diff
changeset
|
986 |
/* Let's take the closest tile of the station as our target for trains */ |
9336
3f75a2c5e0d3
(svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents:
9332
diff
changeset
|
987 |
fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile); |
1247 | 988 |
} else { |
989 |
fstd->dest_coords = v->dest_tile; |
|
3135 | 990 |
fstd->station_index = INVALID_STATION; |
1247 | 991 |
} |
992 |
} |