src/yapf/yapf_rail.cpp
author Tero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 21:51:14 +0300
changeset 11180 982e9f814f97
parent 10806 f3fc164fc538
permissions -rw-r--r--
scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     2
10429
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8977
diff changeset
     3
/** @file yapf_rail.cpp The rail pathfinding. */
6447
3b71e57fd22b (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: 5838
diff changeset
     4
3900
4984308f9125 (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"
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
4984308f9125 (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"
4984308f9125 (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"
4984308f9125 (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"
4984308f9125 (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"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 7615
diff changeset
    11
#include "../vehicle_func.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    12
7595
7931b084aa70 (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: 7580
diff changeset
    13
#define DEBUG_YAPF_CACHE 0
7931b084aa70 (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: 7580
diff changeset
    14
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    15
int _total_pf_time_us = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    16
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    17
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    20
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    21
template <class Types>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    22
class CYapfFollowAnyDepotRailT
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    23
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    24
public:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
    25
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
	typedef typename Types::TrackFollower TrackFollower;
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
    27
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
    28
	typedef typename Node::Key Key;                      ///< key to hash tables
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    29
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    30
protected:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
    31
	/// to access inherited path finder
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    34
public:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
    35
	/** Called by YAPF to move from the given node to the next tile. For each
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3923
diff changeset
    36
	 *  reachable trackdir on the new tile creates new node, initializes it
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3923
diff changeset
    37
	 *  and adds it to the open list by calling Yapf().AddNewNode(n) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    38
	inline void PfFollowNode(Node& old_node)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    39
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    40
		TrackFollower F(Yapf().GetVehicle());
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    41
		if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()))
6458
5cd016e1f018 (svn r8869) [YAPF] -Fix: Large Train Stations/Trains makes OpenTTD crash (Jigsaw_Psyche)
KUDr
parents: 6447
diff changeset
    42
			Yapf().AddMultipleNodes(&old_node, F);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    43
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    44
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
    45
	/// return debug report character to identify the transportation type
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
	FORCEINLINE char TransportTypeChar() const {return 't';}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
    48
	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
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
	{
7595
7931b084aa70 (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: 7580
diff changeset
    50
		Tpf pf1;
7931b084aa70 (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: 7580
diff changeset
    51
		bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, depot_tile, reversed);
7931b084aa70 (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: 7580
diff changeset
    52
7931b084aa70 (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: 7580
diff changeset
    53
#if DEBUG_YAPF_CACHE
7931b084aa70 (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: 7580
diff changeset
    54
		Tpf pf2;
7931b084aa70 (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: 7580
diff changeset
    55
		TileIndex depot_tile2 = INVALID_TILE;
7931b084aa70 (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: 7580
diff changeset
    56
		bool reversed2 = false;
7931b084aa70 (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: 7580
diff changeset
    57
		pf2.DisableCache(true);
7931b084aa70 (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: 7580
diff changeset
    58
		bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, &depot_tile2, &reversed2);
7931b084aa70 (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: 7580
diff changeset
    59
		if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) {
7931b084aa70 (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: 7580
diff changeset
    60
			DEBUG(yapf, 0, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
7931b084aa70 (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: 7580
diff changeset
    61
		}
7931b084aa70 (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: 7580
diff changeset
    62
#endif
7931b084aa70 (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: 7580
diff changeset
    63
7931b084aa70 (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: 7580
diff changeset
    64
		return result1;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    65
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    66
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
    67
	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
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
		// set origin and destination nodes
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
		Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
		Yapf().SetDestination(v);
3923
b84fdfe503b2 (svn r5057) -Fix: [YAPF] trains can't find depot for servicing (thanks Smoky555)
KUDr
parents: 3914
diff changeset
    72
		Yapf().SetMaxCost(YAPF_TILE_LENGTH * max_distance);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    73
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    74
		// find the best path
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    75
		bool bFound = Yapf().FindPath(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    76
		if (!bFound) return false;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    77
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
		// some path found
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    79
		// get found depot tile
7006
e1ff46bee5b4 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6458
diff changeset
    80
		Node *n = Yapf().GetBestNode();
e1ff46bee5b4 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6458
diff changeset
    81
		*depot_tile = n->GetLastTile();
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    83
		// walk through the path back to the origin
7006
e1ff46bee5b4 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6458
diff changeset
    84
		Node *pNode = n;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    85
		while (pNode->m_parent != NULL) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    86
			pNode = pNode->m_parent;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    87
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    88
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    89
		// if the origin node is our front vehicle tile/Trackdir then we didn't reverse
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    90
		// but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    91
		*reversed = (pNode->m_cost != 0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    92
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    93
		return true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    94
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    95
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    96
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    97
template <class Types>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    98
class CYapfFollowRailT
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    99
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   100
public:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   101
	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   102
	typedef typename Types::TrackFollower TrackFollower;
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   103
	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   104
	typedef typename Node::Key Key;                      ///< key to hash tables
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   105
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
protected:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   107
	/// to access inherited path finder
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   108
	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   109
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   110
public:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   111
	/** Called by YAPF to move from the given node to the next tile. For each
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3923
diff changeset
   112
	 *  reachable trackdir on the new tile creates new node, initializes it
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3923
diff changeset
   113
	 *  and adds it to the open list by calling Yapf().AddNewNode(n) */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   114
	inline void PfFollowNode(Node& old_node)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   115
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   116
		TrackFollower F(Yapf().GetVehicle());
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   117
		if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()))
6458
5cd016e1f018 (svn r8869) [YAPF] -Fix: Large Train Stations/Trains makes OpenTTD crash (Jigsaw_Psyche)
KUDr
parents: 6447
diff changeset
   118
			Yapf().AddMultipleNodes(&old_node, F);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   120
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3910
diff changeset
   121
	/// return debug report character to identify the transportation type
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
	FORCEINLINE char TransportTypeChar() const {return 't';}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   124
	static Trackdir stChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   125
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
		// create pathfinder instance
7595
7931b084aa70 (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: 7580
diff changeset
   127
		Tpf pf1;
7931b084aa70 (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: 7580
diff changeset
   128
		Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
7931b084aa70 (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: 7580
diff changeset
   129
7931b084aa70 (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: 7580
diff changeset
   130
#if DEBUG_YAPF_CACHE
7931b084aa70 (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: 7580
diff changeset
   131
		Tpf pf2;
7931b084aa70 (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: 7580
diff changeset
   132
		pf2.DisableCache(true);
7931b084aa70 (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: 7580
diff changeset
   133
		Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
7931b084aa70 (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: 7580
diff changeset
   134
		if (result1 != result2) {
7931b084aa70 (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: 7580
diff changeset
   135
			DEBUG(yapf, 0, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
7615
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   136
			DumpTarget dmp1, dmp2;
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   137
			pf1.DumpBase(dmp1);
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   138
			pf2.DumpBase(dmp2);
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   139
			FILE *f1 = fopen("C:\\yapf1.txt", "wt");
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   140
			FILE *f2 = fopen("C:\\yapf2.txt", "wt");
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   141
			fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   142
			fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   143
			fclose(f1);
42bf748ad323 (svn r10392) -Add [YAPF]: added structured dump support into some essential YAPF classes (node-list, nodes, keys, etc.) and CArrayT
KUDr
parents: 7595
diff changeset
   144
			fclose(f2);
7595
7931b084aa70 (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: 7580
diff changeset
   145
		}
7931b084aa70 (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: 7580
diff changeset
   146
#endif
7931b084aa70 (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: 7580
diff changeset
   147
7931b084aa70 (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: 7580
diff changeset
   148
		return result1;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   149
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   151
	FORCEINLINE Trackdir ChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
		// set origin and destination nodes
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
		Yapf().SetOrigin(v->tile, GetVehicleTrackdir(v), INVALID_TILE, INVALID_TRACKDIR, 1, true);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   155
		Yapf().SetDestination(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   156
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   157
		// find the best path
4870
0ee22ed51ada (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
   158
		bool path_found = Yapf().FindPath(v);
5675
56ac39010fe9 (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: 5573
diff changeset
   159
		if (path_not_found != NULL) {
4870
0ee22ed51ada (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
   160
			// tell controller that the path was only 'guessed'
5675
56ac39010fe9 (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: 5573
diff changeset
   161
			// treat the path as found if stopped on the first two way signal(s)
56ac39010fe9 (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: 5573
diff changeset
   162
			*path_not_found = !(path_found || Yapf().m_stopped_on_first_two_way_signal);
4870
0ee22ed51ada (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
   163
		}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   164
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   165
		// if path not found - return INVALID_TRACKDIR
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   166
		Trackdir next_trackdir = INVALID_TRACKDIR;
7006
e1ff46bee5b4 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6458
diff changeset
   167
		Node *pNode = Yapf().GetBestNode();
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   168
		if (pNode != NULL) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   169
			// path was found or at least suggested
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   170
			// walk through the path back to the origin
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   171
			Node* pPrev = NULL;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   172
			while (pNode->m_parent != NULL) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
				pPrev = pNode;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   174
				pNode = pNode->m_parent;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   175
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   176
			// return trackdir from the best origin node (one of start nodes)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   177
			Node& best_next_node = *pPrev;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   178
			assert(best_next_node.GetTile() == tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
			next_trackdir = best_next_node.GetTrackdir();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   180
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   181
		return next_trackdir;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   184
	static bool stCheckReverseTrain(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   185
	{
7595
7931b084aa70 (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: 7580
diff changeset
   186
		Tpf pf1;
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   187
		bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
7595
7931b084aa70 (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: 7580
diff changeset
   188
7931b084aa70 (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: 7580
diff changeset
   189
#if DEBUG_YAPF_CACHE
7931b084aa70 (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: 7580
diff changeset
   190
		Tpf pf2;
7931b084aa70 (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: 7580
diff changeset
   191
		pf2.DisableCache(true);
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   192
		bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
7595
7931b084aa70 (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: 7580
diff changeset
   193
		if (result1 != result2) {
7931b084aa70 (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: 7580
diff changeset
   194
			DEBUG(yapf, 0, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
7931b084aa70 (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: 7580
diff changeset
   195
		}
7931b084aa70 (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: 7580
diff changeset
   196
#endif
7931b084aa70 (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: 7580
diff changeset
   197
7931b084aa70 (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: 7580
diff changeset
   198
		return result1;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   199
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   200
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   201
	FORCEINLINE bool CheckReverseTrain(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   202
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   203
		// create pathfinder instance
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   204
		// set origin and destination nodes
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   205
		Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   206
		Yapf().SetDestination(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   207
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   208
		// find the best path
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   209
		bool bFound = Yapf().FindPath(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   210
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   211
		if (!bFound) return false;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   212
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   213
		// path was found
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   214
		// walk through the path back to the origin
7006
e1ff46bee5b4 (svn r9693) -Codechange [YAPF]: GetBestNode() now returns pointer to node instead of reference
KUDr
parents: 6458
diff changeset
   215
		Node *pNode = Yapf().GetBestNode();
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   216
		while (pNode->m_parent != NULL) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   217
			pNode = pNode->m_parent;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   218
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   219
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   220
		// check if it was reversed origin
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   221
		Node& best_org_node = *pNode;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   222
		bool reversed = (best_org_node.m_cost != 0);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   223
		return reversed;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   224
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   225
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   226
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   227
template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   228
struct CYapfRail_TypesT
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   229
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   230
	typedef CYapfRail_TypesT<Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT>  Types;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   231
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   232
	typedef Tpf_                                Tpf;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   233
	typedef Ttrack_follower                     TrackFollower;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   234
	typedef Tnode_list                          NodeList;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   235
	typedef CYapfBaseT<Types>                   PfBase;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   236
	typedef TfollowT<Types>                     PfFollow;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   237
	typedef CYapfOriginTileTwoWayT<Types>       PfOrigin;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
	typedef TdestinationT<Types>                PfDestination;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   239
	typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   240
	typedef CYapfCostRailT<Types>               PfCost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   241
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   242
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   243
struct CYapfRail1         : CYapfT<CYapfRail_TypesT<CYapfRail1        , CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
7580
0d05936bc612 (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: 7006
diff changeset
   244
struct CYapfRail2         : CYapfT<CYapfRail_TypesT<CYapfRail2        , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   245
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   246
struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
7580
0d05936bc612 (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: 7006
diff changeset
   247
struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   248
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   249
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   250
Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   251
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   252
	// default is YAPF type 2
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   253
	typedef Trackdir (*PfnChooseRailTrack)(const Vehicle*, TileIndex, DiagDirection, TrackBits, bool*);
7580
0d05936bc612 (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: 7006
diff changeset
   254
	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   255
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   256
	// check if non-default YAPF type needed
10775
7061477bfbcf (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 10703
diff changeset
   257
	if (_settings_game.pf.forbid_90_deg) {
7580
0d05936bc612 (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: 7006
diff changeset
   258
		pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
0d05936bc612 (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: 7006
diff changeset
   259
	}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   260
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   261
	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   262
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   263
	return td_ret;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   264
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   265
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   266
bool YapfCheckReverseTrain(const Vehicle* v)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   267
{
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   268
	/* last wagon */
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   269
	const Vehicle *last_veh = GetLastVehicleInChain(v);
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   270
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   271
	// get trackdirs of both ends
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   272
	Trackdir td = GetVehicleTrackdir(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   273
	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   274
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   275
	/* tiles where front and back are */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   276
	TileIndex tile = v->tile;
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   277
	TileIndex tile_rev = last_veh->tile;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   278
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   279
	int reverse_penalty = 0;
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   280
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   281
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   282
		/* front in tunnel / on bridge */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   283
		DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   284
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   285
		if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   286
		/* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   287
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   288
		/* Current position of the train in the wormhole */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   289
		TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   290
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   291
		/* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   292
		 * Note: Negative penalties are ok for the start tile. */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   293
		reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   294
	}
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   295
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   296
	if (last_veh->u.rail.track == TRACK_BIT_WORMHOLE) {
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   297
		/* back in tunnel / on bridge */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   298
		DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   299
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   300
		if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   301
		/* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   302
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   303
		/* Current position of the last wagon in the wormhole */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   304
		TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   305
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   306
		/* Add distance to drive in the wormhole as penalty for the revere path. */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   307
		reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   308
	}
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   309
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   310
	typedef bool (*PfnCheckReverseTrain)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int);
7580
0d05936bc612 (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: 7006
diff changeset
   311
	PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   312
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   313
	// check if non-default YAPF type needed
10775
7061477bfbcf (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 10703
diff changeset
   314
	if (_settings_game.pf.forbid_90_deg) {
7580
0d05936bc612 (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: 7006
diff changeset
   315
		pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
0d05936bc612 (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: 7006
diff changeset
   316
	}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   317
8977
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   318
	/* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   319
	if (reverse_penalty == 0) reverse_penalty = 1;
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   320
e6ebfc5024cb (svn r12056) -Fix [FS#1704]: Enable YAPF to start searching inside a wormhole.
frosch
parents: 8640
diff changeset
   321
	bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   322
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   323
	return reverse;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   324
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   325
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   326
bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   327
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   328
	*depot_tile = INVALID_TILE;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   329
	*reversed = false;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   330
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   331
	const Vehicle *last_veh = GetLastVehicleInChain(v);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   332
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4870
diff changeset
   333
	TileIndex tile = v->tile;
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 4870
diff changeset
   334
	TileIndex last_tile = last_veh->tile;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   335
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   336
	// their trackdirs
4865
cad5fa7ec427 (svn r6791) -Fix: [YAPF] YapfFindNearestRailDepotTwoWay() did not work for train inside tunnel.
KUDr
parents: 4549
diff changeset
   337
	Trackdir td = GetVehicleTrackdir(v);
cad5fa7ec427 (svn r6791) -Fix: [YAPF] YapfFindNearestRailDepotTwoWay() did not work for train inside tunnel.
KUDr
parents: 4549
diff changeset
   338
	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   339
10806
f3fc164fc538 (svn r13357) -Codechange: add constness to YAPF.
rubidium
parents: 10775
diff changeset
   340
	typedef bool (*PfnFindNearestDepotTwoWay)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
7580
0d05936bc612 (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: 7006
diff changeset
   341
	PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   342
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   343
	// check if non-default YAPF type needed
10775
7061477bfbcf (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 10703
diff changeset
   344
	if (_settings_game.pf.forbid_90_deg) {
7580
0d05936bc612 (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: 7006
diff changeset
   345
		pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
0d05936bc612 (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: 7006
diff changeset
   346
	}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   347
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   348
	bool ret = pfnFindNearestDepotTwoWay(v, tile, td, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   349
	return ret;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   350
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   351
3910
72f2b5f7b155 (svn r5006) Added comments where doxygen generated warnings to test if it can help.
KUDr
parents: 3900
diff changeset
   352
/** if any track changes, this counter is incremented - that will invalidate segment cost cache */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   353
int CSegmentCostCacheBase::s_rail_change_counter = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   354
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   355
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track) {CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);}