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/>.
10 /** @file src/roadveh.h Road vehicle states */
15 #include "ground_vehicle.hpp"
16 #include "engine_base.h"
17 #include "cargotype.h"
18 #include "track_func.h"
19 #include "road_type.h"
20 #include "newgrf_engine.h"
21 #include "pathfinder/pos.h"
25 /** Road vehicle states */
26 enum RoadVehicleStates
{
28 * Lower 4 bits are used for vehicle track direction. (Trackdirs)
29 * When in a road stop (bit 4 or bit 5 set) these bits give the
30 * track direction of the entry to the road stop.
31 * As the entry direction will always be a diagonal
32 * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
33 * are needed to hold this direction. Bit 1 is then used to show
34 * that the vehicle is using the second road stop bay.
35 * Bit 2 is then used for drive-through stops to show the vehicle
36 * is stopping at this road stop.
40 RVSB_IN_DEPOT
= 0xFE, ///< The vehicle is in a depot
41 RVSB_WORMHOLE
= 0xFF, ///< The vehicle is in a tunnel and/or bridge
44 RVS_USING_SECOND_BAY
= 1, ///< Only used while in a road stop
45 RVS_ENTERED_STOP
= 2, ///< Only set when a vehicle has entered the stop
46 RVS_IN_ROAD_STOP
= 4, ///< The vehicle is in a road stop
47 RVS_IN_DT_ROAD_STOP
= 5, ///< The vehicle is in a drive-through road stop
49 /* Bit sets of the above specified bits */
50 RVSB_IN_ROAD_STOP
= 1 << RVS_IN_ROAD_STOP
, ///< The vehicle is in a road stop
51 RVSB_IN_ROAD_STOP_END
= RVSB_IN_ROAD_STOP
+ TRACKDIR_END
,
52 RVSB_IN_DT_ROAD_STOP
= 1 << RVS_IN_DT_ROAD_STOP
, ///< The vehicle is in a drive-through road stop
53 RVSB_IN_DT_ROAD_STOP_END
= RVSB_IN_DT_ROAD_STOP
+ TRACKDIR_END
,
55 RVSB_TRACKDIR_MASK
= 0x0F, ///< The mask used to extract track dirs
56 RVSB_ROAD_STOP_TRACKDIR_MASK
= 0x09, ///< Only bits 0 and 3 are used to encode the trackdir for road stops
59 /** State information about the Road Vehicle controller */
60 static const uint RDE_NEXT_TILE
= 0x80; ///< We should enter the next tile
61 static const uint RDE_TURNED
= 0x40; ///< We just finished turning
63 /* Start frames for when a vehicle enters a tile/changes its state.
64 * The start frame is different for vehicles that turned around or
65 * are leaving the depot as the do not start at the edge of the tile.
66 * For trams there are a few different start frames as there are two
67 * places where trams can turn. */
68 static const uint RVC_DEFAULT_START_FRAME
= 0;
69 static const uint RVC_SHORT_TURN_START_FRAME
= 16;
70 static const uint RVC_LONG_TURN_START_FRAME
= 0;
71 static const uint RVC_AFTER_TURN_START_FRAME
= 1;
72 static const uint RVC_DEPOT_START_FRAME
= 6;
73 /* Stop frame for a vehicle in a drive-through stop */
74 static const uint RVC_DRIVE_THROUGH_STOP_FRAME
= 11;
75 static const uint RVC_DEPOT_STOP_FRAME
= 11;
77 /** The number of ticks a vehicle has for overtaking. */
78 static const byte RV_OVERTAKE_TIMEOUT
= 35;
80 void RoadVehUpdateCache(RoadVehicle
*v
, bool same_length
= false);
81 void GetRoadVehSpriteSize(EngineID engine
, uint
&width
, uint
&height
, int &xoffs
, int &yoffs
, EngineImageType image_type
);
84 * Buses, trucks and trams belong to this class.
86 struct RoadVehicle FINAL
: public GroundVehicle
<RoadVehicle
, VEH_ROAD
> {
87 byte state
; ///< @see RoadVehicleStates
90 byte overtaking
; ///< Set to 1 when overtaking, otherwise 0.
91 byte overtaking_ctr
; ///< The length of the current overtake attempt.
92 uint16 crashed_ctr
; ///< Animation counter when the vehicle has crashed. @see RoadVehIsCrashed
96 RoadTypes compatible_roadtypes
;
98 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
99 RoadVehicle() : GroundVehicleBase() {}
100 /** We want to 'destruct' the right class. */
101 virtual ~RoadVehicle() { this->PreDestructor(); }
103 friend struct GroundVehicle
<RoadVehicle
, VEH_ROAD
>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
106 void UpdateDeltaXY(Direction direction
);
107 ExpensesType
GetExpenseType(bool income
) const { return income
? EXPENSES_ROADVEH_INC
: EXPENSES_ROADVEH_RUN
; }
108 bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
109 SpriteID
GetImage(Direction direction
, EngineImageType image_type
) const;
110 int GetDisplaySpeed() const { return this->gcache
.last_speed
/ 2; }
111 int GetDisplayMaxSpeed() const { return this->vcache
.cached_max_speed
/ 2; }
112 Money
GetRunningCost() const;
113 int GetDisplayImageWidth(Point
*offset
= NULL
) const;
114 bool IsInDepot() const { return this->state
== RVSB_IN_DEPOT
; }
117 uint
Crash(bool flooded
= false);
118 TileIndex
GetOrderStationLocation(StationID station
);
119 bool FindClosestDepot(TileIndex
*location
, DestinationID
*destination
, bool *reverse
);
123 int GetCurrentMaxSpeed() const;
126 protected: // These functions should not be called outside acceleration code.
129 * Allows to know the power value that this vehicle will use.
130 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
132 inline uint16
GetPower() const
134 /* Power is not added for articulated parts */
135 if (!this->IsArticulatedPart()) {
136 /* Road vehicle power is in units of 10 HP. */
137 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER
, RoadVehInfo(this->engine_type
)->power
);
143 * Returns a value if this articulated part is powered.
144 * @return Zero, because road vehicles don't have powered parts.
146 inline uint16
GetPoweredPartPower(const RoadVehicle
*head
) const
152 * Allows to know the weight value that this vehicle will use.
153 * @return Weight value from the engine in tonnes.
155 inline uint16
GetWeight() const
157 uint16 weight
= (CargoSpec::Get(this->cargo_type
)->weight
* this->cargo
.StoredCount()) / 16;
159 /* Vehicle weight is not added for articulated parts. */
160 if (!this->IsArticulatedPart()) {
161 /* Road vehicle weight is in units of 1/4 t. */
162 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
169 * Allows to know the tractive effort value that this vehicle will use.
170 * @return Tractive effort value from the engine.
172 inline byte
GetTractiveEffort() const
174 /* The tractive effort coefficient is in units of 1/256. */
175 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT
, RoadVehInfo(this->engine_type
)->tractive_effort
);
179 * Gets the area used for calculating air drag.
180 * @return Area of the engine in m^2.
182 inline byte
GetAirDragArea() const
188 * Gets the air drag coefficient of this vehicle.
189 * @return Air drag value from the engine.
191 inline byte
GetAirDrag() const
193 return RoadVehInfo(this->engine_type
)->air_drag
;
197 * Checks the current acceleration status of this vehicle.
198 * @return Acceleration status.
200 inline AccelStatus
GetAccelerationStatus() const
202 return (this->vehstatus
& VS_STOPPED
) ? AS_BRAKE
: AS_ACCEL
;
206 * Calculates the current speed of this vehicle.
207 * @return Current speed in km/h-ish.
209 inline uint16
GetCurrentSpeed() const
211 return this->cur_speed
/ 2;
215 * Returns the rolling friction coefficient of this vehicle.
216 * @return Rolling friction coefficient in [1e-4].
218 inline uint32
GetRollingFriction() const
220 /* Trams have a slightly greater friction coefficient than trains.
221 * The rest of road vehicles have bigger values. */
222 uint32 coeff
= (this->roadtype
== ROADTYPE_TRAM
) ? 40 : 75;
223 /* The friction coefficient increases with speed in a way that
224 * it doubles at 128 km/h, triples at 256 km/h and so on. */
225 return coeff
* (128 + this->GetCurrentSpeed()) / 128;
229 * Allows to know the acceleration type of a vehicle.
230 * @return Zero, road vehicles always use a normal acceleration method.
232 inline int GetAccelerationType() const
238 * Returns the slope steepness used by this vehicle.
239 * @return Slope steepness used by the vehicle.
241 inline uint32
GetSlopeSteepness() const
243 return _settings_game
.vehicle
.roadveh_slope_steepness
;
247 * Gets the maximum speed allowed by the track for this vehicle.
248 * @return Since roads don't limit road vehicle speed, it returns always zero.
250 inline uint16
GetMaxTrackSpeed() const
256 * Checks if the vehicle is at a tile that can be sloped.
257 * @return True if the tile can be sloped.
259 inline bool TileMayHaveSlopedTrack() const
261 TrackStatus ts
= GetTileRoadStatus(this->tile
, this->compatible_roadtypes
);
262 TrackBits trackbits
= TrackStatusToTrackBits(ts
);
264 return trackbits
== TRACK_BIT_X
|| trackbits
== TRACK_BIT_Y
;
268 * Road vehicles have to use GetSlopePixelZ() to compute their height
269 * if they are reversing because in that case, their direction
270 * is not parallel with the road. It is safe to return \c true
271 * even if it is not reversing.
272 * @return are we (possibly) reversing?
274 inline bool HasToUseGetSlopePixelZ()
276 const RoadVehicle
*rv
= this->First();
278 /* Check if this vehicle is in the same direction as the road under.
279 * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
281 if (rv
->state
<= RVSB_TRACKDIR_MASK
&& IsReversingRoadTrackdir((Trackdir
)rv
->state
)) {
282 /* If the first vehicle is reversing, this vehicle may be reversing too
283 * (especially if this is the first, and maybe the only, vehicle).*/
288 /* If any previous vehicle has different direction,
289 * we may be in the middle of reversing. */
290 if (this->direction
!= rv
->direction
) return true;
298 #define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
300 #endif /* ROADVEH_H */