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