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 <http://www.gnu.org/licenses/>.
8 /** @file src/roadveh.h Road vehicle states */
13 #include "ground_vehicle.hpp"
14 #include "engine_base.h"
15 #include "cargotype.h"
16 #include "track_func.h"
19 #include "newgrf_engine.h"
23 /** Road vehicle states */
24 enum RoadVehicleStates
{
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.
38 RVSB_IN_DEPOT
= 0xFE, ///< The vehicle is in a depot
39 RVSB_WORMHOLE
= 0xFF, ///< The vehicle is in a tunnel and/or bridge
42 RVS_USING_SECOND_BAY
= 1, ///< Only used while in a road stop
43 RVS_ENTERED_STOP
= 2, ///< Only set when a vehicle has entered the stop
44 RVS_DRIVE_SIDE
= 4, ///< Only used when retrieving move data
45 RVS_IN_ROAD_STOP
= 5, ///< The vehicle is in a road stop
46 RVS_IN_DT_ROAD_STOP
= 6, ///< The vehicle is in a drive-through road stop
48 /* Bit sets of the above specified bits */
49 RVSB_IN_ROAD_STOP
= 1 << RVS_IN_ROAD_STOP
, ///< The vehicle is in a road stop
50 RVSB_IN_ROAD_STOP_END
= RVSB_IN_ROAD_STOP
+ TRACKDIR_END
,
51 RVSB_IN_DT_ROAD_STOP
= 1 << RVS_IN_DT_ROAD_STOP
, ///< The vehicle is in a drive-through road stop
52 RVSB_IN_DT_ROAD_STOP_END
= RVSB_IN_DT_ROAD_STOP
+ TRACKDIR_END
,
54 RVSB_DRIVE_SIDE
= 1 << RVS_DRIVE_SIDE
, ///< The vehicle is at the opposite side of the road
56 RVSB_TRACKDIR_MASK
= 0x0F, ///< The mask used to extract track dirs
57 RVSB_ROAD_STOP_TRACKDIR_MASK
= 0x09, ///< Only bits 0 and 3 are used to encode the trackdir for road stops
60 /** State information about the Road Vehicle controller */
61 static const uint RDE_NEXT_TILE
= 0x80; ///< We should enter the next tile
62 static const uint RDE_TURNED
= 0x40; ///< We just finished turning
64 /* Start frames for when a vehicle enters a tile/changes its state.
65 * The start frame is different for vehicles that turned around or
66 * are leaving the depot as the do not start at the edge of the tile.
67 * For trams there are a few different start frames as there are two
68 * places where trams can turn. */
69 static const uint RVC_DEFAULT_START_FRAME
= 0;
70 static const uint RVC_TURN_AROUND_START_FRAME
= 1;
71 static const uint RVC_DEPOT_START_FRAME
= 6;
72 static const uint RVC_START_FRAME_AFTER_LONG_TRAM
= 21;
73 static const uint RVC_TURN_AROUND_START_FRAME_SHORT_TRAM
= 16;
74 /* Stop frame for a vehicle in a drive-through stop */
75 static const uint RVC_DRIVE_THROUGH_STOP_FRAME
= 11;
76 static const uint RVC_DEPOT_STOP_FRAME
= 11;
78 /** The number of ticks a vehicle has for overtaking. */
79 static const uint8_t RV_OVERTAKE_TIMEOUT
= 35;
81 void RoadVehUpdateCache(RoadVehicle
*v
, bool same_length
= false);
82 void GetRoadVehSpriteSize(EngineID engine
, uint
&width
, uint
&height
, int &xoffs
, int &yoffs
, EngineImageType image_type
);
84 struct RoadVehPathCache
{
85 std::deque
<Trackdir
> td
;
86 std::deque
<TileIndex
> tile
;
88 inline bool empty() const { return this->td
.empty(); }
90 inline size_t size() const
92 assert(this->td
.size() == this->tile
.size());
93 return this->td
.size();
104 * Buses, trucks and trams belong to this class.
106 struct RoadVehicle final
: public GroundVehicle
<RoadVehicle
, VEH_ROAD
> {
107 RoadVehPathCache path
; ///< Cached path.
108 uint8_t state
; ///< @see RoadVehicleStates
110 uint16_t blocked_ctr
;
111 uint8_t overtaking
; ///< Set to #RVSB_DRIVE_SIDE when overtaking, otherwise 0.
112 uint8_t overtaking_ctr
; ///< The length of the current overtake attempt.
113 uint16_t crashed_ctr
; ///< Animation counter when the vehicle has crashed. @see RoadVehIsCrashed
116 RoadType roadtype
; ///< NOSAVE: Roadtype of this vehicle.
117 VehicleID disaster_vehicle
= INVALID_VEHICLE
; ///< NOSAVE: Disaster vehicle targetting this vehicle.
118 RoadTypes compatible_roadtypes
; ///< NOSAVE: Roadtypes this consist is powered on.
120 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
121 RoadVehicle() : GroundVehicleBase() {}
122 /** We want to 'destruct' the right class. */
123 virtual ~RoadVehicle() { this->PreDestructor(); }
125 friend struct GroundVehicle
<RoadVehicle
, VEH_ROAD
>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
127 void MarkDirty() override
;
128 void UpdateDeltaXY() override
;
129 ExpensesType
GetExpenseType(bool income
) const override
{ return income
? EXPENSES_ROADVEH_REVENUE
: EXPENSES_ROADVEH_RUN
; }
130 bool IsPrimaryVehicle() const override
{ return this->IsFrontEngine(); }
131 void GetImage(Direction direction
, EngineImageType image_type
, VehicleSpriteSeq
*result
) const override
;
132 int GetDisplaySpeed() const override
{ return this->gcache
.last_speed
/ 2; }
133 int GetDisplayMaxSpeed() const override
{ return this->vcache
.cached_max_speed
/ 2; }
134 Money
GetRunningCost() const override
;
135 int GetDisplayImageWidth(Point
*offset
= nullptr) const;
136 bool IsInDepot() const override
{ return this->state
== RVSB_IN_DEPOT
; }
137 bool Tick() override
;
138 void OnNewCalendarDay() override
;
139 void OnNewEconomyDay() override
;
140 uint
Crash(bool flooded
= false) override
;
141 Trackdir
GetVehicleTrackdir() const override
;
142 TileIndex
GetOrderStationLocation(StationID station
) override
;
143 ClosestDepot
FindClosestDepot() override
;
147 int GetCurrentMaxSpeed() const override
;
149 void SetDestTile(TileIndex tile
) override
;
151 protected: // These functions should not be called outside acceleration code.
154 * Allows to know the power value that this vehicle will use.
155 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
157 inline uint16_t GetPower() const
159 /* Power is not added for articulated parts */
160 if (!this->IsArticulatedPart()) {
161 /* Road vehicle power is in units of 10 HP. */
162 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER
, RoadVehInfo(this->engine_type
)->power
);
168 * Returns a value if this articulated part is powered.
169 * @return Zero, because road vehicles don't have powered parts.
171 inline uint16_t GetPoweredPartPower(const RoadVehicle
*) const
177 * Allows to know the weight value that this vehicle will use.
178 * @return Weight value from the engine in tonnes.
180 inline uint16_t GetWeight() const
182 uint16_t weight
= CargoSpec::Get(this->cargo_type
)->WeightOfNUnits(this->cargo
.StoredCount());
184 /* Vehicle weight is not added for articulated parts. */
185 if (!this->IsArticulatedPart()) {
186 /* Road vehicle weight is in units of 1/4 t. */
187 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
194 * Calculates the weight value that this vehicle will have when fully loaded with its current cargo.
195 * @return Weight value in tonnes.
197 uint16_t GetMaxWeight() const override
;
200 * Allows to know the tractive effort value that this vehicle will use.
201 * @return Tractive effort value from the engine.
203 inline uint8_t GetTractiveEffort() const
205 /* The tractive effort coefficient is in units of 1/256. */
206 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT
, RoadVehInfo(this->engine_type
)->tractive_effort
);
210 * Gets the area used for calculating air drag.
211 * @return Area of the engine in m^2.
213 inline uint8_t GetAirDragArea() const
219 * Gets the air drag coefficient of this vehicle.
220 * @return Air drag value from the engine.
222 inline uint8_t GetAirDrag() const
224 return RoadVehInfo(this->engine_type
)->air_drag
;
228 * Checks the current acceleration status of this vehicle.
229 * @return Acceleration status.
231 inline AccelStatus
GetAccelerationStatus() const
233 return (this->vehstatus
& VS_STOPPED
) ? AS_BRAKE
: AS_ACCEL
;
237 * Calculates the current speed of this vehicle.
238 * @return Current speed in km/h-ish.
240 inline uint16_t GetCurrentSpeed() const
242 return this->cur_speed
/ 2;
246 * Returns the rolling friction coefficient of this vehicle.
247 * @return Rolling friction coefficient in [1e-4].
249 inline uint32_t GetRollingFriction() const
251 /* Trams have a slightly greater friction coefficient than trains.
252 * The rest of road vehicles have bigger values. */
253 uint32_t coeff
= RoadTypeIsTram(this->roadtype
) ? 40 : 75;
254 /* The friction coefficient increases with speed in a way that
255 * it doubles at 128 km/h, triples at 256 km/h and so on. */
256 return coeff
* (128 + this->GetCurrentSpeed()) / 128;
260 * Allows to know the acceleration type of a vehicle.
261 * @return Zero, road vehicles always use a normal acceleration method.
263 inline int GetAccelerationType() const
269 * Returns the slope steepness used by this vehicle.
270 * @return Slope steepness used by the vehicle.
272 inline uint32_t GetSlopeSteepness() const
274 return _settings_game
.vehicle
.roadveh_slope_steepness
;
278 * Gets the maximum speed allowed by the track for this vehicle.
279 * @return Since roads don't limit road vehicle speed, it returns always zero.
281 inline uint16_t GetMaxTrackSpeed() const
283 return GetRoadTypeInfo(GetRoadType(this->tile
, GetRoadTramType(this->roadtype
)))->max_speed
;
287 * Checks if the vehicle is at a tile that can be sloped.
288 * @return True if the tile can be sloped.
290 inline bool TileMayHaveSlopedTrack() const
292 TrackStatus ts
= GetTileTrackStatus(this->tile
, TRANSPORT_ROAD
, GetRoadTramType(this->roadtype
));
293 TrackBits trackbits
= TrackStatusToTrackBits(ts
);
295 return trackbits
== TRACK_BIT_X
|| trackbits
== TRACK_BIT_Y
;
299 * Road vehicles have to use GetSlopePixelZ() to compute their height
300 * if they are reversing because in that case, their direction
301 * is not parallel with the road. It is safe to return \c true
302 * even if it is not reversing.
303 * @return are we (possibly) reversing?
305 inline bool HasToUseGetSlopePixelZ()
307 const RoadVehicle
*rv
= this->First();
309 /* Check if this vehicle is in the same direction as the road under.
310 * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
312 if (rv
->state
<= RVSB_TRACKDIR_MASK
&& IsReversingRoadTrackdir((Trackdir
)rv
->state
)) {
313 /* If the first vehicle is reversing, this vehicle may be reversing too
314 * (especially if this is the first, and maybe the only, vehicle).*/
319 /* If any previous vehicle has different direction,
320 * we may be in the middle of reversing. */
321 if (this->direction
!= rv
->direction
) return true;
329 #endif /* ROADVEH_H */