# ai_rail.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_rail.hpp"
    00014 #include "ai_map.hpp"
    00015 #include "ai_station.hpp"
    00016 #include "ai_industrytype.hpp"
    00017 #include "../../debug.h"
    00018 #include "../../station_base.h"
    00019 #include "../../company_func.h"
    00020 #include "../../newgrf.h"
    00021 #include "../../newgrf_generic.h"
    00022 #include "../../newgrf_station.h"
    00023 #include "../../strings_func.h"
    00024
    00025 /* static */ char *AIRail::GetName(RailType rail_type)
    00026 {
    00027   if (!IsRailTypeAvailable(rail_type)) return NULL;
    00028
    00029   static const int len = 64;
    00030   char *railtype_name = MallocT<char>(len);
    00031
    00032   ::GetString(railtype_name, GetRailTypeInfo((::RailType)rail_type)->strings.menu_text, &railtype_name[len - 1]);
    00033   return railtype_name;
    00034 }
    00035
    00036 /* static */ bool AIRail::IsRailTile(TileIndex tile)
    00037 {
    00038   if (!::IsValidTile(tile)) return false;
    00039
    00040   return (::IsTileType(tile, MP_RAILWAY) && !::IsRailDepot(tile)) ||
    00041       (::HasStationTileRail(tile) && !::IsStationTileBlocked(tile)) || ::IsLevelCrossingTile(tile);
    00042 }
    00043
    00044 /* static */ bool AIRail::IsLevelCrossingTile(TileIndex tile)
    00045 {
    00046   if (!::IsValidTile(tile)) return false;
    00047
    00048   return ::IsLevelCrossingTile(tile);
    00049 }
    00050
    00051 /* static */ bool AIRail::IsRailDepotTile(TileIndex tile)
    00052 {
    00053   if (!::IsValidTile(tile)) return false;
    00054
    00055   return ::IsRailDepotTile(tile);
    00056 }
    00057
    00058 /* static */ bool AIRail::IsRailStationTile(TileIndex tile)
    00059 {
    00060   if (!::IsValidTile(tile)) return false;
    00061
    00062   return ::IsRailStationTile(tile);
    00063 }
    00064
    00065 /* static */ bool AIRail::IsRailWaypointTile(TileIndex tile)
    00066 {
    00067   if (!::IsValidTile(tile)) return false;
    00068
    00069   return ::IsRailWaypointTile(tile);
    00070 }
    00071
    00072 /* static */ bool AIRail::IsRailTypeAvailable(RailType rail_type)
    00073 {
    00074   if ((::RailType)rail_type < RAILTYPE_BEGIN || (::RailType)rail_type >= RAILTYPE_END) return false;
    00075
    00076   return ::HasRailtypeAvail(_current_company, (::RailType)rail_type);
    00077 }
    00078
    00079 /* static */ AIRail::RailType AIRail::GetCurrentRailType()
    00080 {
    00081   return (RailType)AIObject::GetRailType();
    00082 }
    00083
    00084 /* static */ void AIRail::SetCurrentRailType(RailType rail_type)
    00085 {
    00086   if (!IsRailTypeAvailable(rail_type)) return;
    00087
    00088   AIObject::SetRailType((::RailType)rail_type);
    00089 }
    00090
    00091 /* static */ bool AIRail::TrainCanRunOnRail(AIRail::RailType engine_rail_type, AIRail::RailType track_rail_type)
    00092 {
    00093   if (!AIRail::IsRailTypeAvailable(engine_rail_type)) return false;
    00094   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00095
    00096   return ::IsCompatibleRail((::RailType)engine_rail_type, (::RailType)track_rail_type);
    00097 }
    00098
    00099 /* static */ bool AIRail::TrainHasPowerOnRail(AIRail::RailType engine_rail_type, AIRail::RailType track_rail_type)
    00100 {\
    00101   if (!AIRail::IsRailTypeAvailable(engine_rail_type)) return false;
    00102   if (!AIRail::IsRailTypeAvailable(track_rail_type)) return false;
    00103
    00104   return ::HasPowerOnRail((::RailType)engine_rail_type, (::RailType)track_rail_type);
    00105 }
    00106
    00107 /* static */ AIRail::RailType AIRail::GetRailType(TileIndex tile)
    00108 {
    00109   if (!AITile::HasTransportType(tile, AITile::TRANSPORT_RAIL)) return RAILTYPE_INVALID;
    00110
    00111   return (RailType)::GetRailType(tile);
    00112 }
    00113
    00114 /* static */ bool AIRail::ConvertRailType(TileIndex start_tile, TileIndex end_tile, AIRail::RailType convert_to)
    00115 {
    00116   EnforcePrecondition(false, ::IsValidTile(start_tile));
    00117   EnforcePrecondition(false, ::IsValidTile(end_tile));
    00118   EnforcePrecondition(false, IsRailTypeAvailable(convert_to));
    00119
    00120   return AIObject::DoCommand(start_tile, end_tile, convert_to, CMD_CONVERT_RAIL);
    00121 }
    00122
    00123 /* static */ TileIndex AIRail::GetRailDepotFrontTile(TileIndex depot)
    00124 {
    00125   if (!IsRailDepotTile(depot)) return INVALID_TILE;
    00126
    00127   return depot + ::TileOffsByDiagDir(::GetRailDepotDirection(depot));
    00128 }
    00129
    00130 /* static */ AIRail::RailTrack AIRail::GetRailStationDirection(TileIndex tile)
    00131 {
    00132   if (!IsRailStationTile(tile)) return RAILTRACK_INVALID;
    00133
    00134   return (RailTrack)::GetRailStationTrackBits(tile);
    00135 }
    00136
    00137 /* static */ bool AIRail::BuildRailDepot(TileIndex tile, TileIndex front)
    00138 {
    00139   EnforcePrecondition(false, tile != front);
    00140   EnforcePrecondition(false, ::IsValidTile(tile));
    00141   EnforcePrecondition(false, ::IsValidTile(front));
    00142   EnforcePrecondition(false, ::TileX(tile) == ::TileX(front) || ::TileY(tile) == ::TileY(front));
    00143   EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
    00144
    00145   uint entrance_dir = (::TileX(tile) == ::TileX(front)) ? (::TileY(tile) < ::TileY(front) ? 1 : 3) : (::TileX(tile) < ::TileX(front) ? 2 : 0);
    00146
    00147   return AIObject::DoCommand(tile, AIObject::GetRailType(), entrance_dir, CMD_BUILD_TRAIN_DEPOT);
    00148 }
    00149
    00150 /* static */ bool AIRail::BuildRailStation(TileIndex tile, RailTrack direction, uint num_platforms, uint platform_length, StationID station_id)
    00151 {
    00152   EnforcePrecondition(false, ::IsValidTile(tile));
    00153   EnforcePrecondition(false, direction == RAILTRACK_NW_SE || direction == RAILTRACK_NE_SW);
    00154   EnforcePrecondition(false, num_platforms > 0 && num_platforms <= 0xFF);
    00155   EnforcePrecondition(false, platform_length > 0 && platform_length <= 0xFF);
    00156   EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
    00157   EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
    00158
    00159   uint32 p1 = GetCurrentRailType() | (platform_length << 16) | (num_platforms << 8);
    00160   if (direction == RAILTRACK_NW_SE) p1 |= (1 << 4);
    00161   if (station_id != AIStation::STATION_JOIN_ADJACENT) p1 |= (1 << 24);
    00162   return AIObject::DoCommand(tile, p1, (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16, CMD_BUILD_RAIL_STATION);
    00163 }
    00164
    00165 /* static */ bool AIRail::BuildNewGRFRailStation(TileIndex tile, RailTrack direction, uint num_platforms, uint platform_length, StationID station_id, CargoID cargo_id, IndustryType source_industry, IndustryType goal_industry, int distance, bool source_station)
    00166 {
    00167   EnforcePrecondition(false, ::IsValidTile(tile));
    00168   EnforcePrecondition(false, direction == RAILTRACK_NW_SE || direction == RAILTRACK_NE_SW);
    00169   EnforcePrecondition(false, num_platforms > 0 && num_platforms <= 0xFF);
    00170   EnforcePrecondition(false, platform_length > 0 && platform_length <= 0xFF);
    00171   EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
    00172   EnforcePrecondition(false, station_id == AIStation::STATION_NEW || station_id == AIStation::STATION_JOIN_ADJACENT || AIStation::IsValidStation(station_id));
    00173   EnforcePrecondition(false, source_industry == AIIndustryType::INDUSTRYTYPE_UNKNOWN || source_industry == AIIndustryType::INDUSTRYTYPE_TOWN || AIIndustryType::IsValidIndustryType(source_industry));
    00174   EnforcePrecondition(false, goal_industry   == AIIndustryType::INDUSTRYTYPE_UNKNOWN || goal_industry   == AIIndustryType::INDUSTRYTYPE_TOWN || AIIndustryType::IsValidIndustryType(goal_industry));
    00175
    00176   uint32 p1 = GetCurrentRailType() | (platform_length << 16) | (num_platforms << 8);
    00177   if (direction == RAILTRACK_NW_SE) p1 |= 1 << 4;
    00178   if (station_id != AIStation::STATION_JOIN_ADJACENT) p1 |= (1 << 24);
    00179
    00180   const GRFFile *file;
    00181   uint16 res = GetAiPurchaseCallbackResult(GSF_STATIONS, cargo_id, 0, source_industry, goal_industry, min(255, distance / 2), AICE_STATION_GET_STATION_ID, source_station ? 0 : 1, min(15, num_platforms) << 4 | min(15, platform_length), &file);
    00182   uint32 p2 = (AIStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
    00183   if (res != CALLBACK_FAILED) {
    00184     int index = 0;
    00185     const StationSpec *spec = StationClass::GetByGrf(file->grfid, res, &index);
    00186     if (spec == NULL) {
    00187       DEBUG(grf, 1, "%s returned an invalid station ID for 'AI construction/purchase selection (18)' callback", file->filename);
    00188     } else {
    00189       p2 |= spec->cls_id | index << 8;
    00190     }
    00191
    00192   }
    00193   return AIObject::DoCommand(tile, p1, p2, CMD_BUILD_RAIL_STATION);
    00194 }
    00195
    00196 /* static */ bool AIRail::BuildRailWaypoint(TileIndex tile)
    00197 {
    00198   EnforcePrecondition(false, ::IsValidTile(tile));
    00199   EnforcePrecondition(false, IsRailTile(tile));
    00200   EnforcePrecondition(false, GetRailTracks(tile) == RAILTRACK_NE_SW || GetRailTracks(tile) == RAILTRACK_NW_SE);
    00201   EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
    00202
    00203   return AIObject::DoCommand(tile, GetCurrentRailType() | (GetRailTracks(tile) == RAILTRACK_NE_SW ? AXIS_X : AXIS_Y) << 4 | 1 << 8 | 1 << 16, STAT_CLASS_WAYP | INVALID_STATION << 16, CMD_BUILD_RAIL_WAYPOINT);
    00204 }
    00205
    00206 /* static */ bool AIRail::RemoveRailWaypointTileRectangle(TileIndex tile, TileIndex tile2, bool keep_rail)
    00207 {
    00208   EnforcePrecondition(false, ::IsValidTile(tile));
    00209   EnforcePrecondition(false, ::IsValidTile(tile2));
    00210
    00211   return AIObject::DoCommand(tile, tile2, keep_rail ? 1 : 0, CMD_REMOVE_FROM_RAIL_WAYPOINT);
    00212 }
    00213
    00214 /* static */ bool AIRail::RemoveRailStationTileRectangle(TileIndex tile, TileIndex tile2, bool keep_rail)
    00215 {
    00216   EnforcePrecondition(false, ::IsValidTile(tile));
    00217   EnforcePrecondition(false, ::IsValidTile(tile2));
    00218
    00219   return AIObject::DoCommand(tile, tile2, keep_rail ? 1 : 0, CMD_REMOVE_FROM_RAIL_STATION);
    00220 }
    00221
    00222 /* static */ uint AIRail::GetRailTracks(TileIndex tile)
    00223 {
    00224   if (!IsRailTile(tile)) return RAILTRACK_INVALID;
    00225
    00226   if (IsRailStationTile(tile) || IsRailWaypointTile(tile)) return ::TrackToTrackBits(::GetRailStationTrack(tile));
    00227   if (IsLevelCrossingTile(tile)) return ::GetCrossingRailBits(tile);
    00228   if (IsRailDepotTile(tile)) return ::TRACK_BIT_NONE;
    00229   return ::GetTrackBits(tile);
    00230 }
    00231
    00232 /* static */ bool AIRail::BuildRailTrack(TileIndex tile, RailTrack rail_track)
    00233 {
    00234   EnforcePrecondition(false, ::IsValidTile(tile));
    00235   EnforcePrecondition(false, rail_track != 0);
    00236   EnforcePrecondition(false, (rail_track & ~::TRACK_BIT_ALL) == 0);
    00237   EnforcePrecondition(false, KillFirstBit((uint)rail_track) == 0);
    00238   EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
    00239
    00240   return AIObject::DoCommand(tile, tile, GetCurrentRailType() | (FindFirstTrack((::TrackBits)rail_track) << 4), CMD_BUILD_RAILROAD_TRACK);
    00241 }
    00242
    00243 /* static */ bool AIRail::RemoveRailTrack(TileIndex tile, RailTrack rail_track)
    00244 {
    00245   EnforcePrecondition(false, ::IsValidTile(tile));
    00246   EnforcePrecondition(false, ::IsPlainRailTile(tile) || ::IsLevelCrossingTile(tile));
    00247   EnforcePrecondition(false, GetRailTracks(tile) & rail_track);
    00248   EnforcePrecondition(false, KillFirstBit((uint)rail_track) == 0);
    00249
    00250   return AIObject::DoCommand(tile, tile, GetCurrentRailType() | (FindFirstTrack((::TrackBits)rail_track) << 4), CMD_REMOVE_RAILROAD_TRACK);
    00251 }
    00252
    00253 /* static */ bool AIRail::AreTilesConnected(TileIndex from, TileIndex tile, TileIndex to)
    00254 {
    00255   if (!IsRailTile(tile)) return false;
    00256   if (from == to || AIMap::DistanceManhattan(from, tile) != 1 || AIMap::DistanceManhattan(tile, to) != 1) return false;
    00257
    00258   if (to < from) ::Swap(from, to);
    00259
    00260   if (tile - from == 1) {
    00261     if (to - tile == 1) return (GetRailTracks(tile) & RAILTRACK_NE_SW) != 0;
    00262     if (to - tile == ::MapSizeX()) return (GetRailTracks(tile) & RAILTRACK_NE_SE) != 0;
    00263   } else if (tile - from == ::MapSizeX()) {
    00264     if (tile - to == 1) return (GetRailTracks(tile) & RAILTRACK_NW_NE) != 0;
    00265     if (to - tile == 1) return (GetRailTracks(tile) & RAILTRACK_NW_SW) != 0;
    00266     if (to - tile == ::MapSizeX()) return (GetRailTracks(tile) & RAILTRACK_NW_SE) != 0;
    00267   } else {
    00268     return (GetRailTracks(tile) & RAILTRACK_SW_SE) != 0;
    00269   }
    00270
    00271   NOT_REACHED();
    00272 }
    00273
    00278 static uint32 SimulateDrag(TileIndex from, TileIndex tile, TileIndex *to)
    00279 {
    00280   int diag_offset = abs(abs((int)::TileX(*to) - (int)::TileX(tile)) - abs((int)::TileY(*to) - (int)::TileY(tile)));
    00281   uint32 p2 = AIRail::GetCurrentRailType();
    00282   if (::TileY(from) == ::TileY(*to)) {
    00283     p2 |= (TRACK_X << 4);
    00284     *to -= Clamp((int)::TileX(*to) - (int)::TileX(tile), -1, 1);
    00285   } else if (::TileX(from) == ::TileX(*to)) {
    00286     p2 |= (TRACK_Y << 4);
    00287     *to -= ::MapSizeX() * Clamp((int)::TileY(*to) - (int)::TileY(tile), -1, 1);
    00288   } else if (::TileY(from) < ::TileY(tile)) {
    00289     if (::TileX(*to) < ::TileX(tile)) {
    00290       p2 |= (TRACK_UPPER << 4);
    00291     } else {
    00292       p2 |= (TRACK_LEFT << 4);
    00293     }
    00294     if (diag_offset) {
    00295       *to -= Clamp((int)::TileX(*to) - (int)::TileX(tile), -1, 1);
    00296     } else {
    00297       *to -= ::MapSizeX() * Clamp((int)::TileY(*to) - (int)::TileY(tile), -1, 1);
    00298     }
    00299   } else if (::TileY(from) > ::TileY(tile)) {
    00300     if (::TileX(*to) < ::TileX(tile)) {
    00301       p2 |= (TRACK_RIGHT << 4);
    00302     } else {
    00303       p2 |= (TRACK_LOWER << 4);
    00304     }
    00305     if (diag_offset) {
    00306       *to -= Clamp((int)::TileX(*to) - (int)::TileX(tile), -1, 1);
    00307     } else {
    00308       *to -= ::MapSizeX() * Clamp((int)::TileY(*to) - (int)::TileY(tile), -1, 1);
    00309     }
    00310   } else if (::TileX(from) < ::TileX(tile)) {
    00311     if (::TileY(*to) < ::TileY(tile)) {
    00312       p2 |= (TRACK_UPPER << 4);
    00313     } else {
    00314       p2 |= (TRACK_RIGHT << 4);
    00315     }
    00316     if (!diag_offset) {
    00317       *to -= Clamp((int)::TileX(*to) - (int)::TileX(tile), -1, 1);
    00318     } else {
    00319       *to -= ::MapSizeX() * Clamp((int)::TileY(*to) - (int)::TileY(tile), -1, 1);
    00320     }
    00321   } else if (::TileX(from) > ::TileX(tile)) {
    00322     if (::TileY(*to) < ::TileY(tile)) {
    00323       p2 |= (TRACK_LEFT << 4);
    00324     } else {
    00325       p2 |= (TRACK_LOWER << 4);
    00326     }
    00327     if (!diag_offset) {
    00328       *to -= Clamp((int)::TileX(*to) - (int)::TileX(tile), -1, 1);
    00329     } else {
    00330       *to -= ::MapSizeX() * Clamp((int)::TileY(*to) - (int)::TileY(tile), -1, 1);
    00331     }
    00332   }
    00333   return p2;
    00334 }
    00335
    00336 /* static */ bool AIRail::BuildRail(TileIndex from, TileIndex tile, TileIndex to)
    00337 {
    00338   EnforcePrecondition(false, ::IsValidTile(from));
    00339   EnforcePrecondition(false, ::IsValidTile(tile));
    00340   EnforcePrecondition(false, ::IsValidTile(to));
    00341   EnforcePrecondition(false, ::DistanceManhattan(from, tile) == 1);
    00342   EnforcePrecondition(false, ::DistanceManhattan(tile, to) >= 1);
    00343   EnforcePrecondition(false, IsRailTypeAvailable(GetCurrentRailType()));
    00344   int diag_offset = abs(abs((int)::TileX(to) - (int)::TileX(tile)) - abs((int)::TileY(to) - (int)::TileY(tile)));
    00345   EnforcePrecondition(false, diag_offset <= 1 ||
    00346       (::TileX(from) == ::TileX(tile) && ::TileX(tile) == ::TileX(to)) ||
    00347       (::TileY(from) == ::TileY(tile) && ::TileY(tile) == ::TileY(to)));
    00348
    00349   uint32 p2 = SimulateDrag(from, tile, &to) | 1 << 8;
    00350   return AIObject::DoCommand(tile, to, p2, CMD_BUILD_RAILROAD_TRACK);
    00351 }
    00352
    00353 /* static */ bool AIRail::RemoveRail(TileIndex from, TileIndex tile, TileIndex to)
    00354 {
    00355   EnforcePrecondition(false, ::IsValidTile(from));
    00356   EnforcePrecondition(false, ::IsValidTile(tile));
    00357   EnforcePrecondition(false, ::IsValidTile(to));
    00358   EnforcePrecondition(false, ::DistanceManhattan(from, tile) == 1);
    00359   EnforcePrecondition(false, ::DistanceManhattan(tile, to) >= 1);
    00360   int diag_offset = abs(abs((int)::TileX(to) - (int)::TileX(tile)) - abs((int)::TileY(to) - (int)::TileY(tile)));
    00361   EnforcePrecondition(false, diag_offset <= 1 ||
    00362       (::TileX(from) == ::TileX(tile) && ::TileX(tile) == ::TileX(to)) ||
    00363       (::TileY(from) == ::TileY(tile) && ::TileY(tile) == ::TileY(to)));
    00364
    00365   if (!IsRailTypeAvailable(GetCurrentRailType())) SetCurrentRailType(GetRailType(tile));
    00366   uint32 p2 = SimulateDrag(from, tile, &to);
    00367   return AIObject::DoCommand(tile, to, p2, CMD_REMOVE_RAILROAD_TRACK);
    00368 }
    00369
    00374 struct AIRailSignalData {
    00375   Track track;
    00376   Trackdir trackdir;
    00377   uint signal_cycles;
    00378 };
    00379
    00380 static const int NUM_TRACK_DIRECTIONS = 3;
    00381
    00388 static const AIRailSignalData _possible_trackdirs[5][NUM_TRACK_DIRECTIONS] = {
    00389   {{TRACK_UPPER,   TRACKDIR_UPPER_E, 0}, {TRACK_Y,       TRACKDIR_Y_SE,    0}, {TRACK_LEFT,    TRACKDIR_LEFT_S,  1}},
    00390   {{TRACK_RIGHT,   TRACKDIR_RIGHT_S, 1}, {TRACK_X,       TRACKDIR_X_SW,    1}, {TRACK_UPPER,   TRACKDIR_UPPER_W, 1}},
    00391   {{INVALID_TRACK, INVALID_TRACKDIR, 0}, {INVALID_TRACK, INVALID_TRACKDIR, 0}, {INVALID_TRACK, INVALID_TRACKDIR, 0}},
    00392   {{TRACK_LOWER,   TRACKDIR_LOWER_E, 0}, {TRACK_X,       TRACKDIR_X_NE,    0}, {TRACK_LEFT,    TRACKDIR_LEFT_N,  0}},
    00393   {{TRACK_RIGHT,   TRACKDIR_RIGHT_N, 0}, {TRACK_Y,       TRACKDIR_Y_NW,    1}, {TRACK_LOWER,   TRACKDIR_LOWER_W, 1}}
    00394 };
    00395
    00396 /* static */ AIRail::SignalType AIRail::GetSignalType(TileIndex tile, TileIndex front)
    00397 {
    00398   if (AIMap::DistanceManhattan(tile, front) != 1) return SIGNALTYPE_NONE;
    00399   if (!::IsTileType(tile, MP_RAILWAY) || !::HasSignals(tile)) return SIGNALTYPE_NONE;
    00400
    00401   int data_index = 2 + (::TileX(front) - ::TileX(tile)) + 2 * (::TileY(front) - ::TileY(tile));
    00402
    00403   for (int i = 0; i < NUM_TRACK_DIRECTIONS; i++) {
    00404     const Track &track = _possible_trackdirs[data_index][i].track;
    00405     if (!(::TrackToTrackBits(track) & GetRailTracks(tile))) continue;
    00406     if (!HasSignalOnTrack(tile, track)) continue;
    00407     if (!HasSignalOnTrackdir(tile, _possible_trackdirs[data_index][i].trackdir)) continue;
    00408     SignalType st = (SignalType)::GetSignalType(tile, track);
    00409     if (HasSignalOnTrackdir(tile, ::ReverseTrackdir(_possible_trackdirs[data_index][i].trackdir))) st = (SignalType)(st | SIGNALTYPE_TWOWAY);
    00410     return st;
    00411   }
    00412
    00413   return SIGNALTYPE_NONE;
    00414 }
    00415
    00419 static bool IsValidSignalType(int signal_type)
    00420 {
    00421   if (signal_type < AIRail::SIGNALTYPE_NORMAL || signal_type > AIRail::SIGNALTYPE_COMBO_TWOWAY) return false;
    00422   if (signal_type > AIRail::SIGNALTYPE_PBS_ONEWAY && signal_type < AIRail::SIGNALTYPE_NORMAL_TWOWAY) return false;
    00423   return true;
    00424 }
    00425
    00426 /* static */ bool AIRail::BuildSignal(TileIndex tile, TileIndex front, SignalType signal)
    00427 {
    00428   EnforcePrecondition(false, AIMap::DistanceManhattan(tile, front) == 1)
    00429   EnforcePrecondition(false, ::IsPlainRailTile(tile));
    00430   EnforcePrecondition(false, ::IsValidSignalType(signal));
    00431
    00432   Track track = INVALID_TRACK;
    00433   uint signal_cycles;
    00434
    00435   int data_index = 2 + (::TileX(front) - ::TileX(tile)) + 2 * (::TileY(front) - ::TileY(tile));
    00436   for (int i = 0; i < NUM_TRACK_DIRECTIONS; i++) {
    00437     const Track &t = _possible_trackdirs[data_index][i].track;
    00438     if (!(::TrackToTrackBits(t) & GetRailTracks(tile))) continue;
    00439     track = t;
    00440     signal_cycles = _possible_trackdirs[data_index][i].signal_cycles;
    00441     break;
    00442   }
    00443   EnforcePrecondition(false, track != INVALID_TRACK);
    00444
    00445   uint p1 = track;
    00446   if (signal < SIGNALTYPE_TWOWAY) {
    00447     if (signal != SIGNALTYPE_PBS && signal != SIGNALTYPE_PBS_ONEWAY) signal_cycles++;
    00448     p1 |= (signal_cycles << 15);
    00449   }
    00450   p1 |= ((signal >= SIGNALTYPE_TWOWAY ? signal ^ SIGNALTYPE_TWOWAY : signal) << 5);
    00451
    00452   return AIObject::DoCommand(tile, p1, 0, CMD_BUILD_SIGNALS);
    00453 }
    00454
    00455 /* static */ bool AIRail::RemoveSignal(TileIndex tile, TileIndex front)
    00456 {
    00457   EnforcePrecondition(false, AIMap::DistanceManhattan(tile, front) == 1)
    00458   EnforcePrecondition(false, GetSignalType(tile, front) != SIGNALTYPE_NONE);
    00459
    00460   Track track = INVALID_TRACK;
    00461   int data_index = 2 + (::TileX(front) - ::TileX(tile)) + 2 * (::TileY(front) - ::TileY(tile));
    00462   for (int i = 0; i < NUM_TRACK_DIRECTIONS; i++) {
    00463     const Track &t = _possible_trackdirs[data_index][i].track;
    00464     if (!(::TrackToTrackBits(t) & GetRailTracks(tile))) continue;
    00465     track = t;
    00466     break;
    00467   }
    00468   EnforcePrecondition(false, track != INVALID_TRACK);
    00469
    00470   return AIObject::DoCommand(tile, track, 0, CMD_REMOVE_SIGNALS);
    00471 }
    00472
    00473 /* static */ Money AIRail::GetBuildCost(RailType railtype, BuildType build_type)
    00474 {
    00475   if (!AIRail::IsRailTypeAvailable(railtype)) return -1;
    00476
    00477   switch (build_type) {
    00478     case BT_TRACK:    return ::RailBuildCost((::RailType)railtype);
    00479     case BT_SIGNAL:   return ::GetPrice(PR_BUILD_SIGNALS, 1, NULL);
    00480     case BT_DEPOT:    return ::GetPrice(PR_BUILD_DEPOT_TRAIN, 1, NULL);
    00481     case BT_STATION:  return ::GetPrice(PR_BUILD_STATION_RAIL, 1, NULL) + ::GetPrice(PR_BUILD_STATION_RAIL_LENGTH, 1, NULL);
    00482     case BT_WAYPOINT: return ::GetPrice(PR_BUILD_WAYPOINT_RAIL, 1, NULL);
    00483     default: return -1;
    00484   }
    00485 }
    00486
    00487 /* static */ int32 AIRail::GetMaxSpeed(RailType railtype)
    00488 {
    00489   if (!AIRail::IsRailTypeAvailable(railtype)) return -1;
    00490
    00491   return ::GetRailTypeInfo((::RailType)railtype)->max_speed;
    00492 }