OpenTTD Source  20240917-master-g9ab0a47812
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef AIRCRAFT_H
11 #define AIRCRAFT_H
12 
13 #include "station_map.h"
14 #include "vehicle_base.h"
15 
25 };
26 
27 struct Aircraft;
28 
33  AIR_SHADOW = 4,
34  AIR_ROTOR = 6,
35 };
36 
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. */
46 
48 };
49 
50 static const int ROTOR_Z_OFFSET = 5;
51 
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);
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 
66 struct AircraftCache {
68  uint16_t cached_max_range;
69 };
70 
74 struct Aircraft final : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
75  uint16_t crashed_counter;
76  uint8_t pos;
77  uint8_t previous_pos;
78  StationID targetairport;
79  uint8_t state;
80  Direction last_direction;
82  uint8_t turn_counter;
83  uint8_t flags;
84 
85  AircraftCache acache;
86 
90  virtual ~Aircraft() { this->PreDestructor(); }
91 
92  void MarkDirty() override;
93  void UpdateDeltaXY() override;
94  ExpensesType GetExpenseType(bool income) const override { return income ? EXPENSES_AIRCRAFT_REVENUE : EXPENSES_AIRCRAFT_RUN; }
95  bool IsPrimaryVehicle() const override { return this->IsNormalAircraft(); }
96  void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override;
97  int GetDisplaySpeed() const override { return this->cur_speed; }
98  int GetDisplayMaxSpeed() const override { return this->vcache.cached_max_speed; }
99  int GetSpeedOldUnits() const { return this->vcache.cached_max_speed * 10 / 128; }
100  int GetCurrentMaxSpeed() const override { return this->GetSpeedOldUnits(); }
101  Money GetRunningCost() const override;
102 
103  bool IsInDepot() const override
104  {
105  assert(this->IsPrimaryVehicle());
106  return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile);
107  }
108 
109  bool Tick() override;
110  void OnNewCalendarDay() override;
111  void OnNewEconomyDay() override;
112  uint Crash(bool flooded = false) override;
113  TileIndex GetOrderStationLocation(StationID station) override;
114  TileIndex GetCargoTile() const override { return this->First()->tile; }
115  ClosestDepot FindClosestDepot() override;
116 
123  inline bool IsNormalAircraft() const
124  {
125  /* To be fully correct the commented out functionality is the proper one,
126  * but since value can only be 0 or 2, it is sufficient to only check <= 2
127  * return (this->subtype == AIR_HELICOPTER) || (this->subtype == AIR_AIRCRAFT); */
128  return this->subtype <= AIR_AIRCRAFT;
129  }
130 
135  uint16_t GetRange() const
136  {
137  return this->acache.cached_max_range;
138  }
139 };
140 
141 void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result);
142 
144 void HandleMissingAircraftOrders(Aircraft *v);
145 
146 #endif /* AIRCRAFT_H */
Aircraft::targetairport
StationID targetairport
Airport to go to next.
Definition: aircraft.h:78
Aircraft::Aircraft
Aircraft()
We don't want GCC to zero our struct! It already is zeroed and has an index!
Definition: aircraft.h:88
AircraftNextAirportPos_and_Order
void AircraftNextAirportPos_and_Order(Aircraft *v)
set the right pos when heading to other airports after takeoff
Definition: aircraft_cmd.cpp:1449
Vehicle::PreDestructor
void PreDestructor()
Destroy all stuff that (still) needs the virtual functions to work properly.
Definition: vehicle.cpp:826
AircraftSubType
AircraftSubType
An aircraft can be one of those types.
Definition: aircraft.h:30
Aircraft::~Aircraft
virtual ~Aircraft()
We want to 'destruct' the right class.
Definition: aircraft.h:90
UpdateAirplanesOnNewStation
void UpdateAirplanesOnNewStation(const Station *st)
Updates the status of the Aircraft heading or in the station.
Definition: aircraft_cmd.cpp:2162
Station
Station data structure.
Definition: station_base.h:439
PLANE_HOLD_MAX_FLYING_ALTITUDE
@ PLANE_HOLD_MAX_FLYING_ALTITUDE
holding flying altitude above tile of planes.
Definition: aircraft.h:23
Aircraft::GetDisplaySpeed
int GetDisplaySpeed() const override
Gets the speed in km-ish/h that can be sent into SetDParam for string processing.
Definition: aircraft.h:97
AircraftCache::cached_max_range
uint16_t cached_max_range
Cached maximum range.
Definition: aircraft.h:68
Aircraft::GetCurrentMaxSpeed
int GetCurrentMaxSpeed() const override
Calculates the maximum speed of the vehicle under its current conditions.
Definition: aircraft.h:100
VAF_IN_MAX_HEIGHT_CORRECTION
@ VAF_IN_MAX_HEIGHT_CORRECTION
The vehicle is currently lowering its altitude because it hit the upper bound.
Definition: aircraft.h:44
vehicle_base.h
AircraftLeaveHangar
void AircraftLeaveHangar(Aircraft *v, Direction exit_dir)
Aircraft is about to leave the hangar.
Definition: aircraft_cmd.cpp:1469
Aircraft::FindClosestDepot
ClosestDepot FindClosestDepot() override
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
Definition: aircraft_cmd.cpp:399
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:78
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:244
AIR_AIRCRAFT
@ AIR_AIRCRAFT
an airplane
Definition: aircraft.h:32
GetTargetAirportIfValid
Station * GetTargetAirportIfValid(const Aircraft *v)
Returns aircraft's target station if v->target_airport is a valid station with airport.
Definition: aircraft_cmd.cpp:2148
Vehicle::vehstatus
uint8_t vehstatus
Status.
Definition: vehicle_base.h:354
AIR_SHADOW
@ AIR_SHADOW
shadow of the aircraft
Definition: aircraft.h:33
AircraftCache
Variables that are cached to improve performance and such.
Definition: aircraft.h:66
HandleAircraftEnterHangar
void HandleAircraftEnterHangar(Aircraft *v)
Handle Aircraft specific tasks when an Aircraft enters a hangar.
Definition: aircraft_cmd.cpp:573
Aircraft::GetRunningCost
Money GetRunningCost() const override
Gets the running cost of a vehicle.
Definition: aircraft_cmd.cpp:441
Aircraft
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition: aircraft.h:74
VehicleCache::cached_max_speed
uint16_t cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
Definition: vehicle_base.h:126
Aircraft::crashed_counter
uint16_t crashed_counter
Timer for handling crash animations.
Definition: aircraft.h:75
VS_HIDDEN
@ VS_HIDDEN
Vehicle is not visible.
Definition: vehicle_base.h:33
AIRCRAFT_MAX_FLYING_ALTITUDE
@ AIRCRAFT_MAX_FLYING_ALTITUDE
Maximum flying altitude above tile.
Definition: aircraft.h:22
VAF_HELI_DIRECT_DESCENT
@ VAF_HELI_DIRECT_DESCENT
The helicopter is descending directly at its destination (helipad or in front of hangar)
Definition: aircraft.h:47
Aircraft::flags
uint8_t flags
Aircraft flags.
Definition: aircraft.h:83
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:264
EXPENSES_AIRCRAFT_RUN
@ EXPENSES_AIRCRAFT_RUN
Running costs aircraft.
Definition: economy_type.h:177
Aircraft::pos
uint8_t pos
Next desired position of the aircraft.
Definition: aircraft.h:76
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:135
Aircraft::GetDisplayMaxSpeed
int GetDisplayMaxSpeed() const override
Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
Definition: aircraft.h:98
SpecializedVehicle
Class defining several overloaded accessors so we don't have to cast vehicle types that often.
Definition: vehicle_base.h:1095
Vehicle::cur_speed
uint16_t cur_speed
current speed
Definition: vehicle_base.h:328
GetAircraftFlightLevelBounds
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...
Definition: aircraft_cmd.cpp:730
SetAircraftPosition
void SetAircraftPosition(Aircraft *v, int x, int y, int z)
Set aircraft position.
Definition: aircraft_cmd.cpp:534
AirVehicleFlags
AirVehicleFlags
Flags for air vehicles; shared with disaster vehicles.
Definition: aircraft.h:38
VAF_IN_MIN_HEIGHT_CORRECTION
@ VAF_IN_MIN_HEIGHT_CORRECTION
The vehicle is currently raising its altitude because it hit the lower bound.
Definition: aircraft.h:45
Aircraft::previous_pos
uint8_t previous_pos
Previous desired position of the aircraft.
Definition: aircraft.h:77
Aircraft::IsInDepot
bool IsInDepot() const override
Check whether the vehicle is in the depot.
Definition: aircraft.h:103
Aircraft::OnNewCalendarDay
void OnNewCalendarDay() override
Calendar day handler.
Definition: aircraft_cmd.cpp:449
Aircraft::number_consecutive_turns
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:81
Vehicle::direction
Direction direction
facing
Definition: vehicle_base.h:307
UpdateAircraftCache
void UpdateAircraftCache(Aircraft *v, bool update_range=false)
Update cached values of an aircraft.
Definition: aircraft_cmd.cpp:603
Vehicle::subtype
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
Definition: vehicle_base.h:355
VAF_DEST_TOO_FAR
@ VAF_DEST_TOO_FAR
Next destination is too far away.
Definition: aircraft.h:39
Vehicle::vcache
VehicleCache vcache
Cache of often used vehicle values.
Definition: vehicle_base.h:364
EXPENSES_AIRCRAFT_REVENUE
@ EXPENSES_AIRCRAFT_REVENUE
Revenue from aircraft.
Definition: economy_type.h:182
GetAircraftSpriteSize
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).
Definition: aircraft_cmd.cpp:249
HELICOPTER_HOLD_MAX_FLYING_ALTITUDE
@ HELICOPTER_HOLD_MAX_FLYING_ALTITUDE
holding flying altitude above tile of helicopters.
Definition: aircraft.h:24
AircraftCache::cached_max_range_sqr
uint32_t cached_max_range_sqr
Cached squared maximum range.
Definition: aircraft.h:67
Aircraft::state
uint8_t state
State of the airport.
Definition: aircraft.h:79
Aircraft::GetRange
uint16_t GetRange() const
Get the range of this aircraft.
Definition: aircraft.h:135
AIR_HELICOPTER
@ AIR_HELICOPTER
an helicopter
Definition: aircraft.h:31
Aircraft::IsNormalAircraft
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow.
Definition: aircraft.h:123
Aircraft::OnNewEconomyDay
void OnNewEconomyDay() override
Economy day handler.
Definition: aircraft_cmd.cpp:456
ExpensesType
ExpensesType
Types of expenses.
Definition: economy_type.h:172
Aircraft::turn_counter
uint8_t turn_counter
Ticks between each turn to prevent > 45 degree turns.
Definition: aircraft.h:82
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
ClosestDepot
Structure to return information about the closest depot location, and whether it could be found.
Definition: vehicle_base.h:230
station_map.h
OverflowSafeInt< int64_t >
Aircraft::IsPrimaryVehicle
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition: aircraft.h:95
AircraftFlyingAltitude
AircraftFlyingAltitude
Base values for flight levels above ground level for 'normal' flight and holding patterns.
Definition: aircraft.h:20
Aircraft::Crash
uint Crash(bool flooded=false) override
Crash the (whole) vehicle chain.
Definition: aircraft_cmd.cpp:1323
EngineID
uint16_t EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::First
Aircraft * First() const
Get the first vehicle in the chain.
Definition: vehicle_base.h:1112
Aircraft::Tick
bool Tick() override
Calls the tick handler of the vehicle.
Definition: aircraft_cmd.cpp:2119
Aircraft::UpdateDeltaXY
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
Definition: aircraft_cmd.cpp:49
Aircraft::MarkDirty
void MarkDirty() override
Marks the vehicles to be redrawn and updates cached variables.
Definition: aircraft_cmd.cpp:1313
AIRCRAFT_MIN_FLYING_ALTITUDE
@ AIRCRAFT_MIN_FLYING_ALTITUDE
Minimum flying altitude above tile.
Definition: aircraft.h:21
AIR_ROTOR
@ AIR_ROTOR
rotor of an helicopter
Definition: aircraft.h:34
IsHangarTile
bool IsHangarTile(Tile t)
Is tile t an hangar tile?
Definition: station_map.h:413
ROTOR_Z_OFFSET
static const int ROTOR_Z_OFFSET
Z Offset between helicopter- and rotorsprite.
Definition: aircraft.h:50