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"
24 /** Road vehicle states */
25 enum RoadVehicleStates
{
27 * Lower 4 bits are used for vehicle track direction. (Trackdirs)
28 * When in a road stop (bit 5 or bit 6 set) these bits give the
29 * track direction of the entry to the road stop.
30 * As the entry direction will always be a diagonal
31 * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
32 * are needed to hold this direction. Bit 1 is then used to show
33 * that the vehicle is using the second road stop bay.
34 * Bit 2 is then used for drive-through stops to show the vehicle
35 * is stopping at this road stop.
39 RVSB_IN_DEPOT
= 0xFE, ///< The vehicle is in a depot
40 RVSB_WORMHOLE
= 0xFF, ///< The vehicle is in a tunnel and/or bridge
43 RVS_USING_SECOND_BAY
= 1, ///< Only used while in a road stop
44 RVS_ENTERED_STOP
= 2, ///< Only set when a vehicle has entered the stop
45 RVS_DRIVE_SIDE
= 4, ///< Only used when retrieving move data
46 RVS_IN_ROAD_STOP
= 5, ///< The vehicle is in a road stop
47 RVS_IN_DT_ROAD_STOP
= 6, ///< 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_DRIVE_SIDE
= 1 << RVS_DRIVE_SIDE
, ///< The vehicle is at the opposite side of the road
57 RVSB_TRACKDIR_MASK
= 0x0F, ///< The mask used to extract track dirs
58 RVSB_ROAD_STOP_TRACKDIR_MASK
= 0x09, ///< Only bits 0 and 3 are used to encode the trackdir for road stops
61 /** State information about the Road Vehicle controller */
62 static const uint RDE_NEXT_TILE
= 0x80; ///< We should enter the next tile
63 static const uint RDE_TURNED
= 0x40; ///< We just finished turning
65 /* Start frames for when a vehicle enters a tile/changes its state.
66 * The start frame is different for vehicles that turned around or
67 * are leaving the depot as the do not start at the edge of the tile.
68 * For trams there are a few different start frames as there are two
69 * places where trams can turn. */
70 static const uint RVC_DEFAULT_START_FRAME
= 0;
71 static const uint RVC_TURN_AROUND_START_FRAME
= 1;
72 static const uint RVC_DEPOT_START_FRAME
= 6;
73 static const uint RVC_START_FRAME_AFTER_LONG_TRAM
= 21;
74 static const uint RVC_TURN_AROUND_START_FRAME_SHORT_TRAM
= 16;
75 /* Stop frame for a vehicle in a drive-through stop */
76 static const uint RVC_DRIVE_THROUGH_STOP_FRAME
= 11;
77 static const uint RVC_DEPOT_STOP_FRAME
= 11;
79 /** The number of ticks a vehicle has for overtaking. */
80 static const byte RV_OVERTAKE_TIMEOUT
= 35;
82 void RoadVehUpdateCache(RoadVehicle
*v
, bool same_length
= false);
83 void GetRoadVehSpriteSize(EngineID engine
, uint
&width
, uint
&height
, int &xoffs
, int &yoffs
, EngineImageType image_type
);
86 * Buses, trucks and trams belong to this class.
88 struct RoadVehicle FINAL
: public GroundVehicle
<RoadVehicle
, VEH_ROAD
> {
89 byte state
; ///< @see RoadVehicleStates
92 byte overtaking
; ///< Set to #RVSB_DRIVE_SIDE when overtaking, otherwise 0.
93 byte overtaking_ctr
; ///< The length of the current overtake attempt.
94 uint16 crashed_ctr
; ///< Animation counter when the vehicle has crashed. @see RoadVehIsCrashed
98 RoadTypes compatible_roadtypes
;
100 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
101 RoadVehicle() : GroundVehicleBase() {}
102 /** We want to 'destruct' the right class. */
103 virtual ~RoadVehicle() { this->PreDestructor(); }
105 friend struct GroundVehicle
<RoadVehicle
, VEH_ROAD
>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
108 void UpdateDeltaXY(Direction direction
);
109 ExpensesType
GetExpenseType(bool income
) const { return income
? EXPENSES_ROADVEH_INC
: EXPENSES_ROADVEH_RUN
; }
110 bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
111 void GetImage(Direction direction
, EngineImageType image_type
, VehicleSpriteSeq
*result
) const;
112 int GetDisplaySpeed() const { return this->gcache
.last_speed
/ 2; }
113 int GetDisplayMaxSpeed() const { return this->vcache
.cached_max_speed
/ 2; }
114 Money
GetRunningCost() const;
115 int GetDisplayImageWidth(Point
*offset
= NULL
) const;
116 bool IsInDepot() const { return this->state
== RVSB_IN_DEPOT
; }
119 uint
Crash(bool flooded
= false);
120 Trackdir
GetVehicleTrackdir() const;
121 TileIndex
GetOrderStationLocation(StationID station
);
122 bool FindClosestDepot(TileIndex
*location
, DestinationID
*destination
, bool *reverse
);
126 int GetCurrentMaxSpeed() const;
129 protected: // These functions should not be called outside acceleration code.
132 * Allows to know the power value that this vehicle will use.
133 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
135 inline uint16
GetPower() const
137 /* Power is not added for articulated parts */
138 if (!this->IsArticulatedPart()) {
139 /* Road vehicle power is in units of 10 HP. */
140 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER
, RoadVehInfo(this->engine_type
)->power
);
146 * Returns a value if this articulated part is powered.
147 * @return Zero, because road vehicles don't have powered parts.
149 inline uint16
GetPoweredPartPower(const RoadVehicle
*head
) const
155 * Allows to know the weight value that this vehicle will use.
156 * @return Weight value from the engine in tonnes.
158 inline uint16
GetWeight() const
160 uint16 weight
= (CargoSpec::Get(this->cargo_type
)->weight
* this->cargo
.StoredCount()) / 16;
162 /* Vehicle weight is not added for articulated parts. */
163 if (!this->IsArticulatedPart()) {
164 /* Road vehicle weight is in units of 1/4 t. */
165 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
172 * Allows to know the weight value that this vehicle will use.
173 * @return Empty weight value from the engine in tonnes.
175 inline uint16
GetEmptyWeight() const
179 /* Vehicle weight is not added for articulated parts. */
180 if (!this->IsArticulatedPart()) {
181 /* Road vehicle weight is in units of 1/4 t. */
182 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
189 * Allows to know the weight value that this vehicle will use.
190 * @return Loaded weight value from the engine in tonnes.
192 inline uint16
GetLoadedWeight() const
194 uint16 weight
= (CargoSpec::Get(this->cargo_type
)->weight
* this->cargo_cap
) / 16;
196 /* Vehicle weight is not added for articulated parts. */
197 if (!this->IsArticulatedPart()) {
198 /* Road vehicle weight is in units of 1/4 t. */
199 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
206 * Allows to know the tractive effort value that this vehicle will use.
207 * @return Tractive effort value from the engine.
209 inline byte
GetTractiveEffort() const
211 /* The tractive effort coefficient is in units of 1/256. */
212 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT
, RoadVehInfo(this->engine_type
)->tractive_effort
);
216 * Gets the area used for calculating air drag.
217 * @return Area of the engine in m^2.
219 inline byte
GetAirDragArea() const
225 * Gets the air drag coefficient of this vehicle.
226 * @return Air drag value from the engine.
228 inline byte
GetAirDrag() const
230 return RoadVehInfo(this->engine_type
)->air_drag
;
234 * Checks the current acceleration status of this vehicle.
235 * @return Acceleration status.
237 inline AccelStatus
GetAccelerationStatus() const
239 return (this->vehstatus
& VS_STOPPED
) ? AS_BRAKE
: AS_ACCEL
;
243 * Calculates the current speed of this vehicle.
244 * @return Current speed in km/h-ish.
246 inline uint16
GetCurrentSpeed() const
248 return this->cur_speed
/ 2;
252 * Returns the rolling friction coefficient of this vehicle.
253 * @return Rolling friction coefficient in [1e-4].
255 inline uint32
GetRollingFriction() const
257 /* Roughly 1000 * 9.81 * 0.002
258 * 1000 for tonnes to kg
260 * 0.002 for track to wheel friction or
261 * 0.008 for truck wheels on asphalt
263 uint32 coeff
= (this->roadtype
== ROADTYPE_TRAM
) ? 17 : 78;
269 * Allows to know the acceleration type of a vehicle.
270 * @return Zero, road vehicles always use a normal acceleration method.
272 inline int GetAccelerationType() const
278 * Returns the slope steepness used by this vehicle.
279 * @return Slope steepness used by the vehicle.
281 inline uint32
GetSlopeSteepness() const
283 return _settings_game
.vehicle
.roadveh_slope_steepness
;
287 * Gets the maximum speed allowed by the track for this vehicle.
288 * @return Since roads don't limit road vehicle speed, it returns always zero.
290 inline uint16
GetMaxTrackSpeed() const
296 * Checks if the vehicle is at a tile that can be sloped.
297 * @return True if the tile can be sloped.
299 inline bool TileMayHaveSlopedTrack() const
301 TrackStatus ts
= GetTileTrackStatus(this->tile
, TRANSPORT_ROAD
, this->compatible_roadtypes
);
302 TrackBits trackbits
= TrackStatusToTrackBits(ts
);
304 return trackbits
== TRACK_BIT_X
|| trackbits
== TRACK_BIT_Y
;
308 * Road vehicles have to use GetSlopePixelZ() to compute their height
309 * if they are reversing because in that case, their direction
310 * is not parallel with the road. It is safe to return \c true
311 * even if it is not reversing.
312 * @return are we (possibly) reversing?
314 inline bool HasToUseGetSlopePixelZ()
316 const RoadVehicle
*rv
= this->First();
318 /* Check if this vehicle is in the same direction as the road under.
319 * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
321 if (rv
->state
<= RVSB_TRACKDIR_MASK
&& IsReversingRoadTrackdir((Trackdir
)rv
->state
)) {
322 /* If the first vehicle is reversing, this vehicle may be reversing too
323 * (especially if this is the first, and maybe the only, vehicle).*/
328 /* If any previous vehicle has different direction,
329 * we may be in the middle of reversing. */
330 if (this->direction
!= rv
->direction
) return true;
338 #define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
340 #endif /* ROADVEH_H */