2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file train_cmd.cpp Handling of trains. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-10-28 20:04:54 +00:00
|
|
|
#include "gui.h"
|
2007-06-01 11:41:02 +00:00
|
|
|
#include "articulated_vehicles.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2009-12-02 09:57:17 +00:00
|
|
|
#include "pathfinder/npf/npf_func.h"
|
2009-12-07 08:41:18 +00:00
|
|
|
#include "pathfinder/yapf/yapf.hpp"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2005-05-02 23:59:11 +00:00
|
|
|
#include "vehicle_gui.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2006-05-02 21:43:47 +00:00
|
|
|
#include "newgrf_text.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-10-28 15:40:18 +00:00
|
|
|
#include "table/sprites.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_gui.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2008-04-19 23:19:12 +00:00
|
|
|
#include "newgrf_station.h"
|
2008-04-20 11:12:07 +00:00
|
|
|
#include "effectvehicle_func.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "effectvehicle_base.h"
|
2008-07-24 15:19:26 +00:00
|
|
|
#include "gamelog.h"
|
|
|
|
#include "network/network.h"
|
2009-11-16 17:58:46 +00:00
|
|
|
#include "spritecache.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/random_func.hpp"
|
|
|
|
#include "company_base.h"
|
|
|
|
#include "engine_base.h"
|
|
|
|
#include "engine_func.h"
|
|
|
|
#include "newgrf.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/train_cmd.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
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);
|
|
|
|
static void TrainController(Train *v, Vehicle *nomove);
|
|
|
|
static TileIndex TrainApproachingCrossingTile(const Train *v);
|
|
|
|
static void CheckIfTrainNeedsService(Train *v);
|
|
|
|
static void CheckNextTrainTile(Train *v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
static const byte _vehicle_initial_x_fract[4] = {10, 8, 4, 8};
|
|
|
|
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
|
2008-01-11 15:10:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the side in which the train will leave the tile
|
|
|
|
*
|
|
|
|
* @param direction vehicle direction
|
|
|
|
* @param track vehicle track bits
|
|
|
|
* @return side of tile the train will leave
|
|
|
|
*/
|
|
|
|
static inline DiagDirection TrainExitDir(Direction direction, TrackBits track)
|
|
|
|
{
|
|
|
|
static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
|
|
|
|
|
|
|
|
DiagDirection diagdir = DirToDiagDir(direction);
|
|
|
|
|
|
|
|
/* Determine the diagonal direction in which we will exit this tile */
|
|
|
|
if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
|
|
|
|
diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return diagdir;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-27 23:11:56 +00:00
|
|
|
|
|
|
|
/** Return the cargo weight multiplier to use for a rail vehicle
|
2006-12-10 19:00:06 +00:00
|
|
|
* @param cargo Cargo type to get multiplier for
|
2006-11-27 23:11:56 +00:00
|
|
|
* @return Cargo weight multiplier
|
|
|
|
*/
|
2006-12-10 19:00:06 +00:00
|
|
|
byte FreightWagonMult(CargoID cargo)
|
2006-11-27 23:11:56 +00:00
|
|
|
{
|
2009-07-16 19:00:13 +00:00
|
|
|
if (!CargoSpec::Get(cargo)->is_freight) return 1;
|
2008-05-29 15:13:28 +00:00
|
|
|
return _settings_game.vehicle.freight_trains;
|
2006-11-27 23:11:56 +00:00
|
|
|
}
|
|
|
|
|
2008-07-24 15:19:26 +00:00
|
|
|
/** Logs a bug in GRF and shows a warning message if this
|
|
|
|
* is for the first time this happened.
|
|
|
|
* @param u first vehicle of chain
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static void RailVehicleLengthChanged(const Train *u)
|
2008-07-24 15:19:26 +00:00
|
|
|
{
|
|
|
|
/* show a warning once for each engine in whole game and once for each GRF after each game load */
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *engine = Engine::Get(u->engine_type);
|
2008-07-24 15:19:26 +00:00
|
|
|
uint32 grfid = engine->grffile->grfid;
|
|
|
|
GRFConfig *grfconfig = GetGRFConfig(grfid);
|
|
|
|
if (GamelogGRFBugReverse(grfid, engine->internal_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
|
2009-03-13 21:28:40 +00:00
|
|
|
ShowNewGrfVehicleError(u->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_VEHICLE_LENGTH, GBUG_VEH_LENGTH, true);
|
2008-07-24 15:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Checks if lengths of all rail vehicles are valid. If not, shows an error message. */
|
|
|
|
void CheckTrainsLengths()
|
|
|
|
{
|
2009-05-26 22:45:48 +00:00
|
|
|
const Train *v;
|
2008-07-24 15:19:26 +00:00
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
FOR_ALL_TRAINS(v) {
|
|
|
|
if (v->First() == v && !(v->vehstatus & VS_CRASHED)) {
|
|
|
|
for (const Train *u = v, *w = v->Next(); w != NULL; u = w, w = w->Next()) {
|
2009-05-22 22:33:05 +00:00
|
|
|
if (u->track != TRACK_BIT_DEPOT) {
|
|
|
|
if ((w->track != TRACK_BIT_DEPOT &&
|
2009-05-22 22:55:41 +00:00
|
|
|
max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->tcache.cached_veh_length) ||
|
2009-05-22 22:33:05 +00:00
|
|
|
(w->track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
|
2008-07-24 15:19:26 +00:00
|
|
|
SetDParam(0, v->index);
|
|
|
|
SetDParam(1, v->owner);
|
2010-02-24 14:46:15 +00:00
|
|
|
ShowErrorMessage(STR_BROKEN_VEHICLE_LENGTH, INVALID_STRING_ID, WL_CRITICAL);
|
2008-07-24 16:23:55 +00:00
|
|
|
|
2009-05-06 15:06:57 +00:00
|
|
|
if (!_networking) DoCommandP(0, PM_PAUSED_ERROR, 1, CMD_PAUSE);
|
2008-07-24 15:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-05 15:37:00 +00:00
|
|
|
/**
|
2005-06-06 14:26:15 +00:00
|
|
|
* 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)
|
2008-07-24 15:19:26 +00:00
|
|
|
* @param same_length should length of vehicles stay the same?
|
2005-06-05 15:37:00 +00:00
|
|
|
*/
|
2010-01-15 18:23:52 +00:00
|
|
|
void Train::ConsistChanged(bool same_length)
|
2005-11-14 19:48:04 +00:00
|
|
|
{
|
2008-04-07 12:36:50 +00:00
|
|
|
uint16 max_speed = UINT16_MAX;
|
2005-06-06 14:26:15 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
assert(this->IsFrontEngine() || this->IsFreeWagon());
|
2005-06-06 14:26:15 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
const RailVehicleInfo *rvi_v = RailVehInfo(this->engine_type);
|
|
|
|
EngineID first_engine = this->IsFrontEngine() ? this->engine_type : INVALID_ENGINE;
|
|
|
|
this->tcache.cached_total_length = 0;
|
|
|
|
this->compatible_railtypes = RAILTYPES_NONE;
|
2005-06-05 15:37:00 +00:00
|
|
|
|
2008-01-01 22:24:18 +00:00
|
|
|
bool train_can_tilt = true;
|
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
for (Train *u = this; u != NULL; u = u->Next()) {
|
2005-06-06 00:19:24 +00:00
|
|
|
const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
|
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
/* Check the this->first cache. */
|
|
|
|
assert(u->First() == this);
|
2006-02-07 18:55:06 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* update the 'first engine' */
|
2010-01-15 18:23:52 +00:00
|
|
|
u->tcache.first_engine = this == u ? INVALID_ENGINE : first_engine;
|
2009-05-22 22:33:05 +00:00
|
|
|
u->railtype = rvi_u->railtype;
|
2005-06-06 14:26:15 +00:00
|
|
|
|
2009-07-01 23:57:20 +00:00
|
|
|
if (u->IsEngine()) first_engine = u->engine_type;
|
2008-06-13 13:28:55 +00:00
|
|
|
|
2008-04-23 21:55:24 +00:00
|
|
|
/* Set user defined data to its default value */
|
2009-05-22 22:55:41 +00:00
|
|
|
u->tcache.user_def_data = rvi_u->user_def_data;
|
2010-01-15 18:23:52 +00:00
|
|
|
this->InvalidateNewGRFCache();
|
2009-05-31 12:03:14 +00:00
|
|
|
u->InvalidateNewGRFCache();
|
2008-04-23 21:55:24 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
for (Train *u = this; u != NULL; u = u->Next()) {
|
2008-04-23 21:55:24 +00:00
|
|
|
/* Update user defined data (must be done before other properties) */
|
2009-09-22 19:28:57 +00:00
|
|
|
u->tcache.user_def_data = GetVehicleProperty(u, PROP_TRAIN_USER_DATA, u->tcache.user_def_data);
|
2010-01-15 18:23:52 +00:00
|
|
|
this->InvalidateNewGRFCache();
|
2009-05-31 12:03:14 +00:00
|
|
|
u->InvalidateNewGRFCache();
|
2008-04-23 21:55:24 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
for (Train *u = this; u != NULL; u = u->Next()) {
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *e_u = Engine::Get(u->engine_type);
|
2009-02-21 12:52:41 +00:00
|
|
|
const RailVehicleInfo *rvi_u = &e_u->u.rail;
|
2008-04-23 21:55:24 +00:00
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
|
2008-04-23 21:55:24 +00:00
|
|
|
|
2007-05-10 06:42:43 +00:00
|
|
|
/* Cache wagon override sprite group. NULL is returned if there is none */
|
2009-05-22 22:55:41 +00:00
|
|
|
u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->tcache.first_engine);
|
2007-05-10 06:42:43 +00:00
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Reset colour map */
|
|
|
|
u->colourmap = PAL_NONE;
|
2007-10-28 15:40:18 +00:00
|
|
|
|
2005-11-04 12:58:18 +00:00
|
|
|
if (rvi_u->visual_effect != 0) {
|
2009-05-22 22:55:41 +00:00
|
|
|
u->tcache.cached_vis_effect = rvi_u->visual_effect;
|
2005-11-04 12:58:18 +00:00
|
|
|
} else {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (u->IsWagon() || u->IsArticulatedPart()) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Wagons and articulated parts have no effect by default */
|
2009-05-22 22:55:41 +00:00
|
|
|
u->tcache.cached_vis_effect = 0x40;
|
2005-11-04 12:58:18 +00:00
|
|
|
} else if (rvi_u->engclass == 0) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Steam is offset by -4 units */
|
2009-05-22 22:55:41 +00:00
|
|
|
u->tcache.cached_vis_effect = 4;
|
2005-11-04 12:58:18 +00:00
|
|
|
} else {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Diesel fumes and sparks come from the centre */
|
2009-05-22 22:55:41 +00:00
|
|
|
u->tcache.cached_vis_effect = 8;
|
2005-11-04 12:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-29 22:19:37 +00:00
|
|
|
/* Check powered wagon / visual effect callback */
|
2009-10-06 19:17:07 +00:00
|
|
|
if (HasBit(e_u->info.callback_mask, CBM_TRAIN_WAGON_POWER)) {
|
2008-07-29 22:19:37 +00:00
|
|
|
uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
|
2005-06-06 00:19:24 +00:00
|
|
|
|
2009-05-22 22:55:41 +00:00
|
|
|
if (callback != CALLBACK_FAILED) u->tcache.cached_vis_effect = GB(callback, 0, 8);
|
2008-07-29 22:19:37 +00:00
|
|
|
}
|
2006-05-31 18:00:08 +00:00
|
|
|
|
2008-07-29 22:19:37 +00:00
|
|
|
if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
|
2009-05-22 22:55:41 +00:00
|
|
|
UsesWagonOverride(u) && !HasBit(u->tcache.cached_vis_effect, 7)) {
|
2008-07-29 22:19:37 +00:00
|
|
|
/* wagon is powered */
|
2009-05-22 22:33:05 +00:00
|
|
|
SetBit(u->flags, VRF_POWEREDWAGON); // cache 'powered' status
|
2008-07-29 22:19:37 +00:00
|
|
|
} else {
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(u->flags, VRF_POWEREDWAGON);
|
2008-07-29 22:19:37 +00:00
|
|
|
}
|
2005-06-06 00:19:24 +00:00
|
|
|
|
2009-07-01 23:57:20 +00:00
|
|
|
if (!u->IsArticulatedPart()) {
|
2006-03-29 16:30:26 +00:00
|
|
|
/* Do not count powered wagons for the compatible railtypes, as wagons always
|
|
|
|
have railtype normal */
|
|
|
|
if (rvi_u->power > 0) {
|
2010-01-15 18:23:52 +00:00
|
|
|
this->compatible_railtypes |= GetRailTypeInfo(u->railtype)->powered_railtypes;
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 19:31:44 +00:00
|
|
|
/* 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 */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(u->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) {
|
|
|
|
u->railtype = RAILTYPE_RAIL;
|
|
|
|
u->compatible_railtypes |= RAILTYPES_RAIL;
|
2006-11-17 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* max speed is the minimum of the speed limits of all vehicles in the consist */
|
2008-05-29 15:13:28 +00:00
|
|
|
if ((rvi_u->railveh_type != RAILVEH_WAGON || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) {
|
2009-09-22 19:28:57 +00:00
|
|
|
uint16 speed = GetVehicleProperty(u, PROP_TRAIN_SPEED, rvi_u->max_speed);
|
2007-04-18 18:37:40 +00:00
|
|
|
if (speed != 0) max_speed = min(speed, max_speed);
|
|
|
|
}
|
2005-11-05 16:07:26 +00:00
|
|
|
}
|
2005-06-06 22:44:11 +00:00
|
|
|
|
2009-10-28 18:31:16 +00:00
|
|
|
u->cargo_cap = GetVehicleCapacity(u);
|
2007-05-12 07:05:34 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* check the vehicle length (callback) */
|
2007-02-25 09:27:03 +00:00
|
|
|
uint16 veh_len = CALLBACK_FAILED;
|
2009-10-06 19:17:07 +00:00
|
|
|
if (HasBit(e_u->info.callback_mask, CBM_VEHICLE_LENGTH)) {
|
2007-07-09 19:38:12 +00:00
|
|
|
veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
|
2006-06-04 18:22:32 +00:00
|
|
|
}
|
|
|
|
if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
|
2009-03-21 22:32:51 +00:00
|
|
|
veh_len = 8 - Clamp(veh_len, 0, 7);
|
2008-07-24 15:19:26 +00:00
|
|
|
|
|
|
|
/* verify length hasn't changed */
|
2009-05-22 22:55:41 +00:00
|
|
|
if (same_length && veh_len != u->tcache.cached_veh_length) RailVehicleLengthChanged(u);
|
2008-07-24 15:19:26 +00:00
|
|
|
|
|
|
|
/* update vehicle length? */
|
2009-05-22 22:55:41 +00:00
|
|
|
if (!same_length) u->tcache.cached_veh_length = veh_len;
|
2008-07-24 15:19:26 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
this->tcache.cached_total_length += u->tcache.cached_veh_length;
|
|
|
|
this->InvalidateNewGRFCache();
|
2009-05-31 12:03:14 +00:00
|
|
|
u->InvalidateNewGRFCache();
|
2007-02-25 09:27:03 +00:00
|
|
|
}
|
2005-06-05 15:37:00 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* store consist weight/max speed in cache */
|
2010-01-15 18:23:52 +00:00
|
|
|
this->tcache.cached_max_speed = max_speed;
|
|
|
|
this->tcache.cached_tilt = train_can_tilt;
|
|
|
|
this->tcache.cached_max_curve_speed = this->GetCurveSpeedLimit();
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* 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) */
|
2010-01-15 18:23:52 +00:00
|
|
|
this->CargoChanged();
|
2008-03-17 21:50:53 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
if (this->IsFrontEngine()) {
|
|
|
|
this->UpdateAcceleration();
|
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
2008-03-17 22:13:00 +00:00
|
|
|
}
|
2005-06-05 15:37:00 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-12 14:11:14 +00:00
|
|
|
/**
|
|
|
|
* 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 v the vehicle 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.
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length)
|
2009-04-12 14:11:14 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
const Station *st = Station::Get(station_id);
|
2009-04-12 14:11:14 +00:00
|
|
|
*station_ahead = st->GetPlatformLength(tile, DirToDiagDir(v->direction)) * 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 = OSL_PLATFORM_MIDDLE;
|
2009-05-22 22:55:41 +00:00
|
|
|
if (v->tcache.cached_total_length >= *station_length) {
|
2009-04-12 14:11:14 +00:00
|
|
|
/* The train is longer than the station, make it stop at the far end of the platform */
|
|
|
|
osl = OSL_PLATFORM_FAR_END;
|
|
|
|
} else if (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == station_id) {
|
|
|
|
osl = v->current_order.GetStopLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The stop location of the FRONT! of the train */
|
|
|
|
int stop;
|
|
|
|
switch (osl) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
|
|
|
|
case OSL_PLATFORM_NEAR_END:
|
2009-05-22 22:55:41 +00:00
|
|
|
stop = v->tcache.cached_total_length;
|
2009-04-12 14:11:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OSL_PLATFORM_MIDDLE:
|
2009-05-22 22:55:41 +00:00
|
|
|
stop = *station_length - (*station_length - v->tcache.cached_total_length) / 2;
|
2009-04-12 14:11:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OSL_PLATFORM_FAR_END:
|
|
|
|
stop = *station_length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Substract half the front vehicle length of the train so we get the real
|
|
|
|
* stop location of the train. */
|
2009-05-22 22:55:41 +00:00
|
|
|
return stop - (v->tcache.cached_veh_length + 1) / 2;
|
2009-04-12 14:11:14 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 13:14:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes train speed limit caused by curves
|
|
|
|
* @return imposed speed limit
|
|
|
|
*/
|
2010-01-15 18:23:52 +00:00
|
|
|
int Train::GetCurveSpeedLimit() const
|
2005-01-26 12:51:04 +00:00
|
|
|
{
|
2010-01-15 18:23:52 +00:00
|
|
|
assert(this->First() == this);
|
|
|
|
|
2008-04-07 12:36:50 +00:00
|
|
|
static const int absolute_max_speed = UINT16_MAX;
|
2008-01-01 14:00:31 +00:00
|
|
|
int max_speed = absolute_max_speed;
|
2009-06-01 13:14:56 +00:00
|
|
|
|
2010-01-30 16:27:35 +00:00
|
|
|
if (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) return max_speed;
|
2009-06-01 13:14:56 +00:00
|
|
|
|
2005-01-26 12:51:04 +00:00
|
|
|
int curvecount[2] = {0, 0};
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* first find the curve speed limit */
|
2007-02-25 09:27:03 +00:00
|
|
|
int numcurve = 0;
|
|
|
|
int sum = 0;
|
|
|
|
int pos = 0;
|
|
|
|
int lastpos = -1;
|
2010-01-15 18:23:52 +00:00
|
|
|
for (const Vehicle *u = this; u->Next() != NULL; u = u->Next(), pos++) {
|
2008-01-01 18:12:17 +00:00
|
|
|
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_45LEFT) curvecount[0]++;
|
|
|
|
if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++;
|
|
|
|
if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) {
|
|
|
|
if (lastpos != -1) {
|
|
|
|
numcurve++;
|
|
|
|
sum += pos - lastpos;
|
|
|
|
if (pos - lastpos == 1) {
|
|
|
|
max_speed = 88;
|
2005-01-26 12:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-01 18:12:17 +00:00
|
|
|
lastpos = pos;
|
2005-01-26 12:51:04 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* if we have a 90 degree turn, fix the speed limit to 60 */
|
2008-01-01 18:12:17 +00:00
|
|
|
if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) {
|
2005-01-26 12:51:04 +00:00
|
|
|
max_speed = 61;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 13:14:56 +00:00
|
|
|
if (numcurve > 0 && max_speed > 88) {
|
2005-01-26 12:51:04 +00:00
|
|
|
if (curvecount[0] == 1 && curvecount[1] == 1) {
|
2008-01-01 14:00:31 +00:00
|
|
|
max_speed = absolute_max_speed;
|
2009-06-01 13:14:56 +00:00
|
|
|
} else {
|
|
|
|
sum /= numcurve;
|
2007-11-19 18:38:10 +00:00
|
|
|
max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
|
2005-01-26 12:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-01 14:00:31 +00:00
|
|
|
if (max_speed != absolute_max_speed) {
|
|
|
|
/* Apply the engine's rail type curve speed advantage, if it slowed by curves */
|
2010-01-15 18:23:52 +00:00
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
|
2008-01-01 14:00:31 +00:00
|
|
|
max_speed += (max_speed / 2) * rti->curve_speed;
|
2008-01-01 22:24:18 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
if (this->tcache.cached_tilt) {
|
2008-01-01 22:24:18 +00:00
|
|
|
/* Apply max_speed bonus of 20% for a tilting train */
|
|
|
|
max_speed += max_speed / 5;
|
|
|
|
}
|
2008-01-01 14:00:31 +00:00
|
|
|
}
|
2005-01-26 12:51:04 +00:00
|
|
|
|
2009-06-01 13:14:56 +00:00
|
|
|
return max_speed;
|
|
|
|
}
|
|
|
|
|
2010-01-16 22:30:04 +00:00
|
|
|
/**
|
|
|
|
* Calculates the maximum speed of the vehicle under its current conditions.
|
|
|
|
* @return Maximum speed of the vehicle.
|
|
|
|
*/
|
|
|
|
int Train::GetCurrentMaxSpeed() const
|
2009-06-01 13:14:56 +00:00
|
|
|
{
|
2010-01-16 22:30:04 +00:00
|
|
|
int max_speed = this->tcache.cached_max_curve_speed;
|
|
|
|
assert(max_speed == this->GetCurveSpeedLimit());
|
2009-06-01 13:14:56 +00:00
|
|
|
|
2010-01-16 22:30:04 +00:00
|
|
|
if (IsRailStationTile(this->tile)) {
|
|
|
|
StationID sid = GetStationIndex(this->tile);
|
|
|
|
if (this->current_order.ShouldStopAtStation(this, sid)) {
|
2009-04-12 14:11:14 +00:00
|
|
|
int station_ahead;
|
|
|
|
int station_length;
|
2010-01-16 22:30:04 +00:00
|
|
|
int stop_at = GetTrainStopLocation(sid, this->tile, this, &station_ahead, &station_length);
|
2009-04-12 14:11:14 +00:00
|
|
|
|
|
|
|
/* 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;
|
2005-01-26 12:51:04 +00:00
|
|
|
|
2009-04-13 22:27:21 +00:00
|
|
|
if (distance_to_go > 0) {
|
|
|
|
int st_max_speed = 120;
|
|
|
|
|
2010-01-16 22:30:04 +00:00
|
|
|
int delta_v = this->cur_speed / (distance_to_go + 1);
|
|
|
|
if (this->max_speed > (this->cur_speed - delta_v)) {
|
|
|
|
st_max_speed = this->cur_speed - (delta_v / 10);
|
2009-04-13 22:27:21 +00:00
|
|
|
}
|
2005-01-26 12:51:04 +00:00
|
|
|
|
2009-04-13 22:27:21 +00:00
|
|
|
st_max_speed = max(st_max_speed, 25 * distance_to_go);
|
|
|
|
max_speed = min(max_speed, st_max_speed);
|
|
|
|
}
|
2005-01-26 12:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-16 22:30:04 +00:00
|
|
|
for (const Train *u = this; u != NULL; u = u->Next()) {
|
|
|
|
if (u->track == TRACK_BIT_DEPOT) {
|
|
|
|
max_speed = min(max_speed, 61);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-01-26 12:51:04 +00:00
|
|
|
|
2010-03-06 12:42:53 +00:00
|
|
|
return min(max_speed, this->acc_cache.cached_max_track_speed);
|
2010-01-16 22:30:04 +00:00
|
|
|
}
|
2005-01-26 12:51:04 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
void Train::UpdateAcceleration()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-01-15 18:23:52 +00:00
|
|
|
assert(this->IsFrontEngine());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-06 12:42:53 +00:00
|
|
|
this->max_speed = this->acc_cache.cached_max_track_speed;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-06 12:42:53 +00:00
|
|
|
uint power = this->acc_cache.cached_power;
|
|
|
|
uint weight = this->acc_cache.cached_weight;
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(weight != 0);
|
2010-01-15 18:23:52 +00:00
|
|
|
this->acceleration = Clamp(power / weight * 4, 1, 255);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 22:00:13 +00:00
|
|
|
/**
|
|
|
|
* Get the width of a train vehicle image in the GUI.
|
|
|
|
* @param offset Additional offset for positioning the sprite; set to NULL 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 = Engine::Get(this->engine_type);
|
|
|
|
if (e->grffile != NULL && is_custom_sprite(e->u.rail.image_index)) {
|
|
|
|
reference_width = e->grffile->traininfo_vehicle_width;
|
|
|
|
vehicle_pitch = e->grffile->traininfo_vehicle_pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset != NULL) {
|
|
|
|
offset->x = reference_width / 2;
|
|
|
|
offset->y = vehicle_pitch;
|
|
|
|
}
|
|
|
|
return this->tcache.cached_veh_length * reference_width / 8;
|
|
|
|
}
|
|
|
|
|
2009-05-26 08:53:16 +00:00
|
|
|
static SpriteID GetDefaultTrainSprite(uint8 spritenum, Direction direction)
|
|
|
|
{
|
|
|
|
return ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
|
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
SpriteID Train::GetImage(Direction direction) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-21 20:50:58 +00:00
|
|
|
uint8 spritenum = this->spritenum;
|
|
|
|
SpriteID sprite;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(this->flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
|
2006-03-18 13:00:32 +00:00
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
|
sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
|
|
|
|
if (sprite != 0) return sprite;
|
|
|
|
|
2009-06-16 13:31:06 +00:00
|
|
|
spritenum = Engine::Get(this->engine_type)->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-26 08:53:16 +00:00
|
|
|
sprite = GetDefaultTrainSprite(spritenum, direction);
|
2008-04-21 20:50:58 +00:00
|
|
|
|
|
|
|
if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
return sprite;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-17 22:00:13 +00:00
|
|
|
const Engine *e = Engine::Get(engine);
|
2008-04-21 20:50:58 +00:00
|
|
|
Direction dir = rear_head ? DIR_E : DIR_W;
|
2009-07-17 22:00:13 +00:00
|
|
|
uint8 spritenum = e->u.rail.image_index;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
|
SpriteID sprite = GetCustomVehicleIcon(engine, dir);
|
|
|
|
if (sprite != 0) {
|
2009-07-17 22:00:13 +00:00
|
|
|
if (e->grffile != NULL) {
|
|
|
|
y += e->grffile->traininfo_vehicle_pitch;
|
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
return sprite;
|
2005-10-30 21:47:42 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2009-06-16 13:31:06 +00:00
|
|
|
spritenum = Engine::Get(engine)->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
if (rear_head) spritenum++;
|
|
|
|
|
2009-05-26 08:53:16 +00:00
|
|
|
return GetDefaultTrainSprite(spritenum, DIR_W);
|
2008-04-21 20:50:58 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 01:38:13 +00:00
|
|
|
void DrawTrainEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
|
|
|
if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
|
|
|
|
int yf = y;
|
|
|
|
int yr = y;
|
|
|
|
|
|
|
|
SpriteID spritef = GetRailIcon(engine, false, yf);
|
|
|
|
SpriteID spriter = GetRailIcon(engine, true, yr);
|
2009-11-16 17:58:46 +00:00
|
|
|
const Sprite *real_spritef = GetSprite(spritef, ST_NORMAL);
|
|
|
|
const Sprite *real_spriter = GetSprite(spriter, ST_NORMAL);
|
|
|
|
|
|
|
|
preferred_x = Clamp(preferred_x, left - real_spritef->x_offs + 14, right - real_spriter->width - real_spriter->x_offs - 15);
|
|
|
|
|
2009-11-16 17:15:42 +00:00
|
|
|
DrawSprite(spritef, pal, preferred_x - 14, yf);
|
|
|
|
DrawSprite(spriter, pal, preferred_x + 15, yr);
|
2008-04-21 20:50:58 +00:00
|
|
|
} else {
|
|
|
|
SpriteID sprite = GetRailIcon(engine, false, y);
|
2009-11-16 17:58:46 +00:00
|
|
|
const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
|
|
|
|
preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
|
2009-11-16 17:15:42 +00:00
|
|
|
DrawSprite(sprite, pal, preferred_x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:20:05 +00:00
|
|
|
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *e = Engine::Get(engine);
|
2009-02-21 12:52:41 +00:00
|
|
|
const RailVehicleInfo *rvi = &e->u.rail;
|
|
|
|
CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
|
|
|
|
|
|
|
|
/* Engines without valid cargo should not be available */
|
|
|
|
if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
if (flags & DC_QUERY_COST) return value;
|
2005-11-05 16:07:26 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
/* Check that the wagon can drive on the track in question */
|
|
|
|
if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
|
2008-03-28 22:59:43 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
uint num_vehicles = 1 + CountArticulatedParts(engine, false);
|
2006-10-12 15:03:19 +00:00
|
|
|
|
2009-05-13 21:38:23 +00:00
|
|
|
/* Allow for the wagon and the articulated parts */
|
|
|
|
if (!Vehicle::CanAllocateItem(num_vehicles)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
|
2009-01-25 15:35:21 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *v = new Train();
|
2009-05-13 21:38:23 +00:00
|
|
|
v->spritenum = rvi->image_index;
|
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
v->engine_type = engine;
|
2009-10-03 14:46:48 +00:00
|
|
|
v->tcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
DiagDirection dir = GetRailDepotDirection(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
v->direction = DiagDirToDir(dir);
|
|
|
|
v->tile = tile;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
|
|
|
|
int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
v->z_pos = GetSlopeZ(x, y);
|
|
|
|
v->owner = _current_company;
|
2009-05-22 22:33:05 +00:00
|
|
|
v->track = TRACK_BIT_DEPOT;
|
2009-01-25 15:35:21 +00:00
|
|
|
v->vehstatus = VS_HIDDEN | VS_DEFPAL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-02 00:17:27 +00:00
|
|
|
v->SetWagon();
|
2007-06-01 11:17:30 +00:00
|
|
|
|
2009-12-11 22:23:48 +00:00
|
|
|
v->SetFreeWagon();
|
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
v->cargo_type = e->GetDefaultCargoType();
|
2009-01-25 15:35:21 +00:00
|
|
|
v->cargo_cap = rvi->capacity;
|
|
|
|
v->value = value.GetCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
v->railtype = rvi->railtype;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
v->build_year = _cur_year;
|
2009-05-13 21:45:56 +00:00
|
|
|
v->cur_image = SPR_IMG_QUERY;
|
2009-01-25 15:35:21 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
v->group_id = DEFAULT_GROUP;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2009-10-06 19:30:31 +00:00
|
|
|
AddArticulatedParts(v);
|
2005-11-05 16:07:26 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, false);
|
2010-01-15 18:23:52 +00:00
|
|
|
v->First()->ConsistChanged(false);
|
2009-01-25 15:35:21 +00:00
|
|
|
UpdateTrainGroupID(v->First());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
2009-01-25 15:35:21 +00:00
|
|
|
if (IsLocalCompany()) {
|
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-16 23:34:14 +00:00
|
|
|
Company::Get(_current_company)->num_engines[engine]++;
|
2009-03-13 21:28:40 +00:00
|
|
|
|
|
|
|
CheckConsistencyOfArticulatedVehicle(v);
|
2009-12-11 22:23:48 +00:00
|
|
|
|
|
|
|
/* Try to connect the vehicle to one of free chains of wagons. */
|
|
|
|
Train *w;
|
|
|
|
FOR_ALL_TRAINS(w) {
|
2009-12-28 18:26:39 +00:00
|
|
|
if (w->tile == tile && ///< Same depot
|
|
|
|
w->IsFreeWagon() && ///< A free wagon chain
|
|
|
|
w->engine_type == engine && ///< Same type
|
|
|
|
w->First() != v && ///< Don't connect to ourself
|
|
|
|
!(w->vehstatus & VS_CRASHED)) { ///< Not crashed/flooded
|
2009-12-11 22:23:48 +00:00
|
|
|
DoCommand(0, v->index | (w->Last()->index << 16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
return value;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/** Move all free vehicles in the depot to the train */
|
2009-05-22 22:22:46 +00:00
|
|
|
static void NormalizeTrainVehInDepot(const Train *u)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 22:45:48 +00:00
|
|
|
const Train *v;
|
|
|
|
FOR_ALL_TRAINS(v) {
|
2009-07-01 23:49:13 +00:00
|
|
|
if (v->IsFreeWagon() && v->tile == u->tile &&
|
2009-05-26 22:45:48 +00:00
|
|
|
v->track == TRACK_BIT_DEPOT) {
|
2010-01-18 22:57:21 +00:00
|
|
|
if (DoCommand(0, v->index | (u->index << 16), 1, DC_EXEC,
|
|
|
|
CMD_MOVE_RAIL_VEHICLE).Failed())
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void AddRearEngineToMultiheadedTrain(Train *v)
|
2005-01-18 23:27:06 +00:00
|
|
|
{
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *u = new Train();
|
2009-05-13 21:38:23 +00:00
|
|
|
v->value >>= 1;
|
|
|
|
u->value = v->value;
|
2005-01-18 23:27:06 +00:00
|
|
|
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;
|
2009-05-22 22:33:05 +00:00
|
|
|
u->track = TRACK_BIT_DEPOT;
|
2005-01-18 23:27:06 +00:00
|
|
|
u->vehstatus = v->vehstatus & ~VS_STOPPED;
|
|
|
|
u->spritenum = v->spritenum + 1;
|
|
|
|
u->cargo_type = v->cargo_type;
|
2006-05-19 10:04:03 +00:00
|
|
|
u->cargo_subtype = v->cargo_subtype;
|
2005-01-18 23:27:06 +00:00
|
|
|
u->cargo_cap = v->cargo_cap;
|
2009-05-22 22:33:05 +00:00
|
|
|
u->railtype = v->railtype;
|
2005-01-18 23:27:06 +00:00
|
|
|
u->engine_type = v->engine_type;
|
|
|
|
u->build_year = v->build_year;
|
2009-05-13 21:45:56 +00:00
|
|
|
u->cur_image = SPR_IMG_QUERY;
|
2005-12-28 22:29:59 +00:00
|
|
|
u->random_bits = VehicleRandomBits();
|
2009-07-02 00:17:27 +00:00
|
|
|
v->SetMultiheaded();
|
|
|
|
u->SetMultiheaded();
|
2009-05-13 21:38:23 +00:00
|
|
|
v->SetNext(u);
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(u, false);
|
2009-05-13 21:38:23 +00:00
|
|
|
|
|
|
|
/* Now we need to link the front and rear engines together */
|
2009-05-22 22:33:05 +00:00
|
|
|
v->other_multiheaded_part = u;
|
|
|
|
u->other_multiheaded_part = v;
|
2005-01-18 23:27:06 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Build a railroad vehicle.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile of the depot where rail-vehicle is built
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p1 engine type id
|
2008-07-09 20:55:47 +00:00
|
|
|
* @param p2 bit 1 prevents any free cars from being added to the train
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Check if the engine-type is valid (for the company) */
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_TRAIN, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE);
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *e = Engine::Get(p1);
|
2009-10-06 19:17:07 +00:00
|
|
|
const RailVehicleInfo *rvi = &e->u.rail;
|
2009-01-25 15:35:21 +00:00
|
|
|
CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
/* Engines with CT_INVALID should not be available */
|
|
|
|
if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
|
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
if (flags & DC_QUERY_COST) return value;
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/* Check if the train is actually being built in a depot belonging
|
2008-09-30 20:39:50 +00:00
|
|
|
* to the company. Doesn't matter if only the cost is queried */
|
2009-01-25 15:35:21 +00:00
|
|
|
if (!IsRailDepotTile(tile)) return CMD_ERROR;
|
|
|
|
if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
|
2005-01-29 23:58:07 +00:00
|
|
|
|
2007-01-30 11:53:35 +00:00
|
|
|
if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
uint num_vehicles =
|
|
|
|
(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) +
|
2007-09-17 04:23:03 +00:00
|
|
|
CountArticulatedParts(p1, false);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
/* 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 eletric engines in non-electric depots */
|
|
|
|
if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
|
2008-03-28 22:59:43 +00:00
|
|
|
|
2009-05-13 21:38:23 +00:00
|
|
|
/* Allow for the dual-heads and the articulated parts */
|
|
|
|
if (!Vehicle::CanAllocateItem(num_vehicles)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
|
2009-01-25 15:35:21 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
UnitID unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_TRAIN);
|
|
|
|
if (unit_num > _settings_game.vehicle.max_trains) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
|
2009-01-25 15:35:21 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
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];
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *v = new Train();
|
2009-01-25 15:35:21 +00:00
|
|
|
v->unitnumber = unit_num;
|
|
|
|
v->direction = DiagDirToDir(dir);
|
|
|
|
v->tile = tile;
|
|
|
|
v->owner = _current_company;
|
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
v->z_pos = GetSlopeZ(x, y);
|
2009-05-22 22:33:05 +00:00
|
|
|
v->track = TRACK_BIT_DEPOT;
|
2009-01-25 15:35:21 +00:00
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
|
|
|
v->spritenum = rvi->image_index;
|
2009-02-21 12:52:41 +00:00
|
|
|
v->cargo_type = e->GetDefaultCargoType();
|
2009-01-25 15:35:21 +00:00
|
|
|
v->cargo_cap = rvi->capacity;
|
|
|
|
v->max_speed = rvi->max_speed;
|
|
|
|
v->value = value.GetCost();
|
|
|
|
v->last_station_visited = INVALID_STATION;
|
|
|
|
|
|
|
|
v->engine_type = p1;
|
2009-10-03 14:46:48 +00:00
|
|
|
v->tcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
|
2009-01-25 15:35:21 +00:00
|
|
|
|
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
2009-06-16 13:52:18 +00:00
|
|
|
v->max_age = e->GetLifeLengthInDays();
|
2009-01-25 15:35:21 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
v->railtype = rvi->railtype;
|
2009-01-25 15:35:21 +00:00
|
|
|
_new_vehicle_id = v->index;
|
|
|
|
|
2009-05-26 21:59:49 +00:00
|
|
|
v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_trains;
|
2009-01-25 15:35:21 +00:00
|
|
|
v->date_of_last_service = _date;
|
|
|
|
v->build_year = _cur_year;
|
2009-05-13 21:45:56 +00:00
|
|
|
v->cur_image = SPR_IMG_QUERY;
|
2009-01-25 15:35:21 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
|
|
|
|
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
|
|
|
|
|
|
|
v->group_id = DEFAULT_GROUP;
|
|
|
|
|
2009-07-02 00:17:27 +00:00
|
|
|
v->SetFrontEngine();
|
|
|
|
v->SetEngine();
|
2009-01-25 15:35:21 +00:00
|
|
|
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, false);
|
2009-01-25 15:35:21 +00:00
|
|
|
|
|
|
|
if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
|
2009-05-13 21:38:23 +00:00
|
|
|
AddRearEngineToMultiheadedTrain(v);
|
2009-01-25 15:35:21 +00:00
|
|
|
} else {
|
2009-10-06 19:30:31 +00:00
|
|
|
AddArticulatedParts(v);
|
2009-01-25 15:35:21 +00:00
|
|
|
}
|
2005-07-31 13:08:08 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
v->ConsistChanged(false);
|
2009-01-25 15:35:21 +00:00
|
|
|
UpdateTrainGroupID(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
if (!HasBit(p2, 1) && !(flags & DC_AUTOREPLACE)) { // check if the cars should be added to the new vehicle
|
|
|
|
NormalizeTrainVehInDepot(v);
|
|
|
|
}
|
2007-02-06 11:11:12 +00:00
|
|
|
|
2009-01-25 15:35:21 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_COMPANY, v->owner);
|
2009-01-25 15:35:21 +00:00
|
|
|
if (IsLocalCompany()) {
|
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-01-25 15:35:21 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
Company::Get(_current_company)->num_engines[p1]++;
|
2009-03-13 21:28:40 +00:00
|
|
|
|
|
|
|
CheckConsistencyOfArticulatedVehicle(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-23 22:01:51 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
return value;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-13 17:13:20 +00:00
|
|
|
bool Train::IsInDepot() const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-13 17:13:20 +00:00
|
|
|
/* Is the front engine stationary in the depot? */
|
|
|
|
if (!IsRailDepotTile(this->tile) || this->cur_speed != 0) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-12-13 17:13:20 +00:00
|
|
|
/* Check whether the rest is also already trying to enter the depot. */
|
|
|
|
for (const Train *v = this; v != NULL; v = v->Next()) {
|
|
|
|
if (v->track != TRACK_BIT_DEPOT || v->tile != this->tile) return false;
|
2009-12-11 22:04:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-13 17:13:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-13 17:13:20 +00:00
|
|
|
bool Train::IsStoppedInDepot() const
|
|
|
|
{
|
|
|
|
/* Are we stopped? Ofcourse wagons don't really care... */
|
|
|
|
if (this->IsFrontEngine() && !(this->vehstatus & VS_STOPPED)) return false;
|
|
|
|
return this->IsInDepot();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static Train *FindGoodVehiclePos(const Train *src)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-10-01 12:43:34 +00:00
|
|
|
EngineID eng = src->engine_type;
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex tile = src->tile;
|
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *dst;
|
|
|
|
FOR_ALL_TRAINS(dst) {
|
2009-07-01 23:57:20 +00:00
|
|
|
if (dst->IsFreeWagon() && dst->tile == tile && !(dst->vehstatus & VS_CRASHED)) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* check so all vehicles in the line have the same engine. */
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *t = dst;
|
|
|
|
while (t->engine_type == eng) {
|
|
|
|
t = t->Next();
|
|
|
|
if (t == NULL) return dst;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/** Helper type for lists/vectors of trains */
|
|
|
|
typedef SmallVector<Train *, 16> TrainList;
|
|
|
|
|
2009-12-11 13:37:01 +00:00
|
|
|
/**
|
2009-12-12 22:11:43 +00:00
|
|
|
* 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
|
2009-12-11 13:37:01 +00:00
|
|
|
*/
|
2009-12-12 22:11:43 +00:00
|
|
|
static void MakeTrainBackup(TrainList &list, Train *t)
|
2009-12-11 13:37:01 +00:00
|
|
|
{
|
2009-12-12 22:11:43 +00:00
|
|
|
for (; t != NULL; t = t->Next()) *list.Append() = t;
|
|
|
|
}
|
2009-12-11 13:37:01 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/**
|
|
|
|
* 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.Length() == 0) return;
|
|
|
|
|
|
|
|
Train *prev = NULL;
|
|
|
|
/* Iterate over the list and rebuild it. */
|
|
|
|
for (Train **iter = list.Begin(); iter != list.End(); iter++) {
|
|
|
|
Train *t = *iter;
|
|
|
|
if (prev != NULL) {
|
|
|
|
prev->SetNext(t);
|
|
|
|
} else if (t->Previous() != NULL) {
|
|
|
|
/* Make sure the head of the train is always the first in the chain. */
|
|
|
|
t->Previous()->SetNext(NULL);
|
|
|
|
}
|
|
|
|
prev = t;
|
2009-12-11 13:37:01 +00:00
|
|
|
}
|
2009-12-12 22:11:43 +00:00
|
|
|
}
|
2009-12-11 13:37:01 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/**
|
|
|
|
* Remove the given wagon from it's 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 = chain ? part->Last() : part->GetLastEnginePart();
|
|
|
|
|
|
|
|
/* Unlink at the front, but make it point to the next
|
|
|
|
* vehicle after the to be remove part. */
|
|
|
|
if (part->Previous() != NULL) part->Previous()->SetNext(tail->Next());
|
|
|
|
|
|
|
|
/* Unlink at the back */
|
|
|
|
tail->SetNext(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->Next() == NULL || !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 != NULL; 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() != NULL && !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);
|
2009-12-11 13:37:01 +00:00
|
|
|
}
|
2009-12-12 22:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 == NULL) return;
|
2009-12-11 13:37:01 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* We must be the first in the chain. */
|
|
|
|
assert(chain->Previous() == NULL);
|
|
|
|
|
|
|
|
/* Set the appropirate bits for the first in the chain. */
|
|
|
|
if (chain->IsWagon()) {
|
|
|
|
chain->SetFreeWagon();
|
2009-12-11 13:37:01 +00:00
|
|
|
} else {
|
2009-12-12 22:11:43 +00:00
|
|
|
assert(chain->IsEngine());
|
|
|
|
chain->SetFrontEngine();
|
2009-12-11 13:37:01 +00:00
|
|
|
}
|
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* Now clear the bits for the rest of the chain */
|
|
|
|
for (Train *t = chain->Next(); t != NULL; t = t->Next()) {
|
|
|
|
t->ClearFreeWagon();
|
|
|
|
t->ClearFrontEngine();
|
|
|
|
}
|
2009-12-11 13:37:01 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 15:50:07 +00:00
|
|
|
/**
|
2009-12-12 22:11:43 +00:00
|
|
|
* 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_dst The original source chain.
|
|
|
|
* @param dst The source chain after constructing the train.
|
2009-12-11 15:50:07 +00:00
|
|
|
* @return possible error of this command.
|
|
|
|
*/
|
2009-12-12 22:11:43 +00:00
|
|
|
static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
|
2009-12-11 15:50:07 +00:00
|
|
|
{
|
2009-12-12 22:11:43 +00:00
|
|
|
/* Just add 'new' engines and substract 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 != NULL && src->IsEngine() ? 1 : 0) +
|
|
|
|
(dst != NULL && dst->IsEngine() ? 1 : 0) -
|
|
|
|
(original_src != NULL && original_src->IsEngine() ? 1 : 0) -
|
|
|
|
(original_dst != NULL && original_dst->IsEngine() ? 1 : 0) <= 0) {
|
|
|
|
return CommandCost();
|
2009-12-11 15:50:07 +00:00
|
|
|
}
|
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* 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(VEH_TRAIN) <= _settings_game.vehicle.max_trains) return CommandCost();
|
2009-12-11 15:50:07 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
|
2009-12-11 15:50:07 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 16:16:36 +00:00
|
|
|
/**
|
2009-12-12 22:11:43 +00:00
|
|
|
* Check whether the train parts can be attached.
|
|
|
|
* @param t the train to check
|
2009-12-11 16:16:36 +00:00
|
|
|
* @return possible error of this command.
|
|
|
|
*/
|
2009-12-12 22:11:43 +00:00
|
|
|
static CommandCost CheckTrainAttachment(Train *t)
|
2009-12-11 16:16:36 +00:00
|
|
|
{
|
2009-12-12 22:11:43 +00:00
|
|
|
/* No multi-part train, no need to check. */
|
|
|
|
if (t == NULL || t->Next() == NULL || !t->IsEngine()) 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.mammoth_trains ? 100 : 10;
|
|
|
|
|
|
|
|
Train *head = t;
|
|
|
|
Train *prev = t;
|
|
|
|
|
|
|
|
/* Break the prev -> t link so it always holds within the loop. */
|
|
|
|
t = t->Next();
|
|
|
|
allowed_len--;
|
|
|
|
prev->SetNext(NULL);
|
|
|
|
|
|
|
|
/* Make sure the cache is cleared. */
|
|
|
|
head->InvalidateNewGRFCache();
|
|
|
|
|
|
|
|
while (t != NULL) {
|
|
|
|
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(NULL);
|
|
|
|
|
2009-12-11 16:16:36 +00:00
|
|
|
/* Don't check callback for articulated or rear dual headed parts */
|
2009-12-12 22:11:43 +00:00
|
|
|
if (!t->IsArticulatedPart() && !t->IsRearDualheaded()) {
|
|
|
|
allowed_len--; // We do not count articulated parts and rear heads either.
|
|
|
|
|
2009-12-11 16:16:36 +00:00
|
|
|
/* Back up and clear the first_engine data to avoid using wagon override group */
|
2009-12-12 22:11:43 +00:00
|
|
|
EngineID first_engine = t->tcache.first_engine;
|
|
|
|
t->tcache.first_engine = INVALID_ENGINE;
|
2009-12-11 16:16:36 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* 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();
|
|
|
|
|
|
|
|
uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, head->engine_type, t, head);
|
2009-12-11 16:16:36 +00:00
|
|
|
|
|
|
|
/* Restore original first_engine data */
|
2009-12-12 22:11:43 +00:00
|
|
|
t->tcache.first_engine = first_engine;
|
2009-12-11 16:16:36 +00:00
|
|
|
|
|
|
|
/* We do not want to remember any cached variables from the test run */
|
2009-12-12 22:11:43 +00:00
|
|
|
t->InvalidateNewGRFCache();
|
|
|
|
head->InvalidateNewGRFCache();
|
2009-12-11 16:16:36 +00:00
|
|
|
|
|
|
|
if (callback != CALLBACK_FAILED) {
|
2009-12-12 22:11:43 +00:00
|
|
|
/* A failing callback means everything is okay */
|
2009-12-11 16:16:36 +00:00
|
|
|
StringID error = STR_NULL;
|
|
|
|
|
|
|
|
if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
|
2009-12-12 22:11:43 +00:00
|
|
|
if (callback < 0xFD) error = GetGRFStringID(GetEngineGRFID(head->engine_type), 0xD000 + callback);
|
|
|
|
|
|
|
|
if (error != STR_NULL) return_cmd_error(error);
|
2009-12-11 16:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* And link it to the new part. */
|
|
|
|
prev->SetNext(t);
|
|
|
|
prev = t;
|
|
|
|
t = next;
|
|
|
|
}
|
2009-12-11 16:16:36 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
if (allowed_len <= 0) return_cmd_error(STR_ERROR_TRAIN_TOO_LONG);
|
|
|
|
return CommandCost();
|
|
|
|
}
|
2009-12-11 16:16:36 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/**
|
|
|
|
* 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_dst The original source chain.
|
|
|
|
* @param dst The source chain after constructing the train.
|
|
|
|
* @return possible error of this command.
|
|
|
|
*/
|
|
|
|
static CommandCost ValidateTrains(Train *original_dst, Train *dst, Train *original_src, Train *src)
|
|
|
|
{
|
|
|
|
/* 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 CheckNewTrain(original_dst, dst, original_src, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = NULL;
|
|
|
|
} else if (*dst_head == NULL) {
|
|
|
|
/* 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 ? NULL :
|
|
|
|
(src->IsMultiheaded() ? src->GetNextUnit() : src->GetNextVehicle());
|
2009-12-11 16:16:36 +00:00
|
|
|
}
|
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* 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);
|
2009-12-11 16:16:36 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* 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 == NULL) return;
|
|
|
|
|
|
|
|
/* Tell the 'world' the train changed. */
|
2010-01-15 18:23:52 +00:00
|
|
|
head->ConsistChanged(false);
|
2009-12-12 22:11:43 +00:00
|
|
|
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 */
|
|
|
|
SetWindowDirty(WC_VEHICLE_REFIT, head->index);
|
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, head->index, VVW_WIDGET_REFIT_VEH);
|
|
|
|
|
|
|
|
/* If we don't have a unit number yet, set one. */
|
|
|
|
if (head->unitnumber != 0) return;
|
|
|
|
head->unitnumber = GetFreeUnitNumber(VEH_TRAIN);
|
2009-12-11 16:16:36 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Move a rail vehicle around inside the depot.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2008-08-16 14:02:20 +00:00
|
|
|
* Note: DC_AUTOREPLACE is set when autoreplace tries to undo its modifications or moves vehicles to temporary locations inside the depot.
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p1 various bitstuffed elements
|
2005-11-14 19:48:04 +00:00
|
|
|
* - p1 (bit 0 - 15) source vehicle index
|
|
|
|
* - p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p2 (bit 0) move all vehicles following the source vehicle
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-10-03 21:20:01 +00:00
|
|
|
VehicleID s = GB(p1, 0, 16);
|
|
|
|
VehicleID d = GB(p1, 16, 16);
|
2009-12-11 15:50:07 +00:00
|
|
|
bool move_chain = HasBit(p2, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *src = Train::GetIfValid(s);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (src == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(src->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-08 20:16:25 +00:00
|
|
|
/* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
|
2009-06-01 15:01:54 +00:00
|
|
|
if (src->vehstatus & VS_CRASHED) return CMD_ERROR;
|
2007-10-08 20:16:25 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* if nothing is selected as destination, try and find a matching vehicle to drag to. */
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *dst;
|
2005-10-03 21:20:01 +00:00
|
|
|
if (d == INVALID_VEHICLE) {
|
2009-07-01 23:57:20 +00:00
|
|
|
dst = src->IsEngine() ? NULL : FindGoodVehiclePos(src);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-05-26 22:10:13 +00:00
|
|
|
dst = Train::GetIfValid(d);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (dst == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(dst->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2007-10-08 20:16:25 +00:00
|
|
|
|
|
|
|
/* Do not allow appending to crashed vehicles, too */
|
2009-06-01 15:01:54 +00:00
|
|
|
if (dst->vehstatus & VS_CRASHED) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* if an articulated part is being handled, deal with its parent vehicle */
|
2009-07-19 19:17:41 +00:00
|
|
|
src = src->GetFirstEnginePart();
|
2005-11-05 16:07:26 +00:00
|
|
|
if (dst != NULL) {
|
2009-07-19 19:17:41 +00:00
|
|
|
dst = dst->GetFirstEnginePart();
|
2005-11-05 16:07:26 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* don't move the same vehicle.. */
|
2007-06-18 19:53:50 +00:00
|
|
|
if (src == dst) return CommandCost();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* locate the head of the two chains */
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *src_head = src->First();
|
|
|
|
Train *dst_head;
|
2005-11-05 16:07:26 +00:00
|
|
|
if (dst != NULL) {
|
2007-08-30 21:11:12 +00:00
|
|
|
dst_head = dst->First();
|
2007-02-24 14:36:14 +00:00
|
|
|
if (dst_head->tile != src_head->tile) return CMD_ERROR;
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Now deal with articulated part of destination wagon */
|
2009-07-02 12:10:39 +00:00
|
|
|
dst = dst->GetLastEnginePart();
|
2007-02-24 14:36:14 +00:00
|
|
|
} else {
|
|
|
|
dst_head = NULL;
|
2005-11-05 16:07:26 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-07-01 23:57:20 +00:00
|
|
|
if (src->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
|
2005-11-18 23:41:03 +00:00
|
|
|
|
2009-12-12 23:24:11 +00:00
|
|
|
/* When moving all wagons, we can't have the same src_head and dst_head */
|
2009-12-11 15:50:07 +00:00
|
|
|
if (move_chain && src_head == dst_head) return CommandCost();
|
2006-01-25 21:33:57 +00:00
|
|
|
|
2009-12-12 23:24:11 +00:00
|
|
|
/* When moving a multiheaded part to be place after itself, bail out. */
|
2009-12-12 23:49:30 +00:00
|
|
|
if (!move_chain && dst != NULL && dst->IsRearDualheaded() && src == dst->other_multiheaded_part) return CommandCost();
|
2009-12-12 23:24:11 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* Check if all vehicles in the source train are stopped inside a depot. */
|
|
|
|
if (!src_head->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* Check if all vehicles in the destination train are stopped inside a depot. */
|
|
|
|
if (dst_head != NULL && !dst_head->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
|
2006-10-05 12:59:28 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* 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;
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
MakeTrainBackup(original_src, src_head);
|
|
|
|
MakeTrainBackup(original_dst, dst_head);
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* 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 ? NULL : dst_head);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* (Re)arrange the trains in the wanted arrangement. */
|
|
|
|
ArrangeTrains(&dst_head, dst, &src_head, src, move_chain);
|
2005-03-09 19:09:04 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
if ((flags & DC_AUTOREPLACE) == 0) {
|
|
|
|
/* 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. */
|
|
|
|
CommandCost ret = ValidateTrains(original_dst_head, dst_head, original_src_head, src_head);
|
|
|
|
if (ret.Failed()) {
|
|
|
|
/* Restore the train we had. */
|
|
|
|
RestoreTrainBackup(original_src);
|
|
|
|
RestoreTrainBackup(original_dst);
|
|
|
|
return ret;
|
2005-11-06 01:15:10 +00:00
|
|
|
}
|
2009-12-12 22:11:43 +00:00
|
|
|
}
|
2005-11-06 01:15:10 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* do it? */
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* 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 an 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 hapens
|
|
|
|
* 7) wagon gets moved, nothing happens
|
|
|
|
*/
|
|
|
|
if (src == original_src_head && src->IsEngine() && !src->IsFrontEngine()) {
|
|
|
|
/* Cases #2 and #3: the front engine gets trashed. */
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, src->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_ORDERS, src->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_REFIT, src->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_DETAILS, src->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_TIMETABLE, src->index);
|
|
|
|
|
2009-12-12 23:49:30 +00:00
|
|
|
/* We are going to be move to another train. So we
|
|
|
|
* are no part of this group anymore. In case we
|
|
|
|
* are not moving group... well, then we do not need
|
2009-12-13 22:17:12 +00:00
|
|
|
* to move.
|
|
|
|
* Or we are moving to later in the train and our
|
|
|
|
* new head isn't a front engine anymore.
|
|
|
|
*/
|
|
|
|
if (dst_head != NULL ? dst_head != src : !src_head->IsFrontEngine()) {
|
|
|
|
DecreaseGroupNumVehicle(src->group_id);
|
|
|
|
}
|
2009-12-12 23:49:30 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* Delete orders, group stuff and the unit number as we're not the
|
|
|
|
* front of any vehicle anymore. */
|
|
|
|
DeleteVehicleOrders(src);
|
|
|
|
RemoveVehicleFromGroup(src);
|
|
|
|
src->unitnumber = 0;
|
2007-02-25 09:27:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-12 23:49:30 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* Handle 'new engine' part of cases #1b, #2b, #3b, #4b and #5 in NormaliseTrainHead. */
|
|
|
|
NormaliseTrainHead(src_head);
|
|
|
|
NormaliseTrainHead(dst_head);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-12 22:11:43 +00:00
|
|
|
/* We are undoubtedly changing something in the depot and train list. */
|
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
|
2009-12-12 22:11:43 +00:00
|
|
|
} else {
|
|
|
|
/* We don't want to execute what we're just tried. */
|
|
|
|
RestoreTrainBackup(original_src);
|
|
|
|
RestoreTrainBackup(original_dst);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Sell a (single) train wagon/engine.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-05 20:46:14 +00:00
|
|
|
* @param p1 the wagon/engine index
|
|
|
|
* @param p2 the selling mode
|
2005-05-09 22:33:00 +00:00
|
|
|
* - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
|
|
|
|
* - p2 = 1: sell the vehicle and all vehicles following it in the chain
|
2008-04-18 04:37:06 +00:00
|
|
|
* if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-05 20:46:14 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSellRailWagon(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-02 20:08:23 +00:00
|
|
|
/* Check if we deleted a vehicle window */
|
|
|
|
Window *w = NULL;
|
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *v = Train::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-12-12 22:59:48 +00:00
|
|
|
|
|
|
|
/* Sell a chain of vehicles or not? */
|
|
|
|
bool sell_chain = HasBit(p2, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-05 17:59:21 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE);
|
2007-10-08 20:06:37 +00:00
|
|
|
|
2009-07-19 19:17:41 +00:00
|
|
|
v = v->GetFirstEnginePart();
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *first = v->First();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* make sure the vehicle is stopped in the depot */
|
2009-12-11 22:04:57 +00:00
|
|
|
if (!first->IsStoppedInDepot()) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
|
2006-03-12 12:19:25 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-01 23:57:20 +00:00
|
|
|
if (v->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
|
2005-11-18 23:41:03 +00:00
|
|
|
|
2009-12-12 22:59:48 +00:00
|
|
|
/* 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 = NULL;
|
|
|
|
|
|
|
|
/* Split the train in the wanted way. */
|
|
|
|
ArrangeTrains(&sell_head, NULL, &new_head, v, sell_chain);
|
|
|
|
|
|
|
|
/* We don't need to validate the second train; it's going to be sold. */
|
2010-03-13 17:11:28 +00:00
|
|
|
ret = ValidateTrains(NULL, NULL, first, new_head);
|
2009-12-12 22:59:48 +00:00
|
|
|
if (ret.Failed()) {
|
|
|
|
/* Restore the train we had. */
|
|
|
|
RestoreTrainBackup(original);
|
|
|
|
return ret;
|
2005-05-05 20:46:14 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_NEW_VEHICLES);
|
2009-12-12 22:59:48 +00:00
|
|
|
for (Train *t = sell_head; t != NULL; t = t->Next()) cost.AddCost(-t->value);
|
2005-03-09 19:09:04 +00:00
|
|
|
|
2009-12-12 22:59:48 +00:00
|
|
|
/* do it? */
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* First normalise the sub types of the chain. */
|
|
|
|
NormaliseSubtypes(new_head);
|
|
|
|
|
|
|
|
if (v == first && v->IsEngine() && !sell_chain && new_head != NULL && new_head->IsFrontEngine()) {
|
|
|
|
/* We are selling the front engine. In this case we want to
|
|
|
|
* 'give' the order, unitnumber and such to the new head. */
|
|
|
|
new_head->orders.list = first->orders.list;
|
|
|
|
new_head->AddToShared(first);
|
|
|
|
DeleteVehicleOrders(first);
|
|
|
|
|
|
|
|
/* Copy other important data from the front engine */
|
|
|
|
new_head->CopyVehicleConfigAndStatistics(first);
|
|
|
|
IncreaseGroupNumVehicle(new_head->group_id);
|
|
|
|
|
|
|
|
/* If we deleted a window then open a new one for the 'new' train */
|
|
|
|
if (IsLocalCompany() && w != NULL) ShowVehicleViewWindow(new_head);
|
|
|
|
}
|
2005-05-05 20:46:14 +00:00
|
|
|
|
2009-12-12 22:59:48 +00:00
|
|
|
/* We need to update the information about the train. */
|
|
|
|
NormaliseTrainHead(new_head);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-12-12 22:59:48 +00:00
|
|
|
/* We are undoubtedly changing something in the depot and train list. */
|
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-12 22:59:48 +00:00
|
|
|
/* Actually delete the sold 'goods' */
|
|
|
|
delete sell_head;
|
|
|
|
} else {
|
|
|
|
/* We don't want to execute what we're just tried. */
|
|
|
|
RestoreTrainBackup(original);
|
2005-05-05 20:46:14 +00:00
|
|
|
}
|
2009-12-12 22:59:48 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
void Train::UpdateDeltaXY(Direction direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-01 16:35:14 +00:00
|
|
|
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
static const uint32 _delta_xy_table[8] = {
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(3, 7, -1, -3),
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(7, 3, -3, -1),
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(3, 7, -1, -3),
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(7, 3, -3, -1),
|
|
|
|
};
|
|
|
|
#undef MKIT
|
|
|
|
|
|
|
|
uint32 x = _delta_xy_table[direction];
|
2007-05-01 16:35:14 +00:00
|
|
|
this->x_offs = GB(x, 0, 8);
|
|
|
|
this->y_offs = GB(x, 8, 8);
|
2008-04-01 14:03:20 +00:00
|
|
|
this->x_extent = GB(x, 16, 8);
|
|
|
|
this->y_extent = GB(x, 24, 8);
|
|
|
|
this->z_extent = 6;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static inline void SetLastSpeed(Train *v, int spd)
|
2005-11-14 19:48:04 +00:00
|
|
|
{
|
2009-05-22 22:55:41 +00:00
|
|
|
int old = v->tcache.last_speed;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (spd != old) {
|
2009-05-22 22:55:41 +00:00
|
|
|
v->tcache.last_speed = spd;
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.vehicle_speed || (old == 0) != (spd == 0)) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2008-04-07 12:36:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:53:21 +00:00
|
|
|
/** Mark a train as stuck and stop it if it isn't stopped right now. */
|
2009-05-22 22:22:46 +00:00
|
|
|
static void MarkTrainAsStuck(Train *v)
|
2005-01-09 16:02:06 +00:00
|
|
|
{
|
2009-05-22 22:33:05 +00:00
|
|
|
if (!HasBit(v->flags, VRF_TRAIN_STUCK)) {
|
2008-08-02 22:53:21 +00:00
|
|
|
/* It is the first time the problem occured, set the "train stuck" flag. */
|
2009-05-22 22:33:05 +00:00
|
|
|
SetBit(v->flags, VRF_TRAIN_STUCK);
|
2009-07-21 17:14:05 +00:00
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2008-08-02 22:53:21 +00:00
|
|
|
|
|
|
|
/* Stop train */
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
SetLastSpeed(v, 0);
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2008-08-02 22:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SwapTrainFlags(uint16 *swap_flag1, uint16 *swap_flag2)
|
|
|
|
{
|
|
|
|
uint16 flag1 = *swap_flag1;
|
|
|
|
uint16 flag2 = *swap_flag2;
|
2005-01-09 16:02:06 +00:00
|
|
|
|
|
|
|
/* Clear the flags */
|
2010-03-06 12:50:55 +00:00
|
|
|
ClrBit(*swap_flag1, GVF_GOINGUP_BIT);
|
|
|
|
ClrBit(*swap_flag1, GVF_GOINGDOWN_BIT);
|
|
|
|
ClrBit(*swap_flag2, GVF_GOINGUP_BIT);
|
|
|
|
ClrBit(*swap_flag2, GVF_GOINGDOWN_BIT);
|
2005-01-09 16:02:06 +00:00
|
|
|
|
|
|
|
/* Reverse the rail-flags (if needed) */
|
2010-03-06 12:50:55 +00:00
|
|
|
if (HasBit(flag1, GVF_GOINGUP_BIT)) {
|
|
|
|
SetBit(*swap_flag2, GVF_GOINGDOWN_BIT);
|
|
|
|
} else if (HasBit(flag1, GVF_GOINGDOWN_BIT)) {
|
|
|
|
SetBit(*swap_flag2, GVF_GOINGUP_BIT);
|
2010-03-06 16:15:50 +00:00
|
|
|
}
|
2010-03-06 12:50:55 +00:00
|
|
|
if (HasBit(flag2, GVF_GOINGUP_BIT)) {
|
|
|
|
SetBit(*swap_flag1, GVF_GOINGDOWN_BIT);
|
|
|
|
} else if (HasBit(flag2, GVF_GOINGDOWN_BIT)) {
|
|
|
|
SetBit(*swap_flag1, GVF_GOINGUP_BIT);
|
2010-03-06 16:15:50 +00:00
|
|
|
}
|
2005-01-09 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void ReverseTrainSwapVeh(Train *v, int l, int r)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *a, *b;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* locate vehicles to swap */
|
2007-08-30 13:03:56 +00:00
|
|
|
for (a = v; l != 0; l--) a = a->Next();
|
|
|
|
for (b = v; r != 0; r--) b = b->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (a != b) {
|
|
|
|
/* swap the hidden bits */
|
|
|
|
{
|
2010-02-25 21:13:50 +00:00
|
|
|
uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus & VS_HIDDEN);
|
|
|
|
b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus & VS_HIDDEN);
|
2004-08-09 17:04:08 +00:00
|
|
|
a->vehstatus = tmp;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
Swap(a->track, b->track);
|
2010-02-25 22:59:24 +00:00
|
|
|
Swap(a->direction, b->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* toggle direction */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (a->track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
|
|
|
|
if (b->track != TRACK_BIT_DEPOT) b->direction = ReverseDir(b->direction);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-01-19 11:47:48 +00:00
|
|
|
Swap(a->x_pos, b->x_pos);
|
|
|
|
Swap(a->y_pos, b->y_pos);
|
|
|
|
Swap(a->tile, b->tile);
|
|
|
|
Swap(a->z_pos, b->z_pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-06 12:50:55 +00:00
|
|
|
SwapTrainFlags(&a->gv_flags, &b->gv_flags);
|
2005-01-09 16:02:06 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* update other vars */
|
2009-08-08 18:45:12 +00:00
|
|
|
a->UpdateViewport(true, true);
|
|
|
|
b->UpdateViewport(true, true);
|
2005-03-25 12:07:26 +00:00
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
/* call the proper EnterTile function unless we are in a wormhole */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (a->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
|
|
|
|
if (b->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-05-22 22:33:05 +00:00
|
|
|
if (a->track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
|
2009-08-08 18:45:12 +00:00
|
|
|
a->UpdateViewport(true, true);
|
2005-03-25 12:07:26 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (a->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
|
|
|
|
/* Update train's power incase tiles were different rail type */
|
2010-01-15 18:23:52 +00:00
|
|
|
v->PowerChanged();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the vehicle is a train
|
|
|
|
* @param v vehicle on tile
|
|
|
|
* @return v if it is a train, NULL otherwise
|
|
|
|
*/
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *TrainOnTileEnum(Vehicle *v, void *)
|
2004-12-21 16:17:27 +00:00
|
|
|
{
|
2008-01-17 17:57:39 +00:00
|
|
|
return (v->type == VEH_TRAIN) ? v : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a train is approaching a rail-road crossing
|
|
|
|
* @param v vehicle on tile
|
|
|
|
* @param data tile with crossing we are testing
|
|
|
|
* @return v if it is approaching a crossing, NULL otherwise
|
|
|
|
*/
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *TrainApproachingCrossingEnum(Vehicle *v, void *data)
|
2008-01-17 17:57:39 +00:00
|
|
|
{
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
Train *t = Train::From(v);
|
|
|
|
if (!t->IsFrontEngine()) return NULL;
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
TileIndex tile = *(TileIndex *)data;
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (TrainApproachingCrossingTile(t) != tile) return NULL;
|
|
|
|
|
|
|
|
return t;
|
2004-12-21 16:17:27 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a vehicle approaching rail-road crossing
|
|
|
|
* @param tile tile to test
|
2008-09-07 11:23:10 +00:00
|
|
|
* @return true if a vehicle is approaching the crossing
|
2008-01-17 17:57:39 +00:00
|
|
|
* @pre tile is a rail-road crossing
|
|
|
|
*/
|
2008-09-07 11:23:10 +00:00
|
|
|
static bool TrainApproachingCrossing(TileIndex tile)
|
2008-01-17 17:57:39 +00:00
|
|
|
{
|
|
|
|
assert(IsLevelCrossingTile(tile));
|
|
|
|
|
2008-02-18 18:35:36 +00:00
|
|
|
DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
|
2008-01-17 17:57:39 +00:00
|
|
|
TileIndex tile_from = tile + TileOffsByDiagDir(dir);
|
|
|
|
|
2008-09-07 11:23:10 +00:00
|
|
|
if (HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum)) return true;
|
2008-01-17 17:57:39 +00:00
|
|
|
|
|
|
|
dir = ReverseDiagDir(dir);
|
|
|
|
tile_from = tile + TileOffsByDiagDir(dir);
|
|
|
|
|
2008-09-07 11:23:10 +00:00
|
|
|
return HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum);
|
2008-01-17 17:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets correct crossing state
|
|
|
|
* @param tile tile to update
|
2008-01-17 20:41:33 +00:00
|
|
|
* @param sound should we play sound?
|
2008-01-17 17:57:39 +00:00
|
|
|
* @pre tile is a rail-road crossing
|
|
|
|
*/
|
2008-01-17 20:41:33 +00:00
|
|
|
void UpdateLevelCrossing(TileIndex tile, bool sound)
|
2005-01-23 10:40:54 +00:00
|
|
|
{
|
2008-01-17 17:57:39 +00:00
|
|
|
assert(IsLevelCrossingTile(tile));
|
|
|
|
|
2008-08-02 22:57:18 +00:00
|
|
|
/* train on crossing || train approaching crossing || reserved */
|
2009-07-21 22:37:10 +00:00
|
|
|
bool new_state = HasVehicleOnPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile) || HasCrossingReservation(tile);
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2008-01-17 20:41:33 +00:00
|
|
|
if (new_state != IsCrossingBarred(tile)) {
|
|
|
|
if (new_state && sound) {
|
|
|
|
SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
|
|
|
|
}
|
|
|
|
SetCrossingBarred(tile, new_state);
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
2005-01-23 10:40:54 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2008-01-18 21:44:20 +00:00
|
|
|
/**
|
|
|
|
* 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)) {
|
|
|
|
BarCrossing(tile);
|
|
|
|
SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-06 22:44:11 +00:00
|
|
|
/**
|
|
|
|
* Advances wagons for train reversing, needed for variable length wagons.
|
2008-03-19 20:50:19 +00:00
|
|
|
* This one is called before the train is reversed.
|
2005-06-06 22:44:11 +00:00
|
|
|
* @param v First vehicle in chain
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static void AdvanceWagonsBeforeSwap(Train *v)
|
2005-06-06 22:44:11 +00:00
|
|
|
{
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *base = v;
|
|
|
|
Train *first = base; // first vehicle to move
|
2009-07-20 19:58:33 +00:00
|
|
|
Train *last = v->Last(); // last vehicle to move
|
2007-02-25 09:27:03 +00:00
|
|
|
uint length = CountVehiclesInChain(v);
|
2005-06-06 22:44:11 +00:00
|
|
|
|
|
|
|
while (length > 2) {
|
2008-03-19 20:50:19 +00:00
|
|
|
last = last->Previous();
|
|
|
|
first = first->Next();
|
2005-06-06 22:44:11 +00:00
|
|
|
|
2009-05-22 22:55:41 +00:00
|
|
|
int differential = base->tcache.cached_veh_length - last->tcache.cached_veh_length;
|
2005-06-06 22:44:11 +00:00
|
|
|
|
2008-03-19 20:42:05 +00:00
|
|
|
/* do not update images now
|
2008-03-19 20:50:19 +00:00
|
|
|
* negative differential will be handled in AdvanceWagonsAfterSwap() */
|
2009-03-11 23:23:08 +00:00
|
|
|
for (int i = 0; i < differential; i++) TrainController(first, last->Next());
|
2005-06-06 22:44:11 +00:00
|
|
|
|
2008-03-19 20:50:19 +00:00
|
|
|
base = first; // == base->Next()
|
|
|
|
length -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances wagons for train reversing, needed for variable length wagons.
|
|
|
|
* This one is called after the train is reversed.
|
|
|
|
* @param v First vehicle in chain
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static void AdvanceWagonsAfterSwap(Train *v)
|
2008-03-19 20:50:19 +00:00
|
|
|
{
|
|
|
|
/* first of all, fix the situation when the train was entering a depot */
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *dep = v; // last vehicle in front of just left depot
|
2009-05-22 22:33:05 +00:00
|
|
|
while (dep->Next() != NULL && (dep->track == TRACK_BIT_DEPOT || dep->Next()->track != TRACK_BIT_DEPOT)) {
|
2008-03-19 20:50:19 +00:00
|
|
|
dep = dep->Next(); // find first vehicle outside of a depot, with next vehicle inside a depot
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *leave = dep->Next(); // first vehicle in a depot we are leaving now
|
2008-03-19 20:50:19 +00:00
|
|
|
|
|
|
|
if (leave != NULL) {
|
|
|
|
/* '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 &= ~VS_HIDDEN; // move it out of the depot
|
2009-05-22 22:33:05 +00:00
|
|
|
leave->track = TrackToTrackBits(GetRailDepotTrack(leave->tile));
|
2009-03-11 23:23:08 +00:00
|
|
|
for (int i = 0; i >= d; i--) TrainController(leave, NULL); // maybe move it, and maybe let another wagon leave
|
2008-03-19 20:50:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dep = NULL; // no vehicle in a depot, so no vehicle leaving a depot
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *base = v;
|
|
|
|
Train *first = base; // first vehicle to move
|
2009-07-20 19:58:33 +00:00
|
|
|
Train *last = v->Last(); // last vehicle to move
|
2008-03-19 20:50:19 +00:00
|
|
|
uint length = CountVehiclesInChain(v);
|
|
|
|
|
|
|
|
/* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly
|
|
|
|
* they have already correct spacing, so we have to make sure they are moved how they should */
|
|
|
|
bool nomove = (dep == NULL); // if there is no vehicle leaving a depot, limit the number of wagons moved immediatelly
|
|
|
|
|
|
|
|
while (length > 2) {
|
|
|
|
/* we reached vehicle (originally) in front of a depot, stop now
|
|
|
|
* (we would move wagons that are alredy 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->Previous();
|
2007-08-30 13:03:56 +00:00
|
|
|
first = first->Next();
|
2008-03-19 20:50:19 +00:00
|
|
|
|
2009-05-22 22:55:41 +00:00
|
|
|
int differential = last->tcache.cached_veh_length - base->tcache.cached_veh_length;
|
2008-03-19 20:50:19 +00:00
|
|
|
|
|
|
|
/* do not update images now */
|
2009-03-11 23:23:08 +00:00
|
|
|
for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL));
|
2008-03-19 20:50:19 +00:00
|
|
|
|
|
|
|
base = first; // == base->Next()
|
2005-06-06 22:44:11 +00:00
|
|
|
length -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void ReverseTrainDirection(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsRailDepotTile(v->tile)) {
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-10 23:52:44 +00:00
|
|
|
/* Clear path reservation in front if train is not stuck. */
|
|
|
|
if (!HasBit(v->flags, VRF_TRAIN_STUCK)) FreeTrainTrackReservation(v);
|
2008-08-02 22:56:07 +00:00
|
|
|
|
2004-12-21 16:02:14 +00:00
|
|
|
/* Check if we were approaching a rail/road-crossing */
|
2008-01-17 17:57:39 +00:00
|
|
|
TileIndex crossing = TrainApproachingCrossingTile(v);
|
2004-12-21 16:02:14 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* count number of vehicles */
|
2008-04-07 12:36:50 +00:00
|
|
|
int r = CountVehiclesInChain(v) - 1; // number of vehicles - 1
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-03-19 20:50:19 +00:00
|
|
|
AdvanceWagonsBeforeSwap(v);
|
2005-06-06 22:44:11 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* swap start<>end, start+1<>end-1, ... */
|
2007-02-25 09:27:03 +00:00
|
|
|
int l = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
ReverseTrainSwapVeh(v, l++, r--);
|
|
|
|
} while (l <= r);
|
|
|
|
|
2008-03-19 20:50:19 +00:00
|
|
|
AdvanceWagonsAfterSwap(v);
|
2005-06-06 22:44:11 +00:00
|
|
|
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsRailDepotTile(v->tile)) {
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
ToggleBit(v->flags, VRF_TOGGLE_REVERSE);
|
2007-12-27 13:25:23 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(v->flags, VRF_REVERSING);
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2008-03-03 21:42:37 +00:00
|
|
|
/* recalculate cached data */
|
2010-01-15 18:23:52 +00:00
|
|
|
v->ConsistChanged(true);
|
2008-03-03 21:42:37 +00:00
|
|
|
|
|
|
|
/* update all images */
|
2009-08-08 18:45:12 +00:00
|
|
|
for (Vehicle *u = v; u != NULL; u = u->Next()) u->UpdateViewport(false, false);
|
2008-03-03 21:42:37 +00:00
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
/* update crossing we were approaching */
|
2008-01-17 19:49:06 +00:00
|
|
|
if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
|
2008-01-17 17:57:39 +00:00
|
|
|
|
|
|
|
/* maybe we are approaching crossing now, after reversal */
|
|
|
|
crossing = TrainApproachingCrossingTile(v);
|
2008-01-18 21:44:20 +00:00
|
|
|
if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);
|
2008-08-02 22:56:07 +00:00
|
|
|
|
|
|
|
/* If we are inside a depot after reversing, don't bother with path reserving. */
|
2009-06-10 16:02:00 +00:00
|
|
|
if (v->track == TRACK_BIT_DEPOT) {
|
2008-08-22 22:28:26 +00:00
|
|
|
/* Can't be stuck here as inside a depot is always a safe tile. */
|
2009-09-13 19:15:59 +00:00
|
|
|
if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(v->flags, VRF_TRAIN_STUCK);
|
2008-08-22 22:28:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-08-02 22:56:07 +00:00
|
|
|
|
|
|
|
/* TrainExitDir does not always produce the desired dir for depots and
|
|
|
|
* tunnels/bridges that is needed for UpdateSignalsOnSegment. */
|
2009-05-22 22:33:05 +00:00
|
|
|
DiagDirection dir = TrainExitDir(v->direction, v->track);
|
2008-08-02 22:56:07 +00:00
|
|
|
if (IsRailDepotTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE)) dir = INVALID_DIAGDIR;
|
|
|
|
|
|
|
|
if (UpdateSignalsOnSegment(v->tile, dir, v->owner) == SIGSEG_PBS || _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 = !(IsTileType(v->tile, MP_RAILWAY) &&
|
2009-05-22 18:17:20 +00:00
|
|
|
HasSignalOnTrackdir(v->tile, v->GetVehicleTrackdir()) &&
|
2009-05-22 22:33:05 +00:00
|
|
|
!IsPbsSignal(GetSignalType(v->tile, FindFirstTrack(v->track))));
|
2008-08-02 22:56:07 +00:00
|
|
|
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
|
2008-08-10 21:56:47 +00:00
|
|
|
if (TryPathReserve(v, false, first_tile_okay)) {
|
2008-08-02 22:56:07 +00:00
|
|
|
/* Do a look-ahead now in case our current tile was already a safe tile. */
|
|
|
|
CheckNextTrainTile(v);
|
2008-08-10 21:56:47 +00:00
|
|
|
} else if (v->current_order.GetType() != OT_LOADING) {
|
|
|
|
/* Do not wait for a way out when we're still loading */
|
|
|
|
MarkTrainAsStuck(v);
|
2008-08-02 22:56:07 +00:00
|
|
|
}
|
2009-05-22 22:33:05 +00:00
|
|
|
} else if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
|
2009-01-05 20:29:00 +00:00
|
|
|
/* A train not inside a PBS block can't be stuck. */
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(v->flags, VRF_TRAIN_STUCK);
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2008-08-02 22:56:07 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Reverse train.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p1 train to reverse
|
2006-03-18 13:00:32 +00:00
|
|
|
* @param p2 if true, reverse a unit in a train (needs to be in a depot)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-09 22:33:00 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *v = Train::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 12:36:50 +00:00
|
|
|
if (p2 != 0) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* turn a single unit around */
|
2006-03-18 13:00:32 +00:00
|
|
|
|
2009-09-14 12:22:57 +00:00
|
|
|
if (v->IsMultiheaded() || HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) {
|
2009-07-08 22:11:55 +00:00
|
|
|
return_cmd_error(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE_MULTIPLE_UNITS);
|
2006-03-18 13:00:32 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *front = v->First();
|
2007-04-04 03:21:14 +00:00
|
|
|
/* make sure the vehicle is stopped in the depot */
|
2009-12-11 22:04:57 +00:00
|
|
|
if (!front->IsStoppedInDepot()) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
|
2006-03-18 13:00:32 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-18 13:15:34 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-05-22 22:33:05 +00:00
|
|
|
ToggleBit(v->flags, VRF_REVERSE_DIRECTION);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
2009-12-16 16:23:44 +00:00
|
|
|
/* We cancel any 'skip signal at dangers' here */
|
|
|
|
v->force_proceed = 0;
|
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
2006-03-18 13:15:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-08-26 20:16:02 +00:00
|
|
|
/* turn the whole train around */
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((v->vehstatus & VS_CRASHED) || v->breakdown_ctr != 0) return CMD_ERROR;
|
2006-03-18 13:15:34 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-02-10 12:49:12 +00:00
|
|
|
/* Properly leave the station if we are loading and won't be loading anymore */
|
|
|
|
if (v->current_order.IsType(OT_LOADING)) {
|
|
|
|
const Vehicle *last = v;
|
|
|
|
while (last->Next() != NULL) last = last->Next();
|
|
|
|
|
|
|
|
/* not a station || different station --> leave the station */
|
|
|
|
if (!IsTileType(last->tile, MP_STATION) || GetStationIndex(last->tile) != GetStationIndex(v->tile)) {
|
|
|
|
v->LeaveStation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-16 16:23:44 +00:00
|
|
|
/* We cancel any 'skip signal at dangers' here */
|
|
|
|
v->force_proceed = 0;
|
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
|
|
|
|
2010-01-30 16:27:35 +00:00
|
|
|
if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && v->cur_speed != 0) {
|
2009-05-22 22:33:05 +00:00
|
|
|
ToggleBit(v->flags, VRF_REVERSING);
|
2006-03-18 13:00:32 +00:00
|
|
|
} else {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
SetLastSpeed(v, 0);
|
2008-09-21 18:28:35 +00:00
|
|
|
HideFillingPercent(&v->fill_percent_te_id);
|
2006-03-18 13:00:32 +00:00
|
|
|
ReverseTrainDirection(v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Force a train through a red signal
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p1 train to ignore the red signal
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-09 22:33:00 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *t = Train::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (t == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(t->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-16 16:23:44 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* 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. In the other cases we
|
|
|
|
* would like to pass the signal at danger and run till the
|
|
|
|
* next signal we encounter. */
|
|
|
|
t->force_proceed = t->force_proceed == 2 ? 0 : HasBit(t->flags, VRF_TRAIN_STUCK) || t->IsInDepot() ? 1 : 2;
|
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, t->index);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
/** Refits a train to the specified cargo type.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-14 12:36:16 +00:00
|
|
|
* @param p1 vehicle ID of the train to refit
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
2006-06-04 17:38:48 +00:00
|
|
|
* - p2 = (bit 0-7) - the new cargo type to refit to
|
|
|
|
* - p2 = (bit 8-15) - the new cargo subtype to refit to
|
2007-04-29 08:43:00 +00:00
|
|
|
* - p2 = (bit 16) - refit only this vehicle
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-14 12:36:16 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRefitRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 08:09:57 +00:00
|
|
|
CargoID new_cid = GB(p2, 0, 8);
|
2006-06-04 17:38:48 +00:00
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
2007-11-19 21:02:30 +00:00
|
|
|
bool only_this = HasBit(p2, 16);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
Train *v = Train::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-05-22 22:22:46 +00:00
|
|
|
|
2009-12-11 22:04:57 +00:00
|
|
|
if (!v->IsStoppedInDepot()) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
|
2009-08-05 17:59:21 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
|
2005-05-14 12:36:16 +00:00
|
|
|
|
|
|
|
/* Check cargo */
|
2007-03-16 21:42:11 +00:00
|
|
|
if (new_cid >= NUM_CARGO) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-10-28 21:09:37 +00:00
|
|
|
CommandCost cost = RefitVehicle(v, only_this, new_cid, new_subtype, flags);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-02 22:10:04 +00:00
|
|
|
/* Update the train's cached variables */
|
2009-10-27 20:37:55 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-10-28 21:09:37 +00:00
|
|
|
Train *front = v->First();
|
2010-01-15 18:23:52 +00:00
|
|
|
front->ConsistChanged(false);
|
2009-10-28 21:09:37 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, front->index);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
|
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
|
2009-10-27 20:37:55 +00:00
|
|
|
} else {
|
|
|
|
v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
|
|
|
|
}
|
2006-10-02 22:10:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/** returns the tile of a depot to goto to. The given vehicle must not be
|
|
|
|
* crashed! */
|
2009-12-02 09:57:17 +00:00
|
|
|
static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-03 20:45:23 +00:00
|
|
|
assert(!(v->vehstatus & VS_CRASHED));
|
|
|
|
|
2009-12-02 09:57:17 +00:00
|
|
|
if (IsRailDepotTile(v->tile)) return FindDepotData(v->tile, 0);
|
2008-08-17 11:04:37 +00:00
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
PBSTileInfo origin = FollowTrainReservation(v);
|
2009-12-02 09:57:17 +00:00
|
|
|
if (IsRailDepotTile(origin.tile)) return FindDepotData(origin.tile, 0);
|
2008-08-17 11:04:37 +00:00
|
|
|
|
2009-12-01 22:18:51 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_trains) {
|
2009-12-20 16:19:47 +00:00
|
|
|
case VPF_NPF: return NPFTrainFindNearestDepot(v, max_distance);
|
|
|
|
case VPF_YAPF: return YapfTrainFindNearestDepot(v, max_distance);
|
2008-02-13 17:54:11 +00:00
|
|
|
|
2009-12-02 09:57:17 +00:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-11 08:14:43 +00:00
|
|
|
bool Train::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
|
|
|
|
{
|
2009-12-02 09:57:17 +00:00
|
|
|
FindDepotData tfdd = FindClosestTrainDepot(this, 0);
|
2008-10-14 18:38:51 +00:00
|
|
|
if (tfdd.best_length == UINT_MAX) return false;
|
2008-04-11 08:14:43 +00:00
|
|
|
|
|
|
|
if (location != NULL) *location = tfdd.tile;
|
2009-09-10 14:37:55 +00:00
|
|
|
if (destination != NULL) *destination = GetDepotIndex(tfdd.tile);
|
2008-04-11 08:14:43 +00:00
|
|
|
if (reverse != NULL) *reverse = tfdd.reverse;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Send a train to a depot
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p1 train to send to the depot
|
2006-08-29 23:39:57 +00:00
|
|
|
* @param p2 various bitmasked elements
|
2006-09-01 10:24:15 +00:00
|
|
|
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
|
|
|
|
* - p2 bit 8-10 - VLW flag (for mass goto depot)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-09 22:33:00 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSendTrainToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_MASS_SEND) {
|
|
|
|
/* Mass goto depot requested */
|
2006-09-04 15:16:58 +00:00
|
|
|
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
|
2008-09-30 20:39:50 +00:00
|
|
|
return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
|
2006-08-30 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
Train *v = Train::GetIfValid(p1);
|
|
|
|
if (v == NULL) return CMD_ERROR;
|
2007-04-20 11:39:01 +00:00
|
|
|
|
2008-04-11 08:40:10 +00:00
|
|
|
return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-04 12:58:18 +00:00
|
|
|
static const int8 _vehicle_smoke_pos[8] = {
|
|
|
|
1, 1, 1, 0, -1, -1, -1, 0
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void HandleLocomotiveSmokeCloud(const Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-27 18:17:01 +00:00
|
|
|
bool sound = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-01 18:35:24 +00:00
|
|
|
if ((v->vehstatus & VS_TRAIN_SLOWING) || v->cur_speed < 2) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2008-04-07 12:36:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
const Train *u = v;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
2007-01-24 07:14:09 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
|
2009-05-22 22:55:41 +00:00
|
|
|
int effect_offset = GB(v->tcache.cached_vis_effect, 0, 4) - 8;
|
|
|
|
byte effect_type = GB(v->tcache.cached_vis_effect, 4, 2);
|
|
|
|
bool disable_effect = HasBit(v->tcache.cached_vis_effect, 6);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* no smoke? */
|
2007-01-30 11:53:35 +00:00
|
|
|
if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 0) ||
|
2005-11-04 12:58:18 +00:00
|
|
|
disable_effect ||
|
2006-04-14 10:48:54 +00:00
|
|
|
v->vehstatus & VS_HIDDEN) {
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* No smoke in depots or tunnels */
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsRailDepotTile(v->tile) || IsTunnelTile(v->tile)) continue;
|
2006-04-19 16:10:17 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* No sparks for electric vehicles on nonelectrified tracks */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (!HasPowerOnRail(v->railtype, GetTileRailType(v->tile))) continue;
|
2005-11-07 13:02:33 +00:00
|
|
|
|
2005-11-04 12:58:18 +00:00
|
|
|
if (effect_type == 0) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Use default effect type for engine class. */
|
2007-01-24 07:14:09 +00:00
|
|
|
effect_type = rvi->engclass;
|
2005-11-04 12:58:18 +00:00
|
|
|
} else {
|
|
|
|
effect_type--;
|
|
|
|
}
|
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
int x = _vehicle_smoke_pos[v->direction] * effect_offset;
|
|
|
|
int y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset;
|
2005-11-04 12:58:18 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(v->flags, VRF_REVERSE_DIRECTION)) {
|
2006-03-18 13:00:32 +00:00
|
|
|
x = -x;
|
|
|
|
y = -y;
|
|
|
|
}
|
|
|
|
|
2005-11-04 12:58:18 +00:00
|
|
|
switch (effect_type) {
|
2008-04-07 12:36:50 +00:00
|
|
|
case 0:
|
|
|
|
/* steam smoke. */
|
|
|
|
if (GB(v->tick_counter, 0, 4) == 0) {
|
|
|
|
CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
|
|
|
|
sound = true;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 12:36:50 +00:00
|
|
|
case 1:
|
|
|
|
/* diesel smoke */
|
|
|
|
if (u->cur_speed <= 40 && Chance16(15, 128)) {
|
|
|
|
CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
|
|
|
|
sound = true;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 12:36:50 +00:00
|
|
|
case 2:
|
|
|
|
/* blue spark */
|
|
|
|
if (GB(v->tick_counter, 0, 2) == 0 && Chance16(1, 45)) {
|
|
|
|
CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
|
|
|
|
sound = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((v = v->Next()) != NULL);
|
2006-09-27 18:17:01 +00:00
|
|
|
|
|
|
|
if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 09:34:56 +00:00
|
|
|
void Train::PlayLeaveStationSound() const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-04 09:26:39 +00:00
|
|
|
static const SoundFx sfx[] = {
|
|
|
|
SND_04_TRAIN,
|
|
|
|
SND_0A_TRAIN_HORN,
|
2007-05-06 19:51:25 +00:00
|
|
|
SND_0A_TRAIN_HORN,
|
|
|
|
SND_47_MAGLEV_2,
|
|
|
|
SND_41_MAGLEV
|
2004-12-04 09:26:39 +00:00
|
|
|
};
|
|
|
|
|
2008-02-13 09:34:56 +00:00
|
|
|
if (PlayVehicleSound(this, VSE_START)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-13 09:34:56 +00:00
|
|
|
EngineID engtype = this->engine_type;
|
|
|
|
SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], this);
|
2007-05-07 15:58:05 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 22:55:23 +00:00
|
|
|
/** Check if the train is on the last reserved tile and try to extend the path then. */
|
2009-05-22 22:22:46 +00:00
|
|
|
static void CheckNextTrainTile(Train *v)
|
2008-08-02 22:55:23 +00:00
|
|
|
{
|
|
|
|
/* Don't do any look-ahead if path_backoff_interval is 255. */
|
|
|
|
if (_settings_game.pf.path_backoff_interval == 255) return;
|
|
|
|
|
2008-09-09 18:47:53 +00:00
|
|
|
/* Exit if we reached our destination depot or are inside a depot. */
|
2009-06-10 16:02:00 +00:00
|
|
|
if ((v->tile == v->dest_tile && v->current_order.IsType(OT_GOTO_DEPOT)) || v->track == TRACK_BIT_DEPOT) return;
|
2008-08-02 22:55:23 +00:00
|
|
|
/* Exit if we are on a station tile and are going to stop. */
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile) && v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile))) return;
|
2008-08-02 22:55:23 +00:00
|
|
|
/* Exit if the current order doesn't have a destination, but the train has orders. */
|
2009-01-12 14:56:31 +00:00
|
|
|
if ((v->current_order.IsType(OT_NOTHING) || v->current_order.IsType(OT_LEAVESTATION) || v->current_order.IsType(OT_LOADING)) && v->GetNumOrders() > 0) return;
|
2008-08-02 22:55:23 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir td = v->GetVehicleTrackdir();
|
2008-08-02 22:55:23 +00:00
|
|
|
|
|
|
|
/* On a tile with a red non-pbs signal, don't look ahead. */
|
|
|
|
if (IsTileType(v->tile, MP_RAILWAY) && HasSignalOnTrackdir(v->tile, td) &&
|
|
|
|
!IsPbsSignal(GetSignalType(v->tile, TrackdirToTrack(td))) &&
|
|
|
|
GetSignalStateByTrackdir(v->tile, td) == SIGNAL_STATE_RED) return;
|
|
|
|
|
|
|
|
CFollowTrackRail ft(v);
|
|
|
|
if (!ft.Follow(v->tile, td)) return;
|
|
|
|
|
|
|
|
if (!HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(ft.m_new_td_bits))) {
|
|
|
|
/* Next tile is not reserved. */
|
|
|
|
if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
|
|
|
|
if (HasPbsSignalOnTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) {
|
|
|
|
/* If the next tile is a PBS signal, try to make a reservation. */
|
|
|
|
TrackBits tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg) {
|
2008-08-02 22:55:23 +00:00
|
|
|
tracks &= ~TrackCrossesTracks(TrackdirToTrack(ft.m_old_td));
|
|
|
|
}
|
|
|
|
ChooseTrainTrack(v, ft.m_new_tile, ft.m_exitdir, tracks, false, NULL, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool CheckTrainStayInDepot(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-04 03:21:14 +00:00
|
|
|
/* bail out if not all wagons are in the same depot or not in a depot at all */
|
2009-05-22 22:22:46 +00:00
|
|
|
for (const Train *u = v; u != NULL; u = u->Next()) {
|
2009-05-22 22:33:05 +00:00
|
|
|
if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* if the train got no power, then keep it in the depot */
|
2010-03-06 12:42:53 +00:00
|
|
|
if (v->acc_cache.cached_power == 0) {
|
2006-08-12 11:08:02 +00:00
|
|
|
v->vehstatus |= VS_STOPPED;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
2006-08-12 11:08:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:55:52 +00:00
|
|
|
SigSegState seg_state;
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->force_proceed == 0) {
|
2008-08-02 22:55:52 +00:00
|
|
|
/* force proceed was not pressed */
|
2010-01-09 14:43:08 +00:00
|
|
|
if (++v->wait_counter < 37) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
2005-01-24 22:24:47 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-08-02 22:55:52 +00:00
|
|
|
seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
|
2009-07-22 08:52:41 +00:00
|
|
|
if (seg_state == SIGSEG_FULL || HasDepotReservation(v->tile)) {
|
2008-08-02 22:55:52 +00:00
|
|
|
/* Full and no PBS signal in block or depot reserved, can't exit. */
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
2005-01-24 22:24:47 +00:00
|
|
|
}
|
2008-08-02 22:55:52 +00:00
|
|
|
} else {
|
|
|
|
seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
|
|
|
|
}
|
|
|
|
|
2009-05-16 20:08:49 +00:00
|
|
|
/* We are leaving a depot, but have to go to the exact same one; re-enter */
|
2009-06-13 10:22:22 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
|
2009-05-16 20:08:49 +00:00
|
|
|
/* We need to have a reservation for this to work. */
|
2009-07-22 08:52:41 +00:00
|
|
|
if (HasDepotReservation(v->tile)) return true;
|
|
|
|
SetDepotReservation(v->tile, true);
|
2009-05-16 20:08:49 +00:00
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:55:52 +00:00
|
|
|
/* Only leave when we can reserve a path to our destination. */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (seg_state == SIGSEG_PBS && !TryPathReserve(v) && v->force_proceed == 0) {
|
2008-08-02 22:55:52 +00:00
|
|
|
/* No path and no force proceed. */
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_TRAINS_LIST);
|
2008-08-02 22:55:52 +00:00
|
|
|
MarkTrainAsStuck(v);
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
|
2009-07-22 08:52:41 +00:00
|
|
|
SetDepotReservation(v->tile, true);
|
2008-08-02 22:55:52 +00:00
|
|
|
if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
|
|
|
|
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_TRAINS_LIST);
|
2008-02-13 09:34:56 +00:00
|
|
|
v->PlayLeaveStationSound();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
v->track = TRACK_BIT_X;
|
|
|
|
if (v->direction & 2) v->track = TRACK_BIT_Y;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
v->cur_speed = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, false);
|
2008-01-15 15:00:01 +00:00
|
|
|
UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
|
2010-01-15 18:23:52 +00:00
|
|
|
v->UpdateAcceleration();
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:53:37 +00:00
|
|
|
/** Clear the reservation of a tile that was just left by a wagon on track_dir. */
|
2009-05-22 22:22:46 +00:00
|
|
|
static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
|
2008-08-02 22:53:37 +00:00
|
|
|
{
|
|
|
|
DiagDirection dir = TrackdirToExitdir(track_dir);
|
|
|
|
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
/* Are we just leaving a tunnel/bridge? */
|
|
|
|
if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(dir)) {
|
|
|
|
TileIndex end = GetOtherTunnelBridgeEnd(tile);
|
|
|
|
|
2010-02-28 18:15:19 +00:00
|
|
|
if (TunnelBridgeIsFree(tile, end, v).Succeeded()) {
|
2008-09-09 19:02:47 +00:00
|
|
|
/* Free the reservation only if no other train is on the tiles. */
|
|
|
|
SetTunnelBridgeReservation(tile, false);
|
|
|
|
SetTunnelBridgeReservation(end, false);
|
2008-08-02 22:53:37 +00:00
|
|
|
|
2008-09-09 19:02:47 +00:00
|
|
|
if (_settings_client.gui.show_track_reservation) {
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
MarkTileDirtyByTile(end);
|
|
|
|
}
|
2008-08-02 22:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 11:47:12 +00:00
|
|
|
} else if (IsRailStationTile(tile)) {
|
2008-08-02 22:53:37 +00:00
|
|
|
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)) {
|
2009-07-24 11:47:12 +00:00
|
|
|
SetRailStationPlatformReservation(tile, ReverseDiagDir(dir), false);
|
2008-08-02 22:53:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Any other tile */
|
|
|
|
UnreserveRailTrack(tile, TrackdirToTrack(track_dir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
/** Free the reserved path in front of a vehicle. */
|
2009-05-22 22:22:46 +00:00
|
|
|
void FreeTrainTrackReservation(const Train *v, TileIndex origin, Trackdir orig_td)
|
2008-08-02 22:53:51 +00:00
|
|
|
{
|
2009-07-01 22:22:01 +00:00
|
|
|
assert(v->IsFrontEngine());
|
2008-08-02 22:53:51 +00:00
|
|
|
|
|
|
|
TileIndex tile = origin != INVALID_TILE ? origin : v->tile;
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir td = orig_td != INVALID_TRACKDIR ? orig_td : v->GetVehicleTrackdir();
|
2009-07-24 11:47:12 +00:00
|
|
|
bool free_tile = tile != v->tile || !(IsRailStationTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE));
|
|
|
|
StationID station_id = IsRailStationTile(v->tile) ? GetStationIndex(v->tile) : INVALID_STATION;
|
2008-08-02 22:53:51 +00:00
|
|
|
|
|
|
|
/* Don't free reservation if it's not ours. */
|
|
|
|
if (TracksOverlap(GetReservedTrackbits(tile) | TrackToTrackBits(TrackdirToTrack(td)))) return;
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
CFollowTrackRail ft(v, GetRailTypeInfo(v->railtype)->compatible_railtypes);
|
2008-08-02 22:53:51 +00:00
|
|
|
while (ft.Follow(tile, td)) {
|
|
|
|
tile = ft.m_new_tile;
|
2009-02-02 01:28:42 +00:00
|
|
|
TrackdirBits bits = ft.m_new_td_bits & TrackBitsToTrackdirBits(GetReservedTrackbits(tile));
|
2008-08-02 22:53:51 +00:00
|
|
|
td = RemoveFirstTrackdir(&bits);
|
|
|
|
assert(bits == TRACKDIR_BIT_NONE);
|
|
|
|
|
|
|
|
if (!IsValidTrackdir(td)) break;
|
|
|
|
|
|
|
|
if (IsTileType(tile, MP_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) == SIGNAL_STATE_RED) {
|
|
|
|
/* Red PBS signal? Can't be our reservation, would be green then. */
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Turn the signal back to red. */
|
|
|
|
SetSignalStateByTrackdir(tile, td, SIGNAL_STATE_RED);
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
} else if (HasSignalOnTrackdir(tile, ReverseTrackdir(td)) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't free first station/bridge/tunnel if we are on it. */
|
2008-09-09 19:02:47 +00:00
|
|
|
if (free_tile || (!(ft.m_is_station && GetStationIndex(ft.m_new_tile) == station_id) && !ft.m_is_tunnel && !ft.m_is_bridge)) ClearPathReservation(v, tile, td);
|
2008-08-02 22:53:51 +00:00
|
|
|
|
|
|
|
free_tile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static const byte _initial_tile_subcoord[6][4][3] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0, 0, 0 }},
|
|
|
|
{{ 0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
|
|
|
|
{{ 0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0, 0, 0 }},
|
|
|
|
{{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
|
|
|
|
{{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0, 0, 0 }},
|
|
|
|
{{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
/**
|
|
|
|
* 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 path_not_found [out] Set to false if the pathfinder couldn't find a way to the destination
|
2009-02-02 01:28:42 +00:00
|
|
|
* @param do_track_reservation Path reservation is requested
|
|
|
|
* @param dest [out] State and destination of the requested path
|
2008-08-02 22:53:51 +00:00
|
|
|
* @return The best track the train should follow
|
|
|
|
*/
|
2009-12-02 09:57:17 +00:00
|
|
|
static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool do_track_reservation, PBSTileInfo *dest)
|
2004-09-10 19:02:27 +00:00
|
|
|
{
|
2009-12-01 22:18:51 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_trains) {
|
2009-12-02 09:57:17 +00:00
|
|
|
case VPF_NPF: return NPFTrainChooseTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
|
|
|
|
case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
|
2008-08-02 22:53:51 +00:00
|
|
|
|
2009-12-02 09:57:17 +00:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-08-02 22:53:51 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 22:55:38 +00:00
|
|
|
/**
|
|
|
|
* Extend a train path as far as possible. Stops on encountering a safe tile,
|
|
|
|
* another reservation or a track choice.
|
|
|
|
* @return INVALID_TILE indicates that the reservation failed.
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, DiagDirection *enterdir)
|
2008-08-02 22:55:38 +00:00
|
|
|
{
|
|
|
|
PBSTileInfo origin = FollowTrainReservation(v);
|
|
|
|
|
|
|
|
CFollowTrackRail ft(v);
|
|
|
|
|
|
|
|
TileIndex tile = origin.tile;
|
|
|
|
Trackdir cur_td = origin.trackdir;
|
|
|
|
while (ft.Follow(tile, cur_td)) {
|
|
|
|
if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
|
|
|
|
/* Possible signal tile. */
|
|
|
|
if (HasOnewaySignalBlockingTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) break;
|
|
|
|
}
|
|
|
|
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg) {
|
2008-08-02 22:55:38 +00:00
|
|
|
ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
|
|
|
|
if (ft.m_new_td_bits == TRACKDIR_BIT_NONE) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Station, depot or waypoint are a possible target. */
|
2009-05-18 01:26:23 +00:00
|
|
|
bool target_seen = ft.m_is_station || (IsTileType(ft.m_new_tile, MP_RAILWAY) && !IsPlainRail(ft.m_new_tile));
|
2008-08-02 22:55:38 +00:00
|
|
|
if (target_seen || KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
|
|
|
|
/* 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 bogous call to TryReserveSafePath which might reserve
|
|
|
|
* a wrong path not leading to our next destination. */
|
|
|
|
if (HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(TrackdirReachesTrackdirs(ft.m_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.m_tiles_skipped != 0) ft.m_new_tile -= TileOffsByDiagDir(ft.m_exitdir) * ft.m_tiles_skipped;
|
|
|
|
|
|
|
|
/* Choice found, path valid but not okay. Save info about the choice tile as well. */
|
|
|
|
if (new_tracks) *new_tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
|
|
|
|
if (enterdir) *enterdir = ft.m_exitdir;
|
|
|
|
return PBSTileInfo(ft.m_new_tile, ft.m_old_td, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
tile = ft.m_new_tile;
|
|
|
|
cur_td = FindFirstTrackdir(ft.m_new_td_bits);
|
|
|
|
|
2009-12-01 22:18:51 +00:00
|
|
|
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);
|
2008-08-02 22:55:38 +00:00
|
|
|
if (!(wp_free && TryReserveRailTrack(tile, TrackdirToTrack(cur_td)))) break;
|
|
|
|
/* Safe position is all good, path valid and okay. */
|
|
|
|
return PBSTileInfo(tile, cur_td, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryReserveRailTrack(tile, TrackdirToTrack(cur_td))) break;
|
|
|
|
}
|
|
|
|
|
2008-09-24 23:25:24 +00:00
|
|
|
if (ft.m_err == CFollowTrackRail::EC_OWNER || ft.m_err == CFollowTrackRail::EC_NO_WAY) {
|
2008-08-02 22:55:38 +00:00
|
|
|
/* End of line, path valid and okay. */
|
|
|
|
return PBSTileInfo(ft.m_old_tile, ft.m_old_td, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sorry, can't reserve path, back out. */
|
|
|
|
tile = origin.tile;
|
|
|
|
cur_td = origin.trackdir;
|
|
|
|
TileIndex stopped = ft.m_old_tile;
|
|
|
|
Trackdir stopped_td = ft.m_old_td;
|
|
|
|
while (tile != stopped || cur_td != stopped_td) {
|
|
|
|
if (!ft.Follow(tile, cur_td)) break;
|
|
|
|
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg) {
|
2008-08-02 22:55:38 +00:00
|
|
|
ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
|
|
|
|
assert(ft.m_new_td_bits != TRACKDIR_BIT_NONE);
|
|
|
|
}
|
|
|
|
assert(KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE);
|
|
|
|
|
|
|
|
tile = ft.m_new_tile;
|
|
|
|
cur_td = FindFirstTrackdir(ft.m_new_td_bits);
|
|
|
|
|
|
|
|
UnreserveRailTrack(tile, TrackdirToTrack(cur_td));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Path invalid. */
|
|
|
|
return PBSTileInfo();
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2009-12-02 09:57:17 +00:00
|
|
|
* @param override_railtype Whether all physically compatible railtypes should be followed.
|
2008-08-02 22:53:51 +00:00
|
|
|
* @return True if a path to a safe stopping tile could be reserved.
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_tailtype)
|
2008-08-02 22:53:51 +00:00
|
|
|
{
|
2009-12-02 09:57:17 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_trains) {
|
|
|
|
case VPF_NPF: return NPFTrainFindNearestSafeTile(v, tile, td, override_tailtype);
|
|
|
|
case VPF_YAPF: return YapfTrainFindNearestSafeTile(v, tile, td, override_tailtype);
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
2008-08-02 22:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-18 13:13:27 +00:00
|
|
|
/** This class will save the current order of a vehicle and restore it on destruction. */
|
|
|
|
class VehicleOrderSaver
|
2008-08-02 22:54:23 +00:00
|
|
|
{
|
2008-10-18 13:13:27 +00:00
|
|
|
private:
|
|
|
|
Vehicle *v;
|
|
|
|
Order old_order;
|
|
|
|
TileIndex old_dest_tile;
|
2008-11-19 14:48:12 +00:00
|
|
|
StationID old_last_station_visited;
|
2008-10-18 13:13:27 +00:00
|
|
|
VehicleOrderID index;
|
2008-08-02 22:54:23 +00:00
|
|
|
|
2008-10-18 13:13:27 +00:00
|
|
|
public:
|
2008-11-19 14:48:12 +00:00
|
|
|
VehicleOrderSaver(Vehicle *_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_order_index)
|
2008-10-18 13:13:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~VehicleOrderSaver()
|
|
|
|
{
|
|
|
|
this->v->current_order = this->old_order;
|
|
|
|
this->v->dest_tile = this->old_dest_tile;
|
2008-11-19 14:48:12 +00:00
|
|
|
this->v->last_station_visited = this->old_last_station_visited;
|
2008-10-18 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current vehicle order to the next order in the order list.
|
2009-01-11 16:33:03 +00:00
|
|
|
* @param skip_first Shall the first (i.e. active) order be skipped?
|
2008-10-18 13:13:27 +00:00
|
|
|
* @return True if a suitable next order could be found.
|
|
|
|
*/
|
2009-01-11 16:33:03 +00:00
|
|
|
bool SwitchToNextOrder(bool skip_first)
|
2008-10-18 13:13:27 +00:00
|
|
|
{
|
2009-01-17 16:25:32 +00:00
|
|
|
if (this->v->GetNumOrders() == 0) return false;
|
|
|
|
|
2009-01-11 16:33:03 +00:00
|
|
|
if (skip_first) ++this->index;
|
2008-10-18 13:13:27 +00:00
|
|
|
|
2009-01-04 14:36:19 +00:00
|
|
|
int conditional_depth = 0;
|
|
|
|
|
2008-10-18 13:13:27 +00:00
|
|
|
do {
|
|
|
|
/* Wrap around. */
|
2009-01-03 13:20:32 +00:00
|
|
|
if (this->index >= this->v->GetNumOrders()) this->index = 0;
|
2008-10-18 13:13:27 +00:00
|
|
|
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = this->v->GetOrder(this->index);
|
2008-10-18 13:13:27 +00:00
|
|
|
assert(order != NULL);
|
|
|
|
|
|
|
|
switch (order->GetType()) {
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
/* Skip service in depot orders when the train doesn't need service. */
|
|
|
|
if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !this->v->NeedsServicing()) break;
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
|
this->v->current_order = *order;
|
|
|
|
UpdateOrderDest(this->v, order);
|
|
|
|
return true;
|
|
|
|
case OT_CONDITIONAL: {
|
2009-01-04 14:36:19 +00:00
|
|
|
if (conditional_depth > this->v->GetNumOrders()) return false;
|
2008-10-18 13:13:27 +00:00
|
|
|
VehicleOrderID next = ProcessConditionalOrder(order, this->v);
|
|
|
|
if (next != INVALID_VEH_ORDER_ID) {
|
2009-01-04 14:36:19 +00:00
|
|
|
conditional_depth++;
|
2008-10-18 13:13:27 +00:00
|
|
|
this->index = next;
|
2008-10-19 17:16:26 +00:00
|
|
|
/* Don't increment next, so no break here. */
|
2008-10-18 13:13:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2008-08-02 22:54:23 +00:00
|
|
|
}
|
2008-10-18 13:13:27 +00:00
|
|
|
default:
|
|
|
|
break;
|
2008-08-02 22:54:23 +00:00
|
|
|
}
|
2008-10-19 17:16:26 +00:00
|
|
|
/* Don't increment inside the while because otherwise conditional
|
|
|
|
* orders can lead to an infinite loop. */
|
|
|
|
++this->index;
|
|
|
|
} while (this->index != this->v->cur_order_index);
|
2008-08-02 22:54:23 +00:00
|
|
|
|
2008-10-18 13:13:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2008-08-02 22:54:23 +00:00
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
/* choose a track */
|
2009-05-22 22:22:46 +00:00
|
|
|
static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck)
|
2008-08-02 22:53:51 +00:00
|
|
|
{
|
|
|
|
Track best_track = INVALID_TRACK;
|
|
|
|
bool do_track_reservation = _settings_game.pf.reserve_paths || force_res;
|
|
|
|
bool changed_signal = false;
|
|
|
|
|
|
|
|
assert((tracks & ~TRACK_BIT_MASK) == 0);
|
|
|
|
|
|
|
|
if (got_reservation != NULL) *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 = (TrackBits)(GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir));
|
|
|
|
/* Do we have a suitable reserved track? */
|
|
|
|
if (res_tracks != TRACK_BIT_NONE) return FindFirstTrack(res_tracks);
|
|
|
|
|
|
|
|
/* Quick return in case only one possible track is available */
|
|
|
|
if (KillFirstBit(tracks) == TRACK_BIT_NONE) {
|
|
|
|
Track track = FindFirstTrack(tracks);
|
|
|
|
/* We need to check for signals only here, as a junction tile can't have signals. */
|
|
|
|
if (track != INVALID_TRACK && HasPbsSignalOnTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir))) {
|
|
|
|
do_track_reservation = true;
|
|
|
|
changed_signal = true;
|
|
|
|
SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir), SIGNAL_STATE_GREEN);
|
|
|
|
} else if (!do_track_reservation) {
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
best_track = track;
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:55:38 +00:00
|
|
|
PBSTileInfo res_dest(tile, INVALID_TRACKDIR, false);
|
|
|
|
DiagDirection dest_enterdir = enterdir;
|
2008-08-02 22:53:51 +00:00
|
|
|
if (do_track_reservation) {
|
2008-08-02 22:56:50 +00:00
|
|
|
/* 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(v);
|
|
|
|
if (v->current_order.IsType(OT_DUMMY) || v->current_order.IsType(OT_CONDITIONAL) || v->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(v);
|
2008-08-02 22:55:38 +00:00
|
|
|
|
|
|
|
res_dest = ExtendTrainReservation(v, &tracks, &dest_enterdir);
|
|
|
|
if (res_dest.tile == INVALID_TILE) {
|
|
|
|
/* Reservation failed? */
|
|
|
|
if (mark_stuck) MarkTrainAsStuck(v);
|
|
|
|
if (changed_signal) SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(best_track, enterdir), SIGNAL_STATE_RED);
|
|
|
|
return FindFirstTrack(tracks);
|
|
|
|
}
|
2008-08-02 22:53:51 +00:00
|
|
|
}
|
|
|
|
|
2008-10-18 13:13:31 +00:00
|
|
|
/* Save the current train order. The destructor will restore the old order on function exit. */
|
|
|
|
VehicleOrderSaver orders(v);
|
|
|
|
|
|
|
|
/* If the current tile is the destination of the current order and
|
2008-10-31 16:46:34 +00:00
|
|
|
* 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. */
|
2009-01-11 16:33:03 +00:00
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION)) {
|
|
|
|
orders.SwitchToNextOrder(false);
|
|
|
|
} else if (v->current_order.IsType(OT_LOADING) || (!v->current_order.IsType(OT_GOTO_DEPOT) && (
|
2008-12-20 00:16:23 +00:00
|
|
|
v->current_order.IsType(OT_GOTO_STATION) ?
|
2009-07-24 11:47:12 +00:00
|
|
|
IsRailStationTile(v->tile) && v->current_order.GetDestination() == GetStationIndex(v->tile) :
|
2009-01-11 16:33:03 +00:00
|
|
|
v->tile == v->dest_tile))) {
|
|
|
|
orders.SwitchToNextOrder(true);
|
2008-10-18 13:13:31 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 22:55:38 +00:00
|
|
|
if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
|
|
|
|
/* Pathfinders are able to tell that route was only 'guessed'. */
|
|
|
|
bool path_not_found = false;
|
|
|
|
TileIndex new_tile = res_dest.tile;
|
|
|
|
|
|
|
|
Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, &path_not_found, do_track_reservation, &res_dest);
|
|
|
|
if (new_tile == tile) best_track = next_track;
|
|
|
|
|
|
|
|
/* handle "path not found" state */
|
|
|
|
if (path_not_found) {
|
|
|
|
/* PF didn't find the route */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (!HasBit(v->flags, VRF_NO_PATH_TO_DESTINATION)) {
|
2008-08-02 22:55:38 +00:00
|
|
|
/* it is first time the problem occurred, set the "path not found" flag */
|
2009-05-22 22:33:05 +00:00
|
|
|
SetBit(v->flags, VRF_NO_PATH_TO_DESTINATION);
|
2008-08-02 22:55:38 +00:00
|
|
|
/* and notify user about the event */
|
2009-01-25 13:20:13 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventVehicleLost(v->index));
|
2008-09-30 20:39:50 +00:00
|
|
|
if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
|
2009-01-04 17:42:46 +00:00
|
|
|
SetDParam(0, v->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-08-04 18:04:33 +00:00
|
|
|
STR_NEWS_TRAIN_IS_LOST,
|
2008-08-02 22:55:38 +00:00
|
|
|
NS_ADVICE,
|
2009-05-24 16:52:42 +00:00
|
|
|
v->index
|
|
|
|
);
|
2008-08-02 22:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* route found, is the train marked with "path not found" flag? */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(v->flags, VRF_NO_PATH_TO_DESTINATION)) {
|
2008-08-02 22:55:38 +00:00
|
|
|
/* clear the flag as the PF's problem was solved */
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(v->flags, VRF_NO_PATH_TO_DESTINATION);
|
2008-08-02 22:55:38 +00:00
|
|
|
/* can we also delete the "News" item somehow? */
|
2006-10-17 16:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
/* 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(v);
|
|
|
|
FreeTrainTrackReservation(v);
|
|
|
|
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(v);
|
|
|
|
if (TryReserveSafeTrack(v, origin.tile, origin.trackdir, false)) {
|
|
|
|
TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
|
|
|
|
best_track = FindFirstTrack(res);
|
2009-05-22 18:17:20 +00:00
|
|
|
TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
|
2008-08-02 22:53:51 +00:00
|
|
|
if (got_reservation != NULL) *got_reservation = true;
|
|
|
|
if (changed_signal) MarkTileDirtyByTile(tile);
|
|
|
|
} else {
|
|
|
|
FreeTrainTrackReservation(v);
|
|
|
|
if (mark_stuck) MarkTrainAsStuck(v);
|
|
|
|
}
|
|
|
|
return best_track;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got_reservation != NULL) *got_reservation = true;
|
|
|
|
|
2008-08-02 22:54:23 +00:00
|
|
|
/* Reservation target found and free, check if it is safe. */
|
|
|
|
while (!IsSafeWaitingPosition(v, 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((TrackdirBits)(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0))) & DiagdirReachesTracks(exitdir);
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg) {
|
2008-08-02 22:54:23 +00:00
|
|
|
reachable &= ~TrackCrossesTracks(TrackdirToTrack(res_dest.trackdir));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next order with destination. */
|
2009-01-11 16:33:03 +00:00
|
|
|
if (orders.SwitchToNextOrder(true)) {
|
2008-08-02 22:54:23 +00:00
|
|
|
PBSTileInfo cur_dest;
|
|
|
|
DoTrainPathfind(v, next_tile, exitdir, reachable, NULL, true, &cur_dest);
|
|
|
|
if (cur_dest.tile != INVALID_TILE) {
|
|
|
|
res_dest = cur_dest;
|
|
|
|
if (res_dest.okay) continue;
|
|
|
|
/* Path found, but could not be reserved. */
|
|
|
|
FreeTrainTrackReservation(v);
|
|
|
|
if (mark_stuck) MarkTrainAsStuck(v);
|
|
|
|
if (got_reservation != NULL) *got_reservation = false;
|
|
|
|
changed_signal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No order or no safe position found, try any position. */
|
|
|
|
if (!TryReserveSafeTrack(v, res_dest.tile, res_dest.trackdir, true)) {
|
|
|
|
FreeTrainTrackReservation(v);
|
|
|
|
if (mark_stuck) MarkTrainAsStuck(v);
|
|
|
|
if (got_reservation != NULL) *got_reservation = false;
|
|
|
|
changed_signal = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
|
2008-08-02 22:53:51 +00:00
|
|
|
|
|
|
|
if (changed_signal) MarkTileDirtyByTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return best_track;
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:54:38 +00:00
|
|
|
/**
|
|
|
|
* Try to reserve a path to a safe position.
|
|
|
|
*
|
|
|
|
* @param v The vehicle
|
2009-02-02 01:28:42 +00:00
|
|
|
* @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.
|
2008-08-02 22:54:38 +00:00
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
bool TryPathReserve(Train *v, bool mark_as_stuck, bool first_tile_okay)
|
2008-08-02 22:54:38 +00:00
|
|
|
{
|
2009-07-01 22:22:01 +00:00
|
|
|
assert(v->IsFrontEngine());
|
2008-08-02 22:54:38 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2009-06-10 16:02:00 +00:00
|
|
|
if (v->track == TRACK_BIT_DEPOT) {
|
2009-07-22 08:52:41 +00:00
|
|
|
if (HasDepotReservation(v->tile)) {
|
2008-08-02 22:54:38 +00:00
|
|
|
if (mark_as_stuck) MarkTrainAsStuck(v);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
/* Depot not reserved, but the next tile might be. */
|
|
|
|
TileIndex next_tile = TileAddByDiagDir(v->tile, GetRailDepotDirection(v->tile));
|
|
|
|
if (HasReservedTracks(next_tile, DiagdirReachesTracks(GetRailDepotDirection(v->tile)))) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-27 14:37:50 +00:00
|
|
|
Vehicle *other_train = NULL;
|
2008-08-03 14:16:57 +00:00
|
|
|
PBSTileInfo origin = FollowTrainReservation(v, &other_train);
|
2009-08-10 23:52:40 +00:00
|
|
|
/* The path we are driving on is alread 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. */
|
2009-12-27 14:37:50 +00:00
|
|
|
if (other_train != NULL && other_train->index != v->index) {
|
2009-08-10 23:52:40 +00:00
|
|
|
if (mark_as_stuck) MarkTrainAsStuck(v);
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-02 22:54:38 +00:00
|
|
|
/* If we have a reserved path and the path ends at a safe tile, we are finished already. */
|
|
|
|
if (origin.okay && (v->tile != origin.tile || first_tile_okay)) {
|
|
|
|
/* Can't be stuck then. */
|
2009-09-13 19:15:59 +00:00
|
|
|
if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(v->flags, VRF_TRAIN_STUCK);
|
2008-08-02 22:54:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are in a depot, tentativly reserve the depot. */
|
2009-06-10 16:02:00 +00:00
|
|
|
if (v->track == TRACK_BIT_DEPOT) {
|
2009-07-22 08:52:41 +00:00
|
|
|
SetDepotReservation(v->tile, true);
|
2008-08-02 22:54:38 +00:00
|
|
|
if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagDirection exitdir = TrackdirToExitdir(origin.trackdir);
|
|
|
|
TileIndex new_tile = TileAddByDiagDir(origin.tile, exitdir);
|
2009-02-02 01:28:42 +00:00
|
|
|
TrackBits reachable = TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(GetTileTrackStatus(new_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTrackdirs(exitdir));
|
2008-08-02 22:54:38 +00:00
|
|
|
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg) reachable &= ~TrackCrossesTracks(TrackdirToTrack(origin.trackdir));
|
2008-08-02 22:54:38 +00:00
|
|
|
|
|
|
|
bool res_made = false;
|
|
|
|
ChooseTrainTrack(v, new_tile, exitdir, reachable, true, &res_made, mark_as_stuck);
|
|
|
|
|
|
|
|
if (!res_made) {
|
|
|
|
/* Free the depot reservation as well. */
|
2009-07-22 08:52:41 +00:00
|
|
|
if (v->track == TRACK_BIT_DEPOT) SetDepotReservation(v->tile, false);
|
2008-08-02 22:54:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2009-01-05 20:28:56 +00:00
|
|
|
}
|
2009-05-22 22:33:05 +00:00
|
|
|
ClrBit(v->flags, VRF_TRAIN_STUCK);
|
2008-08-02 22:54:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-02 09:57:17 +00:00
|
|
|
static bool CheckReverseTrain(const Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.difficulty.line_reverse_mode != 0 ||
|
2009-05-22 22:33:05 +00:00
|
|
|
v->track == TRACK_BIT_DEPOT || v->track == TRACK_BIT_WORMHOLE ||
|
2008-04-07 12:36:50 +00:00
|
|
|
!(v->direction & 1)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2008-04-07 12:36:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-02 09:57:17 +00:00
|
|
|
assert(v->track != TRACK_BIT_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_trains) {
|
2009-12-02 09:57:17 +00:00
|
|
|
case VPF_NPF: return NPFTrainCheckReverse(v);
|
|
|
|
case VPF_YAPF: return YapfTrainCheckReverse(v);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2009-12-02 09:57:17 +00:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
TileIndex Train::GetOrderStationLocation(StationID station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-05 12:01:34 +00:00
|
|
|
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Station *st = Station::Get(station);
|
2008-04-14 07:08:43 +00:00
|
|
|
if (!(st->facilities & FACIL_TRAIN)) {
|
|
|
|
/* The destination station has no trainstation tiles. */
|
2009-05-09 13:37:18 +00:00
|
|
|
this->IncrementOrderIndex();
|
2008-04-14 07:08:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return st->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 22:33:51 +00:00
|
|
|
void Train::MarkDirty()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-29 22:33:51 +00:00
|
|
|
Vehicle *v = this;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2009-08-08 18:45:12 +00:00
|
|
|
v->UpdateViewport(false, false);
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((v = v->Next()) != NULL);
|
2007-04-29 22:33:51 +00:00
|
|
|
|
|
|
|
/* need to update acceleration and cached values since the goods on the train changed. */
|
2010-01-15 18:23:52 +00:00
|
|
|
this->CargoChanged();
|
|
|
|
this->UpdateAcceleration();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-03 12:55:39 +00:00
|
|
|
/**
|
|
|
|
* This function looks at the vehicle and updates it's speed (cur_speed
|
|
|
|
* and subspeed) variables. Furthermore, it returns the distance that
|
|
|
|
* the train can drive this tick. This distance is expressed as 256 * n,
|
|
|
|
* where n is the number of straight (long) tracks the train can
|
|
|
|
* traverse. This means that moving along a straight track costs 256
|
|
|
|
* "speed" and a diagonal track costs 192 "speed".
|
|
|
|
* @return distance to drive.
|
|
|
|
*/
|
2010-01-16 22:16:28 +00:00
|
|
|
int Train::UpdateSpeed()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint accel;
|
|
|
|
|
2010-01-16 22:30:04 +00:00
|
|
|
switch (_settings_game.vehicle.train_acceleration_model) {
|
|
|
|
default: NOT_REACHED();
|
2010-01-30 16:27:35 +00:00
|
|
|
case AM_ORIGINAL:
|
|
|
|
accel = this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2);
|
|
|
|
break;
|
|
|
|
case AM_REALISTIC:
|
2010-01-16 22:30:04 +00:00
|
|
|
this->max_speed = this->GetCurrentMaxSpeed();
|
|
|
|
accel = this->GetAcceleration();
|
|
|
|
break;
|
2010-01-17 00:51:18 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-16 22:16:28 +00:00
|
|
|
uint spd = this->subspeed + accel;
|
|
|
|
this->subspeed = (byte)spd;
|
2005-01-26 12:51:04 +00:00
|
|
|
{
|
2010-01-16 22:16:28 +00:00
|
|
|
int tempmax = this->max_speed;
|
|
|
|
if (this->cur_speed > this->max_speed)
|
|
|
|
tempmax = this->cur_speed - (this->cur_speed / 10) - 1;
|
|
|
|
this->cur_speed = spd = Clamp(this->cur_speed + ((int)spd >> 8), 0, tempmax);
|
2005-01-26 12:51:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-10-03 12:55:39 +00:00
|
|
|
/* Scale speed by 3/4. Previously this was only done when the train was
|
|
|
|
* facing diagonally and would apply to however many moves the train made
|
|
|
|
* regardless the of direction actually moved in. Now it is always scaled,
|
|
|
|
* 256 spd is used to go straight and 192 is used to go diagonally
|
|
|
|
* (3/4 of 256). This results in the same effect, but without the error the
|
|
|
|
* previous method caused.
|
|
|
|
*
|
|
|
|
* The scaling is done in this direction and not by multiplying the amount
|
|
|
|
* to be subtracted by 4/3 so that the leftover speed can be saved in a
|
2010-01-16 22:16:28 +00:00
|
|
|
* byte in this->progress.
|
2008-10-03 12:55:39 +00:00
|
|
|
*/
|
|
|
|
int scaled_spd = spd * 3 >> 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-16 22:16:28 +00:00
|
|
|
scaled_spd += this->progress;
|
|
|
|
this->progress = 0; // set later in TrainLocoHandler or TrainController
|
2008-10-03 12:55:39 +00:00
|
|
|
return scaled_spd;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void TrainEnterStation(Train *v, StationID station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->last_station_visited = station;
|
|
|
|
|
|
|
|
/* check if a train ever visited this station before */
|
2009-05-16 23:34:14 +00:00
|
|
|
Station *st = Station::Get(station);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
|
|
|
|
st->had_vehicle_of_type |= HVOT_TRAIN;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-04-21 23:40:56 +00:00
|
|
|
STR_NEWS_FIRST_TRAIN_ARRIVAL,
|
2008-09-30 20:39:50 +00:00
|
|
|
v->owner == _local_company ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
2006-02-13 21:15:00 +00:00
|
|
|
);
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 18:21:24 +00:00
|
|
|
v->BeginLoading();
|
2008-04-19 23:19:12 +00:00
|
|
|
|
|
|
|
StationAnimationTrigger(st, v->tile, STAT_ANIM_TRAIN_ARRIVES);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the vehicle is compatible with the specified tile */
|
2009-05-22 22:22:46 +00:00
|
|
|
static inline bool CheckCompatibleRail(const Train *v, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-17 09:41:46 +00:00
|
|
|
return
|
2005-10-23 13:04:44 +00:00
|
|
|
IsTileOwner(tile, v->owner) && (
|
2009-07-01 22:22:01 +00:00
|
|
|
!v->IsFrontEngine() ||
|
2009-05-22 22:33:05 +00:00
|
|
|
HasBit(v->compatible_railtypes, GetRailType(tile))
|
2005-10-23 13:04:44 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct RailtypeSlowdownParams {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte small_turn, large_turn;
|
|
|
|
byte z_up; // fraction to remove when moving up
|
|
|
|
byte z_down; // fraction to remove when moving down
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
static const RailtypeSlowdownParams _railtype_slowdown[] = {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* normal accel */
|
2007-04-04 03:21:14 +00:00
|
|
|
{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
|
|
|
|
{256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified
|
|
|
|
{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
|
|
|
|
{0, 256 / 2, 256 / 4, 2}, ///< maglev
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/** Modify the speed of the vehicle due to a change in altitude */
|
2009-05-22 22:22:46 +00:00
|
|
|
static inline void AffectSpeedByZChange(Train *v, byte old_z)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-01-30 16:27:35 +00:00
|
|
|
if (old_z == v->z_pos || _settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (old_z < v->z_pos) {
|
|
|
|
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
|
|
|
|
} else {
|
|
|
|
uint16 spd = v->cur_speed + rsp->z_down;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (spd <= v->max_speed) v->cur_speed = spd;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:56:35 +00:00
|
|
|
static bool TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-19 12:06:12 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) &&
|
2006-05-09 08:17:33 +00:00
|
|
|
GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
|
2008-04-01 14:47:57 +00:00
|
|
|
TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir);
|
|
|
|
Trackdir trackdir = FindFirstTrackdir(tracks);
|
2008-08-02 22:56:35 +00:00
|
|
|
if (UpdateSignalsOnSegment(tile, TrackdirToExitdir(trackdir), GetTileOwner(tile)) == SIGSEG_PBS && HasSignalOnTrackdir(tile, trackdir)) {
|
|
|
|
/* A PBS block with a non-PBS signal facing us? */
|
|
|
|
if (!IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-08-02 22:56:35 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 13:54:06 +00:00
|
|
|
/**
|
|
|
|
* Tries to reserve track under whole train consist
|
|
|
|
*/
|
|
|
|
void Train::ReserveTrackUnderConsist() const
|
|
|
|
{
|
|
|
|
for (const Train *u = this; u != NULL; u = u->Next()) {
|
|
|
|
switch (u->track) {
|
|
|
|
case TRACK_BIT_WORMHOLE:
|
|
|
|
TryReserveRailTrack(u->tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(u->tile)));
|
|
|
|
break;
|
|
|
|
case TRACK_BIT_DEPOT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TryReserveRailTrack(u->tile, TrackBitsToTrack(u->track));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
uint Train::Crash(bool flooded)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-04 20:29:46 +00:00
|
|
|
uint pass = 0;
|
|
|
|
if (this->IsFrontEngine()) {
|
|
|
|
pass += 4; // driver
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-10 23:52:44 +00:00
|
|
|
/* Remove the reserved path in front of the train if it is not stuck.
|
|
|
|
* Also clear all reserved tracks the train is currently on. */
|
2009-12-04 20:29:46 +00:00
|
|
|
if (!HasBit(this->flags, VRF_TRAIN_STUCK)) FreeTrainTrackReservation(this);
|
|
|
|
for (const Train *v = this; v != NULL; v = v->Next()) {
|
|
|
|
ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
|
|
|
|
if (IsTileType(v->tile, MP_TUNNELBRIDGE)) {
|
2008-08-04 13:15:15 +00:00
|
|
|
/* ClearPathReservation will not free the wormhole exit
|
|
|
|
* if the train has just entered the wormhole. */
|
2009-12-04 20:29:46 +00:00
|
|
|
SetTunnelBridgeReservation(GetOtherTunnelBridgeEnd(v->tile), false);
|
2008-08-04 13:15:15 +00:00
|
|
|
}
|
2008-08-02 22:54:07 +00:00
|
|
|
}
|
2008-01-13 14:42:28 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
/* we may need to update crossing we were approaching,
|
|
|
|
* but must be updated after the train has been marked crashed */
|
|
|
|
TileIndex crossing = TrainApproachingCrossingTile(this);
|
|
|
|
if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
|
2010-01-28 22:06:13 +00:00
|
|
|
|
|
|
|
/* Remove the loading indicators (if any) */
|
|
|
|
HideFillingPercent(&this->fill_percent_te_id);
|
2008-05-21 12:06:05 +00:00
|
|
|
}
|
2008-01-17 17:57:39 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
pass += Vehicle::Crash(flooded);
|
2008-05-21 12:06:05 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
this->crash_anim_pos = flooded ? 4000 : 1; // max 4440, disappear pretty fast when flooded
|
|
|
|
return pass;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-16 17:16:58 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static uint TrainCrashed(Train *v)
|
2009-02-16 17:16:58 +00:00
|
|
|
{
|
2009-07-17 13:54:06 +00:00
|
|
|
uint num = 0;
|
2009-06-24 23:59:20 +00:00
|
|
|
|
2009-02-16 17:16:58 +00:00
|
|
|
/* do not crash train twice */
|
2009-07-17 13:54:06 +00:00
|
|
|
if (!(v->vehstatus & VS_CRASHED)) {
|
2009-12-04 20:29:46 +00:00
|
|
|
num = v->Crash();
|
2009-07-17 13:54:06 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventVehicleCrashed(v->index, v->tile, AIEventVehicleCrashed::CRASH_TRAIN));
|
|
|
|
}
|
2009-02-16 17:16:58 +00:00
|
|
|
|
2009-07-17 13:54:06 +00:00
|
|
|
/* Try to re-reserve track under already crashed train too.
|
|
|
|
* SetVehicleCrashed() clears the reservation! */
|
|
|
|
v->ReserveTrackUnderConsist();
|
2009-02-16 17:16:58 +00:00
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2007-06-19 16:40:59 +00:00
|
|
|
struct TrainCollideChecker {
|
2009-06-24 23:59:20 +00:00
|
|
|
Train *v; ///< vehicle we are testing for collision
|
|
|
|
uint num; ///< number of victims if train collided
|
2007-06-19 16:40:59 +00:00
|
|
|
};
|
|
|
|
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *FindTrainCollideEnum(Vehicle *v, void *data)
|
2007-06-19 16:40:59 +00:00
|
|
|
{
|
2008-04-07 12:36:50 +00:00
|
|
|
TrainCollideChecker *tcc = (TrainCollideChecker*)data;
|
2007-06-19 16:40:59 +00:00
|
|
|
|
2009-06-24 23:59:20 +00:00
|
|
|
/* not a train or in depot */
|
|
|
|
if (v->type != VEH_TRAIN || Train::From(v)->track == TRACK_BIT_DEPOT) return NULL;
|
2008-01-13 23:53:33 +00:00
|
|
|
|
|
|
|
/* get first vehicle now to make most usual checks faster */
|
2009-06-24 23:59:20 +00:00
|
|
|
Train *coll = Train::From(v)->First();
|
2007-06-19 16:40:59 +00:00
|
|
|
|
2009-06-24 23:59:20 +00:00
|
|
|
/* can't collide with own wagons */
|
|
|
|
if (coll == tcc->v) return NULL;
|
|
|
|
|
|
|
|
int x_diff = v->x_pos - tcc->v->x_pos;
|
|
|
|
int y_diff = v->y_pos - tcc->v->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 NULL;
|
|
|
|
|
|
|
|
/* Slower check using multiplication */
|
|
|
|
if (x_diff * x_diff + y_diff * y_diff > 25) return NULL;
|
|
|
|
|
|
|
|
/* Happens when there is a train under bridge next to bridge head */
|
|
|
|
if (abs(v->z_pos - tcc->v->z_pos) > 5) return NULL;
|
|
|
|
|
|
|
|
/* crash both trains */
|
|
|
|
tcc->num += TrainCrashed(tcc->v);
|
|
|
|
tcc->num += TrainCrashed(coll);
|
|
|
|
|
|
|
|
return NULL; // continue searching
|
2007-06-19 16:40:59 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/**
|
2005-03-06 12:54:19 +00:00
|
|
|
* Checks whether the specified train has a collision with another vehicle. If
|
2005-11-18 23:41:03 +00:00
|
|
|
* so, destroys this vehicle, and the other vehicle if its subtype has TS_Front.
|
2004-08-11 22:07:08 +00:00
|
|
|
* Reports the incident in a flashy news item, modifies station ratings and
|
|
|
|
* plays a sound.
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool CheckTrainCollision(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* can't collide in depot */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->track == TRACK_BIT_DEPOT) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
assert(v->track == TRACK_BIT_WORMHOLE || TileVirtXY(v->x_pos, v->y_pos) == v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
TrainCollideChecker tcc;
|
2004-08-09 17:04:08 +00:00
|
|
|
tcc.v = v;
|
2007-06-19 16:40:59 +00:00
|
|
|
tcc.num = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-19 16:40:59 +00:00
|
|
|
/* find colliding vehicles */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->track == TRACK_BIT_WORMHOLE) {
|
2008-09-07 11:23:10 +00:00
|
|
|
FindVehicleOnPos(v->tile, &tcc, FindTrainCollideEnum);
|
|
|
|
FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
|
2007-07-30 08:49:41 +00:00
|
|
|
} else {
|
2008-09-07 11:23:10 +00:00
|
|
|
FindVehicleOnPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
|
2007-07-30 08:49:41 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-19 16:40:59 +00:00
|
|
|
/* any dead -> no crash */
|
2008-10-22 23:06:36 +00:00
|
|
|
if (tcc.num == 0) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-19 16:40:59 +00:00
|
|
|
SetDParam(0, tcc.num);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(STR_NEWS_TRAIN_CRASH,
|
|
|
|
NS_ACCIDENT,
|
|
|
|
v->index
|
2005-03-06 12:54:19 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ModifyStationRatingAround(v->tile, v->owner, -160, 30);
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_13_BIG_CRASH, v);
|
2008-10-22 23:06:36 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
static Vehicle *CheckTrainAtSignal(Vehicle *v, void *data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
|
|
|
|
|
|
|
|
Train *t = Train::From(v);
|
2008-07-23 18:23:12 +00:00
|
|
|
DiagDirection exitdir = *(DiagDirection *)data;
|
2005-03-06 12:41:18 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
/* not front engine of a train, inside wormhole or depot, crashed */
|
|
|
|
if (!t->IsFrontEngine() || !(t->track & TRACK_BIT_MASK)) return NULL;
|
2008-07-23 18:23:12 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (t->cur_speed > 5 || TrainExitDir(t->direction, t->track) != exitdir) return NULL;
|
|
|
|
|
|
|
|
return t;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void TrainController(Train *v, Vehicle *nomove)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-06-01 13:14:56 +00:00
|
|
|
Train *first = v->First();
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *prev;
|
2009-06-01 13:14:56 +00:00
|
|
|
bool direction_changed = false; // has direction of any part changed?
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/* For every vehicle after and including the given vehicle */
|
2008-03-19 20:42:05 +00:00
|
|
|
for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) {
|
2007-06-12 11:22:32 +00:00
|
|
|
DiagDirection enterdir = DIAGDIR_BEGIN;
|
2008-01-17 17:57:39 +00:00
|
|
|
bool update_signals_crossing = false; // will we update signals or crossing state?
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-25 10:49:13 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->track != TRACK_BIT_WORMHOLE) {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Not inside tunnel */
|
2007-02-25 10:25:25 +00:00
|
|
|
if (gp.old_tile == gp.new_tile) {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Staying in the old tile */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->track == TRACK_BIT_DEPOT) {
|
2007-02-13 11:29:20 +00:00
|
|
|
/* Inside depot */
|
2004-08-09 17:04:08 +00:00
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
} else {
|
2007-02-13 11:29:20 +00:00
|
|
|
/* Not inside depot */
|
2004-12-21 16:00:14 +00:00
|
|
|
|
2009-01-10 15:27:57 +00:00
|
|
|
/* Reverse when we are at the end of the track already, do not move to the new position */
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->IsFrontEngine() && !TrainCheckIfLineEnds(v)) return;
|
2004-12-21 16:00:14 +00:00
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
goto invalid_rail;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_ENTERED_STATION)) {
|
2009-01-10 15:27:57 +00:00
|
|
|
/* The new position is the end of the platform */
|
2007-02-13 10:26:53 +00:00
|
|
|
TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* A new tile is about to be entered. */
|
|
|
|
|
|
|
|
/* Determine what direction we're entering the new tile from */
|
2009-03-21 21:43:23 +00:00
|
|
|
enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
|
2007-02-13 11:29:20 +00:00
|
|
|
assert(IsValidDiagDirection(enterdir));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Get the status of the tracks in the new tile and mask
|
|
|
|
* away the bits that aren't reachable. */
|
2008-04-01 14:47:57 +00:00
|
|
|
TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir));
|
|
|
|
TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);
|
|
|
|
|
|
|
|
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
|
|
|
|
TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg && prev == NULL) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* We allow wagons to make 90 deg turns, because forbid_90_deg
|
|
|
|
* can be switched on halfway a turn */
|
2009-05-22 22:33:05 +00:00
|
|
|
bits &= ~TrackCrossesTracks(FindFirstTrack(v->track));
|
2006-02-13 21:15:00 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2007-02-13 11:29:20 +00:00
|
|
|
if (bits == TRACK_BIT_NONE) goto invalid_rail;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Check if the new tile contrains tracks that are compatible
|
|
|
|
* with the current train, if not, bail out. */
|
2007-02-13 11:29:20 +00:00
|
|
|
if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
TrackBits chosen_track;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (prev == NULL) {
|
|
|
|
/* Currently the locomotive is active. Determine which one of the
|
|
|
|
* available tracks to choose */
|
2008-08-02 22:53:51 +00:00
|
|
|
chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits, false, NULL, true));
|
|
|
|
assert(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-16 16:23:44 +00:00
|
|
|
if (v->force_proceed != 0 && 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 (GetSignalType(gp.new_tile, TrackdirToTrack(dir)) != SIGTYPE_PBS ||
|
|
|
|
!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(dir))) {
|
|
|
|
/* However, we do not want to be stopped by PBS signals
|
|
|
|
* entered via the back. */
|
|
|
|
v->force_proceed--;
|
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Check if it's a red signal and that force proceed is not clicked. */
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((red_signals & chosen_track) && v->force_proceed == 0) {
|
2008-02-20 17:49:50 +00:00
|
|
|
/* In front of a red signal */
|
|
|
|
Trackdir i = FindFirstTrackdir(trackdirbits);
|
2007-02-25 09:47:46 +00:00
|
|
|
|
2008-08-02 22:53:21 +00:00
|
|
|
/* Don't handle stuck trains here. */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(v->flags, VRF_TRAIN_STUCK)) return;
|
2008-08-02 22:53:21 +00:00
|
|
|
|
2007-02-25 09:47:46 +00:00
|
|
|
if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 255 - 100;
|
2010-01-09 14:43:08 +00:00
|
|
|
if (_settings_game.pf.wait_oneway_signal == 255 || ++v->wait_counter < _settings_game.pf.wait_oneway_signal * 20) return;
|
2007-02-25 09:47:46 +00:00
|
|
|
} else if (HasSignalOnTrackdir(gp.new_tile, i)) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
2007-04-18 22:10:36 +00:00
|
|
|
v->progress = 255 - 10;
|
2010-01-09 14:43:08 +00:00
|
|
|
if (_settings_game.pf.wait_twoway_signal == 255 || ++v->wait_counter < _settings_game.pf.wait_twoway_signal * 73) {
|
2008-07-23 18:23:12 +00:00
|
|
|
DiagDirection exitdir = TrackdirToExitdir(i);
|
|
|
|
TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);
|
|
|
|
|
|
|
|
exitdir = ReverseDiagDir(exitdir);
|
2007-02-25 09:47:46 +00:00
|
|
|
|
|
|
|
/* check if a train is waiting on the other side */
|
2009-07-01 22:22:01 +00:00
|
|
|
if (!HasVehicleOnPos(o_tile, &exitdir, &CheckTrainAtSignal)) return;
|
2007-02-25 09:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-02 22:56:21 +00:00
|
|
|
|
|
|
|
/* If we would reverse but are currently in a PBS block and
|
2010-02-28 08:18:20 +00:00
|
|
|
* 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.wait_for_pbs_path == 255 && !HasOnewaySignalBlockingTrackdir(gp.new_tile, i) &&
|
|
|
|
UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SIGSEG_PBS) {
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2008-08-02 22:56:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-02-25 09:47:46 +00:00
|
|
|
goto reverse_train_direction;
|
2008-08-10 13:37:09 +00:00
|
|
|
} else {
|
|
|
|
TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track));
|
2007-02-25 09:47:46 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
/* The wagon is active, simply follow the prev vehicle. */
|
2009-03-21 22:25:38 +00:00
|
|
|
if (prev->tile == gp.new_tile) {
|
|
|
|
/* Choose the same track as prev */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (prev->track == TRACK_BIT_WORMHOLE) {
|
2009-03-21 22:25:38 +00:00
|
|
|
/* 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 {
|
2009-05-22 22:33:05 +00:00
|
|
|
chosen_track = prev->track;
|
2009-03-21 22:25:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-17 23:16:01 +00:00
|
|
|
/* 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 tunnelentries, where the vehicle is already inside the wormhole at 8/16 from the tileedge.
|
|
|
|
* Is also the train just reversing, the wagon inside the tunnel is 'on' the tile of the opposite tunnelentry.
|
|
|
|
*/
|
2009-03-21 22:25:38 +00:00
|
|
|
static const TrackBits _connecting_track[DIAGDIR_END][DIAGDIR_END] = {
|
2009-04-17 23:16:01 +00:00
|
|
|
{TRACK_BIT_X, TRACK_BIT_LOWER, TRACK_BIT_NONE, TRACK_BIT_LEFT },
|
|
|
|
{TRACK_BIT_UPPER, TRACK_BIT_Y, TRACK_BIT_LEFT, TRACK_BIT_NONE },
|
|
|
|
{TRACK_BIT_NONE, TRACK_BIT_RIGHT, TRACK_BIT_X, TRACK_BIT_UPPER},
|
|
|
|
{TRACK_BIT_RIGHT, TRACK_BIT_NONE, TRACK_BIT_LOWER, TRACK_BIT_Y }
|
2009-03-21 22:25:38 +00:00
|
|
|
};
|
|
|
|
DiagDirection exitdir = DiagdirBetweenTiles(gp.new_tile, prev->tile);
|
|
|
|
assert(IsValidDiagDirection(exitdir));
|
|
|
|
chosen_track = _connecting_track[enterdir][exitdir];
|
|
|
|
}
|
|
|
|
chosen_track &= bits;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 11:29:20 +00:00
|
|
|
/* Make sure chosen track is a valid track */
|
|
|
|
assert(
|
|
|
|
chosen_track == TRACK_BIT_X || chosen_track == TRACK_BIT_Y ||
|
|
|
|
chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
|
|
|
|
chosen_track == TRACK_BIT_LEFT || chosen_track == TRACK_BIT_RIGHT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Update XY to reflect the entrance to the new tile, and select the direction to use */
|
2007-02-25 09:27:03 +00:00
|
|
|
const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
|
|
|
|
gp.x = (gp.x & ~0xF) | b[0];
|
|
|
|
gp.y = (gp.y & ~0xF) | b[1];
|
|
|
|
Direction chosen_dir = (Direction)b[2];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Call the landscape function and tell it that the vehicle entered the tile */
|
2007-02-25 09:27:03 +00:00
|
|
|
uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
goto invalid_rail;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
|
2008-08-02 22:53:37 +00:00
|
|
|
Track track = FindFirstTrack(chosen_track);
|
|
|
|
Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->IsFrontEngine() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
|
2008-08-02 22:53:37 +00:00
|
|
|
SetSignalStateByTrackdir(gp.new_tile, tdir, SIGNAL_STATE_RED);
|
|
|
|
MarkTileDirtyByTile(gp.new_tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear any track reservation when the last vehicle leaves the tile */
|
2009-05-22 18:17:20 +00:00
|
|
|
if (v->Next() == NULL) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
|
2008-08-02 22:53:37 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = gp.new_tile;
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2007-02-25 11:36:19 +00:00
|
|
|
if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
|
2010-01-15 18:23:52 +00:00
|
|
|
v->First()->PowerChanged();
|
2010-01-31 12:56:32 +00:00
|
|
|
v->First()->UpdateAcceleration();
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
v->track = chosen_track;
|
|
|
|
assert(v->track);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-12 11:22:32 +00:00
|
|
|
/* We need to update signal status, but after the vehicle position hash
|
2010-03-06 12:52:44 +00:00
|
|
|
* has been updated by UpdateInclination() */
|
2008-01-17 17:57:39 +00:00
|
|
|
update_signals_crossing = true;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-06-01 13:14:56 +00:00
|
|
|
if (chosen_dir != v->direction) {
|
2010-01-30 16:27:35 +00:00
|
|
|
if (prev == NULL && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
|
2009-06-01 13:14:56 +00:00
|
|
|
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
|
|
|
|
DirDiff diff = DirDifference(v->direction, chosen_dir);
|
|
|
|
v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
|
|
|
|
}
|
|
|
|
direction_changed = true;
|
|
|
|
v->direction = chosen_dir;
|
|
|
|
}
|
2008-08-02 22:55:23 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2008-08-02 22:55:23 +00:00
|
|
|
|
2008-08-02 22:57:18 +00:00
|
|
|
/* If we are approching a crossing that is reserved, play the sound now. */
|
|
|
|
TileIndex crossing = TrainApproachingCrossingTile(v);
|
2009-07-21 22:37:10 +00:00
|
|
|
if (crossing != INVALID_TILE && HasCrossingReservation(crossing)) SndPlayTileFx(SND_0E_LEVEL_CROSSING, crossing);
|
2008-08-02 22:57:18 +00:00
|
|
|
|
2008-08-02 22:55:23 +00:00
|
|
|
/* Always try to extend the reservation when entering a tile. */
|
|
|
|
CheckNextTrainTile(v);
|
|
|
|
}
|
2009-04-12 14:11:14 +00:00
|
|
|
|
|
|
|
if (HasBit(r, VETS_ENTERED_STATION)) {
|
|
|
|
/* The new position is the location where we want to stop */
|
|
|
|
TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-12-27 13:25:23 +00:00
|
|
|
/* In a tunnel or on a bridge
|
|
|
|
* - for tunnels, only the part when the vehicle is not visible (part of enter/exit tile too)
|
|
|
|
* - for bridges, only the middle part - without the bridge heads */
|
2007-02-24 18:44:30 +00:00
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
v->cur_speed =
|
2008-02-05 05:21:02 +00:00
|
|
|
min(v->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed);
|
2007-02-24 18:44:30 +00:00
|
|
|
}
|
2006-12-27 12:38:02 +00:00
|
|
|
|
2008-08-02 22:55:23 +00:00
|
|
|
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
|
|
|
|
/* Perform look-ahead on tunnel exit. */
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2008-08-10 13:37:09 +00:00
|
|
|
TryReserveRailTrack(gp.new_tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(gp.new_tile)));
|
|
|
|
CheckNextTrainTile(v);
|
|
|
|
}
|
2008-08-02 22:55:23 +00:00
|
|
|
} else {
|
2005-07-19 11:42:40 +00:00
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
|
2005-07-19 11:42:40 +00:00
|
|
|
continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update image of train, as well as delta XY */
|
2007-12-27 13:25:23 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
|
|
|
|
/* update the Z position of the vehicle */
|
2010-03-06 12:52:44 +00:00
|
|
|
byte old_z = v->UpdateInclination(gp.new_tile != gp.old_tile, false);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (prev == NULL) {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* This is the first vehicle in the train */
|
2004-08-09 17:04:08 +00:00
|
|
|
AffectSpeedByZChange(v, old_z);
|
|
|
|
}
|
2007-06-12 11:22:32 +00:00
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
if (update_signals_crossing) {
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2008-08-02 22:56:35 +00:00
|
|
|
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. */
|
2009-05-22 22:33:05 +00:00
|
|
|
if ((!HasReservedTracks(gp.new_tile, v->track) &&
|
|
|
|
!TryReserveRailTrack(gp.new_tile, FindFirstTrack(v->track))) ||
|
2008-08-02 22:56:35 +00:00
|
|
|
!TryPathReserve(v)) {
|
|
|
|
MarkTrainAsStuck(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-12 11:22:32 +00:00
|
|
|
|
|
|
|
/* Signals can only change when the first
|
|
|
|
* (above) or the last vehicle moves. */
|
2008-01-17 17:57:39 +00:00
|
|
|
if (v->Next() == NULL) {
|
|
|
|
TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
|
2008-01-17 19:49:06 +00:00
|
|
|
if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile);
|
2008-01-17 17:57:39 +00:00
|
|
|
}
|
2007-06-12 11:22:32 +00:00
|
|
|
}
|
2008-08-02 22:55:23 +00:00
|
|
|
|
|
|
|
/* Do not check on every tick to save some computing time. */
|
2009-07-01 22:22:01 +00:00
|
|
|
if (v->IsFrontEngine() && v->tick_counter % _settings_game.pf.path_backoff_interval == 0) CheckNextTrainTile(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-06-01 13:14:56 +00:00
|
|
|
|
2010-01-15 18:23:52 +00:00
|
|
|
if (direction_changed) first->tcache.cached_max_curve_speed = first->GetCurveSpeedLimit();
|
2009-06-01 13:14:56 +00:00
|
|
|
|
2005-03-06 15:15:27 +00:00
|
|
|
return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
invalid_rail:
|
2004-08-11 22:07:08 +00:00
|
|
|
/* We've reached end of line?? */
|
2008-06-05 20:54:52 +00:00
|
|
|
if (prev != NULL) error("Disconnecting train");
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
reverse_train_direction:
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
|
|
|
}
|
|
|
|
|
2008-08-03 19:04:11 +00:00
|
|
|
/** Collect trackbits of all crashed train vehicles on a tile
|
2008-09-07 11:23:10 +00:00
|
|
|
* @param v Vehicle passed from Find/HasVehicleOnPos()
|
2008-08-03 19:04:11 +00:00
|
|
|
* @param data trackdirbits for the result
|
2008-09-07 11:23:10 +00:00
|
|
|
* @return NULL to iterate over all vehicles on the tile.
|
2008-08-03 19:04:11 +00:00
|
|
|
*/
|
|
|
|
static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
|
|
|
|
{
|
|
|
|
TrackBits *trackbits = (TrackBits *)data;
|
|
|
|
|
2008-08-04 13:15:15 +00:00
|
|
|
if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) {
|
2009-06-10 16:02:00 +00:00
|
|
|
if (Train::From(v)->track == TRACK_BIT_WORMHOLE) {
|
2009-05-22 22:33:05 +00:00
|
|
|
/* Vehicle is inside a wormhole, v->track contains no useful value then. */
|
2008-08-04 13:15:15 +00:00
|
|
|
*trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(v->tile));
|
|
|
|
} else {
|
2009-06-06 16:54:22 +00:00
|
|
|
*trackbits |= Train::From(v)->track;
|
2008-08-04 13:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-03 19:04:11 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-03 23:26:35 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
2007-12-15 00:04:01 +00:00
|
|
|
* or inside a tunnel/bridge, recalculate the signals as they might need updating
|
2007-04-18 00:41:09 +00:00
|
|
|
* @param v the Vehicle of which last wagon is to be removed
|
2005-03-03 23:26:35 +00:00
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static void DeleteLastWagon(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *first = v->First();
|
2008-01-13 13:11:59 +00:00
|
|
|
|
2005-03-03 23:26:35 +00:00
|
|
|
/* Go to the last wagon and delete the link pointing there
|
|
|
|
* *u is then the one-before-last wagon, and *v the last
|
|
|
|
* one which will physicially be removed */
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *u = v;
|
2007-08-30 13:03:56 +00:00
|
|
|
for (; v->Next() != NULL; v = v->Next()) u = v;
|
|
|
|
u->SetNext(NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-12-26 21:08:51 +00:00
|
|
|
if (first != v) {
|
2008-01-13 13:11:59 +00:00
|
|
|
/* Recalculate cached train properties */
|
2010-01-15 18:23:52 +00:00
|
|
|
first->ConsistChanged(false);
|
2008-01-13 13:11:59 +00:00
|
|
|
/* Update the depot window if the first vehicle is in depot -
|
|
|
|
* if v == first, then it is updated in PreDestructor() */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (first->track == TRACK_BIT_DEPOT) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, first->tile);
|
2008-01-13 13:11:59 +00:00
|
|
|
}
|
2008-01-12 19:24:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-12 19:20:44 +00:00
|
|
|
/* 'v' shouldn't be accessed after it has been deleted */
|
2009-05-22 22:33:05 +00:00
|
|
|
TrackBits trackbits = v->track;
|
2008-01-12 19:20:44 +00:00
|
|
|
TileIndex tile = v->tile;
|
2008-01-15 15:00:01 +00:00
|
|
|
Owner owner = v->owner;
|
2008-01-12 19:20:44 +00:00
|
|
|
|
2007-08-03 19:36:00 +00:00
|
|
|
delete v;
|
2008-08-03 19:04:11 +00:00
|
|
|
v = NULL; // make sure nobody will try to read 'v' anymore
|
|
|
|
|
2009-06-10 16:02:00 +00:00
|
|
|
if (trackbits == TRACK_BIT_WORMHOLE) {
|
2009-05-22 22:33:05 +00:00
|
|
|
/* Vehicle is inside a wormhole, v->track contains no useful value then. */
|
2009-06-10 16:02:00 +00:00
|
|
|
trackbits = DiagDirToDiagTrackBits(GetTunnelBridgeDirection(tile));
|
2008-08-04 13:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Track track = TrackBitsToTrack(trackbits);
|
2008-08-03 19:04:11 +00:00
|
|
|
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 = TRACK_BIT_NONE;
|
2008-09-07 11:23:10 +00:00
|
|
|
FindVehicleOnPos(tile, &remaining_trackbits, CollectTrackbitsFromCrashedVehiclesEnum);
|
2008-08-03 19:04:11 +00:00
|
|
|
|
|
|
|
/* 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 && TRACK_Y == TRACK_BEGIN + 1);
|
|
|
|
for (Track t = TRACK_BEGIN; t < TRACK_END; t++) {
|
|
|
|
if (HasBit(remaining_trackbits, t)) {
|
|
|
|
TryReserveRailTrack(tile, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
/* check if the wagon was on a road/rail-crossing */
|
2008-01-17 19:49:06 +00:00
|
|
|
if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile);
|
2005-01-23 10:40:54 +00:00
|
|
|
|
2008-01-12 19:33:25 +00:00
|
|
|
/* Update signals */
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE) || IsRailDepotTile(tile)) {
|
2008-01-15 15:00:01 +00:00
|
|
|
UpdateSignalsOnSegment(tile, INVALID_DIAGDIR, owner);
|
2008-01-12 19:33:25 +00:00
|
|
|
} else {
|
2008-08-03 19:04:11 +00:00
|
|
|
SetSignalsOnBothDir(tile, track, owner);
|
2004-08-21 09:57:02 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void ChangeTrainDirRandomly(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-08 08:16:31 +00:00
|
|
|
static const DirDiff delta[] = {
|
|
|
|
DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
|
|
|
|
};
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2006-12-27 12:38:02 +00:00
|
|
|
/* We don't need to twist around vehicles if they're not visible */
|
2006-06-07 19:35:21 +00:00
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
2006-12-27 12:38:02 +00:00
|
|
|
v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2006-12-27 12:38:02 +00:00
|
|
|
/* Refrain from updating the z position of the vehicle when on
|
2010-03-06 12:52:44 +00:00
|
|
|
* a bridge, because UpdateInclination() will put the vehicle under
|
2008-04-07 12:36:50 +00:00
|
|
|
* the bridge in that case */
|
2010-03-06 12:52:44 +00:00
|
|
|
if (v->track != TRACK_BIT_WORMHOLE) v->UpdateInclination(false, false);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((v = v->Next()) != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool HandleCrashedTrain(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 22:33:05 +00:00
|
|
|
int state = ++v->crash_anim_pos;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-13 10:46:45 +00:00
|
|
|
if (state == 4 && !(v->vehstatus & VS_HIDDEN)) {
|
2005-02-12 15:53:32 +00:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
uint32 r;
|
2007-11-25 15:35:25 +00:00
|
|
|
if (state <= 200 && Chance16R(1, 7, r)) {
|
2006-02-13 21:15:00 +00:00
|
|
|
int index = (r * 10 >> 16);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
Vehicle *u = v;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
if (--index < 0) {
|
2005-01-24 14:39:22 +00:00
|
|
|
r = Random();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
CreateEffectVehicleRel(u,
|
2005-07-20 15:29:28 +00:00
|
|
|
GB(r, 8, 3) + 2,
|
|
|
|
GB(r, 16, 3) + 2,
|
|
|
|
GB(r, 0, 3) + 5,
|
2005-02-12 15:53:32 +00:00
|
|
|
EV_EXPLOSION_SMALL);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((u = u->Next()) != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-18 04:37:06 +00:00
|
|
|
if (state >= 4440 && !(v->tick_counter & 0x1F)) {
|
2009-05-22 13:53:14 +00:00
|
|
|
bool ret = v->Next() != NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteLastWagon(v);
|
2009-05-22 13:53:14 +00:00
|
|
|
return ret;
|
2005-01-23 22:01:51 +00:00
|
|
|
}
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void HandleBrokenTrain(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (v->breakdown_ctr != 1) {
|
|
|
|
v->breakdown_ctr = 1;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
|
|
|
|
if (v->breakdowns_since_last_service != 255)
|
|
|
|
v->breakdowns_since_last_service++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-09-20 19:36:27 +00:00
|
|
|
v->MarkDirty();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
|
2008-05-29 15:13:28 +00:00
|
|
|
SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
|
2006-09-27 18:17:01 +00:00
|
|
|
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
2009-05-22 18:56:25 +00:00
|
|
|
EffectVehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
|
|
|
if (u != NULL) u->animation_state = v->breakdown_delay * 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(v->tick_counter & 3)) {
|
|
|
|
if (!--v->breakdown_delay) {
|
|
|
|
v->breakdown_ctr = 0;
|
2009-09-20 19:36:27 +00:00
|
|
|
v->MarkDirty();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-14 14:46:09 +00:00
|
|
|
/** Maximum speeds for train that is broken down or approaching line end */
|
2008-01-16 15:48:36 +00:00
|
|
|
static const uint16 _breakdown_speeds[16] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
|
|
|
|
};
|
|
|
|
|
2008-01-16 00:33:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Train is approaching line end, slow down and possibly reverse
|
|
|
|
*
|
|
|
|
* @param v front train engine
|
|
|
|
* @param signal not line end, just a red signal
|
|
|
|
* @return true iff we did NOT have to reverse
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool TrainApproachingLineEnd(Train *v, bool signal)
|
2008-01-16 00:33:28 +00:00
|
|
|
{
|
|
|
|
/* Calc position within the current tile */
|
|
|
|
uint x = v->x_pos & 0xF;
|
|
|
|
uint y = v->y_pos & 0xF;
|
|
|
|
|
2008-01-16 15:48:36 +00:00
|
|
|
/* for diagonal directions, 'x' will be 0..15 -
|
|
|
|
* for other directions, it will be 1, 3, 5, ..., 15 */
|
2008-01-16 00:33:28 +00:00
|
|
|
switch (v->direction) {
|
2008-01-16 15:48:36 +00:00
|
|
|
case DIR_N : x = ~x + ~y + 25; break;
|
2009-03-15 00:32:18 +00:00
|
|
|
case DIR_NW: x = y; // FALLTHROUGH
|
2008-01-16 00:33:28 +00:00
|
|
|
case DIR_NE: x = ~x + 16; break;
|
2008-01-16 15:48:36 +00:00
|
|
|
case DIR_E : x = ~x + y + 9; break;
|
2008-01-16 00:33:28 +00:00
|
|
|
case DIR_SE: x = y; break;
|
2008-01-16 15:48:36 +00:00
|
|
|
case DIR_S : x = x + y - 7; break;
|
|
|
|
case DIR_W : x = ~y + x + 9; break;
|
2008-01-16 00:33:28 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do not reverse when approaching red signal */
|
2009-05-22 22:55:41 +00:00
|
|
|
if (!signal && x + (v->tcache.cached_veh_length + 1) / 2 >= TILE_SIZE) {
|
2008-01-16 00:33:28 +00:00
|
|
|
/* we are too near the tile end, reverse now */
|
|
|
|
v->cur_speed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slow down */
|
|
|
|
v->vehstatus |= VS_TRAIN_SLOWING;
|
|
|
|
uint16 break_speed = _breakdown_speeds[x & 0xF];
|
|
|
|
if (break_speed < v->cur_speed) v->cur_speed = break_speed;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
/**
|
|
|
|
* Determines whether train would like to leave the tile
|
|
|
|
* @param v train to test
|
|
|
|
* @return true iff vehicle is NOT entering or inside a depot or tunnel/bridge
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool TrainCanLeaveTile(const Train *v)
|
2008-01-17 17:57:39 +00:00
|
|
|
{
|
|
|
|
/* Exit if inside a tunnel/bridge or a depot */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->track == TRACK_BIT_WORMHOLE || v->track == TRACK_BIT_DEPOT) return false;
|
2008-01-17 17:57:39 +00:00
|
|
|
|
|
|
|
TileIndex tile = v->tile;
|
|
|
|
|
|
|
|
/* entering a tunnel/bridge? */
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
DiagDirection dir = GetTunnelBridgeDirection(tile);
|
|
|
|
if (DiagDirToDir(dir) == v->direction) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* entering a depot? */
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsRailDepotTile(tile)) {
|
2008-01-17 17:57:39 +00:00
|
|
|
DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile));
|
|
|
|
if (DiagDirToDir(dir) == v->direction) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether train is approaching a rail-road crossing
|
|
|
|
* (thus making it barred)
|
|
|
|
* @param v front engine of train
|
|
|
|
* @return TileIndex of crossing the train is approaching, else INVALID_TILE
|
|
|
|
* @pre v in non-crashed front engine
|
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static TileIndex TrainApproachingCrossingTile(const Train *v)
|
2008-01-17 17:57:39 +00:00
|
|
|
{
|
2009-07-01 22:22:01 +00:00
|
|
|
assert(v->IsFrontEngine());
|
2008-01-17 17:57:39 +00:00
|
|
|
assert(!(v->vehstatus & VS_CRASHED));
|
|
|
|
|
|
|
|
if (!TrainCanLeaveTile(v)) return INVALID_TILE;
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
DiagDirection dir = TrainExitDir(v->direction, v->track);
|
2008-01-17 17:57:39 +00:00
|
|
|
TileIndex tile = v->tile + TileOffsByDiagDir(dir);
|
|
|
|
|
2008-02-20 11:00:17 +00:00
|
|
|
/* not a crossing || wrong axis || unusable rail (wrong type or owner) */
|
2008-01-17 17:57:39 +00:00
|
|
|
if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
|
2008-02-20 11:00:17 +00:00
|
|
|
!CheckCompatibleRail(v, tile)) {
|
2008-01-17 17:57:39 +00:00
|
|
|
return INVALID_TILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-14 14:46:09 +00:00
|
|
|
/**
|
|
|
|
* Checks for line end. Also, bars crossing at next tile if needed
|
|
|
|
*
|
|
|
|
* @param v vehicle we are checking
|
2008-01-16 00:33:28 +00:00
|
|
|
* @return true iff we did NOT have to reverse
|
2008-01-14 14:46:09 +00:00
|
|
|
*/
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool TrainCheckIfLineEnds(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-16 00:33:28 +00:00
|
|
|
/* First, handle broken down train */
|
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
int t = v->breakdown_ctr;
|
2005-10-03 21:20:01 +00:00
|
|
|
if (t > 1) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus |= VS_TRAIN_SLOWING;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-25 09:27:03 +00:00
|
|
|
uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)];
|
2005-10-03 21:20:01 +00:00
|
|
|
if (break_speed < v->cur_speed) v->cur_speed = break_speed;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
v->vehstatus &= ~VS_TRAIN_SLOWING;
|
|
|
|
}
|
|
|
|
|
2008-01-17 17:57:39 +00:00
|
|
|
if (!TrainCanLeaveTile(v)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Determine the non-diagonal direction in which we will exit this tile */
|
2009-05-22 22:33:05 +00:00
|
|
|
DiagDirection dir = TrainExitDir(v->direction, v->track);
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Calculate next tile */
|
2008-01-17 17:57:39 +00:00
|
|
|
TileIndex tile = v->tile + TileOffsByDiagDir(dir);
|
2008-01-16 00:33:28 +00:00
|
|
|
|
|
|
|
/* Determine the track status on the next tile */
|
2008-04-01 14:47:57 +00:00
|
|
|
TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir));
|
|
|
|
TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);
|
|
|
|
|
|
|
|
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
|
|
|
|
TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-01-16 00:33:28 +00:00
|
|
|
/* We are sure the train is not entering a depot, it is detected above */
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-04 15:56:39 +00:00
|
|
|
/* mask unreachable track bits if we are forbidden to do 90deg turns */
|
2008-02-20 17:49:50 +00:00
|
|
|
TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
|
2009-12-01 22:18:51 +00:00
|
|
|
if (_settings_game.pf.forbid_90_deg) {
|
2009-05-22 22:33:05 +00:00
|
|
|
bits &= ~TrackCrossesTracks(FindFirstTrack(v->track));
|
2008-02-04 15:56:39 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 11:00:17 +00:00
|
|
|
/* no suitable trackbits at all || unusable rail (wrong type or owner) */
|
|
|
|
if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(v, tile)) {
|
2008-01-16 00:33:28 +00:00
|
|
|
return TrainApproachingLineEnd(v, false);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 00:33:28 +00:00
|
|
|
/* approaching red signal */
|
2008-02-20 17:49:50 +00:00
|
|
|
if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(v, true);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-16 00:33:28 +00:00
|
|
|
/* approaching a rail/road crossing? then make it red */
|
2008-01-18 21:44:20 +00:00
|
|
|
if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile);
|
2004-12-21 16:00:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 00:33:28 +00:00
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static bool TrainLocoHandler(Train *v, bool mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* train has crashed? */
|
2007-08-26 20:16:02 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) {
|
2009-05-22 13:53:14 +00:00
|
|
|
return mode ? true : HandleCrashedTrain(v); // 'this' can be deleted here
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->force_proceed != 0) {
|
|
|
|
ClrBit(v->flags, VRF_TRAIN_STUCK);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2008-08-02 22:53:21 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* train is broken down? */
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenTrain(v);
|
2009-05-22 13:53:14 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-19 21:23:42 +00:00
|
|
|
if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (HasBit(v->flags, VRF_REVERSING) && v->cur_speed == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ReverseTrainDirection(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exit if train is stopped */
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((v->vehstatus & VS_STOPPED) && v->cur_speed == 0) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
bool valid_order = !v->current_order.IsType(OT_NOTHING) && v->current_order.GetType() != OT_CONDITIONAL;
|
2008-04-05 10:55:50 +00:00
|
|
|
if (ProcessOrders(v) && CheckReverseTrain(v)) {
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
2009-05-22 13:53:14 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 16:21:34 +00:00
|
|
|
v->HandleLoading(mode);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (CheckTrainStayInDepot(v)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!mode) HandleLocomotiveSmokeCloud(v);
|
|
|
|
|
2008-08-02 22:55:23 +00:00
|
|
|
/* We had no order but have an order now, do look ahead. */
|
2009-05-22 15:13:50 +00:00
|
|
|
if (!valid_order && !v->current_order.IsType(OT_NOTHING)) {
|
2008-08-02 22:55:23 +00:00
|
|
|
CheckNextTrainTile(v);
|
|
|
|
}
|
|
|
|
|
2008-08-02 22:54:38 +00:00
|
|
|
/* Handle stuck trains. */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (!mode && HasBit(v->flags, VRF_TRAIN_STUCK)) {
|
2010-01-09 14:43:08 +00:00
|
|
|
++v->wait_counter;
|
2008-08-02 22:54:38 +00:00
|
|
|
|
|
|
|
/* Should we try reversing this tick if still stuck? */
|
2010-01-09 14:43:08 +00:00
|
|
|
bool turn_around = v->wait_counter % (_settings_game.pf.wait_for_pbs_path * DAY_TICKS) == 0 && _settings_game.pf.wait_for_pbs_path < 255;
|
2008-08-02 22:54:38 +00:00
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
if (!turn_around && v->wait_counter % _settings_game.pf.path_backoff_interval != 0 && v->force_proceed == 0) return true;
|
2008-08-02 22:54:38 +00:00
|
|
|
if (!TryPathReserve(v)) {
|
|
|
|
/* Still stuck. */
|
|
|
|
if (turn_around) ReverseTrainDirection(v);
|
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
if (HasBit(v->flags, VRF_TRAIN_STUCK) && v->wait_counter > 2 * _settings_game.pf.wait_for_pbs_path * DAY_TICKS) {
|
2008-08-02 22:54:38 +00:00
|
|
|
/* Show message to player. */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
|
2009-01-04 17:42:46 +00:00
|
|
|
SetDParam(0, v->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-08-04 18:04:33 +00:00
|
|
|
STR_NEWS_TRAIN_IS_STUCK,
|
2008-08-02 22:54:38 +00:00
|
|
|
NS_ADVICE,
|
2009-05-24 16:52:42 +00:00
|
|
|
v->index
|
|
|
|
);
|
2008-08-02 22:54:38 +00:00
|
|
|
}
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2008-08-02 22:54:38 +00:00
|
|
|
}
|
|
|
|
/* Exit if force proceed not pressed, else reset stuck flag anyway. */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (v->force_proceed == 0) return true;
|
|
|
|
ClrBit(v->flags, VRF_TRAIN_STUCK);
|
2010-01-09 14:43:08 +00:00
|
|
|
v->wait_counter = 0;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2008-08-02 22:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-11 15:39:49 +00:00
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION)) {
|
|
|
|
v->current_order.Free();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2009-05-22 13:53:14 +00:00
|
|
|
return true;
|
2009-01-11 15:39:49 +00:00
|
|
|
}
|
|
|
|
|
2010-01-16 22:16:28 +00:00
|
|
|
int j = v->UpdateSpeed();
|
2008-01-27 20:20:53 +00:00
|
|
|
|
|
|
|
/* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
|
2009-06-01 11:43:36 +00:00
|
|
|
if (v->cur_speed == 0 && v->tcache.last_speed == 0 && (v->vehstatus & VS_STOPPED)) {
|
2009-12-16 16:23:44 +00:00
|
|
|
/* If we manually stopped, we're not force-proceeding anymore. */
|
|
|
|
v->force_proceed = 0;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2008-01-27 20:20:53 +00:00
|
|
|
}
|
|
|
|
|
2008-10-03 12:55:39 +00:00
|
|
|
int adv_spd = (v->direction & 1) ? 192 : 256;
|
|
|
|
if (j < adv_spd) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* if the vehicle has speed 0, update the last_speed field. */
|
2008-10-03 12:55:39 +00:00
|
|
|
if (v->cur_speed == 0) SetLastSpeed(v, v->cur_speed);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
TrainCheckIfLineEnds(v);
|
2008-10-03 12:55:39 +00:00
|
|
|
/* Loop until the train has finished moving. */
|
2009-04-19 04:36:53 +00:00
|
|
|
for (;;) {
|
2008-10-03 12:55:39 +00:00
|
|
|
j -= adv_spd;
|
2009-03-11 23:23:08 +00:00
|
|
|
TrainController(v, NULL);
|
2009-04-19 04:36:53 +00:00
|
|
|
/* Don't continue to move if the train crashed. */
|
|
|
|
if (CheckTrainCollision(v)) break;
|
2008-10-03 12:55:39 +00:00
|
|
|
/* 192 spd used for going straight, 256 for going diagonally. */
|
|
|
|
adv_spd = (v->direction & 1) ? 192 : 256;
|
2009-04-18 13:43:05 +00:00
|
|
|
|
2009-04-19 04:36:53 +00:00
|
|
|
/* No more moving this tick */
|
|
|
|
if (j < adv_spd || v->cur_speed == 0) break;
|
|
|
|
|
|
|
|
OrderType order_type = v->current_order.GetType();
|
|
|
|
/* Do not skip waypoints (incl. 'via' stations) when passing through at full speed. */
|
2009-07-22 08:59:57 +00:00
|
|
|
if ((order_type == OT_GOTO_WAYPOINT || order_type == OT_GOTO_STATION) &&
|
2009-04-19 04:36:53 +00:00
|
|
|
(v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) &&
|
|
|
|
IsTileType(v->tile, MP_STATION) &&
|
2009-07-22 08:59:57 +00:00
|
|
|
v->current_order.GetDestination() == GetStationIndex(v->tile)) {
|
2009-04-19 04:36:53 +00:00
|
|
|
ProcessOrders(v);
|
2009-04-18 13:43:05 +00:00
|
|
|
}
|
2009-04-19 04:36:53 +00:00
|
|
|
}
|
2008-10-03 12:55:39 +00:00
|
|
|
SetLastSpeed(v, v->cur_speed);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
for (Train *u = v; u != NULL; u = u->Next()) {
|
2009-03-11 23:23:08 +00:00
|
|
|
if ((u->vehstatus & VS_HIDDEN) != 0) continue;
|
|
|
|
|
2009-08-08 18:45:12 +00:00
|
|
|
u->UpdateViewport(false, false);
|
2009-03-11 23:23:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 22:48:57 +00:00
|
|
|
if (v->progress == 0) v->progress = j; // Save unused spd for next time, if TrainController didn't set progress
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-29 21:27:16 +00:00
|
|
|
|
|
|
|
Money Train::GetRunningCost() const
|
|
|
|
{
|
|
|
|
Money cost = 0;
|
2009-05-22 22:22:46 +00:00
|
|
|
const Train *v = this;
|
2007-08-29 21:27:16 +00:00
|
|
|
|
|
|
|
do {
|
2009-11-24 13:12:34 +00:00
|
|
|
const Engine *e = Engine::Get(v->engine_type);
|
|
|
|
if (e->u.rail.running_cost_class == INVALID_PRICE) continue;
|
2007-08-29 21:27:16 +00:00
|
|
|
|
2009-11-24 13:12:34 +00:00
|
|
|
uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->u.rail.running_cost);
|
2007-08-29 21:27:16 +00:00
|
|
|
if (cost_factor == 0) continue;
|
|
|
|
|
2008-05-13 20:39:57 +00:00
|
|
|
/* Halve running cost for multiheaded parts */
|
2009-07-01 23:57:20 +00:00
|
|
|
if (v->IsMultiheaded()) cost_factor /= 2;
|
2008-05-13 20:39:57 +00:00
|
|
|
|
2009-11-24 13:15:58 +00:00
|
|
|
cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->grffile);
|
2009-07-02 12:10:39 +00:00
|
|
|
} while ((v = v->GetNextVehicle()) != NULL);
|
2007-08-29 21:27:16 +00:00
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
bool Train::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-07-01 19:24:54 +00:00
|
|
|
this->tick_counter++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (this->IsFrontEngine()) {
|
2008-02-13 19:24:40 +00:00
|
|
|
if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
|
2009-05-22 13:53:14 +00:00
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
this->current_order_time++;
|
2007-06-20 19:17:22 +00:00
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (!TrainLocoHandler(this, false)) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
return TrainLocoHandler(this, true);
|
2009-07-01 23:49:13 +00:00
|
|
|
} else if (this->IsFreeWagon() && (this->vehstatus & VS_CRASHED)) {
|
2007-10-08 20:16:25 +00:00
|
|
|
/* Delete flooded standalone wagon chain */
|
2009-05-22 22:33:05 +00:00
|
|
|
if (++this->crash_anim_pos >= 4400) {
|
2009-05-22 13:53:14 +00:00
|
|
|
delete this;
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 22:22:46 +00:00
|
|
|
static void CheckIfTrainNeedsService(Train *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 21:59:49 +00:00
|
|
|
if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
|
2007-08-31 17:13:39 +00:00
|
|
|
if (v->IsInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-13 10:48:44 +00:00
|
|
|
uint max_penalty;
|
|
|
|
switch (_settings_game.pf.pathfinder_for_trains) {
|
|
|
|
case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break;
|
|
|
|
case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
|
2004-09-23 21:20:38 +00:00
|
|
|
/* Only go to the depot if it is not too far out of our way. */
|
2009-12-13 10:48:44 +00:00
|
|
|
if (tfdd.best_length == UINT_MAX || tfdd.best_length > max_penalty) {
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
2004-09-23 21:20:38 +00:00
|
|
|
/* If we were already heading for a depot but it has
|
|
|
|
* suddenly moved farther away, we continue our normal
|
|
|
|
* schedule? */
|
2008-04-05 23:36:54 +00:00
|
|
|
v->current_order.MakeDummy();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-10 14:37:55 +00:00
|
|
|
DepotID depot = GetDepotIndex(tfdd.tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) &&
|
2009-09-10 14:37:55 +00:00
|
|
|
v->current_order.GetDestination() != depot &&
|
2007-11-25 15:35:25 +00:00
|
|
|
!Chance16(3, 16)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2006-02-13 21:15:00 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-10 14:37:55 +00:00
|
|
|
v->current_order.MakeGoToDepot(depot, ODTFB_SERVICE);
|
2004-09-23 21:20:38 +00:00
|
|
|
v->dest_tile = tfdd.tile;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
void Train::OnNewDay()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-01 22:02:14 +00:00
|
|
|
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-01 22:22:01 +00:00
|
|
|
if (this->IsFrontEngine()) {
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckVehicleBreakdown(this);
|
|
|
|
AgeVehicle(this);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckIfTrainNeedsService(this);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckOrders(this);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* update destination */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (this->current_order.IsType(OT_GOTO_STATION)) {
|
2009-07-25 08:54:19 +00:00
|
|
|
TileIndex tile = Station::Get(this->current_order.GetDestination())->train_station.tile;
|
2008-12-26 18:01:15 +00:00
|
|
|
if (tile != INVALID_TILE) this->dest_tile = tile;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
if (this->running_ticks != 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* running costs */
|
2009-01-13 22:58:03 +00:00
|
|
|
CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
this->profit_this_year -= cost.GetCost();
|
|
|
|
this->running_ticks = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompanyFract(this->owner, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
|
|
|
SetWindowClassesDirty(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-07-01 23:57:20 +00:00
|
|
|
} else if (this->IsEngine()) {
|
2007-06-22 22:42:18 +00:00
|
|
|
/* Also age engines that aren't front engines */
|
2008-02-01 22:02:14 +00:00
|
|
|
AgeVehicle(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir Train::GetVehicleTrackdir() const
|
|
|
|
{
|
|
|
|
if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (this->track == TRACK_BIT_DEPOT) {
|
2009-05-22 18:17:20 +00:00
|
|
|
/* We'll assume the train is facing outwards */
|
|
|
|
return DiagDirToDiagTrackdir(GetRailDepotDirection(this->tile)); // Train in depot
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
if (this->track == TRACK_BIT_WORMHOLE) {
|
2009-05-22 18:17:20 +00:00
|
|
|
/* train in tunnel or on bridge, so just use his direction and assume a diagonal track */
|
|
|
|
return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
|
|
|
|
}
|
|
|
|
|
2009-05-22 22:33:05 +00:00
|
|
|
return TrackDirectionToTrackdir(FindFirstTrack(this->track), this->direction);
|
2009-05-22 18:17:20 +00:00
|
|
|
}
|