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 RoadPathPos
GetPos() const;
119 TileIndex
GetOrderStationLocation(StationID station
);
120 bool FindClosestDepot(TileIndex
*location
, DestinationID
*destination
, bool *reverse
);
124 int GetCurrentMaxSpeed() const;
127 protected: // These functions should not be called outside acceleration code.
130 * Allows to know the power value that this vehicle will use.
131 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
133 inline uint16
GetPower() const
135 /* Power is not added for articulated parts */
136 if (!this->IsArticulatedPart()) {
137 /* Road vehicle power is in units of 10 HP. */
138 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER
, RoadVehInfo(this->engine_type
)->power
);
144 * Returns a value if this articulated part is powered.
145 * @return Zero, because road vehicles don't have powered parts.
147 inline uint16
GetPoweredPartPower(const RoadVehicle
*head
) const
153 * Allows to know the weight value that this vehicle will use.
154 * @return Weight value from the engine in tonnes.
156 inline uint16
GetWeight() const
158 uint16 weight
= (CargoSpec::Get(this->cargo_type
)->weight
* this->cargo
.StoredCount()) / 16;
160 /* Vehicle weight is not added for articulated parts. */
161 if (!this->IsArticulatedPart()) {
162 /* Road vehicle weight is in units of 1/4 t. */
163 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
170 * Allows to know the tractive effort value that this vehicle will use.
171 * @return Tractive effort value from the engine.
173 inline byte
GetTractiveEffort() const
175 /* The tractive effort coefficient is in units of 1/256. */
176 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT
, RoadVehInfo(this->engine_type
)->tractive_effort
);
180 * Gets the area used for calculating air drag.
181 * @return Area of the engine in m^2.
183 inline byte
GetAirDragArea() const
189 * Gets the air drag coefficient of this vehicle.
190 * @return Air drag value from the engine.
192 inline byte
GetAirDrag() const
194 return RoadVehInfo(this->engine_type
)->air_drag
;
198 * Checks the current acceleration status of this vehicle.
199 * @return Acceleration status.
201 inline AccelStatus
GetAccelerationStatus() const
203 return (this->vehstatus
& VS_STOPPED
) ? AS_BRAKE
: AS_ACCEL
;
207 * Calculates the current speed of this vehicle.
208 * @return Current speed in km/h-ish.
210 inline uint16
GetCurrentSpeed() const
212 return this->cur_speed
/ 2;
216 * Returns the rolling friction coefficient of this vehicle.
217 * @return Rolling friction coefficient in [1e-4].
219 inline uint32
GetRollingFriction() const
221 /* Trams have a slightly greater friction coefficient than trains.
222 * The rest of road vehicles have bigger values. */
223 uint32 coeff
= (this->roadtype
== ROADTYPE_TRAM
) ? 40 : 75;
224 /* The friction coefficient increases with speed in a way that
225 * it doubles at 128 km/h, triples at 256 km/h and so on. */
226 return coeff
* (128 + this->GetCurrentSpeed()) / 128;
230 * Allows to know the acceleration type of a vehicle.
231 * @return Zero, road vehicles always use a normal acceleration method.
233 inline int GetAccelerationType() const
239 * Returns the slope steepness used by this vehicle.
240 * @return Slope steepness used by the vehicle.
242 inline uint32
GetSlopeSteepness() const
244 return _settings_game
.vehicle
.roadveh_slope_steepness
;
248 * Gets the maximum speed allowed by the track for this vehicle.
249 * @return Since roads don't limit road vehicle speed, it returns always zero.
251 inline uint16
GetMaxTrackSpeed() const
257 * Checks if the vehicle is at a tile that can be sloped.
258 * @return True if the tile can be sloped.
260 inline bool TileMayHaveSlopedTrack() const
262 TrackStatus ts
= GetTileRoadStatus(this->tile
, this->compatible_roadtypes
);
263 TrackBits trackbits
= TrackStatusToTrackBits(ts
);
265 return trackbits
== TRACK_BIT_X
|| trackbits
== TRACK_BIT_Y
;
269 * Road vehicles have to use GetSlopePixelZ() to compute their height
270 * if they are reversing because in that case, their direction
271 * is not parallel with the road. It is safe to return \c true
272 * even if it is not reversing.
273 * @return are we (possibly) reversing?
275 inline bool HasToUseGetSlopePixelZ()
277 const RoadVehicle
*rv
= this->First();
279 /* Check if this vehicle is in the same direction as the road under.
280 * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
282 if (rv
->state
<= RVSB_TRACKDIR_MASK
&& IsReversingRoadTrackdir((Trackdir
)rv
->state
)) {
283 /* If the first vehicle is reversing, this vehicle may be reversing too
284 * (especially if this is the first, and maybe the only, vehicle).*/
289 /* If any previous vehicle has different direction,
290 * we may be in the middle of reversing. */
291 if (this->direction
!= rv
->direction
) return true;
299 #define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
301 #endif /* ROADVEH_H */