Codechange: Use functor for Kdtree's XYFunc. (#13074)
[openttd-github.git] / src / roadveh.h
blob90790b9b51b02e0645f1c6db51a5779873f28e0a
1 /*
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/>.
6 */
8 /** @file src/roadveh.h Road vehicle states */
10 #ifndef ROADVEH_H
11 #define ROADVEH_H
13 #include "ground_vehicle.hpp"
14 #include "engine_base.h"
15 #include "cargotype.h"
16 #include "track_func.h"
17 #include "road.h"
18 #include "road_map.h"
19 #include "newgrf_engine.h"
21 struct RoadVehicle;
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.
37 /* Numeric values */
38 RVSB_IN_DEPOT = 0xFE, ///< The vehicle is in a depot
39 RVSB_WORMHOLE = 0xFF, ///< The vehicle is in a tunnel and/or bridge
41 /* Bit numbers */
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 /** Element of the RoadVehPathCache. */
85 struct RoadVehPathElement {
86 Trackdir trackdir; ///< Trackdir for this element.
87 TileIndex tile; ///< Tile for this element.
89 constexpr RoadVehPathElement() : trackdir(INVALID_TRACKDIR), tile(INVALID_TILE) {}
90 constexpr RoadVehPathElement(Trackdir trackdir, TileIndex tile) : trackdir(trackdir), tile(tile) {}
93 using RoadVehPathCache = std::vector<RoadVehPathElement>;
95 /**
96 * Buses, trucks and trams belong to this class.
98 struct RoadVehicle final : public GroundVehicle<RoadVehicle, VEH_ROAD> {
99 RoadVehPathCache path; ///< Cached path.
100 uint8_t state; ///< @see RoadVehicleStates
101 uint8_t frame;
102 uint16_t blocked_ctr;
103 uint8_t overtaking; ///< Set to #RVSB_DRIVE_SIDE when overtaking, otherwise 0.
104 uint8_t overtaking_ctr; ///< The length of the current overtake attempt.
105 uint16_t crashed_ctr; ///< Animation counter when the vehicle has crashed. @see RoadVehIsCrashed
106 uint8_t reverse_ctr;
108 RoadType roadtype; ///< NOSAVE: Roadtype of this vehicle.
109 VehicleID disaster_vehicle = INVALID_VEHICLE; ///< NOSAVE: Disaster vehicle targetting this vehicle.
110 RoadTypes compatible_roadtypes; ///< NOSAVE: Roadtypes this consist is powered on.
112 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
113 RoadVehicle() : GroundVehicleBase() {}
114 /** We want to 'destruct' the right class. */
115 virtual ~RoadVehicle() { this->PreDestructor(); }
117 friend struct GroundVehicle<RoadVehicle, VEH_ROAD>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
119 void MarkDirty() override;
120 void UpdateDeltaXY() override;
121 ExpensesType GetExpenseType(bool income) const override { return income ? EXPENSES_ROADVEH_REVENUE : EXPENSES_ROADVEH_RUN; }
122 bool IsPrimaryVehicle() const override { return this->IsFrontEngine(); }
123 void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override;
124 int GetDisplaySpeed() const override { return this->gcache.last_speed / 2; }
125 int GetDisplayMaxSpeed() const override { return this->vcache.cached_max_speed / 2; }
126 Money GetRunningCost() const override;
127 int GetDisplayImageWidth(Point *offset = nullptr) const;
128 bool IsInDepot() const override { return this->state == RVSB_IN_DEPOT; }
129 bool Tick() override;
130 void OnNewCalendarDay() override;
131 void OnNewEconomyDay() override;
132 uint Crash(bool flooded = false) override;
133 Trackdir GetVehicleTrackdir() const override;
134 TileIndex GetOrderStationLocation(StationID station) override;
135 ClosestDepot FindClosestDepot() override;
137 bool IsBus() const;
139 int GetCurrentMaxSpeed() const override;
140 int UpdateSpeed();
141 void SetDestTile(TileIndex tile) override;
143 protected: // These functions should not be called outside acceleration code.
146 * Allows to know the power value that this vehicle will use.
147 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
149 inline uint16_t GetPower() const
151 /* Power is not added for articulated parts */
152 if (!this->IsArticulatedPart()) {
153 /* Road vehicle power is in units of 10 HP. */
154 return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER, RoadVehInfo(this->engine_type)->power);
156 return 0;
160 * Returns a value if this articulated part is powered.
161 * @return Zero, because road vehicles don't have powered parts.
163 inline uint16_t GetPoweredPartPower(const RoadVehicle *) const
165 return 0;
169 * Allows to know the weight value that this vehicle will use.
170 * @return Weight value from the engine in tonnes.
172 inline uint16_t GetWeight() const
174 uint16_t weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnits(this->cargo.StoredCount());
176 /* Vehicle weight is not added for articulated parts. */
177 if (!this->IsArticulatedPart()) {
178 /* Road vehicle weight is in units of 1/4 t. */
179 weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
182 return weight;
186 * Calculates the weight value that this vehicle will have when fully loaded with its current cargo.
187 * @return Weight value in tonnes.
189 uint16_t GetMaxWeight() const override;
192 * Allows to know the tractive effort value that this vehicle will use.
193 * @return Tractive effort value from the engine.
195 inline uint8_t GetTractiveEffort() const
197 /* The tractive effort coefficient is in units of 1/256. */
198 return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT, RoadVehInfo(this->engine_type)->tractive_effort);
202 * Gets the area used for calculating air drag.
203 * @return Area of the engine in m^2.
205 inline uint8_t GetAirDragArea() const
207 return 6;
211 * Gets the air drag coefficient of this vehicle.
212 * @return Air drag value from the engine.
214 inline uint8_t GetAirDrag() const
216 return RoadVehInfo(this->engine_type)->air_drag;
220 * Checks the current acceleration status of this vehicle.
221 * @return Acceleration status.
223 inline AccelStatus GetAccelerationStatus() const
225 return (this->vehstatus & VS_STOPPED) ? AS_BRAKE : AS_ACCEL;
229 * Calculates the current speed of this vehicle.
230 * @return Current speed in km/h-ish.
232 inline uint16_t GetCurrentSpeed() const
234 return this->cur_speed / 2;
238 * Returns the rolling friction coefficient of this vehicle.
239 * @return Rolling friction coefficient in [1e-4].
241 inline uint32_t GetRollingFriction() const
243 /* Trams have a slightly greater friction coefficient than trains.
244 * The rest of road vehicles have bigger values. */
245 uint32_t coeff = RoadTypeIsTram(this->roadtype) ? 40 : 75;
246 /* The friction coefficient increases with speed in a way that
247 * it doubles at 128 km/h, triples at 256 km/h and so on. */
248 return coeff * (128 + this->GetCurrentSpeed()) / 128;
252 * Allows to know the acceleration type of a vehicle.
253 * @return Zero, road vehicles always use a normal acceleration method.
255 inline int GetAccelerationType() const
257 return 0;
261 * Returns the slope steepness used by this vehicle.
262 * @return Slope steepness used by the vehicle.
264 inline uint32_t GetSlopeSteepness() const
266 return _settings_game.vehicle.roadveh_slope_steepness;
270 * Gets the maximum speed allowed by the track for this vehicle.
271 * @return Since roads don't limit road vehicle speed, it returns always zero.
273 inline uint16_t GetMaxTrackSpeed() const
275 return GetRoadTypeInfo(GetRoadType(this->tile, GetRoadTramType(this->roadtype)))->max_speed;
279 * Checks if the vehicle is at a tile that can be sloped.
280 * @return True if the tile can be sloped.
282 inline bool TileMayHaveSlopedTrack() const
284 TrackStatus ts = GetTileTrackStatus(this->tile, TRANSPORT_ROAD, GetRoadTramType(this->roadtype));
285 TrackBits trackbits = TrackStatusToTrackBits(ts);
287 return trackbits == TRACK_BIT_X || trackbits == TRACK_BIT_Y;
291 * Road vehicles have to use GetSlopePixelZ() to compute their height
292 * if they are reversing because in that case, their direction
293 * is not parallel with the road. It is safe to return \c true
294 * even if it is not reversing.
295 * @return are we (possibly) reversing?
297 inline bool HasToUseGetSlopePixelZ()
299 const RoadVehicle *rv = this->First();
301 /* Check if this vehicle is in the same direction as the road under.
302 * We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
304 if (rv->state <= RVSB_TRACKDIR_MASK && IsReversingRoadTrackdir((Trackdir)rv->state)) {
305 /* If the first vehicle is reversing, this vehicle may be reversing too
306 * (especially if this is the first, and maybe the only, vehicle).*/
307 return true;
310 while (rv != this) {
311 /* If any previous vehicle has different direction,
312 * we may be in the middle of reversing. */
313 if (this->direction != rv->direction) return true;
314 rv = rv->Next();
317 return false;
321 #endif /* ROADVEH_H */