OpenTTD Source 20260311-master-g511d3794ce
roadveh.h
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 ROADVEH_H
11#define ROADVEH_H
12
13#include "ground_vehicle.hpp"
14#include "engine_base.h"
15#include "cargotype.h"
16#include "track_func.h"
17#include "road.h"
18#include "road_map.h"
19#include "newgrf_engine.h"
20
21struct RoadVehicle;
22
24enum RoadVehicleStates : uint8_t {
25 /*
26 * Lower 4 bits are used for vehicle track direction. (Trackdirs)
27 * When in a road stop (bit 5 or bit 6 set) these bits give the
28 * track direction of the entry to the road stop.
29 * As the entry direction will always be a diagonal
30 * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
31 * are needed to hold this direction. Bit 1 is then used to show
32 * that the vehicle is using the second road stop bay.
33 * Bit 2 is then used for drive-through stops to show the vehicle
34 * is stopping at this road stop.
35 */
36
37 /* Numeric values */
40
41 /* Bit numbers */
47
48 /* Bit sets of the above specified bits */
50 RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END,
52 RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END,
53
55
58};
59
62static const uint RDE_NEXT_TILE = 0x80;
63static const uint RDE_TURNED = 0x40;
65
74static const uint RVC_DEFAULT_START_FRAME = 0;
75static const uint RVC_TURN_AROUND_START_FRAME = 1;
76static const uint RVC_DEPOT_START_FRAME = 6;
77static const uint RVC_START_FRAME_AFTER_LONG_TRAM = 21;
78static const uint RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16;
80
82static const uint RVC_DRIVE_THROUGH_STOP_FRAME = 11;
83static const uint RVC_DEPOT_STOP_FRAME = 11;
84
86static const uint8_t RV_OVERTAKE_TIMEOUT = 35;
87
88void RoadVehUpdateCache(RoadVehicle *v, bool same_length = false);
89void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
90
92struct RoadVehPathElement {
95
96 constexpr RoadVehPathElement() {}
98};
99
100using RoadVehPathCache = std::vector<RoadVehPathElement>;
101
105struct RoadVehicle final : public GroundVehicle<RoadVehicle, VEH_ROAD> {
106 RoadVehPathCache path{};
107 uint8_t state = 0;
108 uint8_t frame = 0;
109 uint16_t blocked_ctr = 0;
110 uint8_t overtaking = 0;
111 uint8_t overtaking_ctr = 0;
112 uint16_t crashed_ctr = 0;
113 uint8_t reverse_ctr = 0;
114
116 VehicleID disaster_vehicle = VehicleID::Invalid();
118
119 RoadVehicle(VehicleID index) : GroundVehicleBase(index) {}
121 ~RoadVehicle() override { this->PreDestructor(); }
122
123 friend struct GroundVehicle<RoadVehicle, VEH_ROAD>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
124
125 void MarkDirty() override;
126 void UpdateDeltaXY() override;
127 ExpensesType GetExpenseType(bool income) const override { return income ? EXPENSES_ROADVEH_REVENUE : EXPENSES_ROADVEH_RUN; }
128 bool IsPrimaryVehicle() const override { return this->IsFrontEngine(); }
129 void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override;
130 int GetDisplaySpeed() const override { return this->gcache.last_speed / 2; }
131 int GetDisplayMaxSpeed() const override { return this->vcache.cached_max_speed / 2; }
132 Money GetRunningCost() const override;
133 int GetDisplayImageWidth(Point *offset = nullptr) const;
134 bool IsInDepot() const override { return this->state == RVSB_IN_DEPOT; }
135 bool Tick() override;
136 void OnNewCalendarDay() override;
137 void OnNewEconomyDay() override;
138 uint Crash(bool flooded = false) override;
139 Trackdir GetVehicleTrackdir() const override;
140 TileIndex GetOrderStationLocation(StationID station) override;
142
143 bool IsBus() const;
144
145 int GetCurrentMaxSpeed() const override;
146 int UpdateSpeed();
147 void SetDestTile(TileIndex tile) override;
148
149protected: // These functions should not be called outside acceleration code.
150
155 inline uint16_t GetPower() const
156 {
157 /* Power is not added for articulated parts */
158 if (!this->IsArticulatedPart()) {
159 /* Road vehicle power is in units of 10 HP. */
160 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER, RoadVehInfo(this->engine_type)->power);
161 }
162 return 0;
163 }
164
169 inline uint16_t GetPoweredPartPower() const
170 {
171 return 0;
172 }
173
178 inline uint16_t GetWeight() const
179 {
180 uint16_t weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnits(this->cargo.StoredCount());
181
182 /* Vehicle weight is not added for articulated parts. */
183 if (!this->IsArticulatedPart()) {
184 /* Road vehicle weight is in units of 1/4 t. */
185 weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
186 }
187
188 return weight;
189 }
190
195 uint16_t GetMaxWeight() const override;
196
201 inline uint8_t GetTractiveEffort() const
202 {
203 /* The tractive effort coefficient is in units of 1/256. */
204 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT, RoadVehInfo(this->engine_type)->tractive_effort);
205 }
206
211 inline uint8_t GetAirDragArea() const
212 {
213 return 6;
214 }
215
220 inline uint8_t GetAirDrag() const
221 {
222 return RoadVehInfo(this->engine_type)->air_drag;
223 }
224
230 {
231 return this->vehstatus.Test(VehState::Stopped) ? AS_BRAKE : AS_ACCEL;
232 }
233
238 inline uint16_t GetCurrentSpeed() const
239 {
240 return this->cur_speed / 2;
241 }
242
247 inline uint32_t GetRollingFriction() const
248 {
249 /* Trams have a slightly greater friction coefficient than trains.
250 * The rest of road vehicles have bigger values. */
251 uint32_t coeff = RoadTypeIsTram(this->roadtype) ? 40 : 75;
252 /* The friction coefficient increases with speed in a way that
253 * it doubles at 128 km/h, triples at 256 km/h and so on. */
254 return coeff * (128 + this->GetCurrentSpeed()) / 128;
255 }
256
265
270 inline uint32_t GetSlopeSteepness() const
271 {
272 return _settings_game.vehicle.roadveh_slope_steepness;
273 }
274
279 inline uint16_t GetMaxTrackSpeed() const
280 {
281 return GetRoadTypeInfo(GetRoadType(this->tile, GetRoadTramType(this->roadtype)))->max_speed;
282 }
283
288 inline bool TileMayHaveSlopedTrack() const
289 {
290 TrackStatus ts = GetTileTrackStatus(this->tile, TRANSPORT_ROAD, GetRoadTramType(this->roadtype));
291 TrackBits trackbits = TrackStatusToTrackBits(ts);
292
293 return trackbits == TRACK_BIT_X || trackbits == TRACK_BIT_Y;
294 }
295
304 {
305 const RoadVehicle *rv = this->First();
306
307 /* Check if this vehicle is in the same direction as the road under.
308 * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
309
311 /* If the first vehicle is reversing, this vehicle may be reversing too
312 * (especially if this is the first, and maybe the only, vehicle).*/
313 return true;
314 }
315
316 while (rv != this) {
317 /* If any previous vehicle has different direction,
318 * we may be in the middle of reversing. */
319 if (this->direction != rv->direction) return true;
320 rv = rv->Next();
321 }
322
323 return false;
324 }
325};
326
327#endif /* ROADVEH_H */
Types/functions related to cargoes.
uint16_t max_speed
Maximum speed for vehicles travelling on this road type.
Definition road.h:116
Direction
Defines the 8 directions on the map.
ExpensesType
Types of expenses.
@ EXPENSES_ROADVEH_RUN
Running costs road vehicles.
@ EXPENSES_ROADVEH_REVENUE
Revenue from road vehicles.
Base class for engines.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
VehicleAccelerationModel
Acceleration model of a vehicle.
Definition engine_type.h:47
@ Normal
Default acceleration model.
Definition engine_type.h:48
Base class and functions for all vehicles that move through ground.
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.
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
#define Point
Macro that prevents name conflicts between included headers.
Functions for NewGRF engines.
@ PROP_ROADVEH_WEIGHT
Weight in 1/4 t.
@ PROP_ROADVEH_TRACTIVE_EFFORT
Tractive effort coefficient in 1/256.
@ PROP_ROADVEH_POWER
Power in 10 HP.
Road specific functions.
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:215
Map accessors for roads.
RoadType GetRoadType(Tile t, RoadTramType rtt)
Get the road type for the given RoadTramType.
Definition road_map.h:175
RoadType
The different roadtypes we support.
Definition road_type.h:23
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:28
RoadVehicleStates
Road vehicle states.
Definition roadveh.h:24
@ RVSB_IN_DT_ROAD_STOP
The vehicle is in a drive-through road stop.
Definition roadveh.h:51
@ RVS_ENTERED_STOP
Only set when a vehicle has entered the stop.
Definition roadveh.h:43
@ RVSB_IN_ROAD_STOP
The vehicle is in a road stop.
Definition roadveh.h:49
@ RVS_USING_SECOND_BAY
Only used while in a road stop.
Definition roadveh.h:42
@ RVSB_ROAD_STOP_TRACKDIR_MASK
Only bits 0 and 3 are used to encode the trackdir for road stops.
Definition roadveh.h:57
@ RVS_IN_DT_ROAD_STOP
The vehicle is in a drive-through road stop.
Definition roadveh.h:46
@ RVSB_TRACKDIR_MASK
The mask used to extract track dirs.
Definition roadveh.h:56
@ RVSB_DRIVE_SIDE
The vehicle is at the opposite side of the road.
Definition roadveh.h:54
@ RVS_IN_ROAD_STOP
The vehicle is in a road stop.
Definition roadveh.h:45
@ RVSB_IN_DEPOT
The vehicle is in a depot.
Definition roadveh.h:38
@ RVSB_WORMHOLE
The vehicle is in a tunnel and/or bridge.
Definition roadveh.h:39
@ RVS_DRIVE_SIDE
Only used when retrieving move data.
Definition roadveh.h:44
static const uint RDE_TURNED
We just finished turning.
Definition roadveh.h:63
static const uint RVC_DRIVE_THROUGH_STOP_FRAME
Stop frame for a vehicle in a drive-through stop.
Definition roadveh.h:82
static const uint8_t RV_OVERTAKE_TIMEOUT
The number of ticks a vehicle has for overtaking.
Definition roadveh.h:86
void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
Get the size of the sprite of a road vehicle sprite heading west (used for lists).
void RoadVehUpdateCache(RoadVehicle *v, bool same_length=false)
Update the cache of a road vehicle.
static const uint RDE_NEXT_TILE
We should enter the next tile.
Definition roadveh.h:62
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:138
Structure to return information about the closest depot location, and whether it could be found.
Base class for all vehicles that move through ground.
GroundVehicle< RoadVehicle, Type > GroundVehicleBase
Element of the RoadVehPathCache.
Definition roadveh.h:92
Trackdir trackdir
Trackdir for this element.
Definition roadveh.h:93
TileIndex tile
Tile for this element.
Definition roadveh.h:94
Buses, trucks and trams belong to this class.
Definition roadveh.h:105
uint Crash(bool flooded=false) override
Common code executed for crashed ground vehicles.
uint8_t GetAirDragArea() const
Gets the area used for calculating air drag.
Definition roadveh.h:211
TileIndex GetOrderStationLocation(StationID station) override
Determine the location for the station where the vehicle goes to next.
void OnNewEconomyDay() override
Economy day handler.
uint8_t state
Definition roadveh.h:107
int GetDisplaySpeed() const override
Gets the speed in km-ish/h that can be sent into string parameters for string processing.
Definition roadveh.h:130
void SetDestTile(TileIndex tile) override
Set the destination of this vehicle.
VehicleAccelerationModel GetAccelerationType() const
Allows to know the acceleration type of a vehicle.
Definition roadveh.h:261
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
uint32_t GetSlopeSteepness() const
Returns the slope steepness used by this vehicle.
Definition roadveh.h:270
uint16_t GetPower() const
Allows to know the power value that this vehicle will use.
Definition roadveh.h:155
Money GetRunningCost() const override
Gets the running cost of a vehicle.
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition roadveh.h:128
uint16_t GetMaxWeight() const override
Calculates the weight value that this vehicle will have when fully loaded with its current cargo.
uint16_t GetCurrentSpeed() const
Calculates the current speed of this vehicle.
Definition roadveh.h:238
RoadTypes compatible_roadtypes
NOSAVE: Roadtypes this consist is powered on.
Definition roadveh.h:117
ExpensesType GetExpenseType(bool income) const override
Sets the expense type associated to this vehicle type.
Definition roadveh.h:127
AccelStatus GetAccelerationStatus() const
Checks the current acceleration status of this vehicle.
Definition roadveh.h:229
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
uint16_t crashed_ctr
Animation counter when the vehicle has crashed.
Definition roadveh.h:112
bool IsBus() const
Check whether a roadvehicle is a bus.
uint8_t overtaking_ctr
The length of the current overtake attempt.
Definition roadveh.h:111
void OnNewCalendarDay() override
Calander day handler.
uint16_t GetMaxTrackSpeed() const
Gets the maximum speed allowed by the track for this vehicle.
Definition roadveh.h:279
uint32_t GetRollingFriction() const
Returns the rolling friction coefficient of this vehicle.
Definition roadveh.h:247
uint8_t GetTractiveEffort() const
Allows to know the tractive effort value that this vehicle will use.
Definition roadveh.h:201
bool IsInDepot() const override
Check whether the vehicle is in the depot.
Definition roadveh.h:134
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override
Gets the sprite to show for the given direction.
int GetDisplayMaxSpeed() const override
Gets the maximum speed in km-ish/h that can be sent into string parameters for string processing.
Definition roadveh.h:131
RoadVehPathCache path
Cached path.
Definition roadveh.h:106
Trackdir GetVehicleTrackdir() const override
Returns the Trackdir on which the vehicle is currently located.
uint16_t GetPoweredPartPower() const
Returns a value if this articulated part is powered.
Definition roadveh.h:169
bool TileMayHaveSlopedTrack() const
Checks if the vehicle is at a tile that can be sloped.
Definition roadveh.h:288
int GetCurrentMaxSpeed() const override
Calculates the maximum speed of the vehicle under its current conditions.
bool HasToUseGetSlopePixelZ()
Road vehicles have to use GetSlopePixelZ() to compute their height if they are reversing because in t...
Definition roadveh.h:303
RoadType roadtype
NOSAVE: Roadtype of this vehicle.
Definition roadveh.h:115
uint8_t overtaking
Set to RVSB_DRIVE_SIDE when overtaking, otherwise 0.
Definition roadveh.h:110
int UpdateSpeed()
This function looks at the vehicle and updates its speed (cur_speed and subspeed) variables.
bool Tick() override
Calls the tick handler of the vehicle.
VehicleID disaster_vehicle
NOSAVE: Disaster vehicle targetting this vehicle.
Definition roadveh.h:116
ClosestDepot FindClosestDepot() override
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
uint16_t GetWeight() const
Allows to know the weight value that this vehicle will use.
Definition roadveh.h:178
~RoadVehicle() override
We want to 'destruct' the right class.
Definition roadveh.h:121
void MarkDirty() override
Marks the vehicles to be redrawn and updates cached variables.
uint8_t GetAirDrag() const
Gets the air drag coefficient of this vehicle.
Definition roadveh.h:220
T * Next() const
Get next vehicle in the chain.
Sprite sequence for a vehicle part.
EngineID engine_type
The type of engine used for this vehicle.
Direction direction
facing
VehicleCargoList cargo
The cargo this vehicle is carrying.
VehStates vehstatus
Status.
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
CargoType cargo_type
type of cargo this vehicle is carrying
void PreDestructor()
Destroy all stuff that (still) needs the virtual functions to work properly.
Definition vehicle.cpp:825
VehicleCache vcache
Cache of often used vehicle values.
uint16_t cur_speed
current speed
bool IsFrontEngine() const
Check if the vehicle is a front engine.
TileIndex tile
Current tile index.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Different conversion functions from one kind of track to another.
bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition track_func.h:678
TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition track_func.h:365
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
@ TRACK_BIT_Y
Y-axis track.
Definition track_type.h:38
@ TRACK_BIT_X
X-axis track.
Definition track_type.h:37
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
@ TRACKDIR_END
Used for iterations.
Definition track_type.h:84
@ TRANSPORT_ROAD
Transport by road vehicle.
@ Stopped
Vehicle is stopped by the player.
EngineImageType
Visualisation contexts of vehicles and engines.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
@ VEH_ROAD
Road vehicle type.