# ai_rail.cpp

Go to the documentation of this file.

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