1
/* $Id: ai_airport.cpp 15489 2009-02-14 21:16:21Z yexo $ */
3
/** @file ai_airport.cpp Implementation of AIAirport. */
5
#include "ai_airport.hpp"
6
#include "ai_station.hpp"
7
#include "../../station_map.h"
8
#include "../../company_func.h"
9
#include "../../command_type.h"
10
#include "../../town.h"
12
/* static */ bool AIAirport::IsValidAirportType(AirportType type)
14
return type >= AT_SMALL && type <= AT_HELISTATION && HasBit(::GetValidAirports(), type);
17
/* static */ bool AIAirport::IsHangarTile(TileIndex tile)
19
if (!::IsValidTile(tile)) return false;
21
return ::IsTileType(tile, MP_STATION) && ::IsHangar(tile);
24
/* static */ bool AIAirport::IsAirportTile(TileIndex tile)
26
if (!::IsValidTile(tile)) return false;
28
return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
31
/* static */ int32 AIAirport::GetAirportWidth(AirportType type)
33
if (!IsValidAirportType(type)) return -1;
35
return ::GetAirport(type)->size_x;
38
/* static */ int32 AIAirport::GetAirportHeight(AirportType type)
40
if (!IsValidAirportType(type)) return -1;
42
return ::GetAirport(type)->size_y;
45
/* static */ int32 AIAirport::GetAirportCoverageRadius(AirportType type)
47
if (!IsValidAirportType(type)) return -1;
49
return _settings_game.station.modified_catchment ? ::GetAirport(type)->catchment : (uint)CA_UNMODIFIED;
52
/* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type, StationID station_id)
54
EnforcePrecondition(false, ::IsValidTile(tile));
55
EnforcePrecondition(false, IsValidAirportType(type));
56
EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
58
uint p2 = station_id == AIStation::STATION_JOIN_ADJACENT ? 0 : 1;
59
p2 |= (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
60
return AIObject::DoCommand(tile, type, p2, CMD_BUILD_AIRPORT);
63
/* static */ bool AIAirport::RemoveAirport(TileIndex tile)
65
EnforcePrecondition(false, ::IsValidTile(tile))
66
EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
68
return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
71
/* static */ int32 AIAirport::GetNumHangars(TileIndex tile)
73
if (!::IsValidTile(tile)) return -1;
74
if (!::IsTileType(tile, MP_STATION)) return -1;
76
const Station *st = ::GetStationByTile(tile);
77
if (st->owner != _current_company) return -1;
78
if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
80
return st->Airport()->nof_depots;
83
/* static */ TileIndex AIAirport::GetHangarOfAirport(TileIndex tile)
85
if (!::IsValidTile(tile)) return INVALID_TILE;
86
if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
87
if (GetNumHangars(tile) < 1) return INVALID_TILE;
89
const Station *st = ::GetStationByTile(tile);
90
if (st->owner != _current_company) return INVALID_TILE;
91
if ((st->facilities & FACIL_AIRPORT) == 0) return INVALID_TILE;
93
return ::ToTileIndexDiff(st->Airport()->airport_depots[0]) + st->airport_tile;
96
/* static */ AIAirport::AirportType AIAirport::GetAirportType(TileIndex tile)
98
if (!AITile::IsStationTile(tile)) return AT_INVALID;
100
StationID station_id = ::GetStationIndex(tile);
102
if (!AIStation::HasStationType(station_id, AIStation::STATION_AIRPORT)) return AT_INVALID;
104
return (AirportType)::GetStation(station_id)->airport_type;
108
/* static */ int AIAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
110
extern Town *AirportGetNearestTown(const AirportFTAClass *afc, TileIndex airport_tile);
111
extern uint8 GetAirportNoiseLevelForTown(const AirportFTAClass *afc, TileIndex town_tile, TileIndex tile);
113
if (!::IsValidTile(tile)) return -1;
114
if (!IsValidAirportType(type)) return -1;
116
if (_settings_game.economy.station_noise_level) {
117
const AirportFTAClass *afc = ::GetAirport(type);
118
const Town *t = AirportGetNearestTown(afc, tile);
119
return GetAirportNoiseLevelForTown(afc, t->xy, tile);
125
/* static */ TownID AIAirport::GetNearestTown(TileIndex tile, AirportType type)
127
extern Town *AirportGetNearestTown(const AirportFTAClass *afc, TileIndex airport_tile);
129
if (!::IsValidTile(tile)) return INVALID_TOWN;
130
if (!IsValidAirportType(type)) return INVALID_TOWN;
132
return AirportGetNearestTown(GetAirport(type), tile)->index;