|
|
/* $Id$ */
|
|
|
|
|
|
/*
|
|
|
* 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/>.
|
|
|
*/
|
|
|
|
|
|
/** @file train.h Base for the train class. */
|
|
|
|
|
|
#ifndef TRAIN_H
|
|
|
#define TRAIN_H
|
|
|
|
|
|
#include "newgrf_engine.h"
|
|
|
#include "cargotype.h"
|
|
|
#include "rail.h"
|
|
|
#include "engine_base.h"
|
|
|
#include "rail_map.h"
|
|
|
#include "ground_vehicle.hpp"
|
|
|
|
|
|
struct Train;
|
|
|
|
|
|
enum VehicleRailFlags {
|
|
|
VRF_REVERSING = 0,
|
|
|
|
|
|
/* used to store if a wagon is powered or not */
|
|
|
VRF_POWEREDWAGON = 3,
|
|
|
|
|
|
/* used to reverse the visible direction of the vehicle */
|
|
|
VRF_REVERSE_DIRECTION = 4,
|
|
|
|
|
|
/* used to mark train as lost because PF can't find the route */
|
|
|
VRF_NO_PATH_TO_DESTINATION = 5,
|
|
|
|
|
|
/* used to mark that electric train engine is allowed to run on normal rail */
|
|
|
VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6,
|
|
|
|
|
|
/* used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only) */
|
|
|
VRF_TOGGLE_REVERSE = 7,
|
|
|
|
|
|
/* used to mark a train that can't get a path reservation */
|
|
|
VRF_TRAIN_STUCK = 8,
|
|
|
};
|
|
|
|
|
|
/** Modes for ignoring signals. */
|
|
|
enum TrainForceProceeding {
|
|
|
TFP_NONE = 0, ///< Normal operation.
|
|
|
TFP_STUCK = 1, ///< Proceed till next signal, but ignore being stuck till then. This includes force leaving depots.
|
|
|
TFP_SIGNAL = 2, ///< Ignore next signal, after the signal ignore being stucked.
|
|
|
};
|
|
|
typedef SimpleTinyEnumT<TrainForceProceeding, byte> TrainForceProceedingByte;
|
|
|
|
|
|
byte FreightWagonMult(CargoID cargo);
|
|
|
|
|
|
void CheckTrainsLengths();
|
|
|
|
|
|
void FreeTrainTrackReservation(const Train *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
|
|
|
bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay = false);
|
|
|
|
|
|
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length);
|
|
|
|
|
|
/** Variables that are cached to improve performance and such */
|
|
|
struct TrainCache {
|
|
|
/* Cached wagon override spritegroup */
|
|
|
const struct SpriteGroup *cached_override;
|
|
|
|
|
|
uint16 last_speed; // NOSAVE: only used in UI
|
|
|
|
|
|
/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
|
|
|
uint16 cached_total_length; ///< Length of the whole train, valid only for first engine.
|
|
|
uint8 cached_veh_length; ///< length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback
|
|
|
bool cached_tilt; ///< train can tilt; feature provides a bonus in curves
|
|
|
|
|
|
/* cached max. speed / acceleration data */
|
|
|
uint16 cached_max_speed; ///< max speed of the consist. (minimum of the max speed of all vehicles in the consist)
|
|
|
int cached_max_curve_speed; ///< max consist speed limited by curves
|
|
|
|
|
|
/**
|
|
|
* Position/type of visual effect.
|
|
|
* bit 0 - 3 = position of effect relative to vehicle. (0 = front, 8 = centre, 15 = rear)
|
|
|
* bit 4 - 5 = type of effect. (0 = default for engine class, 1 = steam, 2 = diesel, 3 = electric)
|
|
|
* bit 6 = disable visual effect.
|
|
|
* bit 7 = disable powered wagons.
|
|
|
*/
|
|
|
byte cached_vis_effect;
|
|
|
byte user_def_data;
|
|
|
|
|
|
EngineID first_engine; ///< cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 'Train' is either a loco or a wagon.
|
|
|
*/
|
|
|
struct Train : public GroundVehicle<Train, VEH_TRAIN> {
|
|
|
TrainCache tcache;
|
|
|
|
|
|
/* Link between the two ends of a multiheaded engine */
|
|
|
Train *other_multiheaded_part;
|
|
|
|
|
|
uint16 crash_anim_pos;
|
|
|
|
|
|
uint16 flags;
|
|
|
TrackBitsByte track;
|
|
|
byte force_proceed;
|
|
|
TrainForceProceedingByte force_proceed;
|
|
|
RailTypeByte railtype;
|
|
|
RailTypes compatible_railtypes;
|
|
|
|
|
|
/** Ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through signals. */
|
|
|
uint16 wait_counter;
|
|
|
|
|
|
/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
|
|
|
Train() : GroundVehicle<Train, VEH_TRAIN>() {}
|
|
|
/** We want to 'destruct' the right class. */
|
|
|
virtual ~Train() { this->PreDestructor(); }
|
|
|
|
|
|
friend struct GroundVehicle<Train, VEH_TRAIN>; // GroundVehicle needs to use the acceleration functions defined at Train.
|
|
|
|
|
|
const char *GetTypeString() const { return "train"; }
|
|
|
void MarkDirty();
|
|
|
void UpdateDeltaXY(Direction direction);
|
|
|
ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
|
|
|
void PlayLeaveStationSound() const;
|
|
|
bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
|
|
|
SpriteID GetImage(Direction direction) const;
|
|
|
int GetDisplaySpeed() const { return this->tcache.last_speed; }
|
|
|
int GetDisplayMaxSpeed() const { return this->tcache.cached_max_speed; }
|
|
|
Money GetRunningCost() const;
|
|
|
int GetDisplayImageWidth(Point *offset = NULL) const;
|
|
|
bool IsInDepot() const;
|
|
|
bool IsStoppedInDepot() const;
|
|
|
bool Tick();
|
|
|
void OnNewDay();
|
|
|
uint Crash(bool flooded = false);
|
|
|
Trackdir GetVehicleTrackdir() const;
|
|
|
TileIndex GetOrderStationLocation(StationID station);
|
|
|
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
|
|
|
|
|
|
void ReserveTrackUnderConsist() const;
|
|
|
|
|
|
int GetCurveSpeedLimit() const;
|
|
|
|
|
|
void ConsistChanged(bool same_length);
|
|
|
|
|
|
int UpdateSpeed();
|
|
|
|
|
|
void UpdateAcceleration();
|
|
|
|
|
|
int GetCurrentMaxSpeed() const;
|
|
|
|
|
|
/**
|
|
|
* enum to handle train subtypes
|
|
|
* Do not access it directly unless you have to. Use the access functions below
|