/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
 */

/** @file train_cmd.cpp Handling of trains. */

#include "stdafx.h"
#include "error.h"
#include "articulated_vehicles.h"
#include "command_func.h"
#include "error_func.h"
#include "pathfinder/yapf/yapf.hpp"
#include "news_func.h"
#include "company_func.h"
#include "newgrf_sound.h"
#include "newgrf_text.h"
#include "strings_func.h"
#include "viewport_func.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "newgrf_station.h"
#include "effectvehicle_func.h"
#include "network/network.h"
#include "core/random_func.hpp"
#include "company_base.h"
#include "newgrf.h"
#include "order_backup.h"
#include "zoom_func.h"
#include "newgrf_debug.h"
#include "framerate_type.h"
#include "train_cmd.h"
#include "misc_cmd.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"

#include "table/strings.h"
#include "table/train_sprites.h"

#include "safeguards.h"

static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck);
static bool TrainCheckIfLineEnds(Train *v, bool reverse = true);
bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // Also used in vehicle_sl.cpp.
static TileIndex TrainApproachingCrossingTile(const Train *v);
static void CheckIfTrainNeedsService(Train *v);
static void CheckNextTrainTile(Train *v);

/** Initial x subtile coordinate of rail vehicles for each direction. */
static constexpr DiagDirectionIndexArray<uint8_t> _vehicle_initial_x_fract{10, 8, 4,  8};
/** Initial y subtile coordinate of rail vehicles for each direction. */
static constexpr DiagDirectionIndexArray<uint8_t> _vehicle_initial_y_fract{ 8, 4, 8, 10};

/** @copydoc IsValidImageIndex */
template <>
bool IsValidImageIndex<VehicleType::Train>(uint8_t image_index)
{
	return image_index < lengthof(_engine_sprite_base);
}


/**
 * Return the cargo weight multiplier to use for a rail vehicle
 * @param cargo Cargo type to get multiplier for
 * @return Cargo weight multiplier
 */
uint8_t FreightWagonMult(CargoType cargo)
{
	if (!CargoSpec::Get(cargo)->is_freight) return 1;
	return _settings_game.vehicle.freight_trains;
}

/** Checks if lengths of all rail vehicles are valid. If not, shows an error message. */
void CheckTrainsLengths()
{
	bool first = true;

	for (const Train *v : Train::Iterate()) {
		if (v->First() == v && !v->vehstatus.Test(VehState::Crashed)) {
			for (const Train *u = v->GetMovingFront(), *w = v->GetMovingNext(); w != nullptr; u = w, w = w->GetMovingNext()) {
				if (u->track != Track::Depot) {
					if ((w->track != Track::Depot &&
							std::max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->CalcNextVehicleOffset()) ||
							(w->track == Track::Depot && TicksToLeaveDepot(u) <= 0)) {
						ShowErrorMessage(GetEncodedString(STR_BROKEN_VEHICLE_LENGTH, v->index, v->owner), {}, WarningLevel::Critical);

						if (!_networking && first) {
							first = false;
							Command<Commands::Pause>::Post(PauseMode::Error, true);
						}
						/* Break so we warn only once for each train. */
						break;
					}
				}
			}
		}
	}
}

/**
 * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
 * to/removed from the chain, and when the game is loaded.
 * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
 * @param allowed_changes Stuff that is allowed to change.
 */
void Train::ConsistChanged(ConsistChangeFlags allowed_changes)
{
	uint16_t max_speed = UINT16_MAX;

	assert(this->IsFrontEngine() || this->IsFreeWagon());

	const RailVehicleInfo *rvi_v = RailVehInfo(this->engine_type);
	EngineID first_engine = this->IsFrontEngine() ? this->engine_type : EngineID::Invalid();
	this->gcache.cached_total_length = 0;
	this->compatible_railtypes = {};

	bool train_can_tilt = true;
	int16_t min_curve_speed_mod = INT16_MAX;

	for (Train *u = this; u != nullptr; u = u->Next()) {
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);

		/* Check the this->first cache. */
		assert(u->First() == this);

		/* update the 'first engine' */
		u->gcache.first_engine = this == u ? EngineID::Invalid() : first_engine;
		u->railtypes = rvi_u->railtypes;

		if (u->IsEngine()) first_engine = u->engine_type;

		/* Set user defined data to its default value */
		u->tcache.user_def_data = rvi_u->user_def_data;
		this->InvalidateNewGRFCache();
		u->InvalidateNewGRFCache();
	}

	for (Train *u = this; u != nullptr; u = u->Next()) {
		/* Update user defined data (must be done before other properties) */
		u->tcache.user_def_data = GetVehicleProperty(u, PROP_TRAIN_USER_DATA, u->tcache.user_def_data);
		this->InvalidateNewGRFCache();
		u->InvalidateNewGRFCache();
	}

	for (Train *u = this; u != nullptr; u = u->Next()) {
		const Engine *e_u = u->GetEngine();
		const RailVehicleInfo *rvi_u = &e_u->VehInfo<RailVehicleInfo>();

		if (!e_u->info.misc_flags.Test(EngineMiscFlag::RailTilts)) train_can_tilt = false;
		min_curve_speed_mod = std::min(min_curve_speed_mod, u->GetCurveSpeedModifier());

		/* Cache wagon override sprite group. nullptr is returned if there is none */
		u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->gcache.first_engine);

		/* Reset colour map */
		u->colourmap = PAL_NONE;

		/* Update powered-wagon-status and visual effect */
		u->UpdateVisualEffect(true);

		if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RailVehicleType::Wagon &&
				UsesWagonOverride(u) && !HasBit(u->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER)) {
			/* wagon is powered */
			u->flags.Set(VehicleRailFlag::PoweredWagon); // cache 'powered' status
		} else {
			u->flags.Reset(VehicleRailFlag::PoweredWagon);
		}

		if (!u->IsArticulatedPart()) {
			/* Do not count powered wagons for the compatible railtypes, as wagons always
			   have railtype normal */
			if (rvi_u->power > 0) {
				this->compatible_railtypes.Set(GetAllPoweredRailTypes(u->railtypes));
			}

			/* Some electric engines can be allowed to run on normal rail. It happens to all
			 * existing electric engines when elrails are disabled and then re-enabled */
			if (u->flags.Test(VehicleRailFlag::AllowedOnNormalRail)) {
				u->railtypes.Set(RAILTYPE_RAIL);
				u->compatible_railtypes.Set(RAILTYPE_RAIL);
			}

			/* max speed is the minimum of the speed limits of all vehicles in the consist */
			if ((rvi_u->railveh_type != RailVehicleType::Wagon || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) {
				uint16_t speed = GetVehicleProperty(u, PROP_TRAIN_SPEED, rvi_u->max_speed);
				if (speed != 0) max_speed = std::min(speed, max_speed);
			}
		}

		uint16_t new_cap = e_u->DetermineCapacity(u);
		if (allowed_changes.Test(ConsistChangeFlag::Capacity)) {
			/* Update vehicle capacity. */
			if (u->cargo_cap > new_cap) u->cargo.Truncate(new_cap);
			u->refit_cap = std::min(new_cap, u->refit_cap);
			u->cargo_cap = new_cap;
		} else {
			/* Verify capacity hasn't changed. */
			if (new_cap != u->cargo_cap) ShowNewGrfVehicleError(u->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_CAPACITY, GRFBug::VehCapacity, true);
		}
		u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_TRAIN_CARGO_AGE_PERIOD, e_u->info.cargo_age_period);

		/* check the vehicle length (callback) */
		uint16_t veh_len = CALLBACK_FAILED;
		if (e_u->GetGRF() != nullptr && e_u->GetGRF()->grf_version >= 8) {
			/* Use callback 36 */
			veh_len = GetVehicleProperty(u, PROP_TRAIN_SHORTEN_FACTOR, CALLBACK_FAILED);

			if (veh_len != CALLBACK_FAILED && veh_len >= VEHICLE_LENGTH) {
				ErrorUnknownCallbackResult(e_u->GetGRFID(), CBID_VEHICLE_LENGTH, veh_len);
			}
		} else if (e_u->info.callback_mask.Test(VehicleCallbackMask::Length)) {
			/* Use callback 11 */
			veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
		}
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
		veh_len = VEHICLE_LENGTH - Clamp(veh_len, 0, VEHICLE_LENGTH - 1);

		if (allowed_changes.Test(ConsistChangeFlag::Length)) {
			/* Update vehicle length. */
			u->gcache.cached_veh_length = veh_len;
		} else {
			/* Verify length hasn't changed. */
			if (veh_len != u->gcache.cached_veh_length) VehicleLengthChanged(u);
		}

		this->gcache.cached_total_length += u->gcache.cached_veh_length;
		this->InvalidateNewGRFCache();
		u->InvalidateNewGRFCache();
	}

	/* store consist weight/max speed in cache */
	this->vcache.cached_max_speed = max_speed;
	this->tcache.cached_tilt = train_can_tilt;
	this->tcache.cached_curve_speed_mod = min_curve_speed_mod;
	this->tcache.cached_max_curve_speed = this->GetCurveSpeedLimit();

	/* recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) */
	this->CargoChanged();

	if (this->IsFrontEngine()) {
		this->UpdateAcceleration();
		SetWindowDirty(WindowClass::VehicleDetails, this->index);
		InvalidateWindowData(WindowClass::VehicleRefit, this->index, VIWD_CONSIST_CHANGED);
		InvalidateWindowData(WindowClass::VehicleOrders, this->index, VIWD_CONSIST_CHANGED);
		InvalidateNewGRFInspectWindow(GrfSpecFeature::Trains, this->index);

		/* If the consist is changed while in a depot, the vehicle view window must be invalidated to update the availability of refitting. */
		InvalidateWindowData(WindowClass::VehicleView, this->index, VIWD_CONSIST_CHANGED);
	}
}

/**
 * Get the stop location of (the center) of the front vehicle of a train at
 * a platform of a station.
 * @param station_id     the ID of the station where we're stopping
 * @param tile           the tile where the vehicle currently is
 * @param moving_front the moving front of the train to get the stop location of
 * @param station_ahead  'return' the amount of 1/16th tiles in front of the train
 * @param station_length 'return' the station length in 1/16th tiles
 * @return the location, calculated from the begin of the station to stop at.
 */
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *moving_front, int *station_ahead, int *station_length)
{
	const Train *consist = moving_front->First();
	const Station *st = Station::Get(station_id);
	*station_ahead  = st->GetPlatformLength(tile, DirToDiagDir(moving_front->GetMovingDirection())) * TILE_SIZE;
	*station_length = st->GetPlatformLength(tile) * TILE_SIZE;

	/* Default to the middle of the station for stations stops that are not in
	 * the order list like intermediate stations when non-stop is disabled */
	OrderStopLocation osl = OrderStopLocation::Middle;
	if (consist->gcache.cached_total_length >= *station_length) {
		/* The train is longer than the station, make it stop at the far end of the platform */
		osl = OrderStopLocation::FarEnd;
	} else if (consist->current_order.IsType(OT_GOTO_STATION) && consist->current_order.GetDestination() == station_id) {
		osl = consist->current_order.GetStopLocation();
	}

	/* The stop location of the FRONT! of the train */
	int stop;
	switch (osl) {
		default: NOT_REACHED();

		case OrderStopLocation::NearEnd:
			stop = consist->gcache.cached_total_length;
			break;

		case OrderStopLocation::Middle:
			stop = *station_length - (*station_length - consist->gcache.cached_total_length) / 2;
			break;

		case OrderStopLocation::FarEnd:
			stop = *station_length;
			break;
	}

	/* Subtract half the front vehicle length of the train so we get the real
	 * stop location of the train. */
	uint8_t rounding = consist->IsDrivingBackwards() ? 2 : 1;
	return stop - (consist->gcache.cached_veh_length + rounding) / 2;
}


/**
 * Computes train speed limit caused by curves
 * @return imposed speed limit
 */
uint16_t Train::GetCurveSpeedLimit() const
{
	assert(this->First() == this);

	static const int absolute_max_speed = UINT16_MAX;
	int max_speed = absolute_max_speed;

	if (_settings_game.vehicle.train_acceleration_model == AccelerationModel::Original) return max_speed;

	int curvecount[2] = {0, 0};

	/* first find the curve speed limit */
	int numcurve = 0;
	int sum = 0;
	int pos = 0;
	int lastpos = -1;
	for (const Train *u = this; u->Next() != nullptr; u = u->Next(), pos += u->gcache.cached_veh_length) {
		Direction this_dir = u->direction;
		Direction next_dir = u->Next()->direction;

		DirDiff dirdiff = DirDifference(this_dir, next_dir);
		if (dirdiff == DirDiff::Same) continue;

		if (dirdiff == DirDiff::Left45) curvecount[0]++;
		if (dirdiff == DirDiff::Right45) curvecount[1]++;
		if (dirdiff == DirDiff::Left45 || dirdiff == DirDiff::Right45) {
			if (lastpos != -1) {
				numcurve++;
				sum += pos - lastpos;
				if (pos - lastpos <= static_cast<int>(VEHICLE_LENGTH) && max_speed > 88) {
					max_speed = 88;
				}
			}
			lastpos = pos;
		}

		/* if we have a 90 degree turn, fix the speed limit to 60 */
		if (dirdiff == DirDiff::Left90 || dirdiff == DirDiff::Right90) {
			max_speed = 61;
		}
	}

	if (numcurve > 0 && max_speed > 88) {
		if (curvecount[0] == 1 && curvecount[1] == 1) {
			max_speed = absolute_max_speed;
		} else {
			sum = CeilDiv(sum, VEHICLE_LENGTH);
			sum /= numcurve;
			max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
		}
	}

	if (max_speed != absolute_max_speed) {
		/* Apply the current railtype's curve speed advantage */
		const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(this->tile));
		max_speed += (max_speed / 2) * rti->curve_speed;

		if (this->tcache.cached_tilt) {
			/* Apply max_speed bonus of 20% for a tilting train */
			max_speed += max_speed / 5;
		}

		/* Apply max_speed modifier (cached value is fixed-point binary with 8 fractional bits)
		 * and clamp the result to an acceptable range. */
		max_speed += (max_speed * this->tcache.cached_curve_speed_mod) / 256;
		max_speed = Clamp(max_speed, 2, absolute_max_speed);
	}

	return static_cast<uint16_t>(max_speed);
}

/**
 * Calculates the maximum speed of the vehicle under its current conditions.
 * @return Maximum speed of the vehicle.
 */
int Train::GetCurrentMaxSpeed() const
{
	const Train *moving_front = this->GetMovingFront();
	int max_speed = _settings_game.vehicle.train_acceleration_model == AccelerationModel::Original ?
			this->gcache.cached_max_track_speed :
			this->tcache.cached_max_curve_speed;

	if (_settings_game.vehicle.train_acceleration_model == AccelerationModel::Realistic && IsRailStationTile(moving_front->tile)) {
		StationID sid = GetStationIndex(moving_front->tile);
		if (this->current_order.ShouldStopAtStation(this, sid)) {
			int station_ahead;
			int station_length;
			int stop_at = GetTrainStopLocation(sid, moving_front->tile, moving_front, &station_ahead, &station_length);

			/* The distance to go is whatever is still ahead of the train minus the
			 * distance from the train's stop location to the end of the platform */
			int distance_to_go = station_ahead / TILE_SIZE - (station_length - stop_at) / TILE_SIZE;

			if (distance_to_go > 0) {
				int st_max_speed = 120;

				int delta_v = this->cur_speed / (distance_to_go + 1);
				if (max_speed > (this->cur_speed - delta_v)) {
					st_max_speed = this->cur_speed - (delta_v / 10);
				}

				st_max_speed = std::max(st_max_speed, 25 * distance_to_go);
				max_speed = std::min(max_speed, st_max_speed);
			}
		}
	}

	for (const Train *u = this; u != nullptr; u = u->Next()) {
		if (_settings_game.vehicle.train_acceleration_model == AccelerationModel::Realistic && u->track == Track::Depot) {
			constexpr int DEPOT_SPEED_LIMIT = 61;
			max_speed = std::min(max_speed, DEPOT_SPEED_LIMIT);
			break;
		}

		/* Vehicle is on the middle part of a bridge. */
		if (u->track == Track::Wormhole && !u->vehstatus.Test(VehState::Hidden)) {
			max_speed = std::min<int>(max_speed, GetBridgeSpec(GetBridgeType(u->tile))->speed);
		}
	}

	max_speed = std::min<int>(max_speed, this->current_order.GetMaxSpeed());

	/* If the train is going backwards, without a leading cab, restrict its speed. */
	if (!moving_front->CanLeadTrain()) {
		constexpr int BACKWARDS_NO_CAB_SPEED_LIMIT = 32;
		max_speed = std::min<int>(max_speed, BACKWARDS_NO_CAB_SPEED_LIMIT);
	}

	return std::min<int>(max_speed, this->gcache.cached_max_track_speed);
}

/** Update acceleration of the train from the cached power and weight. */
void Train::UpdateAcceleration()
{
	assert(this->IsFrontEngine() || this->IsFreeWagon());

	uint power = this->gcache.cached_power;
	uint weight = this->gcache.cached_weight;
	assert(weight != 0);
	this->acceleration = Clamp(power / weight * 4, 1, 255);
}

/**
 * Get the offset for train image when it is used as cursor.
 * @return The offset in horizontal direction.
 */
int Train::GetCursorImageOffset() const
{
	if (this->gcache.cached_veh_length != 8 && this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips)) {
		int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;

		const Engine *e = this->GetEngine();
		if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->VehInfo<RailVehicleInfo>().image_index)) {
			reference_width = e->GetGRF()->traininfo_vehicle_width;
		}

		return ScaleSpriteTrad((this->gcache.cached_veh_length - (int)VEHICLE_LENGTH) * reference_width / (int)VEHICLE_LENGTH);
	}
	return 0;
}

/**
 * Get the width of a train vehicle image in the GUI.
 * @param offset Additional offset for positioning the sprite; set to nullptr if not needed
 * @return Width in pixels
 */
int Train::GetDisplayImageWidth(Point *offset) const
{
	int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
	int vehicle_pitch = 0;

	const Engine *e = this->GetEngine();
	if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->VehInfo<RailVehicleInfo>().image_index)) {
		reference_width = e->GetGRF()->traininfo_vehicle_width;
		vehicle_pitch = e->GetGRF()->traininfo_vehicle_pitch;
	}

	if (offset != nullptr) {
		if (this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips)) {
			offset->x = ScaleSpriteTrad(((int)this->gcache.cached_veh_length - (int)VEHICLE_LENGTH / 2) * reference_width / (int)VEHICLE_LENGTH);
		} else {
			offset->x = ScaleSpriteTrad(reference_width) / 2;
		}
		offset->y = ScaleSpriteTrad(vehicle_pitch);
	}
	return ScaleSpriteTrad(this->gcache.cached_veh_length * reference_width / VEHICLE_LENGTH);
}

static SpriteID GetDefaultTrainSprite(uint8_t spritenum, Direction direction)
{
	assert(IsValidImageIndex<VehicleType::Train>(spritenum));
	return ((to_underlying(direction) + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
}

/**
 * Get the sprite to display the train.
 * @param direction Direction of view/travel.
 * @param image_type Visualisation context.
 * @param result Sprite sequence to add the to be drawn sprites to.
 */
void Train::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
{
	uint8_t spritenum = this->spritenum;

	if (this->flags.Test(VehicleRailFlag::Flipped)) direction = ReverseDir(direction);

	if (IsCustomVehicleSpriteNum(spritenum)) {
		if (spritenum == CUSTOM_VEHICLE_SPRITENUM_REVERSED) direction = ReverseDir(direction);
		GetCustomVehicleSprite(this, direction, image_type, result);
		if (result->IsValid()) return;

		spritenum = this->GetEngine()->original_image_index;
	}

	assert(IsValidImageIndex<VehicleType::Train>(spritenum));
	SpriteID sprite = GetDefaultTrainSprite(spritenum, direction);

	if (this->cargo.StoredCount() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];

	result->Set(sprite);
}

static void GetRailIcon(EngineID engine, bool rear_head, int &y, EngineImageType image_type, VehicleSpriteSeq *result)
{
	const Engine *e = Engine::Get(engine);
	Direction dir = rear_head ? Direction::E : Direction::W;
	uint8_t spritenum = e->VehInfo<RailVehicleInfo>().image_index;

	if (IsCustomVehicleSpriteNum(spritenum)) {
		GetCustomVehicleIcon(engine, dir, image_type, result);
		if (result->IsValid()) {
			if (e->GetGRF() != nullptr) {
				y += ScaleSpriteTrad(e->GetGRF()->traininfo_vehicle_pitch);
			}
			return;
		}

		spritenum = Engine::Get(engine)->original_image_index;
	}

	if (rear_head) spritenum++;

	result->Set(GetDefaultTrainSprite(spritenum, Direction::W));
}

void DrawTrainEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
{
	const GRFFile *grf = Engine::Get(engine)->GetGRF();
	int vehicle_width = ScaleSpriteTrad(grf == nullptr ? TRAININFO_DEFAULT_VEHICLE_WIDTH : grf->traininfo_vehicle_width);

	if (RailVehInfo(engine)->railveh_type == RailVehicleType::Multihead) {
		int yf = y;
		int yr = y;

		VehicleSpriteSeq seqf, seqr;
		GetRailIcon(engine, false, yf, image_type, &seqf);
		GetRailIcon(engine, true, yr, image_type, &seqr);

		Rect rectf, rectr;
		seqf.GetBounds(&rectf);
		seqr.GetBounds(&rectr);

		preferred_x = Clamp(preferred_x,
				left - UnScaleGUI(rectf.left) + vehicle_width / 2,
				right - UnScaleGUI(rectr.right) - (vehicle_width - vehicle_width / 2));

		seqf.Draw(preferred_x - vehicle_width / 2, yf, pal, pal == PALETTE_CRASH);
		seqr.Draw(preferred_x + (vehicle_width - vehicle_width / 2), yr, pal, pal == PALETTE_CRASH);
	} else {
		VehicleSpriteSeq seq;
		GetRailIcon(engine, false, y, image_type, &seq);

		Rect rect;
		seq.GetBounds(&rect);
		preferred_x = Clamp(preferred_x,
				left - UnScaleGUI(rect.left),
				right - UnScaleGUI(rect.right));

		seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
	}
}

/**
 * Get the size of the sprite of a train sprite heading west, or both heads (used for lists).
 * @param engine The engine to get the sprite from.
 * @param[out] width The width of the sprite.
 * @param[out] height The height of the sprite.
 * @param[out] xoffs Number of pixels to shift the sprite to the right.
 * @param[out] yoffs Number of pixels to shift the sprite downwards.
 * @param image_type Context the sprite is used in.
 */
void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
{
	int y = 0;

	VehicleSpriteSeq seq;
	GetRailIcon(engine, false, y, image_type, &seq);

	Rect rect;
	seq.GetBounds(&rect);

	width  = UnScaleGUI(rect.Width());
	height = UnScaleGUI(rect.Height());
	xoffs  = UnScaleGUI(rect.left);
	yoffs  = UnScaleGUI(rect.top);

	if (RailVehInfo(engine)->railveh_type == RailVehicleType::Multihead) {
		const GRFFile *grf = Engine::Get(engine)->GetGRF();
		int vehicle_width = ScaleSpriteTrad(grf == nullptr ? TRAININFO_DEFAULT_VEHICLE_WIDTH : grf->traininfo_vehicle_width);

		GetRailIcon(engine, true, y, image_type, &seq);
		seq.GetBounds(&rect);

		/* Calculate values relative to an imaginary center between the two sprites. */
		width = vehicle_width + UnScaleGUI(rect.right) - xoffs;
		height = std::max<uint>(height, UnScaleGUI(rect.Height()));
		xoffs  = xoffs - vehicle_width / 2;
		yoffs  = std::min(yoffs, UnScaleGUI(rect.top));
	}
}

/**
 * Get a list of free wagons in a depot.
 * @param tile Tile of depot.
 * @return List of free wagons, sorted by vehicle index.
 */
static std::vector<VehicleID> GetFreeWagonsInDepot(TileIndex tile)
{
	std::vector<VehicleID> free_wagons;

	for (Vehicle *v : VehiclesOnTile(tile)) {
		if (v->type != VehicleType::Train) continue;
		if (v->vehstatus.Test(VehState::Crashed)) continue;
		if (!Train::From(v)->IsFreeWagon()) continue;

		free_wagons.push_back(v->index);
	}

	/* Sort by vehicle index for consistency across clients. */
	std::ranges::sort(free_wagons);
	return free_wagons;
}

/**
 * Build a railroad wagon.
 * @param flags    type of operation.
 * @param tile     tile of the depot where rail-vehicle is built.
 * @param e        the engine to build.
 * @param[out] ret the vehicle that has been built.
 * @return the cost of this operation or an error.
 */
static CommandCost CmdBuildRailWagon(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
{
	const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();

	/* Check that the wagon can drive on the track in question */
	if (!IsCompatibleRail(rvi->railtypes, GetRailType(tile))) return CMD_ERROR;

	if (flags.Test(DoCommandFlag::Execute)) {
		Train *v = Train::Create();
		*ret = v;
		v->spritenum = rvi->image_index;

		v->engine_type = e->index;
		v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback

		DiagDirection dir = GetRailDepotDirection(tile);

		v->direction = DiagDirToDir(dir);
		v->tile = tile;

		int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
		int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];

		v->x_pos = x;
		v->y_pos = y;
		v->z_pos = GetSlopePixelZ(x, y, true);
		v->owner = _current_company;
		v->track = Track::Depot;
		v->vehstatus = {VehState::Hidden, VehState::DefaultPalette};

		v->SetWagon();

		v->SetFreeWagon();
		InvalidateWindowData(WindowClass::VehicleDepot, v->tile);

		v->cargo_type = e->GetDefaultCargoType();
		assert(IsValidCargoType(v->cargo_type));
		v->cargo_cap = rvi->capacity;
		v->refit_cap = 0;

		v->railtypes = rvi->railtypes;

		v->date_of_last_service = TimerGameEconomy::date;
		v->date_of_last_service_newgrf = TimerGameCalendar::date;
		v->build_year = TimerGameCalendar::year;
		v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
		v->random_bits = Random();

		v->group_id = DEFAULT_GROUP;

		auto prob = TestVehicleBuildProbability(v, BuildProbabilityType::Reversed);
		if (prob.has_value()) v->flags.Set(VehicleRailFlag::Flipped, prob.value());
		AddArticulatedParts(v);

		v->UpdatePosition();
		v->First()->ConsistChanged(CCF_ARRANGE);
		UpdateTrainGroupID(v->First());

		CheckConsistencyOfArticulatedVehicle(v);

		/* Try to connect the vehicle to one of free chains of wagons. */
		for (VehicleID vehicle : GetFreeWagonsInDepot(tile)) {
			if (vehicle == v->index) continue;

			const Train *w = Train::Get(vehicle);
			if (w->engine_type != v->engine_type) continue; ///< Must be same type
			if (w->First() == v) continue; ///< Don't connect to ourself

			if (Command<Commands::MoveRailVehicle>::Do(DoCommandFlag::Execute, v->index, w->Last()->index, true).Succeeded()) {
				break;
			}
		}
	}

	return CommandCost();
}

/**
 * Move all free vehicles in the depot to the train.
 * @param u The train to move the free vehicles to.
 */
void NormalizeTrainVehInDepot(const Train *u)
{
	assert(u->IsEngine());
	for (VehicleID vehicle : GetFreeWagonsInDepot(u->tile)) {
		if (Command<Commands::MoveRailVehicle>::Do(DoCommandFlag::Execute, vehicle, u->index, true).Failed()) {
			break;
		}
	}
}

static void AddRearEngineToMultiheadedTrain(Train *v)
{
	Train *u = Train::Create();
	v->value >>= 1;
	u->value = v->value;
	u->direction = v->direction;
	u->owner = v->owner;
	u->tile = v->tile;
	u->x_pos = v->x_pos;
	u->y_pos = v->y_pos;
	u->z_pos = v->z_pos;
	u->track = Track::Depot;
	u->vehstatus = v->vehstatus;
	u->vehstatus.Reset(VehState::Stopped);
	u->spritenum = v->spritenum + 1;
	u->cargo_type = v->cargo_type;
	u->cargo_subtype = v->cargo_subtype;
	u->cargo_cap = v->cargo_cap;
	u->refit_cap = v->refit_cap;
	u->railtypes = v->railtypes;
	u->engine_type = v->engine_type;
	u->date_of_last_service = v->date_of_last_service;
	u->date_of_last_service_newgrf = v->date_of_last_service_newgrf;
	u->build_year = v->build_year;
	u->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
	u->random_bits = Random();
	v->SetMultiheaded();
	u->SetMultiheaded();
	v->SetNext(u);
	auto prob = TestVehicleBuildProbability(u, BuildProbabilityType::Reversed);
	if (prob.has_value()) u->flags.Set(VehicleRailFlag::Flipped, prob.value());
	u->UpdatePosition();

	/* Now we need to link the front and rear engines together */
	v->other_multiheaded_part = u;
	u->other_multiheaded_part = v;
}

/**
 * Build a railroad vehicle.
 * @param flags    type of operation.
 * @param tile     tile of the depot where rail-vehicle is built.
 * @param e        the engine to build.
 * @param[out] ret the vehicle that has been built.
 * @return the cost of this operation or an error.
 */
CommandCost CmdBuildRailVehicle(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
{
	const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();

	if (rvi->railveh_type == RailVehicleType::Wagon) return CmdBuildRailWagon(flags, tile, e, ret);

	/* Check if depot and new engine uses the same kind of tracks *
	 * We need to see if the engine got power on the tile to avoid electric engines in non-electric depots */
	if (!HasPowerOnRail(rvi->railtypes, GetRailType(tile))) return CMD_ERROR;

	if (flags.Test(DoCommandFlag::Execute)) {
		DiagDirection dir = GetRailDepotDirection(tile);
		int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
		int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];

		Train *v = Train::Create();
		*ret = v;
		v->direction = DiagDirToDir(dir);
		v->tile = tile;
		v->owner = _current_company;
		v->x_pos = x;
		v->y_pos = y;
		v->z_pos = GetSlopePixelZ(x, y, true);
		v->track = Track::Depot;
		v->vehstatus = {VehState::Hidden, VehState::Stopped, VehState::DefaultPalette};
		v->spritenum = rvi->image_index;
		v->cargo_type = e->GetDefaultCargoType();
		assert(IsValidCargoType(v->cargo_type));
		v->cargo_cap = rvi->capacity;
		v->refit_cap = 0;
		v->last_station_visited = StationID::Invalid();
		v->last_loading_station = StationID::Invalid();

		v->engine_type = e->index;
		v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback

		v->reliability = e->reliability;
		v->reliability_spd_dec = e->reliability_spd_dec;
		v->max_age = e->GetLifeLengthInDays();

		v->railtypes = rvi->railtypes;

		v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_trains);
		v->date_of_last_service = TimerGameEconomy::date;
		v->date_of_last_service_newgrf = TimerGameCalendar::date;
		v->build_year = TimerGameCalendar::year;
		v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
		v->random_bits = Random();

		if (e->flags.Test(EngineFlag::ExclusivePreview)) v->vehicle_flags.Set(VehicleFlag::BuiltAsPrototype);
		v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);

		v->group_id = DEFAULT_GROUP;

		v->SetFrontEngine();
		v->SetEngine();

		auto prob = TestVehicleBuildProbability(v, BuildProbabilityType::Reversed);
		if (prob.has_value()) v->flags.Set(VehicleRailFlag::Flipped, prob.value());
		v->UpdatePosition();

		if (rvi->railveh_type == RailVehicleType::Multihead) {
			AddRearEngineToMultiheadedTrain(v);
		} else {
			AddArticulatedParts(v);
		}

		v->ConsistChanged(CCF_ARRANGE);
		UpdateTrainGroupID(v);

		CheckConsistencyOfArticulatedVehicle(v);
	}

	return CommandCost();
}

static Train *FindGoodVehiclePos(const Train *src)
{
	EngineID eng = src->engine_type;

	for (VehicleID vehicle : GetFreeWagonsInDepot(src->tile)) {
		Train *dst = Train::Get(vehicle);

		/* check so all vehicles in the line have the same engine. */
		Train *t = dst;
		while (t->engine_type == eng) {
			t = t->Next();
			if (t == nullptr) return dst;
		}
	}

	return nullptr;
}

/** Helper type for lists/vectors of trains */
typedef std::vector<Train *> TrainList;

/**
 * Make a backup of a train into a train list.
 * @param list to make the backup in
 * @param t    the train to make the backup of
 */
static void MakeTrainBackup(TrainList &list, Train *t)
{
	for (; t != nullptr; t = t->Next()) list.push_back(t);
}

/**
 * Restore the train from the backup list.
 * @param list the train to restore.
 */
static void RestoreTrainBackup(TrainList &list)
{
	/* No train, nothing to do. */
	if (list.empty()) return;

	Train *prev = nullptr;
	/* Iterate over the list and rebuild it. */
	for (Train *t : list) {
		if (prev != nullptr) {
			prev->SetNext(t);
		} else if (t->Previous() != nullptr) {
			/* Make sure the head of the train is always the first in the chain. */
			t->Previous()->SetNext(nullptr);
		}
		prev = t;
	}
}

/**
 * Remove the given wagon from its consist.
 * @param part the part of the train to remove.
 * @param chain whether to remove the whole chain.
 */
static void RemoveFromConsist(Train *part, bool chain = false)
{
	Train *tail;

	if (chain) {
		/* We're moving several vehicles, find the last one in the chain. */
		tail = part;
		while (tail->Next() != nullptr) tail = tail->Next();
	} else {
		/* We're just moving one vehicle, but make sure we get all the articulated parts. */
		tail = part->GetLastEnginePart();
	}

	/* Unlink at the front, but make it point to the next
	 * vehicle after the to be remove part. */
	if (part->Previous() != nullptr) part->Previous()->SetNext(tail->Next());

	/* Unlink at the back */
	tail->SetNext(nullptr);
}

/**
 * Inserts a chain into the train at dst.
 * @param dst   the place where to append after.
 * @param chain the chain to actually add.
 */
static void InsertInConsist(Train *dst, Train *chain)
{
	/* We do not want to add something in the middle of an articulated part. */
	assert(dst != nullptr && (dst->Next() == nullptr || !dst->Next()->IsArticulatedPart()));

	chain->Last()->SetNext(dst->Next());
	dst->SetNext(chain);
}

/**
 * Normalise the dual heads in the train, i.e. if one is
 * missing move that one to this train.
 * @param t the train to normalise.
 */
static void NormaliseDualHeads(Train *t)
{
	for (; t != nullptr; t = t->GetNextVehicle()) {
		if (!t->IsMultiheaded() || !t->IsEngine()) continue;

		/* Make sure that there are no free cars before next engine */
		Train *u;
		for (u = t; u->Next() != nullptr && !u->Next()->IsEngine(); u = u->Next()) {}

		if (u == t->other_multiheaded_part) continue;

		/* Remove the part from the 'wrong' train */
		RemoveFromConsist(t->other_multiheaded_part);
		/* And add it to the 'right' train */
		InsertInConsist(u, t->other_multiheaded_part);
	}
}

/**
 * Normalise the sub types of the parts in this chain.
 * @param chain the chain to normalise.
 */
static void NormaliseSubtypes(Train *chain)
{
	/* Nothing to do */
	if (chain == nullptr) return;

	/* We must be the first in the chain. */
	assert(chain->Previous() == nullptr);

	/* Set the appropriate bits for the first in the chain. */
	if (chain->IsWagon()) {
		chain->SetFreeWagon();
	} else {
		assert(chain->IsEngine());
		chain->SetFrontEngine();
	}

	/* Now clear the bits for the rest of the chain */
	for (Train *t = chain->Next(); t != nullptr; t = t->Next()) {
		t->ClearFreeWagon();
		t->ClearFrontEngine();
	}
}

/**
 * Check/validate whether we may actually build a new train.
 * @note All vehicles are/were 'heads' of their chains.
 * @param original_dst The original destination chain.
 * @param dst          The destination chain after constructing the train.
 * @param original_src The original source chain.
 * @param src          The source chain after constructing the train.
 * @return possible error of this command.
 */
static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
{
	/* Just add 'new' engines and subtract the original ones.
	 * If that's less than or equal to 0 we can be sure we did
	 * not add any engines (read: trains) along the way. */
	if ((src          != nullptr && src->IsEngine()          ? 1 : 0) +
			(dst          != nullptr && dst->IsEngine()          ? 1 : 0) -
			(original_src != nullptr && original_src->IsEngine() ? 1 : 0) -
			(original_dst != nullptr && original_dst->IsEngine() ? 1 : 0) <= 0) {
		return CommandCost();
	}

	/* Get a free unit number and check whether it's within the bounds.
	 * There will always be a maximum of one new train. */
	if (GetFreeUnitNumber(VehicleType::Train) <= _settings_game.vehicle.max_trains) return CommandCost();

	return CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
}

/**
 * Check whether the train parts can be attached.
 * @param t the train to check
 * @return possible error of this command.
 */
static CommandCost CheckTrainAttachment(Train *t)
{
	/* No multi-part train, no need to check. */
	if (t == nullptr || t->Next() == nullptr) return CommandCost();

	/* The maximum length for a train. For each part we decrease this by one
	 * and if the result is negative the train is simply too long. */
	int allowed_len = _settings_game.vehicle.max_train_length * TILE_SIZE - t->gcache.cached_veh_length;

	/* For free-wagon chains, check if they are within the max_train_length limit. */
	if (!t->IsEngine()) {
		t = t->Next();
		while (t != nullptr) {
			allowed_len -= t->gcache.cached_veh_length;

			t = t->Next();
		}

		if (allowed_len < 0) return CommandCost(STR_ERROR_TRAIN_TOO_LONG);
		return CommandCost();
	}

	Train *head = t;
	Train *prev = t;

	/* Break the prev -> t link so it always holds within the loop. */
	t = t->Next();
	prev->SetNext(nullptr);

	/* Make sure the cache is cleared. */
	head->InvalidateNewGRFCache();

	while (t != nullptr) {
		allowed_len -= t->gcache.cached_veh_length;

		Train *next = t->Next();

		/* Unlink the to-be-added piece; it is already unlinked from the previous
		 * part due to the fact that the prev -> t link is broken. */
		t->SetNext(nullptr);

		/* Don't check callback for articulated or rear dual headed parts */
		if (!t->IsArticulatedPart() && !t->IsRearDualheaded()) {
			/* Back up and clear the first_engine data to avoid using wagon override group */
			EngineID first_engine = t->gcache.first_engine;
			t->gcache.first_engine = EngineID::Invalid();

			/* We don't want the cache to interfere. head's cache is cleared before
			 * the loop and after each callback does not need to be cleared here. */
			t->InvalidateNewGRFCache();

			std::array<int32_t, 1> regs100;
			uint16_t callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, head->engine_type, t, head, regs100);

			/* Restore original first_engine data */
			t->gcache.first_engine = first_engine;

			/* We do not want to remember any cached variables from the test run */
			t->InvalidateNewGRFCache();
			head->InvalidateNewGRFCache();

			if (callback != CALLBACK_FAILED) {
				/* A failing callback means everything is okay */
				StringID error = STR_NULL;

				if (head->GetGRF()->grf_version < 8) {
					if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
					if (callback  < 0xFD) error = GetGRFStringID(head->GetGRFID(), GRFSTR_MISC_GRF_TEXT + callback);
					if (callback >= 0x100) ErrorUnknownCallbackResult(head->GetGRFID(), CBID_TRAIN_ALLOW_WAGON_ATTACH, callback);
				} else {
					if (callback < 0x400) {
						error = GetGRFStringID(head->GetGRFID(), GRFSTR_MISC_GRF_TEXT + callback);
					} else {
						switch (callback) {
							case 0x400: // allow if railtypes match (always the case for OpenTTD)
							case 0x401: // allow
								break;

							case 0x40F:
								error = GetGRFStringID(head->GetGRFID(), static_cast<GRFStringID>(regs100[0]));
								break;

							default:    // unknown reason -> disallow
							case 0x402: // disallow attaching
								error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
								break;
						}
					}
				}

				if (error != STR_NULL) return CommandCost(error);
			}
		}

		/* And link it to the new part. */
		prev->SetNext(t);
		prev = t;
		t = next;
	}

	if (allowed_len < 0) return CommandCost(STR_ERROR_TRAIN_TOO_LONG);
	return CommandCost();
}

/**
 * Validate whether we are going to create valid trains.
 * @note All vehicles are/were 'heads' of their chains.
 * @param original_dst The original destination chain.
 * @param dst          The destination chain after constructing the train.
 * @param original_src The original source chain.
 * @param src          The source chain after constructing the train.
 * @param check_limit  Whether to check the vehicle limit.
 * @return possible error of this command.
 */
static CommandCost ValidateTrains(Train *original_dst, Train *dst, Train *original_src, Train *src, bool check_limit)
{
	/* Check whether we may actually construct the trains. */
	CommandCost ret = CheckTrainAttachment(src);
	if (ret.Failed()) return ret;
	ret = CheckTrainAttachment(dst);
	if (ret.Failed()) return ret;

	/* Check whether we need to build a new train. */
	return check_limit ? CheckNewTrain(original_dst, dst, original_src, src) : CommandCost();
}

/**
 * Arrange the trains in the wanted way.
 * @param dst_head   The destination chain of the to be moved vehicle.
 * @param dst        The destination for the to be moved vehicle.
 * @param src_head   The source chain of the to be moved vehicle.
 * @param src        The to be moved vehicle.
 * @param move_chain Whether to move all vehicles after src or not.
 */
static void ArrangeTrains(Train **dst_head, Train *dst, Train **src_head, Train *src, bool move_chain)
{
	/* First determine the front of the two resulting trains */
	if (*src_head == *dst_head) {
		/* If we aren't moving part(s) to a new train, we are just moving the
		 * front back and there is not destination head. */
		*dst_head = nullptr;
	} else if (*dst_head == nullptr) {
		/* If we are moving to a new train the head of the move train would become
		 * the head of the new vehicle. */
		*dst_head = src;
	}

	if (src == *src_head) {
		/* If we are moving the front of a train then we are, in effect, creating
		 * a new head for the train. Point to that. Unless we are moving the whole
		 * train in which case there is not 'source' train anymore.
		 * In case we are a multiheaded part we want the complete thing to come
		 * with us, so src->GetNextUnit(), however... when we are e.g. a wagon
		 * that is followed by a rear multihead we do not want to include that. */
		*src_head = move_chain ? nullptr :
				(src->IsMultiheaded() ? src->GetNextUnit() : src->GetNextVehicle());
	}

	/* Now it's just simply removing the part that we are going to move from the
	 * source train and *if* the destination is a not a new train add the chain
	 * at the destination location. */
	RemoveFromConsist(src, move_chain);
	if (*dst_head != src) InsertInConsist(dst, src);

	/* Now normalise the dual heads, that is move the dual heads around in such
	 * a way that the head and rear of a dual head are in the same train */
	NormaliseDualHeads(*src_head);
	NormaliseDualHeads(*dst_head);
}

/**
 * Normalise the head of the train again, i.e. that is tell the world that
 * we have changed and update all kinds of variables.
 * @param head the train to update.
 */
static void NormaliseTrainHead(Train *head)
{
	/* Not much to do! */
	if (head == nullptr) return;

	/* Tell the 'world' the train changed. */
	head->ConsistChanged(CCF_ARRANGE);
	UpdateTrainGroupID(head);

	/* Not a front engine, i.e. a free wagon chain. No need to do more. */
	if (!head->IsFrontEngine()) return;

	/* Update the refit button and window */
	InvalidateWindowData(WindowClass::VehicleRefit, head->index, VIWD_CONSIST_CHANGED);
	SetWindowWidgetDirty(WindowClass::VehicleView, head->index, WID_VV_REFIT);

	/* If we don't have a unit number yet, set one. */
	if (head->unitnumber != 0) return;
	head->unitnumber = Company::Get(head->owner)->freeunits[head->type].UseID(GetFreeUnitNumber(VehicleType::Train));
}

/**
 * Move a rail vehicle around inside the depot.
 * @param flags type of operation
 *              Note: DoCommandFlag::AutoReplace is set when autoreplace tries to undo its modifications or moves vehicles to temporary locations inside the depot.
 * @param src_veh source vehicle index
 * @param dest_veh what wagon to put the source wagon AFTER, XXX - VehicleID::Invalid() to make a new line
 * @param move_chain move all vehicles following the source vehicle
 * @return the cost of this operation or an error
 */
CommandCost CmdMoveRailVehicle(DoCommandFlags flags, VehicleID src_veh, VehicleID dest_veh, bool move_chain)
{
	Train *src = Train::GetIfValid(src_veh);
	if (src == nullptr) return CMD_ERROR;

	CommandCost ret = CheckOwnership(src->owner);
	if (ret.Failed()) return ret;

	/* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
	if (src->vehstatus.Test(VehState::Crashed)) return CMD_ERROR;

	/* if nothing is selected as destination, try and find a matching vehicle to drag to. */
	Train *dst;
	if (dest_veh == VehicleID::Invalid()) {
		dst = (src->IsEngine() || flags.Test(DoCommandFlag::AutoReplace)) ? nullptr : FindGoodVehiclePos(src);
	} else {
		dst = Train::GetIfValid(dest_veh);
		if (dst == nullptr) return CMD_ERROR;

		ret = CheckOwnership(dst->owner);
		if (ret.Failed()) return ret;

		/* Do not allow appending to crashed vehicles, too */
		if (dst->vehstatus.Test(VehState::Crashed)) return CMD_ERROR;
	}

	/* if an articulated part is being handled, deal with its parent vehicle */
	src = src->GetFirstEnginePart();
	if (dst != nullptr) {
		dst = dst->GetFirstEnginePart();
	}

	/* don't move the same vehicle.. */
	if (src == dst) return CommandCost();

	/* locate the head of the two chains */
	Train *src_head = src->First();
	Train *dst_head;
	if (dst != nullptr) {
		dst_head = dst->First();
		if (dst_head->tile != src_head->tile) return CMD_ERROR;
		/* Now deal with articulated part of destination wagon */
		dst = dst->GetLastEnginePart();
	} else {
		dst_head = nullptr;
	}

	if (src->IsRearDualheaded()) return CommandCost(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);

	/* When moving all wagons, we can't have the same src_head and dst_head */
	if (move_chain && src_head == dst_head) return CommandCost();

	/* When moving a multiheaded part to be place after itself, bail out. */
	if (!move_chain && dst != nullptr && dst->IsRearDualheaded() && src == dst->other_multiheaded_part) return CommandCost();

	/* Check if all vehicles in the source train are stopped inside a depot. */
	if (!src_head->IsStoppedInDepot()) return CommandCost(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);

	/* Check if all vehicles in the destination train are stopped inside a depot. */
	if (dst_head != nullptr && !dst_head->IsStoppedInDepot()) return CommandCost(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);

	/* First make a backup of the order of the trains. That way we can do
	 * whatever we want with the order and later on easily revert. */
	TrainList original_src;
	TrainList original_dst;

	MakeTrainBackup(original_src, src_head);
	MakeTrainBackup(original_dst, dst_head);

	/* Also make backup of the original heads as ArrangeTrains can change them.
	 * For the destination head we do not care if it is the same as the source
	 * head because in that case it's just a copy. */
	Train *original_src_head = src_head;
	Train *original_dst_head = (dst_head == src_head ? nullptr : dst_head);

	/* We want this information from before the rearrangement, but execute this after the validation.
	 * original_src_head can't be nullptr; src is by definition != nullptr, so src_head can't be nullptr as
	 * src->GetFirst() always yields non-nullptr, so eventually original_src_head != nullptr as well. */
	bool original_src_head_front_engine = original_src_head->IsFrontEngine();
	bool original_dst_head_front_engine = original_dst_head != nullptr && original_dst_head->IsFrontEngine();

	/* (Re)arrange the trains in the wanted arrangement. */
	ArrangeTrains(&dst_head, dst, &src_head, src, move_chain);

	if (!flags.Test(DoCommandFlag::AutoReplace)) {
		/* If the autoreplace flag is set we do not need to test for the validity
		 * because we are going to revert the train to its original state. As we
		 * assume the original state was correct autoreplace can skip this. */
		ret = ValidateTrains(original_dst_head, dst_head, original_src_head, src_head, true);
		if (ret.Failed()) {
			/* Restore the train we had. */
			RestoreTrainBackup(original_src);
			RestoreTrainBackup(original_dst);
			return ret;
		}
	}

	/* do it? */
	if (flags.Test(DoCommandFlag::Execute)) {
		/* Remove old heads from the statistics */
		if (original_src_head_front_engine) GroupStatistics::CountVehicle(original_src_head, -1);
		if (original_dst_head_front_engine) GroupStatistics::CountVehicle(original_dst_head, -1);

		/* First normalise the sub types of the chains. */
		NormaliseSubtypes(src_head);
		NormaliseSubtypes(dst_head);

		/* There are 14 different cases:
		 *  1) front engine gets moved to a new train, it stays a front engine.
		 *     a) the 'next' part is a wagon that becomes a free wagon chain.
		 *     b) the 'next' part is an engine that becomes a front engine.
		 *     c) there is no 'next' part, nothing else happens
		 *  2) front engine gets moved to another train, it is not a front engine anymore
		 *     a) the 'next' part is a wagon that becomes a free wagon chain.
		 *     b) the 'next' part is an engine that becomes a front engine.
		 *     c) there is no 'next' part, nothing else happens
		 *  3) front engine gets moved to later in the current train, it is not a front engine anymore.
		 *     a) the 'next' part is a wagon that becomes a free wagon chain.
		 *     b) the 'next' part is an engine that becomes a front engine.
		 *  4) free wagon gets moved
		 *     a) the 'next' part is a wagon that becomes a free wagon chain.
		 *     b) the 'next' part is an engine that becomes a front engine.
		 *     c) there is no 'next' part, nothing else happens
		 *  5) non front engine gets moved and becomes a new train, nothing else happens
		 *  6) non front engine gets moved within a train / to another train, nothing happens
		 *  7) wagon gets moved, nothing happens
		 */
		if (src == original_src_head && src->IsEngine() && !src->IsFrontEngine()) {
			/* Cases #2 and #3: the front engine gets trashed. */
			CloseWindowById(WindowClass::VehicleView, src->index);
			CloseWindowById(WindowClass::VehicleOrders, src->index);
			CloseWindowById(WindowClass::VehicleRefit, src->index);
			CloseWindowById(WindowClass::VehicleDetails, src->index);
			CloseWindowById(WindowClass::VehicleTimetable, src->index);
			DeleteNewGRFInspectWindow(GrfSpecFeature::Trains, src->index);
			SetWindowDirty(WindowClass::Company, _current_company);

			if (src_head != nullptr && src_head->IsFrontEngine()) {
				/* Cases #?b: Transfer order, unit number and other stuff
				 * to the new front engine. */
				src_head->orders = src->orders;
				if (src_head->orders != nullptr) src_head->AddToShared(src);
				src_head->CopyVehicleConfigAndStatistics(src);
			}
			/* Remove stuff not valid anymore for non-front engines. */
			DeleteVehicleOrders(src);
			src->ReleaseUnitNumber();
			src->name.clear();
		}

		/* We weren't a front engine but are becoming one. So
		 * we should be put in the default group. */
		if (original_src_head != src && dst_head == src) {
			SetTrainGroupID(src, DEFAULT_GROUP);
			SetWindowDirty(WindowClass::Company, _current_company);
		}

		/* Handle 'new engine' part of cases #1b, #2b, #3b, #4b and #5 in NormaliseTrainHead. */
		NormaliseTrainHead(src_head);
		NormaliseTrainHead(dst_head);

		/* Add new heads to statistics.
		 * This should be done after NormaliseTrainHead due to engine total limit checks in GetFreeUnitNumber. */
		if (src_head != nullptr && src_head->IsFrontEngine()) GroupStatistics::CountVehicle(src_head, 1);
		if (dst_head != nullptr && dst_head->IsFrontEngine()) GroupStatistics::CountVehicle(dst_head, 1);

		if (!flags.Test(DoCommandFlag::NoCargoCapacityCheck)) {
			CheckCargoCapacity(src_head);
			CheckCargoCapacity(dst_head);
		}

		if (src_head != nullptr) src_head->First()->MarkDirty();
		if (dst_head != nullptr) dst_head->First()->MarkDirty();

		/* We are undoubtedly changing something in the depot and train list. */
		InvalidateWindowData(WindowClass::VehicleDepot, src->tile);
		InvalidateWindowClassesData(WindowClass::TrainList, 0);
	} else {
		/* We don't want to execute what we're just tried. */
		RestoreTrainBackup(original_src);
		RestoreTrainBackup(original_dst);
	}

	return CommandCost();
}

/**
 * Sell a (single) train wagon/engine.
 * @param flags type of operation
 * @param t     the train wagon to sell
 * @param sell_chain  the selling mode
 * - sell_chain = false: only sell the single dragged wagon/engine (and any belonging rear-engines)
 * - sell_chain = true:  sell the vehicle and all vehicles following it in the chain
 *                       if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
 * @param backup_order make order backup?
 * @param user  the user for the order backup.
 * @return the cost of this operation or an error
 */
CommandCost CmdSellRailWagon(DoCommandFlags flags, Vehicle *t, bool sell_chain, bool backup_order, ClientID user)
{
	Train *v = Train::From(t)->GetFirstEnginePart();
	Train *first = v->First();

	if (v->IsRearDualheaded()) return CommandCost(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);

	/* First make a backup of the order of the train. That way we can do
	 * whatever we want with the order and later on easily revert. */
	TrainList original;
	MakeTrainBackup(original, first);

	/* We need to keep track of the new head and the head of what we're going to sell. */
	Train *new_head = first;
	Train *sell_head = nullptr;

	/* Split the train in the wanted way. */
	ArrangeTrains(&sell_head, nullptr, &new_head, v, sell_chain);

	/* We don't need to validate the second train; it's going to be sold. */
	CommandCost ret = ValidateTrains(nullptr, nullptr, first, new_head, !flags.Test(DoCommandFlag::AutoReplace));
	if (ret.Failed()) {
		/* Restore the train we had. */
		RestoreTrainBackup(original);
		return ret;
	}

	if (first->orders == nullptr && !OrderList::CanAllocateItem()) {
		/* Restore the train we had. */
		RestoreTrainBackup(original);
		return CommandCost(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
	}

	CommandCost cost(ExpensesType::NewVehicles);
	for (Train *part = sell_head; part != nullptr; part = part->Next()) cost.AddCost(-part->value);

	/* do it? */
	if (flags.Test(DoCommandFlag::Execute)) {
		/* First normalise the sub types of the chain. */
		NormaliseSubtypes(new_head);

		if (v == first && !sell_chain && new_head != nullptr && new_head->IsFrontEngine()) {
			if (v->IsEngine()) {
				/* We are selling the front engine. In this case we want to
				 * 'give' the order, unit number and such to the new head. */
				new_head->orders = first->orders;
				new_head->AddToShared(first);
				DeleteVehicleOrders(first);

				/* Copy other important data from the front engine */
				new_head->CopyVehicleConfigAndStatistics(first);
			}
			GroupStatistics::CountVehicle(new_head, 1); // after copying over the profit, if required
		} else if (v->IsPrimaryVehicle() && backup_order) {
			OrderBackup::Backup(v, user);
		}

		/* We need to update the information about the train. */
		NormaliseTrainHead(new_head);

		/* We are undoubtedly changing something in the depot and train list. */
		InvalidateWindowData(WindowClass::VehicleDepot, v->tile);
		InvalidateWindowClassesData(WindowClass::TrainList, 0);

		/* Actually delete the sold 'goods' */
		delete sell_head;
	} else {
		/* We don't want to execute what we're just tried. */
		RestoreTrainBackup(original);
	}

	return cost;
}

void Train::UpdateDeltaXY()
{
	/* Set common defaults. */
	this->bounds = {{-1, -1, 0}, {3, 3, 6}, {}};

	/* Set if flipped and engine is NOT flagged with custom flip handling. */
	int flipped = this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips);
	/* If flipped and vehicle length is odd, we need to adjust the bounding box offset slightly. */
	int flip_offs = flipped && (this->gcache.cached_veh_length & 1);

	Direction dir = this->direction;
	if (flipped) dir = ReverseDir(dir);

	if (!IsDiagonalDirection(dir)) {
		static constexpr DiagDirectionIndexArray<Point> _sign_table{{{
			/* x, y */
			{-1, -1}, // DiagDirection::N
			{-1,  1}, // DiagDirection::E
			{ 1,  1}, // DiagDirection::S
			{ 1, -1}, // DiagDirection::W
		}}};

		int half_shorten = (VEHICLE_LENGTH - this->gcache.cached_veh_length + flipped) / 2;

		/* For all straight directions, move the bound box to the centre of the vehicle, but keep the size. */
		this->bounds.offset.x -= half_shorten * _sign_table[DirToDiagDir(dir)].x;
		this->bounds.offset.y -= half_shorten * _sign_table[DirToDiagDir(dir)].y;
	} else {
		switch (dir) {
				/* Shorten southern corner of the bounding box according the vehicle length
				 * and center the bounding box on the vehicle. */
			case Direction::NE:
				this->bounds.origin.x = -(this->gcache.cached_veh_length + 1) / 2 + flip_offs;
				this->bounds.extent.x = this->gcache.cached_veh_length;
				this->bounds.offset.x = 1;
				break;

			case Direction::NW:
				this->bounds.origin.y = -(this->gcache.cached_veh_length + 1) / 2 + flip_offs;
				this->bounds.extent.y = this->gcache.cached_veh_length;
				this->bounds.offset.y = 1;
				break;

				/* Move northern corner of the bounding box down according to vehicle length
				 * and center the bounding box on the vehicle. */
			case Direction::SW:
				this->bounds.origin.x = -(this->gcache.cached_veh_length) / 2 - flip_offs;
				this->bounds.extent.x = this->gcache.cached_veh_length;
				this->bounds.offset.x = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
				break;

			case Direction::SE:
				this->bounds.origin.y = -(this->gcache.cached_veh_length) / 2 - flip_offs;
				this->bounds.extent.y = this->gcache.cached_veh_length;
				this->bounds.offset.y = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
				break;

			default:
				NOT_REACHED();
		}
	}
}

/**
 * Mark a train as stuck and stop it if it isn't stopped right now.
 * @param consist %Train to mark as being stuck.
 */
static void MarkTrainAsStuck(Train *consist)
{
	if (!consist->flags.Test(VehicleRailFlag::Stuck)) {
		/* It is the first time the problem occurred, set the "train stuck" flag. */
		consist->flags.Set(VehicleRailFlag::Stuck);

		consist->wait_counter = 0;

		/* Stop train */
		consist->cur_speed = 0;
		consist->subspeed = 0;
		consist->SetLastSpeed();

		SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
	}
}

/**
 * Swap the two up/down flags in two ways:
 * - Swap values of \a swap_flag1 and \a swap_flag2, and
 * - If going up previously (#GroundVehicleFlag::GoingUp set), the #GroundVehicleFlag::GoingDown is set, and vice versa.
 * @param[in,out] swap_flag1 First train flag.
 * @param[in,out] swap_flag2 Second train flag.
 */
static void SwapTrainFlags(GroundVehicleFlags *swap_flag1, GroundVehicleFlags *swap_flag2)
{
	GroundVehicleFlags flag1 = *swap_flag1;
	GroundVehicleFlags flag2 = *swap_flag2;

	/* Reverse the rail-flags (if needed) */
	swap_flag2->Set(GroundVehicleFlag::GoingDown, flag1.Test(GroundVehicleFlag::GoingUp));
	swap_flag2->Set(GroundVehicleFlag::GoingUp, flag1.Test(GroundVehicleFlag::GoingDown));
	swap_flag1->Set(GroundVehicleFlag::GoingDown, flag2.Test(GroundVehicleFlag::GoingUp));
	swap_flag1->Set(GroundVehicleFlag::GoingUp, flag2.Test(GroundVehicleFlag::GoingDown));
}

/**
 * Updates some variables after swapping the vehicle.
 * @param v swapped vehicle
 * @param reverse Should we reverse the direction of the vehicle?
 */
static void UpdateStatusAfterSwap(Train *v, bool reverse = true)
{
	/* Maybe reverse the direction. */
	if (reverse) v->direction = ReverseDir(v->direction);

	/* Call the proper EnterTile function unless we are in a wormhole. */
	if (v->track != Track::Wormhole) {
		VehicleEnterTile(v, v->tile, v->x_pos, v->y_pos);
	} else {
		/* VehicleEnterTile_TunnelBridge() sets Track::Wormhole when the vehicle
		 * is on the last bit of the bridge head (frame == TILE_SIZE - 1).
		 * If we were swapped with such a vehicle, we have set Track::Wormhole,
		 * when we shouldn't have. Check if this is the case. */
		TileIndex vt = TileVirtXY(v->x_pos, v->y_pos);
		if (IsTileType(vt, TileType::TunnelBridge)) {
			VehicleEnterTile(v, vt, v->x_pos, v->y_pos);
			if (v->track != Track::Wormhole && IsBridgeTile(v->tile)) {
				/* We have just left the wormhole, possibly set the
				 * "goingdown" bit. UpdateInclination() can be used
				 * because we are at the border of the tile. */
				v->UpdatePosition();
				v->UpdateInclination(true, true);
				return;
			}
		}
	}

	v->UpdatePosition();
	v->UpdateViewport(true, true);
}

/**
 * Swap vehicles \a l and \a r in consist \a v, and reverse their direction.
 * UpdateStatusAfterSwap calls should be made after all ReverseTrainSwapVeh calls have been completed.
 * @param v Consist to change.
 * @param l %Vehicle index in the consist of the first vehicle.
 * @param r %Vehicle index in the consist of the second vehicle.
 */
static void ReverseTrainSwapVeh(Train *v, int l, int r)
{
	Train *a, *b;

	/* locate vehicles to swap */
	for (a = v; l != 0; l--) a = a->Next();
	for (b = v; r != 0; r--) b = b->Next();

	if (a != b) {
		/* swap the hidden bits */
		{
			bool a_hidden = a->vehstatus.Test(VehState::Hidden);
			bool b_hidden = b->vehstatus.Test(VehState::Hidden);
			b->vehstatus.Set(VehState::Hidden, a_hidden);
			a->vehstatus.Set(VehState::Hidden, b_hidden);
		}

		std::swap(a->track, b->track);
		std::swap(a->direction, b->direction);
		std::swap(a->x_pos, b->x_pos);
		std::swap(a->y_pos, b->y_pos);
		std::swap(a->tile,  b->tile);
		std::swap(a->z_pos, b->z_pos);

		SwapTrainFlags(&a->gv_flags, &b->gv_flags);
	} else {
		/* Swap GroundVehicleFlag::GoingUp/GroundVehicleFlag::GoingDown.
		 * This is a little bit redundant way, a->gv_flags will
		 * be (re)set twice, but it reduces code duplication */
		SwapTrainFlags(&a->gv_flags, &a->gv_flags);
	}
}

/**
 * Swap vehicles in chain starting from \a v, and reverse their direction.
 * @param v First vehicle in chain to change.
 */
void ReverseTrainSwapVehicles(Train *v)
{
	int r = CountVehiclesInChain(v) - 1;  // number of vehicles - 1

	/* swap start<>end, start+1<>end-1, ... */
	int l = 0;
	do {
		ReverseTrainSwapVeh(v, l++, r--);
	} while (l <= r);

	for (Train *u = v; u != nullptr; u = u->Next()) {
		UpdateStatusAfterSwap(u);
	}
}

/**
 * Check if the vehicle is a train
 * @param v vehicle on tile
 * @return true if v is a train
 */
static bool IsTrain(const Vehicle *v)
{
	return v->type == VehicleType::Train;
}

/**
 * Check if a level crossing tile has a train on it
 * @param tile tile to test
 * @return true if a train is on the crossing
 * @pre tile is a level crossing
 */
bool TrainOnCrossing(TileIndex tile)
{
	assert(IsLevelCrossingTile(tile));

	return HasVehicleOnTile(tile, IsTrain);
}

/**
 * Checks if a train is approaching a rail-road crossing
 * @param v vehicle on tile
 * @param tile tile with crossing we are testing
 * @return true if v is approaching a crossing
 */
static bool TrainApproachingCrossingEnum(const Vehicle *v, TileIndex tile)
{
	if (v->type != VehicleType::Train || v->vehstatus.Test(VehState::Crashed)) return false;

	const Train *t = Train::From(v);
	if (!t->IsMovingFront()) return false;

	return TrainApproachingCrossingTile(t) == tile;
}


/**
 * Finds a vehicle approaching rail-road crossing
 * @param tile tile to test
 * @return true if a vehicle is approaching the crossing
 * @pre tile is a rail-road crossing
 */
static bool TrainApproachingCrossing(TileIndex tile)
{
	assert(IsLevelCrossingTile(tile));

	DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
	TileIndex tile_from = tile + TileOffsByDiagDir(dir);

	if (HasVehicleOnTile(tile_from, [&](const Vehicle *v) {
			return TrainApproachingCrossingEnum(v, tile);
		})) return true;

	dir = ReverseDiagDir(dir);
	tile_from = tile + TileOffsByDiagDir(dir);

	return HasVehicleOnTile(tile_from, [&](const Vehicle *v) {
		return TrainApproachingCrossingEnum(v, tile);
	});
}

/**
 * Check if a level crossing should be barred.
 * @param tile The tile to check.
 * @return True if the crossing should be barred, else false.
 */
static inline bool CheckLevelCrossing(TileIndex tile)
{
	/* reserved || train on crossing || train approaching crossing */
	return HasCrossingReservation(tile) || TrainOnCrossing(tile) || TrainApproachingCrossing(tile);
}

/**
 * Sets a level crossing tile to the correct state.
 * @param tile Tile to update.
 * @param sound Should we play sound?
 * @param force_barred Should we set the crossing to barred?
 * @pre tile is a rail-road crossing.
 */
static void UpdateLevelCrossingTile(TileIndex tile, bool sound, bool force_barred)
{
	assert(IsLevelCrossingTile(tile));
	bool set_barred;

	/* We force the crossing to be barred when an adjacent crossing is barred, otherwise let it decide for itself. */
	set_barred = force_barred || CheckLevelCrossing(tile);

	/* The state has changed */
	if (set_barred != IsCrossingBarred(tile)) {
		if (set_barred && sound && _settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
		SetCrossingBarred(tile, set_barred);
		MarkTileDirtyByTile(tile);
	}
}

/**
 * Update a level crossing to barred or open (crossing may include multiple adjacent tiles).
 * @param tile Tile which causes the update.
 * @param sound Should we play sound?
 * @param force_bar Should we force the crossing to be barred?
 */
void UpdateLevelCrossing(TileIndex tile, bool sound, bool force_bar)
{
	if (!IsLevelCrossingTile(tile)) return;

	bool forced_state = force_bar;

	Axis axis = GetCrossingRoadAxis(tile);
	DiagDirections diagdirs = AxisToDiagDirs(axis);

	/* Check if an adjacent crossing is barred. */
	for (DiagDirection dir : diagdirs) {
		for (TileIndex t = tile; !forced_state && t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == axis; t = TileAddByDiagDir(t, dir)) {
			forced_state |= CheckLevelCrossing(t);
		}
	}

	/* Now that we know whether all tiles in this crossing should be barred or open,
	 * we need to update those tiles. We start with the tile itself, then look along the road axis. */
	UpdateLevelCrossingTile(tile, sound, forced_state);
	for (DiagDirection dir : diagdirs) {
		for (TileIndex t = TileAddByDiagDir(tile, dir); t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == axis; t = TileAddByDiagDir(t, dir)) {
			UpdateLevelCrossingTile(t, sound, forced_state);
		}
	}
}

/**
 * Find adjacent level crossing tiles in this multi-track crossing and mark them dirty.
 * @param tile The tile which causes the update.
 * @param road_axis The road axis.
 */
void MarkDirtyAdjacentLevelCrossingTiles(TileIndex tile, Axis road_axis)
{
	for (DiagDirection dir : AxisToDiagDirs(road_axis)) {
		const TileIndex t = TileAddByDiagDir(tile, dir);
		if (t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis) {
			MarkTileDirtyByTile(t);
		}
	}
}

/**
 * Update adjacent level crossing tiles in this multi-track crossing, due to removal of a level crossing tile.
 * @param tile The crossing tile which has been or is about to be removed, and which caused the update.
 * @param road_axis The road axis.
 */
void UpdateAdjacentLevelCrossingTilesOnLevelCrossingRemoval(TileIndex tile, Axis road_axis)
{
	for (DiagDirection dir : AxisToDiagDirs(road_axis)) {
		const TileIndexDiff diff = TileOffsByDiagDir(dir);
		bool occupied = false;
		for (TileIndex t = tile + diff; t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis; t += diff) {
			occupied |= CheckLevelCrossing(t);
		}
		if (occupied) {
			/* Mark the immediately adjacent tile dirty */
			const TileIndex t = tile + diff;
			if (t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis) {
				MarkTileDirtyByTile(t);
			}
		} else {
			/* Unbar the crossing tiles in this direction as necessary */
			for (TileIndex t = tile + diff; t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis; t += diff) {
				if (IsCrossingBarred(t)) {
					/* The crossing tile is barred, unbar it and continue to check the next tile */
					SetCrossingBarred(t, false);
					MarkTileDirtyByTile(t);
				} else {
					/* The crossing tile is already unbarred, mark the tile dirty and stop checking */
					MarkTileDirtyByTile(t);
					break;
				}
			}
		}
	}
}

/**
 * Bars crossing and plays ding-ding sound if not barred already
 * @param tile tile with crossing
 * @pre tile is a rail-road crossing
 */
static inline void MaybeBarCrossingWithSound(TileIndex tile)
{
	if (!IsCrossingBarred(tile)) {
		SetCrossingReservation(tile, true);
		UpdateLevelCrossing(tile, true);
	}
}


/**
 * Advances wagons for train reversing, needed for variable length wagons.
 * This one is called before the train is reversed.
 * @param moving_front Moving front vehicle
 */
static void AdvanceWagonsBeforeSwap(Train *moving_front)
{
	Train *base = moving_front;
	Train *first = base; // first vehicle to move
	Train *last = moving_front->GetMovingBack(); // last vehicle to move
	uint length = CountVehiclesInChain(moving_front->First());

	while (length > 2) {
		last = last->GetMovingPrev();
		first = first->GetMovingNext();

		int differential = base->CalcNextVehicleOffset() - last->CalcNextVehicleOffset();

		/* do not update images now
		 * negative differential will be handled in AdvanceWagonsAfterSwap() */
		for (int i = 0; i < differential; i++) TrainController(first, last->GetMovingNext());

		base = first; // == base->GetMovingNext()
		length -= 2;
	}
}


/**
 * Advances wagons for train reversing, needed for variable length wagons.
 * This one is called after the train is reversed.
 * @param moving_front Moving front vehicle
 */
static void AdvanceWagonsAfterSwap(Train *moving_front)
{
	/* first of all, fix the situation when the train was entering a depot */
	Train *dep = moving_front; // last vehicle in front of just left depot
	while (dep->GetMovingNext() != nullptr && (dep->track == Track::Depot || dep->GetMovingNext()->track != Track::Depot)) {
		dep = dep->GetMovingNext(); // find first vehicle outside of a depot, with next vehicle inside a depot
	}

	Train *leave = dep->GetMovingNext(); // first vehicle in a depot we are leaving now

	if (leave != nullptr) {
		/* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */
		int d = TicksToLeaveDepot(dep);

		if (d <= 0) {
			leave->vehstatus.Reset(VehState::Hidden); // move it out of the depot
			leave->track = GetRailDepotTrack(leave->tile);
			for (int i = 0; i >= d; i--) TrainController(leave, nullptr); // maybe move it, and maybe let another wagon leave
		}
	} else {
		dep = nullptr; // no vehicle in a depot, so no vehicle leaving a depot
	}

	Train *base = moving_front;
	Train *first = base; // first vehicle to move
	Train *last = moving_front->GetMovingBack(); // last vehicle to move
	uint length = CountVehiclesInChain(moving_front->First());

	/* We have to make sure all wagons that leave a depot because of train reversing are moved correctly
	 * they have already correct spacing, so we have to make sure they are moved how they should */
	bool nomove = (dep == nullptr); // If there is no vehicle leaving a depot, limit the number of wagons moved immediately.

	while (length > 2) {
		/* we reached vehicle (originally) in front of a depot, stop now
		 * (we would move wagons that are already moved with new wagon length). */
		if (base == dep) break;

		/* the last wagon was that one leaving a depot, so do not move it anymore */
		if (last == dep) nomove = true;

		last = last->GetMovingPrev();
		first = first->GetMovingNext();

		int differential = last->CalcNextVehicleOffset() - base->CalcNextVehicleOffset();

		/* do not update images now */
		for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->GetMovingNext() : nullptr));

		base = first; // == base->GetMovingNext()
		length -= 2;
	}
}

static bool IsWholeTrainInsideDepot(const Train *v)
{
	for (const Train *u = v; u != nullptr; u = u->Next()) {
		if (u->track != Track::Depot || u->tile != v->tile) return false;
	}
	return true;
}

/**
 * Turn a train around.
 * @param consist %Train to turn around.
 */
static void ReverseTrainDirection(Train *consist)
{
	Train *moving_front = consist->GetMovingFront();
	if (IsRailDepotTile(moving_front->tile)) {
		if (IsWholeTrainInsideDepot(consist)) return;
		InvalidateWindowData(WindowClass::VehicleDepot, moving_front->tile);
	}

	/* Clear path reservation in front if train is not stuck. */
	if (!consist->flags.Test(VehicleRailFlag::Stuck)) FreeTrainTrackReservation(consist);

	/* Check if we were approaching a rail/road-crossing */
	TileIndex crossing = TrainApproachingCrossingTile(moving_front);

	/* Check if we should back up or flip the train. */
	if (consist->vehicle_flags.Test(VehicleFlag::DrivingBackwards) || _settings_game.difficulty.train_flip_reverse_allowed == TrainFlipReversingAllowed::None || consist->Last()->CanLeadTrain()) {
		/* The train will back up. */
		consist->vehicle_flags.Flip(VehicleFlag::DrivingBackwards);

		for (Train *u = consist; u != nullptr; u = u->Next()) {
			/* Invert going up/down */
			if (u->gv_flags.Any({GroundVehicleFlag::GoingUp, GroundVehicleFlag::GoingDown})) {
				u->gv_flags.Flip({GroundVehicleFlag::GoingUp, GroundVehicleFlag::GoingDown});
			}
			UpdateStatusAfterSwap(u, false);
		}
		/* We may have entered a depot and stopped driving backwards. */
		moving_front = consist->GetMovingFront();
	} else {
		/* The train will flip. */
		AdvanceWagonsBeforeSwap(moving_front);

		/* swap start<>end, start+1<>end-1, ... */
		ReverseTrainSwapVehicles(consist);

		AdvanceWagonsAfterSwap(moving_front);
	}

	if (IsRailDepotTile(moving_front->tile)) {
		InvalidateWindowData(WindowClass::VehicleDepot, moving_front->tile);
	}

	consist->flags.Flip(VehicleRailFlag::Reversed);
	consist->flags.Reset(VehicleRailFlag::Reversing);

	/* recalculate cached data */
	consist->ConsistChanged(CCF_TRACK);

	/* update all images */
	for (Train *u = consist; u != nullptr; u = u->Next()) u->UpdateViewport(false, false);

	/* update crossing we were approaching */
	if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);

	/* maybe we are approaching crossing now, after reversal */
	crossing = TrainApproachingCrossingTile(moving_front);
	if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);

	/* If we are inside a depot after reversing, don't bother with path reserving. */
	if (moving_front->track == Track::Depot) {
		/* Can't be stuck here as inside a depot is always a safe tile. */
		if (consist->flags.Test(VehicleRailFlag::Stuck)) SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
		consist->flags.Reset(VehicleRailFlag::Stuck);
		return;
	}

	/* VehicleExitDir does not always produce the desired dir for depots and
	 * tunnels/bridges that is needed for UpdateSignalsOnSegment. */
	DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
	if (IsRailDepotTile(moving_front->tile) || IsTileType(moving_front->tile, TileType::TunnelBridge)) dir = DiagDirection::Invalid;

	if (UpdateSignalsOnSegment(moving_front->tile, dir, consist->owner) == SigSegState::Path || _settings_game.pf.reserve_paths) {
		/* If we are currently on a tile with conventional signals, we can't treat the
		 * current tile as a safe tile or we would enter a PBS block without a reservation. */
		bool first_tile_okay = !HasBlockSignalOnTrackdir(moving_front->tile, moving_front->GetVehicleTrackdir());

		/* If we are on a depot tile facing outwards, do not treat the current tile as safe. */
		if (IsRailDepotTile(moving_front->tile) && TrackdirToExitdir(moving_front->GetVehicleTrackdir()) == GetRailDepotDirection(moving_front->tile)) first_tile_okay = false;

		if (IsRailStationTile(moving_front->tile)) SetRailStationPlatformReservation(moving_front->tile, TrackdirToExitdir(moving_front->GetVehicleTrackdir()), true);
		if (TryPathReserve(consist, false, first_tile_okay)) {
			/* Do a look-ahead now in case our current tile was already a safe tile. */
			CheckNextTrainTile(consist);
		} else if (consist->current_order.GetType() != OT_LOADING) {
			/* Do not wait for a way out when we're still loading */
			MarkTrainAsStuck(consist);
		}
	} else if (consist->flags.Test(VehicleRailFlag::Stuck)) {
		/* A train not inside a PBS block can't be stuck. */
		consist->flags.Reset(VehicleRailFlag::Stuck);
		consist->wait_counter = 0;
	}
}

/**
 * Reverse train.
 * @param flags type of operation
 * @param veh_id train to reverse
 * @param reverse_single_veh if true, reverse a unit in a train (needs to be in a depot)
 * @return the cost of this operation or an error
 */
CommandCost CmdReverseTrainDirection(DoCommandFlags flags, VehicleID veh_id, bool reverse_single_veh)
{
	Train *v = Train::GetIfValid(veh_id);
	if (v == nullptr) return CMD_ERROR;

	CommandCost ret = CheckOwnership(v->owner);
	if (ret.Failed()) return ret;

	if (reverse_single_veh) {
		/* turn a single unit around */

		if (v->IsMultiheaded() || EngInfo(v->engine_type)->callback_mask.Test(VehicleCallbackMask::ArticEngine)) {
			return CommandCost(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE_MULTIPLE_UNITS);
		}

		Train *front = v->First();
		/* make sure the vehicle is stopped in the depot */
		if (!front->IsStoppedInDepot()) {
			return CommandCost(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
		}

		if (flags.Test(DoCommandFlag::Execute)) {
			v->flags.Flip(VehicleRailFlag::Flipped);

			front->ConsistChanged(CCF_ARRANGE);
			SetWindowDirty(WindowClass::VehicleDepot, front->tile);
			SetWindowDirty(WindowClass::VehicleDetails, front->index);
			InvalidateWindowData(WindowClass::VehicleView, front->index);
			SetWindowClassesDirty(WindowClass::TrainList);
		}
	} else {
		/* turn the whole train around */
		if (!v->IsPrimaryVehicle()) return CMD_ERROR;
		if (v->vehstatus.Test(VehState::Crashed) || v->breakdown_ctr != 0) return CMD_ERROR;

		if (flags.Test(DoCommandFlag::Execute)) {
			/* Properly leave the station if we are loading and won't be loading anymore */
			if (v->current_order.IsType(OT_LOADING)) {
				const Vehicle *moving_back = v->GetMovingBack();

				/* not a station || different station --> leave the station */
				if (!IsTileType(moving_back->tile, TileType::Station) || GetStationIndex(moving_back->tile) != GetStationIndex(v->GetMovingFront()->tile)) {
					v->LeaveStation();
				}
			}

			/* We cancel any 'skip signal at dangers' here */
			v->force_proceed = TFP_NONE;
			InvalidateWindowData(WindowClass::VehicleView, v->index);

			if (_settings_game.vehicle.train_acceleration_model != AccelerationModel::Original && v->cur_speed != 0) {
				v->flags.Flip(VehicleRailFlag::Reversing);
			} else {
				v->cur_speed = 0;
				v->SetLastSpeed();
				HideFillingPercent(&v->fill_percent_te_id);
				ReverseTrainDirection(v);
			}

			/* Unbunching data is no longer valid. */
			v->ResetDepotUnbunching();
		}
	}
	return CommandCost();
}

/**
 * Determine to what force_proceed should be changed.
 * If we are forced to proceed, cancel that order.
 * If we are marked stuck we would want to force the train to
 * proceed to the next signal unless we are stuck just before
 * the next signal. In the other cases we would like to pass
 * the signal at danger and run till the next signal we encounter.
 * @param t The train to determine the new value of force_proceed for.
 * @return The next state of force_proceed.
 */
static TrainForceProceeding DetermineNextTrainForceProceeding(const Train *t)
{
	if (t->vehstatus.Test(VehState::Crashed) || t->force_proceed == TFP_SIGNAL) return TFP_NONE;
	if (!t->flags.Test(VehicleRailFlag::Stuck)) return t->IsChainInDepot() ? TFP_STUCK : TFP_SIGNAL;

	const Train *moving_front = t->GetMovingFront();
	TileIndex next_tile = TileAddByDiagDir(moving_front->tile, TrackdirToExitdir(moving_front->GetVehicleTrackdir()));
	if (next_tile == INVALID_TILE || !IsTileType(next_tile, TileType::Railway) || !HasSignals(next_tile)) return TFP_STUCK;
	TrackBits new_tracks = DiagdirReachesTracks(TrackdirToExitdir(moving_front->GetVehicleTrackdir())) & GetTrackBits(next_tile);
	return new_tracks.Any() && HasSignalOnTrack(next_tile, FindFirstTrack(new_tracks)) ? TFP_SIGNAL : TFP_STUCK;
}

/**
 * Force a train through a red signal
 * @param flags type of operation
 * @param veh_id train to ignore the red signal
 * @return the cost of this operation or an error
 */
CommandCost CmdForceTrainProceed(DoCommandFlags flags, VehicleID veh_id)
{
	Train *t = Train::GetIfValid(veh_id);
	if (t == nullptr) return CMD_ERROR;

	if (!t->IsPrimaryVehicle()) return CMD_ERROR;

	CommandCost ret = CheckOwnership(t->owner);
	if (ret.Failed()) return ret;


	if (flags.Test(DoCommandFlag::Execute)) {
		t->force_proceed = DetermineNextTrainForceProceeding(t);
		InvalidateWindowData(WindowClass::VehicleView, t->index);

		/* Unbunching data is no longer valid. */
		t->ResetDepotUnbunching();
	}

	return CommandCost();
}

/**
 * Try to find a depot nearby.
 * @param v %Train that wants a depot.
 * @param max_distance Maximal search distance.
 * @return Information where the closest train depot is located.
 * @pre The given vehicle must not be crashed!
 */
static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
{
	assert(!v->vehstatus.Test(VehState::Crashed));

	return YapfTrainFindNearestDepot(v, max_distance);
}

ClosestDepot Train::FindClosestDepot()
{
	FindDepotData tfdd = FindClosestTrainDepot(this, 0);
	if (tfdd.best_length == UINT_MAX) return ClosestDepot();

	return ClosestDepot(tfdd.tile, GetDepotIndex(tfdd.tile), tfdd.reverse);
}

void Train::PlayLeaveStationSound(bool force) const
{
	static const SoundFx sfx[] = {
		SND_04_DEPARTURE_STEAM,
		SND_0A_DEPARTURE_TRAIN,
		SND_0A_DEPARTURE_TRAIN,
		SND_47_DEPARTURE_MONORAIL,
		SND_41_DEPARTURE_MAGLEV
	};

	if (PlayVehicleSound(this, VSE_START, force)) return;

	SndPlayVehicleFx(sfx[to_underlying(RailVehInfo(this->engine_type)->engclass)], this);
}

/**
 * Check if the train is on the last reserved tile and try to extend the path then.
 * @param consist %Train that needs its path extended.
 */
static void CheckNextTrainTile(Train *consist)
{
	/* Don't do any look-ahead if path_backoff_interval is 255. */
	if (_settings_game.pf.path_backoff_interval == 255) return;

	const Train *moving_front = consist->GetMovingFront();

	/* Exit if we are inside a depot. */
	if (moving_front->track == Track::Depot) return;

	switch (consist->current_order.GetType()) {
		/* Exit if we reached our destination depot. */
		case OT_GOTO_DEPOT:
			if (moving_front->tile == consist->dest_tile) return;
			break;

		case OT_GOTO_WAYPOINT:
			/* If we reached our waypoint, make sure we see that. */
			if (IsRailWaypointTile(moving_front->tile) && GetStationIndex(moving_front->tile) == consist->current_order.GetDestination()) ProcessOrders(consist);
			break;

		case OT_NOTHING:
		case OT_LEAVESTATION:
		case OT_LOADING:
			/* Exit if the current order doesn't have a destination, but the train has orders. */
			if (consist->GetNumOrders() > 0) return;
			break;

		default:
			break;
	}
	/* Exit if we are on a station tile and are going to stop. */
	if (IsRailStationTile(moving_front->tile) && consist->current_order.ShouldStopAtStation(consist, GetStationIndex(moving_front->tile))) return;

	Trackdir td = moving_front->GetVehicleTrackdir();

	/* On a tile with a red non-pbs signal, don't look ahead. */
	if (HasBlockSignalOnTrackdir(moving_front->tile, td) && GetSignalStateByTrackdir(moving_front->tile, td) == SignalState::Red) return;

	CFollowTrackRail ft(consist);
	if (!ft.Follow(moving_front->tile, td)) return;

	if (!HasReservedTracks(ft.new_tile, TrackdirBitsToTrackBits(ft.new_td_bits))) {
		/* Next tile is not reserved. */
		if (ft.new_td_bits.Count() == 1) {
			if (HasPbsSignalOnTrackdir(ft.new_tile, FindFirstTrackdir(ft.new_td_bits))) {
				/* If the next tile is a PBS signal, try to make a reservation. */
				TrackBits tracks = TrackdirBitsToTrackBits(ft.new_td_bits);
				if (Rail90DegTurnDisallowed(GetTileRailType(ft.old_tile), GetTileRailType(ft.new_tile))) {
					tracks.Reset(TrackCrossesTracks(TrackdirToTrack(ft.old_td)));
				}
				ChooseTrainTrack(consist, ft.new_tile, ft.exitdir, tracks, false, nullptr, false);
			}
		}
	}
}

/**
 * Will the train stay in the depot the next tick?
 * @param v %Train to check.
 * @return True if it stays in the depot, false otherwise.
 */
static bool CheckTrainStayInDepot(Train *v)
{
	/* bail out if not all wagons are in the same depot or not in a depot at all */
	for (const Train *u = v; u != nullptr; u = u->Next()) {
		if (u->track != Track::Depot || u->tile != v->tile) return false;
	}

	/* if the train got no power, then keep it in the depot */
	if (v->gcache.cached_power == 0) {
		v->vehstatus.Set(VehState::Stopped);
		SetWindowDirty(WindowClass::VehicleDepot, v->tile);
		return true;
	}

	/* Check if we should wait here for unbunching. */
	if (v->IsWaitingForUnbunching()) return true;

	SigSegState seg_state;

	if (v->force_proceed == TFP_NONE) {
		/* force proceed was not pressed */
		if (++v->wait_counter < 37) {
			SetWindowClassesDirty(WindowClass::TrainList);
			return true;
		}

		v->wait_counter = 0;

		seg_state = _settings_game.pf.reserve_paths ? SigSegState::Path : UpdateSignalsOnSegment(v->tile, DiagDirection::Invalid, v->owner);
		if (seg_state == SigSegState::Full || HasDepotReservation(v->tile)) {
			/* Full and no PBS signal in block or depot reserved, can't exit. */
			SetWindowClassesDirty(WindowClass::TrainList);
			return true;
		}
	} else {
		seg_state = _settings_game.pf.reserve_paths ? SigSegState::Path : UpdateSignalsOnSegment(v->tile, DiagDirection::Invalid, v->owner);
	}

	/* We are leaving a depot, but have to go to the exact same one; re-enter. */
	if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
		/* Service when depot has no reservation. */
		if (!HasDepotReservation(v->tile)) VehicleEnterDepot(v);
		return true;
	}

	/* Only leave when we can reserve a path to our destination. */
	if (seg_state == SigSegState::Path && !TryPathReserve(v) && v->force_proceed == TFP_NONE) {
		/* No path and no force proceed. */
		SetWindowClassesDirty(WindowClass::TrainList);
		MarkTrainAsStuck(v);
		return true;
	}

	SetDepotReservation(v->tile, true);
	if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);

	VehicleServiceInDepot(v);
	v->LeaveUnbunchingDepot();
	v->PlayLeaveStationSound();
	SetWindowClassesDirty(WindowClass::TrainList);

	v->track = AxisToTrack(DiagDirToAxis(DirToDiagDir(v->direction)));

	v->vehstatus.Reset(VehState::Hidden);
	v->cur_speed = 0;

	v->UpdateViewport(true, true);
	v->UpdatePosition();
	UpdateSignalsOnSegment(v->tile, DiagDirection::Invalid, v->owner);
	v->UpdateAcceleration();
	InvalidateWindowData(WindowClass::VehicleDepot, v->tile);

	return false;
}

/**
 * Clear the reservation of \a tile that was just left by a wagon on \a track_dir.
 * @param v %Train owning the reservation.
 * @param tile Tile with reservation to clear.
 * @param track_dir Track direction to clear.
 */
static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
{
	DiagDirection dir = TrackdirToExitdir(track_dir);

	if (IsTileType(tile, TileType::TunnelBridge)) {
		/* Are we just leaving a tunnel/bridge? */
		if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(dir)) {
			TileIndex end = GetOtherTunnelBridgeEnd(tile);

			if (TunnelBridgeIsFree(tile, end, v).Succeeded()) {
				/* Free the reservation only if no other train is on the tiles. */
				SetTunnelBridgeReservation(tile, false);
				SetTunnelBridgeReservation(end, false);

				if (_settings_client.gui.show_track_reservation) {
					if (IsBridge(tile)) {
						MarkBridgeDirty(tile);
					} else {
						MarkTileDirtyByTile(tile);
						MarkTileDirtyByTile(end);
					}
				}
			}
		}
	} else if (IsRailStationTile(tile)) {
		TileIndex new_tile = TileAddByDiagDir(tile, dir);
		/* If the new tile is not a further tile of the same station, we
		 * clear the reservation for the whole platform. */
		if (!IsCompatibleTrainStationTile(new_tile, tile)) {
			SetRailStationPlatformReservation(tile, ReverseDiagDir(dir), false);
		}
	} else {
		/* Any other tile */
		UnreserveRailTrack(tile, TrackdirToTrack(track_dir));
	}
}

/**
 * Free the reserved path in front of a vehicle.
 * @param consist %Train owning the reserved path.
 */
void FreeTrainTrackReservation(const Train *consist)
{
	assert(consist->IsFrontEngine());

	const Train *moving_front = consist->GetMovingFront();
	TileIndex tile = moving_front->tile;
	Trackdir td = moving_front->GetVehicleTrackdir();
	bool free_tile = tile != moving_front->tile || !(IsRailStationTile(moving_front->tile) || IsTileType(moving_front->tile, TileType::TunnelBridge));
	StationID station_id = IsRailStationTile(moving_front->tile) ? GetStationIndex(moving_front->tile) : StationID::Invalid();

	/* Can't be holding a reservation if we enter a depot. */
	if (IsRailDepotTile(tile) && TrackdirToExitdir(td) != GetRailDepotDirection(tile)) return;
	if (moving_front->track == Track::Depot) {
		/* Front engine is in a depot. We enter if some part is not in the depot. */
		for (const Train *u = consist; u != nullptr; u = u->Next()) {
			if (u->track != Track::Depot || u->tile != consist->tile) return;
		}
	}
	/* Don't free reservation if it's not ours. */
	if (TracksOverlap(GetReservedTrackbits(tile) | TrackdirToTrack(td))) return;

	CFollowTrackRail ft(consist, GetAllCompatibleRailTypes(consist->railtypes));
	while (ft.Follow(tile, td)) {
		tile = ft.new_tile;
		TrackdirBits bits = ft.new_td_bits & TrackBitsToTrackdirBits(GetReservedTrackbits(tile));
		td = RemoveFirstTrackdir(bits);
		assert(bits.None());

		if (!IsValidTrackdir(td)) break;

		if (IsTileType(tile, TileType::Railway)) {
			if (HasSignalOnTrackdir(tile, td) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)))) {
				/* Conventional signal along trackdir: remove reservation and stop. */
				UnreserveRailTrack(tile, TrackdirToTrack(td));
				break;
			}
			if (HasPbsSignalOnTrackdir(tile, td)) {
				if (GetSignalStateByTrackdir(tile, td) == SignalState::Red) {
					/* Red PBS signal? Can't be our reservation, would be green then. */
					break;
				} else {
					/* Turn the signal back to red. */
					SetSignalStateByTrackdir(tile, td, SignalState::Red);
					MarkTileDirtyByTile(tile);
				}
			} else if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(td))) {
				/* Reservation passes an opposing path signal. Mark signal for update to re-establish the proper default state. */
				AddSideToSignalBuffer(tile, TrackdirToExitdir(ReverseTrackdir(td)), consist->owner);
			} else if (HasSignalOnTrackdir(tile, ReverseTrackdir(td)) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
				break;
			}
		}

		/* Don't free first station/bridge/tunnel if we are on it. */
		if (free_tile || (!(ft.is_station && GetStationIndex(ft.new_tile) == station_id) && !ft.is_tunnel && !ft.is_bridge)) ClearPathReservation(consist, tile, td);

		free_tile = true;
	}

	UpdateSignalsInBuffer();
}

/**
 * Perform pathfinding for a train.
 *
 * @param v The train
 * @param tile The tile the train is about to enter
 * @param enterdir Diagonal direction the train is coming from
 * @param tracks Usable tracks on the new tile
 * @param[out] path_found Whether a path has been found or not.
 * @param do_track_reservation Path reservation is requested
 * @param[out] dest State and destination of the requested path
 * @param[out] final_dest Final tile of the best path found
 * @return The best track the train should follow
 */
static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest, TileIndex *final_dest)
{
	if (final_dest != nullptr) *final_dest = INVALID_TILE;
	return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest, final_dest);
}

/**
 * Extend a train path as far as possible. Stops on encountering a safe tile,
 * another reservation or a track choice.
 * @param v The train to extend the reservation for.
 * @param new_tracks Optional pointer to the track bits at the end of the reservation.
 * @param enterdir Optional pointer to the enter dir at the end of the reservation.
 * @return Information about the reservation, empty if no path could be found.
 */
static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, DiagDirection *enterdir)
{
	PBSTileInfo origin = FollowTrainReservation(v);

	CFollowTrackRail ft(v);

	std::vector<std::pair<TileIndex, Trackdir>> signals_set_to_red;

	TileIndex tile = origin.tile;
	Trackdir  cur_td = origin.trackdir;
	while (ft.Follow(tile, cur_td)) {
		if (ft.new_td_bits.Count() == 1) {
			/* Possible signal tile. */
			if (HasOnewaySignalBlockingTrackdir(ft.new_tile, FindFirstTrackdir(ft.new_td_bits))) break;
		}

		if (Rail90DegTurnDisallowed(GetTileRailType(ft.old_tile), GetTileRailType(ft.new_tile))) {
			ft.new_td_bits.Reset(TrackdirCrossesTrackdirs(ft.old_td));
			if (ft.new_td_bits.None()) break;
		}

		/* Station, depot or waypoint are a possible target. */
		bool target_seen = ft.is_station || (IsTileType(ft.new_tile, TileType::Railway) && !IsPlainRail(ft.new_tile));
		if (target_seen || ft.new_td_bits.Count() > 1) {
			/* Choice found or possible target encountered.
			 * On finding a possible target, we need to stop and let the pathfinder handle the
			 * remaining path. This is because we don't know if this target is in one of our
			 * orders, so we might cause pathfinding to fail later on if we find a choice.
			 * This failure would cause a bogus call to TryReserveSafePath which might reserve
			 * a wrong path not leading to our next destination. */
			if (HasReservedTracks(ft.new_tile, TrackdirBitsToTrackBits(TrackdirReachesTrackdirs(ft.old_td)))) break;

			/* If we did skip some tiles, backtrack to the first skipped tile so the pathfinder
			 * actually starts its search at the first unreserved tile. */
			if (ft.tiles_skipped != 0) ft.new_tile -= TileOffsByDiagDir(ft.exitdir) * ft.tiles_skipped;

			/* Choice found, path valid but not okay. Save info about the choice tile as well. */
			if (new_tracks != nullptr) *new_tracks = TrackdirBitsToTrackBits(ft.new_td_bits);
			if (enterdir != nullptr) *enterdir = ft.exitdir;
			return PBSTileInfo(ft.new_tile, ft.old_td, false);
		}

		tile = ft.new_tile;
		cur_td = FindFirstTrackdir(ft.new_td_bits);

		Trackdir rev_td = ReverseTrackdir(cur_td);
		if (IsSafeWaitingPosition(v, tile, cur_td, true, _settings_game.pf.forbid_90_deg)) {
			bool wp_free = IsWaitingPositionFree(v, tile, cur_td, _settings_game.pf.forbid_90_deg);
			if (!(wp_free && TryReserveRailTrack(tile, TrackdirToTrack(cur_td)))) break;
			/* Green path signal opposing the path? Turn to red. */
			if (HasPbsSignalOnTrackdir(tile, rev_td) && GetSignalStateByTrackdir(tile, rev_td) == SignalState::Green) {
				signals_set_to_red.emplace_back(tile, rev_td);
				SetSignalStateByTrackdir(tile, rev_td, SignalState::Red);
				MarkTileDirtyByTile(tile);
			}
			/* Safe position is all good, path valid and okay. */
			return PBSTileInfo(tile, cur_td, true);
		}

		if (!TryReserveRailTrack(tile, TrackdirToTrack(cur_td))) break;

		/* Green path signal opposing the path? Turn to red. */
		if (HasPbsSignalOnTrackdir(tile, rev_td) && GetSignalStateByTrackdir(tile, rev_td) == SignalState::Green) {
			signals_set_to_red.emplace_back(tile, rev_td);
			SetSignalStateByTrackdir(tile, rev_td, SignalState::Red);
			MarkTileDirtyByTile(tile);
		}
	}

	if (ft.err == CFollowTrackRail::ErrorCode::Owner || ft.err == CFollowTrackRail::ErrorCode::NoWay) {
		/* End of line, path valid and okay. */
		return PBSTileInfo(ft.old_tile, ft.old_td, true);
	}

	/* Sorry, can't reserve path, back out. */
	tile = origin.tile;
	cur_td = origin.trackdir;
	TileIndex stopped = ft.old_tile;
	Trackdir  stopped_td = ft.old_td;
	while (tile != stopped || cur_td != stopped_td) {
		if (!ft.Follow(tile, cur_td)) break;

		if (Rail90DegTurnDisallowed(GetTileRailType(ft.old_tile), GetTileRailType(ft.new_tile))) {
			ft.new_td_bits.Reset(TrackdirCrossesTrackdirs(ft.old_td));
			assert(ft.new_td_bits.Any());
		}
		assert(ft.new_td_bits.Count() == 1);

		tile = ft.new_tile;
		cur_td = FindFirstTrackdir(ft.new_td_bits);

		UnreserveRailTrack(tile, TrackdirToTrack(cur_td));
	}

	/* Re-instate green signals we turned to red. */
	for (auto [sig_tile, td] : signals_set_to_red) {
		SetSignalStateByTrackdir(sig_tile, td, SignalState::Green);
	}

	/* Path invalid. */
	return PBSTileInfo();
}

/**
 * Try to reserve any path to a safe tile, ignoring the vehicle's destination.
 * Safe tiles are tiles in front of a signal, depots and station tiles at end of line.
 *
 * @param v The vehicle.
 * @param tile The tile the search should start from.
 * @param td The trackdir the search should start from.
 * @param override_railtype Whether all physically compatible railtypes should be followed.
 * @return True if a path to a safe stopping tile could be reserved.
 */
static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
{
	return YapfTrainFindNearestSafeTile(v, tile, td, override_railtype);
}

/** This class will save the current order of a vehicle and restore it on destruction. */
class VehicleOrderSaver {
private:
	Train          *v;
	Order          old_order;
	TileIndex      old_dest_tile;
	StationID      old_last_station_visited;
	VehicleOrderID index;
	bool           suppress_implicit_orders;
	bool           restored;

public:
	VehicleOrderSaver(Train *_v) :
		v(_v),
		old_order(_v->current_order),
		old_dest_tile(_v->dest_tile),
		old_last_station_visited(_v->last_station_visited),
		index(_v->cur_real_order_index),
		suppress_implicit_orders(_v->gv_flags.Test(GroundVehicleFlag::SuppressImplicitOrders)),
		restored(false)
	{
	}

	/**
	 * Restore the saved order to the vehicle.
	 */
	void Restore()
	{
		this->v->current_order = this->old_order;
		this->v->dest_tile = this->old_dest_tile;
		this->v->last_station_visited = this->old_last_station_visited;
		this->v->gv_flags.Set(GroundVehicleFlag::SuppressImplicitOrders, suppress_implicit_orders);
		this->restored = true;
	}

	/**
	 * Restore the saved order to the vehicle, if Restore() has not already been called.
	 */
	~VehicleOrderSaver()
	{
		if (!this->restored) this->Restore();
	}

	/**
	 * Set the current vehicle order to the next order in the order list.
	 * @param skip_first Shall the first (i.e. active) order be skipped?
	 * @return True if a suitable next order could be found.
	 */
	bool SwitchToNextOrder(bool skip_first)
	{
		if (this->v->GetNumOrders() == 0) return false;

		if (skip_first) ++this->index;

		int depth = 0;

		do {
			/* Wrap around. */
			if (this->index >= this->v->GetNumOrders()) this->index = 0;

			Order *order = this->v->GetOrder(this->index);
			assert(order != nullptr);

			switch (order->GetType()) {
				case OT_GOTO_DEPOT:
					/* Skip service in depot orders when the train doesn't need service. */
					if (order->GetDepotOrderType().Test(OrderDepotTypeFlag::Service) && !this->v->NeedsServicing()) break;
					[[fallthrough]];
				case OT_GOTO_STATION:
				case OT_GOTO_WAYPOINT:
					this->v->current_order = *order;
					return UpdateOrderDest(this->v, order, 0, true);
				case OT_CONDITIONAL: {
					VehicleOrderID next = ProcessConditionalOrder(order, this->v);
					if (next != INVALID_VEH_ORDER_ID) {
						depth++;
						this->index = next;
						/* Don't increment next, so no break here. */
						continue;
					}
					break;
				}
				default:
					break;
			}
			/* Don't increment inside the while because otherwise conditional
			 * orders can lead to an infinite loop. */
			++this->index;
			depth++;
		} while (this->index != this->v->cur_real_order_index && depth < this->v->GetNumOrders());

		return false;
	}
};

/* choose a track */
static Track ChooseTrainTrack(Train *consist, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck)
{
	Track best_track = Track::Invalid;
	bool do_track_reservation = _settings_game.pf.reserve_paths || force_res;
	bool changed_signal = false;
	TileIndex final_dest = INVALID_TILE;

	assert(tracks == (tracks & TRACK_BIT_ALL));

	if (got_reservation != nullptr) *got_reservation = false;

	/* Don't use tracks here as the setting to forbid 90 deg turns might have been switched between reservation and now. */
	TrackBits res_tracks = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
	/* Do we have a suitable reserved track? */
	if (res_tracks.Any()) return FindFirstTrack(res_tracks);

	/* Quick return in case only one possible track is available */
	if (tracks.Count() == 1) {
		Track track = FindFirstTrack(tracks);
		/* We need to check for signals only here, as a junction tile can't have signals. */
		if (IsValidTrack(track) && HasPbsSignalOnTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir))) {
			do_track_reservation = true;
			changed_signal = true;
			SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir), SignalState::Green);
		} else if (!do_track_reservation) {
			return track;
		}
		best_track = track;
	}

	const Train *moving_front = consist->GetMovingFront();

	PBSTileInfo   res_dest(tile, Trackdir::Invalid, false);
	DiagDirection dest_enterdir = enterdir;
	if (do_track_reservation) {
		res_dest = ExtendTrainReservation(consist, &tracks, &dest_enterdir);
		if (res_dest.tile == INVALID_TILE) {
			/* Reservation failed? */
			if (mark_stuck) MarkTrainAsStuck(consist);
			if (changed_signal) SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(best_track, enterdir), SignalState::Red);
			return FindFirstTrack(tracks);
		}
		if (res_dest.okay) {
			/* Got a valid reservation that ends at a safe target, quick exit. */
			if (got_reservation != nullptr) *got_reservation = true;
			if (changed_signal) MarkTileDirtyByTile(tile);
			TryReserveRailTrack(moving_front->tile, TrackdirToTrack(moving_front->GetVehicleTrackdir()));
			return best_track;
		}

		/* Check if the train needs service here, so it has a chance to always find a depot.
		 * Also check if the current order is a service order so we don't reserve a path to
		 * the destination but instead to the next one if service isn't needed. */
		CheckIfTrainNeedsService(consist);
		if (consist->current_order.IsType(OT_DUMMY) || consist->current_order.IsType(OT_CONDITIONAL) || consist->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(consist);
	}

	/* Save the current train order. The destructor will restore the old order on function exit. */
	VehicleOrderSaver orders(consist);

	/* If the current tile is the destination of the current order and
	 * a reservation was requested, advance to the next order.
	 * Don't advance on a depot order as depots are always safe end points
	 * for a path and no look-ahead is necessary. This also avoids a
	 * problem with depot orders not part of the order list when the
	 * order list itself is empty. */
	if (consist->current_order.IsType(OT_LEAVESTATION)) {
		orders.SwitchToNextOrder(false);
	} else if (consist->current_order.IsType(OT_LOADING) || (!consist->current_order.IsType(OT_GOTO_DEPOT) && (
			consist->current_order.IsType(OT_GOTO_STATION) ?
			IsRailStationTile(moving_front->tile) && consist->current_order.GetDestination() == GetStationIndex(moving_front->tile) :
			moving_front->tile == consist->dest_tile))) {
		orders.SwitchToNextOrder(true);
	}

	if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
		/* Pathfinders are able to tell that route was only 'guessed'. */
		bool      path_found = true;
		TileIndex new_tile = res_dest.tile;

		Track next_track = DoTrainPathfind(consist, new_tile, dest_enterdir, tracks, path_found, do_track_reservation, &res_dest, &final_dest);
		if (new_tile == tile) best_track = next_track;
		consist->HandlePathfindingResult(path_found);
	}

	/* No track reservation requested -> finished. */
	if (!do_track_reservation) return best_track;

	/* A path was found, but could not be reserved. */
	if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
		if (mark_stuck) MarkTrainAsStuck(consist);
		FreeTrainTrackReservation(consist);
		return best_track;
	}

	/* No possible reservation target found, we are probably lost. */
	if (res_dest.tile == INVALID_TILE) {
		/* Try to find any safe destination. */
		PBSTileInfo origin = FollowTrainReservation(consist);
		if (TryReserveSafeTrack(consist, origin.tile, origin.trackdir, false)) {
			TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
			best_track = FindFirstTrack(res);
			TryReserveRailTrack(moving_front->tile, TrackdirToTrack(moving_front->GetVehicleTrackdir()));
			if (got_reservation != nullptr) *got_reservation = true;
			if (changed_signal) MarkTileDirtyByTile(tile);
		} else {
			FreeTrainTrackReservation(consist);
			if (mark_stuck) MarkTrainAsStuck(consist);
		}
		return best_track;
	}

	if (got_reservation != nullptr) *got_reservation = true;

	/* Reservation target found and free, check if it is safe. */
	while (!IsSafeWaitingPosition(consist, res_dest.tile, res_dest.trackdir, true, _settings_game.pf.forbid_90_deg)) {
		/* Extend reservation until we have found a safe position. */
		DiagDirection exitdir = TrackdirToExitdir(res_dest.trackdir);
		TileIndex next_tile = TileAddByDiagDir(res_dest.tile, exitdir);
		TrackBits reachable = TrackdirBitsToTrackBits(GetTileTrackStatus(next_tile, TransportType::Rail, RoadTramType::Invalid).trackdirs) & DiagdirReachesTracks(exitdir);
		if (Rail90DegTurnDisallowed(GetTileRailType(res_dest.tile), GetTileRailType(next_tile))) {
			reachable.Reset(TrackCrossesTracks(TrackdirToTrack(res_dest.trackdir)));
		}

		/* Get next order with destination. */
		if (orders.SwitchToNextOrder(true)) {
			PBSTileInfo cur_dest;
			bool path_found;
			DoTrainPathfind(consist, next_tile, exitdir, reachable, path_found, true, &cur_dest, nullptr);
			if (cur_dest.tile != INVALID_TILE) {
				res_dest = cur_dest;
				if (res_dest.okay) continue;
				/* Path found, but could not be reserved. */
				FreeTrainTrackReservation(consist);
				if (mark_stuck) MarkTrainAsStuck(consist);
				if (got_reservation != nullptr) *got_reservation = false;
				changed_signal = false;
				break;
			}
		}
		/* No order or no safe position found, try any position. */
		if (!TryReserveSafeTrack(consist, res_dest.tile, res_dest.trackdir, true)) {
			FreeTrainTrackReservation(consist);
			if (mark_stuck) MarkTrainAsStuck(consist);
			if (got_reservation != nullptr) *got_reservation = false;
			changed_signal = false;
		}
		break;
	}

	TryReserveRailTrack(moving_front->tile, TrackdirToTrack(moving_front->GetVehicleTrackdir()));

	if (changed_signal) MarkTileDirtyByTile(tile);

	orders.Restore();
	if (consist->current_order.IsType(OT_GOTO_DEPOT) &&
			consist->current_order.GetDepotActionType().Test(OrderDepotActionFlag::NearestDepot) &&
			final_dest != INVALID_TILE && IsRailDepotTile(final_dest)) {
		consist->current_order.SetDestination(GetDepotIndex(final_dest));
		consist->dest_tile = final_dest;
		SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
	}

	return best_track;
}

/**
 * Try to reserve a path to a safe position.
 *
 * @param consist The vehicle
 * @param mark_as_stuck Should the train be marked as stuck on a failed reservation?
 * @param first_tile_okay True if no path should be reserved if the current tile is a safe position.
 * @return True if a path could be reserved.
 */
bool TryPathReserve(Train *consist, bool mark_as_stuck, bool first_tile_okay)
{
	assert(consist->IsFrontEngine());

	const Train *moving_front = consist->GetMovingFront();

	/* We have to handle depots specially as the track follower won't look
	 * at the depot tile itself but starts from the next tile. If we are still
	 * inside the depot, a depot reservation can never be ours. */
	if (moving_front->track == Track::Depot) {
		if (HasDepotReservation(moving_front->tile)) {
			if (mark_as_stuck) MarkTrainAsStuck(consist);
			return false;
		} else {
			/* Depot not reserved, but the next tile might be. */
			TileIndex next_tile = TileAddByDiagDir(moving_front->tile, GetRailDepotDirection(moving_front->tile));
			if (HasReservedTracks(next_tile, DiagdirReachesTracks(GetRailDepotDirection(moving_front->tile)))) return false;
		}
	}

	Vehicle *other_train = nullptr;
	PBSTileInfo origin = FollowTrainReservation(consist, &other_train);
	/* The path we are driving on is already blocked by some other train.
	 * This can only happen in certain situations when mixing path and
	 * block signals or when changing tracks and/or signals.
	 * Exit here as doing any further reservations will probably just
	 * make matters worse. */
	if (other_train != nullptr && other_train->index != consist->index) {
		if (mark_as_stuck) MarkTrainAsStuck(consist);
		return false;
	}
	/* If we have a reserved path and the path ends at a safe tile, we are finished already. */
	if (origin.okay && (moving_front->tile != origin.tile || first_tile_okay)) {
		/* Can't be stuck then. */
		if (consist->flags.Test(VehicleRailFlag::Stuck)) SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
		consist->flags.Reset(VehicleRailFlag::Stuck);
		return true;
	}

	/* If we are in a depot, tentatively reserve the depot. */
	if (moving_front->track == Track::Depot) {
		SetDepotReservation(moving_front->tile, true);
		if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(moving_front->tile);
	}

	DiagDirection exitdir = TrackdirToExitdir(origin.trackdir);
	TileIndex new_tile = TileAddByDiagDir(origin.tile, exitdir);
	TrackBits reachable = TrackdirBitsToTrackBits(GetTileTrackStatus(new_tile, TransportType::Rail, RoadTramType::Invalid).trackdirs & DiagdirReachesTrackdirs(exitdir));

	if (Rail90DegTurnDisallowed(GetTileRailType(origin.tile), GetTileRailType(new_tile))) reachable.Reset(TrackCrossesTracks(TrackdirToTrack(origin.trackdir)));

	bool res_made = false;
	ChooseTrainTrack(consist, new_tile, exitdir, reachable, true, &res_made, mark_as_stuck);

	if (!res_made) {
		/* Free the depot reservation as well. */
		if (moving_front->track == Track::Depot) SetDepotReservation(moving_front->tile, false);
		return false;
	}

	if (consist->flags.Test(VehicleRailFlag::Stuck)) {
		consist->wait_counter = 0;
		SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
	}
	consist->flags.Reset(VehicleRailFlag::Stuck);
	return true;
}

/**
 * Can the train reverse?
 * @param consist The train to check.
 * @return \c true iff the train can be reversed.
 */
static bool CheckReverseTrain(const Train *consist)
{
	const Train *moving_front = consist->GetMovingFront();
	if (_settings_game.difficulty.train_flip_reverse_allowed == TrainFlipReversingAllowed::EndOfLineOnly ||
			moving_front->track == Track::Depot || moving_front->track == Track::Wormhole ||
			!IsDiagonalDirection(moving_front->GetMovingDirection())) {
		return false;
	}

	assert(moving_front->track.Any());

	return YapfTrainCheckReverse(consist);
}

/**
 * Get the location of the next station to visit.
 * @param station Next station to visit.
 * @return Location of the new station.
 */
TileIndex Train::GetOrderStationLocation(StationID station)
{
	if (station == this->last_station_visited) this->last_station_visited = StationID::Invalid();

	const Station *st = Station::Get(station);
	if (!st->facilities.Test(StationFacility::Train)) {
		/* The destination station has no trainstation tiles. */
		this->IncrementRealOrderIndex();
		return TileIndex{};
	}

	return st->xy;
}

/** Goods at the consist have changed, update the graphics, cargo, and acceleration. */
void Train::MarkDirty()
{
	Train *v = this;
	do {
		v->colourmap = PAL_NONE;
		v->UpdateViewport(true, false);
	} while ((v = v->Next()) != nullptr);

	/* need to update acceleration and cached values since the goods on the train changed. */
	this->CargoChanged();
	this->UpdateAcceleration();
}

/**
 * This function looks at the vehicle and updates its speed (cur_speed
 * and subspeed) variables. Furthermore, it returns the distance that
 * the train can drive this tick. #Vehicle::GetAdvanceDistance() determines
 * the distance to drive before moving a step on the map.
 * @return distance to drive.
 */
int Train::UpdateSpeed()
{
	switch (_settings_game.vehicle.train_acceleration_model) {
		default: NOT_REACHED();
		case AccelerationModel::Original:
			return this->DoUpdateSpeed(this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2), 0, this->GetCurrentMaxSpeed());

		case AccelerationModel::Realistic:
			return this->DoUpdateSpeed(this->GetAcceleration(), this->GetAccelerationStatus() == AS_BRAKE ? 0 : 2, this->GetCurrentMaxSpeed());
	}
}

/**
 * Trains enters a station, send out a news item if it is the first train, and start loading.
 * @param consist Train that entered the station.
 * @param station Station visited.
 */
static void TrainEnterStation(Train *consist, StationID station)
{
	consist->last_station_visited = station;

	/* check if a train ever visited this station before */
	Station *st = Station::Get(station);
	if (!st->had_vehicle_of_type.Test(StationVehicleType::Train)) {
		st->had_vehicle_of_type.Set(StationVehicleType::Train);
		AddVehicleNewsItem(
			GetEncodedString(STR_NEWS_FIRST_TRAIN_ARRIVAL, st->index),
			consist->owner == _local_company ? NewsType::ArrivalCompany : NewsType::ArrivalOther,
			consist->index,
			st->index
		);
		AI::NewEvent(consist->owner, new ScriptEventStationFirstVehicle(st->index, consist->index));
		Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, consist->index));
	}

	consist->force_proceed = TFP_NONE;
	InvalidateWindowData(WindowClass::VehicleView, consist->index);

	consist->BeginLoading();

	TileIndex tile = consist->GetMovingFront()->tile;
	TriggerStationRandomisation(st, tile, StationRandomTrigger::VehicleArrives);
	TriggerStationAnimation(st, tile, StationAnimationTrigger::VehicleArrives);
}

/**
 * Check if the vehicle is compatible with the specified tile.
 * @param v The train to check.
 * @param tile The tile to check.
 * @param check_railtype Should we check the railtype for compatibility?
 * @return \c true iff the tile is compatible with the train.
 */
static inline bool CheckCompatibleRail(const Train *v, TileIndex tile, bool check_railtype)
{
	return IsTileOwner(tile, v->owner) &&
			(!check_railtype || !v->IsFrontEngine() || v->compatible_railtypes.Test(GetRailType(tile)));
}

/** Data structure for storing engine speed changes of an acceleration type. */
struct AccelerationSlowdownParams {
	uint8_t small_turn; ///< Speed change due to a small turn.
	uint8_t large_turn; ///< Speed change due to a large turn.
	uint8_t z_up;       ///< Fraction to remove when moving up.
	uint8_t z_down;     ///< Fraction to add when moving down.
};

/** Speed update fractions for each acceleration type. */
static const AccelerationSlowdownParams _accel_slowdown[] = {
	/* normal accel */
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
	{0,       256 / 2, 256 / 4, 2}, ///< maglev
};

/**
 * Modify the speed of the vehicle due to a change in altitude.
 * @param consist %Train to update.
 * @param z_diff Z difference new - old.
 */
static inline void AffectSpeedByZChange(Train *consist, int z_diff)
{
	if (z_diff == 0 || _settings_game.vehicle.train_acceleration_model != AccelerationModel::Original) return;

	const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast<int>(consist->GetAccelerationType())];

	if (z_diff > 0) {
		consist->cur_speed -= (consist->cur_speed * asp->z_up >> 8);
	} else {
		uint16_t spd = consist->cur_speed + asp->z_down;
		if (spd <= consist->gcache.cached_max_track_speed) consist->cur_speed = spd;
	}
}

static bool TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
{
	if (IsTileType(tile, TileType::Railway) &&
			GetRailTileType(tile) == RailTileType::Signals) {
		TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir);
		Trackdir trackdir = FindFirstTrackdir(tracks);
		if (UpdateSignalsOnSegment(tile,  TrackdirToExitdir(trackdir), GetTileOwner(tile)) == SigSegState::Path && HasSignalOnTrackdir(tile, trackdir)) {
			/* A PBS block with a non-PBS signal facing us? */
			if (!IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
		}
	}
	return false;
}

/** Tries to reserve track under whole train consist. */
void Train::ReserveTrackUnderConsist() const
{
	for (const Train *u = this; u != nullptr; u = u->Next()) {
		switch (u->track.base()) {
			case TrackBits{Track::Wormhole}.base():
				TryReserveRailTrack(u->tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(u->tile)));
				break;
			case TrackBits{Track::Depot}.base():
				break;
			default:
				TryReserveRailTrack(u->tile, TrackBitsToTrack(u->track));
				break;
		}
	}
}

/**
 * The train vehicle crashed!
 * Update its status and other parts around it.
 * @param flooded Crash was caused by flooding.
 * @return Number of people killed.
 */
uint Train::Crash(bool flooded)
{
	uint victims = 0;
	if (this->IsFrontEngine()) {
		victims += 2; // driver

		/* Remove the reserved path in front of the train if it is not stuck.
		 * Also clear all reserved tracks the train is currently on. */
		if (!this->flags.Test(VehicleRailFlag::Stuck)) FreeTrainTrackReservation(this);
		for (const Train *v = this; v != nullptr; v = v->Next()) {
			ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
			if (IsTileType(v->tile, TileType::TunnelBridge)) {
				/* ClearPathReservation will not free the wormhole exit
				 * if the train has just entered the wormhole. */
				SetTunnelBridgeReservation(GetOtherTunnelBridgeEnd(v->tile), false);
			}
		}

		/* we may need to update crossing we were approaching,
		 * but must be updated after the train has been marked crashed */
		TileIndex crossing = TrainApproachingCrossingTile(this->GetMovingFront());
		if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);

		/* Remove the loading indicators (if any) */
		HideFillingPercent(&this->fill_percent_te_id);
	}

	victims += this->GroundVehicleBase::Crash(flooded);

	this->crash_anim_pos = flooded ? 4000 : 1; // max 4440, disappear pretty fast when flooded
	return victims;
}

/**
 * Marks train as crashed and creates an AI event.
 * Doesn't do anything if the train is crashed already.
 * @param v first vehicle of chain
 * @return number of victims (including 2 drivers; zero if train was already crashed)
 */
static uint TrainCrashed(Train *v)
{
	uint victims = 0;

	/* do not crash train twice */
	if (!v->vehstatus.Test(VehState::Crashed)) {
		victims = v->Crash();
		TileIndex tile = v->GetMovingFront()->tile;
		AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, tile, ScriptEventVehicleCrashed::CRASH_TRAIN, victims, v->owner));
		Game::NewEvent(new ScriptEventVehicleCrashed(v->index, tile, ScriptEventVehicleCrashed::CRASH_TRAIN, victims, v->owner));
	}

	/* Try to re-reserve track under already crashed train too.
	 * Crash() clears the reservation! */
	v->ReserveTrackUnderConsist();

	return victims;
}

/**
 * Collision test function.
 * @param v The %Train vehicle we may have collided with.
 * @param moving_front The %Train vehicle being examined.
 * @return Number of victims.
 */
static uint CheckTrainCollision(Vehicle *v, Train *moving_front)
{
	/* Make sure we are a train, and are not in a depot. */
	if (v->type != VehicleType::Train) return 0;

	/* We can't crash into trains in a depot. */
	if (Train::From(v)->track == Track::Depot) return 0;

	/* Do not crash into trains of another company. */
	if (v->owner != moving_front->First()->owner) return 0;

	/* Do not collide with our own wagons */
	if (v->First() == moving_front->First()) return 0;

	int x_diff = v->x_pos - moving_front->x_pos;
	int y_diff = v->y_pos - moving_front->y_pos;

	/* Do fast calculation to check whether trains are not in close vicinity
	 * and quickly reject trains distant enough for any collision.
	 * Differences are shifted by 7, mapping range [-7 .. 8] into [0 .. 15]
	 * Differences are then ORed and then we check for any higher bits */
	uint hash = (y_diff + 7) | (x_diff + 7);
	if (hash & ~15) return 0;

	/* Slower check using multiplication */
	int min_diff = (Train::From(v)->gcache.cached_veh_length + 1) / 2 + (moving_front->gcache.cached_veh_length + 1) / 2 - 1;
	if (x_diff * x_diff + y_diff * y_diff > min_diff * min_diff) return 0;

	/* Happens when there is a train under bridge next to bridge head */
	if (abs(v->z_pos - moving_front->z_pos) > 5) return 0;

	/* Crash both trains. Two statements required to guarantee execution
	 * order because RandomRange() is involved. */
	uint num_victims = TrainCrashed(moving_front->First());
	return num_victims + TrainCrashed(Train::From(v)->First());
}

/**
 * Checks whether the specified train has a collision with another vehicle. If
 * so, destroys this vehicle, and the other vehicle if its subtype has TS_Front.
 * Reports the incident in a flashy news item, modifies station ratings and
 * plays a sound.
 * @param moving_front %Train to test.
 * @return \c true iff there has been a collision.
 */
static bool CheckTrainCollision(Train *moving_front)
{
	/* can't collide in depot */
	if (moving_front->track == Track::Depot) return false;

	assert(moving_front->track == Track::Wormhole || TileVirtXY(moving_front->x_pos, moving_front->y_pos) == moving_front->tile);

	uint num_victims = 0;

	/* find colliding vehicles */
	if (moving_front->track == Track::Wormhole) {
		for (Vehicle *u : VehiclesOnTile(moving_front->tile)) {
			num_victims += CheckTrainCollision(u, moving_front);
		}
		for (Vehicle *u : VehiclesOnTile(GetOtherTunnelBridgeEnd(moving_front->tile))) {
			num_victims += CheckTrainCollision(u, moving_front);
		}
	} else {
		for (Vehicle *u : VehiclesNearTileXY(moving_front->x_pos, moving_front->y_pos, 7)) {
			num_victims += CheckTrainCollision(u, moving_front);
		}
	}

	/* any dead -> no crash */
	if (num_victims == 0) return false;

	AddTileNewsItem(GetEncodedString(STR_NEWS_TRAIN_CRASH, num_victims), NewsType::Accident, moving_front->tile);

	ModifyStationRatingAround(moving_front->tile, moving_front->First()->owner, -160, 30);
	if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_13_TRAIN_COLLISION, moving_front);
	return true;
}

/**
 * Move a vehicle chain one movement stop forwards.
 * @param v First vehicle to move.
 * @param nomove Stop moving this and all following vehicles.
 * @param reverse Set to false to not execute the vehicle reversing. This does not change any other logic.
 * @return True if the vehicle could be moved forward, false otherwise.
 */
bool TrainController(Train *v, Vehicle *nomove, bool reverse)
{
	Train *first = v->First();
	Train *prev;
	bool direction_changed = false; // has direction of any part changed?

	/* For every vehicle after and including the given vehicle */
	for (prev = v->GetMovingPrev(); v != nomove; prev = v, v = v->GetMovingNext()) {
		DiagDirection enterdir = DiagDirection::Begin;
		bool update_signals_crossing = false; // will we update signals or crossing state?

		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
		if (v->track != Track::Wormhole) {
			/* Not inside tunnel */
			if (gp.old_tile == gp.new_tile) {
				/* Staying in the old tile */
				if (v->track == Track::Depot) {
					/* Inside depot */
					gp.x = v->x_pos;
					gp.y = v->y_pos;
				} else {
					/* Not inside depot */

					/* Reverse when we are at the end of the track already, do not move to the new position */
					if (v->IsMovingFront() && !TrainCheckIfLineEnds(v, reverse)) return false;

					auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
					if (vets.Test(VehicleEnterTileState::CannotEnter)) {
						goto invalid_rail;
					}
					if (vets.Test(VehicleEnterTileState::EnteredStation)) {
						/* The new position is the end of the platform */
						TrainEnterStation(first, GetStationIndex(gp.new_tile));
					}
				}
			} else {
				/* A new tile is about to be entered. */

				/* Determine what direction we're entering the new tile from */
				enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
				assert(IsValidDiagDirection(enterdir));

				/* Get the status of the tracks in the new tile and mask
				 * away the bits that aren't reachable. */
				TrackStatus ts = GetTileTrackStatus(gp.new_tile, TransportType::Rail, RoadTramType::Invalid, ReverseDiagDir(enterdir));
				TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);

				TrackdirBits trackdirbits = ts.trackdirs & reachable_trackdirs;
				TrackBits red_signals = TrackdirBitsToTrackBits(ts.signals & reachable_trackdirs);

				TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
				if (Rail90DegTurnDisallowed(GetTileRailType(gp.old_tile), GetTileRailType(gp.new_tile)) && prev == nullptr) {
					/* We allow wagons to make 90 deg turns, because forbid_90_deg
					 * can be switched on halfway a turn */
					bits.Reset(TrackCrossesTracks(FindFirstTrack(v->track)));
				}

				if (bits.None()) goto invalid_rail;

				/* Check if the new tile constrains tracks that are compatible
				 * with the current train, if not, bail out. */
				if (!CheckCompatibleRail(v->First(), gp.new_tile, v->IsMovingFront())) goto invalid_rail;

				TrackBits chosen_track;
				if (v->IsMovingFront()) {
					/* Currently the locomotive is active. Determine which one of the
					 * available tracks to choose */
					chosen_track = ChooseTrainTrack(first, gp.new_tile, enterdir, bits, false, nullptr, true);
					assert(chosen_track.Any(bits | GetReservedTrackbits(gp.new_tile)));

					if (first->force_proceed != TFP_NONE && IsPlainRailTile(gp.new_tile) && HasSignals(gp.new_tile)) {
						/* For each signal we find decrease the counter by one.
						 * We start at two, so the first signal we pass decreases
						 * this to one, then if we reach the next signal it is
						 * decreased to zero and we won't pass that new signal. */
						Trackdir dir = FindFirstTrackdir(trackdirbits);
						if (HasSignalOnTrackdir(gp.new_tile, dir) ||
								(HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(dir)) &&
								GetSignalType(gp.new_tile, TrackdirToTrack(dir)) != SignalType::Path)) {
							/* However, we do not want to be stopped by PBS signals
							 * entered via the back. */
							first->force_proceed = (first->force_proceed == TFP_SIGNAL) ? TFP_STUCK : TFP_NONE;
							InvalidateWindowData(WindowClass::VehicleView, first->index);
						}
					}

					/* Check if it's a red signal and that force proceed is not clicked. */
					if (red_signals.Any(chosen_track) && first->force_proceed == TFP_NONE) {
						/* In front of a red signal */
						Trackdir i = FindFirstTrackdir(trackdirbits);

						/* Don't handle stuck trains here. */
						if (first->flags.Test(VehicleRailFlag::Stuck)) return false;

						if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
							first->cur_speed = 0;
							first->subspeed = 0;
							first->progress = 255; // make sure that every bit of acceleration will hit the signal again, so speed stays 0.
							if (!_settings_game.pf.reverse_at_signals || ++first->wait_counter < _settings_game.pf.wait_oneway_signal * Ticks::DAY_TICKS * 2) return false;
						} else if (HasSignalOnTrackdir(gp.new_tile, i)) {
							first->cur_speed = 0;
							first->subspeed = 0;
							first->progress = 255; // make sure that every bit of acceleration will hit the signal again, so speed stays 0.
							if (!_settings_game.pf.reverse_at_signals || ++first->wait_counter < _settings_game.pf.wait_twoway_signal * Ticks::DAY_TICKS * 2) {
								DiagDirection exitdir = TrackdirToExitdir(i);
								TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);

								exitdir = ReverseDiagDir(exitdir);

								/* check if a train is waiting on the other side */
								if (!HasVehicleOnTile(o_tile, [&exitdir](const Vehicle *u) {
										if (u->type != VehicleType::Train || u->vehstatus.Test(VehState::Crashed)) return false;
										const Train *t = Train::From(u);

										/* not front engine of a train, inside wormhole or depot, crashed */
										if (!t->IsFrontEngine() || t->track.Any({Track::Wormhole, Track::Depot})) return false;

										if (t->cur_speed > 5 || VehicleExitDir(t->direction, t->track) != exitdir) return false;

										return true;
									})) return false;
							}
						}

						/* If we would reverse but are currently in a PBS block and
						 * reversing of stuck trains is disabled, don't reverse.
						 * This does not apply if the reason for reversing is a one-way
						 * signal blocking us, because a train would then be stuck forever. */
						if (!_settings_game.pf.reverse_at_signals && !HasOnewaySignalBlockingTrackdir(gp.new_tile, i) &&
								UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SigSegState::Path) {
							first->wait_counter = 0;
							return false;
						}
						goto reverse_train_direction;
					} else {
						TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track), false);
					}
				} else {
					/* The wagon is active, simply follow the prev vehicle. */
					if (prev->tile == gp.new_tile) {
						/* Choose the same track as prev */
						if (prev->track == Track::Wormhole) {
							/* Vehicles entering tunnels enter the wormhole earlier than for bridges.
							 * However, just choose the track into the wormhole. */
							assert(IsTunnel(prev->tile));
							chosen_track = bits;
						} else {
							chosen_track = prev->track;
						}
					} else {
						/* Choose the track that leads to the tile where prev is.
						 * This case is active if 'prev' is already on the second next tile, when 'v' just enters the next tile.
						 * I.e. when the tile between them has only space for a single vehicle like
						 *  1) horizontal/vertical track tiles and
						 *  2) some orientations of tunnel entries, where the vehicle is already inside the wormhole at 8/16 from the tile edge.
						 *     Is also the train just reversing, the wagon inside the tunnel is 'on' the tile of the opposite tunnel entry.
						 */
						static const DiagDirectionIndexArray<DiagDirectionIndexArray<TrackBits>> _connecting_track{{{
							{{{Track::X,     Track::Lower, {},           Track::Left }}},
							{{{Track::Upper, Track::Y,     Track::Left,  {}          }}},
							{{{{},           Track::Right, Track::X,     Track::Upper}}},
							{{{Track::Right, {},           Track::Lower, Track::Y    }}}
						}}};
						DiagDirection exitdir = DiagdirBetweenTiles(gp.new_tile, prev->tile);
						assert(IsValidDiagDirection(exitdir));
						chosen_track = _connecting_track[enterdir][exitdir];
					}
					chosen_track &= bits;
				}

				/* Update XY to reflect the entrance to the new tile, and select the direction to use */
				Direction chosen_dir = VehicleEnterTileCoordinates(gp, enterdir, TrackBitsToTrack(chosen_track));

				/* Call the landscape function and tell it that the vehicle entered the tile */
				auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
				if (vets.Test(VehicleEnterTileState::CannotEnter)) {
					goto invalid_rail;
				}

				if (!vets.Test(VehicleEnterTileState::EnteredWormhole)) {
					Track track = FindFirstTrack(chosen_track);
					Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
					if (v->IsMovingFront() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
						SetSignalStateByTrackdir(gp.new_tile, tdir, SignalState::Red);
						MarkTileDirtyByTile(gp.new_tile);
					}

					/* Clear any track reservation when the last vehicle leaves the tile */
					if (v->GetMovingNext() == nullptr) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());

					v->tile = gp.new_tile;

					if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
						first->ConsistChanged(CCF_TRACK);
					}

					v->track = chosen_track;
					assert(v->track.Any());
				}

				/* We need to update signal status, but after the vehicle position hash
				 * has been updated by UpdateInclination() */
				update_signals_crossing = true;

				if (chosen_dir != v->GetMovingDirection()) {
					if (prev == nullptr && _settings_game.vehicle.train_acceleration_model == AccelerationModel::Original) {
						const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast<int>(v->GetAccelerationType())];
						DirDiff diff = DirDifference(v->direction, chosen_dir);
						v->cur_speed -= (diff == DirDiff::Right45 || diff == DirDiff::Left45 ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8;
					}
					direction_changed = true;
					v->SetMovingDirection(chosen_dir);
				}

				if (v->IsMovingFront()) {
					first->wait_counter = 0;

					/* If we are approaching a crossing that is reserved, play the sound now. */
					TileIndex crossing = TrainApproachingCrossingTile(v); // We know we are the moving front, so we can check v.
					if (crossing != INVALID_TILE && HasCrossingReservation(crossing) && _settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, crossing);

					/* Always try to extend the reservation when entering a tile. */
					CheckNextTrainTile(first);
				}

				if (vets.Test(VehicleEnterTileState::EnteredStation)) {
					/* The new position is the location where we want to stop */
					TrainEnterStation(first, GetStationIndex(gp.new_tile));
				}
			}
		} else {
			if (IsTileType(gp.new_tile, TileType::TunnelBridge) && VehicleEnterTile(v, gp.new_tile, gp.x, gp.y).Test(VehicleEnterTileState::EnteredWormhole)) {
				/* Perform look-ahead on tunnel exit. */
				if (v->IsMovingFront()) {
					TryReserveRailTrack(gp.new_tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(gp.new_tile)));
					CheckNextTrainTile(first);
				}
				/* Prevent v->UpdateInclination() being called with wrong parameters.
				 * This could happen if the train was reversed inside the tunnel/bridge. */
				if (gp.old_tile == gp.new_tile) {
					gp.old_tile = GetOtherTunnelBridgeEnd(gp.old_tile);
				}
			} else {
				v->x_pos = gp.x;
				v->y_pos = gp.y;
				v->UpdatePosition();
				if (!v->vehstatus.Test(VehState::Hidden)) v->Vehicle::UpdateViewport(true);
				continue;
			}
		}

		/* update image of train, as well as delta XY */
		v->UpdateDeltaXY();

		v->x_pos = gp.x;
		v->y_pos = gp.y;
		v->UpdatePosition();

		/* update the Z position of the vehicle */
		int old_z = v->UpdateInclination(gp.new_tile != gp.old_tile, false);

		if (prev == nullptr) {
			/* This is the first vehicle in the train */
			AffectSpeedByZChange(first, v->z_pos - old_z);
		}

		if (update_signals_crossing) {
			if (v->IsMovingFront()) {
				if (TrainMovedChangeSignals(gp.new_tile, enterdir)) {
					/* We are entering a block with PBS signals right now, but
					 * not through a PBS signal. This means we don't have a
					 * reservation right now. As a conventional signal will only
					 * ever be green if no other train is in the block, getting
					 * a path should always be possible. If the player built
					 * such a strange network that it is not possible, the train
					 * will be marked as stuck and the player has to deal with
					 * the problem. */
					if ((!HasReservedTracks(gp.new_tile, v->track) &&
							!TryReserveRailTrack(gp.new_tile, FindFirstTrack(v->track))) ||
							!TryPathReserve(first)) {
						MarkTrainAsStuck(first);
					}
				}
			}

			/* Signals can only change when the first
			 * (above) or the last vehicle moves. */
			if (v->GetMovingNext() == nullptr) {
				TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
				if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile);
			}
		}

		/* Do not check on every tick to save some computing time. */
		if (v->IsMovingFront() && first->tick_counter % _settings_game.pf.path_backoff_interval == 0) CheckNextTrainTile(first);
	}

	if (direction_changed) first->tcache.cached_max_curve_speed = first->GetCurveSpeedLimit();

	return true;

invalid_rail:
	/* We've reached end of line?? */
	if (prev != nullptr) FatalError("Disconnecting train");

reverse_train_direction:
	if (reverse) {
		first->wait_counter = 0;
		first->cur_speed = 0;
		first->subspeed = 0;
		ReverseTrainDirection(first);
	}

	return false;
}

static bool IsRailStationPlatformOccupied(TileIndex tile)
{
	TileIndexDiff delta = TileOffsByAxis(GetRailStationAxis(tile));

	for (TileIndex t = tile; IsCompatibleTrainStationTile(t, tile); t -= delta) {
		if (HasVehicleOnTile(t, IsTrain)) return true;
	}
	for (TileIndex t = tile + delta; IsCompatibleTrainStationTile(t, tile); t += delta) {
		if (HasVehicleOnTile(t, IsTrain)) return true;
	}

	return false;
}

/**
 * Deletes/Clears the last wagon of a crashed train. It takes the engine of the
 * train, then goes to the last wagon and deletes that. Each call to this function
 * will remove the last wagon of a crashed train. If this wagon was on a crossing,
 * or inside a tunnel/bridge, recalculate the signals as they might need updating
 * @param v the Vehicle of which last wagon is to be removed
 */
static void DeleteLastWagon(Train *v)
{
	Train *first = v->First();

	/* Go to the last wagon and delete the link pointing there
	 * new_last is then the one-before-last wagon, and v the last
	 * one which will physically be removed */
	Train *new_last = v;
	for (; v->Next() != nullptr; v = v->Next()) new_last = v;
	new_last->SetNext(nullptr);

	if (first != v) {
		/* Recalculate cached train properties */
		first->ConsistChanged(CCF_ARRANGE);
		/* Update the depot window in case a part of the consist is in a depot. */
		SetWindowDirty(WindowClass::VehicleDepot, first->tile);
		SetWindowDirty(WindowClass::VehicleDepot, v->tile);
	}

	/* 'v' shouldn't be accessed after it has been deleted */
	TrackBits trackbits = v->track;
	TileIndex tile = v->tile;
	Owner owner = v->owner;

	delete v;
	v = nullptr; // make sure nobody will try to read 'v' anymore

	if (trackbits == Track::Wormhole) {
		/* Vehicle is inside a wormhole, v->track contains no useful value then. */
		trackbits = DiagDirToDiagTrack(GetTunnelBridgeDirection(tile));
	}

	Track track = TrackBitsToTrack(trackbits);
	if (HasReservedTracks(tile, trackbits)) {
		UnreserveRailTrack(tile, track);

		/* If there are still crashed vehicles on the tile, give the track reservation to them */
		TrackBits remaining_trackbits{};
		for (const Vehicle *u : VehiclesOnTile(tile)) {
			if (u->type != VehicleType::Train || !u->vehstatus.Test(VehState::Crashed)) continue;
			TrackBits train_tbits = Train::From(u)->track;
			if (train_tbits == Track::Wormhole) {
				/* Vehicle is inside a wormhole, u->track contains no useful value then. */
				remaining_trackbits.Set(DiagDirToDiagTrack(GetTunnelBridgeDirection(u->tile)));
			} else if (train_tbits != Track::Depot) {
				remaining_trackbits.Set(train_tbits);
			}
		}

		/* It is important that these two are the first in the loop, as reservation cannot deal with every trackbit combination */
		assert(Track::Begin == Track::X && to_underlying(Track::Y) == to_underlying(Track::Begin) + 1);
		for (Track t : remaining_trackbits) TryReserveRailTrack(tile, t);
	}

	/* check if the wagon was on a road/rail-crossing */
	if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile);

	if (IsRailStationTile(tile)) {
		bool occupied = IsRailStationPlatformOccupied(tile);
		DiagDirection dir = AxisToDiagDir(GetRailStationAxis(tile));
		SetRailStationPlatformReservation(tile, dir, occupied);
		SetRailStationPlatformReservation(tile, ReverseDiagDir(dir), occupied);
	}

	/* Update signals */
	if (IsTileType(tile, TileType::TunnelBridge) || IsRailDepotTile(tile)) {
		UpdateSignalsOnSegment(tile, DiagDirection::Invalid, owner);
	} else {
		SetSignalsOnBothDir(tile, track, owner);
	}
}

/**
 * Rotate all vehicles of a (crashed) train chain randomly to animate the crash.
 * @param v First crashed vehicle.
 */
static void ChangeTrainDirRandomly(Train *v)
{
	static const DirDiff delta[] = {
		DirDiff::Left45, DirDiff::Same, DirDiff::Same, DirDiff::Right45
	};

	do {
		/* We don't need to twist around vehicles if they're not visible */
		if (!v->vehstatus.Test(VehState::Hidden)) {
			v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
			/* Refrain from updating the z position of the vehicle when on
			 * a bridge, because UpdateInclination() will put the vehicle under
			 * the bridge in that case */
			if (v->track != Track::Wormhole) {
				v->UpdatePosition();
				v->UpdateInclination(false, true);
			} else {
				v->UpdateViewport(false, true);
			}
		}
	} while ((v = v->Next()) != nullptr);
}

/**
 * Handle a crashed train.
 * @param v First train vehicle.
 * @return %Vehicle chain still exists.
 */
static bool HandleCrashedTrain(Train *v)
{
	int state = ++v->crash_anim_pos;

	if (state == 4 && !v->vehstatus.Test(VehState::Hidden)) {
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
	}

	uint32_t r;
	if (state <= 200 && Chance16R(1, 7, r)) {
		int index = (r * 10 >> 16);

		Vehicle *u = v;
		do {
			if (--index < 0) {
				r = Random();

				CreateEffectVehicleRel(u,
					GB(r,  8, 3) + 2,
					GB(r, 16, 3) + 2,
					GB(r,  0, 3) + 5,
					EV_EXPLOSION_SMALL);
				break;
			}
		} while ((u = u->Next()) != nullptr);
	}

	if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);

	if (state >= 4440 && !(v->tick_counter & 0x1F)) {
		bool ret = v->Next() != nullptr;
		DeleteLastWagon(v);
		return ret;
	}

	return true;
}

/** Maximum speeds for train that is broken down or approaching line end */
static const uint16_t _breakdown_speeds[16] = {
	225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
};


/**
 * Train is approaching line end, slow down and possibly reverse
 *
 * @param moving_front moving front vehicle
 * @param signal not line end, just a red signal
 * @param reverse Set to false to not execute the vehicle reversing. This does not change any other logic.
 * @return true iff we did NOT have to reverse
 */
static bool TrainApproachingLineEnd(Train *moving_front, bool signal, bool reverse)
{
	/* Calc position within the current tile */
	uint x = moving_front->x_pos & 0xF;
	uint y = moving_front->y_pos & 0xF;

	Direction vdir = moving_front->GetMovingDirection();

	/* for diagonal directions, 'x' will be 0..15 -
	 * for other directions, it will be 1, 3, 5, ..., 15 */
	switch (vdir) {
		case Direction::N : x = ~x + ~y + 25; break;
		case Direction::NW: x = y;            [[fallthrough]];
		case Direction::NE: x = ~x + 16;      break;
		case Direction::E : x = ~x + y + 9;   break;
		case Direction::SE: x = y;            break;
		case Direction::S : x = x + y - 7;    break;
		case Direction::W : x = ~y + x + 9;   break;
		default: break;
	}

	Train *consist = moving_front->First();

	/* Do not reverse when approaching red signal. Make sure the vehicle's front
	 * does not cross the tile boundary when we do reverse, but as the vehicle's
	 * location is based on their center, use half a vehicle's length as offset.
	 * Multiply the half-length by two for straight directions to compensate that
	 * we only get odd x offsets there. */
	uint8_t rounding = moving_front->IsDrivingBackwards() ? 0 : 1;
	if (!signal && x + (moving_front->gcache.cached_veh_length + rounding) / 2 * (IsDiagonalDirection(vdir) ? 1 : 2) >= TILE_SIZE) {
		/* we are too near the tile end, reverse now */
		consist->cur_speed = 0;
		if (reverse) ReverseTrainDirection(consist);
		return false;
	}

	/* slow down */
	consist->vehstatus.Set(VehState::TrainSlowing);
	uint16_t break_speed = _breakdown_speeds[x & 0xF];
	if (break_speed < consist->cur_speed) consist->cur_speed = break_speed;

	return true;
}


/**
 * Determines whether train would like to leave the tile.
 * @param moving_front The moving front vehicle of the train.
 * @return true iff vehicle is NOT entering or inside a depot or tunnel/bridge.
 */
static bool TrainCanLeaveTile(const Train *moving_front)
{
	/* Exit if inside a tunnel/bridge or a depot */
	if (moving_front->track == Track::Wormhole || moving_front->track == Track::Depot) return false;

	TileIndex tile = moving_front->tile;

	/* entering a tunnel/bridge? */
	if (IsTileType(tile, TileType::TunnelBridge)) {
		DiagDirection dir = GetTunnelBridgeDirection(tile);
		if (DiagDirToDir(dir) == moving_front->GetMovingDirection()) return false;
	}

	/* entering a depot? */
	if (IsRailDepotTile(tile)) {
		DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile));
		if (DiagDirToDir(dir) == moving_front->GetMovingDirection()) return false;
	}

	return true;
}


/**
 * Determines whether train is approaching a rail-road crossing
 *   (thus making it barred)
 * @param moving_front moving front of train
 * @return TileIndex of crossing the train is approaching, else INVALID_TILE
 * @pre v in non-crashed front engine
 */
static TileIndex TrainApproachingCrossingTile(const Train *moving_front)
{
	assert(moving_front->IsMovingFront());
	assert(!moving_front->First()->vehstatus.Test(VehState::Crashed));

	if (!TrainCanLeaveTile(moving_front)) return INVALID_TILE;

	DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
	TileIndex tile = moving_front->tile + TileOffsByDiagDir(dir);

	/* not a crossing || wrong axis || unusable rail (wrong type or owner) */
	if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
			!CheckCompatibleRail(moving_front->First(), tile, true)) {
		return INVALID_TILE;
	}

	return tile;
}


/**
 * Checks for line end. Also, bars crossing at next tile if needed
 *
 * @param moving_front moving vehicle front we are checking
 * @param reverse Set to false to not execute the vehicle reversing. This does not change any other logic.
 * @return true iff we did NOT have to reverse
 */
static bool TrainCheckIfLineEnds(Train *moving_front, bool reverse)
{
	/* First, handle broken down train */

	Train *consist = moving_front->First();
	int t = consist->breakdown_ctr;
	if (t > 1) {
		consist->vehstatus.Set(VehState::TrainSlowing);

		uint16_t break_speed = _breakdown_speeds[GB(~t, 4, 4)];
		if (break_speed < consist->cur_speed) consist->cur_speed = break_speed;
	} else {
		consist->vehstatus.Reset(VehState::TrainSlowing);
	}

	if (!TrainCanLeaveTile(moving_front)) return true;

	/* Determine the non-diagonal direction in which we will exit this tile */
	DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
	/* Calculate next tile */
	TileIndex tile = moving_front->tile + TileOffsByDiagDir(dir);

	/* Determine the track status on the next tile */
	TrackStatus ts = GetTileTrackStatus(tile, TransportType::Rail, RoadTramType::Invalid, ReverseDiagDir(dir));
	TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);

	TrackdirBits trackdirbits = ts.trackdirs & reachable_trackdirs;
	TrackdirBits red_signals = ts.signals & reachable_trackdirs;

	/* We are sure the train is not entering a depot, it is detected above */

	/* mask unreachable track bits if we are forbidden to do 90deg turns */
	TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
	if (Rail90DegTurnDisallowed(GetTileRailType(moving_front->tile), GetTileRailType(tile))) {
		bits.Reset(TrackCrossesTracks(FindFirstTrack(moving_front->track)));
	}

	/* no suitable trackbits at all || unusable rail (wrong type or owner) */
	if (bits.None() || !CheckCompatibleRail(consist, tile, true)) {
		return TrainApproachingLineEnd(moving_front, false, reverse);
	}

	/* approaching red signal */
	if (trackdirbits.Any(red_signals)) return TrainApproachingLineEnd(moving_front, true, reverse);

	/* approaching a rail/road crossing? then make it red */
	if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile);

	return true;
}

/**
 * Per-tick handler of each front engine.
 * @param consist The front engine we are working with.
 * @param mode Set to \c True if we want the train to keep existing, \c False if we want to consider deleting it (it has been crashed).
 * @return \c true if we want the train to keep existing, \c False if we want to delete it (it has been crashed).
 */
static bool TrainLocoHandler(Train *consist, bool mode)
{
	/* train has crashed? */
	if (consist->vehstatus.Test(VehState::Crashed)) {
		return mode ? true : HandleCrashedTrain(consist); // 'this' can be deleted here
	}

	if (consist->force_proceed != TFP_NONE) {
		consist->flags.Reset(VehicleRailFlag::Stuck);
		SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
	}

	/* train is broken down? */
	if (consist->HandleBreakdown()) return true;

	if (consist->flags.Test(VehicleRailFlag::Reversing) && consist->cur_speed == 0) {
		ReverseTrainDirection(consist);
	}

	/* exit if train is stopped */
	if (consist->vehstatus.Test(VehState::Stopped) && consist->cur_speed == 0) return true;

	bool valid_order = !consist->current_order.IsType(OT_NOTHING) && consist->current_order.GetType() != OT_CONDITIONAL;
	if (ProcessOrders(consist) && CheckReverseTrain(consist)) {
		consist->wait_counter = 0;
		consist->cur_speed = 0;
		consist->subspeed = 0;
		consist->flags.Reset(VehicleRailFlag::LeavingStation);
		ReverseTrainDirection(consist);
		return true;
	} else if (consist->flags.Test(VehicleRailFlag::LeavingStation)) {
		/* Try to reserve a path when leaving the station as we
		 * might not be marked as wanting a reservation, e.g.
		 * when an overlength train gets turned around in a station. */
		const Train *moving_front = consist->GetMovingFront();
		DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
		if (IsRailDepotTile(moving_front->tile) || IsTileType(moving_front->tile, TileType::TunnelBridge)) dir = DiagDirection::Invalid;

		if (UpdateSignalsOnSegment(moving_front->tile, dir, consist->owner) == SigSegState::Path || _settings_game.pf.reserve_paths) {
			TryPathReserve(consist, true, true);
		}
		consist->flags.Reset(VehicleRailFlag::LeavingStation);
	}

	consist->HandleLoading(mode);

	if (consist->current_order.IsType(OT_LOADING)) return true;

	if (CheckTrainStayInDepot(consist)) return true;

	if (!mode) consist->ShowVisualEffect();

	/* We had no order but have an order now, do look ahead. */
	if (!valid_order && !consist->current_order.IsType(OT_NOTHING)) {
		CheckNextTrainTile(consist);
	}

	/* Handle stuck trains. */
	if (!mode && consist->flags.Test(VehicleRailFlag::Stuck)) {
		++consist->wait_counter;

		/* Should we try reversing this tick if still stuck? */
		bool turn_around = consist->wait_counter % (_settings_game.pf.wait_for_pbs_path * Ticks::DAY_TICKS) == 0 && _settings_game.pf.reverse_at_signals;

		if (!turn_around && consist->wait_counter % _settings_game.pf.path_backoff_interval != 0 && consist->force_proceed == TFP_NONE) return true;
		if (!TryPathReserve(consist)) {
			/* Still stuck. */
			if (turn_around) ReverseTrainDirection(consist);

			if (consist->flags.Test(VehicleRailFlag::Stuck) && consist->wait_counter > 2 * _settings_game.pf.wait_for_pbs_path * Ticks::DAY_TICKS) {
				/* Show message to player. */
				if (_settings_client.gui.lost_vehicle_warn && consist->owner == _local_company) {
					AddVehicleAdviceNewsItem(AdviceType::TrainStuck, GetEncodedString(STR_NEWS_TRAIN_IS_STUCK, consist->index), consist->index);
				}
				consist->wait_counter = 0;
			}
			/* Exit if force proceed not pressed, else reset stuck flag anyway. */
			if (consist->force_proceed == TFP_NONE) return true;
			consist->flags.Reset(VehicleRailFlag::Stuck);
			consist->wait_counter = 0;
			SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
		}
	}

	if (consist->current_order.IsType(OT_LEAVESTATION)) {
		consist->current_order.Free();
		SetWindowWidgetDirty(WindowClass::VehicleView, consist->index, WID_VV_START_STOP);
		return true;
	}

	int j = consist->UpdateSpeed();

	/* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
	if (consist->cur_speed == 0 && consist->vehstatus.Test(VehState::Stopped)) {
		/* If we manually stopped, we're not force-proceeding anymore. */
		consist->force_proceed = TFP_NONE;
		InvalidateWindowData(WindowClass::VehicleView, consist->index);
	}

	Train* moving_front = consist->GetMovingFront();
	int adv_spd = moving_front->GetAdvanceDistance();
	if (j < adv_spd) {
		/* if the vehicle has speed 0, update the last_speed field. */
		if (consist->cur_speed == 0) consist->SetLastSpeed();
	} else {
		TrainCheckIfLineEnds(moving_front);
		moving_front = moving_front->GetMovingFront();
		/* Loop until the train has finished moving. */
		for (;;) {
			j -= adv_spd;
			TrainController(moving_front, nullptr);
			moving_front = moving_front->GetMovingFront();
			/* Don't continue to move if the train crashed. */
			if (CheckTrainCollision(moving_front)) break;
			/* Determine distance to next map position */
			adv_spd = moving_front->GetAdvanceDistance();

			/* No more moving this tick */
			if (j < adv_spd || consist->cur_speed == 0) break;

			OrderType order_type = consist->current_order.GetType();
			/* Do not skip waypoints (incl. 'via' stations) when passing through at full speed. */
			if ((order_type == OT_GOTO_WAYPOINT || order_type == OT_GOTO_STATION) &&
						consist->current_order.GetNonStopType().Test(OrderNonStopFlag::GoVia) &&
						IsTileType(moving_front->tile, TileType::Station) &&
						consist->current_order.GetDestination() == GetStationIndex(moving_front->tile)) {
				ProcessOrders(consist);
			}
		}
		consist->SetLastSpeed();
	}

	for (Train *u = consist; u != nullptr; u = u->Next()) {
		if (u->vehstatus.Test(VehState::Hidden)) continue;

		u->UpdateViewport(false, false);
	}

	if (consist->progress == 0) consist->progress = j; // Save unused spd for next time, if TrainController didn't set progress

	return true;
}

/**
 * Get running cost for the train consist.
 * @return Yearly running costs.
 */
Money Train::GetRunningCost() const
{
	Money cost = 0;
	const Train *v = this;

	do {
		const Engine *e = v->GetEngine();
		if (e->VehInfo<RailVehicleInfo>().running_cost_class == Price::Invalid) continue;

		uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->VehInfo<RailVehicleInfo>().running_cost);
		if (cost_factor == 0) continue;

		/* Halve running cost for multiheaded parts */
		if (v->IsMultiheaded()) cost_factor /= 2;

		cost += GetPrice(e->VehInfo<RailVehicleInfo>().running_cost_class, cost_factor, e->GetGRF());
	} while ((v = v->GetNextVehicle()) != nullptr);

	return cost;
}

/**
 * Update train vehicle data for a tick.
 * @return True if the vehicle still exists, false if it has ceased to exist (front of consists only).
 */
bool Train::Tick()
{
	this->tick_counter++;

	if (this->IsFrontEngine()) {
		PerformanceAccumulator framerate(PFE_GL_TRAINS);

		if (!this->vehstatus.Test(VehState::Stopped) || this->cur_speed > 0) this->running_ticks++;

		this->current_order_time++;

		if (!TrainLocoHandler(this, false)) return false;

		return TrainLocoHandler(this, true);
	} else if (this->IsFreeWagon() && this->vehstatus.Test(VehState::Crashed)) {
		/* Delete flooded standalone wagon chain */
		if (++this->crash_anim_pos >= 4400) {
			delete this;
			return false;
		}
	}

	return true;
}

/**
 * Check whether a train needs service, and if so, find a depot or service it.
 * @param v %Train to check.
 */
static void CheckIfTrainNeedsService(Train *v)
{
	if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
	if (v->IsChainInDepot()) {
		VehicleServiceInDepot(v);
		return;
	}

	uint max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty;

	FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
	/* Only go to the depot if it is not too far out of our way. */
	if (tfdd.best_length == UINT_MAX || tfdd.best_length > max_penalty) {
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
			/* If we were already heading for a depot but it has
			 * suddenly moved farther away, we continue our normal
			 * schedule? */
			v->current_order.MakeDummy();
			SetWindowWidgetDirty(WindowClass::VehicleView, v->index, WID_VV_START_STOP);
		}
		return;
	}

	DepotID depot = GetDepotIndex(tfdd.tile);

	if (v->current_order.IsType(OT_GOTO_DEPOT) &&
			v->current_order.GetDestination() != depot &&
			!Chance16(3, 16)) {
		return;
	}

	v->gv_flags.Set(GroundVehicleFlag::SuppressImplicitOrders);
	v->current_order.MakeGoToDepot(depot, OrderDepotTypeFlag::Service, OrderNonStopFlag::NonStop, OrderDepotActionFlag::NearestDepot);
	v->dest_tile = tfdd.tile;
	SetWindowWidgetDirty(WindowClass::VehicleView, v->index, WID_VV_START_STOP);
}

/** Calendar day handler. */
void Train::OnNewCalendarDay()
{
	AgeVehicle(this);
}

/** Economy day handler. */
void Train::OnNewEconomyDay()
{
	EconomyAgeVehicle(this);

	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);

	if (this->IsFrontEngine()) {
		CheckVehicleBreakdown(this);

		CheckIfTrainNeedsService(this);

		CheckOrders(this);

		/* update destination */
		if (this->current_order.IsType(OT_GOTO_STATION)) {
			TileIndex tile = Station::Get(this->current_order.GetDestination().ToStationID())->train_station.tile;
			if (tile != INVALID_TILE) this->dest_tile = tile;
		}

		if (this->running_ticks != 0) {
			/* running costs */
			CommandCost cost(ExpensesType::TrainRun, this->GetRunningCost() * this->running_ticks / (CalendarTime::DAYS_IN_YEAR  * Ticks::DAY_TICKS));

			this->profit_this_year -= cost.GetCost();
			this->running_ticks = 0;

			SubtractMoneyFromCompanyFract(this->owner, cost);

			SetWindowDirty(WindowClass::VehicleDetails, this->index);
			SetWindowClassesDirty(WindowClass::TrainList);
		}
	}
}

/**
 * Get the tracks of the train vehicle.
 * @return Current tracks of the vehicle.
 */
Trackdir Train::GetVehicleTrackdir() const
{
	if (this->vehstatus.Test(VehState::Crashed)) return Trackdir::Invalid;

	if (this->track == Track::Depot) {
		/* We'll assume the train is facing outwards */
		return DiagDirToDiagTrackdir(GetRailDepotDirection(this->tile)); // Train in depot
	}

	if (this->track == Track::Wormhole) {
		/* train in tunnel or on bridge, so just use its direction and assume a diagonal track */
		return DiagDirToDiagTrackdir(DirToDiagDir(this->GetMovingDirection()));
	}

	return TrackDirectionToTrackdir(FindFirstTrack(this->track), this->GetMovingDirection());
}

uint16_t Train::GetMaxWeight() const
{
	uint16_t weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnitsInTrain(this->GetEngine()->DetermineCapacity(this));

	/* Vehicle weight is not added for articulated parts. */
	if (!this->IsArticulatedPart()) {
		weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
	}

	/* Powered wagons have extra weight added. */
	if (this->flags.Test(VehicleRailFlag::PoweredWagon)) {
		weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
	}

	return weight;
}
