# ai_airport.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_airport.cpp 16272 2009-05-10 21:26:46Z rubidium $ */
    00002
    00005 #include "ai_airport.hpp"
    00006 #include "ai_station.hpp"
    00007 #include "../../station_map.h"
    00008 #include "../../company_func.h"
    00009 #include "../../command_type.h"
    00010 #include "../../town.h"
    00011 #include "../../economy_func.h"
    00012
    00013 /* static */ bool AIAirport::IsValidAirportType(AirportType type)
    00014 {
    00015   return type >= AT_SMALL && type <= AT_HELISTATION && HasBit(::GetValidAirports(), type);
    00016 }
    00017
    00018 /* static */ Money AIAirport::GetPrice(AirportType type)
    00019 {
    00020   if (!IsValidAirportType(type)) return -1;
    00021
    00022   const AirportFTAClass *afc = ::GetAirport(type);
    00023   return _price.build_airport * afc->size_x * afc->size_y;
    00024 }
    00025
    00026 /* static */ bool AIAirport::IsHangarTile(TileIndex tile)
    00027 {
    00028   if (!::IsValidTile(tile)) return false;
    00029
    00030   return ::IsTileType(tile, MP_STATION) && ::IsHangar(tile);
    00031 }
    00032
    00033 /* static */ bool AIAirport::IsAirportTile(TileIndex tile)
    00034 {
    00035   if (!::IsValidTile(tile)) return false;
    00036
    00037   return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
    00038 }
    00039
    00040 /* static */ int32 AIAirport::GetAirportWidth(AirportType type)
    00041 {
    00042   if (!IsValidAirportType(type)) return -1;
    00043
    00044   return ::GetAirport(type)->size_x;
    00045 }
    00046
    00047 /* static */ int32 AIAirport::GetAirportHeight(AirportType type)
    00048 {
    00049   if (!IsValidAirportType(type)) return -1;
    00050
    00051   return ::GetAirport(type)->size_y;
    00052 }
    00053
    00054 /* static */ int32 AIAirport::GetAirportCoverageRadius(AirportType type)
    00055 {
    00056   if (!IsValidAirportType(type)) return -1;
    00057
    00058   return _settings_game.station.modified_catchment ? ::GetAirport(type)->catchment : (uint)CA_UNMODIFIED;
    00059 }
    00060
    00061 /* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type, StationID station_id)
    00062 {
    00063   EnforcePrecondition(false, ::IsValidTile(tile));
    00064   EnforcePrecondition(false, IsValidAirportType(type));
    00065   EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
    00066
    00067   uint p2 = station_id == AIStation::STATION_JOIN_ADJACENT ? 0 : 1;
    00068   p2 |= (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
    00069   return AIObject::DoCommand(tile, type, p2, CMD_BUILD_AIRPORT);
    00070 }
    00071
    00072 /* static */ bool AIAirport::RemoveAirport(TileIndex tile)
    00073 {
    00074   EnforcePrecondition(false, ::IsValidTile(tile))
    00075   EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
    00076
    00077   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00078 }
    00079
    00080 /* static */ int32 AIAirport::GetNumHangars(TileIndex tile)
    00081 {
    00082   if (!::IsValidTile(tile)) return -1;
    00083   if (!::IsTileType(tile, MP_STATION)) return -1;
    00084
    00085   const Station *st = ::GetStationByTile(tile);
    00086   if (st->owner != _current_company) return -1;
    00087   if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
    00088
    00089   return st->Airport()->nof_depots;
    00090 }
    00091
    00092 /* static */ TileIndex AIAirport::GetHangarOfAirport(TileIndex tile)
    00093 {
    00094   if (!::IsValidTile(tile)) return INVALID_TILE;
    00095   if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
    00096   if (GetNumHangars(tile) < 1) return INVALID_TILE;
    00097
    00098   const Station *st = ::GetStationByTile(tile);
    00099   if (st->owner != _current_company) return INVALID_TILE;
    00100   if ((st->facilities & FACIL_AIRPORT) == 0) return INVALID_TILE;
    00101
    00102   return ::ToTileIndexDiff(st->Airport()->airport_depots[0]) + st->airport_tile;
    00103 }
    00104
    00105 /* static */ AIAirport::AirportType AIAirport::GetAirportType(TileIndex tile)
    00106 {
    00107   if (!AITile::IsStationTile(tile)) return AT_INVALID;
    00108
    00109   StationID station_id = ::GetStationIndex(tile);
    00110
    00111   if (!AIStation::HasStationType(station_id, AIStation::STATION_AIRPORT)) return AT_INVALID;
    00112
    00113   return (AirportType)::GetStation(station_id)->airport_type;
    00114 }
    00115
    00116
    00117 /* static */ int AIAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
    00118 {
    00119   extern Town *AirportGetNearestTown(const AirportFTAClass *afc, TileIndex airport_tile);
    00120   extern uint8 GetAirportNoiseLevelForTown(const AirportFTAClass *afc, TileIndex town_tile, TileIndex tile);
    00121
    00122   if (!::IsValidTile(tile)) return -1;
    00123   if (!IsValidAirportType(type)) return -1;
    00124
    00125   if (_settings_game.economy.station_noise_level) {
    00126     const AirportFTAClass *afc = ::GetAirport(type);
    00127     const Town *t = AirportGetNearestTown(afc, tile);
    00128     return GetAirportNoiseLevelForTown(afc, t->xy, tile);
    00129   }
    00130
    00131   return 1;
    00132 }
    00133
    00134 /* static */ TownID AIAirport::GetNearestTown(TileIndex tile, AirportType type)
    00135 {
    00136   extern Town *AirportGetNearestTown(const AirportFTAClass *afc, TileIndex airport_tile);
    00137
    00138   if (!::IsValidTile(tile)) return INVALID_TOWN;
    00139   if (!IsValidAirportType(type)) return INVALID_TOWN;
    00140
    00141   return AirportGetNearestTown(GetAirport(type), tile)->index;
    00142 }