src/yapf/yapf_road.cpp
author rubidium
Sat, 05 Apr 2008 23:36:54 +0000
changeset 8836 f6f1ea3d7e93
parent 8616 fd862a55c47f
child 8954 3993bae3bfb8
permissions -rw-r--r--
(svn r12584) -Codechange: do not access the order type directly.
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     2
6121
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 6012
diff changeset
     3
/** @file yapf_road.cpp */
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 6012
diff changeset
     4
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     5
#include "../stdafx.h"
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     7
#include "yapf.hpp"
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     8
#include "yapf_node_road.hpp"
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     9
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    10
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    11
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    12
class CYapfCostRoadT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    13
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    14
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    15
	typedef typename Types::Tpf Tpf; ///< pathfinder (derived from THIS class)
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    16
	typedef typename Types::TrackFollower TrackFollower; ///< track follower helper
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    17
	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
	typedef typename Node::Key Key;    ///< key to hash tables
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    20
protected:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    21
	/// to access inherited path finder
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    22
	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    23
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    24
	int SlopeCost(TileIndex tile, TileIndex next_tile, Trackdir trackdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    25
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
		// height of the center of the current tile
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    27
		int x1 = TileX(tile) * TILE_SIZE;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    28
		int y1 = TileY(tile) * TILE_SIZE;
3993
1ac50ad7e554 (svn r5196) - Codechange: replace two incorrect uses of TILE_HEIGHT with TILE_SIZE / 2
peter1138
parents: 3981
diff changeset
    29
		int z1 = GetSlopeZ(x1 + TILE_SIZE / 2, y1 + TILE_SIZE / 2);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    30
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    31
		// height of the center of the next tile
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
		int x2 = TileX(next_tile) * TILE_SIZE;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
		int y2 = TileY(next_tile) * TILE_SIZE;
3993
1ac50ad7e554 (svn r5196) - Codechange: replace two incorrect uses of TILE_HEIGHT with TILE_SIZE / 2
peter1138
parents: 3981
diff changeset
    34
		int z2 = GetSlopeZ(x2 + TILE_SIZE / 2, y2 + TILE_SIZE / 2);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    35
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    36
		if (z2 - z1 > 1) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    37
			/* Slope up */
3981
b61ec1c297cd (svn r5169) - CodeChange: [YAPF] RVs now use YAPF_TILE_LENGTH as base unit and 2 configurable settings (slope and crossing penalties) for cost calculation
KUDr
parents: 3977
diff changeset
    38
			return Yapf().PfGetSettings().road_slope_penalty;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    39
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    40
		return 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    41
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    42
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    43
	/** return one tile cost */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    44
	FORCEINLINE int OneTileCost(TileIndex tile, Trackdir trackdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    45
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
		int cost = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
		// set base cost
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
		if (IsDiagonalTrackdir(trackdir)) {
3981
b61ec1c297cd (svn r5169) - CodeChange: [YAPF] RVs now use YAPF_TILE_LENGTH as base unit and 2 configurable settings (slope and crossing penalties) for cost calculation
KUDr
parents: 3977
diff changeset
    49
			cost += YAPF_TILE_LENGTH;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    50
			switch (GetTileType(tile)) {
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7037
diff changeset
    51
				case MP_ROAD:
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    52
					/* Increase the cost for level crossings */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    53
					if (IsLevelCrossing(tile))
3981
b61ec1c297cd (svn r5169) - CodeChange: [YAPF] RVs now use YAPF_TILE_LENGTH as base unit and 2 configurable settings (slope and crossing penalties) for cost calculation
KUDr
parents: 3977
diff changeset
    54
						cost += Yapf().PfGetSettings().road_crossing_penalty;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
					break;
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    56
				case MP_STATION:
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    57
					if (IsDriveThroughStopTile(tile))
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    58
						cost += Yapf().PfGetSettings().road_stop_penalty;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    59
					break;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    60
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
				default:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
					break;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    63
			}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    64
		} else {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    65
			// non-diagonal trackdir
4411
d5ddc484d4d7 (svn r6164) -Fix [YAPF]: Road vehicles preferring bends - added yapf.road_curve_penalty setting with default value 100 (=YAPF_TILE_LENGTH) that is saved only in MP games (mart3p)
KUDr
parents: 4410
diff changeset
    66
			cost = YAPF_TILE_CORNER_LENGTH + Yapf().PfGetSettings().road_curve_penalty;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    67
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
		return cost;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    72
	/** Called by YAPF to calculate the cost from the origin to the given node.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4411
diff changeset
    73
	 *  Calculates only the cost of given node, adds it to the parent node cost
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4411
diff changeset
    74
	 *  and stores the result into Node::m_cost member */
7037
64249224bb74 (svn r10301) -Fix [FS#901, YAPF]: another assert violation in some special cases (immeR)
KUDr
parents: 6683
diff changeset
    75
	FORCEINLINE bool PfCalcCost(Node& n, const TrackFollower *tf)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    76
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    77
		int segment_cost = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
		// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    79
		TileIndex tile = n.m_key.m_tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    80
		Trackdir trackdir = n.m_key.m_td;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    81
		while (true) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
			// base tile cost depending on distance between edges
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    83
			segment_cost += Yapf().OneTileCost(tile, trackdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    84
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    85
			const Vehicle* v = Yapf().GetVehicle();
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    86
			// we have reached the vehicle's destination - segment should end here to avoid target skipping
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8616
diff changeset
    87
			if (v->current_order.IsType(OT_GOTO_STATION) && tile == v->dest_tile) break;
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 5475
diff changeset
    88
3976
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    89
			// stop if we have just entered the depot
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    90
			if (IsTileDepotType(tile, TRANSPORT_ROAD) && trackdir == DiagdirToDiagTrackdir(ReverseDiagDir(GetRoadDepotDirection(tile)))) {
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    91
				// next time we will reverse and leave the depot
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    92
				break;
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    93
			}
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    94
6135bc445350 (svn r5153) - Fix: [YAPF] RVs can now plan path reversing in depot or end of road
KUDr
parents: 3975
diff changeset
    95
			// if there are no reachable trackdirs on new tile, we have end of road
4407
685c613f3fec (svn r6160) -Fix [ 1519167 ] Bus trying to service in depot of other company (mart3p)
KUDr
parents: 4269
diff changeset
    96
			TrackFollower F(Yapf().GetVehicle());
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    97
			if (!F.Follow(tile, trackdir)) break;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    98
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    99
			// if there are more trackdirs available & reachable, we are at the end of segment
7833
ba402b153b79 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7370
diff changeset
   100
			if (KillFirstBit(F.m_new_td_bits) != TRACKDIR_BIT_NONE) break;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   101
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   102
			Trackdir new_td = (Trackdir)FindFirstBit2x64(F.m_new_td_bits);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   103
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   104
			// stop if RV is on simple loop with no junctions
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   105
			if (F.m_new_tile == n.m_key.m_tile && new_td == n.m_key.m_td) return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   107
			// if we skipped some tunnel tiles, add their cost
3981
b61ec1c297cd (svn r5169) - CodeChange: [YAPF] RVs now use YAPF_TILE_LENGTH as base unit and 2 configurable settings (slope and crossing penalties) for cost calculation
KUDr
parents: 3977
diff changeset
   108
			segment_cost += F.m_tiles_skipped * YAPF_TILE_LENGTH;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   109
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   110
			// add hilly terrain penalty
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   111
			segment_cost += Yapf().SlopeCost(tile, F.m_new_tile, trackdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   112
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   113
			// add min/max speed penalties
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   114
			int min_speed = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   115
			int max_speed = F.GetSpeedLimit(&min_speed);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   116
			if (max_speed < v->max_speed) segment_cost += 1 * (v->max_speed - max_speed);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   117
			if (min_speed > v->max_speed) segment_cost += 10 * (min_speed - v->max_speed);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   118
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
			// move to the next tile
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   120
			tile = F.m_new_tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   121
			trackdir = new_td;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
		};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   124
		// save end of segment back to the node
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   125
		n.m_segment_last_tile = tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
		n.m_segment_last_td = trackdir;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   127
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   128
		// save also tile cost
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   129
		int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   130
		n.m_cost = parent_cost + segment_cost;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   131
		return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   132
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   133
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   134
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   135
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   136
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   137
class CYapfDestinationAnyDepotRoadT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   138
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   139
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   140
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   141
	typedef typename Types::TrackFollower TrackFollower;
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   142
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   143
	typedef typename Node::Key Key;                      ///< key to hash tables
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   144
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   145
	/// to access inherited path finder
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   146
	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   147
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   148
	/// Called by YAPF to detect if node ends in the desired destination
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   149
	FORCEINLINE bool PfDetectDestination(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   151
		bool bDest = IsTileDepotType(n.m_segment_last_tile, TRANSPORT_ROAD);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
		return bDest;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   155
	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4411
diff changeset
   156
	 *  adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   157
	FORCEINLINE bool PfCalcEstimate(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   158
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   159
		n.m_estimate = n.m_cost;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   160
		return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   161
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   162
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   163
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   164
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   165
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   166
class CYapfDestinationTileRoadT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   167
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   168
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   169
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   170
	typedef typename Types::TrackFollower TrackFollower;
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   171
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   172
	typedef typename Node::Key Key;                      ///< key to hash tables
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   174
protected:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   175
	TileIndex    m_destTile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   176
	TrackdirBits m_destTrackdirs;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   177
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   178
public:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
	void SetDestination(TileIndex tile, TrackdirBits trackdirs)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   180
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   181
		m_destTile = tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
		m_destTrackdirs = trackdirs;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   184
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   185
protected:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   186
	/// to access inherited path finder
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   187
	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   188
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   189
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   190
	/// Called by YAPF to detect if node ends in the desired destination
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   191
	FORCEINLINE bool PfDetectDestination(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   192
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   193
		bool bDest = (n.m_segment_last_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.m_segment_last_td)) != TRACKDIR_BIT_NONE);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   194
		return bDest;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   195
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   196
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   197
	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4411
diff changeset
   198
	 *  adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   199
	inline bool PfCalcEstimate(Node& n)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   200
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   201
		static int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   202
		static int dg_dir_to_y_offs[] = {0, 1, 0, -1};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   203
		if (PfDetectDestination(n)) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   204
			n.m_estimate = n.m_cost;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   205
			return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   206
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   207
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   208
		TileIndex tile = n.m_segment_last_tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   209
		DiagDirection exitdir = TrackdirToExitdir(n.m_segment_last_td);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   210
		int x1 = 2 * TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   211
		int y1 = 2 * TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   212
		int x2 = 2 * TileX(m_destTile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   213
		int y2 = 2 * TileY(m_destTile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   214
		int dx = abs(x1 - x2);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   215
		int dy = abs(y1 - y2);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   216
		int dmin = min(dx, dy);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   217
		int dxy = abs(dx - dy);
3981
b61ec1c297cd (svn r5169) - CodeChange: [YAPF] RVs now use YAPF_TILE_LENGTH as base unit and 2 configurable settings (slope and crossing penalties) for cost calculation
KUDr
parents: 3977
diff changeset
   218
		int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   219
		n.m_estimate = n.m_cost + d;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   220
		assert(n.m_estimate >= n.m_parent->m_estimate);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   221
		return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   222
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   223
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   224
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   225
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   226
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   227
template <class Types>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   228
class CYapfFollowRoadT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   229
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   230
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   231
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   232
	typedef typename Types::TrackFollower TrackFollower;
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   233
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   234
	typedef typename Node::Key Key;                      ///< key to hash tables
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   235
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   236
protected:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   237
	/// to access inherited path finder
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   239
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   240
public:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   241
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   242
	/** Called by YAPF to move from the given node to the next tile. For each
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4411
diff changeset
   243
	 *  reachable trackdir on the new tile creates new node, initializes it
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4411
diff changeset
   244
	 *  and adds it to the open list by calling Yapf().AddNewNode(n) */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   245
	inline void PfFollowNode(Node& old_node)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   246
	{
4407
685c613f3fec (svn r6160) -Fix [ 1519167 ] Bus trying to service in depot of other company (mart3p)
KUDr
parents: 4269
diff changeset
   247
		TrackFollower F(Yapf().GetVehicle());
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   248
		if (F.Follow(old_node.m_segment_last_tile, old_node.m_segment_last_td))
6132
8b4edf37c5ff (svn r8869) [YAPF] -Fix: Large Train Stations/Trains makes OpenTTD crash (Jigsaw_Psyche)
KUDr
parents: 6121
diff changeset
   249
			Yapf().AddMultipleNodes(&old_node, F);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   250
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   251
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   252
	/// return debug report character to identify the transportation type
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   253
	FORCEINLINE char TransportTypeChar() const {return 'r';}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   254
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   255
	static Trackdir stChooseRoadTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   256
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   257
		Tpf pf;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   258
		return pf.ChooseRoadTrack(v, tile, enterdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   259
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   260
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   261
	FORCEINLINE Trackdir ChooseRoadTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   262
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   263
		// handle special case - when next tile is destination tile
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   264
		if (tile == v->dest_tile) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   265
			// choose diagonal trackdir reachable from enterdir
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   266
			return (Trackdir)DiagdirToDiagTrackdir(enterdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   267
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   268
		// our source tile will be the next vehicle tile (should be the given one)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   269
		TileIndex src_tile = tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   270
		// get available trackdirs on the start tile
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 7833
diff changeset
   271
		TrackdirBits src_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes));
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   272
		// select reachable trackdirs only
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   273
		src_trackdirs &= DiagdirReachesTrackdirs(enterdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   274
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   275
		// get available trackdirs on the destination tile
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   276
		TileIndex dest_tile = v->dest_tile;
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 7833
diff changeset
   277
		TrackdirBits dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(dest_tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes));
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   278
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   279
		// set origin and destination nodes
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   280
		Yapf().SetOrigin(src_tile, src_trackdirs);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   281
		Yapf().SetDestination(dest_tile, dest_trackdirs);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   282
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   283
		// find the best path
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   284
		Yapf().FindPath(v);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   285
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   286
		// if path not found - return INVALID_TRACKDIR
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   287
		Trackdir next_trackdir = INVALID_TRACKDIR;
6510
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   288
		Node *pNode = Yapf().GetBestNode();
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   289
		if (pNode != NULL) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   290
			// path was found or at least suggested
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   291
			// walk through the path back to its origin
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   292
			while (pNode->m_parent != NULL) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   293
				pNode = pNode->m_parent;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   294
			}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   295
			// return trackdir from the best origin node (one of start nodes)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   296
			Node& best_next_node = *pNode;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   297
			assert(best_next_node.GetTile() == tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   298
			next_trackdir = best_next_node.GetTrackdir();
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   299
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   300
		return next_trackdir;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   301
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   302
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   303
	static uint stDistanceToTile(const Vehicle *v, TileIndex tile)
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   304
	{
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   305
		Tpf pf;
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   306
		return pf.DistanceToTile(v, tile);
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   307
	}
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   308
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   309
	FORCEINLINE uint DistanceToTile(const Vehicle *v, TileIndex dst_tile)
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   310
	{
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   311
		// handle special case - when current tile is the destination tile
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   312
		if (dst_tile == v->tile) {
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   313
			// distance is zero in this case
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   314
			return 0;
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   315
		}
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   316
3918
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   317
		if (!SetOriginFromVehiclePos(v)) return UINT_MAX;
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   318
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   319
		// set destination tile, trackdir
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   320
		//   get available trackdirs on the destination tile
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 7833
diff changeset
   321
		TrackdirBits dst_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes));
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   322
		Yapf().SetDestination(dst_tile, dst_td_bits);
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   323
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   324
		// find the best path
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   325
		Yapf().FindPath(v);
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   326
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   327
		// if path not found - return distance = UINT_MAX
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   328
		uint dist = UINT_MAX;
6510
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   329
		Node *pNode = Yapf().GetBestNode();
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   330
		if (pNode != NULL) {
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   331
			// path was found or at least suggested
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   332
			// get the path cost estimate
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   333
			dist = pNode->GetCostEstimate();
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   334
		}
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   335
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   336
		return dist;
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   337
	}
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   338
3918
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   339
	/** Return true if the valid origin (tile/trackdir) was set from the current vehicle position. */
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   340
	FORCEINLINE bool SetOriginFromVehiclePos(const Vehicle *v)
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   341
	{
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   342
		// set origin (tile, trackdir)
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   343
		TileIndex src_tile = v->tile;
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   344
		Trackdir src_td = GetVehicleTrackdir(v);
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 7833
diff changeset
   345
		if ((TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes)) & TrackdirToTrackdirBits(src_td)) == 0) {
3918
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   346
			// sometimes the roadveh is not on the road (it resides on non-existing track)
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   347
			// how should we handle that situation?
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   348
			return false;
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   349
		}
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   350
		Yapf().SetOrigin(src_tile, TrackdirToTrackdirBits(src_td));
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   351
		return true;
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   352
	}
2432e667e8a7 (svn r5037) -Fix: assert when GetVehicleTrackdir() returns wrong trackdir - introduced by r5033 (thanks yanek)
KUDr
parents: 3915
diff changeset
   353
4989
3d6541afbbaf (svn r6992) Make a const_cast<> obsolete by properly propagating the type
tron
parents: 4549
diff changeset
   354
	static Depot* stFindNearestDepot(const Vehicle* v, TileIndex tile, Trackdir td)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   355
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   356
		Tpf pf;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   357
		return pf.FindNearestDepot(v, tile, td);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   358
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   359
4989
3d6541afbbaf (svn r6992) Make a const_cast<> obsolete by properly propagating the type
tron
parents: 4549
diff changeset
   360
	FORCEINLINE Depot* FindNearestDepot(const Vehicle* v, TileIndex tile, Trackdir td)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   361
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   362
		// set origin and destination nodes
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   363
		Yapf().SetOrigin(tile, TrackdirToTrackdirBits(td));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   364
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   365
		// find the best path
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   366
		bool bFound = Yapf().FindPath(v);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   367
		if (!bFound) return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   368
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   369
		// some path found
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   370
		// get found depot tile
6510
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   371
		Node *n = Yapf().GetBestNode();
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   372
		TileIndex depot_tile = n->m_segment_last_tile;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   373
		assert(IsTileDepotType(depot_tile, TRANSPORT_ROAD));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   374
		Depot* ret = GetDepotByTile(depot_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   375
		return ret;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   376
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   377
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   378
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   379
template <class Tpf_, class Tnode_list, template <class Types> class Tdestination>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   380
struct CYapfRoad_TypesT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   381
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   382
	typedef CYapfRoad_TypesT<Tpf_, Tnode_list, Tdestination>  Types;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   383
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   384
	typedef Tpf_                              Tpf;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   385
	typedef CFollowTrackRoad                  TrackFollower;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   386
	typedef Tnode_list                        NodeList;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   387
	typedef CYapfBaseT<Types>                 PfBase;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   388
	typedef CYapfFollowRoadT<Types>           PfFollow;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   389
	typedef CYapfOriginTileT<Types>           PfOrigin;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   390
	typedef Tdestination<Types>               PfDestination;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   391
	typedef CYapfSegmentCostCacheNoneT<Types> PfCache;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   392
	typedef CYapfCostRoadT<Types>             PfCost;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   393
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   394
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   395
struct CYapfRoad1         : CYapfT<CYapfRoad_TypesT<CYapfRoad1        , CRoadNodeListTrackDir, CYapfDestinationTileRoadT    > > {};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   396
struct CYapfRoad2         : CYapfT<CYapfRoad_TypesT<CYapfRoad2        , CRoadNodeListExitDir , CYapfDestinationTileRoadT    > > {};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   397
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   398
struct CYapfRoadAnyDepot1 : CYapfT<CYapfRoad_TypesT<CYapfRoadAnyDepot1, CRoadNodeListTrackDir, CYapfDestinationAnyDepotRoadT> > {};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   399
struct CYapfRoadAnyDepot2 : CYapfT<CYapfRoad_TypesT<CYapfRoadAnyDepot2, CRoadNodeListExitDir , CYapfDestinationAnyDepotRoadT> > {};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   400
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   401
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   402
Trackdir YapfChooseRoadTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   403
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   404
	// default is YAPF type 2
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   405
	typedef Trackdir (*PfnChooseRoadTrack)(Vehicle*, TileIndex, DiagDirection);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   406
	PfnChooseRoadTrack pfnChooseRoadTrack = &CYapfRoad2::stChooseRoadTrack; // default: ExitDir, allow 90-deg
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   407
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   408
	// check if non-default YAPF type should be used
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   409
	if (_patches.yapf.disable_node_optimization)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   410
		pfnChooseRoadTrack = &CYapfRoad1::stChooseRoadTrack; // Trackdir, allow 90-deg
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   411
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   412
	Trackdir td_ret = pfnChooseRoadTrack(v, tile, enterdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   413
	return td_ret;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   414
}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   415
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   416
uint YapfRoadVehDistanceToTile(const Vehicle* v, TileIndex tile)
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   417
{
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   418
	// default is YAPF type 2
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   419
	typedef uint (*PfnDistanceToTile)(const Vehicle*, TileIndex);
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   420
	PfnDistanceToTile pfnDistanceToTile = &CYapfRoad2::stDistanceToTile; // default: ExitDir, allow 90-deg
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   421
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   422
	// check if non-default YAPF type should be used
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   423
	if (_patches.yapf.disable_node_optimization)
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   424
		pfnDistanceToTile = &CYapfRoad1::stDistanceToTile; // Trackdir, allow 90-deg
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   425
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   426
	// measure distance in YAPF units
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   427
	uint dist = pfnDistanceToTile(v, tile);
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   428
	// convert distance to tiles
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   429
	if (dist != UINT_MAX)
4410
f873dee9373d (svn r6163) -Codechange: distance rounded up (correction of r6162)
KUDr
parents: 4409
diff changeset
   430
		dist = (dist + YAPF_TILE_LENGTH - 1) / YAPF_TILE_LENGTH;
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   431
	return dist;
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   432
}
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   433
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   434
Depot* YapfFindNearestRoadDepot(const Vehicle *v)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   435
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   436
	TileIndex tile = v->tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   437
	Trackdir trackdir = GetVehicleTrackdir(v);
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 7833
diff changeset
   438
	if ((TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes)) & TrackdirToTrackdirBits(trackdir)) == 0)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   439
		return NULL;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   440
3975
92753022c942 (svn r5148) - Fix: [YAPF] RVs can now find depot they are currently in (thanks Darkvater)
KUDr
parents: 3933
diff changeset
   441
	// handle the case when our vehicle is already in the depot tile
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7037
diff changeset
   442
	if (IsTileType(tile, MP_ROAD) && IsTileDepotType(tile, TRANSPORT_ROAD)) {
3975
92753022c942 (svn r5148) - Fix: [YAPF] RVs can now find depot they are currently in (thanks Darkvater)
KUDr
parents: 3933
diff changeset
   443
		// only what we need to return is the Depot*
92753022c942 (svn r5148) - Fix: [YAPF] RVs can now find depot they are currently in (thanks Darkvater)
KUDr
parents: 3933
diff changeset
   444
		return GetDepotByTile(tile);
92753022c942 (svn r5148) - Fix: [YAPF] RVs can now find depot they are currently in (thanks Darkvater)
KUDr
parents: 3933
diff changeset
   445
	}
92753022c942 (svn r5148) - Fix: [YAPF] RVs can now find depot they are currently in (thanks Darkvater)
KUDr
parents: 3933
diff changeset
   446
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   447
	// default is YAPF type 2
4989
3d6541afbbaf (svn r6992) Make a const_cast<> obsolete by properly propagating the type
tron
parents: 4549
diff changeset
   448
	typedef Depot* (*PfnFindNearestDepot)(const Vehicle*, TileIndex, Trackdir);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   449
	PfnFindNearestDepot pfnFindNearestDepot = &CYapfRoadAnyDepot2::stFindNearestDepot;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   450
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   451
	// check if non-default YAPF type should be used
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   452
	if (_patches.yapf.disable_node_optimization)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   453
		pfnFindNearestDepot = &CYapfRoadAnyDepot1::stFindNearestDepot; // Trackdir, allow 90-deg
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   454
4989
3d6541afbbaf (svn r6992) Make a const_cast<> obsolete by properly propagating the type
tron
parents: 4549
diff changeset
   455
	Depot* ret = pfnFindNearestDepot(v, tile, trackdir);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   456
	return ret;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   457
}