OpenTTD: src/train.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

train.h
Go to the documentation of this file.
1 /* $Id: train.h 27666 2016-10-16 14:57:56Z frosch $ */
2
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 */
9
12 #ifndef TRAIN_H
13 #define TRAIN_H
14
15 #include "core/enum_type.hpp"
16
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"
23
24 struct Train;
25
27 enum VehicleRailFlags {
28 VRF_REVERSING = 0,
29 VRF_POWEREDWAGON = 3,
30 VRF_REVERSE_DIRECTION = 4,
31
32 VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6,
33 VRF_TOGGLE_REVERSE = 7,
34 VRF_TRAIN_STUCK = 8,
35 VRF_LEAVING_STATION = 9,
36 };
37
39 enum TrainForceProceeding {
40 TFP_NONE = 0,
41 TFP_STUCK = 1,
42 TFP_SIGNAL = 2,
43 };
44 typedef SimpleTinyEnumT<TrainForceProceeding, byte> TrainForceProceedingByte;
45
47 enum ConsistChangeFlags {
48 CCF_LENGTH = 0x01,
49 CCF_CAPACITY = 0x02,
50
51 CCF_TRACK = 0,
52 CCF_LOADUNLOAD = 0,
53 CCF_AUTOREFIT = CCF_CAPACITY,
54 CCF_REFIT = CCF_LENGTH | CCF_CAPACITY,
55 CCF_ARRANGE = CCF_LENGTH | CCF_CAPACITY,
56 CCF_SAVELOAD = CCF_LENGTH,
57 };
58 DECLARE_ENUM_AS_BIT_SET(ConsistChangeFlags)
59
60 byte FreightWagonMult(CargoID cargo);
61
62 void CheckTrainsLengths();
63
64 void FreeTrainTrackReservation(const Train *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
65 bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay = false);
66
67 int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length);
68
69 void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
70
72 struct TrainCache {
73 /* Cached wagon override spritegroup */
74 const struct SpriteGroup *cached_override;
75
76 /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
77 bool cached_tilt;
78
79 byte user_def_data;
80
81 /* cached max. speed / acceleration data */
82 int cached_max_curve_speed;
83 };
84
88 struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
89 TrainCache tcache;
90
91 /* Link between the two ends of a multiheaded engine */
92 Train *other_multiheaded_part;
93
94 uint16 crash_anim_pos;
95
96 uint16 flags;
97 TrackBitsByte track;
98 TrainForceProceedingByte force_proceed;
99 RailTypeByte railtype;
100 RailTypes compatible_railtypes;
101
103 uint16 wait_counter;
104
106 Train() : GroundVehicleBase() {}
108 virtual ~Train() { this->PreDestructor(); }
109
110 friend struct GroundVehicle<Train, VEH_TRAIN>; // GroundVehicle needs to use the acceleration functions defined at Train.
111
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);
129
130 void ReserveTrackUnderConsist() const;
131
132 int GetCurveSpeedLimit() const;
133
134 void ConsistChanged(ConsistChangeFlags allowed_changes);
135
136 int UpdateSpeed();
137
138 void UpdateAcceleration();
139
140 int GetCurrentMaxSpeed() const;
141
146 inline Train *GetNextUnit() const
147 {
148 Train *v = this->GetNextVehicle();
149 if (v != NULL && v->IsRearDualheaded()) v = v->GetNextVehicle();
150
151 return v;
152 }
153
158 inline Train *GetPrevUnit()
159 {
160 Train *v = this->GetPrevVehicle();
161 if (v != NULL && v->IsRearDualheaded()) v = v->GetPrevVehicle();
162
163 return v;
164 }
165
170 int CalcNextVehicleOffset() const
171 {
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;
177 }
178
179 protected: // These functions should not be called outside acceleration code.
180
185 inline uint16 GetPower() const
186 {
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;
193 }
194
195 return 0;
196 }
197
202 inline uint16 GetPoweredPartPower(const Train *head) const
203 {
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;
207 }
208
209 return 0;
210 }
211
216 inline uint16 GetWeight() const
217 {
218 uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.StoredCount() * FreightWagonMult(this->cargo_type)) / 16;
219
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);
223 }
224
225 /* Powered wagons have extra weight added. */
226 if (HasBit(this->flags, VRF_POWEREDWAGON)) {
227 weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
228 }
229
230 return weight;
231 }
232
237 inline byte GetTractiveEffort() const
238 {
239 return GetVehicleProperty(this, PROP_TRAIN_TRACTIVE_EFFORT, RailVehInfo(this->engine_type)->tractive_effort);
240 }
241
246 inline byte GetAirDragArea() const
247 {
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;
250 }
251
256 inline byte GetAirDrag() const
257 {
258 return RailVehInfo(this->engine_type)->air_drag;
259 }
260
265 inline AccelStatus GetAccelerationStatus() const
266 {
267 return (this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK) ? AS_BRAKE : AS_ACCEL;
268 }
269
274 inline uint16 GetCurrentSpeed() const
275 {
276 return this->cur_speed;
277 }
278
283 inline uint32 GetRollingFriction() const
284 {
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;
289 }
290
295 inline int GetAccelerationType() const
296 {
297 return GetRailTypeInfo(this->railtype)->acceleration_type;
298 }
299
304 inline uint32 GetSlopeSteepness() const
305 {
306 return _settings_game.vehicle.train_slope_steepness;
307 }
308
313 inline uint16 GetMaxTrackSpeed() const
314 {
315 return GetRailTypeInfo(GetRailType(this->tile))->max_speed;
316 }
317
322 inline bool TileMayHaveSlopedTrack() const
323 {
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;
326 }
327
333 inline bool HasToUseGetSlopePixelZ()
334 {
335 return false;
336 }
337 };
338
339 #define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)
340
341 #endif /* TRAIN_H */