OpenTTD Source 20260208-master-g43af8e94d0
ground_vehicle.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef GROUND_VEHICLE_HPP
11#define GROUND_VEHICLE_HPP
12
13#include "vehicle_base.h"
14#include "vehicle_gui.h"
15#include "landscape.h"
16#include "window_func.h"
17
19
21enum AccelStatus : uint8_t {
24};
25
31 /* Cached acceleration values, recalculated when the cargo on a vehicle changes (in addition to the conditions below) */
32 uint32_t cached_weight = 0;
34 uint32_t cached_max_te = 0;
36
37 /* Cached acceleration values, recalculated on load and each time a vehicle is added to/removed from the consist. */
39 uint32_t cached_power = 0;
40 uint32_t cached_air_drag = 0;
41
42 /* Cached NewGRF values, recalculated on load and each time a vehicle is added to/removed from the consist. */
43 uint16_t cached_total_length = 0;
44 EngineID first_engine = EngineID::Invalid();
45 uint8_t cached_veh_length = 0;
46
47 /* Cached UI information. */
48 uint16_t last_speed = 0;
49
50 auto operator<=>(const GroundVehicleCache &) const = default;
51};
52
59
81template <class T, VehicleType Type>
82struct GroundVehicle : public SpecializedVehicle<T, Type> {
84 uint16_t gv_flags = 0;
85
87
93
94 void PowerChanged();
95 void CargoChanged();
96 int GetAcceleration() const;
97 bool IsChainInDepot() const override;
98
104 uint Crash(bool flooded) override
105 {
106 /* Crashed vehicles aren't going up or down */
107 for (T *v = T::From(this); v != nullptr; v = v->Next()) {
108 ClrBit(v->gv_flags, GVF_GOINGUP_BIT);
109 ClrBit(v->gv_flags, GVF_GOINGDOWN_BIT);
110 }
111 return this->Vehicle::Crash(flooded);
112 }
113
118 inline int64_t GetSlopeResistance() const
119 {
120 int64_t incl = 0;
121
122 for (const T *u = T::From(this); u != nullptr; u = u->Next()) {
123 if (HasBit(u->gv_flags, GVF_GOINGUP_BIT)) {
124 incl += u->gcache.cached_slope_resistance;
125 } else if (HasBit(u->gv_flags, GVF_GOINGDOWN_BIT)) {
126 incl -= u->gcache.cached_slope_resistance;
127 }
128 }
129
130 return incl;
131 }
132
140 {
141 this->z_pos = GetSlopePixelZ(this->x_pos, this->y_pos, true);
142 ClrBit(this->gv_flags, GVF_GOINGUP_BIT);
143 ClrBit(this->gv_flags, GVF_GOINGDOWN_BIT);
144
145 if (T::From(this)->TileMayHaveSlopedTrack()) {
146 /* To check whether the current tile is sloped, and in which
147 * direction it is sloped, we get the 'z' at the center of
148 * the tile (middle_z) and the edge of the tile (old_z),
149 * which we then can compare. */
150 int middle_z = GetSlopePixelZ((this->x_pos & ~TILE_UNIT_MASK) | (TILE_SIZE / 2), (this->y_pos & ~TILE_UNIT_MASK) | (TILE_SIZE / 2), true);
151
152 if (middle_z != this->z_pos) {
153 SetBit(this->gv_flags, (middle_z > this->z_pos) ? GVF_GOINGUP_BIT : GVF_GOINGDOWN_BIT);
154 }
155 }
156 }
157
164 inline void UpdateZPosition()
165 {
166#if 0
167 /* The following code does this: */
168
169 if (HasBit(this->gv_flags, GVF_GOINGUP_BIT)) {
170 switch (this->direction) {
171 case DIR_NE:
172 this->z_pos += (this->x_pos & 1) ^ 1; break;
173 case DIR_SW:
174 this->z_pos += (this->x_pos & 1); break;
175 case DIR_NW:
176 this->z_pos += (this->y_pos & 1) ^ 1; break;
177 case DIR_SE:
178 this->z_pos += (this->y_pos & 1); break;
179 default: break;
180 }
181 } else if (HasBit(this->gv_flags, GVF_GOINGDOWN_BIT)) {
182 switch (this->direction) {
183 case DIR_NE:
184 this->z_pos -= (this->x_pos & 1) ^ 1; break;
185 case DIR_SW:
186 this->z_pos -= (this->x_pos & 1); break;
187 case DIR_NW:
188 this->z_pos -= (this->y_pos & 1) ^ 1; break;
189 case DIR_SE:
190 this->z_pos -= (this->y_pos & 1); break;
191 default: break;
192 }
193 }
194
195 /* But gcc 4.4.5 isn't able to nicely optimise it, and the resulting
196 * code is full of conditional jumps. */
197#endif
198
199 /* Vehicle's Z position can change only if it has GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set.
200 * Furthermore, if this function is called once every time the vehicle's position changes,
201 * we know the Z position changes by +/-1 at certain moments - when x_pos, y_pos is odd/even,
202 * depending on orientation of the slope and vehicle's direction */
203
204 if (HasBit(this->gv_flags, GVF_GOINGUP_BIT) || HasBit(this->gv_flags, GVF_GOINGDOWN_BIT)) {
205 if (T::From(this)->HasToUseGetSlopePixelZ()) {
206 /* In some cases, we have to use GetSlopePixelZ() */
207 this->z_pos = GetSlopePixelZ(this->x_pos, this->y_pos, true);
208 return;
209 }
210 /* DirToDiagDir() is a simple right shift */
212 /* Read variables, so the compiler knows the access doesn't trap */
213 int8_t x_pos = this->x_pos;
214 int8_t y_pos = this->y_pos;
215 /* DiagDirToAxis() is a simple mask */
216 int8_t d = DiagDirToAxis(dir) == AXIS_X ? x_pos : y_pos;
217 /* We need only the least significant bit */
218 d &= 1;
219 d ^= (int8_t)(dir == DIAGDIR_NW || dir == DIAGDIR_NE);
220 /* Subtraction instead of addition because we are testing for GVF_GOINGUP_BIT.
221 * GVF_GOINGUP_BIT is used because it's bit 0, so simple AND can be used,
222 * without any shift */
223 this->z_pos += HasBit(this->gv_flags, GVF_GOINGUP_BIT) ? d : -d;
224 }
225
226 assert(this->z_pos == GetSlopePixelZ(this->x_pos, this->y_pos, true));
227 }
228
235 inline int UpdateInclination(bool new_tile, bool update_delta)
236 {
237 int old_z = this->z_pos;
238
239 if (new_tile) {
241 } else {
242 this->UpdateZPosition();
243 }
244
245 this->UpdateViewport(true, update_delta);
246 return old_z;
247 }
248
252 inline void SetFrontEngine() { SetBit(this->subtype, GVSF_FRONT); }
253
257 inline void ClearFrontEngine() { ClrBit(this->subtype, GVSF_FRONT); }
258
263
268
272 inline void SetWagon() { SetBit(this->subtype, GVSF_WAGON); }
273
277 inline void ClearWagon() { ClrBit(this->subtype, GVSF_WAGON); }
278
282 inline void SetEngine() { SetBit(this->subtype, GVSF_ENGINE); }
283
287 inline void ClearEngine() { ClrBit(this->subtype, GVSF_ENGINE); }
288
292 inline void SetFreeWagon() { SetBit(this->subtype, GVSF_FREE_WAGON); }
293
297 inline void ClearFreeWagon() { ClrBit(this->subtype, GVSF_FREE_WAGON); }
298
303
308
313 inline bool IsFreeWagon() const { return HasBit(this->subtype, GVSF_FREE_WAGON); }
314
319 inline bool IsEngine() const { return HasBit(this->subtype, GVSF_ENGINE); }
320
325 inline bool IsWagon() const { return HasBit(this->subtype, GVSF_WAGON); }
326
331 inline bool IsMultiheaded() const { return HasBit(this->subtype, GVSF_MULTIHEADED); }
332
337 inline bool IsRearDualheaded() const { return this->IsMultiheaded() && !this->IsEngine(); }
338
344 inline void SetLastSpeed()
345 {
346 if (this->cur_speed != this->gcache.last_speed) {
348 this->gcache.last_speed = this->cur_speed;
349 }
350 }
351
352protected:
366 inline uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
367 {
368 uint spd = this->subspeed + accel;
369 this->subspeed = (uint8_t)spd;
370
371 /* When we are going faster than the maximum speed, reduce the speed
372 * somewhat gradually. But never lower than the maximum speed. */
373 int tempmax = max_speed;
374 if (this->cur_speed > max_speed) {
375 tempmax = std::max(this->cur_speed - (this->cur_speed / 10) - 1, max_speed);
376 }
377
378 /* Enforce a maximum and minimum speed. Normally we would use something like
379 * Clamp for this, but in this case min_speed might be below the maximum speed
380 * threshold for some reason. That makes acceleration fail and assertions
381 * happen in Clamp. So make it explicit that min_speed overrules the maximum
382 * speed by explicit ordering of min and max. */
383 this->cur_speed = spd = std::max(std::min(this->cur_speed + ((int)spd >> 8), tempmax), min_speed);
384
385 int scaled_spd = this->GetAdvanceSpeed(spd);
386
387 scaled_spd += this->progress;
388 this->progress = 0; // set later in *Handler or *Controller
389 return scaled_spd;
390 }
391};
392
393#endif /* GROUND_VEHICLE_HPP */
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
@ DIR_SW
Southwest.
@ DIR_NW
Northwest.
@ DIR_SE
Southeast.
@ DIR_NE
Northeast.
@ AXIS_X
The X axis.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_NE
Northeast, upper right on your monitor.
@ DIAGDIR_NW
Northwest.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
AccelStatus
What is the status of our acceleration?
@ AS_BRAKE
We want to stop.
@ AS_ACCEL
We want to go faster, if possible of course.
GroundVehicleFlags
Ground vehicle flags.
@ GVF_SUPPRESS_IMPLICIT_ORDERS
Disable insertion and removal of automatic orders until the vehicle completes the real order.
@ GVF_GOINGDOWN_BIT
Vehicle is currently going downhill. (Cached track information for acceleration).
@ GVF_GOINGUP_BIT
Vehicle is currently going uphill. (Cached track information for acceleration).
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Functions related to OTTD's landscape.
Cached, frequently calculated values.
uint32_t cached_weight
Total weight of the consist (valid only for the first engine).
uint32_t cached_air_drag
Air drag coefficient of the vehicle (valid only for the first engine).
uint16_t cached_axle_resistance
Resistance caused by the axles of the vehicle (valid only for the first engine).
EngineID first_engine
Cached EngineID of the front vehicle. EngineID::Invalid() for the front vehicle itself.
uint16_t last_speed
The last speed we did display, so we only have to redraw when this changes.
uint32_t cached_power
Total power of the consist (valid only for the first engine).
uint16_t cached_total_length
Length of the whole vehicle (valid only for the first engine).
uint8_t cached_veh_length
Length of this vehicle in units of 1/VEHICLE_LENGTH of normal length. It is cached because this can b...
uint32_t cached_max_te
Maximum tractive effort of consist (valid only for the first engine).
uint16_t cached_max_track_speed
Maximum consist speed (in internal units) limited by track type (valid only for the first engine).
uint32_t cached_slope_resistance
Resistance caused by weight when this vehicle part is at a slope.
bool IsChainInDepot() const override
Check whether the whole vehicle chain is in the depot.
void SetFreeWagon()
Set a vehicle as a free wagon.
int UpdateInclination(bool new_tile, bool update_delta)
Checks if the vehicle is in a slope and sets the required flags in that case.
void ClearWagon()
Clear wagon property.
GroundVehicle(VehicleID index)
The constructor at SpecializedVehicle must be called.
GroundVehicle< T, Type > GroundVehicleBase
Our type.
GroundVehicleCache gcache
Cache of often calculated values.
void CargoChanged()
Recalculates the cached weight of a vehicle and its parts.
void SetMultiheaded()
Set a vehicle as a multiheaded engine.
bool IsEngine() const
Check if a vehicle is an engine (can be first in a consist).
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
bool IsMultiheaded() const
Check if the vehicle is a multiheaded engine.
void SetEngine()
Set engine status.
void SetWagon()
Set a vehicle to be a wagon.
void UpdateZPositionAndInclination()
Updates vehicle's Z position and inclination.
void SetFrontEngine()
Set front engine state.
void ClearFreeWagon()
Clear a vehicle from being a free wagon.
bool IsWagon() const
Check if a vehicle is a wagon.
int64_t GetSlopeResistance() const
Calculates the total slope resistance for this vehicle.
void PowerChanged()
Recalculates the cached total power of a vehicle.
void ClearArticulatedPart()
Clear a vehicle from being an articulated part.
void SetArticulatedPart()
Set a vehicle to be an articulated part.
uint Crash(bool flooded) override
Common code executed for crashed ground vehicles.
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
void ClearEngine()
Clear engine status.
void ClearMultiheaded()
Clear multiheaded engine property.
uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
Update the speed of the vehicle.
int GetAcceleration() const
Calculates the acceleration of the vehicle under its current conditions.
void ClearFrontEngine()
Remove the front engine state.
void UpdateZPosition()
Updates vehicle's Z position.
void SetLastSpeed()
Update the GUI variant of the current speed of the vehicle.
SpecializedVehicle(VehicleID index)
Set vehicle type correctly.
void UpdateViewport(bool force_update, bool update_delta)
Update vehicle sprite- and position caches.
static uint GetAdvanceSpeed(uint speed)
Determines the effective vehicle movement speed.
int32_t z_pos
z coordinate.
Direction direction
facing
virtual uint Crash(bool flooded=false)
Crash the (whole) vehicle chain.
Definition vehicle.cpp:291
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
uint8_t subspeed
fractional speed
int32_t y_pos
y coordinate.
int32_t x_pos
x coordinate.
uint16_t cur_speed
current speed
uint8_t progress
The percentage (if divided by 256) this vehicle already crossed the tile unit.
static constexpr uint TILE_UNIT_MASK
For masking in/out the inner-tile world coordinate units.
Definition tile_type.h:16
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
Base class for all vehicles.
@ GVSF_ARTICULATED_PART
Articulated part of an engine.
@ GVSF_FRONT
Leading engine of a consist.
@ GVSF_MULTIHEADED
Engine is multiheaded (not used for road vehicles).
@ GVSF_FREE_WAGON
First in a wagon chain (in depot) (not used for road vehicles).
@ GVSF_WAGON
Wagon (not used for road vehicles).
@ GVSF_ENGINE
Engine that can be front engine, but might be placed behind another engine (not used for road vehicle...
Functions related to the vehicle's GUIs.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
Types related to the vehicle widgets.
@ WID_VV_START_STOP
Start or stop this vehicle, and show information about the current state.
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, WidgetID widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting).
Definition window.cpp:3204
Window functions not directly related to making/drawing windows.
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers: