# ai_airport.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_airport.cpp 18860 2010-01-18 14:32:47Z yexo $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "ai_airport.hpp"
    00013 #include "ai_station.hpp"
    00014 #include "../../station_base.h"
    00015 #include "../../company_func.h"
    00016 #include "../../town.h"
    00017
    00018 /* static */ bool AIAirport::IsValidAirportType(AirportType type)
    00019 {
    00020   return IsAirportInformationAvailable(type) && ::AirportSpec::Get(type)->IsAvailable();
    00021 }
    00022
    00023 /* static */ bool AIAirport::IsAirportInformationAvailable(AirportType type)
    00024 {
    00025   return type >= 0 && type < (AirportType)NUM_AIRPORTS;
    00026 }
    00027
    00028 /* static */ Money AIAirport::GetPrice(AirportType type)
    00029 {
    00030   if (!IsValidAirportType(type)) return -1;
    00031
    00032   const AirportSpec *as = ::AirportSpec::Get(type);
    00033   return _price[PR_BUILD_STATION_AIRPORT] * as->size_x * as->size_y;
    00034 }
    00035
    00036 /* static */ bool AIAirport::IsHangarTile(TileIndex tile)
    00037 {
    00038   if (!::IsValidTile(tile)) return false;
    00039
    00040   return ::IsTileType(tile, MP_STATION) && ::IsHangar(tile);
    00041 }
    00042
    00043 /* static */ bool AIAirport::IsAirportTile(TileIndex tile)
    00044 {
    00045   if (!::IsValidTile(tile)) return false;
    00046
    00047   return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
    00048 }
    00049
    00050 /* static */ int32 AIAirport::GetAirportWidth(AirportType type)
    00051 {
    00052   if (!IsAirportInformationAvailable(type)) return -1;
    00053
    00054   return ::AirportSpec::Get(type)->size_x;
    00055 }
    00056
    00057 /* static */ int32 AIAirport::GetAirportHeight(AirportType type)
    00058 {
    00059   if (!IsAirportInformationAvailable(type)) return -1;
    00060
    00061   return ::AirportSpec::Get(type)->size_y;
    00062 }
    00063
    00064 /* static */ int32 AIAirport::GetAirportCoverageRadius(AirportType type)
    00065 {
    00066   if (!IsAirportInformationAvailable(type)) return -1;
    00067
    00068   return _settings_game.station.modified_catchment ? ::AirportSpec::Get(type)->catchment : (uint)CA_UNMODIFIED;
    00069 }
    00070
    00071 /* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type, StationID station_id)
    00072 {
    00073   EnforcePrecondition(false, ::IsValidTile(tile));
    00074   EnforcePrecondition(false, IsValidAirportType(type));
    00075   EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
    00076
    00077   uint p2 = station_id == AIStation::STATION_JOIN_ADJACENT ? 0 : 1;
    00078   p2 |= (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
    00079   return AIObject::DoCommand(tile, type, p2, CMD_BUILD_AIRPORT);
    00080 }
    00081
    00082 /* static */ bool AIAirport::RemoveAirport(TileIndex tile)
    00083 {
    00084   EnforcePrecondition(false, ::IsValidTile(tile))
    00085   EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
    00086
    00087   return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00088 }
    00089
    00090 /* static */ int32 AIAirport::GetNumHangars(TileIndex tile)
    00091 {
    00092   if (!::IsValidTile(tile)) return -1;
    00093   if (!::IsTileType(tile, MP_STATION)) return -1;
    00094
    00095   const Station *st = ::Station::GetByTile(tile);
    00096   if (st->owner != _current_company) return -1;
    00097   if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
    00098
    00099   return st->GetAirportSpec()->nof_depots;
    00100 }
    00101
    00102 /* static */ TileIndex AIAirport::GetHangarOfAirport(TileIndex tile)
    00103 {
    00104   if (!::IsValidTile(tile)) return INVALID_TILE;
    00105   if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
    00106   if (GetNumHangars(tile) < 1) return INVALID_TILE;
    00107
    00108   const Station *st = ::Station::GetByTile(tile);
    00109   if (st->owner != _current_company) return INVALID_TILE;
    00110   if ((st->facilities & FACIL_AIRPORT) == 0) return INVALID_TILE;
    00111
    00112   return st->GetHangarTile(0);
    00113 }
    00114
    00115 /* static */ AIAirport::AirportType AIAirport::GetAirportType(TileIndex tile)
    00116 {
    00117   if (!AITile::IsStationTile(tile)) return AT_INVALID;
    00118
    00119   StationID station_id = ::GetStationIndex(tile);
    00120
    00121   if (!AIStation::HasStationType(station_id, AIStation::STATION_AIRPORT)) return AT_INVALID;
    00122
    00123   return (AirportType)::Station::Get(station_id)->airport_type;
    00124 }
    00125
    00126
    00127 /* static */ int AIAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
    00128 {
    00129   extern Town *AirportGetNearestTown(const AirportSpec *as, TileIndex airport_tile);
    00130   extern uint8 GetAirportNoiseLevelForTown(const AirportSpec *as, TileIndex town_tile, TileIndex tile);
    00131
    00132   if (!::IsValidTile(tile)) return -1;
    00133   if (!IsValidAirportType(type)) return -1;
    00134
    00135   if (_settings_game.economy.station_noise_level) {
    00136     const AirportSpec *as = ::AirportSpec::Get(type);
    00137     const Town *t = AirportGetNearestTown(as, tile);
    00138     return GetAirportNoiseLevelForTown(as, t->xy, tile);
    00139   }
    00140
    00141   return 1;
    00142 }
    00143
    00144 /* static */ TownID AIAirport::GetNearestTown(TileIndex tile, AirportType type)
    00145 {
    00146   extern Town *AirportGetNearestTown(const AirportSpec *as, TileIndex airport_tile);
    00147
    00148   if (!::IsValidTile(tile)) return INVALID_TOWN;
    00149   if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
    00150
    00151   return AirportGetNearestTown(AirportSpec::Get(type), tile)->index;
    00152 }