# aircraft_cmd.cpp File Reference

This file deals with aircraft and airport movements functionalities. More...

`#include "stdafx.h"`
`#include "aircraft.h"`
`#include "debug.h"`
`#include "landscape.h"`
`#include "news_func.h"`
`#include "vehicle_gui.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_sound.h"`
`#include "spritecache.h"`
`#include "strings_func.h"`
`#include "command_func.h"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "functions.h"`
`#include "cheat_type.h"`
`#include "company_base.h"`
`#include "autoreplace_gui.h"`
`#include "ai/ai.hpp"`
`#include "company_func.h"`
`#include "effectvehicle_func.h"`
`#include "station_base.h"`
`#include "engine_base.h"`
`#include "engine_func.h"`
`#include "core/random_func.hpp"`
`#include "table/strings.h"`
`#include "table/sprites.h"`

Go to the source code of this file.

## Defines
#define | MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
## Typedefs
typedef void | AircraftStateHandler (Aircraft *v, const AirportFTAClass *apc)
## Enumerations
enum | HelicopterRotorStates { HRS_ROTOR_STOPPED, HRS_ROTOR_MOVING_1, HRS_ROTOR_MOVING_2, HRS_ROTOR_MOVING_3 }
| Helicopter rotor animation states.
enum | AircraftSpeedLimits { SPEED_LIMIT_TAXI = 50, SPEED_LIMIT_APPROACH = 230, SPEED_LIMIT_BROKEN = 320, SPEED_LIMIT_HOLD = 425, SPEED_LIMIT_NONE = 0xFFFF }
| Special velocities for aircraft. More...
## Functions
static bool | AirportMove (Aircraft *v, const AirportFTAClass *apc)
static bool | AirportSetBlocks (Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
| "reserve" a block for the plane
static bool | AirportHasBlock (Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
static bool | AirportFindFreeTerminal (Aircraft *v, const AirportFTAClass *apc)
static bool | AirportFindFreeHelipad (Aircraft *v, const AirportFTAClass *apc)
static void | CrashAirplane (Aircraft *v)
static StationID | FindNearestHangar (const Aircraft *v)
| Find the nearest hangar to v INVALID_STATION is returned, if the company does not have any suitable airports (like helipads only).
SpriteID | GetRotorImage (const Aircraft *v)
static SpriteID | GetAircraftIcon (EngineID engine)
void | DrawAircraftEngine (int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
void | GetAircraftSpriteSize (EngineID engine, uint &width, uint &height)
| Get the size of the sprite of an aircraft sprite heading west (used for lists).
CommandCost | CmdBuildAircraft (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build an aircraft.
CommandCost | CmdSellAircraft (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Sell an aircraft.
CommandCost | CmdSendAircraftToHangar (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Send an aircraft to the hangar.
CommandCost | CmdRefitAircraft (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Refits an aircraft to the specified cargo type.
static void | CheckIfAircraftNeedsService (Aircraft *v)
static void | HelicopterTickHandler (Aircraft *v)
void | SetAircraftPosition (Aircraft *v, int x, int y, int z)
void | HandleAircraftEnterHangar (Aircraft *v)
| Handle Aircraft specific tasks when a an Aircraft enters a hangar.
static void | PlayAircraftSound (const Vehicle *v)
void | UpdateAircraftCache (Aircraft *v)
| Update cached values of an aircraft.
static int | UpdateAircraftSpeed (Aircraft *v, uint speed_limit=SPEED_LIMIT_NONE, bool hard_limit=true)
| Sets the new speed for an aircraft.
byte | GetAircraftFlyingAltitude (const Aircraft *v)
| Gets the cruise altitude of an aircraft.
static byte | AircraftGetEntryPoint (const Aircraft *v, const AirportFTAClass *apc)
| Find the entry point to an airport depending on direction which the airport is being approached from.
static void | MaybeCrashAirplane (Aircraft *v)
static bool | AircraftController (Aircraft *v)
| Controls the movement of an aircraft.
static bool | HandleCrashedAircraft (Aircraft *v)
static void | HandleBrokenAircraft (Aircraft *v)
static void | HandleAircraftSmoke (Aircraft *v)
void | HandleMissingAircraftOrders (Aircraft *v)
static void | AircraftEntersTerminal (Aircraft *v)
| we've landed and just arrived at a terminal
static void | AircraftLandAirplane (Aircraft *v)
void | AircraftNextAirportPos_and_Order (Aircraft *v)
| set the right pos when heading to other airports after takeoff
void | AircraftLeaveHangar (Aircraft *v)
static void | AircraftEventHandler_EnterTerminal (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_EnterHangar (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_InHangar (Aircraft *v, const AirportFTAClass *apc)
| In an Airport Hangar.
static void | AircraftEventHandler_AtTerminal (Aircraft *v, const AirportFTAClass *apc)
| At one of the Airport's Terminals.
static void | AircraftEventHandler_General (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_TakeOff (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_StartTakeOff (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_EndTakeOff (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_HeliTakeOff (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_Flying (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_Landing (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_HeliLanding (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_EndLanding (Aircraft *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_HeliEndLanding (Aircraft *v, const AirportFTAClass *apc)
static void | AirportClearBlock (const Aircraft *v, const AirportFTAClass *apc)
static void | AirportGoToNextPosition (Aircraft *v)
static bool | FreeTerminal (Aircraft *v, byte i, byte last_terminal)
static uint | GetNumTerminals (const AirportFTAClass *apc)
static uint | GetNumHelipads (const AirportFTAClass *apc)
static bool | AircraftEventHandler (Aircraft *v, int loop)
Station * | GetTargetAirportIfValid (const Aircraft *v)
| Returns aircraft's target station if v->target_airport is a valid station with airport.
void | UpdateAirplanesOnNewStation (const Station *st)
| Updates the status of the Aircraft heading or in the station.
## Variables
static const byte | _airport_terminal_state [] = {2, 3, 4, 5, 6, 7, 19, 20, 0, 0, 8, 9, 21, 22}
| this maps the terminal to its corresponding state and block flag currently set for 10 terms, 4 helipads
static const byte | _airport_terminal_flag [] = {0, 1, 2, 3, 4, 5, 22, 23, 0, 0, 6, 7, 24, 25}
static const SpriteID | _aircraft_sprite []
static AircraftStateHandler *const | _aircraft_state_handlers []

## Detailed Description

This file deals with aircraft and airport movements functionalities.

Definition in file aircraft_cmd.cpp.

## Enumeration Type Documentation

enum AircraftSpeedLimits

Special velocities for aircraft.

Enumerator:

  SPEED_LIMIT_TAXI | Maximum speed of an aircraft while taxiing.
  SPEED_LIMIT_APPROACH | Maximum speed of an aircraft on finals.
  SPEED_LIMIT_BROKEN | Maximum speed of an aircraft that is broken.
  SPEED_LIMIT_HOLD | Maximum speed of an aircraft that flies the holding pattern.
  SPEED_LIMIT_NONE | No environmental speed limit. Speed limit is type dependent.

Definition at line 697 of file aircraft_cmd.cpp.

## Function Documentation

static bool AircraftController | ( | Aircraft * | v | ) | `[static]`

Controls the movement of an aircraft.

This function actually moves the vehicle on the map and takes care of minor things like sound playback.

Todo:
  De-mystify the cur_speed values for helicopter rotors.

Parameters:

  | v | The vehicle that is moved. Must be the first vehicle of the chain

Returns:
  Whether the position requested by the State Machine has been reached

Definition at line 849 of file aircraft_cmd.cpp.

References _settings_game, abs(), AIR_AIRCRAFT, AircraftGetEntryPoint(), AircraftNextAirportPos_and_Order(), Station::Airport(), Station::airport_tile, Station::airport_type, ChangeDir(), AirportFTAClass::delta_z, DIRDIFF_45LEFT, DIRDIFF_45RIGHT, DIRDIFF_REVERSE, DIRDIFF_SAME, DirDifference(), GetAircraftFlyingAltitude(), SpecializedStation< Station, false >::GetIfValid(), GetNewVehiclePos(), INVALID_TILE, Aircraft::last_direction, max(), min(), AirportFTAClass::MovingData(), GetNewVehiclePosResult::new_tile, SpecializedVehicle< T, Type >::Next(), AirportFTAClass::nofelements, Aircraft::number_consecutive_turns, GetNewVehiclePosResult::old_tile, VehicleSettings::plane_speed, Aircraft::pos, Aircraft::previous_pos, SPEED_LIMIT_APPROACH, SPEED_LIMIT_HOLD, SPEED_LIMIT_NONE, SPEED_LIMIT_TAXI, Aircraft::state, Aircraft::targetairport, TILE_SIZE, TileX(), TileY(), Aircraft::turn_counter, UpdateAircraftCache(), UpdateAircraftSpeed(), GameSettings::vehicle, GetNewVehiclePosResult::x, BaseStation::xy, and GetNewVehiclePosResult::y.

static byte AircraftGetEntryPoint | ( | const Aircraft * | v,
|  | const AirportFTAClass * | apc |
| ) |  |  | `[static]`

Find the entry point to an airport depending on direction which the airport is being approached from.

Each airport can have up to four entry points for its approach system so that approaching aircraft do not fly through each other or are forced to do 180 degree turns during the approach. The arrivals are grouped into four sectors dependent on the DiagDirection from which the airport is approached.

Parameters:

  | v | The vehicle that is approaching the airport
  | apc | The Airport Class being approached.

Returns:
  The index of the entry point

Definition at line 809 of file aircraft_cmd.cpp.

References abs(), Station::airport_tile, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, AirportFTAClass::entry_points, SpecializedStation< Station, false >::GetIfValid(), INVALID_TILE, Aircraft::targetairport, TILE_SIZE, TileX(), TileY(), and BaseStation::xy.

Referenced by AircraftController(), AircraftNextAirportPos_and_Order(), and UpdateAirplanesOnNewStation().

static bool AirportSetBlocks | ( | Aircraft * | v,
|  | const AirportFTA * | current_pos,
|  | const AirportFTAClass * | apc |
| ) |  |  | `[static]`

"reserve" a block for the plane

Parameters:

  | v | airplane that requires the operation
  | current_pos | of the vehicle in the list of blocks
  | apc | airport on which block is requsted to be set

Returns:
  true on success. Eg, next block was free and we have occupied it

Definition at line 1760 of file aircraft_cmd.cpp.

References Station::airport_flags, AirportFTA::block, SpecializedStation< Station, false >::Get(), AirportFTA::heading, AirportFTAClass::layout, AirportFTA::next, AirportFTA::next_position, Aircraft::pos, AirportFTA::position, SETBITS, and Aircraft::targetairport.

CommandCost CmdBuildAircraft | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build an aircraft.

Parameters:

  | tile | tile of depot where aircraft is built
  | flags | for command
  | p1 | aircraft type being built (engine)
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 238 of file aircraft_cmd.cpp.

References _cur_year, _date, _settings_game, AIR_AIRCRAFT, AIR_HELICOPTER, AIR_ROTOR, AIR_SHADOW, Aircraft::Aircraft(), Station::Airport(), CanVehicleUseStation(), CMD_ERROR, CT_INVALID, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, DIR_N, DIR_SE, ENGINE_EXCLUSIVE_PREVIEW, SpecializedStation< Station, false >::GetByTile(), Station::GetHangarTile(), GetStationIndex(), GetVehicleCapacity(), AirportFTA::heading, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_DIR, InvalidateAutoreplaceWindow(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), IsHangarTile(), IsTileOwner(), AirportFTAClass::layout, VehicleSettings::max_aircraft, Aircraft::pos, AirportFTA::position, Aircraft::previous_pos, return_cmd_error, SetBit(), SetWindowDirty(), Aircraft::state, Aircraft::targetairport, TILE_SIZE, TileX(), TileY(), UpdateAircraftCache(), Aircraft::UpdateDeltaXY(), VEH_AIRCRAFT, GameSettings::vehicle, VehicleMove(), and VehicleRandomBits().

CommandCost CmdRefitAircraft | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Refits an aircraft to the specified cargo type.

Parameters:

  | tile | unused
  | flags | for command type
  | p1 | vehicle ID of the aircraft to refit
  | p2 | various bitstuffed elements p2 = (bit 0-7) - the new cargo type to refit to p2 = (bit 8-15) - the new cargo subtype to refit to p2 = (bit 16) - refit only this vehicle (ignored)
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 491 of file aircraft_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_EXEC, GB(), SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowClassesData(), NUM_CARGO, RefitVehicle(), return_cmd_error, and SetWindowDirty().

CommandCost CmdSellAircraft | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Sell an aircraft.

Parameters:

  | tile | unused
  | flags | for command type
  | p1 | vehicle ID to be sold
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 418 of file aircraft_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_EXEC, SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::GetIfValid(), and return_cmd_error.

CommandCost CmdSendAircraftToHangar | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Send an aircraft to the hangar.

Parameters:

  | tile | unused
  | flags | for command type
  | p1 | vehicle ID to send to the hangar
  | p2 | various bitmasked elements p2 bit 0-3 - DEPOT_ flags (see vehicle.h) p2 bit 8-10 - VLW flag (for mass goto depot)
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 465 of file aircraft_cmd.cpp.

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::GetIfValid(), SendAllVehiclesToDepot(), and VEH_AIRCRAFT.

static StationID FindNearestHangar | ( | const Aircraft * | v | ) | `[static]`

Find the nearest hangar to v INVALID_STATION is returned, if the company does not have any suitable airports (like helipads only).

Parameters:

  | v | vehicle looking for a hangar

Returns:
  the StationID if one is found, otherwise, INVALID_STATION

Definition at line 108 of file aircraft_cmd.cpp.

References Station::Airport(), Station::airport_tile, DistanceSquare(), FACIL_AIRPORT, BaseStation::facilities, AirportFTAClass::flags, Station::GetAirportSpec(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Cheats::no_jetcrash, AirportSpec::nof_depots, BaseStation::owner, and Cheat::value.

byte GetAircraftFlyingAltitude | ( | const Aircraft * | v | ) |

Gets the cruise altitude of an aircraft.

The cruise altitude is determined by the velocity of the vehicle and the direction it is moving

Parameters:

  | v | The vehicle. Should be an aircraft

Returns:
  Altitude in pixel units

Definition at line 769 of file aircraft_cmd.cpp.

References DIR_E, DIR_N, DIR_NE, DIR_SE, and min().

Referenced by AircraftController(), UpdateAirplanesOnNewStation(), and UpdateOldAircraft().

void GetAircraftSpriteSize | ( | EngineID | engine,
|  | uint & | width,
|  | uint & | height |
| ) |  |  |

Get the size of the sprite of an aircraft sprite heading west (used for lists).

Parameters:

  | engine | The engine to get the sprite from
  | width | The width of the sprite
  | height | The height of the sprite

Definition at line 222 of file aircraft_cmd.cpp.

References Sprite::height, ST_NORMAL, and Sprite::width.

Station* GetTargetAirportIfValid | ( | const Aircraft * | v | ) |

Returns aircraft's target station if v->target_airport is a valid station with airport.

Parameters:

  | v | vehicle to get target airport for

Returns:
  pointer to target station, NULL if invalid

Definition at line 1976 of file aircraft_cmd.cpp.

References Station::airport_tile, SpecializedStation< Station, false >::GetIfValid(), INVALID_TILE, Aircraft::targetairport, and VEH_AIRCRAFT.

Referenced by AircraftNextAirportPos_and_Order(), and MapAircraftMovementState().

void HandleAircraftEnterHangar | ( | Aircraft * | v | ) |

Handle Aircraft specific tasks when a an Aircraft enters a hangar.

Parameters:

  | *v | Vehicle that enters the hangar

Definition at line 656 of file aircraft_cmd.cpp.

References SpecializedVehicle< T, Type >::Next().

void UpdateAircraftCache | ( | Aircraft * | v | ) |

Update cached values of an aircraft.

Currently caches callback 36 max speed.

Parameters:

  | v | Vehicle

Definition at line 680 of file aircraft_cmd.cpp.

References Aircraft::acache, AircraftCache::cached_max_speed, and PROP_AIRCRAFT_SPEED.

Referenced by AfterLoadVehicles(), AircraftController(), CmdBuildAircraft(), StateGameLoop(), and UpdateAirplanesOnNewStation().

static int UpdateAircraftSpeed | ( | Aircraft * | v,
|  | uint | speed_limit = `SPEED_LIMIT_NONE`,
|  | bool | hard_limit = `true` |
| ) |  |  | `[static]`

Sets the new speed for an aircraft.

Parameters:

  | v | The vehicle for which the speed should be obtained
  | speed_limit | The maximum speed the vehicle may have.
  | hard_limit | If true, the limit is directly enforced, otherwise the plane is slowed down gradually

Returns:
  The number of position updates needed within the tick

Definition at line 712 of file aircraft_cmd.cpp.

References _settings_client, _settings_game, Aircraft::acache, AircraftCache::cached_max_speed, ClientSettings::gui, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, max(), min(), VehicleSettings::plane_speed, SetWindowWidgetDirty(), SPEED_LIMIT_BROKEN, GameSettings::vehicle, and GUISettings::vehicle_speed.

Referenced by AircraftController().

void UpdateAirplanesOnNewStation | ( | const Station * | st | ) |

Updates the status of the Aircraft heading or in the station.

Parameters:

  | st | Station been updated

Definition at line 1990 of file aircraft_cmd.cpp.

References AIR_HELICOPTER, AircraftGetEntryPoint(), Station::Airport(), GetAircraftFlyingAltitude(), GetNewVehiclePos(), AirportFTA::heading, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Aircraft::IsNormalAircraft(), AirportFTAClass::layout, AirportFTAClass::nofelements, Aircraft::pos, AirportFTA::position, Aircraft::previous_pos, Aircraft::state, Aircraft::targetairport, UpdateAircraftCache(), GetNewVehiclePosResult::x, and GetNewVehiclePosResult::y.

Referenced by CmdBuildAirport().

## Variable Documentation

const SpriteID _aircraft_sprite[]`[static]`

Initial value:

     {
      0x0EB5, 0x0EBD, 0x0EC5, 0x0ECD,
      0x0ED5, 0x0EDD, 0x0E9D, 0x0EA5,
      0x0EAD, 0x0EE5, 0x0F05, 0x0F0D,
      0x0F15, 0x0F1D, 0x0F25, 0x0F2D,
      0x0EED, 0x0EF5, 0x0EFD, 0x0F35,
      0x0E9D, 0x0EA5, 0x0EAD, 0x0EB5,
      0x0EBD, 0x0EC5
    }

Definition at line 84 of file aircraft_cmd.cpp.

AircraftStateHandler* const _aircraft_state_handlers[]`[static]`

Initial value:

     {
      AircraftEventHandler_General,
      AircraftEventHandler_InHangar,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_TakeOff,
      AircraftEventHandler_StartTakeOff,
      AircraftEventHandler_EndTakeOff,
      AircraftEventHandler_HeliTakeOff,
      AircraftEventHandler_Flying,
      AircraftEventHandler_Landing,
      AircraftEventHandler_EndLanding,
      AircraftEventHandler_HeliLanding,
      AircraftEventHandler_HeliEndLanding,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
      AircraftEventHandler_AtTerminal,
    }

Definition at line 1633 of file aircraft_cmd.cpp.