# 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 "variables.h"`
`#include "cheat_type.h"`
`#include "autoreplace_func.h"`
`#include "autoreplace_gui.h"`
`#include "gfx_func.h"`
`#include "ai/ai.hpp"`
`#include "company_func.h"`
`#include "effectvehicle_func.h"`
`#include "settings_type.h"`
`#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 (Vehicle *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 (Vehicle *v, const AirportFTAClass *apc)
static bool | AirportSetBlocks (Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
| "reserve" a block for the plane
static bool | AirportHasBlock (Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
static bool | AirportFindFreeTerminal (Vehicle *v, const AirportFTAClass *apc)
static bool | AirportFindFreeHelipad (Vehicle *v, const AirportFTAClass *apc)
static void | CrashAirplane (Vehicle *v)
static StationID | FindNearestHangar (const Vehicle *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 Vehicle *v)
static SpriteID | GetAircraftIcon (EngineID engine)
void | DrawAircraftEngine (int x, int y, EngineID engine, SpriteID pal)
void | GetAircraftSpriteSize (EngineID engine, uint &width, uint &height)
| Get the size of the sprite of an aircraft sprite heading west (used for lists).
uint16 | AircraftDefaultCargoCapacity (CargoID cid, const AircraftVehicleInfo *avi)
| Calculates cargo capacity based on an aircraft's passenger and mail capacities.
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 (Vehicle *v)
static void | AgeAircraftCargo (Vehicle *v)
static void | HelicopterTickHandler (Vehicle *v)
void | SetAircraftPosition (Vehicle *v, int x, int y, int z)
void | HandleAircraftEnterHangar (Vehicle *v)
| Handle Aircraft specific tasks when a an Aircraft enters a hangar.
static void | PlayAircraftSound (const Vehicle *v)
void | UpdateAircraftCache (Vehicle *v)
| Update cached values of an aircraft.
static int | UpdateAircraftSpeed (Vehicle *v, uint speed_limit=SPEED_LIMIT_NONE, bool hard_limit=true)
| Sets the new speed for an aircraft.
byte | GetAircraftFlyingAltitude (const Vehicle *v)
| Gets the cruise altitude of an aircraft.
static byte | AircraftGetEntryPoint (const Vehicle *v, const AirportFTAClass *apc)
| Find the entry point to an airport depending on direction which the airport is being approached from.
static bool | AircraftController (Vehicle *v)
| Controls the movement of an aircraft.
static void | HandleCrashedAircraft (Vehicle *v)
static void | HandleBrokenAircraft (Vehicle *v)
static void | HandleAircraftSmoke (Vehicle *v)
void | HandleMissingAircraftOrders (Vehicle *v)
static void | MaybeCrashAirplane (Vehicle *v)
static void | AircraftEntersTerminal (Vehicle *v)
| we've landed and just arrived at a terminal
static void | AircraftLandAirplane (Vehicle *v)
void | AircraftNextAirportPos_and_Order (Vehicle *v)
| set the right pos when heading to other airports after takeoff
void | AircraftLeaveHangar (Vehicle *v)
static bool | CheckSendAircraftToHangarForReplacement (const Vehicle *v)
| Checks if an aircraft should head towards a hangar because it needs replacement.
static void | AircraftEventHandler_EnterTerminal (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_EnterHangar (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_InHangar (Vehicle *v, const AirportFTAClass *apc)
| In an Airport Hangar.
static void | AircraftEventHandler_AtTerminal (Vehicle *v, const AirportFTAClass *apc)
| At one of the Airport's Terminals.
static void | AircraftEventHandler_General (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_TakeOff (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_StartTakeOff (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_EndTakeOff (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_HeliTakeOff (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_Flying (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_Landing (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_HeliLanding (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_EndLanding (Vehicle *v, const AirportFTAClass *apc)
static void | AircraftEventHandler_HeliEndLanding (Vehicle *v, const AirportFTAClass *apc)
static void | AirportClearBlock (const Vehicle *v, const AirportFTAClass *apc)
static void | AirportGoToNextPosition (Vehicle *v)
static bool | FreeTerminal (Vehicle *v, byte i, byte last_terminal)
static uint | GetNumTerminals (const AirportFTAClass *apc)
static uint | GetNumHelipads (const AirportFTAClass *apc)
static void | AircraftEventHandler (Vehicle *v, int loop)
Station * | GetTargetAirportIfValid (const Vehicle *v)
| Returns aircraft's target station if v->u.air.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 781 of file aircraft_cmd.cpp.

## Function Documentation

static bool AircraftController | ( | Vehicle * | 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 930 of file aircraft_cmd.cpp.

References abs(), 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(), GetNewVehiclePos(), INVALID_TILE, max(), min(), AirportFTAClass::MovingData(), GetNewVehiclePosResult::new_tile, AirportFTAClass::nofelements, SPEED_LIMIT_APPROACH, SPEED_LIMIT_HOLD, SPEED_LIMIT_NONE, SPEED_LIMIT_TAXI, TILE_SIZE, TileX(), TileY(), UpdateAircraftCache(), UpdateAircraftSpeed(), GetNewVehiclePosResult::x, Station::xy, and GetNewVehiclePosResult::y.

uint16 AircraftDefaultCargoCapacity | ( | CargoID | cid,
|  | const AircraftVehicleInfo * | avi |
| ) |  |  |

Calculates cargo capacity based on an aircraft's passenger and mail capacities.

Parameters:

  | cid | Which cargo type to calculate a capacity for.
  | avi | Which engine to find a cargo capacity for.

Returns:
  New cargo capacity value.

Definition at line 224 of file aircraft_cmd.cpp.

Referenced by CmdBuildAircraft(), and CmdRefitAircraft().

static byte AircraftGetEntryPoint | ( | const Vehicle * | 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 893 of file aircraft_cmd.cpp.

References abs(), Station::airport_tile, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, AirportFTAClass::entry_points, INVALID_TILE, TILE_SIZE, TileX(), TileY(), and Station::xy.

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

static bool AirportSetBlocks | ( | Vehicle * | 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 1843 of file aircraft_cmd.cpp.

References Station::airport_flags, AirportFTA::block, HASBITS, AirportFTA::heading, AirportFTAClass::layout, AirportFTA::next, AirportFTA::next_position, AirportFTA::position, and SETBITS.

static bool CheckSendAircraftToHangarForReplacement | ( | const Vehicle * | v | ) | `[inline, static]`

Checks if an aircraft should head towards a hangar because it needs replacement.

Parameters:

  | *v | the vehicle to test

Returns:
  true if the aircraft should head towards a hangar

Definition at line 1438 of file aircraft_cmd.cpp.

References CMD_BUILD_AIRCRAFT, DC_QUERY_COST, DoCommand(), EngineReplacementForCompany(), HasBit(), and VehicleHasDepotOrders().

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 return result of operation. Could be cost, error

Definition at line 249 of file aircraft_cmd.cpp.

References _cur_year, _date, _settings_game, AIR_AIRCRAFT, AIR_HELICOPTER, AIR_ROTOR, AIR_SHADOW, Aircraft::Aircraft(), AircraftDefaultCargoCapacity(), Station::Airport(), AirportFTAClass::airport_depots, Station::airport_tile, CALLBACK_FAILED, CanVehicleUseStation(), CBID_VEHICLE_REFIT_CAPACITY, CBM_VEHICLE_REFIT_CAPACITY, CMD_ERROR, DAYS_IN_LEAP_YEAR, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, DIR_N, DIR_SE, ENGINE_EXCLUSIVE_PREVIEW, GetStationIndex(), GetVehicleCallback(), HasBit(), AirportFTA::heading, INVALID_DIR, InvalidateAutoreplaceWindow(), InvalidateWindow(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), IsTileOwner(), AirportFTAClass::layout, VehicleSettings::max_aircraft, AirportFTAClass::nof_depots, AirportFTA::position, return_cmd_error, VehicleSettings::servint_aircraft, SetBit(), TILE_SIZE, TileX(), TileY(), ToTileIndexDiff(), UpdateAircraftCache(), 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)

Returns:
  cost of refit or error

Definition at line 527 of file aircraft_cmd.cpp.

References AircraftDefaultCargoCapacity(), CALLBACK_FAILED, CanRefitTo(), CBID_VEHICLE_REFIT_CAPACITY, CBM_VEHICLE_REFIT_CAPACITY, CC_PASSENGERS, CMD_ERROR, DC_EXEC, GB(), GetRefitCost(), GetVehicleCallback(), HasBit(), InvalidateWindow(), InvalidateWindowClassesData(), IsValidVehicleID(), and return_cmd_error.

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

Returns:
  result of operation. Error or sold value

Definition at line 451 of file aircraft_cmd.cpp.

References CMD_ERROR, DC_EXEC, HASBITS, IsValidVehicleID(), 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)

Returns:
  o if everything went well

Definition at line 499 of file aircraft_cmd.cpp.

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, IsValidVehicleID(), and SendAllVehiclesToDepot().

static StationID FindNearestHangar | ( | const Vehicle * | 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 100 of file aircraft_cmd.cpp.

References Station::Airport(), Station::airport_tile, DistanceSquare(), Station::facilities, AirportFTAClass::flags, PoolItem< T, Tid, Tpool >::index, Cheats::no_jetcrash, AirportFTAClass::nof_depots, Station::owner, and Cheat::value.

byte GetAircraftFlyingAltitude | ( | const Vehicle * | 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 853 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 209 of file aircraft_cmd.cpp.

References ST_NORMAL.

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

Returns aircraft's target station if v->u.air.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 2059 of file aircraft_cmd.cpp.

References Station::airport_tile, and INVALID_TILE.

Referenced by AircraftNextAirportPos_and_Order(), and MapAircraftMovementState().

void HandleAircraftEnterHangar | ( | Vehicle * | v | ) |

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

Parameters:

  | *v | Vehicle that enters the hangar

Definition at line 740 of file aircraft_cmd.cpp.

void UpdateAircraftCache | ( | Vehicle * | v | ) |

Update cached values of an aircraft.

Currently caches callback 36 max speed.

Parameters:

  | v | Vehicle

Definition at line 764 of file aircraft_cmd.cpp.

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

static int UpdateAircraftSpeed | ( | Vehicle * | 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 796 of file aircraft_cmd.cpp.

References _settings_client, _settings_game, ClientSettings::gui, InvalidateWindowWidget(), max(), min(), VehicleSettings::plane_speed, 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 2076 of file aircraft_cmd.cpp.

References AIR_HELICOPTER, AircraftGetEntryPoint(), Station::Airport(), GetAircraftFlyingAltitude(), GetNewVehiclePos(), AirportFTA::heading, PoolItem< T, Tid, Tpool >::index, IsNormalAircraft(), AirportFTAClass::layout, AirportFTAClass::nofelements, AirportFTA::position, 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 76 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 1715 of file aircraft_cmd.cpp.