# ai_station.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_station.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00005 #include "ai_station.hpp"
    00006 #include "ai_cargo.hpp"
    00007 #include "ai_map.hpp"
    00008 #include "ai_town.hpp"
    00009 #include "../../command_func.h"
    00010 #include "../../debug.h"
    00011 #include "../../station_map.h"
    00012 #include "../../string_func.h"
    00013 #include "../../strings_func.h"
    00014 #include "../../company_func.h"
    00015 #include "../../town.h"
    00016 #include "table/strings.h"
    00017
    00018 /* static */ bool AIStation::IsValidStation(StationID station_id)
    00019 {
    00020   return ::IsValidStationID(station_id) && ::GetStation(station_id)->owner == _current_company;
    00021 }
    00022
    00023 /* static */ StationID AIStation::GetStationID(TileIndex tile)
    00024 {
    00025   if (!::IsValidTile(tile) || !::IsTileType(tile, MP_STATION)) return INVALID_STATION;
    00026   return ::GetStationIndex(tile);
    00027 }
    00028
    00029 /* static */ char *AIStation::GetName(StationID station_id)
    00030 {
    00031   if (!IsValidStation(station_id)) return NULL;
    00032
    00033   static const int len = 64;
    00034   char *station_name = MallocT<char>(len);
    00035
    00036   ::SetDParam(0, GetStation(station_id)->index);
    00037   ::GetString(station_name, STR_STATION, &station_name[len - 1]);
    00038   return station_name;
    00039 }
    00040
    00041 /* static */ bool AIStation::SetName(StationID station_id, const char *name)
    00042 {
    00043   EnforcePrecondition(false, IsValidStation(station_id));
    00044   EnforcePrecondition(false, !::StrEmpty(name));
    00045   EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_STATION_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
    00046
    00047   return AIObject::DoCommand(0, station_id, 0, CMD_RENAME_STATION, name);
    00048 }
    00049
    00050 /* static */ TileIndex AIStation::GetLocation(StationID station_id)
    00051 {
    00052   if (!IsValidStation(station_id)) return INVALID_TILE;
    00053
    00054   return ::GetStation(station_id)->xy;
    00055 }
    00056
    00057 /* static */ int32 AIStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
    00058 {
    00059   if (!IsValidStation(station_id)) return -1;
    00060   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00061
    00062   return ::GetStation(station_id)->goods[cargo_id].cargo.Count();
    00063 }
    00064
    00065 /* static */ int32 AIStation::GetCargoRating(StationID station_id, CargoID cargo_id)
    00066 {
    00067   if (!IsValidStation(station_id)) return -1;
    00068   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00069
    00070   return ::GetStation(station_id)->goods[cargo_id].rating * 101 >> 8;
    00071 }
    00072
    00073 /* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType station_type)
    00074 {
    00075   if (station_type == STATION_AIRPORT) {
    00076     DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
    00077     return -1;
    00078   }
    00079   if (CountBits(station_type) != 1) return -1;
    00080   if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
    00081
    00082   switch (station_type) {
    00083     case STATION_TRAIN:      return CA_TRAIN;
    00084     case STATION_TRUCK_STOP: return CA_TRUCK;
    00085     case STATION_BUS_STOP:   return CA_BUS;
    00086     case STATION_DOCK:       return CA_DOCK;
    00087     default:                 return CA_NONE;
    00088   }
    00089 }
    00090
    00091 /* static */ int32 AIStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
    00092 {
    00093   if (!IsValidStation(station_id)) return -1;
    00094
    00095   return AIMap::DistanceManhattan(tile, GetLocation(station_id));
    00096 }
    00097
    00098 /* static */ int32 AIStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
    00099 {
    00100   if (!IsValidStation(station_id)) return -1;
    00101
    00102   return AIMap::DistanceSquare(tile, GetLocation(station_id));
    00103 }
    00104
    00105 /* static */ bool AIStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
    00106 {
    00107   if (!IsValidStation(station_id)) return false;
    00108
    00109   return AITown::IsWithinTownInfluence(town_id, GetLocation(station_id));
    00110 }
    00111
    00112 /* static */ bool AIStation::HasStationType(StationID station_id, StationType station_type)
    00113 {
    00114   if (!IsValidStation(station_id)) return false;
    00115   if (CountBits(station_type) != 1) return false;
    00116
    00117   return (::GetStation(station_id)->facilities & station_type) != 0;
    00118 }
    00119
    00120 /* static */ bool AIStation::HasRoadType(StationID station_id, AIRoad::RoadType road_type)
    00121 {
    00122   if (!IsValidStation(station_id)) return false;
    00123   if (!AIRoad::IsRoadTypeAvailable(road_type)) return false;
    00124
    00125 	::RoadTypes r = RoadTypeToRoadTypes((::RoadType)road_type);
    00126
    00127   for (const RoadStop *rs = ::GetStation(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != NULL; rs = rs->next) {
    00128     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
    00129   }
    00130   for (const RoadStop *rs = ::GetStation(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != NULL; rs = rs->next) {
    00131     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
    00132   }
    00133
    00134   return false;
    00135 }
    00136
    00137 /* static */ TownID AIStation::GetNearestTown(StationID station_id)
    00138 {
    00139   if (!IsValidStation(station_id)) return INVALID_TOWN;
    00140
    00141   return ::GetStation(station_id)->town->index;
    00142 }