ai_airport.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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 bool AIAirport::IsValidAirportType(AirportType type)
00019 {
00020 return IsAirportInformationAvailable(type) && ::AirportSpec::Get(type)->IsAvailable();
00021 }
00022
00023 bool AIAirport::IsAirportInformationAvailable(AirportType type)
00024 {
00025 return type >= 0 && type < (AirportType)NUM_AIRPORTS;
00026 }
00027
00028 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 bool AIAirport::IsHangarTile(TileIndex tile)
00037 {
00038 if (!::IsValidTile(tile)) return false;
00039
00040 return ::IsTileType(tile, MP_STATION) && ::IsHangar(tile);
00041 }
00042
00043 bool AIAirport::IsAirportTile(TileIndex tile)
00044 {
00045 if (!::IsValidTile(tile)) return false;
00046
00047 return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
00048 }
00049
00050 int32 AIAirport::GetAirportWidth(AirportType type)
00051 {
00052 if (!IsAirportInformationAvailable(type)) return -1;
00053
00054 return ::AirportSpec::Get(type)->size_x;
00055 }
00056
00057 int32 AIAirport::GetAirportHeight(AirportType type)
00058 {
00059 if (!IsAirportInformationAvailable(type)) return -1;
00060
00061 return ::AirportSpec::Get(type)->size_y;
00062 }
00063
00064 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 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 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 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 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 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 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 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 }