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  */
7 
8 /** @file aircraft.h Base for aircraft. */
9 
10 #ifndef AIRCRAFT_H
11 #define AIRCRAFT_H
12 
13 #include "station_map.h"
14 #include "vehicle_base.h"
15 
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.
19  */
20 enum AircraftFlyingAltitude {
21 	AIRCRAFT_MIN_FLYING_ALTITUDE        = 120, ///< Minimum flying altitude above tile.
22 	AIRCRAFT_MAX_FLYING_ALTITUDE        = 360, ///< Maximum flying altitude above tile.
23 	PLANE_HOLD_MAX_FLYING_ALTITUDE      = 150, ///< holding flying altitude above tile of planes.
24 	HELICOPTER_HOLD_MAX_FLYING_ALTITUDE = 184  ///< holding flying altitude above tile of helicopters.
25 };
26 
27 struct Aircraft;
28 
29 /** An aircraft can be one of those types. */
30 enum AircraftSubType {
31 	AIR_HELICOPTER = 0, ///< an helicopter
32 	AIR_AIRCRAFT   = 2, ///< an airplane
33 	AIR_SHADOW     = 4, ///< shadow of the aircraft
34 	AIR_ROTOR      = 6, ///< rotor of an helicopter
35 };
36 
37 /** Flags for air vehicles; shared with disaster vehicles. */
38 enum AirVehicleFlags {
39 	VAF_DEST_TOO_FAR             = 0, ///< Next destination is too far away.
40 
41 	/* The next two flags are to prevent stair climbing of the aircraft. The idea is that the aircraft
42 	 * will ascend or descend multiple flight levels at a time instead of following the contours of the
43 	 * landscape at a fixed altitude. This only has effect when there are more than 15 height levels. */
44 	VAF_IN_MAX_HEIGHT_CORRECTION = 1, ///< The vehicle is currently lowering its altitude because it hit the upper bound.
45 	VAF_IN_MIN_HEIGHT_CORRECTION = 2, ///< The vehicle is currently raising its altitude because it hit the lower bound.
46 
47 	VAF_HELI_DIRECT_DESCENT      = 3, ///< The helicopter is descending directly at its destination (helipad or in front of hangar)
48 };
49 
50 static const int ROTOR_Z_OFFSET         = 5;    ///< Z Offset between helicopter- and rotorsprite.
51 
52 void HandleAircraftEnterHangar(Aircraft *v);
53 void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
54 void UpdateAirplanesOnNewStation(const Station *st);
55 void UpdateAircraftCache(Aircraft *v, bool update_range = false);
56 
57 void AircraftLeaveHangar(Aircraft *v, Direction exit_dir);
58 void AircraftNextAirportPos_and_Order(Aircraft *v);
59 void SetAircraftPosition(Aircraft *v, int x, int y, int z);
60 
61 void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max);
62 template <class T>
63 int GetAircraftFlightLevel(T *v, bool takeoff = false);
64 
65 /** Variables that are cached to improve performance and such. */
66 struct AircraftCache {
67 	uint32 cached_max_range_sqr;   ///< Cached squared maximum range.
68 	uint16 cached_max_range;       ///< Cached maximum range.
69 };
70 
71 /**
72  * Aircraft, helicopters, rotors and their shadows belong to this class.
73  */
74 struct Aircraft FINAL : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
75 	uint16 crashed_counter;        ///< Timer for handling crash animations.
76 	byte pos;                      ///< Next desired position of the aircraft.
77 	byte previous_pos;             ///< Previous desired position of the aircraft.
78 	StationID targetairport;       ///< Airport to go to next.
79 	byte state;                    ///< State of the airport. @see AirportMovementStates
80 	Direction last_direction;
81 	byte number_consecutive_turns; ///< Protection to prevent the aircraft of making a lot of turns in order to reach a specific point.
82 	byte turn_counter;             ///< Ticks between each turn to prevent > 45 degree turns.
83 	byte flags;                    ///< Aircraft flags. @see AirVehicleFlags
84 
85 	AircraftCache acache;
86 
87 	/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
AircraftFINAL88 	Aircraft() : SpecializedVehicleBase() {}
89 	/** We want to 'destruct' the right class. */
~AircraftFINAL90 	virtual ~Aircraft() { this->PreDestructor(); }
91 
92 	void MarkDirty();
93 	void UpdateDeltaXY();
GetExpenseTypeFINAL94 	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_AIRCRAFT_INC : EXPENSES_AIRCRAFT_RUN; }
IsPrimaryVehicleFINAL95 	bool IsPrimaryVehicle() const                  { return this->IsNormalAircraft(); }
96 	void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const;
GetDisplaySpeedFINAL97 	int GetDisplaySpeed() const    { return this->cur_speed; }
GetDisplayMaxSpeedFINAL98 	int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; }
GetSpeedOldUnitsFINAL99 	int GetSpeedOldUnits() const   { return this->vcache.cached_max_speed * 10 / 128; }
GetCurrentMaxSpeedFINAL100 	int GetCurrentMaxSpeed() const { return this->GetSpeedOldUnits(); }
101 	Money GetRunningCost() const;
102 
IsInDepotFINAL103 	bool IsInDepot() const
104 	{
105 		assert(this->IsPrimaryVehicle());
106 		return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile);
107 	}
108 
109 	bool Tick();
110 	void OnNewDay();
111 	uint Crash(bool flooded = false);
112 	TileIndex GetOrderStationLocation(StationID station);
113 	bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
114 
115 	/**
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
120 	 */
IsNormalAircraftFINAL121 	inline bool IsNormalAircraft() const
122 	{
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;
127 	}
128 
129 	/**
130 	 * Get the range of this aircraft.
131 	 * @return Range in tiles or 0 if unlimited range.
132 	 */
GetRangeFINAL133 	uint16 GetRange() const
134 	{
135 		return this->acache.cached_max_range;
136 	}
137 };
138 
139 void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result);
140 
141 Station *GetTargetAirportIfValid(const Aircraft *v);
142 
143 #endif /* AIRCRAFT_H */
144