# ai_station.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_station.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    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_station.hpp"
    00013 #include "ai_cargo.hpp"
    00014 #include "ai_map.hpp"
    00015 #include "ai_town.hpp"
    00016 #include "../../debug.h"
    00017 #include "../../station_base.h"
    00018 #include "../../roadstop_base.h"
    00019 #include "../../company_func.h"
    00020 #include "../../town.h"
    00021
    00022 /* static */ bool AIStation::IsValidStation(StationID station_id)
    00023 {
    00024   const Station *st = ::Station::GetIfValid(station_id);
    00025   return st != NULL && (st->owner == _current_company || st->owner == OWNER_NONE);
    00026 }
    00027
    00028 /* static */ StationID AIStation::GetStationID(TileIndex tile)
    00029 {
    00030   if (!::IsValidTile(tile) || !::IsTileType(tile, MP_STATION)) return INVALID_STATION;
    00031   return ::GetStationIndex(tile);
    00032 }
    00033
    00034 /* static */ int32 AIStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
    00035 {
    00036   if (!IsValidStation(station_id)) return -1;
    00037   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00038
    00039   return ::Station::Get(station_id)->goods[cargo_id].cargo.Count();
    00040 }
    00041
    00042 /* static */ int32 AIStation::GetCargoRating(StationID station_id, CargoID cargo_id)
    00043 {
    00044   if (!IsValidStation(station_id)) return -1;
    00045   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00046
    00047   return ::ToPercent8(::Station::Get(station_id)->goods[cargo_id].rating);
    00048 }
    00049
    00050 /* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType station_type)
    00051 {
    00052   if (station_type == STATION_AIRPORT) {
    00053     DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
    00054     return -1;
    00055   }
    00056   if (CountBits(station_type) != 1) return -1;
    00057   if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
    00058
    00059   switch (station_type) {
    00060     case STATION_TRAIN:      return CA_TRAIN;
    00061     case STATION_TRUCK_STOP: return CA_TRUCK;
    00062     case STATION_BUS_STOP:   return CA_BUS;
    00063     case STATION_DOCK:       return CA_DOCK;
    00064     default:                 return CA_NONE;
    00065   }
    00066 }
    00067
    00068 /* static */ int32 AIStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
    00069 {
    00070   if (!IsValidStation(station_id)) return -1;
    00071
    00072   return AIMap::DistanceManhattan(tile, GetLocation(station_id));
    00073 }
    00074
    00075 /* static */ int32 AIStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
    00076 {
    00077   if (!IsValidStation(station_id)) return -1;
    00078
    00079   return AIMap::DistanceSquare(tile, GetLocation(station_id));
    00080 }
    00081
    00082 /* static */ bool AIStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
    00083 {
    00084   if (!IsValidStation(station_id)) return false;
    00085
    00086   return AITown::IsWithinTownInfluence(town_id, GetLocation(station_id));
    00087 }
    00088
    00089 /* static */ bool AIStation::HasStationType(StationID station_id, StationType station_type)
    00090 {
    00091   if (!IsValidStation(station_id)) return false;
    00092   if (CountBits(station_type) != 1) return false;
    00093
    00094   return (::Station::Get(station_id)->facilities & station_type) != 0;
    00095 }
    00096
    00097 /* static */ bool AIStation::HasRoadType(StationID station_id, AIRoad::RoadType road_type)
    00098 {
    00099   if (!IsValidStation(station_id)) return false;
    00100   if (!AIRoad::IsRoadTypeAvailable(road_type)) return false;
    00101
    00102 	::RoadTypes r = RoadTypeToRoadTypes((::RoadType)road_type);
    00103
    00104   for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != NULL; rs = rs->next) {
    00105     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
    00106   }
    00107   for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != NULL; rs = rs->next) {
    00108     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
    00109   }
    00110
    00111   return false;
    00112 }
    00113
    00114 /* static */ TownID AIStation::GetNearestTown(StationID station_id)
    00115 {
    00116   if (!IsValidStation(station_id)) return INVALID_TOWN;
    00117
    00118   return ::Station::Get(station_id)->town->index;
    00119 }