2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/** @file npf.cpp */
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "npf.h"
|
2006-03-16 15:16:27 +00:00
|
|
|
#include "bridge_map.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2007-12-21 22:50:51 +00:00
|
|
|
#include "tile_cmd.h"
|
|
|
|
#include "bridge.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2005-01-31 11:23:10 +00:00
|
|
|
#include "aystar.h"
|
|
|
|
#include "pathfind.h"
|
|
|
|
#include "station.h"
|
2006-03-24 08:55:08 +00:00
|
|
|
#include "station_map.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2006-03-06 20:55:24 +00:00
|
|
|
#include "tunnel_map.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network.h"
|
2006-06-05 08:34:39 +00:00
|
|
|
#include "water_map.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_base.h"
|
2008-01-07 00:57:19 +00:00
|
|
|
#include "settings_type.h"
|
2008-01-23 22:34:04 +00:00
|
|
|
#include "tunnelbridge.h"
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2005-06-25 19:08:58 +00:00
|
|
|
static AyStar _npf_aystar;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
|
|
|
|
* the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
|
|
|
|
*/
|
2005-04-11 19:14:48 +00:00
|
|
|
#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
|
2005-06-17 17:11:05 +00:00
|
|
|
static const uint _trackdir_length[TRACKDIR_END] = {
|
2005-03-08 22:21:20 +00:00
|
|
|
NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
|
|
|
|
0, 0,
|
|
|
|
NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
|
2005-01-31 11:23:10 +00:00
|
|
|
};
|
|
|
|
|
2005-09-09 23:14:38 +00:00
|
|
|
/**
|
|
|
|
* Calculates the minimum distance traveled to get from t0 to t1 when only
|
|
|
|
* using tracks (ie, only making 45 degree turns). Returns the distance in the
|
|
|
|
* NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to
|
|
|
|
* prevent rounding.
|
|
|
|
*/
|
2005-09-11 15:01:00 +00:00
|
|
|
static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
|
2005-09-09 23:14:38 +00:00
|
|
|
{
|
2007-11-26 16:01:29 +00:00
|
|
|
const uint dx = Delta(TileX(t0), TileX(t1));
|
|
|
|
const uint dy = Delta(TileY(t0), TileY(t1));
|
2005-09-09 23:14:38 +00:00
|
|
|
|
|
|
|
const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
|
|
|
|
/* OPTIMISATION:
|
|
|
|
* Original: diagTracks = max(dx, dy) - min(dx,dy);
|
|
|
|
* Proof:
|
|
|
|
* (dx+dy) - straightTracks == (min + max) - straightTracks = min + max - 2 * min = max - min */
|
|
|
|
const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
|
|
|
|
|
|
|
|
/* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
|
|
|
|
* precision */
|
|
|
|
return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
|
|
|
|
}
|
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-07-15 21:28:26 +00:00
|
|
|
#if 0
|
2005-06-25 19:08:58 +00:00
|
|
|
static uint NTPHash(uint key1, uint key2)
|
2005-01-31 11:23:10 +00:00
|
|
|
{
|
2005-04-07 19:19:16 +00:00
|
|
|
/* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
|
2005-01-31 11:23:10 +00:00
|
|
|
return PATHFIND_HASH_TILE(key1);
|
|
|
|
}
|
2005-07-15 21:28:26 +00:00
|
|
|
#endif
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2005-06-17 17:11:05 +00:00
|
|
|
/**
|
|
|
|
* Calculates a hash value for use in the NPF.
|
2006-08-28 18:53:03 +00:00
|
|
|
* @param key1 The TileIndex of the tile to hash
|
|
|
|
* @param key2 The Trackdir of the track on the tile.
|
2005-06-17 17:11:05 +00:00
|
|
|
*
|
2006-08-28 18:53:03 +00:00
|
|
|
* @todo Think of a better hash.
|
2005-06-17 17:11:05 +00:00
|
|
|
*/
|
2005-06-25 19:08:58 +00:00
|
|
|
static uint NPFHash(uint key1, uint key2)
|
2005-04-07 19:19:16 +00:00
|
|
|
{
|
|
|
|
/* TODO: think of a better hash? */
|
|
|
|
uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
|
|
|
|
uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
|
2005-06-17 17:11:05 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
assert(IsValidTrackdir((Trackdir)key2));
|
2005-06-17 17:11:05 +00:00
|
|
|
assert(IsValidTile(key1));
|
2006-02-01 06:32:03 +00:00
|
|
|
return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
|
2005-04-07 19:19:16 +00:00
|
|
|
}
|
|
|
|
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-07 23:28:27 +00:00
|
|
|
/* Calcs the tile of given station that is closest to a given tile
|
|
|
|
* for this we assume the station is a rectangle,
|
|
|
|
* as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
|
|
|
|
*/
|
2005-06-25 19:08:58 +00:00
|
|
|
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
|
|
|
|
{
|
2005-03-07 23:28:27 +00:00
|
|
|
const Station* st = GetStation(station);
|
|
|
|
|
2005-06-25 19:08:58 +00:00
|
|
|
uint minx = TileX(st->train_tile); // topmost corner of station
|
|
|
|
uint miny = TileY(st->train_tile);
|
|
|
|
uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
|
|
|
|
uint maxy = miny + st->trainst_h - 1;
|
|
|
|
uint x;
|
|
|
|
uint y;
|
2005-03-07 23:28:27 +00:00
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/* we are going the aim for the x coordinate of the closest corner
|
|
|
|
* but if we are between those coordinates, we will aim for our own x coordinate */
|
2007-11-19 18:38:10 +00:00
|
|
|
x = Clamp(TileX(tile), minx, maxx);
|
2005-03-07 23:28:27 +00:00
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/* same for y coordinate, see above comment */
|
2007-11-19 18:38:10 +00:00
|
|
|
y = Clamp(TileY(tile), miny, maxy);
|
2005-03-07 23:28:27 +00:00
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/* return the tile of our target coordinates */
|
2005-06-25 19:08:58 +00:00
|
|
|
return TileXY(x, y);
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-03-07 23:28:27 +00:00
|
|
|
|
2005-04-11 19:14:48 +00:00
|
|
|
/* Calcs the heuristic to the target station or tile. For train stations, it
|
|
|
|
* takes into account the direction of approach.
|
2005-01-31 11:23:10 +00:00
|
|
|
*/
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
|
|
|
|
NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
|
|
|
|
TileIndex from = current->tile;
|
|
|
|
TileIndex to = fstd->dest_coords;
|
2005-03-08 00:18:31 +00:00
|
|
|
uint dist;
|
2005-03-07 23:28:27 +00:00
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/* for train-stations, we are going to aim for the closest station tile */
|
2006-03-03 20:54:54 +00:00
|
|
|
if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION)
|
2005-03-08 00:18:31 +00:00
|
|
|
to = CalcClosestStationTile(fstd->station_index, from);
|
2005-03-07 23:28:27 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
|
2005-04-11 19:14:48 +00:00
|
|
|
/* Since roads only have diagonal pieces, we use manhattan distance here */
|
|
|
|
dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
|
2006-02-01 06:32:03 +00:00
|
|
|
} else {
|
2005-04-11 19:14:48 +00:00
|
|
|
/* Ships and trains can also go diagonal, so the minimum distance is shorter */
|
2005-09-09 23:14:38 +00:00
|
|
|
dist = NPFDistanceTrack(from, to);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
|
2005-07-04 14:58:55 +00:00
|
|
|
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
if (dist < ftd->best_bird_dist) {
|
2005-01-31 11:23:10 +00:00
|
|
|
ftd->best_bird_dist = dist;
|
2007-01-10 18:56:51 +00:00
|
|
|
ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
|
|
|
|
* get here, either getting it from the current choice or from the parent's
|
|
|
|
* choice */
|
2005-06-25 19:08:58 +00:00
|
|
|
static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent)
|
2005-01-31 11:23:10 +00:00
|
|
|
{
|
|
|
|
if (parent->path.parent == NULL) {
|
2005-06-17 17:11:05 +00:00
|
|
|
Trackdir trackdir = (Trackdir)current->direction;
|
2005-01-31 11:23:10 +00:00
|
|
|
/* This is a first order decision, so we'd better save the
|
|
|
|
* direction we chose */
|
|
|
|
current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2006-02-01 06:32:03 +00:00
|
|
|
/* We've already made the decision, so just save our parent's decision */
|
2005-01-31 11:23:10 +00:00
|
|
|
current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Will return the cost of the tunnel. If it is an entry, it will return the
|
|
|
|
* cost of that tile. If the tile is an exit, it will return the tunnel length
|
|
|
|
* including the exit tile. Requires that this is a Tunnel tile */
|
2005-06-25 19:08:58 +00:00
|
|
|
static uint NPFTunnelCost(AyStarNode* current)
|
|
|
|
{
|
2005-06-17 17:11:05 +00:00
|
|
|
DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction);
|
2005-01-31 11:23:10 +00:00
|
|
|
TileIndex tile = current->tile;
|
2007-12-16 15:38:51 +00:00
|
|
|
if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* We just popped out if this tunnel, since were
|
|
|
|
* facing the tunnel exit */
|
2008-01-23 22:34:04 +00:00
|
|
|
return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
|
2007-03-21 03:06:21 +00:00
|
|
|
/* @todo: Penalty for tunnels? */
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
|
|
|
/* We are entering the tunnel, the enter tile is just a
|
|
|
|
* straight track */
|
|
|
|
return NPF_TILE_LENGTH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
static inline uint NPFBridgeCost(AyStarNode *current)
|
|
|
|
{
|
2008-01-23 22:34:04 +00:00
|
|
|
return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
|
2006-12-27 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2005-06-25 19:08:58 +00:00
|
|
|
static uint NPFSlopeCost(AyStarNode* current)
|
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir((Trackdir)current->direction));
|
2007-10-20 16:50:48 +00:00
|
|
|
|
|
|
|
/* Get center of tiles */
|
|
|
|
int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
|
|
|
|
int dx4 = (x2 - x1) / 4;
|
|
|
|
int dy4 = (y2 - y1) / 4;
|
|
|
|
|
|
|
|
/* Get the height on both sides of the tile edge.
|
|
|
|
* Avoid testing the height on the tile-center. This will fail for halftile-foundations.
|
|
|
|
*/
|
|
|
|
int z1 = GetSlopeZ(x1 + dx4, y1 + dy4);
|
|
|
|
int z2 = GetSlopeZ(x2 - dx4, y2 - dy4);
|
2005-03-14 16:56:05 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (z2 - z1 > 1) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Slope up */
|
|
|
|
return _patches.npf_rail_slope_penalty;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
/* Should we give a bonus for slope down? Probably not, we
|
|
|
|
* could just substract that bonus from the penalty, because
|
|
|
|
* there is only one level of steepness... */
|
|
|
|
}
|
|
|
|
|
2006-04-12 15:08:27 +00:00
|
|
|
/**
|
|
|
|
* Mark tiles by mowing the grass when npf debug level >= 1.
|
|
|
|
* Will not work for multiplayer games, since it can (will) cause desyncs.
|
|
|
|
*/
|
2005-06-25 19:08:58 +00:00
|
|
|
static void NPFMarkTile(TileIndex tile)
|
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
#ifndef NO_DEBUG_MESSAGES
|
2006-04-12 15:08:27 +00:00
|
|
|
if (_debug_npf_level < 1 || _networking) return;
|
2006-02-13 21:15:00 +00:00
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_RAILWAY:
|
|
|
|
/* DEBUG: mark visited tiles by mowing the grass under them ;-) */
|
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) {
|
2006-04-12 14:17:07 +00:00
|
|
|
SetRailGroundType(tile, RAIL_GROUND_BARREN);
|
2006-02-13 21:15:00 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD:
|
2006-02-13 21:15:00 +00:00
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_ROAD)) {
|
2006-06-19 20:13:50 +00:00
|
|
|
SetRoadside(tile, ROADSIDE_BARREN);
|
2006-02-13 21:15:00 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-04-11 19:53:44 +00:00
|
|
|
#endif
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
|
|
|
|
{
|
2007-03-21 03:06:21 +00:00
|
|
|
/* TileIndex tile = current->tile; */
|
2005-01-31 11:23:10 +00:00
|
|
|
int32 cost = 0;
|
2005-06-17 17:11:05 +00:00
|
|
|
Trackdir trackdir = (Trackdir)current->direction;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2005-06-17 17:11:05 +00:00
|
|
|
if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
|
2007-03-21 03:06:21 +00:00
|
|
|
cost += _patches.npf_buoy_penalty; // A small penalty for going over buoys
|
2005-05-02 22:13:20 +00:00
|
|
|
|
2005-06-17 17:11:05 +00:00
|
|
|
if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
|
2005-05-02 22:13:20 +00:00
|
|
|
cost += _patches.npf_water_curve_penalty;
|
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
/* @todo More penalties? */
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the cost of this node, for road tracks */
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
TileIndex tile = current->tile;
|
|
|
|
int32 cost = 0;
|
2005-05-07 22:00:36 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Determine base length */
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_TUNNELBRIDGE:
|
2006-12-27 12:38:02 +00:00
|
|
|
cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
|
2005-11-05 18:32:21 +00:00
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD:
|
2005-01-31 11:23:10 +00:00
|
|
|
cost = NPF_TILE_LENGTH;
|
2005-07-03 13:02:54 +00:00
|
|
|
/* Increase the cost for level crossings */
|
2006-06-10 08:37:41 +00:00
|
|
|
if (IsLevelCrossing(tile)) cost += _patches.npf_crossing_penalty;
|
2005-01-31 11:23:10 +00:00
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
case MP_STATION:
|
|
|
|
cost = NPF_TILE_LENGTH;
|
|
|
|
/* Increase the cost for drive-through road stops */
|
|
|
|
if (IsDriveThroughStopTile(tile)) cost += _patches.npf_road_drive_through_penalty;
|
|
|
|
break;
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine extra costs */
|
|
|
|
|
|
|
|
/* Check for slope */
|
|
|
|
cost += NPFSlopeCost(current);
|
|
|
|
|
2005-06-16 17:53:40 +00:00
|
|
|
/* Check for turns. Road vehicles only really drive diagonal, turns are
|
|
|
|
* represented by non-diagonal tracks */
|
2007-01-10 18:56:51 +00:00
|
|
|
if (!IsDiagonalTrackdir((Trackdir)current->direction))
|
2005-06-16 17:53:40 +00:00
|
|
|
cost += _patches.npf_road_curve_penalty;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
NPFMarkTile(tile);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
|
2005-01-31 11:23:10 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Determine the cost of this node, for railway tracks */
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
TileIndex tile = current->tile;
|
2005-06-17 17:11:05 +00:00
|
|
|
Trackdir trackdir = (Trackdir)current->direction;
|
2005-01-31 11:23:10 +00:00
|
|
|
int32 cost = 0;
|
2006-02-13 21:15:00 +00:00
|
|
|
/* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
|
2005-04-02 10:38:31 +00:00
|
|
|
OpenListNode new_node;
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Determine base length */
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_TUNNELBRIDGE:
|
2006-12-27 12:38:02 +00:00
|
|
|
cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
|
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
case MP_RAILWAY:
|
|
|
|
cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
|
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD: /* Railway crossing */
|
2005-01-31 11:23:10 +00:00
|
|
|
cost = NPF_TILE_LENGTH;
|
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-03-14 16:56:05 +00:00
|
|
|
case MP_STATION:
|
2006-02-01 06:32:03 +00:00
|
|
|
/* We give a station tile a penalty. Logically we would only want to give
|
|
|
|
* station tiles that are not our destination this penalty. This would
|
|
|
|
* discourage trains to drive through busy stations. But, we can just
|
|
|
|
* give any station tile a penalty, because every possible route will get
|
|
|
|
* this penalty exactly once, on its end tile (if it's a station) and it
|
|
|
|
* will therefore not make a difference. */
|
2005-03-14 16:56:05 +00:00
|
|
|
cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty;
|
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine extra costs */
|
|
|
|
|
2005-03-08 19:54:10 +00:00
|
|
|
/* Check for signals */
|
2005-06-17 00:22:46 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir)) {
|
2005-03-08 19:54:10 +00:00
|
|
|
/* Ordinary track with signals */
|
2006-04-12 08:28:03 +00:00
|
|
|
if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Signal facing us is red */
|
2005-03-08 19:54:10 +00:00
|
|
|
if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Penalize the first signal we
|
|
|
|
* encounter, if it is red */
|
2005-04-04 17:53:39 +00:00
|
|
|
|
|
|
|
/* Is this a presignal exit or combo? */
|
2007-05-31 21:21:04 +00:00
|
|
|
SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
|
2006-02-01 06:32:03 +00:00
|
|
|
if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
|
2005-04-04 17:53:39 +00:00
|
|
|
/* Penalise exit and combo signals differently (heavier) */
|
|
|
|
cost += _patches.npf_rail_firstred_exit_penalty;
|
2006-02-01 06:32:03 +00:00
|
|
|
} else {
|
2005-04-04 17:53:39 +00:00
|
|
|
cost += _patches.npf_rail_firstred_penalty;
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2005-03-08 19:54:10 +00:00
|
|
|
/* Record the state of this signal */
|
|
|
|
NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
|
|
|
|
} else {
|
|
|
|
/* Record the state of this signal */
|
|
|
|
NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2005-03-08 19:54:10 +00:00
|
|
|
NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 19:54:10 +00:00
|
|
|
/* Penalise the tile if it is a target tile and the last signal was
|
|
|
|
* red */
|
2005-06-17 17:11:05 +00:00
|
|
|
/* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
|
|
|
|
* of course... */
|
2005-04-02 10:38:31 +00:00
|
|
|
new_node.path.node = *current;
|
2005-06-17 17:11:05 +00:00
|
|
|
if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
|
2005-03-08 19:54:10 +00:00
|
|
|
cost += _patches.npf_rail_lastred_penalty;
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Check for slope */
|
|
|
|
cost += NPFSlopeCost(current);
|
|
|
|
|
|
|
|
/* Check for turns */
|
2005-06-17 17:11:05 +00:00
|
|
|
if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
|
2005-03-08 19:59:56 +00:00
|
|
|
cost += _patches.npf_rail_curve_penalty;
|
2007-03-21 03:06:21 +00:00
|
|
|
/*TODO, with realistic acceleration, also the amount of straight track between
|
|
|
|
* curves should be taken into account, as this affects the speed limit. */
|
2005-01-31 11:23:10 +00:00
|
|
|
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
/* Check for reverse in depot */
|
|
|
|
if (IsTileDepotType(tile, TRANSPORT_RAIL) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
|
2005-05-07 22:00:36 +00:00
|
|
|
/* Penalise any depot tile that is not the last tile in the path. This
|
|
|
|
* _should_ penalise every occurence of reversing in a depot (and only
|
|
|
|
* that) */
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
cost += _patches.npf_rail_depot_reverse_penalty;
|
2005-07-04 14:58:55 +00:00
|
|
|
}
|
2005-05-07 22:00:36 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Check for occupied track */
|
|
|
|
//TODO
|
|
|
|
|
|
|
|
NPFMarkTile(tile);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
|
2005-01-31 11:23:10 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Will find any depot */
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFFindDepot(AyStar* as, OpenListNode *current)
|
|
|
|
{
|
2005-05-07 22:00:36 +00:00
|
|
|
/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
|
|
|
|
* since checking the cache not that much faster than the actual check */
|
2007-01-10 18:56:51 +00:00
|
|
|
return IsTileDepotType(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
|
2006-06-10 08:37:41 +00:00
|
|
|
AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Will find a station identified using the NPFFindStationOrTileData */
|
2005-06-25 19:08:58 +00:00
|
|
|
static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current)
|
|
|
|
{
|
2005-03-08 22:28:59 +00:00
|
|
|
NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
|
2005-04-02 10:38:31 +00:00
|
|
|
AyStarNode *node = ¤t->path.node;
|
2005-03-08 22:28:59 +00:00
|
|
|
TileIndex tile = node->tile;
|
2005-03-08 19:54:10 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* If GetNeighbours said we could get here, we assume the station type
|
|
|
|
* is correct */
|
2005-03-08 19:54:10 +00:00
|
|
|
if (
|
2006-03-03 20:54:54 +00:00
|
|
|
(fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */
|
2006-03-24 08:55:08 +00:00
|
|
|
(IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */
|
2005-03-08 19:54:10 +00:00
|
|
|
) {
|
|
|
|
return AYSTAR_FOUND_END_NODE;
|
|
|
|
} else {
|
2005-01-31 11:23:10 +00:00
|
|
|
return AYSTAR_DONE;
|
2005-03-08 19:54:10 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* To be called when current contains the (shortest route to) the target node.
|
|
|
|
* Will fill the contents of the NPFFoundTargetData using
|
|
|
|
* AyStarNode[NPF_TRACKDIR_CHOICE].
|
|
|
|
*/
|
2005-06-25 19:08:58 +00:00
|
|
|
static void NPFSaveTargetData(AyStar* as, OpenListNode* current)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
|
2005-06-17 17:11:05 +00:00
|
|
|
ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
|
2005-01-31 11:23:10 +00:00
|
|
|
ftd->best_path_dist = current->g;
|
|
|
|
ftd->best_bird_dist = 0;
|
|
|
|
ftd->node = current->path.node;
|
|
|
|
}
|
|
|
|
|
2005-06-22 22:38:18 +00:00
|
|
|
/**
|
|
|
|
* Finds out if a given player's vehicles are allowed to enter a given tile.
|
|
|
|
* @param owner The owner of the vehicle.
|
|
|
|
* @param tile The tile that is about to be entered.
|
2008-02-04 16:24:25 +00:00
|
|
|
* @param enterdir The direction in which the vehicle wants to enter the tile.
|
2005-06-22 22:38:18 +00:00
|
|
|
* @return true if the vehicle can enter the tile.
|
|
|
|
* @todo This function should be used in other places than just NPF,
|
|
|
|
* maybe moved to another file too.
|
|
|
|
*/
|
2008-02-04 16:24:25 +00:00
|
|
|
static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
|
2005-06-22 22:38:18 +00:00
|
|
|
{
|
2006-02-01 06:32:03 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) || /* Rail tile (also rail depot) */
|
2006-04-04 11:35:52 +00:00
|
|
|
IsRailwayStationTile(tile) || /* Rail station tile */
|
2006-02-01 06:32:03 +00:00
|
|
|
IsTileDepotType(tile, TRANSPORT_ROAD) || /* Road depot tile */
|
2008-02-04 16:24:25 +00:00
|
|
|
IsStandardRoadStopTile(tile)) { /* Road station tile (but not drive-through stops) */
|
2005-06-22 22:38:18 +00:00
|
|
|
return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2005-06-22 22:38:18 +00:00
|
|
|
|
|
|
|
switch (GetTileType(tile)) {
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD:
|
2005-06-22 22:38:18 +00:00
|
|
|
/* rail-road crossing : are we looking at the railway part? */
|
2006-04-10 20:46:37 +00:00
|
|
|
if (IsLevelCrossing(tile) &&
|
|
|
|
DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
|
2005-06-22 22:38:18 +00:00
|
|
|
return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */
|
2006-04-10 20:46:37 +00:00
|
|
|
}
|
2005-06-22 22:38:18 +00:00
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-06-22 22:38:18 +00:00
|
|
|
case MP_TUNNELBRIDGE:
|
2007-12-16 19:30:42 +00:00
|
|
|
if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
|
2005-06-22 22:38:18 +00:00
|
|
|
return IsTileOwner(tile, owner);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2005-06-22 22:38:18 +00:00
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2005-06-22 22:38:18 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-03-21 03:06:21 +00:00
|
|
|
return true; // no need to check
|
2005-06-22 22:38:18 +00:00
|
|
|
}
|
|
|
|
|
2006-06-05 08:34:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the direction the exit of the depot on the given tile is facing.
|
|
|
|
*/
|
|
|
|
static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
|
|
|
|
{
|
|
|
|
assert(IsTileDepotType(tile, type));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TRANSPORT_RAIL: return GetRailDepotDirection(tile);
|
|
|
|
case TRANSPORT_ROAD: return GetRoadDepotDirection(tile);
|
|
|
|
case TRANSPORT_WATER: return GetShipDepotDirection(tile);
|
|
|
|
default: return INVALID_DIAGDIR; /* Not reached */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
/** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
|
|
|
|
static DiagDirection GetSingleTramBit(TileIndex tile)
|
|
|
|
{
|
|
|
|
if (IsTileType(tile, MP_ROAD) && GetRoadTileType(tile) == ROAD_TILE_NORMAL) {
|
|
|
|
RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
|
|
|
|
switch (rb) {
|
|
|
|
case ROAD_NW: return DIAGDIR_NW;
|
|
|
|
case ROAD_SW: return DIAGDIR_SW;
|
|
|
|
case ROAD_SE: return DIAGDIR_SE;
|
|
|
|
case ROAD_NE: return DIAGDIR_NE;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INVALID_DIAGDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a tile can be entered or left only from one side.
|
|
|
|
*
|
|
|
|
* Depots, non-drive-through roadstops, and tiles with single trambits are tested.
|
|
|
|
*
|
|
|
|
* @param tile The tile of interest.
|
|
|
|
* @param type The transporttype of the vehicle.
|
|
|
|
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
|
|
|
|
* @return The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions
|
|
|
|
*/
|
|
|
|
static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
|
|
|
|
{
|
|
|
|
if (type != TRANSPORT_WATER && IsTileDepotType(tile, type)) return GetDepotDirection(tile, type);
|
|
|
|
|
|
|
|
if (type == TRANSPORT_ROAD) {
|
|
|
|
if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
|
|
|
|
if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return INVALID_DIAGDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a vehicle must reverse on a tile.
|
|
|
|
*
|
|
|
|
* @param tile The tile of interest.
|
|
|
|
* @param dir The direction in which the vehicle drives on a tile.
|
|
|
|
* @param type The transporttype of the vehicle.
|
|
|
|
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
|
|
|
|
* @return true iff the vehicle must reverse on the tile.
|
|
|
|
*/
|
|
|
|
static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
|
|
|
|
{
|
|
|
|
DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
|
|
|
|
return single_entry != INVALID_DIAGDIR && single_entry != dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a vehicle can enter a tile.
|
|
|
|
*
|
|
|
|
* @param tile The tile of interest.
|
|
|
|
* @param dir The direction in which the vehicle drives onto a tile.
|
|
|
|
* @param type The transporttype of the vehicle.
|
|
|
|
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
|
|
|
|
* @param railtypes For TRANSPORT_RAIL the compatible RailTypes of the vehicle.
|
|
|
|
* @param owner The owner of the vehicle.
|
|
|
|
* @return true iff the vehicle can enter the tile.
|
|
|
|
*/
|
|
|
|
static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
|
|
|
|
{
|
|
|
|
/* Check tunnel entries and bridge ramps */
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
|
|
|
|
|
|
|
|
/* Test ownership */
|
|
|
|
if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false;
|
|
|
|
|
|
|
|
/* check correct rail type (mono, maglev, etc) */
|
|
|
|
if (type == TRANSPORT_RAIL) {
|
|
|
|
RailType rail_type = GetTileRailType(tile);
|
|
|
|
if (!HasBit(railtypes, rail_type)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Depots, standard roadstops and single tram bits can only be entered from one direction */
|
|
|
|
DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
|
|
|
|
if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the driveable Trackdirs on a tile.
|
|
|
|
*
|
|
|
|
* One-way-roads are taken into account. Signals are not tested.
|
|
|
|
*
|
|
|
|
* @param dst_tile The tile of interest.
|
|
|
|
* @param src_trackdir The direction the vehicle is currently moving.
|
|
|
|
* @param type The transporttype of the vehicle.
|
|
|
|
* @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
|
|
|
|
* @return The Trackdirs the vehicle can continue moving on.
|
|
|
|
*/
|
|
|
|
static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
|
|
|
|
{
|
|
|
|
uint32 ts = GetTileTrackStatus(dst_tile, type, subtype);
|
|
|
|
TrackdirBits trackdirbits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
|
|
|
|
|
|
|
|
if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
|
|
|
|
/* GetTileTrackStatus() returns 0 for single tram bits.
|
|
|
|
* As we cannot change it there (easily) without breaking something, change it here */
|
|
|
|
switch (GetSingleTramBit(dst_tile)) {
|
|
|
|
case DIAGDIR_NE:
|
|
|
|
case DIAGDIR_SW:
|
|
|
|
trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIAGDIR_NW:
|
|
|
|
case DIAGDIR_SE:
|
|
|
|
trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
|
|
|
|
|
|
|
|
/* Select only trackdirs we can reach from our current trackdir */
|
|
|
|
trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
|
|
|
|
|
|
|
|
/* Filter out trackdirs that would make 90 deg turns for trains */
|
|
|
|
if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
|
|
|
|
|
|
|
|
DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
|
|
|
|
|
|
|
|
return trackdirbits;
|
|
|
|
}
|
|
|
|
|
2006-06-05 08:34:39 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Will just follow the results of GetTileTrackStatus concerning where we can
|
|
|
|
* go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
|
|
|
|
* an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
|
2005-03-08 19:54:10 +00:00
|
|
|
* entry and exit are neighbours. Will fill
|
|
|
|
* AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
|
|
|
|
* copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
|
2005-06-25 19:08:58 +00:00
|
|
|
static void NPFFollowTrack(AyStar* aystar, OpenListNode* current)
|
|
|
|
{
|
2008-02-04 16:24:25 +00:00
|
|
|
/* We leave src_tile on track src_trackdir in direction src_exitdir */
|
2005-06-17 17:11:05 +00:00
|
|
|
Trackdir src_trackdir = (Trackdir)current->path.node.direction;
|
2005-01-31 11:23:10 +00:00
|
|
|
TileIndex src_tile = current->path.node.tile;
|
2005-06-17 00:22:46 +00:00
|
|
|
DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
|
2008-02-04 16:24:25 +00:00
|
|
|
|
|
|
|
/* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
|
2007-01-10 18:56:51 +00:00
|
|
|
TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
|
2007-05-24 22:41:50 +00:00
|
|
|
uint subtype = aystar->user_data[NPF_SUB_TYPE];
|
2008-02-04 16:24:25 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
|
|
|
|
aystar->num_neighbours = 0;
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
/* We want to determine the tile we arrive, and which choices we have there */
|
|
|
|
TileIndex dst_tile;
|
|
|
|
TrackdirBits trackdirbits;
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Find dest tile */
|
2007-12-16 19:30:42 +00:00
|
|
|
if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
|
2008-02-04 16:24:25 +00:00
|
|
|
/* We drive through the wormhole and arrive on the other side */
|
2008-01-04 19:45:29 +00:00
|
|
|
dst_tile = GetOtherTunnelBridgeEnd(src_tile);
|
2008-02-04 16:24:25 +00:00
|
|
|
trackdirbits = TrackdirToTrackdirBits(src_trackdir);
|
|
|
|
} else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
|
|
|
|
/* We can only reverse on this tile */
|
|
|
|
dst_tile = src_tile;
|
|
|
|
src_trackdir = ReverseTrackdir(src_trackdir);
|
|
|
|
trackdirbits = TrackdirToTrackdirBits(src_trackdir);
|
|
|
|
} else {
|
|
|
|
/* We leave src_tile in src_exitdir and reach dst_tile */
|
|
|
|
dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
if (dst_tile != INVALID_TILE && !CanEnterTile(dst_tile, src_exitdir, type, subtype, (RailTypes)aystar->user_data[NPF_RAILTYPES], (Owner)aystar->user_data[NPF_OWNER])) dst_tile = INVALID_TILE;
|
|
|
|
|
|
|
|
if (dst_tile == INVALID_TILE) {
|
|
|
|
/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
|
|
|
|
if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
|
|
|
|
|
|
|
|
dst_tile = src_tile;
|
|
|
|
src_trackdir = ReverseTrackdir(src_trackdir);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
|
2005-06-22 01:06:04 +00:00
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
if (trackdirbits == 0) {
|
|
|
|
/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
|
|
|
|
if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
|
2005-02-06 22:36:08 +00:00
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
dst_tile = src_tile;
|
|
|
|
src_trackdir = ReverseTrackdir(src_trackdir);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2008-02-04 16:24:25 +00:00
|
|
|
trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enumerate possible track */
|
2008-02-04 16:24:25 +00:00
|
|
|
uint i = 0;
|
2005-06-17 00:22:46 +00:00
|
|
|
while (trackdirbits != 0) {
|
2007-01-11 10:34:42 +00:00
|
|
|
Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Check for oneway signal against us */
|
2006-05-09 08:17:33 +00:00
|
|
|
if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
|
2005-06-17 00:22:46 +00:00
|
|
|
if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir))
|
2007-03-21 03:06:21 +00:00
|
|
|
/* if one way signal not pointing towards us, stop going in this direction. */
|
2005-06-17 00:22:46 +00:00
|
|
|
break;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
/* We've found ourselves a neighbour :-) */
|
|
|
|
AyStarNode* neighbour = &aystar->neighbours[i];
|
|
|
|
neighbour->tile = dst_tile;
|
|
|
|
neighbour->direction = dst_trackdir;
|
|
|
|
/* Save user data */
|
|
|
|
neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
|
|
|
|
NPFFillTrackdirChoice(neighbour, current);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
aystar->num_neighbours = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Plan a route to the specified target (which is checked by target_proc),
|
|
|
|
* from start1 and if not NULL, from start2 as well. The type of transport we
|
2005-05-07 22:00:36 +00:00
|
|
|
* are checking is in type. reverse_penalty is applied to all routes that
|
|
|
|
* originate from the second start node.
|
2005-01-31 11:23:10 +00:00
|
|
|
* When we are looking for one specific target (optionally multiple tiles), we
|
|
|
|
* should use a good heuristic to perform aystar search. When we search for
|
|
|
|
* multiple targets that are spread around, we should perform a breadth first
|
|
|
|
* search by specifiying CalcZero as our heuristic.
|
|
|
|
*/
|
2008-01-09 21:05:03 +00:00
|
|
|
static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
|
2005-06-25 19:08:58 +00:00
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
int r;
|
|
|
|
NPFFoundTargetData result;
|
|
|
|
|
|
|
|
/* Initialize procs */
|
|
|
|
_npf_aystar.CalculateH = heuristic_proc;
|
|
|
|
_npf_aystar.EndNodeCheck = target_proc;
|
|
|
|
_npf_aystar.FoundEndNode = NPFSaveTargetData;
|
|
|
|
_npf_aystar.GetNeighbours = NPFFollowTrack;
|
2006-06-10 08:37:41 +00:00
|
|
|
switch (type) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break;
|
|
|
|
case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break;
|
|
|
|
case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
|
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Initialize Start Node(s) */
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
start1->user_data[NPF_NODE_FLAGS] = 0;
|
2005-05-07 22:00:36 +00:00
|
|
|
_npf_aystar.addstart(&_npf_aystar, start1, 0);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (start2) {
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-03-08 19:54:10 +00:00
|
|
|
start2->user_data[NPF_NODE_FLAGS] = 0;
|
|
|
|
NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
|
2005-05-07 22:00:36 +00:00
|
|
|
_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize result */
|
|
|
|
result.best_bird_dist = (uint)-1;
|
|
|
|
result.best_path_dist = (uint)-1;
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
result.best_trackdir = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
_npf_aystar.user_path = &result;
|
|
|
|
|
|
|
|
/* Initialize target */
|
|
|
|
_npf_aystar.user_target = target;
|
|
|
|
|
|
|
|
/* Initialize user_data */
|
|
|
|
_npf_aystar.user_data[NPF_TYPE] = type;
|
2007-05-24 22:41:50 +00:00
|
|
|
_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
|
2005-02-06 22:36:08 +00:00
|
|
|
_npf_aystar.user_data[NPF_OWNER] = owner;
|
2006-03-29 16:30:26 +00:00
|
|
|
_npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* GO! */
|
|
|
|
r = AyStarMain_Main(&_npf_aystar);
|
|
|
|
assert(r != AYSTAR_STILL_BUSY);
|
|
|
|
|
|
|
|
if (result.best_bird_dist != 0) {
|
2006-02-21 01:15:59 +00:00
|
|
|
if (target != NULL) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
|
|
|
/* Assumption: target == NULL, so we are looking for a depot */
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-01-09 21:05:03 +00:00
|
|
|
NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
|
2005-06-25 19:08:58 +00:00
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
AyStarNode start1;
|
|
|
|
AyStarNode start2;
|
|
|
|
|
|
|
|
start1.tile = tile1;
|
|
|
|
start2.tile = tile2;
|
2005-05-07 22:00:36 +00:00
|
|
|
/* We set this in case the target is also the start tile, we will just
|
|
|
|
* return a not found then */
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
start1.direction = trackdir1;
|
|
|
|
start2.direction = trackdir2;
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2007-05-24 22:41:50 +00:00
|
|
|
return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 21:05:03 +00:00
|
|
|
NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
|
2005-06-25 19:08:58 +00:00
|
|
|
{
|
2007-05-24 22:41:50 +00:00
|
|
|
return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, target, type, sub_type, owner, railtypes);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 21:05:03 +00:00
|
|
|
NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
|
2005-06-25 19:08:58 +00:00
|
|
|
{
|
2005-05-07 22:00:36 +00:00
|
|
|
AyStarNode start1;
|
|
|
|
AyStarNode start2;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2005-05-07 22:00:36 +00:00
|
|
|
start1.tile = tile1;
|
|
|
|
start2.tile = tile2;
|
2005-01-31 11:23:10 +00:00
|
|
|
/* We set this in case the target is also the start tile, we will just
|
|
|
|
* return a not found then */
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-05-07 22:00:36 +00:00
|
|
|
start1.direction = trackdir1;
|
|
|
|
start2.direction = trackdir2;
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* perform a breadth first search. Target is NULL,
|
|
|
|
* since we are just looking for any depot...*/
|
2007-05-24 22:41:50 +00:00
|
|
|
return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
|
2005-05-07 22:00:36 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 21:05:03 +00:00
|
|
|
NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
|
2005-06-25 19:08:58 +00:00
|
|
|
{
|
2007-05-24 22:41:50 +00:00
|
|
|
return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, type, sub_type, owner, railtypes, 0);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 21:05:03 +00:00
|
|
|
NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
|
2005-06-25 19:08:58 +00:00
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Okay, what we're gonna do. First, we look at all depots, calculate
|
|
|
|
* the manhatten distance to get to each depot. We then sort them by
|
|
|
|
* distance. We start by trying to plan a route to the closest, then
|
|
|
|
* the next closest, etc. We stop when the best route we have found so
|
|
|
|
* far, is shorter than the manhattan distance. This will obviously
|
|
|
|
* always find the closest depot. It will probably be most efficient
|
|
|
|
* for ships, since the heuristic will not be to far off then. I hope.
|
|
|
|
*/
|
|
|
|
Queue depots;
|
|
|
|
int r;
|
2006-08-27 22:08:40 +00:00
|
|
|
NPFFoundTargetData best_result = {(uint)-1, (uint)-1, INVALID_TRACKDIR, {INVALID_TILE, 0, {0, 0}}};
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFoundTargetData result;
|
|
|
|
NPFFindStationOrTileData target;
|
|
|
|
AyStarNode start;
|
|
|
|
Depot* current;
|
2005-02-06 10:18:47 +00:00
|
|
|
Depot *depot;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
init_InsSort(&depots);
|
|
|
|
/* Okay, let's find all depots that we can use first */
|
2005-02-06 10:18:47 +00:00
|
|
|
FOR_ALL_DEPOTS(depot) {
|
2005-02-06 22:36:08 +00:00
|
|
|
/* Check if this is really a valid depot, it is of the needed type and
|
|
|
|
* owner */
|
2006-08-22 15:33:35 +00:00
|
|
|
if (IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner))
|
2005-02-06 22:36:08 +00:00
|
|
|
/* If so, let's add it to the queue, sorted by distance */
|
2005-02-06 10:18:47 +00:00
|
|
|
depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, let's initialise the aystar */
|
|
|
|
|
|
|
|
/* Initialize procs */
|
|
|
|
_npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
|
|
|
|
_npf_aystar.EndNodeCheck = NPFFindStationOrTile;
|
|
|
|
_npf_aystar.FoundEndNode = NPFSaveTargetData;
|
|
|
|
_npf_aystar.GetNeighbours = NPFFollowTrack;
|
2006-06-27 21:25:53 +00:00
|
|
|
switch (type) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break;
|
|
|
|
case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break;
|
|
|
|
case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
|
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Initialize target */
|
2006-03-03 20:54:54 +00:00
|
|
|
target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
|
2005-01-31 11:23:10 +00:00
|
|
|
_npf_aystar.user_target = ⌖
|
|
|
|
|
|
|
|
/* Initialize user_data */
|
|
|
|
_npf_aystar.user_data[NPF_TYPE] = type;
|
2007-05-24 22:41:50 +00:00
|
|
|
_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
|
2005-02-06 22:36:08 +00:00
|
|
|
_npf_aystar.user_data[NPF_OWNER] = owner;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Initialize Start Node */
|
|
|
|
start.tile = tile;
|
|
|
|
start.direction = trackdir; /* We will initialize user_data inside the loop below */
|
|
|
|
|
|
|
|
/* Initialize Result */
|
|
|
|
_npf_aystar.user_path = &result;
|
|
|
|
best_result.best_path_dist = (uint)-1;
|
2005-02-06 22:36:08 +00:00
|
|
|
best_result.best_bird_dist = (uint)-1;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Just iterate the depots in order of increasing distance */
|
2007-01-10 18:56:51 +00:00
|
|
|
while ((current = (Depot*)depots.pop(&depots))) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Check to see if we already have a path shorter than this
|
2005-02-06 22:36:08 +00:00
|
|
|
* depot's manhattan distance. HACK: We call DistanceManhattan
|
2005-01-31 11:23:10 +00:00
|
|
|
* again, we should probably modify the queue to give us that
|
|
|
|
* value... */
|
|
|
|
if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Initialize Start Node */
|
|
|
|
/* We set this in case the target is also the start tile, we will just
|
|
|
|
* return a not found then */
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
start.user_data[NPF_NODE_FLAGS] = 0;
|
2005-05-07 22:00:36 +00:00
|
|
|
_npf_aystar.addstart(&_npf_aystar, &start, 0);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Initialize result */
|
|
|
|
result.best_bird_dist = (uint)-1;
|
|
|
|
result.best_path_dist = (uint)-1;
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
result.best_trackdir = INVALID_TRACKDIR;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
/* Initialize target */
|
|
|
|
target.dest_coords = current->xy;
|
|
|
|
|
|
|
|
/* GO! */
|
|
|
|
r = AyStarMain_Main(&_npf_aystar);
|
|
|
|
assert(r != AYSTAR_STILL_BUSY);
|
|
|
|
|
|
|
|
/* This depot is closer */
|
|
|
|
if (result.best_path_dist < best_result.best_path_dist)
|
|
|
|
best_result = result;
|
|
|
|
}
|
|
|
|
if (result.best_bird_dist != 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
return best_result;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeNPF()
|
2005-01-31 11:23:10 +00:00
|
|
|
{
|
2007-06-27 17:05:06 +00:00
|
|
|
static bool first_init = true;
|
|
|
|
if (first_init) {
|
|
|
|
first_init = false;
|
|
|
|
init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
|
|
|
|
} else {
|
|
|
|
AyStarMain_Clear(&_npf_aystar);
|
|
|
|
}
|
2005-03-08 22:21:20 +00:00
|
|
|
_npf_aystar.loops_per_tick = 0;
|
|
|
|
_npf_aystar.max_path_cost = 0;
|
2005-04-15 15:28:01 +00:00
|
|
|
//_npf_aystar.max_search_nodes = 0;
|
|
|
|
/* We will limit the number of nodes for now, until we have a better
|
|
|
|
* solution to really fix performance */
|
|
|
|
_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2005-06-25 19:08:58 +00:00
|
|
|
void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v)
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Ships don't really reach their stations, but the tile in front. So don't
|
|
|
|
* save the station id for ships. For roadvehs we don't store it either,
|
|
|
|
* because multistop depends on vehicles actually reaching the exact
|
|
|
|
* dest_tile, not just any stop of that station.
|
|
|
|
* So only for train orders to stations we fill fstd->station_index, for all
|
|
|
|
* others only dest_coords */
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION && v->type == VEH_TRAIN) {
|
2006-09-03 08:25:27 +00:00
|
|
|
fstd->station_index = v->current_order.dest;
|
2005-03-07 23:28:27 +00:00
|
|
|
/* Let's take the closest tile of the station as our target for trains */
|
2006-09-03 08:25:27 +00:00
|
|
|
fstd->dest_coords = CalcClosestStationTile(v->current_order.dest, v->tile);
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
|
|
|
fstd->dest_coords = v->dest_tile;
|
2006-03-03 20:54:54 +00:00
|
|
|
fstd->station_index = INVALID_STATION;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
}
|