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 struct RoadDriveEntry
{
67 extern const RoadDriveEntry
* const _road_drive_data
[2][2 * TRACKDIR_END
];
69 /* Start frames for when a vehicle enters a tile/changes its state.
70 * The start frame is different for vehicles that turned around or
71 * are leaving the depot as the do not start at the edge of the tile.
72 * For trams there are a few different start frames as there are two
73 * places where trams can turn. */
74 static const uint RVC_DEFAULT_START_FRAME
= 0;
75 static const uint RVC_SHORT_TURN_START_FRAME
= 16;
76 static const uint RVC_LONG_TURN_START_FRAME
= 0;
77 static const uint RVC_AFTER_TURN_START_FRAME
= 1;
78 static const uint RVC_DEPOT_START_FRAME
= 6;
79 /* Stop frame for a vehicle in a drive-through stop */
80 static const uint RVC_DRIVE_THROUGH_STOP_FRAME
= 11;
81 static const uint RVC_DEPOT_STOP_FRAME
= 11;
83 /** The number of ticks a vehicle has for overtaking. */
84 static const byte RV_OVERTAKE_TIMEOUT
= 35;
86 void RoadVehUpdateCache(RoadVehicle
*v
, bool same_length
= false);
87 void GetRoadVehSpriteSize(EngineID engine
, uint
&width
, uint
&height
, int &xoffs
, int &yoffs
, EngineImageType image_type
);
90 * Buses, trucks and trams belong to this class.
92 struct RoadVehicle FINAL
: public GroundVehicle
<RoadVehicle
, VEH_ROAD
> {
93 byte state
; ///< @see RoadVehicleStates
96 byte overtaking
; ///< Set to 1 when overtaking, otherwise 0.
97 byte overtaking_ctr
; ///< The length of the current overtake attempt.
98 uint16 crashed_ctr
; ///< Animation counter when the vehicle has crashed. @see RoadVehIsCrashed
102 RoadTypes compatible_roadtypes
;
104 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
105 RoadVehicle() : GroundVehicle
<RoadVehicle
, VEH_ROAD
> () {}
106 /** We want to 'destruct' the right class. */
107 virtual ~RoadVehicle() { this->PreDestructor(); }
109 friend struct GroundVehicle
<RoadVehicle
, VEH_ROAD
>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
112 void UpdateDeltaXY(Direction direction
);
113 ExpensesType
GetExpenseType(bool income
) const { return income
? EXPENSES_ROADVEH_INC
: EXPENSES_ROADVEH_RUN
; }
114 bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
115 void GetImage(Direction direction
, EngineImageType image_type
, VehicleSpriteSeq
*result
) const;
116 int GetDisplaySpeed() const { return this->gcache
.last_speed
/ 2; }
117 int GetDisplayMaxSpeed() const { return this->vcache
.cached_max_speed
/ 2; }
118 Money
GetRunningCost() const;
119 int GetDisplayImageWidth(Point
*offset
= NULL
) const;
120 bool IsInDepot() const { return this->state
== RVSB_IN_DEPOT
; }
123 uint
Crash(bool flooded
= false);
124 TileIndex
GetOrderStationLocation(StationID station
);
125 bool FindClosestDepot(TileIndex
*location
, DestinationID
*destination
, bool *reverse
);
129 int GetCurrentMaxSpeed() const;
132 protected: // These functions should not be called outside acceleration code.
135 * Allows to know the power value that this vehicle will use.
136 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
138 inline uint16
GetPower() const
140 /* Power is not added for articulated parts */
141 if (!this->IsArticulatedPart()) {
142 /* Road vehicle power is in units of 10 HP. */
143 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER
, RoadVehInfo(this->engine_type
)->power
);
149 * Returns a value if this articulated part is powered.
150 * @return Zero, because road vehicles don't have powered parts.
152 inline uint16
GetPoweredPartPower(const RoadVehicle
*head
) const
158 * Allows to know the weight value that this vehicle will use.
159 * @return Weight value from the engine in tonnes.
161 inline uint16
GetWeight() const
163 uint16 weight
= (CargoSpec::Get(this->cargo_type
)->weight
* this->cargo
.StoredCount()) / 16;
165 /* Vehicle weight is not added for articulated parts. */
166 if (!this->IsArticulatedPart()) {
167 /* Road vehicle weight is in units of 1/4 t. */
168 weight
+= GetVehicleProperty(this, PROP_ROADVEH_WEIGHT
, RoadVehInfo(this->engine_type
)->weight
) / 4;
175 * Allows to know the tractive effort value that this vehicle will use.
176 * @return Tractive effort value from the engine.
178 inline byte
GetTractiveEffort() const
180 /* The tractive effort coefficient is in units of 1/256. */
181 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT
, RoadVehInfo(this->engine_type
)->tractive_effort
);
185 * Gets the area used for calculating air drag.
186 * @return Area of the engine in m^2.
188 inline byte
GetAirDragArea() const
194 * Gets the air drag coefficient of this vehicle.
195 * @return Air drag value from the engine.
197 inline byte
GetAirDrag() const
199 return RoadVehInfo(this->engine_type
)->air_drag
;
203 * Checks the current acceleration status of this vehicle.
204 * @return Acceleration status.
206 inline AccelStatus
GetAccelerationStatus() const
208 return (this->vehstatus
& VS_STOPPED
) ? AS_BRAKE
: AS_ACCEL
;
212 * Calculates the current speed of this vehicle.
213 * @return Current speed in km/h-ish.
215 inline uint16
GetCurrentSpeed() const
217 return this->cur_speed
/ 2;
221 * Returns the rolling friction coefficient of this vehicle.
222 * @return Rolling friction coefficient in [1e-4].
224 inline uint32
GetRollingFriction() const
226 /* Trams have a slightly greater friction coefficient than trains.
227 * The rest of road vehicles have bigger values. */
228 uint32 coeff
= (this->roadtype
== ROADTYPE_TRAM
) ? 40 : 75;
229 /* The friction coefficient increases with speed in a way that
230 * it doubles at 128 km/h, triples at 256 km/h and so on. */
231 return coeff
* (128 + this->GetCurrentSpeed()) / 128;
235 * Allows to know the acceleration type of a vehicle.
236 * @return Zero, road vehicles always use a normal acceleration method.
238 inline int GetAccelerationType() const
244 * Returns the slope steepness used by this vehicle.
245 * @return Slope steepness used by the vehicle.
247 inline uint32
GetSlopeSteepness() const
249 return _settings_game
.vehicle
.roadveh_slope_steepness
;
253 * Gets the maximum speed allowed by the track for this vehicle.
254 * @return Since roads don't limit road vehicle speed, it returns always zero.
256 inline uint16
GetMaxTrackSpeed() const
262 #define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
264 #endif /* ROADVEH_H */