npf.c
author matthijs
Mon, 02 May 2005 23:59:11 +0000
changeset 1752 cdbfb2f23e72
parent 1751 954dd2900ac9
child 1753 091f7a870a2a
permissions -rw-r--r--
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
- Add: GetVehicleTrackdir() helper function.
- Codechange: Moved SortStruct from vehicle_gui.h to ttd.h, so the dependency from vehicle.h on vehicle_gui.h could be removed.
- Codechange: Typedeffed the VehicleTypes struct so it can be used as the type for Vehicle.type instead of "byte".
- Codechange: Removed prototype for VehicleSorter(), which had no implementation anymore and was never called.
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     1
#include "stdafx.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     2
#include "ttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1248
diff changeset
     3
#include "debug.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     4
#include "npf.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     5
#include "aystar.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     6
#include "macros.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     7
#include "pathfind.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     8
#include "station.h"
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
     9
#include "tile.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
    10
#include "depot.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    11
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    12
AyStar _train_find_station;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    13
AyStar _train_find_depot;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    14
AyStar _road_find_station;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    15
AyStar _road_find_depot;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    16
AyStar _npf_aystar;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    17
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    18
/* Maps a trackdir to the bit that stores its status in the map arrays, in the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    19
 * direction along with the trackdir */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    20
const byte _signal_along_trackdir[14] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    21
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    22
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    23
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    24
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    25
/* Maps a trackdir to the bit that stores its status in the map arrays, in the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    26
 * direction against the trackdir */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    27
const byte _signal_against_trackdir[14] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    28
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    29
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    30
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    31
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    32
/* Maps a trackdir to the trackdirs that can be reached from it (ie, when
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    33
 * entering the next tile */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    34
const uint16 _trackdir_reaches_trackdirs[14] = {
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    35
	0x1009, 0x0016, 0x1009, 0x0016, 0x0520, 0x0016, 0, 0,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    36
	0x0520, 0x2A00, 0x2A00, 0x0520, 0x2A00, 0x1009
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    37
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    38
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
    39
const uint16 _next_trackdir[14] = {
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
    40
	0,  1,  3,  2,  5,  4, 0, 0,
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
    41
	8,  9,  11, 10, 13, 12
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
    42
};
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
    43
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    44
/* Maps a trackdir to all trackdirs that make 90 deg turns with it. */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    45
const uint16 _trackdir_crosses_trackdirs[14] = {
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    46
	0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C, 0, 0,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
    47
	0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    48
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    49
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    50
/* Maps a track to all tracks that make 90 deg turns with it. */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    51
const byte _track_crosses_tracks[6] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    52
	0x2, /* Track 1 -> Track 2 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    53
	0x1, /* Track 2 -> Track 1 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    54
	0x30, /* Upper -> Left | Right */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    55
	0x30, /* Lower -> Left | Right */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    56
	0x0C, /* Left -> Upper | Lower */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    57
	0x0C, /* Right -> Upper | Lower */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    58
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    59
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    60
/* Maps a trackdir to the (4-way) direction the tile is exited when following
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    61
 * that trackdir */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    62
const byte _trackdir_to_exitdir[14] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    63
	0,1,0,1,2,1, 0,0,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    64
	2,3,3,2,3,0,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    65
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    66
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    67
const byte _track_exitdir_to_trackdir[6][4] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    68
	{0,    0xff, 8,    0xff},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    69
	{0xff, 1,    0xff, 9},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    70
	{2,    0xff, 0xff, 10},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    71
	{0xff, 3,    11,   0xf},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    72
	{0xff, 0xff, 4,    12},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    73
	{13,   5,    0xff, 0xff}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    74
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    75
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    76
const byte _track_direction_to_trackdir[6][8] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    77
	{0xff, 0,    0xff, 0xff, 0xff, 8,    0xff, 0xff},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    78
	{0xff, 0xff, 0xff, 1,    0xff, 0xff, 0xff, 9},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    79
	{0xff, 0xff, 2,    0xff, 0xff, 0xff, 10,   0xff},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    80
	{0xff, 0xff, 3,    0xff, 0xff, 0xff, 11,   0xff},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    81
	{12,   0xff, 0xff, 0xff, 4,    0xff, 0xff, 0xff},
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    82
	{13,   0xff, 0xff, 0xff, 5,    0xff, 0xff, 0xff}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    83
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    84
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    85
const byte _dir_to_diag_trackdir[4] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    86
	0, 1, 8, 9,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    87
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    88
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    89
const byte _reverse_dir[4] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    90
	2, 3, 0, 1
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    91
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    92
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    93
const byte _reverse_trackdir[14] = {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    94
	8, 9, 10, 11, 12, 13, 0xFF, 0xFF,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    95
	0, 1, 2,  3,  4,  5
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    96
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    97
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    98
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
    99
 * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   100
 */
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   101
#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   102
static const uint _trackdir_length[14] = {
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   103
	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
   104
	0, 0,
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   105
	NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   106
};
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   107
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   108
uint NTPHash(uint key1, uint key2)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   109
{
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   110
	/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   111
	return PATHFIND_HASH_TILE(key1);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   112
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   113
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   114
uint NPFHash(uint key1, uint key2)
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   115
{
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   116
	/* 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
   117
	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
   118
	uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   119
	/* The value of 14 below is based on the maximum value of key2 (13) */
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   120
	return ((((part1 << NPF_HASH_HALFBITS) | part2)) + (NPF_HASH_SIZE * key2 / 14)) % NPF_HASH_SIZE;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   121
}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   122
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   123
int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   124
	return 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   125
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   126
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   127
/* 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
   128
 * 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
   129
 * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   130
 */
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   131
TileIndex CalcClosestStationTile(int station, TileIndex tile) {
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   132
	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
   133
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   134
	int x1,x2,x3,tx;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   135
	int y1,y2,y3,ty;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   136
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   137
	x1 = TileX(st->train_tile);  y1 = TileY(st->train_tile);  // topmost corner of station
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   138
	x2 = x1 + st->trainst_w - 1; y2 = y1 + st->trainst_h - 1; // lowermost corner of station
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   139
	x3 = TileX(tile);            y3 = TileY(tile);            // tile we take the distance from
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   140
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   141
	// we are going the aim for the x coordinate of the closest corner
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   142
	// but if we are between those coordinates, we will aim for our own x coordinate
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   143
	if (x3 < x1)
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   144
		tx = x1;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   145
	else if (x3 > x2)
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   146
		tx = x2;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   147
	else
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   148
		tx = x3;
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
	// same for y coordinate, see above comment
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   151
	if (y3 < y1)
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   152
		ty = y1;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   153
	else if (y3 > y2)
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   154
		ty = y2;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   155
	else
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   156
		ty = y3;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   157
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   158
	// return the tile of our target coordinates
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   159
	return TILE_XY(tx,ty);
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   160
};
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   161
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   162
/* 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
   163
 * 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
   164
 */
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   165
int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent) {
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   166
	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
   167
	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
   168
	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
   169
	TileIndex to = fstd->dest_coords;
1453
687663191db3 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   170
	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
   171
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   172
	// for train-stations, we are going to aim for the closest station tile
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   173
	if ((as->user_data[NPF_TYPE] == TRANSPORT_RAIL) && (fstd->station_index != -1))
1453
687663191db3 (svn r1957) Compilation fix.
pasky
parents: 1452
diff changeset
   174
		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
   175
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   176
	if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD)
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   177
		/* 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
   178
		dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   179
	else
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   180
		/* Ships and trains can also go diagonal, so the minimum distance is shorter */
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1661
diff changeset
   181
		dist = DistanceTrack(from, to) * NPF_TILE_LENGTH;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   182
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   183
	if (dist < ftd->best_bird_dist) {
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   184
		ftd->best_bird_dist = dist;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   185
		ftd->best_trackdir = current->user_data[NPF_TRACKDIR_CHOICE];
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   186
	}
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
   187
	DEBUG(npf, 4)("Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   188
	return dist;
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   189
}
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   190
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   191
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   192
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   193
 * get here, either getting it from the current choice or from the parent's
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   194
 * choice */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   195
void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   196
{
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   197
	if (parent->path.parent == NULL) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   198
		byte trackdir = current->direction;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   199
		/* This is a first order decision, so we'd better save the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   200
		 * direction we chose */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   201
		current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   202
		DEBUG(npf, 6)("Saving trackdir: %#x", trackdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   203
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   204
		/* We've already made the decision, so just save our parent's
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   205
		 * decision */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   206
		current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   207
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   208
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   209
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   210
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   211
/* Will return the cost of the tunnel. If it is an entry, it will return the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   212
 * cost of that tile. If the tile is an exit, it will return the tunnel length
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   213
 * including the exit tile. Requires that this is a Tunnel tile */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   214
uint NPFTunnelCost(AyStarNode* current) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   215
	byte exitdir = _trackdir_to_exitdir[current->direction];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   216
	TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   217
	if ( (uint)(_map5[tile] & 3) == _reverse_dir[exitdir]) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   218
		/* We just popped out if this tunnel, since were
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   219
		 * facing the tunnel exit */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   220
		FindLengthOfTunnelResult flotr;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   221
		flotr = FindLengthOfTunnel(tile, _reverse_dir[exitdir]);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   222
		return flotr.length * NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   223
		//TODO: Penalty for tunnels?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   224
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   225
		/* We are entering the tunnel, the enter tile is just a
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   226
		 * straight track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   227
		return NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   228
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   229
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   230
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   231
uint NPFSlopeCost(AyStarNode* current) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   232
	TileIndex next = current->tile + TileOffsByDir(_trackdir_to_exitdir[current->direction]);
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   233
	int x,y;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   234
	int8 z1,z2;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   235
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   236
	x = TileX(current->tile) * 16;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   237
	y = TileY(current->tile) * 16;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   238
	z1 = GetSlopeZ(x+8, y+8);
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   239
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   240
	x = TileX(next) * 16;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   241
	y = TileY(next) * 16;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   242
	z2 = GetSlopeZ(x+8, y+8);
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   243
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   244
	if ((z2 - z1) > 1) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   245
		/* Slope up */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   246
		return _patches.npf_rail_slope_penalty;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   247
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   248
	return 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   249
	/* Should we give a bonus for slope down? Probably not, we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   250
	 * could just substract that bonus from the penalty, because
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   251
	 * there is only one level of steepness... */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   252
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   253
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
   254
/* Mark tiles by mowing the grass when npf debug level >= 1 */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   255
void NPFMarkTile(TileIndex tile) {
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   256
#ifdef NO_DEBUG_MESSAGES
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   257
	return;
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   258
#else
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
   259
	if (_debug_npf_level >= 1)
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
   260
		switch(GetTileType(tile)) {
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
   261
			case MP_RAILWAY:
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
   262
			case MP_STREET:
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
   263
				/* DEBUG: mark visited tiles by mowing the grass under them
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
   264
				 * ;-) */
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
   265
				_map2[tile] &= ~15;
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
   266
				MarkTileDirtyByTile(tile);
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
   267
				break;
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
   268
			default:
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
   269
				break;
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
   270
		}
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
   271
#endif
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   272
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   273
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   274
int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   275
	//TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   276
	int32 cost = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   277
	byte trackdir = current->direction;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   278
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   279
	cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   280
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   281
	if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(current->direction))
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   282
		cost += _patches.npf_buoy_penalty; /* A small penalty for going over buoys */
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   283
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   284
	if (current->direction != _next_trackdir[parent->path.node.direction])
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   285
		cost += _patches.npf_water_curve_penalty;
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   286
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   287
	/* TODO More penalties? */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   288
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   289
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   290
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   291
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   292
/* Determine the cost of this node, for road tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   293
int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   294
	TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   295
	int32 cost = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   296
	/* Determine base length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   297
	switch (GetTileType(tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   298
		case MP_TUNNELBRIDGE:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   299
			if ((_map5[tile] & 0xF0)==0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   300
				cost = NPFTunnelCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   301
				break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   302
			}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   303
			/* Fall through if above if is false, it is a bridge
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   304
			 * then. We treat that as ordinary rail */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   305
		case MP_STREET:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   306
			cost = NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   307
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   308
		default:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   309
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   310
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   311
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   312
	/* Determine extra costs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   313
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   314
	/* Check for slope */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   315
	cost += NPFSlopeCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   316
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   317
	/* Check for turns */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   318
	//TODO
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   319
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   320
	NPFMarkTile(tile);
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   321
	DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   322
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   323
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   324
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   325
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   326
/* Determine the cost of this node, for railway tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   327
int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   328
	TileIndex tile = current->tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   329
	byte trackdir = current->direction;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   330
	int32 cost = 0;
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   331
	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
   332
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   333
	/* Determine base length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   334
	switch (GetTileType(tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   335
		case MP_TUNNELBRIDGE:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   336
			if ((_map5[tile] & 0xF0)==0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   337
				cost = NPFTunnelCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   338
				break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   339
			}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   340
			/* Fall through if above if is false, it is a bridge
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   341
			 * then. We treat that as ordinary rail */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   342
		case MP_RAILWAY:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   343
			cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   344
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   345
		case MP_STREET: /* Railway crossing */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   346
			cost = NPF_TILE_LENGTH;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   347
			break;
1503
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   348
		case MP_STATION:
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   349
			/* We give a station tile a penalty. Logically we would only
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   350
					* want to give station tiles that are not our destination
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   351
					* this penalty. This would discourage trains to drive through
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   352
					* busy stations. But, we can just give any station tile a
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   353
					* penalty, because every possible route will get this penalty
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   354
					* exactly once, on its end tile (if it's a station) and it
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   355
			* will therefore not make a difference. */
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   356
			cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty;
be35a76c7555 (svn r2007) - Fix: [NPF] Slope penalties did not work correctly with foundations. (HackyKid)
matthijs
parents: 1502
diff changeset
   357
			break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   358
		default:
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   359
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   360
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   361
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   362
	/* Determine extra costs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   363
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   364
	/* Check for signals */
1502
e84925444095 (svn r2006) - Fix: [NPF] Wrong signal detection for last signal red detection. Multiple tracks per tile with only one signal were detected wrong. (HackyKid)
matthijs
parents: 1464
diff changeset
   365
	if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40 && (_map3_lo[tile] & _signal_along_trackdir[trackdir]) != 0) {
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   366
		/* Ordinary track with signals */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   367
		if ((_map2[tile] & _signal_along_trackdir[trackdir]) == 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   368
			/* 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
   369
			if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   370
				/* Penalize the first signal we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   371
				 * 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
   372
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
   373
				/* Is this a presignal exit or combo? */
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
   374
				if ((_map3_hi[tile] & 0x3) == 0x2 || (_map3_hi[tile] & 0x3) == 0x3)
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
   375
					/* Penalise exit and combo signals differently (heavier) */
d38655053062 (svn r2147) - Add: [NPF] Give red presignal exit signals a different (higher) penalty, to discourage trains from waiting at presignal exits.
matthijs
parents: 1617
diff changeset
   376
					cost += _patches.npf_rail_firstred_exit_penalty;
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
   377
				else
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
   378
					cost += _patches.npf_rail_firstred_penalty;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   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
			/* 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
   381
			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
   382
		} else {
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   383
			/* 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
   384
			NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   385
		}
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   386
		NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   387
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   388
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   389
	/* 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
   390
	 * red */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   391
	new_node.path.node = *current;
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   392
	if (as->EndNodeCheck(as, &new_node)==AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   393
		cost += _patches.npf_rail_lastred_penalty;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   394
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   395
	/* Check for slope */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   396
	cost += NPFSlopeCost(current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   397
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   398
	/* Check for turns */
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1749
diff changeset
   399
	if (current->direction != _next_trackdir[parent->path.node.direction])
1460
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   400
		cost += _patches.npf_rail_curve_penalty;
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   401
	//TODO, with realistic acceleration, also the amount of straight track between
ebd1cfae9588 (svn r1964) - Add: [NPF] Added a penalty
matthijs
parents: 1459
diff changeset
   402
	//      curves should be taken into account, as this affects the speed limit.
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   403
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   404
	/* Check for occupied track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   405
	//TODO
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   406
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   407
	NPFMarkTile(tile);
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   408
	DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   409
	return cost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   410
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   411
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   412
/* Will find any depot */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   413
int32 NPFFindDepot(AyStar* as, OpenListNode *current) {
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   414
	TileIndex tile = current->path.node.tile;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   415
	if (IsTileDepotType(tile, as->user_data[NPF_TYPE]))
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   416
		return AYSTAR_FOUND_END_NODE;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   417
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   418
		return AYSTAR_DONE;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   419
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   420
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   421
/* Will find a station identified using the NPFFindStationOrTileData */
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1503
diff changeset
   422
int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current) {
1464
266d3b0ee2c8 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   423
	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
   424
	AyStarNode *node = &current->path.node;
1464
266d3b0ee2c8 (svn r1968) - Fix: [NPF] Mixed declarations and statements
matthijs
parents: 1463
diff changeset
   425
	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
   426
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   427
	/* See if we checked this before */
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   428
	if (NPFGetFlag(node, NPF_FLAG_TARGET_CHECKED))
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   429
		return NPFGetFlag(node, NPF_FLAG_IS_TARGET);
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   430
	/* We're gonna check this now and store the result, let's mark that */
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   431
	NPFSetFlag(node, NPF_FLAG_TARGET_CHECKED, true);
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   432
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   433
	/* If GetNeighbours said we could get here, we assume the station type
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   434
	 * 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
   435
	if (
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   436
		(fstd->station_index == -1 && tile == fstd->dest_coords) || /* We've found the tile, or */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   437
		(IsTileType(tile, MP_STATION) && _map2[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
   438
	) {
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   439
		NPFSetFlag(node, NPF_FLAG_TARGET_CHECKED, true);
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   440
		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
   441
	} else {
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   442
		NPFSetFlag(node, NPF_FLAG_TARGET_CHECKED, false);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   443
		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
   444
	}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   445
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   446
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   447
/* To be called when current contains the (shortest route to) the target node.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   448
 * Will fill the contents of the NPFFoundTargetData using
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   449
 * AyStarNode[NPF_TRACKDIR_CHOICE].
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   450
 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   451
void NPFSaveTargetData(AyStar* as, OpenListNode* current) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   452
	NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   453
	ftd->best_trackdir = current->path.node.user_data[NPF_TRACKDIR_CHOICE];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   454
	ftd->best_path_dist = current->g;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   455
	ftd->best_bird_dist = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   456
	ftd->node = current->path.node;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   457
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   458
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   459
/**
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   460
 * Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   461
 * Note that there is no check if the given trackdir is actually present on
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   462
 * the tile!
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   463
 * The given trackdir is used when there are (could be) multiple rail types on
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   464
 * one tile.
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   465
 */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   466
static inline RailType GetTileRailType(TileIndex tile, byte trackdir)
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   467
{
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   468
	byte type = INVALID_RAILTYPE;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   469
	switch (GetTileType(tile)) {
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   470
		case MP_RAILWAY:
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   471
			/* railway track */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   472
			type = _map3_lo[tile] & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   473
			break;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   474
		case MP_STREET:
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   475
			/* rail/road crossing */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   476
			if ((_map5[tile] & 0xF0) == 0x10)
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   477
				type = _map3_hi[tile] & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   478
			break;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   479
		case MP_STATION:
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   480
			if (IsTrainStationTile(tile))
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   481
				type = _map3_lo[tile] & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   482
			break;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   483
		case MP_TUNNELBRIDGE:
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   484
			/* railway tunnel */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   485
			if ((_map5[tile] & 0xFC) == 0) type = _map3_lo[tile] & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   486
			/* railway bridge ending */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   487
			if ((_map5[tile] & 0xC6) == 0x80) type = _map3_lo[tile] & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   488
			/* on railway bridge */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   489
			if ((_map5[tile] & 0xC6) == 0xC0 && (_map5[tile] & 0x1) == (_trackdir_to_exitdir[trackdir] & 0x1))
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   490
				type = (_map3_lo[tile] >> 4) & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   491
			/* under bridge (any type) */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   492
			if ((_map5[tile] & 0xC0) == 0xC0 && (_map5[tile] & 0x1) != (trackdir & 0x1))
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   493
				type = _map3_lo[tile] & RAILTYPE_MASK;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   494
			break;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   495
		default:
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   496
			break;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   497
	}
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   498
	return type;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   499
}
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   500
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   501
/* Will just follow the results of GetTileTrackStatus concerning where we can
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   502
 * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   503
 * 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
   504
 * 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
   505
 * 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
   506
 * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   507
void NPFFollowTrack(AyStar* aystar, OpenListNode* current) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   508
	byte src_trackdir = current->path.node.direction;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   509
	TileIndex src_tile = current->path.node.tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   510
	byte src_exitdir = _trackdir_to_exitdir[src_trackdir];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   511
	FindLengthOfTunnelResult flotr;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   512
	TileIndex dst_tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   513
	int i = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   514
	uint trackdirs, ts;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   515
	TransportType type = aystar->user_data[NPF_TYPE];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   516
	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   517
	aystar->num_neighbours = 0;
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   518
	DEBUG(npf, 4)("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   519
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   520
	/* Find dest tile */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   521
	if (IsTileType(src_tile, MP_TUNNELBRIDGE) && (_map5[src_tile] & 0xF0)==0 && (_map5[src_tile] & 3) == src_exitdir) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   522
		/* This is a tunnel. We know this tunnel is our type,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   523
		 * otherwise we wouldn't have got here. It is also facing us,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   524
		 * so we should skip it's body */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   525
		flotr = FindLengthOfTunnel(src_tile, src_exitdir);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   526
		dst_tile = flotr.tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   527
	} else {
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   528
		if (type != TRANSPORT_WATER && (IsRoadStationTile(src_tile) || IsTileDepotType(src_tile, type))){
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   529
			/* This is a road station or a train or road depot. We can enter and exit
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   530
			 * those from one side only. Trackdirs don't support that (yet), so we'll
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   531
			 * do this here. */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   532
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   533
			byte exitdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   534
			/* Find out the exit direction first */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   535
			if (IsRoadStationTile(src_tile))
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   536
				exitdir = GetRoadStationDir(src_tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   537
			else /* Train or road depot. Direction is stored the same for both, in map5 */
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   538
				exitdir = GetDepotDirection(src_tile, type);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   539
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   540
			/* Let's see if were headed the right way */
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   541
			if (src_trackdir == _dir_to_diag_trackdir[_reverse_dir[exitdir]])
1644
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   542
				/* We are headed inwards. We can only reverse here, so we'll not
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   543
				 * consider this direction, but jump ahead to the reverse direction.
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   544
				 * It would be nicer to return one neighbour here (the reverse
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   545
				 * trackdir of the one we are considering now) and then considering
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   546
				 * that one to return the tracks outside of the depot. But, because
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   547
				 * the code layout is cleaner this way, we will just pretend we are
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   548
				 * reversed already */
1a08c3ebdcd8 (svn r2148) - Add: [NPF] Trains can now plan taking into account that they can reverse in depots. This makes forced servicing tracks work with NPF.
matthijs
parents: 1643
diff changeset
   549
				src_trackdir = _reverse_trackdir[src_trackdir];
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   550
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   551
		/* This a normal tile, a bridge, a tunnel exit, etc. */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   552
		dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(_trackdir_to_exitdir[src_trackdir]));
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   553
		if (dst_tile == INVALID_TILE) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   554
			/* We reached the border of the map */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   555
			/* TODO Nicer control flow for this */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   556
			return;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   557
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   558
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   559
1749
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   560
	/* check correct rail type (mono, maglev, etc)
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   561
	 * XXX: This now compares with the previous tile, which should not pose a
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   562
	 * problem, but it might be nicer to compare with the first tile, or even
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   563
	 * the type of the vehicle... Maybe an NPF_RAILTYPE userdata sometime? */
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   564
	if (type == TRANSPORT_RAIL) {
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   565
		byte src_type = GetTileRailType(src_tile, src_trackdir);
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   566
		byte dst_type = GetTileRailType(dst_tile, _trackdir_to_exitdir[src_trackdir]);
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   567
		if (src_type != dst_type) {
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   568
			return;
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   569
		}
bb7fa90dd0cb (svn r2253) - Fix: [ 1190896 1184378 ] [NPF] Trains ignoring their railtype (mono, maglev) (glx)
matthijs
parents: 1700
diff changeset
   570
	}
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
   571
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
   572
	/* Check the owner of the tile */
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
   573
	if (
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   574
		IsTileType(dst_tile, MP_RAILWAY) /* Rail tile (also rail depot) */
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   575
		|| IsTrainStationTile(dst_tile) /* Rail station tile */
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   576
		|| IsTileDepotType(dst_tile, TRANSPORT_ROAD) /* Road depot tile */
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   577
		|| IsRoadStationTile(dst_tile) /* Road station tile */
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   578
		|| IsTileDepotType(dst_tile, TRANSPORT_WATER) /* Water depot tile */
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
   579
	) /* TODO: Crossings, tunnels and bridges are "public" now */
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
   580
		/* The above cases are "private" tiles, we need to check the owner */
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
   581
		if (!IsTileOwner(dst_tile, aystar->user_data[NPF_OWNER]))
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
   582
			return;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   583
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   584
	/* Determine available tracks */
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   585
	if (type != TRANSPORT_WATER && (IsRoadStationTile(dst_tile) || IsTileDepotType(dst_tile, type))){
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   586
		/* Road stations and road and train depots return 0 on GTTS, so we have to do this by hand... */
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
   587
		byte exitdir;
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
   588
		if (IsRoadStationTile(dst_tile))
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
   589
			exitdir = GetRoadStationDir(dst_tile);
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   590
		else /* Road or train depot */
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   591
			exitdir = GetDepotDirection(dst_tile, type);
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   592
		/* Find the trackdirs that are available for a depot or station with this
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   593
		 * orientation. They are only "inwards", since we are reaching this tile
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   594
		 * from some other tile. This prevents vehicles driving into depots from
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1644
diff changeset
   595
		 * the back */
1655
f45015d2df03 (svn r2159) - Fix: [NPF] Road vehicles never found their target station or depots (introduced in r2154)
matthijs
parents: 1650
diff changeset
   596
		ts = (1 << _dir_to_diag_trackdir[_reverse_dir[exitdir]]);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   597
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   598
		ts = GetTileTrackStatus(dst_tile, type);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   599
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   600
	trackdirs = ts & 0x3F3F; /* Filter out signal status and the unused bits */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   601
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
   602
	DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirs);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   603
	/* Select only trackdirs we can reach from our current trackdir */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   604
	trackdirs &= _trackdir_reaches_trackdirs[src_trackdir];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   605
	if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   606
		trackdirs &= ~_trackdir_crosses_trackdirs[src_trackdir];
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1677
diff changeset
   607
	DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirs);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   608
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   609
	/* Enumerate possible track */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   610
	while (trackdirs != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   611
		byte dst_trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   612
		dst_trackdir =  FindFirstBit2x64(trackdirs);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   613
		trackdirs = KillFirstBit2x64(trackdirs);
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
   614
		DEBUG(npf, 5)("Expanded into trackdir: %d, remaining trackdirs: %#x", dst_trackdir, trackdirs);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   615
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   616
		/* Check for oneway signal against us */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   617
		if (IsTileType(dst_tile, MP_RAILWAY) && (_map5[dst_tile]&0xC0) == 0x40) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   618
			// the tile has a signal
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   619
			byte signal_present = _map3_lo[dst_tile];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   620
			if (!(signal_present & _signal_along_trackdir[dst_trackdir])) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   621
				// if one way signal not pointing towards us, stop going in this direction.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   622
				if (signal_present & _signal_against_trackdir[dst_trackdir])
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   623
					break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   624
			}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   625
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   626
		{
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   627
			/* We've found ourselves a neighbour :-) */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   628
			AyStarNode* neighbour = &aystar->neighbours[i];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   629
			neighbour->tile = dst_tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   630
			neighbour->direction = dst_trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   631
			/* Save user data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   632
			neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   633
			NPFFillTrackdirChoice(neighbour, current);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   634
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   635
		i++;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   636
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   637
	aystar->num_neighbours = i;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   638
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   639
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   640
/*
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   641
 * Plan a route to the specified target (which is checked by target_proc),
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   642
 * from start1 and if not NULL, from start2 as well. The type of transport we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   643
 * are checking is in type.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   644
 * When we are looking for one specific target (optionally multiple tiles), we
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   645
 * should use a good heuristic to perform aystar search. When we search for
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   646
 * multiple targets that are spread around, we should perform a breadth first
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   647
 * search by specifiying CalcZero as our heuristic.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   648
 */
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
   649
NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   650
	int r;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   651
	NPFFoundTargetData result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   652
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   653
	/* Initialize procs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   654
	_npf_aystar.CalculateH = heuristic_proc;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   655
	_npf_aystar.EndNodeCheck = target_proc;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   656
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   657
	_npf_aystar.GetNeighbours = NPFFollowTrack;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   658
	if (type == TRANSPORT_RAIL)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   659
		_npf_aystar.CalculateG = NPFRailPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   660
	else if (type == TRANSPORT_ROAD)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   661
		_npf_aystar.CalculateG = NPFRoadPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   662
	else if (type == TRANSPORT_WATER)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   663
		_npf_aystar.CalculateG = NPFWaterPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   664
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   665
		assert(0);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   666
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   667
	/* Initialize Start Node(s) */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   668
	start1->user_data[NPF_TRACKDIR_CHOICE] = 0xff;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   669
	start1->user_data[NPF_NODE_FLAGS] = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   670
	_npf_aystar.addstart(&_npf_aystar, start1);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   671
	if (start2) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   672
		start2->user_data[NPF_TRACKDIR_CHOICE] = 0xff;
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   673
		start2->user_data[NPF_NODE_FLAGS] = 0;
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1453
diff changeset
   674
		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   675
		_npf_aystar.addstart(&_npf_aystar, start2);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   676
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   677
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   678
	/* Initialize result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   679
	result.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   680
	result.best_path_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   681
	result.best_trackdir = 0xff;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   682
	_npf_aystar.user_path = &result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   683
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   684
	/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   685
	_npf_aystar.user_target = target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   686
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   687
	/* Initialize user_data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   688
	_npf_aystar.user_data[NPF_TYPE] = type;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   689
	_npf_aystar.user_data[NPF_OWNER] = owner;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   690
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   691
	/* GO! */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   692
	r = AyStarMain_Main(&_npf_aystar);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   693
	assert(r != AYSTAR_STILL_BUSY);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   694
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   695
	if (result.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   696
		if (target) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   697
			DEBUG(misc, 1) ("NPF: Could not find route to 0x%x from 0x%x.", target->dest_coords, start1->tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   698
		} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   699
			/* Assumption: target == NULL, so we are looking for a depot */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   700
			DEBUG(misc, 1) ("NPF: Could not find route to a depot from 0x%x.", start1->tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   701
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   702
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   703
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   704
	return result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   705
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   706
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
   707
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, byte trackdir1, TileIndex tile2, byte trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   708
	AyStarNode start1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   709
	AyStarNode start2;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   710
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   711
	start1.tile = tile1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   712
	start2.tile = tile2;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   713
	start1.direction = trackdir1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   714
	start2.direction = trackdir2;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   715
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
   716
	return NPFRouteInternal(&start1, &start2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   717
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   718
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
   719
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, byte trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   720
	AyStarNode start;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   721
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   722
	assert(tile != 0);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   723
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   724
	start.tile = tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   725
	start.direction = trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   726
	/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   727
	 * return a not found then */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   728
	start.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   729
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
   730
	return NPFRouteInternal(&start, NULL, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   731
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   732
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
   733
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, byte trackdir, TransportType type, Owner owner) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   734
	AyStarNode start;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   735
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   736
	start.tile = tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   737
	start.direction = trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   738
	/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   739
	 * return a not found then */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   740
	start.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   741
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   742
	/* perform a breadth first search. Target is NULL,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   743
	 * since we are just looking for any 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
   744
	return NPFRouteInternal(&start, NULL, NULL, NPFFindDepot, NPFCalcZero, type, owner);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   745
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   746
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
   747
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, byte trackdir, TransportType type, Owner owner) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   748
	/* Okay, what we're gonna do. First, we look at all depots, calculate
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   749
	 * the manhatten distance to get to each depot. We then sort them by
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   750
	 * distance. We start by trying to plan a route to the closest, then
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   751
	 * the next closest, etc. We stop when the best route we have found so
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   752
	 * far, is shorter than the manhattan distance. This will obviously
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   753
	 * always find the closest depot. It will probably be most efficient
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   754
	 * for ships, since the heuristic will not be to far off then. I hope.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   755
	 */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   756
	Queue depots;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   757
	int r;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   758
	NPFFoundTargetData best_result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   759
	NPFFoundTargetData result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   760
	NPFFindStationOrTileData target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   761
	AyStarNode start;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   762
	Depot* current;
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   763
	Depot *depot;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   764
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   765
	init_InsSort(&depots);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   766
	/* 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
   767
	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
   768
		/* 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
   769
		 * owner */
1338
9e3e0bc7fe61 (svn r1842) Fix another typo made in r1834
tron
parents: 1330
diff changeset
   770
		if (IsValidDepot(depot) && IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner))
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   771
			/* 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
   772
			depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   773
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   774
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   775
	/* Now, let's initialise the aystar */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   776
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   777
	/* Initialize procs */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   778
	_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   779
	_npf_aystar.EndNodeCheck = NPFFindStationOrTile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   780
	_npf_aystar.FoundEndNode = NPFSaveTargetData;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   781
	_npf_aystar.GetNeighbours = NPFFollowTrack;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   782
	if (type == TRANSPORT_RAIL)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   783
		_npf_aystar.CalculateG = NPFRailPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   784
	else if (type == TRANSPORT_ROAD)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   785
		_npf_aystar.CalculateG = NPFRoadPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   786
	else if (type == TRANSPORT_WATER)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   787
		_npf_aystar.CalculateG = NPFWaterPathCost;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   788
	else
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   789
		assert(0);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   790
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   791
	/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   792
	target.station_index = -1; /* We will initialize dest_coords inside the loop below */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   793
	_npf_aystar.user_target = &target;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   794
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   795
	/* Initialize user_data */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   796
	_npf_aystar.user_data[NPF_TYPE] = type;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   797
	_npf_aystar.user_data[NPF_OWNER] = owner;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   798
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   799
	/* Initialize Start Node */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   800
	start.tile = tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   801
	start.direction = trackdir; /* We will initialize user_data inside the loop below */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   802
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   803
	/* Initialize Result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   804
	_npf_aystar.user_path = &result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   805
	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
   806
	best_result.best_bird_dist = (uint)-1;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   807
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   808
	/* Just iterate the depots in order of increasing distance */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   809
	while ((current = depots.pop(&depots))) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   810
		/* 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
   811
		 * depot's manhattan distance. HACK: We call DistanceManhattan
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   812
		 * again, we should probably modify the queue to give us that
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   813
		 * value... */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   814
		if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   815
			break;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   816
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   817
		/* Initialize Start Node */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   818
		/* We set this in case the target is also the start tile, we will just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   819
		 * return a not found then */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   820
		start.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   821
		start.user_data[NPF_NODE_FLAGS] = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   822
		_npf_aystar.addstart(&_npf_aystar, &start);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   823
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   824
		/* Initialize result */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   825
		result.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   826
		result.best_path_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   827
		result.best_trackdir = 0xff;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   828
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   829
		/* Initialize target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   830
		target.dest_coords = current->xy;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   831
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   832
		/* GO! */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   833
		r = AyStarMain_Main(&_npf_aystar);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   834
		assert(r != AYSTAR_STILL_BUSY);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   835
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   836
		/* This depot is closer */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   837
		if (result.best_path_dist < best_result.best_path_dist)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   838
			best_result = result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   839
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   840
	if (result.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   841
		DEBUG(misc, 1) ("NPF: Could not find route to any depot from 0x%x.", tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   842
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   843
	return best_result;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   844
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   845
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   846
void InitializeNPF(void)
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   847
{
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1655
diff changeset
   848
	init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   849
	_npf_aystar.loops_per_tick = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   850
	_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
   851
	//_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
   852
	/* 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
   853
	 * solution to really fix performance */
b8ecf0494fdd (svn r2204) - Add: [NPF] NPF now has a maximum number of nodes it will search. The default value is 5000 for now, which is an educated guess. Probably needs some finetuning. Hopefully this "feature" can be removed later on, when more sophisticated means of limiting the pathfinder have been implemented. This should make ships and larger networks playable for now, though.
matthijs
parents: 1678
diff changeset
   854
	_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   855
#if 0
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   856
	init_AyStar(&_train_find_station, NTPHash, 1024);
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   857
	init_AyStar(&_train_find_depot, NTPHash, 1024);
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   858
	init_AyStar(&_road_find_station, NTPHash, 1024);
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   859
	init_AyStar(&_road_find_depot, NTPHash, 1024);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   860
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   861
	_train_find_station.loops_per_tick = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   862
	_train_find_depot.loops_per_tick = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   863
	_road_find_station.loops_per_tick = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   864
	_road_find_depot.loops_per_tick = 0;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   865
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   866
	_train_find_station.max_path_cost = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   867
	_train_find_depot.max_path_cost = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   868
	_road_find_station.max_path_cost = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   869
	_road_find_depot.max_path_cost = 0;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   870
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   871
	_train_find_station.max_search_nodes = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   872
	_train_find_depot.max_search_nodes = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   873
	_road_find_station.max_search_nodes = 0;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   874
	_road_find_depot.max_search_nodes = 0;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   875
1463
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   876
	_train_find_station.CalculateG = NPFRailPathCost;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   877
	_train_find_depot.CalculateG = NPFRailPathCost;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   878
	_road_find_station.CalculateG = NPFRoadPathCost;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   879
	_road_find_depot.CalculateG = NPFRoadPathCost;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   880
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   881
	_train_find_station.CalculateH = NPFCalcStationHeuristic;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   882
	_train_find_depot.CalculateH = NPFCalcStationHeuristic;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   883
	_road_find_station.CalculateH = NPFCalcStationHeuristic;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   884
	_road_find_depot.CalculateH = NPFCalcStationHeuristic;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   885
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   886
	_train_find_station.EndNodeCheck = NPFFindStationOrTile;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   887
	_train_find_depot.EndNodeCheck = NPFFindStationOrTile;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   888
	_road_find_station.EndNodeCheck = NPFFindStationOrTile;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   889
	_road_find_depot.EndNodeCheck = NPFFindStationOrTile;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   890
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   891
	_train_find_station.FoundEndNode = NPFSaveTargetData;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   892
	_train_find_depot.FoundEndNode = NPFSaveTargetData;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   893
	_road_find_station.FoundEndNode = NPFSaveTargetData;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   894
	_road_find_depot.FoundEndNode = NPFSaveTargetData;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   895
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   896
	_train_find_station.GetNeighbours = NPFFollowTrack;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   897
	_train_find_depot.GetNeighbours = NPFFollowTrack;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   898
	_road_find_station.GetNeighbours = NPFFollowTrack;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   899
	_road_find_depot.GetNeighbours = NPFFollowTrack;
a9b4664cef34 (svn r1967) Codechange: A mix of mostly indentation-related tidyups.
pasky
parents: 1460
diff changeset
   900
#endif
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   901
}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   902
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   903
void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   904
	/* Ships don't really reach their stations, but the tile in front. So don't
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   905
	 * save the station id for ships. For roadvehs we don't store it either,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   906
	 * because multistop depends on vehicles actually reaching the exact
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   907
	 * dest_tile, not just any stop of that station.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   908
	 * So only for train orders to stations we fill fstd->station_index, for all
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   909
	 * others only dest_coords */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   910
	if ((v->current_order.type) == OT_GOTO_STATION && v->type == VEH_Train) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   911
		fstd->station_index = v->current_order.station;
1452
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   912
		/* Let's take the closest tile of the station as our target for trains */
9285e482f984 (svn r1956) -Fix: [NPF] New target tile for heuristic should perform better with larger stations (HackyKid)
matthijs
parents: 1339
diff changeset
   913
		fstd->dest_coords = CalcClosestStationTile(v->current_order.station, v->tile);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   914
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   915
		fstd->dest_coords = v->dest_tile;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   916
		fstd->station_index = -1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   917
	}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents:
diff changeset
   918
}