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