src/pbs.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10210 a542a6d595fc
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
9786
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     1
/* $Id$ */
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     2
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     3
/** @file pbs.cpp */
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     4
#include "stdafx.h"
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     5
#include "openttd.h"
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     6
#include "pbs.h"
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     7
#include "rail_map.h"
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     8
#include "road_map.h"
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
     9
#include "station_map.h"
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    10
#include "tunnelbridge_map.h"
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    11
#include "functions.h"
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    12
#include "debug.h"
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    13
#include "direction_func.h"
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    14
#include "settings_type.h"
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
    15
#include "road_func.h"
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
    16
#include "vehicle_base.h"
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
    17
#include "vehicle_func.h"
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
    18
#include "yapf/follow_track.hpp"
9786
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    19
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    20
/**
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    21
 * Get the reserved trackbits for any tile, regardless of type.
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    22
 * @param t the tile
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    23
 * @return the reserved trackbits. TRACK_BIT_NONE on nothing reserved or
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    24
 *     a tile without rail.
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    25
 */
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    26
TrackBits GetReservedTrackbits(TileIndex t)
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    27
{
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    28
	switch (GetTileType(t)) {
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    29
		case MP_RAILWAY:
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    30
			if (IsRailWaypoint(t) || IsRailDepot(t)) return GetRailWaypointReservation(t);
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    31
			if (IsPlainRailTile(t)) return GetTrackReservation(t);
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    32
			break;
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    33
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    34
		case MP_ROAD:
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    35
			if (IsLevelCrossing(t)) return GetRailCrossingReservation(t);
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    36
			break;
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    37
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    38
		case MP_STATION:
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    39
			if (IsRailwayStation(t)) return GetRailStationReservation(t);
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    40
			break;
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    41
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    42
		case MP_TUNNELBRIDGE:
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    43
			if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) return GetRailTunnelBridgeReservation(t);
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    44
			break;
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    45
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    46
		default:
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    47
			break;
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    48
	}
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    49
	return TRACK_BIT_NONE;
0169b327f3d2 (svn r13928) -Add [YAPP]: Function for getting the path reservation state of any tile. (michi_cc)
rubidium
parents:
diff changeset
    50
}
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    51
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    52
/**
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    53
 * Set the reservation for a complete station platform.
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    54
 * @pre IsRailwayStationTile(start)
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    55
 * @param start starting tile of the platform
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    56
 * @param dir the direction in which to follow the platform
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    57
 * @param b the state the reservation should be set to
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    58
 */
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    59
void SetRailwayStationPlatformReservation(TileIndex start, DiagDirection dir, bool b)
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    60
{
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    61
	TileIndex     tile = start;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    62
	TileIndexDiff diff = TileOffsByDiagDir(dir);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    63
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    64
	assert(IsRailwayStationTile(start));
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    65
	assert(GetRailStationAxis(start) == DiagDirToAxis(dir));
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    66
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    67
	do {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    68
		SetRailwayStationReservation(tile, b);
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
    69
		MarkTileDirtyByTile(tile);
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    70
		tile = TILE_ADD(tile, diff);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    71
	} while (IsCompatibleTrainStationTile(tile, start));
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    72
}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    73
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    74
/**
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    75
 * Try to reserve a specific track on a tile
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    76
 * @param tile the tile
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    77
 * @param t the track
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    78
 * @return true if reservation was successfull, i.e. the track was
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    79
 *     free and didn't cross any other reserved tracks.
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    80
 */
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    81
bool TryReserveRailTrack(TileIndex tile, Track t)
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    82
{
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    83
	assert((GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & TrackToTrackBits(t)) != 0);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    84
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    85
	if (_settings_client.gui.show_track_reservation) {
9954
a9ea85501b3b (svn r14109) -Fix (r14107): marking reserved tile dirty is needed for waypoints and stations even when patch option is disabled
smatz
parents: 9952
diff changeset
    86
		/* show the reserved rail if needed */
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    87
		MarkTileDirtyByTile(tile);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    88
	}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    89
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    90
	switch (GetTileType(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    91
		case MP_RAILWAY:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    92
			if (IsPlainRailTile(tile)) return TryReserveTrack(tile, t);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    93
			if (IsRailWaypoint(tile) || IsRailDepot(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    94
				if (!GetDepotWaypointReservation(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    95
					SetDepotWaypointReservation(tile, true);
9954
a9ea85501b3b (svn r14109) -Fix (r14107): marking reserved tile dirty is needed for waypoints and stations even when patch option is disabled
smatz
parents: 9952
diff changeset
    96
					MarkTileDirtyByTile(tile); // some GRFs change their appearance when tile is reserved
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    97
					return true;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    98
				}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
    99
			}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   100
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   101
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   102
		case MP_ROAD:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   103
			if (IsLevelCrossing(tile) && !GetCrossingReservation(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   104
				SetCrossingReservation(tile, true);
9824
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9823
diff changeset
   105
				BarCrossing(tile);
9954
a9ea85501b3b (svn r14109) -Fix (r14107): marking reserved tile dirty is needed for waypoints and stations even when patch option is disabled
smatz
parents: 9952
diff changeset
   106
				MarkTileDirtyByTile(tile); // crossing barred, make tile dirty
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   107
				return true;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   108
			}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   109
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   110
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   111
		case MP_STATION:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   112
			if (IsRailwayStation(tile) && !GetRailwayStationReservation(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   113
				SetRailwayStationReservation(tile, true);
9954
a9ea85501b3b (svn r14109) -Fix (r14107): marking reserved tile dirty is needed for waypoints and stations even when patch option is disabled
smatz
parents: 9952
diff changeset
   114
				MarkTileDirtyByTile(tile); // some GRFs need redraw after reserving track
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   115
				return true;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   116
			}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   117
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   118
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   119
		case MP_TUNNELBRIDGE:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   120
			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL && !GetRailTunnelBridgeReservation(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   121
				SetTunnelBridgeReservation(tile, true);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   122
				return true;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   123
			}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   124
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   125
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   126
		default:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   127
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   128
	}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   129
	return false;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   130
}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   131
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   132
/**
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   133
 * Lift the reservation of a specific track on a tile
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   134
 * @param tile the tile
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   135
 * @param t the track
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   136
 */
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   137
 void UnreserveRailTrack(TileIndex tile, Track t)
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   138
{
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   139
	assert((GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & TrackToTrackBits(t)) != 0);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   140
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   141
	if (_settings_client.gui.show_track_reservation) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   142
		MarkTileDirtyByTile(tile);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   143
	}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   144
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   145
	switch (GetTileType(tile)) {
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   146
		case MP_RAILWAY:
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   147
			if (IsRailWaypoint(tile) || IsRailDepot(tile)) {
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   148
				SetDepotWaypointReservation(tile, false);
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   149
				MarkTileDirtyByTile(tile);
9954
a9ea85501b3b (svn r14109) -Fix (r14107): marking reserved tile dirty is needed for waypoints and stations even when patch option is disabled
smatz
parents: 9952
diff changeset
   150
				break;
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   151
			}
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   152
			if (IsPlainRailTile(tile)) UnreserveTrack(tile, t);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   153
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   154
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   155
		case MP_ROAD:
9824
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9823
diff changeset
   156
			if (IsLevelCrossing(tile)) {
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9823
diff changeset
   157
				SetCrossingReservation(tile, false);
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9823
diff changeset
   158
				UpdateLevelCrossing(tile);
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9823
diff changeset
   159
			}
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   160
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   161
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   162
		case MP_STATION:
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   163
			if (IsRailwayStation(tile)) {
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   164
				SetRailwayStationReservation(tile, false);
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   165
				MarkTileDirtyByTile(tile);
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9814
diff changeset
   166
			}
9787
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   167
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   168
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   169
		case MP_TUNNELBRIDGE:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   170
			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) SetTunnelBridgeReservation(tile, false);
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   171
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   172
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   173
		default:
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   174
			break;
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   175
	}
cedb26977f52 (svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
rubidium
parents: 9786
diff changeset
   176
}
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   177
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   178
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   179
/** Follow a reservation starting from a specific tile to the end. */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   180
static PBSTileInfo FollowReservation(Owner o, RailTypes rts, TileIndex tile, Trackdir trackdir, bool ignore_oneway = false)
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   181
{
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   182
	/* Do not disallow 90 deg turns as the setting might have changed between reserving and now. */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   183
	CFollowTrackRail ft(o, rts);
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   184
	while (ft.Follow(tile, trackdir)) {
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   185
		TrackdirBits reserved = (TrackdirBits)(ft.m_new_td_bits & (GetReservedTrackbits(ft.m_new_tile) * 0x101));
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   186
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   187
		/* No reservation --> path end found */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   188
		if (reserved == TRACKDIR_BIT_NONE) break;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   189
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   190
		/* Can't have more than one reserved trackdir */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   191
		Trackdir new_trackdir = FindFirstTrackdir(reserved);
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   192
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   193
		/* One-way signal against us. The reservation can't be ours as it is not
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   194
		 * a safe position from our direction and we can never pass the signal. */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   195
		if (!ignore_oneway && HasOnewaySignalBlockingTrackdir(ft.m_new_tile, new_trackdir)) break;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   196
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   197
		tile = ft.m_new_tile;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   198
		trackdir = new_trackdir;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   199
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   200
		/* Depot tile? Can't continue. */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   201
		if (IsRailDepotTile(tile)) break;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   202
		/* Non-pbs signal? Reservation can't continue. */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   203
		if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) break;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   204
	}
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   205
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   206
	return PBSTileInfo(tile, trackdir, false);
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   207
}
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   208
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   209
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   210
 * Helper struct for finding the best matching vehicle on a specific track.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   211
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   212
struct FindTrainOnTrackInfo {
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   213
	PBSTileInfo res; ///< Information about the track.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   214
	Vehicle *best;   ///< The currently "best" vehicle we have found.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   215
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   216
	/** Init the best location to NULL always! */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   217
	FindTrainOnTrackInfo() : best(NULL) {}
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   218
};
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   219
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   220
/** Callback for Has/FindVehicleOnPos to find a train on a specific track. */
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   221
static Vehicle *FindTrainOnTrackEnum(Vehicle *v, void *data)
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   222
{
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   223
	FindTrainOnTrackInfo *info = (FindTrainOnTrackInfo *)data;
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   224
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   225
	if (v->type == VEH_TRAIN && !(v->vehstatus & VS_CRASHED) && HasBit((TrackBits)v->u.rail.track, TrackdirToTrack(info->res.trackdir))) {
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   226
		v = v->First();
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   227
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   228
		/* ALWAYS return the lowest ID (anti-desync!) */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   229
		if (info->best == NULL || v->index < info->best->index) info->best = v;
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   230
		return v;
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   231
	}
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   232
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   233
	return NULL;
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   234
}
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   235
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   236
/**
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   237
 * Follow a train reservation to the last tile.
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   238
 *
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   239
 * @param v the vehicle
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9824
diff changeset
   240
 * @param train_on_res Is set to a train we might encounter
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   241
 * @returns The last tile of the reservation or the current train tile if no reservation present.
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   242
 */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   243
PBSTileInfo FollowTrainReservation(const Vehicle *v, bool *train_on_res)
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   244
{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   245
	assert(v->type == VEH_TRAIN);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   246
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   247
	TileIndex tile = v->tile;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   248
	Trackdir  trackdir = GetVehicleTrackdir(v);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   249
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   250
	if (IsRailDepotTile(tile) && !GetRailDepotReservation(tile)) return PBSTileInfo(tile, trackdir, false);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   251
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   252
	FindTrainOnTrackInfo ftoti;
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   253
	ftoti.res = FollowReservation(v->owner, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes, tile, trackdir);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   254
	ftoti.res.okay = IsSafeWaitingPosition(v, ftoti.res.tile, ftoti.res.trackdir, true, _settings_game.pf.forbid_90_deg);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   255
	if (train_on_res != NULL) *train_on_res = HasVehicleOnPos(ftoti.res.tile, &ftoti, FindTrainOnTrackEnum);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   256
	return ftoti.res;
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   257
}
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   258
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   259
/**
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   260
 * Find the train which has reserved a specific path.
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   261
 *
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   262
 * @param tile A tile on the path.
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   263
 * @param track A reserved track on the tile.
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   264
 * @return The vehicle holding the reservation or NULL if the path is stray.
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   265
 */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   266
Vehicle *GetTrainForReservation(TileIndex tile, Track track)
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   267
{
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   268
	assert(HasReservedTracks(tile, TrackToTrackBits(track)));
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   269
	Trackdir  trackdir = TrackToTrackdir(track);
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   270
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   271
	RailTypes rts = GetRailTypeInfo(GetTileRailType(tile))->compatible_railtypes;
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   272
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   273
	/* Follow the path from tile to both ends, one of the end tiles should
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   274
	 * have a train on it. We need FollowReservation to ignore one-way signals
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   275
	 * here, as one of the two search directions will be the "wrong" way. */
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   276
	for (int i = 0; i < 2; ++i, trackdir = ReverseTrackdir(trackdir)) {
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   277
		FindTrainOnTrackInfo ftoti;
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   278
		ftoti.res = FollowReservation(GetTileOwner(tile), rts, tile, trackdir, true);
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   279
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   280
		FindVehicleOnPos(ftoti.res.tile, &ftoti, FindTrainOnTrackEnum);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   281
		if (ftoti.best != NULL) return ftoti.best;
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   282
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   283
		/* Special case for stations: check the whole platform for a vehicle. */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   284
		if (IsRailwayStationTile(ftoti.res.tile)) {
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   285
			TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti.res.trackdir)));
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   286
			for (TileIndex st_tile = ftoti.res.tile + diff; IsCompatibleTrainStationTile(st_tile, ftoti.res.tile); st_tile += diff) {
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   287
				FindVehicleOnPos(st_tile, &ftoti, FindTrainOnTrackEnum);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   288
				if (ftoti.best != NULL) return ftoti.best;
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   289
			}
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   290
		}
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   291
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   292
		/* Special case for bridges/tunnels: check the other end as well. */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   293
		if (IsTileType(ftoti.res.tile, MP_TUNNELBRIDGE)) {
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   294
			FindVehicleOnPos(GetOtherTunnelBridgeEnd(ftoti.res.tile), &ftoti, FindTrainOnTrackEnum);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9954
diff changeset
   295
			if (ftoti.best != NULL) return ftoti.best;
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   296
		}
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   297
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   298
9814
19720a320565 (svn r13956) -Add [YAPP]: Function for getting the train holding a reserved path. (michi_cc)
rubidium
parents: 9798
diff changeset
   299
	return NULL;
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   300
}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   301
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   302
/**
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   303
 * Determine whether a certain track on a tile is a safe position to end a path.
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   304
 *
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   305
 * @param v the vehicle to test for
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   306
 * @param tile The tile
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   307
 * @param trackdir The trackdir to test
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   308
 * @param include_line_end Should end-of-line tiles be considered safe?
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   309
 * @param forbid_90def Don't allow trains to make 90 degree turns
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   310
 * @return True if it is a safe position
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   311
 */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   312
bool IsSafeWaitingPosition(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   313
{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   314
	if (IsRailDepotTile(tile)) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   315
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   316
	if (IsTileType(tile, MP_RAILWAY)) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   317
		/* For non-pbs signals, stop on the signal tile. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   318
		if (HasSignalOnTrackdir(tile, trackdir) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   319
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   320
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   321
	/* Check next tile. For perfomance reasons, we check for 90 degree turns ourself. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   322
	CFollowTrackRail ft(v, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   323
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   324
	/* End of track? */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   325
	if (!ft.Follow(tile, trackdir)) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   326
		/* Last tile of a terminus station is a safe position. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   327
		if (include_line_end) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   328
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   329
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   330
	/* Check for reachable tracks. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   331
	ft.m_new_td_bits &= DiagdirReachesTrackdirs(ft.m_exitdir);
10210
a542a6d595fc (svn r14428) -Fix [FS#2306]: A 90-degree curve can be a safe waiting position if they are forbidden for trains.
michi_cc
parents: 10083
diff changeset
   332
	if (forbid_90deg) ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(trackdir);
9798
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   333
	if (ft.m_new_td_bits == TRACKDIR_BIT_NONE) return include_line_end;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   334
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   335
	if (ft.m_new_td_bits != TRACKDIR_BIT_NONE && KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   336
		/* PBS signal on next trackdir? Safe position. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   337
		if (HasPbsSignalOnTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   338
	}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   339
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   340
	return false;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   341
}
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   342
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   343
/**
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   344
 * Check if a safe position is free.
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   345
 *
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   346
 * @param v the vehicle to test for
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   347
 * @param tile The tile
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   348
 * @param trackdir The trackdir to test
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   349
 * @param forbid_90def Don't allow trains to make 90 degree turns
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   350
 * @return True if the position is free
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   351
 */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   352
bool IsWaitingPositionFree(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   353
{
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   354
	Track     track = TrackdirToTrack(trackdir);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   355
	TrackBits reserved = GetReservedTrackbits(tile);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   356
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   357
	/* Tile reserved? Can never be a free waiting position. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   358
	if (TrackOverlapsTracks(reserved, track)) return false;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   359
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   360
	/* Not reserved and depot or not a pbs signal -> free. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   361
	if (IsRailDepotTile(tile)) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   362
	if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir) && !IsPbsSignal(GetSignalType(tile, track))) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   363
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   364
	/* Check the next tile, if it's a PBS signal, it has to be free as well. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   365
	CFollowTrackRail ft(v, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   366
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   367
	if (!ft.Follow(tile, trackdir)) return true;
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   368
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   369
	/* Check for reachable tracks. */
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   370
	ft.m_new_td_bits &= DiagdirReachesTrackdirs(ft.m_exitdir);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   371
	if (forbid_90deg) ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(trackdir);
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   372
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   373
	return !HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(ft.m_new_td_bits));
4b8cdc1adf4b (svn r13940) -Add [YAPP]: YAPF is now able to reserve the found path. (michi_cc)
rubidium
parents: 9787
diff changeset
   374
}