src/yapf/yapf_costrail.hpp
author celestar
Mon, 15 Jan 2007 20:14:06 +0000
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5643 3778051e8095
permissions -rw-r--r--
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
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
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     3
#ifndef  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
     4
#define  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
     5
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
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
     8
class CYapfCostRailT
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     9
	: public CYapfCostBase
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    10
	, public CostRailSettings
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    11
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    12
public:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    13
	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
    14
	typedef typename Types::TrackFollower TrackFollower;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    15
	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    16
	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
    17
	typedef typename Node::CachedData CachedData;
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
protected:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    20
	int           m_max_cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    21
	CBlobT<int>   m_sig_look_ahead_costs;
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    22
public:
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    23
	bool          m_stopped_on_first_two_way_signal;
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    24
protected:
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    25
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
	static const int s_max_segment_cost = 10000;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    27
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    28
	CYapfCostRailT()
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    29
		: m_max_cost(0)
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    30
		, m_stopped_on_first_two_way_signal(false)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    31
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
		// pre-compute look-ahead penalties into array
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
		int p0 = Yapf().PfGetSettings().rail_look_ahead_signal_p0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    34
		int p1 = Yapf().PfGetSettings().rail_look_ahead_signal_p1;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    35
		int p2 = Yapf().PfGetSettings().rail_look_ahead_signal_p2;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    36
		int *pen = m_sig_look_ahead_costs.GrowSizeNC(Yapf().PfGetSettings().rail_look_ahead_max_signals);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    37
		for (uint i = 0; i < Yapf().PfGetSettings().rail_look_ahead_max_signals; i++)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    38
			pen[i] = p0 + i * (p1 + i * p2);
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
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    41
	/// 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
    42
	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
    43
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    44
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    45
	FORCEINLINE int SlopeCost(TileIndex tile, Trackdir td)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
		CPerfStart perf_cost(Yapf().m_perf_slope_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
		if (!stSlopeCost(tile, td)) return 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
		return Yapf().PfGetSettings().rail_slope_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    50
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    51
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    52
	FORCEINLINE int CurveCost(Trackdir td1, Trackdir td2)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    53
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    54
		int cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
		if (TrackFollower::Allow90degTurns()
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    56
				&& ((TrackdirToTrackdirBits(td2) & (TrackdirBits)TrackdirCrossesTrackdirs(td1)) != 0)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    57
			// 90-deg curve penalty
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    58
			cost += Yapf().PfGetSettings().rail_curve90_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    59
		} else if (td2 != NextTrackdir(td1)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    60
			// 45-deg curve penalty
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
			cost += Yapf().PfGetSettings().rail_curve45_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    63
		return cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    64
	}
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
	/** return one tile cost. If tile is a tunnel entry, it is moved to the end of tunnel */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    67
	FORCEINLINE int OneTileCost(TileIndex& tile, Trackdir trackdir)
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
		int cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
		// set base cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
		if (IsDiagonalTrackdir(trackdir)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    72
			cost += YAPF_TILE_LENGTH;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    73
			switch (GetTileType(tile)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    74
				case MP_STREET:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    75
					/* Increase the cost for level crossings */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    76
					if (IsLevelCrossing(tile))
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    77
						cost += Yapf().PfGetSettings().rail_crossing_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    79
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    80
				case MP_STATION:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    81
					// penalty for passing station tiles
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
					cost += Yapf().PfGetSettings().rail_station_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    83
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    84
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    85
				default:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    86
					break;
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
		} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    89
			// non-diagonal trackdir
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    90
			cost = YAPF_TILE_CORNER_LENGTH;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    91
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    92
		return cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    93
	}
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
	int SignalCost(Node& n, TileIndex tile, Trackdir trackdir)
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
		int cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    98
		// if there is one-way signal in the opposite direction, then it is not our way
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    99
		CPerfStart perf_cost(Yapf().m_perf_other_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   100
		if (IsTileType(tile, MP_RAILWAY)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   101
			bool has_signal_against = HasSignalOnTrackdir(tile, ReverseTrackdir(trackdir));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   102
			bool has_signal_along = HasSignalOnTrackdir(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   103
			if (has_signal_against && !has_signal_along) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   104
				// one-way signal in opposite direction
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   105
				n.m_segment->flags_u.flags_s.m_end_of_line = true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
			} else if (has_signal_along) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   107
				SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
5095
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   108
				// cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   109
				int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) ? m_sig_look_ahead_costs.Data()[n.m_num_signals_passed] : 0;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   110
				if (sig_state != SIGNAL_STATE_RED) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   111
					// green signal
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   112
					n.flags_u.flags_s.m_last_signal_was_red = false;
5095
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   113
					// negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   114
					if (look_ahead_cost < 0) {
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   115
						// add its negation to the cost
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   116
						cost -= look_ahead_cost;
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   117
					}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   118
				} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
					// we have a red signal in our direction
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   120
					// was it first signal which is two-way?
3979
4a9826b12e56 (svn r5163) - CodeChange: [YAPF] "treat first red two-way signal as dead end" is not applied if train didn't pass choice.
KUDr
parents: 3932
diff changeset
   121
					if (Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
						// yes, the first signal is two-way red signal => DEAD END
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
						n.m_segment->flags_u.flags_s.m_end_of_line = true;
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
   124
						Yapf().m_stopped_on_first_two_way_signal = true;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   125
						return -1;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
					}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   127
					SignalType sig_type = GetSignalType(tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   128
					n.m_last_red_signal_type = sig_type;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   129
					n.flags_u.flags_s.m_last_signal_was_red = true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   130
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   131
					// look-ahead signal penalty
5095
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   132
					if (look_ahead_cost > 0) {
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   133
						// add the look ahead penalty only if it is positive
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   134
						cost += look_ahead_cost;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   135
					}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   136
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   137
					// special signal penalties
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   138
					if (n.m_num_signals_passed == 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   139
						switch (sig_type) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   140
							case SIGTYPE_COMBO:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   141
							case SIGTYPE_EXIT:   cost += Yapf().PfGetSettings().rail_firstred_exit_penalty; break; // first signal is red pre-signal-exit
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   142
							case SIGTYPE_NORMAL:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   143
							case SIGTYPE_ENTRY:  cost += Yapf().PfGetSettings().rail_firstred_penalty; break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   144
						};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   145
					}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   146
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   147
				n.m_num_signals_passed++;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   148
				n.m_segment->m_last_signal_tile = tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   149
				n.m_segment->m_last_signal_td = trackdir;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   151
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
		return cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   155
	FORCEINLINE int PlatformLengthPenalty(int platform_length)
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   156
	{
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   157
		int cost = 0;
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   158
		const Vehicle* v = Yapf().GetVehicle();
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   159
		assert(v != NULL);
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   160
		assert(v->type == VEH_Train);
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   161
		assert(v->u.rail.cached_total_length != 0);
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   162
		int needed_platform_length = (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE;
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   163
		if (platform_length > needed_platform_length) {
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   164
			// apply penalty for longer platform than needed
3932
882af4997b60 (svn r5067) -Codechange: [YAPF] Platform selection feature now applies penalty per platform instead of per tile (should give better results)
KUDr
parents: 3931
diff changeset
   165
			cost += Yapf().PfGetSettings().rail_longer_platform_penalty;
882af4997b60 (svn r5067) -Codechange: [YAPF] Platform selection feature now applies penalty per platform instead of per tile (should give better results)
KUDr
parents: 3931
diff changeset
   166
		} else if (needed_platform_length > platform_length) {
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   167
			// apply penalty for shorter platform than needed
3932
882af4997b60 (svn r5067) -Codechange: [YAPF] Platform selection feature now applies penalty per platform instead of per tile (should give better results)
KUDr
parents: 3931
diff changeset
   168
			cost += Yapf().PfGetSettings().rail_shorter_platform_penalty;
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   169
		}
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   170
		return cost;
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   171
	}
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   172
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   174
	FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   175
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   176
	/** Called by YAPF to calculate the cost from the origin to the given node.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3979
diff changeset
   177
	 *  Calculates only the cost of given node, adds it to the parent node cost
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3979
diff changeset
   178
	 *  and stores the result into Node::m_cost member */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
	FORCEINLINE bool PfCalcCost(Node& n)
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
		assert(!n.flags_u.flags_s.m_targed_seen);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
		CPerfStart perf_cost(Yapf().m_perf_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
		int parent_cost = (n.m_parent != NULL) ? n.m_parent->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
   184
		int first_tile_cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   185
		int segment_cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   186
		int extra_cost = 0;
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   187
		const Vehicle* v = Yapf().GetVehicle();
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   188
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   189
		// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   190
		TileIndex prev_tile      = (n.m_parent != NULL) ? n.m_parent->GetLastTile() : INVALID_TILE;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   191
		Trackdir  prev_trackdir  = (n.m_parent != NULL) ? n.m_parent->GetLastTrackdir() : INVALID_TRACKDIR;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   192
		TileType  prev_tile_type = (n.m_parent != NULL) ? GetTileType(n.m_parent->GetLastTile()) : MP_VOID;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   193
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   194
		TileIndex tile = n.m_key.m_tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   195
		Trackdir trackdir = n.m_key.m_td;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   196
		TileType tile_type = GetTileType(tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   197
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5643
diff changeset
   198
		RailType rail_type = GetTileRailType(tile, TrackdirToTrack(trackdir));
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   199
5609
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   200
		// detect exit from bridge wormhole
5627
f5c656cf0a0e (svn r7729) [cbh] - Fix: [YAPF] several cbh related issues. YAPF should now work with cbh
KUDr
parents: 5626
diff changeset
   201
		Trackdir intermediate_trackdir = INVALID_TRACKDIR;
5609
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   202
		if (IsBridgeTile(tile) && TrackdirToExitdir(ReverseTrackdir(trackdir)) == GetBridgeRampDirection(tile)) {
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   203
			// we are jumping over bridge (possible now with custom bridge heads) we must add the cost of skipped tiles
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   204
			int skipped_tiles = DistanceManhattan(prev_tile, tile) - 1;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   205
			if (skipped_tiles > 0) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   206
				segment_cost += skipped_tiles * YAPF_TILE_LENGTH;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   207
				DEBUG(yapf, 6, "    Cost: skipped=%d", skipped_tiles * YAPF_TILE_LENGTH);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   208
				intermediate_trackdir = DiagdirToDiagTrackdir(ReverseDiagDir(GetBridgeRampDirection(tile)));
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   209
			}
5609
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   210
		}
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   211
3930
635b28d85743 (svn r5065) -CodeChange: [YAPF] Added PfDetectDestination(tile, trackdir) for trains (to be used by platform selection feature)
KUDr
parents: 3915
diff changeset
   212
		bool target_seen = Yapf().PfDetectDestination(tile, trackdir);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   213
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   214
		while (true) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   215
			int tile_cost = Yapf().OneTileCost(tile, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   216
			int curve_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   217
			if (intermediate_trackdir == INVALID_TRACKDIR) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   218
				curve_cost = Yapf().CurveCost(prev_trackdir, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   219
			} else {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   220
				curve_cost = Yapf().CurveCost(prev_trackdir, intermediate_trackdir) + Yapf().CurveCost(intermediate_trackdir, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   221
				intermediate_trackdir = INVALID_TRACKDIR;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   222
			}
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   223
			int slope_cost = Yapf().SlopeCost(tile, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   224
			int signal_cost = Yapf().SignalCost(n, tile, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   225
			DEBUG(yapf, 6, "    Cost: tile=%d, curve=%d, slope=%d, signal=%d", tile_cost, curve_cost, slope_cost, signal_cost);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   226
			segment_cost += tile_cost + curve_cost + slope_cost + signal_cost;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   227
			if (n.m_segment->flags_u.flags_s.m_end_of_line) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   228
				DEBUG(yapf, 4, "  end: EOL (signal)");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   229
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   230
			}
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
			// finish if we have reached the destination
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   233
			if (target_seen) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   234
				DEBUG(yapf, 4, "  end: target_seen");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   235
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   236
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   237
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
			// finish on first station tile - segment should end here to avoid target skipping
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   239
			// when cached segments are used
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   240
			if (tile_type == MP_STATION && prev_tile_type != MP_STATION) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   241
				DEBUG(yapf, 4, "  end: first station tile");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   242
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   243
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   244
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   245
			// finish also on waypoint - same workaround as for first station tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   246
			if (tile_type == MP_RAILWAY && IsRailWaypoint(tile)) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   247
				DEBUG(yapf, 4, "  end: waypoint");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   248
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   249
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   250
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   251
			// if there are no reachable trackdirs on the next tile, we have end of road
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   252
			TrackFollower F(v, &Yapf().m_perf_ts_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   253
			if (!F.Follow(tile, trackdir)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   254
				// we can't continue?
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   255
				// n.m_segment->flags_u.flags_s.m_end_of_line = true;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   256
				DEBUG(yapf, 4, "  end: can't foolow (dead end?)");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   257
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   258
			}
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   259
			if (F.m_is_bridge && F.m_tiles_skipped > 0) intermediate_trackdir = F.m_intermediate_trackdir;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   260
			DEBUG(yapf, 4, " next: tile=%04X td=%s", F.m_new_tile, GetTrackdirBitsName(F.m_new_td_bits).Data());
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   261
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   262
			// if there are more trackdirs available & reachable, we are at the end of segment
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   263
			if (KillFirstBit2x64(F.m_new_td_bits) != 0) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   264
				DEBUG(yapf, 4, "  end: choice");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   265
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   266
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   267
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   268
			Trackdir new_td = (Trackdir)FindFirstBit2x64(F.m_new_td_bits);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   269
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   270
			{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   271
				// end segment if train is about to enter simple loop with no junctions
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   272
				// so next time it should stop on the next if
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   273
				if (segment_cost > s_max_segment_cost && IsTileType(F.m_new_tile, MP_RAILWAY)) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   274
					DEBUG(yapf, 4, "  end: loop fuse");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   275
					break;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   276
				}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   277
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   278
				// stop if train is on simple loop with no junctions
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   279
				if (F.m_new_tile == n.m_key.m_tile && new_td == n.m_key.m_td) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   280
					DEBUG(yapf, 4, "  end: loop detected");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   281
					return false;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   282
				}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   283
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   284
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   285
			// if rail type changes, finish segment (cached segment can't contain more rail types)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   286
			{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5643
diff changeset
   287
				RailType new_rail_type = GetTileRailType(F.m_new_tile, TrackdirToTrack(FindFirstTrackdir(F.m_new_td_bits)));
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   288
				if (new_rail_type != rail_type) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   289
					DEBUG(yapf, 4, "  end: rail type changes");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   290
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   291
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   292
				rail_type = new_rail_type;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   293
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   294
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   295
			// move to the next tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   296
			prev_tile = tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   297
			prev_trackdir = trackdir;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   298
			prev_tile_type = tile_type;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   299
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   300
			tile = F.m_new_tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   301
			trackdir = new_td;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   302
			tile_type = GetTileType(tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   303
3930
635b28d85743 (svn r5065) -CodeChange: [YAPF] Added PfDetectDestination(tile, trackdir) for trains (to be used by platform selection feature)
KUDr
parents: 3915
diff changeset
   304
			target_seen = Yapf().PfDetectDestination(tile, trackdir);
635b28d85743 (svn r5065) -CodeChange: [YAPF] Added PfDetectDestination(tile, trackdir) for trains (to be used by platform selection feature)
KUDr
parents: 3915
diff changeset
   305
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   306
			// reversing in depot penalty
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   307
			if (tile == prev_tile) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   308
				int reverse_in_depot_cost = Yapf().PfGetSettings().rail_depot_reverse_penalty;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   309
				segment_cost += reverse_in_depot_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   310
				DEBUG(yapf, 4, "  end: reversing in depot (cost=%d)", reverse_in_depot_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   311
				break;
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
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   314
			// if we skipped some tunnel tiles, add their cost
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   315
			segment_cost += YAPF_TILE_LENGTH * F.m_tiles_skipped;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   316
			DEBUG(yapf, 6, "    Cost: wormhole=%d", YAPF_TILE_LENGTH * F.m_tiles_skipped);
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   317
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   318
			// add penalty for skipped station tiles
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   319
			if (F.m_is_station)
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   320
			{
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   321
				if (target_seen) {
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   322
					// it is our destination station
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   323
					uint platform_length = F.m_tiles_skipped + 1;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   324
					int platform_length_cost = PlatformLengthPenalty(platform_length);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   325
					segment_cost += platform_length_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   326
					DEBUG(yapf, 6, "    Cost: platform=%d", platform_length_cost);
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   327
				} else {
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   328
					// station is not our destination station, apply penalty for skipped platform tiles
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   329
					int station_cost = Yapf().PfGetSettings().rail_station_penalty * F.m_tiles_skipped;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   330
					segment_cost += station_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   331
					DEBUG(yapf, 6, "    Cost: station=%d", station_cost);
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   332
				}
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   333
			}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   334
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   335
			// add min/max speed penalties
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   336
			int min_speed = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   337
			int max_speed = F.GetSpeedLimit(&min_speed);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   338
			if (max_speed < v->max_speed)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   339
				segment_cost += YAPF_TILE_LENGTH * (v->max_speed - max_speed) / v->max_speed;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   340
			if (min_speed > v->max_speed)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   341
				segment_cost += YAPF_TILE_LENGTH * (min_speed - v->max_speed);
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
			// finish if we already exceeded the maximum cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   344
			if (m_max_cost > 0 && (parent_cost + first_tile_cost + segment_cost) > m_max_cost) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   345
				DEBUG(yapf, 4, "  abort: maximum cost reached");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   346
				return false;
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
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   349
			if (first_tile_cost == 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   350
				// we just have done first tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   351
				first_tile_cost = segment_cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   352
				segment_cost = 0;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   353
				DEBUG(yapf, 5, "    TotalCost: first_tile=%d", first_tile_cost);
3900
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
				// look if we can reuse existing (cached) segment cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   356
				if (n.m_segment->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
   357
					// reuse the cached segment cost
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   358
					DEBUG(yapf, 4, "  end: reusing the cached segment cost");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   359
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   360
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   361
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   362
			// segment cost was not filled yes, we have not cached it yet
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   363
			n.SetLastTileTrackdir(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   364
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   365
		} // while (true)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   366
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   367
		if (first_tile_cost == 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   368
			// we have just finished first tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   369
			first_tile_cost = segment_cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   370
			segment_cost = 0;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   371
			DEBUG(yapf, 5, "    TotalCost: first_tile=%d", first_tile_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   372
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   373
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   374
		// do we have cached segment cost?
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   375
		if (n.m_segment->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
   376
			// reuse the cached segment cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   377
			segment_cost = n.m_segment->m_cost;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   378
			DEBUG(yapf, 5, "    TotalCost: cached_segment=%d", segment_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   379
		} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   380
			// save segment cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   381
			n.m_segment->m_cost = segment_cost;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   382
			DEBUG(yapf, 5, "    TotalCost: segment=%d", segment_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   383
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   384
			// save end of segment back to the node
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   385
			n.SetLastTileTrackdir(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   386
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   387
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   388
		// special costs for the case we have reached our target
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   389
		if (target_seen) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   390
			n.flags_u.flags_s.m_targed_seen = true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   391
			if (n.flags_u.flags_s.m_last_signal_was_red) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   392
				if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   393
					// last signal was red pre-signal-exit
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   394
					extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   395
				} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   396
					// last signal was red, but not exit
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   397
					extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   398
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   399
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   400
		}
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   401
		DEBUG(yapf, 5, "    TotalCost: extra=%d", extra_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   402
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   403
		// total node cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   404
		n.m_cost = parent_cost + first_tile_cost + segment_cost + extra_cost;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   405
		DEBUG(yapf, 3, "  leaving: last_tile=%04X, last_td=%s, cost=%d", n.m_segment->m_last_tile, GetTrackdirName(n.m_segment->m_last_td), n.m_cost);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   406
		DEBUG(yapf, 3, " returning %s", n.m_segment->flags_u.flags_s.m_end_of_line ? "false" : "true");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   407
		return !n.m_segment->flags_u.flags_s.m_end_of_line;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   408
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   409
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   410
	FORCEINLINE bool CanUseGlobalCache(Node& n) const
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   411
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   412
		return (n.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
   413
			&& (n.m_parent->m_num_signals_passed >= m_sig_look_ahead_costs.Size());
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   414
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   415
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   416
	FORCEINLINE void ConnectNodeToCachedData(Node& n, CachedData& ci)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   417
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   418
		n.m_segment = &ci;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   419
		if (n.m_segment->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
   420
			n.m_segment->m_last_tile = n.m_key.m_tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   421
			n.m_segment->m_last_td = n.m_key.m_td;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   422
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   423
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   424
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   425
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   426
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   427
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   428
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   429
#endif /* YAPF_COSTRAIL_HPP */