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