(svn r27953) -Cleanup: Adjust other languages for r27952
[openttd.git] / src / train.h
blobc40ab7556082b4c7ec260971db361d2de5fcb8d6
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file train.h Base for the train class. */
12 #ifndef TRAIN_H
13 #define TRAIN_H
15 #include "core/enum_type.hpp"
17 #include "newgrf_engine.h"
18 #include "cargotype.h"
19 #include "rail.h"
20 #include "engine_base.h"
21 #include "rail_map.h"
22 #include "ground_vehicle.hpp"
24 struct Train;
26 /** Rail vehicle flags. */
27 enum VehicleRailFlags {
28 VRF_REVERSING = 0,
29 VRF_POWEREDWAGON = 3, ///< Wagon is powered.
30 VRF_REVERSE_DIRECTION = 4, ///< Reverse the visible direction of the vehicle.
32 VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6, ///< Electric train engine is allowed to run on normal rail. */
33 VRF_TOGGLE_REVERSE = 7, ///< Used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only).
34 VRF_TRAIN_STUCK = 8, ///< Train can't get a path reservation.
35 VRF_LEAVING_STATION = 9, ///< Train is just leaving a station.
38 /** Modes for ignoring signals. */
39 enum TrainForceProceeding {
40 TFP_NONE = 0, ///< Normal operation.
41 TFP_STUCK = 1, ///< Proceed till next signal, but ignore being stuck till then. This includes force leaving depots.
42 TFP_SIGNAL = 2, ///< Ignore next signal, after the signal ignore being stuck.
44 typedef SimpleTinyEnumT<TrainForceProceeding, byte> TrainForceProceedingByte;
46 /** Flags for Train::ConsistChanged */
47 enum ConsistChangeFlags {
48 CCF_LENGTH = 0x01, ///< Allow vehicles to change length.
49 CCF_CAPACITY = 0x02, ///< Allow vehicles to change capacity.
51 CCF_TRACK = 0, ///< Valid changes while vehicle is driving, and possibly changing tracks.
52 CCF_LOADUNLOAD = 0, ///< Valid changes while vehicle is loading/unloading.
53 CCF_AUTOREFIT = CCF_CAPACITY, ///< Valid changes for autorefitting in stations.
54 CCF_REFIT = CCF_LENGTH | CCF_CAPACITY, ///< Valid changes for refitting in a depot.
55 CCF_ARRANGE = CCF_LENGTH | CCF_CAPACITY, ///< Valid changes for arranging the consist in a depot.
56 CCF_SAVELOAD = CCF_LENGTH, ///< Valid changes when loading a savegame. (Everything that is not stored in the save.)
58 DECLARE_ENUM_AS_BIT_SET(ConsistChangeFlags)
60 byte FreightWagonMult(CargoID cargo);
62 void CheckTrainsLengths();
64 void FreeTrainTrackReservation(const Train *v);
65 bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay = false);
67 int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length);
69 void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
71 /** Variables that are cached to improve performance and such */
72 struct TrainCache {
73 /* Cached wagon override spritegroup */
74 const struct SpriteGroup *cached_override;
76 /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
77 bool cached_tilt; ///< train can tilt; feature provides a bonus in curves
79 byte user_def_data; ///< Cached property 0x25. Can be set by Callback 0x36.
81 /* cached max. speed / acceleration data */
82 int cached_max_curve_speed; ///< max consist speed limited by curves
85 /**
86 * 'Train' is either a loco or a wagon.
88 struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
89 TrainCache tcache;
91 /* Link between the two ends of a multiheaded engine */
92 Train *other_multiheaded_part;
94 uint16 crash_anim_pos; ///< Crash animation counter.
96 uint16 flags;
97 TrackBitsByte track;
98 TrainForceProceedingByte force_proceed;
99 RailTypeByte railtype;
100 RailTypes compatible_railtypes;
102 /** Ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through signals. */
103 uint16 wait_counter;
105 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
106 Train() : GroundVehicleBase() {}
107 /** We want to 'destruct' the right class. */
108 virtual ~Train() { this->PreDestructor(); }
110 friend struct GroundVehicle<Train, VEH_TRAIN>; // GroundVehicle needs to use the acceleration functions defined at Train.
112 void MarkDirty();
113 void UpdateDeltaXY(Direction direction);
114 ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
115 void PlayLeaveStationSound() const;
116 bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
117 void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const;
118 int GetDisplaySpeed() const { return this->gcache.last_speed; }
119 int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; }
120 Money GetRunningCost() const;
121 int GetDisplayImageWidth(Point *offset = NULL) const;
122 bool IsInDepot() const { return this->track == TRACK_BIT_DEPOT; }
123 bool Tick();
124 void OnNewDay();
125 uint Crash(bool flooded = false);
126 Trackdir GetVehicleTrackdir() const;
127 TileIndex GetOrderStationLocation(StationID station);
128 bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
130 void ReserveTrackUnderConsist() const;
132 int GetCurveSpeedLimit() const;
134 void ConsistChanged(ConsistChangeFlags allowed_changes);
136 int UpdateSpeed();
138 void UpdateAcceleration();
140 int GetCurrentMaxSpeed() const;
143 * Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
144 * @return Next vehicle in the consist.
146 inline Train *GetNextUnit() const
148 Train *v = this->GetNextVehicle();
149 if (v != NULL && v->IsRearDualheaded()) v = v->GetNextVehicle();
151 return v;
155 * Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
156 * @return Previous vehicle in the consist.
158 inline Train *GetPrevUnit()
160 Train *v = this->GetPrevVehicle();
161 if (v != NULL && v->IsRearDualheaded()) v = v->GetPrevVehicle();
163 return v;
167 * Calculate the offset from this vehicle's center to the following center taking the vehicle lengths into account.
168 * @return Offset from center to center.
170 int CalcNextVehicleOffset() const
172 /* For vehicles with odd lengths the part before the center will be one unit
173 * longer than the part after the center. This means we have to round up the
174 * length of the next vehicle but may not round the length of the current
175 * vehicle. */
176 return this->gcache.cached_veh_length / 2 + (this->Next() != NULL ? this->Next()->gcache.cached_veh_length + 1 : 0) / 2;
179 protected: // These functions should not be called outside acceleration code.
182 * Allows to know the power value that this vehicle will use.
183 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
185 inline uint16 GetPower() const
187 /* Power is not added for articulated parts */
188 if (!this->IsArticulatedPart() && HasPowerOnRail(this->railtype, GetRailType(this->tile))) {
189 uint16 power = GetVehicleProperty(this, PROP_TRAIN_POWER, RailVehInfo(this->engine_type)->power);
190 /* Halve power for multiheaded parts */
191 if (this->IsMultiheaded()) power /= 2;
192 return power;
195 return 0;
199 * Returns a value if this articulated part is powered.
200 * @return Power value from the articulated part in HP, or zero if it is not powered.
202 inline uint16 GetPoweredPartPower(const Train *head) const
204 /* For powered wagons the engine defines the type of engine (i.e. railtype) */
205 if (HasBit(this->flags, VRF_POWEREDWAGON) && HasPowerOnRail(head->railtype, GetRailType(this->tile))) {
206 return RailVehInfo(this->gcache.first_engine)->pow_wag_power;
209 return 0;
213 * Allows to know the weight value that this vehicle will use.
214 * @return Weight value from the engine in tonnes.
216 inline uint16 GetWeight() const
218 uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.StoredCount() * FreightWagonMult(this->cargo_type)) / 16;
220 /* Vehicle weight is not added for articulated parts. */
221 if (!this->IsArticulatedPart()) {
222 weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
225 /* Powered wagons have extra weight added. */
226 if (HasBit(this->flags, VRF_POWEREDWAGON)) {
227 weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
230 return weight;
234 * Allows to know the tractive effort value that this vehicle will use.
235 * @return Tractive effort value from the engine.
237 inline byte GetTractiveEffort() const
239 return GetVehicleProperty(this, PROP_TRAIN_TRACTIVE_EFFORT, RailVehInfo(this->engine_type)->tractive_effort);
243 * Gets the area used for calculating air drag.
244 * @return Area of the engine in m^2.
246 inline byte GetAirDragArea() const
248 /* Air drag is higher in tunnels due to the limited cross-section. */
249 return (this->track == TRACK_BIT_WORMHOLE && this->vehstatus & VS_HIDDEN) ? 28 : 14;
253 * Gets the air drag coefficient of this vehicle.
254 * @return Air drag value from the engine.
256 inline byte GetAirDrag() const
258 return RailVehInfo(this->engine_type)->air_drag;
262 * Checks the current acceleration status of this vehicle.
263 * @return Acceleration status.
265 inline AccelStatus GetAccelerationStatus() const
267 return (this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK) ? AS_BRAKE : AS_ACCEL;
271 * Calculates the current speed of this vehicle.
272 * @return Current speed in km/h-ish.
274 inline uint16 GetCurrentSpeed() const
276 return this->cur_speed;
280 * Returns the rolling friction coefficient of this vehicle.
281 * @return Rolling friction coefficient in [1e-4].
283 inline uint32 GetRollingFriction() const
285 /* Rolling friction for steel on steel is between 0.1% and 0.2%.
286 * The friction coefficient increases with speed in a way that
287 * it doubles at 512 km/h, triples at 1024 km/h and so on. */
288 return 15 * (512 + this->GetCurrentSpeed()) / 512;
292 * Allows to know the acceleration type of a vehicle.
293 * @return Acceleration type of the vehicle.
295 inline int GetAccelerationType() const
297 return GetRailTypeInfo(this->railtype)->acceleration_type;
301 * Returns the slope steepness used by this vehicle.
302 * @return Slope steepness used by the vehicle.
304 inline uint32 GetSlopeSteepness() const
306 return _settings_game.vehicle.train_slope_steepness;
310 * Gets the maximum speed allowed by the track for this vehicle.
311 * @return Maximum speed allowed.
313 inline uint16 GetMaxTrackSpeed() const
315 return GetRailTypeInfo(GetRailType(this->tile))->max_speed;
319 * Checks if the vehicle is at a tile that can be sloped.
320 * @return True if the tile can be sloped.
322 inline bool TileMayHaveSlopedTrack() const
324 /* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped. */
325 return this->track == TRACK_BIT_X || this->track == TRACK_BIT_Y;
329 * Trains can always use the faster algorithm because they
330 * have always the same direction as the track under them.
331 * @return false
333 inline bool HasToUseGetSlopePixelZ()
335 return false;
339 #define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)
341 #endif /* TRAIN_H */