OpenTTD
roadveh.h
Go to the documentation of this file.
1 /* $Id$ */
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 ROADVEH_H
13 #define ROADVEH_H
14 
15 #include "ground_vehicle.hpp"
16 #include "engine_base.h"
17 #include "cargotype.h"
18 #include "track_func.h"
19 #include "road.h"
20 #include "road_map.h"
21 #include "newgrf_engine.h"
22 #include <deque>
23 
24 struct RoadVehicle;
25 
28  /*
29  * Lower 4 bits are used for vehicle track direction. (Trackdirs)
30  * When in a road stop (bit 5 or bit 6 set) these bits give the
31  * track direction of the entry to the road stop.
32  * As the entry direction will always be a diagonal
33  * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
34  * are needed to hold this direction. Bit 1 is then used to show
35  * that the vehicle is using the second road stop bay.
36  * Bit 2 is then used for drive-through stops to show the vehicle
37  * is stopping at this road stop.
38  */
39 
40  /* Numeric values */
41  RVSB_IN_DEPOT = 0xFE,
42  RVSB_WORMHOLE = 0xFF,
43 
44  /* Bit numbers */
50 
51  /* Bit sets of the above specified bits */
53  RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END,
55  RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END,
56 
58 
61 };
62 
64 static const uint RDE_NEXT_TILE = 0x80;
65 static const uint RDE_TURNED = 0x40;
66 
67 /* Start frames for when a vehicle enters a tile/changes its state.
68  * The start frame is different for vehicles that turned around or
69  * are leaving the depot as the do not start at the edge of the tile.
70  * For trams there are a few different start frames as there are two
71  * places where trams can turn. */
72 static const uint RVC_DEFAULT_START_FRAME = 0;
73 static const uint RVC_TURN_AROUND_START_FRAME = 1;
74 static const uint RVC_DEPOT_START_FRAME = 6;
75 static const uint RVC_START_FRAME_AFTER_LONG_TRAM = 21;
76 static const uint RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16;
77 /* Stop frame for a vehicle in a drive-through stop */
78 static const uint RVC_DRIVE_THROUGH_STOP_FRAME = 11;
79 static const uint RVC_DEPOT_STOP_FRAME = 11;
80 
82 static const byte RV_OVERTAKE_TIMEOUT = 35;
83 
84 void RoadVehUpdateCache(RoadVehicle *v, bool same_length = false);
85 void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
86 
88  std::deque<Trackdir> td;
89  std::deque<TileIndex> tile;
90 
91  inline bool empty() const { return this->td.empty(); }
92 
93  inline size_t size() const
94  {
95  assert(this->td.size() == this->tile.size());
96  return this->td.size();
97  }
98 
99  inline void clear()
100  {
101  this->td.clear();
102  this->tile.clear();
103  }
104 };
105 
109 struct RoadVehicle FINAL : public GroundVehicle<RoadVehicle, VEH_ROAD> {
111  byte state;
112  byte frame;
113  uint16 blocked_ctr;
114  byte overtaking;
116  uint16 crashed_ctr;
117  byte reverse_ctr;
118 
121 
125  virtual ~RoadVehicle() { this->PreDestructor(); }
126 
127  friend struct GroundVehicle<RoadVehicle, VEH_ROAD>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
128 
129  void MarkDirty();
130  void UpdateDeltaXY();
131  ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
132  bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
133  void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const;
134  int GetDisplaySpeed() const { return this->gcache.last_speed / 2; }
135  int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed / 2; }
136  Money GetRunningCost() const;
137  int GetDisplayImageWidth(Point *offset = nullptr) const;
138  bool IsInDepot() const { return this->state == RVSB_IN_DEPOT; }
139  bool Tick();
140  void OnNewDay();
141  uint Crash(bool flooded = false);
143  TileIndex GetOrderStationLocation(StationID station);
144  bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
145 
146  bool IsBus() const;
147 
148  int GetCurrentMaxSpeed() const;
149  int UpdateSpeed();
150  void SetDestTile(TileIndex tile);
151 
152 protected: // These functions should not be called outside acceleration code.
153 
158  inline uint16 GetPower() const
159  {
160  /* Power is not added for articulated parts */
161  if (!this->IsArticulatedPart()) {
162  /* Road vehicle power is in units of 10 HP. */
163  return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER, RoadVehInfo(this->engine_type)->power);
164  }
165  return 0;
166  }
167 
172  inline uint16 GetPoweredPartPower(const RoadVehicle *head) const
173  {
174  return 0;
175  }
176 
181  inline uint16 GetWeight() const
182  {
183  uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.StoredCount()) / 16;
184 
185  /* Vehicle weight is not added for articulated parts. */
186  if (!this->IsArticulatedPart()) {
187  /* Road vehicle weight is in units of 1/4 t. */
188  weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
189  }
190 
191  return weight;
192  }
193 
198  inline byte GetTractiveEffort() const
199  {
200  /* The tractive effort coefficient is in units of 1/256. */
201  return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT, RoadVehInfo(this->engine_type)->tractive_effort);
202  }
203 
208  inline byte GetAirDragArea() const
209  {
210  return 6;
211  }
212 
217  inline byte GetAirDrag() const
218  {
219  return RoadVehInfo(this->engine_type)->air_drag;
220  }
221 
227  {
228  return (this->vehstatus & VS_STOPPED) ? AS_BRAKE : AS_ACCEL;
229  }
230 
235  inline uint16 GetCurrentSpeed() const
236  {
237  return this->cur_speed / 2;
238  }
239 
244  inline uint32 GetRollingFriction() const
245  {
246  /* Trams have a slightly greater friction coefficient than trains.
247  * The rest of road vehicles have bigger values. */
248  uint32 coeff = RoadTypeIsTram(this->roadtype) ? 40 : 75;
249  /* The friction coefficient increases with speed in a way that
250  * it doubles at 128 km/h, triples at 256 km/h and so on. */
251  return coeff * (128 + this->GetCurrentSpeed()) / 128;
252  }
253 
258  inline int GetAccelerationType() const
259  {
260  return 0;
261  }
262 
267  inline uint32 GetSlopeSteepness() const
268  {
270  }
271 
276  inline uint16 GetMaxTrackSpeed() const
277  {
278  return GetRoadTypeInfo(GetRoadType(this->tile, GetRoadTramType(this->roadtype)))->max_speed;
279  }
280 
285  inline bool TileMayHaveSlopedTrack() const
286  {
287  TrackStatus ts = GetTileTrackStatus(this->tile, TRANSPORT_ROAD, GetRoadTramType(this->roadtype));
288  TrackBits trackbits = TrackStatusToTrackBits(ts);
289 
290  return trackbits == TRACK_BIT_X || trackbits == TRACK_BIT_Y;
291  }
292 
301  {
302  const RoadVehicle *rv = this->First();
303 
304  /* Check if this vehicle is in the same direction as the road under.
305  * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
306 
308  /* If the first vehicle is reversing, this vehicle may be reversing too
309  * (especially if this is the first, and maybe the only, vehicle).*/
310  return true;
311  }
312 
313  while (rv != this) {
314  /* If any previous vehicle has different direction,
315  * we may be in the middle of reversing. */
316  if (this->direction != rv->direction) return true;
317  rv = rv->Next();
318  }
319 
320  return false;
321  }
322 };
323 
324 #define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
325 
326 #endif /* ROADVEH_H */
VehicleSettings vehicle
options for vehicles
Vehicle is stopped by the player.
Definition: vehicle_base.h:33
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:309
byte state
Definition: roadveh.h:111
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:81
Functions for NewGRF engines.
virtual void MarkDirty()
Marks the vehicles to be redrawn and updates cached variables.
Definition: vehicle_base.h:364
Direction direction
facing
Definition: vehicle_base.h:271
uint8 weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition: cargotype.h:62
AccelStatus
What is the status of our acceleration?
static const uint RDE_TURNED
We just finished turning.
Definition: roadveh.h:65
static bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition: track_func.h:683
uint16 GetPower() const
Allows to know the power value that this vehicle will use.
Definition: roadveh.h:158
The vehicle is at the opposite side of the road.
Definition: roadveh.h:57
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:41
The vehicle is in a drive-through road stop.
Definition: roadveh.h:49
Map accessors for roads.
AccelStatus GetAccelerationStatus() const
Checks the current acceleration status of this vehicle.
Definition: roadveh.h:226
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:72
uint16 cur_speed
current speed
Definition: vehicle_base.h:293
int GetDisplayMaxSpeed() const
Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
Definition: roadveh.h:135
bool TileMayHaveSlopedTrack() const
Checks if the vehicle is at a tile that can be sloped.
Definition: roadveh.h:285
Road specific functions.
RoadVehicle()
We don&#39;t want GCC to zero our struct! It already is zeroed and has an index!
Definition: roadveh.h:123
X-axis track.
Definition: track_type.h:42
static const uint RDE_NEXT_TILE
State information about the Road Vehicle controller.
Definition: roadveh.h:64
Only used when retrieving move data.
Definition: roadveh.h:47
ExpensesType GetExpenseType(bool income) const
Sets the expense type associated to this vehicle type.
Definition: roadveh.h:131
virtual void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
Definition: vehicle_base.h:442
uint32 GetSlopeSteepness() const
Returns the slope steepness used by this vehicle.
Definition: roadveh.h:267
T * First() const
Get the first vehicle in the chain.
RoadType
The different roadtypes we support.
Definition: road_type.h:27
uint16 GetMaxTrackSpeed() const
Gets the maximum speed allowed by the track for this vehicle.
Definition: roadveh.h:276
byte vehstatus
Status.
Definition: vehicle_base.h:317
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:87
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:366
byte overtaking
Set to RVSB_DRIVE_SIDE when overtaking, otherwise 0.
Definition: roadveh.h:114
uint16 cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
Definition: vehicle_base.h:123
RoadVehicleStates
Road vehicle states.
Definition: roadveh.h:27
Buses, trucks and trams belong to this class.
Definition: roadveh.h:109
RoadType roadtype
Roadtype of this vehicle.
Definition: roadveh.h:119
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:226
The vehicle is in a road stop.
Definition: roadveh.h:48
Used for iterations.
Definition: track_type.h:90
virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
Definition: vehicle_base.h:750
We want to stop.
The vehicle is in a tunnel and/or bridge.
Definition: roadveh.h:42
T * Next() const
Get next vehicle in the chain.
Weight in 1/4 t.
byte GetAirDragArea() const
Gets the area used for calculating air drag.
Definition: roadveh.h:208
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Definition: vehicle_base.h:892
Direction
Defines the 8 directions on the map.
Only used while in a road stop.
Definition: roadveh.h:45
virtual int GetCurrentMaxSpeed() const
Calculates the maximum speed of the vehicle under its current conditions.
Definition: vehicle_base.h:492
The vehicle is in a depot.
Definition: roadveh.h:41
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:305
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:40
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:883
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
uint16 max_speed
Maximum speed for vehicles travelling on this road type.
Definition: road.h:141
virtual void UpdateDeltaXY()
Updates the x and y offsets and the size of the sprite used for this vehicle.
Definition: vehicle_base.h:370
Power in 10 HP.
bool HasToUseGetSlopePixelZ()
Road vehicles have to use GetSlopePixelZ() to compute their height if they are reversing because in t...
Definition: roadveh.h:300
uint16 GetPoweredPartPower(const RoadVehicle *head) const
Returns a value if this articulated part is powered.
Definition: roadveh.h:172
virtual ~RoadVehicle()
We want to &#39;destruct&#39; the right class.
Definition: roadveh.h:125
virtual TileIndex GetOrderStationLocation(StationID station)
Determine the location for the station where the vehicle goes to next.
Definition: vehicle_base.h:740
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:130
byte GetTractiveEffort() const
Allows to know the tractive effort value that this vehicle will use.
Definition: roadveh.h:198
We want to go faster, if possible of course.
uint16 crashed_ctr
Animation counter when the vehicle has crashed.
Definition: roadveh.h:116
byte GetAirDrag() const
Gets the air drag coefficient of this vehicle.
Definition: roadveh.h:217
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:591
bool IsInDepot() const
Check whether the vehicle is in the depot.
Definition: roadveh.h:138
uint16 last_speed
The last speed we did display, so we only have to redraw when this changes.
virtual Money GetRunningCost() const
Gets the running cost of a vehicle.
Definition: vehicle_base.h:498
static const byte RV_OVERTAKE_TIMEOUT
The number of ticks a vehicle has for overtaking.
Definition: roadveh.h:82
The vehicle is in a drive-through road stop.
Definition: roadveh.h:54
uint Crash(bool flooded) override
Common code executed for crashed ground vehicles.
static void OnNewDay()
Runs various procedures that have to be done daily.
Definition: date.cpp:249
Base class for engines.
int GetAccelerationType() const
Allows to know the acceleration type of a vehicle.
Definition: roadveh.h:258
Only bits 0 and 3 are used to encode the trackdir for road stops.
Definition: roadveh.h:60
void RoadVehUpdateCache(RoadVehicle *v, bool same_length=false)
Update the cache of a road vehicle.
uint32 GetRollingFriction() const
Returns the rolling friction coefficient of this vehicle.
Definition: roadveh.h:244
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:23
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:119
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Only set when a vehicle has entered the stop.
Definition: roadveh.h:46
static TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition: track_func.h:373
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).
Transport by road vehicle.
The vehicle is in a road stop.
Definition: roadveh.h:52
RoadTypes compatible_roadtypes
Roadtypes this consist is powered on.
Definition: roadveh.h:120
Types/functions related to cargoes.
Coordinates of a point in 2D.
The mask used to extract track dirs.
Definition: roadveh.h:59
Tractive effort coefficient in 1/256.
virtual Trackdir GetVehicleTrackdir() const
Returns the Trackdir on which the vehicle is currently located.
Definition: vehicle_base.h:554
RoadVehPathCache path
Cached path.
Definition: roadveh.h:110
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:288
virtual bool Tick()
Calls the tick handler of the vehicle.
Definition: vehicle_base.h:528
uint8 roadveh_slope_steepness
Steepness of hills for road vehicles when using realistic acceleration.
Income from road vehicles.
Definition: economy_type.h:159
bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: roadveh.h:132
Different conversion functions from one kind of track to another.
int GetDisplaySpeed() const
Gets the speed in km-ish/h that can be sent into SetDParam for string processing. ...
Definition: roadveh.h:134
Base class for all vehicles that move through ground.
Running costs road vehicles.
Definition: economy_type.h:154
VehicleCache vcache
Cache of often used vehicle values.
Definition: vehicle_base.h:330
ExpensesType
Types of expenses.
Definition: economy_type.h:150
Y-axis track.
Definition: track_type.h:43
Base class and functions for all vehicles that move through ground.
GroundVehicleCache gcache
Cache of often calculated values.
byte overtaking_ctr
The length of the current overtake attempt.
Definition: roadveh.h:115
uint16 GetCurrentSpeed() const
Calculates the current speed of this vehicle.
Definition: roadveh.h:235
uint16 GetWeight() const
Allows to know the weight value that this vehicle will use.
Definition: roadveh.h:181