Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / aircraft.h
bloba6b747d5cf3e25c6b7d48542bb1fc597190f0509
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 aircraft.h Base for aircraft. */
10 #ifndef AIRCRAFT_H
11 #define AIRCRAFT_H
13 #include "station_map.h"
14 #include "vehicle_base.h"
16 /**
17 * Base values for flight levels above ground level for 'normal' flight and holding patterns.
18 * Due to speed and direction, the actual flight level may be higher.
20 static constexpr int AIRCRAFT_MIN_FLYING_ALTITUDE = 120; ///< Minimum flying altitude above tile.
21 static constexpr int AIRCRAFT_MAX_FLYING_ALTITUDE = 360; ///< Maximum flying altitude above tile.
22 static constexpr int PLANE_HOLD_MAX_FLYING_ALTITUDE = 150; ///< holding flying altitude above tile of planes.
23 static constexpr int HELICOPTER_HOLD_MAX_FLYING_ALTITUDE = 184; ///< holding flying altitude above tile of helicopters.
25 struct Aircraft;
27 /** An aircraft can be one of those types. */
28 enum AircraftSubType {
29 AIR_HELICOPTER = 0, ///< an helicopter
30 AIR_AIRCRAFT = 2, ///< an airplane
31 AIR_SHADOW = 4, ///< shadow of the aircraft
32 AIR_ROTOR = 6, ///< rotor of an helicopter
35 /** Flags for air vehicles; shared with disaster vehicles. */
36 enum AirVehicleFlags {
37 VAF_DEST_TOO_FAR = 0, ///< Next destination is too far away.
39 /* The next two flags are to prevent stair climbing of the aircraft. The idea is that the aircraft
40 * will ascend or descend multiple flight levels at a time instead of following the contours of the
41 * landscape at a fixed altitude. This only has effect when there are more than 15 height levels. */
42 VAF_IN_MAX_HEIGHT_CORRECTION = 1, ///< The vehicle is currently lowering its altitude because it hit the upper bound.
43 VAF_IN_MIN_HEIGHT_CORRECTION = 2, ///< The vehicle is currently raising its altitude because it hit the lower bound.
45 VAF_HELI_DIRECT_DESCENT = 3, ///< The helicopter is descending directly at its destination (helipad or in front of hangar)
48 static const int ROTOR_Z_OFFSET = 5; ///< Z Offset between helicopter- and rotorsprite.
50 void HandleAircraftEnterHangar(Aircraft *v);
51 void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
52 void UpdateAirplanesOnNewStation(const Station *st);
53 void UpdateAircraftCache(Aircraft *v, bool update_range = false);
55 void AircraftLeaveHangar(Aircraft *v, Direction exit_dir);
56 void AircraftNextAirportPos_and_Order(Aircraft *v);
57 void SetAircraftPosition(Aircraft *v, int x, int y, int z);
59 void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max);
60 template <class T>
61 int GetAircraftFlightLevel(T *v, bool takeoff = false);
63 /** Variables that are cached to improve performance and such. */
64 struct AircraftCache {
65 uint32_t cached_max_range_sqr; ///< Cached squared maximum range.
66 uint16_t cached_max_range; ///< Cached maximum range.
69 /**
70 * Aircraft, helicopters, rotors and their shadows belong to this class.
72 struct Aircraft final : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
73 uint16_t crashed_counter; ///< Timer for handling crash animations.
74 uint8_t pos; ///< Next desired position of the aircraft.
75 uint8_t previous_pos; ///< Previous desired position of the aircraft.
76 StationID targetairport; ///< Airport to go to next.
77 uint8_t state; ///< State of the airport. @see AirportMovementStates
78 Direction last_direction;
79 uint8_t number_consecutive_turns; ///< Protection to prevent the aircraft of making a lot of turns in order to reach a specific point.
80 uint8_t turn_counter; ///< Ticks between each turn to prevent > 45 degree turns.
81 uint8_t flags; ///< Aircraft flags. @see AirVehicleFlags
83 AircraftCache acache;
85 /** We don't want GCC to zero our struct! It already is zeroed and has an index! */
86 Aircraft() : SpecializedVehicleBase() {}
87 /** We want to 'destruct' the right class. */
88 virtual ~Aircraft() { this->PreDestructor(); }
90 void MarkDirty() override;
91 void UpdateDeltaXY() override;
92 ExpensesType GetExpenseType(bool income) const override { return income ? EXPENSES_AIRCRAFT_REVENUE : EXPENSES_AIRCRAFT_RUN; }
93 bool IsPrimaryVehicle() const override { return this->IsNormalAircraft(); }
94 void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override;
95 int GetDisplaySpeed() const override { return this->cur_speed; }
96 int GetDisplayMaxSpeed() const override { return this->vcache.cached_max_speed; }
97 int GetSpeedOldUnits() const { return this->vcache.cached_max_speed * 10 / 128; }
98 int GetCurrentMaxSpeed() const override { return this->GetSpeedOldUnits(); }
99 Money GetRunningCost() const override;
101 bool IsInDepot() const override
103 assert(this->IsPrimaryVehicle());
104 return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile);
107 bool Tick() override;
108 void OnNewCalendarDay() override;
109 void OnNewEconomyDay() override;
110 uint Crash(bool flooded = false) override;
111 TileIndex GetOrderStationLocation(StationID station) override;
112 TileIndex GetCargoTile() const override { return this->First()->tile; }
113 ClosestDepot FindClosestDepot() override;
116 * Check if the aircraft type is a normal flying device; eg
117 * not a rotor or a shadow
118 * @return Returns true if the aircraft is a helicopter/airplane and
119 * false if it is a shadow or a rotor
121 inline bool IsNormalAircraft() const
123 /* To be fully correct the commented out functionality is the proper one,
124 * but since value can only be 0 or 2, it is sufficient to only check <= 2
125 * return (this->subtype == AIR_HELICOPTER) || (this->subtype == AIR_AIRCRAFT); */
126 return this->subtype <= AIR_AIRCRAFT;
130 * Get the range of this aircraft.
131 * @return Range in tiles or 0 if unlimited range.
133 uint16_t GetRange() const
135 return this->acache.cached_max_range;
139 void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result);
141 Station *GetTargetAirportIfValid(const Aircraft *v);
142 void HandleMissingAircraftOrders(Aircraft *v);
144 #endif /* AIRCRAFT_H */