ai_airport.cpp

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

Generated on Thu Apr 14 00:48:10 2011 for OpenTTD by  doxygen 1.6.1