src/yapf/yapf_rail.cpp
author translators
Sun, 30 Nov 2008 18:46:32 +0000
changeset 10395 6fe57f7c5d8d
parent 10104 fb4346183ffe
permissions -rw-r--r--
(svn r14646) -Update: WebTranslator2 update to 2008-11-30 18:46:24
esperanto - 4 fixed by Athaba (4)
finnish - 1 changed by SuomiPoika (1)
indonesian - 16 fixed, 4 changed by fanioz (20)
korean - 1 fixed by dlunch (1)
lithuanian - 57 fixed, 20 changed by Enternald (77)
macedonian - 140 fixed, 1 changed by simonkoco (34), zharko (107)
persian - 21 fixed by ali sattari (21)
serbian - 5 fixed by dejac (5)
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
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8481
diff changeset
     3
/** @file yapf_rail.cpp The rail pathfinding. */
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: 5587
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_rail.hpp"
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     9
#include "yapf_costrail.hpp"
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    10
#include "yapf_destrail.hpp"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 7119
diff changeset
    11
#include "../vehicle_func.h"
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    12
#include "../pbs.h"
10104
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    13
#include "../functions.h"
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    14
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
    15
#define DEBUG_YAPF_CACHE 0
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
    16
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    17
int _total_pf_time_us = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
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
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    21
template <class Types>
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    22
class CYapfReserveTrack
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    23
{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    24
public:
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    25
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    26
	typedef typename Types::TrackFollower TrackFollower;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    27
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    28
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    29
protected:
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    30
	/// to access inherited pathfinder
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    31
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    32
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    33
private:
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    34
	TileIndex m_res_dest;         ///< The reservation target tile
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    35
	Trackdir  m_res_dest_td;      ///< The reservation target trackdir
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    36
	Node      *m_res_node;        ///< The reservation target node
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    37
	TileIndex m_res_fail_tile;    ///< The tile where the reservation failed
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    38
	Trackdir  m_res_fail_td;      ///< The trackdir where the reservation failed
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    39
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    40
	bool FindSafePositionProc(TileIndex tile, Trackdir td)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    41
	{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    42
		if (IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns())) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    43
			m_res_dest = tile;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    44
			m_res_dest_td = td;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    45
			return false;   // Stop iterating segment
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    46
		}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    47
		return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    48
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    49
10104
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    50
	/** Reserve a railway platform. Tile contains the failed tile on abort. */
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    51
	bool ReserveRailwayStationPlatform(TileIndex &tile, DiagDirection dir)
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    52
	{
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    53
		TileIndex     start = tile;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    54
		TileIndexDiff diff = TileOffsByDiagDir(dir);
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    55
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    56
		do {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    57
			if (GetRailwayStationReservation(tile)) return false;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    58
			SetRailwayStationReservation(tile, true);
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    59
			MarkTileDirtyByTile(tile);
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    60
			tile = TILE_ADD(tile, diff);
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    61
		} while (IsCompatibleTrainStationTile(tile, start));
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    62
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    63
		return true;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    64
	}
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    65
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    66
	/** Try to reserve a single track/platform. */
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    67
	bool ReserveSingleTrack(TileIndex tile, Trackdir td)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    68
	{
10104
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    69
		if (IsRailwayStationTile(tile)) {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    70
			if (!ReserveRailwayStationPlatform(tile, TrackdirToExitdir(ReverseTrackdir(td)))) {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    71
				/* Platform could not be reserved, undo. */
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    72
				m_res_fail_tile = tile;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    73
				m_res_fail_td = td;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    74
			}
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    75
		} else {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    76
			if (!TryReserveRailTrack(tile, TrackdirToTrack(td))) {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    77
				/* Tile couldn't be reserved, undo. */
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    78
				m_res_fail_tile = tile;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    79
				m_res_fail_td = td;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    80
				return false;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    81
			}
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    82
		}
10104
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    83
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    84
		return tile != m_res_dest;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    85
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    86
10104
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    87
	/** Unreserve a single track/platform. Stops when the previous failer is reached. */
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    88
	bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
    89
	{
10104
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    90
		if (IsRailwayStationTile(tile)) {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    91
			TileIndex     start = tile;
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    92
			TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(td)));
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    93
			while ((tile != m_res_fail_tile || td != m_res_fail_td) && IsCompatibleTrainStationTile(tile, start)) {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    94
				SetRailwayStationReservation(tile, false);
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    95
				tile = TILE_ADD(tile, diff);
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    96
			}
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    97
		} else if (tile != m_res_fail_tile || td != m_res_fail_td) {
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    98
			UnreserveRailTrack(tile, TrackdirToTrack(td));
fb4346183ffe (svn r14286) -Fix [FS#2265]: If a change of conventional/electric rail coincided with the start of a station platform, stale reservations could be left behind.
michi_cc
parents: 9899
diff changeset
    99
		}
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   100
		return tile != m_res_dest && (tile != m_res_fail_tile || td != m_res_fail_td);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   101
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   102
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   103
public:
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   104
	/** Set the target to where the reservation should be extended. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   105
	inline void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   106
	{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   107
		m_res_node = node;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   108
		m_res_dest = tile;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   109
		m_res_dest_td = td;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   110
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   111
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   112
	/** Check the node for a possible reservation target. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   113
	inline void FindSafePositionOnNode(Node *node)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   114
	{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   115
		assert(node->m_parent != NULL);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   116
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   117
		/* We will never pass more than two signals, no need to check for a safe tile. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   118
		if (node->m_parent->m_num_signals_passed >= 2) return;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   119
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   120
		if (!node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::FindSafePositionProc)) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   121
			m_res_node = node;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   122
		}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   123
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   124
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   125
	/** Try to reserve the path till the reservation target. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   126
	bool TryReservePath(PBSTileInfo *target)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   127
	{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   128
		m_res_fail_tile = INVALID_TILE;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   129
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   130
		if (target != NULL) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   131
			target->tile = m_res_dest;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   132
			target->trackdir = m_res_dest_td;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   133
			target->okay = false;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   134
		}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   135
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   136
		/* Don't bother if the target is reserved. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   137
		if (!IsWaitingPositionFree(Yapf().GetVehicle(), m_res_dest, m_res_dest_td)) return false;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   138
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   139
		for (Node *node = m_res_node; node->m_parent != NULL; node = node->m_parent) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   140
			node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::ReserveSingleTrack);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   141
			if (m_res_fail_tile != INVALID_TILE) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   142
				/* Reservation failed, undo. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   143
				Node *fail_node = m_res_node;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   144
				TileIndex stop_tile = m_res_fail_tile;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   145
				do {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   146
					/* If this is the node that failed, stop at the failed tile. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   147
					m_res_fail_tile = fail_node == node ? stop_tile : INVALID_TILE;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   148
					fail_node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::UnreserveSingleTrack);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   149
				} while (fail_node != node && (fail_node = fail_node->m_parent) != NULL);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   150
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   151
				return false;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   152
			}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   153
		}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   154
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   155
		if (target != NULL) target->okay = true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   156
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   157
		if (Yapf().CanUseGlobalCache(*m_res_node))
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   158
			YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   159
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   160
		return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   161
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   162
};
3900
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
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
   165
class CYapfFollowAnyDepotRailT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   166
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   167
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   168
	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
   169
	typedef typename Types::TrackFollower TrackFollower;
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   170
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   171
	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
   172
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
protected:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   174
	/// 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
   175
	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
   176
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   177
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   178
	/** 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: 3923
diff changeset
   179
	 *  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: 3923
diff changeset
   180
	 *  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
   181
	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
   182
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
		TrackFollower F(Yapf().GetVehicle());
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   184
		if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()))
6132
8b4edf37c5ff (svn r8869) [YAPF] -Fix: Large Train Stations/Trains makes OpenTTD crash (Jigsaw_Psyche)
KUDr
parents: 6121
diff changeset
   185
			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
   186
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   187
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   188
	/// 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
   189
	FORCEINLINE char TransportTypeChar() const {return 't';}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   190
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   191
	static bool stFindNearestDepotTwoWay(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   192
	{
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   193
		Tpf pf1;
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   194
		bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, depot_tile, reversed);
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   195
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   196
#if DEBUG_YAPF_CACHE
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   197
		Tpf pf2;
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   198
		TileIndex depot_tile2 = INVALID_TILE;
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   199
		bool reversed2 = false;
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   200
		pf2.DisableCache(true);
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   201
		bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, &depot_tile2, &reversed2);
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   202
		if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) {
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   203
			DEBUG(yapf, 0, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   204
		}
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   205
#endif
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   206
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   207
		return result1;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   208
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   209
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   210
	FORCEINLINE bool FindNearestDepotTwoWay(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   211
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   212
		// 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
   213
		Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   214
		Yapf().SetDestination(v);
3923
d5c7744483fc (svn r5057) -Fix: [YAPF] trains can't find depot for servicing (thanks Smoky555)
KUDr
parents: 3914
diff changeset
   215
		Yapf().SetMaxCost(YAPF_TILE_LENGTH * max_distance);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   216
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   217
		// 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
   218
		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
   219
		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
   220
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   221
		// 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
   222
		// get found depot tile
6510
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   223
		Node *n = Yapf().GetBestNode();
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   224
		*depot_tile = n->GetLastTile();
3900
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
		// walk through the path back to the origin
6510
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   227
		Node *pNode = n;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   228
		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
   229
			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
   230
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   231
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   232
		// if the origin node is our front vehicle tile/Trackdir then we didn't reverse
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   233
		// but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   234
		*reversed = (pNode->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
   235
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   236
		return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   237
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
};
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
template <class Types>
9802
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   241
class CYapfFollowAnySafeTileRailT : protected CYapfReserveTrack<Types>
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   242
{
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   243
public:
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   244
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   245
	typedef typename Types::TrackFollower TrackFollower;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   246
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   247
	typedef typename Node::Key Key;                      ///< key to hash tables
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   248
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   249
protected:
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   250
	/// to access inherited path finder
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   251
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   252
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   253
public:
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   254
	/** Called by YAPF to move from the given node to the next tile. For each
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   255
	 *  reachable trackdir on the new tile creates new node, initializes it
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   256
	 *  and adds it to the open list by calling Yapf().AddNewNode(n) */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   257
	inline void PfFollowNode(Node& old_node)
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   258
	{
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   259
		TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
9899
03a2d32a2c05 (svn r14048) -Fix (r14019) [FS#2222]: Redo the check for reserved tracks properly this time. (michi_cc)
rubidium
parents: 9802
diff changeset
   260
		if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks())
9802
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   261
			Yapf().AddMultipleNodes(&old_node, F);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   262
	}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   263
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   264
	/** Return debug report character to identify the transportation type */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   265
	FORCEINLINE char TransportTypeChar() const {return 't';}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   266
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   267
	static bool stFindNearestSafeTile(const Vehicle *v, TileIndex t1, Trackdir td, bool override_railtype)
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   268
	{
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   269
		/* Create pathfinder instance */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   270
		Tpf pf1;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   271
#if !DEBUG_YAPF_CACHE
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   272
		bool result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   273
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   274
#else
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   275
		bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   276
		Tpf pf2;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   277
		pf2.DisableCache(true);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   278
		bool result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   279
		if (result1 != result2) {
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   280
			DEBUG(yapf, 0, "CACHE ERROR: FindSafeTile() = [%s, %s]", result2 ? "T" : "F", result1 ? "T" : "F");
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   281
			DumpTarget dmp1, dmp2;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   282
			pf1.DumpBase(dmp1);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   283
			pf2.DumpBase(dmp2);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   284
			FILE *f1 = fopen("C:\\yapf1.txt", "wt");
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   285
			FILE *f2 = fopen("C:\\yapf2.txt", "wt");
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   286
			fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   287
			fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   288
			fclose(f1);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   289
			fclose(f2);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   290
		}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   291
#endif
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   292
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   293
		return result1;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   294
	}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   295
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   296
	bool FindNearestSafeTile(const Vehicle *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   297
	{
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   298
		/* Set origin and destination. */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   299
		Yapf().SetOrigin(t1, td);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   300
		Yapf().SetDestination(v, override_railtype);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   301
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   302
		bool bFound = Yapf().FindPath(v);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   303
		if (!bFound) return false;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   304
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   305
		/* Found a destination, set as reservation target. */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   306
		Node *pNode = Yapf().GetBestNode();
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   307
		this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   308
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   309
		/* Walk through the path back to the origin. */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   310
		Node* pPrev = NULL;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   311
		while (pNode->m_parent != NULL) {
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   312
			pPrev = pNode;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   313
			pNode = pNode->m_parent;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   314
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   315
			this->FindSafePositionOnNode(pPrev);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   316
		}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   317
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   318
		return dont_reserve || this->TryReservePath(NULL);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   319
	}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   320
};
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   321
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   322
template <class Types>
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   323
class CYapfFollowRailT : protected CYapfReserveTrack<Types>
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   324
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   325
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   326
	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
   327
	typedef typename Types::TrackFollower TrackFollower;
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   328
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   329
	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
   330
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   331
protected:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   332
	/// 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
   333
	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
   334
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   335
public:
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   336
	/** 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: 3923
diff changeset
   337
	 *  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: 3923
diff changeset
   338
	 *  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
   339
	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
   340
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   341
		TrackFollower F(Yapf().GetVehicle());
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   342
		if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()))
6132
8b4edf37c5ff (svn r8869) [YAPF] -Fix: Large Train Stations/Trains makes OpenTTD crash (Jigsaw_Psyche)
KUDr
parents: 6121
diff changeset
   343
			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
   344
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   345
3914
6bdd22b93698 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   346
	/// 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
   347
	FORCEINLINE char TransportTypeChar() const {return 't';}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   348
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   349
	static Trackdir stChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   350
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   351
		// create pathfinder instance
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   352
		Tpf pf1;
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   353
#if !DEBUG_YAPF_CACHE
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   354
		Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   355
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   356
#else
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   357
		Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, false, NULL);
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   358
		Tpf pf2;
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   359
		pf2.DisableCache(true);
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   360
		Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   361
		if (result1 != result2) {
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   362
			DEBUG(yapf, 0, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
7119
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   363
			DumpTarget dmp1, dmp2;
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   364
			pf1.DumpBase(dmp1);
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   365
			pf2.DumpBase(dmp2);
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   366
			FILE *f1 = fopen("C:\\yapf1.txt", "wt");
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   367
			FILE *f2 = fopen("C:\\yapf2.txt", "wt");
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   368
			fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   369
			fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   370
			fclose(f1);
afb9000a598e (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7099
diff changeset
   371
			fclose(f2);
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   372
		}
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   373
#endif
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   374
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   375
		return result1;
3900
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
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   378
	FORCEINLINE Trackdir ChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   379
	{
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   380
		if (target != NULL) target->tile = INVALID_TILE;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   381
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   382
		// set origin and destination nodes
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   383
		PBSTileInfo origin = FollowTrainReservation(v);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   384
		Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   385
		Yapf().SetDestination(v);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   386
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   387
		// find the best path
4870
2d8c7ffbbac9 (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4865
diff changeset
   388
		bool path_found = Yapf().FindPath(v);
5424
34b624b01ae1 (svn r7628) -Fix: [YAPF] suppress 'Train is lost' message if pathfinding ended on the first two-way red signal due to yapf.rail_firstred_twoway_eol option.
KUDr
parents: 5385
diff changeset
   389
		if (path_not_found != NULL) {
4870
2d8c7ffbbac9 (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4865
diff changeset
   390
			// tell controller that the path was only 'guessed'
5424
34b624b01ae1 (svn r7628) -Fix: [YAPF] suppress 'Train is lost' message if pathfinding ended on the first two-way red signal due to yapf.rail_firstred_twoway_eol option.
KUDr
parents: 5385
diff changeset
   391
			// treat the path as found if stopped on the first two way signal(s)
34b624b01ae1 (svn r7628) -Fix: [YAPF] suppress 'Train is lost' message if pathfinding ended on the first two-way red signal due to yapf.rail_firstred_twoway_eol option.
KUDr
parents: 5385
diff changeset
   392
			*path_not_found = !(path_found || Yapf().m_stopped_on_first_two_way_signal);
4870
2d8c7ffbbac9 (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4865
diff changeset
   393
		}
3900
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
		// 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
   396
		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
   397
		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
   398
		if (pNode != NULL) {
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   399
			// reserve till end of path
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   400
			this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   401
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   402
			// 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
   403
			// walk through the path back to the origin
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   404
			Node* pPrev = NULL;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   405
			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
   406
				pPrev = pNode;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   407
				pNode = pNode->m_parent;
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   408
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   409
				this->FindSafePositionOnNode(pPrev);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   410
			}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   411
			// 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
   412
			Node& best_next_node = *pPrev;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   413
			next_trackdir = best_next_node.GetTrackdir();
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   414
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   415
			if (reserve_track && path_found) this->TryReservePath(target);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   416
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   417
		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
   418
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   419
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   420
	static bool stCheckReverseTrain(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   421
	{
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   422
		Tpf pf1;
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   423
		bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   424
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   425
#if DEBUG_YAPF_CACHE
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   426
		Tpf pf2;
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   427
		pf2.DisableCache(true);
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   428
		bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
7099
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   429
		if (result1 != result2) {
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   430
			DEBUG(yapf, 0, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   431
		}
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   432
#endif
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   433
a1d7e197cb64 (svn r10366) -Codechange [YAPF]: added DEBUG_YAPF_CACHE macro that (when set to 1) allows to track YAPF "cache errors". They are probably responsible for current MP desyncs. (thanks Rubidium for this great idea!).
KUDr
parents: 7084
diff changeset
   434
		return result1;
3900
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
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   437
	FORCEINLINE bool CheckReverseTrain(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   438
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   439
		// create pathfinder instance
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   440
		// set origin and destination nodes
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   441
		Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   442
		Yapf().SetDestination(v);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   443
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   444
		// 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
   445
		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
   446
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   447
		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
   448
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   449
		// path was found
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   450
		// walk through the path back to the origin
6510
c50a538f16a7 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6132
diff changeset
   451
		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
   452
		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
   453
			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
   454
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   455
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   456
		// check if it was reversed origin
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   457
		Node& best_org_node = *pNode;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   458
		bool reversed = (best_org_node.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
   459
		return reversed;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   460
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   461
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   462
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   463
template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   464
struct CYapfRail_TypesT
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   465
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   466
	typedef CYapfRail_TypesT<Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT>  Types;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   467
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   468
	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
   469
	typedef Ttrack_follower                     TrackFollower;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   470
	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
   471
	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
   472
	typedef TfollowT<Types>                     PfFollow;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   473
	typedef CYapfOriginTileTwoWayT<Types>       PfOrigin;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   474
	typedef TdestinationT<Types>                PfDestination;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   475
	typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   476
	typedef CYapfCostRailT<Types>               PfCost;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   477
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   478
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   479
struct CYapfRail1         : CYapfT<CYapfRail_TypesT<CYapfRail1        , CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   480
struct CYapfRail2         : CYapfT<CYapfRail_TypesT<CYapfRail2        , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   481
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   482
struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   483
struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   484
9802
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   485
struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail    , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   486
struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   487
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   488
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   489
Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   490
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   491
	// default is YAPF type 2
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   492
	typedef Trackdir (*PfnChooseRailTrack)(const Vehicle*, TileIndex, DiagDirection, TrackBits, bool*, bool, PBSTileInfo*);
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   493
	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   494
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   495
	// check if non-default YAPF type needed
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   496
	if (_settings_game.pf.forbid_90_deg) {
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   497
		pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   498
	}
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   499
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   500
	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   501
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   502
	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
   503
}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   504
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   505
bool YapfCheckReverseTrain(const Vehicle* v)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   506
{
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   507
	/* last wagon */
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   508
	const Vehicle *last_veh = GetLastVehicleInChain(v);
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   509
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   510
	// get trackdirs of both ends
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   511
	Trackdir td = GetVehicleTrackdir(v);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   512
	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   513
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   514
	/* tiles where front and back are */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   515
	TileIndex tile = v->tile;
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   516
	TileIndex tile_rev = last_veh->tile;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   517
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   518
	int reverse_penalty = 0;
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   519
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   520
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   521
		/* front in tunnel / on bridge */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   522
		DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   523
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   524
		if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   525
		/* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   526
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   527
		/* Current position of the train in the wormhole */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   528
		TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   529
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   530
		/* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   531
		 * Note: Negative penalties are ok for the start tile. */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   532
		reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   533
	}
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   534
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   535
	if (last_veh->u.rail.track == TRACK_BIT_WORMHOLE) {
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   536
		/* back in tunnel / on bridge */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   537
		DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   538
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   539
		if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   540
		/* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   541
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   542
		/* Current position of the last wagon in the wormhole */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   543
		TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   544
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   545
		/* Add distance to drive in the wormhole as penalty for the revere path. */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   546
		reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   547
	}
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   548
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   549
	typedef bool (*PfnCheckReverseTrain)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int);
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   550
	PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   551
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   552
	// check if non-default YAPF type needed
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   553
	if (_settings_game.pf.forbid_90_deg) {
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   554
		pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   555
	}
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   556
8481
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   557
	/* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   558
	if (reverse_penalty == 0) reverse_penalty = 1;
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   559
b12960a2e770 (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8144
diff changeset
   560
	bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   561
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   562
	return reverse;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   563
}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   564
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   565
bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   566
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   567
	*depot_tile = INVALID_TILE;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   568
	*reversed = false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   569
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   570
	const Vehicle *last_veh = GetLastVehicleInChain(v);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   571
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   572
	PBSTileInfo origin = FollowTrainReservation(v);
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4870
diff changeset
   573
	TileIndex last_tile = last_veh->tile;
4865
8c4fb07e56f6 (svn r6791) -Fix: [YAPF] YapfFindNearestRailDepotTwoWay() did not work for train inside tunnel.
KUDr
parents: 4549
diff changeset
   574
	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   575
9439
c4e61c3f1d50 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 9413
diff changeset
   576
	typedef bool (*PfnFindNearestDepotTwoWay)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   577
	PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   578
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   579
	// check if non-default YAPF type needed
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   580
	if (_settings_game.pf.forbid_90_deg) {
7084
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   581
		pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
3a9673715c50 (svn r10349) -Fix [FS#941, YAPF, r10301]: tile/trackdir must be used as node key and also as segment key in the cache (SmatZ).
KUDr
parents: 6510
diff changeset
   582
	}
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   583
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9439
diff changeset
   584
	bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   585
	return ret;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   586
}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   587
9802
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   588
bool YapfRailFindNearestSafeTile(const Vehicle *v, TileIndex tile, Trackdir td, bool override_railtype)
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   589
{
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   590
	typedef bool (*PfnFindNearestSafeTile)(const Vehicle*, TileIndex, Trackdir, bool);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   591
	PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   592
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   593
	/* check if non-default YAPF type needed */
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   594
	if (_settings_game.pf.forbid_90_deg) {
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   595
		pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   596
	}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   597
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   598
	return pfnFindNearestSafeTile(v, tile, td, override_railtype);
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   599
}
6589c004f0d9 (svn r13944) -Add [YAPP]: Add YAPF provider to find a safe tile and reserve a path. (michi_cc)
rubidium
parents: 9798
diff changeset
   600
3910
7cb4295b8f61 (svn r5006) Added comments where doxygen generated warnings to test if it can help.
KUDr
parents: 3900
diff changeset
   601
/** if any track changes, this counter is incremented - that will invalidate segment cost cache */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   602
int CSegmentCostCacheBase::s_rail_change_counter = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   603
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   604
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track) {CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);}