OpenTTD Source 20260206-master-g4d4e37dbf1
aircraft.h
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef AIRCRAFT_H
11#define AIRCRAFT_H
12
13#include "station_map.h"
14#include "vehicle_base.h"
15
20static constexpr int AIRCRAFT_MIN_FLYING_ALTITUDE = 120;
21static constexpr int AIRCRAFT_MAX_FLYING_ALTITUDE = 360;
22static constexpr int PLANE_HOLD_MAX_FLYING_ALTITUDE = 150;
23static constexpr int HELICOPTER_HOLD_MAX_FLYING_ALTITUDE = 184;
24
25struct Aircraft;
26
34
36enum class VehicleAirFlag : uint8_t {
38
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. */
44
46};
47using VehicleAirFlags = EnumBitSet<VehicleAirFlag, uint8_t>;
48
49static const int ROTOR_Z_OFFSET = 5;
50
52void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
54void UpdateAircraftCache(Aircraft *v, bool update_range = false);
55
56void AircraftLeaveHangar(Aircraft *v, Direction exit_dir);
58void SetAircraftPosition(Aircraft *v, int x, int y, int z);
59
60void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max);
61template <class T>
62int GetAircraftFlightLevel(T *v, bool takeoff = false);
63
66 uint32_t cached_max_range_sqr = 0;
67 uint16_t cached_max_range = 0;
68};
69
73struct Aircraft final : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
74 uint16_t crashed_counter = 0;
75 uint8_t pos = 0;
76 uint8_t previous_pos = 0;
77 StationID targetairport = StationID::Invalid();
78 uint8_t state = 0;
79 Direction last_direction = INVALID_DIR;
81 uint8_t turn_counter = 0;
82 VehicleAirFlags flags{};
83
84 AircraftCache acache{};
85
88 ~Aircraft() override { this->PreDestructor(); }
89
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;
100
101 bool IsInDepot() const override
102 {
103 assert(this->IsPrimaryVehicle());
104 return this->vehstatus.Test(VehState::Hidden) && IsHangarTile(this->tile);
105 }
106
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;
114
121 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
133 uint16_t GetRange() const
134 {
135 return this->acache.cached_max_range;
136 }
137};
138
139void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result);
140
142void HandleMissingAircraftOrders(Aircraft *v);
143
144#endif /* AIRCRAFT_H */
void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max)
Get the 'flight level' bounds, in pixels from 'z_pos' 0 for a particular vehicle for normal flight si...
static constexpr int AIRCRAFT_MIN_FLYING_ALTITUDE
Base values for flight levels above ground level for 'normal' flight and holding patterns.
Definition aircraft.h:20
static constexpr int HELICOPTER_HOLD_MAX_FLYING_ALTITUDE
holding flying altitude above tile of helicopters.
Definition aircraft.h:23
static const int ROTOR_Z_OFFSET
Z Offset between helicopter- and rotorsprite.
Definition aircraft.h:49
static constexpr int PLANE_HOLD_MAX_FLYING_ALTITUDE
holding flying altitude above tile of planes.
Definition aircraft.h:22
void UpdateAirplanesOnNewStation(const Station *st)
Updates the status of the Aircraft heading or in the station.
void AircraftNextAirportPos_and_Order(Aircraft *v)
set the right pos when heading to other airports after takeoff
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
Get the size of the sprite of an aircraft sprite heading west (used for lists).
VehicleAirFlag
Flags for air vehicles; shared with disaster vehicles.
Definition aircraft.h:36
@ HelicopterDirectDescent
The helicopter is descending directly at its destination (helipad or in front of hangar).
Definition aircraft.h:45
@ InMinimumHeightCorrection
The vehicle is currently raising its altitude because it hit the lower bound.
Definition aircraft.h:43
@ DestinationTooFar
Next destination is too far away.
Definition aircraft.h:37
@ InMaximumHeightCorrection
The vehicle is currently lowering its altitude because it hit the upper bound.
Definition aircraft.h:42
Station * GetTargetAirportIfValid(const Aircraft *v)
Returns aircraft's target station if v->target_airport is a valid station with airport.
void AircraftLeaveHangar(Aircraft *v, Direction exit_dir)
Aircraft is about to leave the hangar.
void SetAircraftPosition(Aircraft *v, int x, int y, int z)
Set aircraft position.
AircraftSubType
An aircraft can be one of those types.
Definition aircraft.h:28
@ AIR_AIRCRAFT
an airplane
Definition aircraft.h:30
@ AIR_ROTOR
rotor of an helicopter
Definition aircraft.h:32
@ AIR_SHADOW
shadow of the aircraft
Definition aircraft.h:31
@ AIR_HELICOPTER
an helicopter
Definition aircraft.h:29
void HandleAircraftEnterHangar(Aircraft *v)
Handle Aircraft specific tasks when an Aircraft enters a hangar.
static constexpr int AIRCRAFT_MAX_FLYING_ALTITUDE
Maximum flying altitude above tile.
Definition aircraft.h:21
void UpdateAircraftCache(Aircraft *v, bool update_range=false)
Update cached values of an aircraft.
Enum-as-bit-set wrapper.
Direction
Defines the 8 directions on the map.
@ INVALID_DIR
Flag for an invalid direction.
ExpensesType
Types of expenses.
@ EXPENSES_AIRCRAFT_REVENUE
Revenue from aircraft.
@ EXPENSES_AIRCRAFT_RUN
Running costs aircraft.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
Maps accessors for stations.
bool IsHangarTile(Tile t)
Is tile t an hangar tile?
Variables that are cached to improve performance and such.
Definition aircraft.h:65
uint32_t cached_max_range_sqr
Cached squared maximum range.
Definition aircraft.h:66
uint16_t cached_max_range
Cached maximum range.
Definition aircraft.h:67
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:73
bool Tick() override
Calls the tick handler of the vehicle.
uint8_t pos
Next desired position of the aircraft.
Definition aircraft.h:75
uint8_t state
State of the airport.
Definition aircraft.h:78
int GetDisplayMaxSpeed() const override
Gets the maximum speed in km-ish/h that can be sent into string parameters for string processing.
Definition aircraft.h:96
Money GetRunningCost() const override
Gets the running cost of a vehicle.
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
uint8_t number_consecutive_turns
Protection to prevent the aircraft of making a lot of turns in order to reach a specific point.
Definition aircraft.h:80
void OnNewEconomyDay() override
Economy day handler.
uint Crash(bool flooded=false) override
Crash the (whole) vehicle chain.
int GetCurrentMaxSpeed() const override
Calculates the maximum speed of the vehicle under its current conditions.
Definition aircraft.h:98
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override
Gets the sprite to show for the given direction.
void OnNewCalendarDay() override
Calendar day handler.
uint8_t turn_counter
Ticks between each turn to prevent > 45 degree turns.
Definition aircraft.h:81
~Aircraft() override
We want to 'destruct' the right class.
Definition aircraft.h:88
ExpensesType GetExpenseType(bool income) const override
Sets the expense type associated to this vehicle type.
Definition aircraft.h:92
TileIndex GetOrderStationLocation(StationID station) override
Determine the location for the station where the vehicle goes to next.
VehicleAirFlags flags
Aircraft flags.
Definition aircraft.h:82
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow.
Definition aircraft.h:121
void MarkDirty() override
Marks the vehicles to be redrawn and updates cached variables.
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition aircraft.h:93
uint16_t GetRange() const
Get the range of this aircraft.
Definition aircraft.h:133
uint8_t previous_pos
Previous desired position of the aircraft.
Definition aircraft.h:76
int GetDisplaySpeed() const override
Gets the speed in km-ish/h that can be sent into string parameters for string processing.
Definition aircraft.h:95
bool IsInDepot() const override
Check whether the vehicle is in the depot.
Definition aircraft.h:101
StationID targetairport
Airport to go to next.
Definition aircraft.h:77
ClosestDepot FindClosestDepot() override
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
uint16_t crashed_counter
Timer for handling crash animations.
Definition aircraft.h:74
SpecializedVehicle< Aircraft, Type > SpecializedVehicleBase
Station data structure.
uint16_t cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
Sprite sequence for a vehicle part.
Vehicle data structure.
Direction direction
facing
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
VehStates vehstatus
Status.
void PreDestructor()
Destroy all stuff that (still) needs the virtual functions to work properly.
Definition vehicle.cpp:797
VehicleCache vcache
Cache of often used vehicle values.
uint16_t cur_speed
current speed
TileIndex tile
Current tile index.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
Base class for all vehicles.
@ Hidden
Vehicle is not visible.
EngineImageType
Visualisation contexts of vehicles and engines.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.