OpenTTD

aircraft.h
Go to the documentation of this file.
1 /* $Id: aircraft.h 27666 2016-10-16 14:57:56Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef AIRCRAFT_H
13 #define AIRCRAFT_H
14
15 #include "station_map.h"
16 #include "vehicle_base.h"
17
22 enum AircraftFlyingAltitude {
23 AIRCRAFT_MIN_FLYING_ALTITUDE = 120,
24 AIRCRAFT_MAX_FLYING_ALTITUDE = 360,
25 PLANE_HOLD_MAX_FLYING_ALTITUDE = 150,
26 HELICOPTER_HOLD_MAX_FLYING_ALTITUDE = 184
27 };
28
29 struct Aircraft;
30
32 enum AircraftSubType {
33 AIR_HELICOPTER = 0,
34 AIR_AIRCRAFT = 2,
35 AIR_SHADOW = 4,
36 AIR_ROTOR = 6,
37 };
38
40 enum AirVehicleFlags {
41 VAF_DEST_TOO_FAR = 0,
42
43 /* The next two flags are to prevent stair climbing of the aircraft. The idea is that the aircraft
44 * will ascend or descend multiple flight levels at a time instead of following the contours of the
45 * landscape at a fixed altitude. This only has effect when there are more than 15 height levels. */
46 VAF_IN_MAX_HEIGHT_CORRECTION = 1,
47 VAF_IN_MIN_HEIGHT_CORRECTION = 2,
48 };
49
50 static const int ROTOR_Z_OFFSET = 5;
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
66 struct AircraftCache {
67 uint32 cached_max_range_sqr;
68 uint16 cached_max_range;
69 };
70
74 struct Aircraft FINAL : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
75 uint16 crashed_counter;
76 byte pos;
77 byte previous_pos;
78 StationID targetairport;
79 byte state;
80 DirectionByte last_direction;
81 byte number_consecutive_turns;
82 byte turn_counter;
83 byte flags;
84
85 AircraftCache acache;
86
88 Aircraft() : SpecializedVehicleBase() {}
90 virtual ~Aircraft() { this->PreDestructor(); }
91
92 void MarkDirty();
93 void UpdateDeltaXY(Direction direction);
94 ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_AIRCRAFT_INC : EXPENSES_AIRCRAFT_RUN; }
95 bool IsPrimaryVehicle() const { return this->IsNormalAircraft(); }
96 void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const;
97 int GetDisplaySpeed() const { return this->cur_speed; }
98 int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; }
99 int GetSpeedOldUnits() const { return this->vcache.cached_max_speed * 10 / 128; }
100 int GetCurrentMaxSpeed() const { return this->GetSpeedOldUnits(); }
101 Money GetRunningCost() const;
102
103 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
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 GetRange() const
134 {
135 return this->acache.cached_max_range;
136 }
137 };
138
142 #define FOR_ALL_AIRCRAFT(var) FOR_ALL_VEHICLES_OF_TYPE(Aircraft, var)
143
144 void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result);
145
146 Station *GetTargetAirportIfValid(const Aircraft *v);
147
148 #endif /* AIRCRAFT_H */
Aircraft::number_consecutive_turns
byte 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
Aircraft::targetairport
StationID targetairport
Airport to go to next.
Definition: aircraft.h:78
station_map.h
Maps accessors for stations.
AircraftLeaveHangar
void AircraftLeaveHangar(Aircraft *v, Direction exit_dir)
Aircraft is about to leave the hangar.
Definition: aircraft_cmd.cpp:1393
AIRCRAFT_MIN_FLYING_ALTITUDE
Minimum flying altitude above tile.
Definition: aircraft.h:23
HandleAircraftEnterHangar
void HandleAircraftEnterHangar(Aircraft *v)
Handle Aircraft specific tasks when an Aircraft enters a hangar.
Definition: aircraft_cmd.cpp:546
Aircraft::pos
byte pos
Next desired position of the aircraft.
Definition: aircraft.h:76
ROTOR_Z_OFFSET
static const int ROTOR_Z_OFFSET
Z Offset between helicopter- and rotorsprite.
Definition: aircraft.h:50
AIR_AIRCRAFT
an airplane
Definition: aircraft.h:34
Aircraft
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition: aircraft.h:74
Aircraft::GetCurrentMaxSpeed
int GetCurrentMaxSpeed() const
Calculates the maximum speed of the vehicle under its current conditions.
Definition: aircraft.h:100
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:212
VAF_IN_MIN_HEIGHT_CORRECTION
The vehicle is currently raising its altitude because it hit the lower bound.
Definition: aircraft.h:47
AircraftNextAirportPos_and_Order
void AircraftNextAirportPos_and_Order(Aircraft *v)
set the right pos when heading to other airports after takeoff
Definition: aircraft_cmd.cpp:1373
GetAircraftFlightLevelBounds
void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max)
Get the &#39;flight level&#39; bounds, in pixels from &#39;z_pos&#39; 0 for a particular vehicle for normal flight si...
Definition: aircraft_cmd.cpp:700
PLANE_HOLD_MAX_FLYING_ALTITUDE
holding flying altitude above tile of planes.
Definition: aircraft.h:25
max
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:87
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:121
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:26
HELICOPTER_HOLD_MAX_FLYING_ALTITUDE
holding flying altitude above tile of helicopters.
Definition: aircraft.h:26
SpecializedVehicle
Class defining several overloaded accessors so we don&#39;t have to cast vehicle types that often...
Definition: vehicle_base.h:993
Aircraft::GetExpenseType
ExpensesType GetExpenseType(bool income) const
Sets the expense type associated to this vehicle type.
Definition: aircraft.h:94
UpdateAircraftCache
void UpdateAircraftCache(Aircraft *v, bool update_range=false)
Update cached values of an aircraft.
Definition: aircraft_cmd.cpp:576
GetTargetAirportIfValid
Station * GetTargetAirportIfValid(const Aircraft *v)
Returns aircraft&#39;s target station if v->target_airport is a valid station with airport.
Definition: aircraft_cmd.cpp:2064
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:230
Aircraft::GetRange
uint16 GetRange() const
Get the range of this aircraft.
Definition: aircraft.h:133
VAF_IN_MAX_HEIGHT_CORRECTION
The vehicle is currently lowering its altitude because it hit the upper bound.
Definition: aircraft.h:46
ExpensesType
ExpensesType
Types of expenses.
Definition: economy_type.h:150
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:130
min
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Aircraft::previous_pos
byte previous_pos
Previous desired position of the aircraft.
Definition: aircraft.h:77
TinyEnumT< Direction >
EXPENSES_AIRCRAFT_INC
Income from aircrafts.
Definition: economy_type.h:160
Aircraft::state
byte state
State of the airport.
Definition: aircraft.h:79
Aircraft::turn_counter
byte turn_counter
Ticks between each turn to prevent > 45 degree turns.
Definition: aircraft.h:82
Aircraft::GetDisplayMaxSpeed
int GetDisplayMaxSpeed() const
Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
Definition: aircraft.h:98
Aircraft::IsPrimaryVehicle
bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: aircraft.h:95
AircraftCache
Variables that are cached to improve performance and such.
Definition: aircraft.h:66
AirVehicleFlags
AirVehicleFlags
Flags for air vehicles; shared with disaster vehicles.
Definition: aircraft.h:40
AIR_ROTOR
rotor of an helicopter
Definition: aircraft.h:36
OnNewDay
static void OnNewDay()
Runs various procedures that have to be done daily.
Definition: date.cpp:255
AIRCRAFT_MAX_FLYING_ALTITUDE
Maximum flying altitude above tile.
Definition: aircraft.h:24
AircraftFlyingAltitude
AircraftFlyingAltitude
Base values for flight levels above ground level for &#39;normal&#39; flight and holding patterns.
Definition: aircraft.h:22
SetAircraftPosition
void SetAircraftPosition(Aircraft *v, int x, int y, int z)
Set aircraft position.
Definition: aircraft_cmd.cpp:507
AircraftCache::cached_max_range
uint16 cached_max_range
Cached maximum range.
Definition: aircraft.h:68
vehicle_base.h
Base class for all vehicles.
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
VAF_DEST_TOO_FAR
Next destination is too far away.
Definition: aircraft.h:41
AircraftCache::cached_max_range_sqr
uint32 cached_max_range_sqr
Cached squared maximum range.
Definition: aircraft.h:67
EXPENSES_AIRCRAFT_RUN
Running costs aircrafts.
Definition: economy_type.h:155
VS_HIDDEN
Vehicle is not visible.
Definition: vehicle_base.h:32
Aircraft::IsInDepot
bool IsInDepot() const
Check whether the vehicle is in the depot.
Definition: aircraft.h:103
AIR_HELICOPTER
an helicopter
Definition: aircraft.h:33
IsHangarTile
static bool IsHangarTile(TileIndex t)
Is tile t an hangar tile?
Definition: station_map.h:327
AircraftSubType
AircraftSubType
An aircraft can be one of those types.
Definition: aircraft.h:32
UpdateAirplanesOnNewStation
void UpdateAirplanesOnNewStation(const Station *st)
Updates the status of the Aircraft heading or in the station.
Definition: aircraft_cmd.cpp:2078
Aircraft::flags
byte flags
Aircraft flags.
Definition: aircraft.h:83
Aircraft::crashed_counter
uint16 crashed_counter
Timer for handling crash animations.
Definition: aircraft.h:75
Aircraft::~Aircraft
virtual ~Aircraft()
We want to &#39;destruct&#39; the right class.
Definition: aircraft.h:90
Station
Station data structure.
Definition: station_base.h:446
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
AIR_SHADOW
shadow of the aircraft
Definition: aircraft.h:35
Aircraft::Aircraft
Aircraft()
We don&#39;t want GCC to zero our struct! It already is zeroed and has an index!
Definition: aircraft.h:88
Aircraft::GetDisplaySpeed
int GetDisplaySpeed() const
Gets the speed in km-ish/h that can be sent into SetDParam for string processing. ...
Definition: aircraft.h:97