ai_station.cpp

Go to the documentation of this file.
00001 /* $Id: ai_station.cpp 17080 2009-08-05 23:57:41Z 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::GetConstructionDate(StationID station_id)
00059 {
00060   if (!IsValidStation(station_id)) return -1;
00061 
00062   return ::GetStation(station_id)->build_date;
00063 }
00064 
00065 /* static */ int32 AIStation::GetCargoWaiting(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].cargo.Count();
00071 }
00072 
00073 /* static */ int32 AIStation::GetCargoRating(StationID station_id, CargoID cargo_id)
00074 {
00075   if (!IsValidStation(station_id)) return -1;
00076   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00077 
00078   return ::GetStation(station_id)->goods[cargo_id].rating * 101 >> 8;
00079 }
00080 
00081 /* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType station_type)
00082 {
00083   if (station_type == STATION_AIRPORT) {
00084     DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
00085     return -1;
00086   }
00087   if (CountBits(station_type) != 1) return -1;
00088   if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
00089 
00090   switch (station_type) {
00091     case STATION_TRAIN:      return CA_TRAIN;
00092     case STATION_TRUCK_STOP: return CA_TRUCK;
00093     case STATION_BUS_STOP:   return CA_BUS;
00094     case STATION_DOCK:       return CA_DOCK;
00095     default:                 return CA_NONE;
00096   }
00097 }
00098 
00099 /* static */ int32 AIStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
00100 {
00101   if (!IsValidStation(station_id)) return -1;
00102 
00103   return AIMap::DistanceManhattan(tile, GetLocation(station_id));
00104 }
00105 
00106 /* static */ int32 AIStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
00107 {
00108   if (!IsValidStation(station_id)) return -1;
00109 
00110   return AIMap::DistanceSquare(tile, GetLocation(station_id));
00111 }
00112 
00113 /* static */ bool AIStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
00114 {
00115   if (!IsValidStation(station_id)) return false;
00116 
00117   return AITown::IsWithinTownInfluence(town_id, GetLocation(station_id));
00118 }
00119 
00120 /* static */ bool AIStation::HasStationType(StationID station_id, StationType station_type)
00121 {
00122   if (!IsValidStation(station_id)) return false;
00123   if (CountBits(station_type) != 1) return false;
00124 
00125   return (::GetStation(station_id)->facilities & station_type) != 0;
00126 }
00127 
00128 /* static */ bool AIStation::HasRoadType(StationID station_id, AIRoad::RoadType road_type)
00129 {
00130   if (!IsValidStation(station_id)) return false;
00131   if (!AIRoad::IsRoadTypeAvailable(road_type)) return false;
00132 
00133 	::RoadTypes r = RoadTypeToRoadTypes((::RoadType)road_type);
00134 
00135   for (const RoadStop *rs = ::GetStation(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != NULL; rs = rs->next) {
00136     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
00137   }
00138   for (const RoadStop *rs = ::GetStation(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != NULL; rs = rs->next) {
00139     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
00140   }
00141 
00142   return false;
00143 }
00144 
00145 /* static */ TownID AIStation::GetNearestTown(StationID station_id)
00146 {
00147   if (!IsValidStation(station_id)) return INVALID_TOWN;
00148 
00149   return ::GetStation(station_id)->town->index;
00150 }

Generated on Thu Sep 24 19:35:00 2009 for OpenTTD by  doxygen 1.5.6