ai_station.cpp

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

Generated on Mon Jun 8 23:04:02 2009 for OpenTTD by  doxygen 1.5.6