# pbs.cpp

Go to the documentation of this file.

    00001 /* $Id: pbs.cpp 18477 2009-12-13 00:24:53Z rubidium $ */
    00002
    00004 #include "stdafx.h"
    00005 #include "pbs.h"
    00006 #include "functions.h"
    00007 #include "vehicle_func.h"
    00008 #include "yapf/follow_track.hpp"
    00009
    00016 TrackBits GetReservedTrackbits(TileIndex t)
    00017 {
    00018   switch (GetTileType(t)) {
    00019     case MP_RAILWAY:
    00020       if (IsRailWaypoint(t) || IsRailDepot(t)) return GetRailWaypointReservation(t);
    00021       if (IsPlainRail(t)) return GetTrackReservation(t);
    00022       break;
    00023
    00024     case MP_ROAD:
    00025       if (IsLevelCrossing(t)) return GetRailCrossingReservation(t);
    00026       break;
    00027
    00028     case MP_STATION:
    00029       if (IsRailwayStation(t)) return GetRailStationReservation(t);
    00030       break;
    00031
    00032     case MP_TUNNELBRIDGE:
    00033       if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) return GetRailTunnelBridgeReservation(t);
    00034       break;
    00035
    00036     default:
    00037       break;
    00038   }
    00039   return TRACK_BIT_NONE;
    00040 }
    00041
    00049 void SetRailwayStationPlatformReservation(TileIndex start, DiagDirection dir, bool b)
    00050 {
    00051   TileIndex     tile = start;
    00052   TileIndexDiff diff = TileOffsByDiagDir(dir);
    00053
    00054   assert(IsRailwayStationTile(start));
    00055   assert(GetRailStationAxis(start) == DiagDirToAxis(dir));
    00056
    00057   do {
    00058     SetRailwayStationReservation(tile, b);
    00059     MarkTileDirtyByTile(tile);
    00060     tile = TILE_ADD(tile, diff);
    00061   } while (IsCompatibleTrainStationTile(tile, start));
    00062 }
    00063
    00071 bool TryReserveRailTrack(TileIndex tile, Track t)
    00072 {
    00073   assert((GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & TrackToTrackBits(t)) != 0);
    00074
    00075   if (_settings_client.gui.show_track_reservation) {
    00076     /* show the reserved rail if needed */
    00077     MarkTileDirtyByTile(tile);
    00078   }
    00079
    00080   switch (GetTileType(tile)) {
    00081     case MP_RAILWAY:
    00082       if (IsPlainRail(tile)) return TryReserveTrack(tile, t);
    00083       if (IsRailWaypoint(tile) || IsRailDepot(tile)) {
    00084         if (!GetDepotWaypointReservation(tile)) {
    00085           SetDepotWaypointReservation(tile, true);
    00086           MarkTileDirtyByTile(tile); // some GRFs change their appearance when tile is reserved
    00087           return true;
    00088         }
    00089       }
    00090       break;
    00091
    00092     case MP_ROAD:
    00093       if (IsLevelCrossing(tile) && !GetCrossingReservation(tile)) {
    00094         SetCrossingReservation(tile, true);
    00095         BarCrossing(tile);
    00096         MarkTileDirtyByTile(tile); // crossing barred, make tile dirty
    00097         return true;
    00098       }
    00099       break;
    00100
    00101     case MP_STATION:
    00102       if (IsRailwayStation(tile) && !GetRailwayStationReservation(tile)) {
    00103         SetRailwayStationReservation(tile, true);
    00104         MarkTileDirtyByTile(tile); // some GRFs need redraw after reserving track
    00105         return true;
    00106       }
    00107       break;
    00108
    00109     case MP_TUNNELBRIDGE:
    00110       if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL && !GetRailTunnelBridgeReservation(tile)) {
    00111         SetTunnelBridgeReservation(tile, true);
    00112         return true;
    00113       }
    00114       break;
    00115
    00116     default:
    00117       break;
    00118   }
    00119   return false;
    00120 }
    00121
    00127  void UnreserveRailTrack(TileIndex tile, Track t)
    00128 {
    00129   assert((GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & TrackToTrackBits(t)) != 0);
    00130
    00131   if (_settings_client.gui.show_track_reservation) {
    00132     MarkTileDirtyByTile(tile);
    00133   }
    00134
    00135   switch (GetTileType(tile)) {
    00136     case MP_RAILWAY:
    00137       if (IsRailWaypoint(tile) || IsRailDepot(tile)) {
    00138         SetDepotWaypointReservation(tile, false);
    00139         MarkTileDirtyByTile(tile);
    00140         break;
    00141       }
    00142       if (IsPlainRail(tile)) UnreserveTrack(tile, t);
    00143       break;
    00144
    00145     case MP_ROAD:
    00146       if (IsLevelCrossing(tile)) {
    00147         SetCrossingReservation(tile, false);
    00148         UpdateLevelCrossing(tile);
    00149       }
    00150       break;
    00151
    00152     case MP_STATION:
    00153       if (IsRailwayStation(tile)) {
    00154         SetRailwayStationReservation(tile, false);
    00155         MarkTileDirtyByTile(tile);
    00156       }
    00157       break;
    00158
    00159     case MP_TUNNELBRIDGE:
    00160       if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) SetTunnelBridgeReservation(tile, false);
    00161       break;
    00162
    00163     default:
    00164       break;
    00165   }
    00166 }
    00167
    00168
    00170 static PBSTileInfo FollowReservation(Owner o, RailTypes rts, TileIndex tile, Trackdir trackdir, bool ignore_oneway = false)
    00171 {
    00172   TileIndex start_tile = tile;
    00173   Trackdir  start_trackdir = trackdir;
    00174   bool      first_loop = true;
    00175
    00176   /* Start track not reserved? This can happen if two trains
    00177    * are on the same tile. The reservation on the next tile
    00178    * is not ours in this case, so exit. */
    00179   if (!HasReservedTracks(tile, TrackToTrackBits(TrackdirToTrack(trackdir)))) return PBSTileInfo(tile, trackdir, false);
    00180
    00181   /* Do not disallow 90 deg turns as the setting might have changed between reserving and now. */
    00182   CFollowTrackRail ft(o, rts);
    00183   while (ft.Follow(tile, trackdir)) {
    00184     TrackdirBits reserved = ft.m_new_td_bits & TrackBitsToTrackdirBits(GetReservedTrackbits(ft.m_new_tile));
    00185
    00186     /* No reservation --> path end found */
    00187     if (reserved == TRACKDIR_BIT_NONE) break;
    00188
    00189     /* Can't have more than one reserved trackdir */
    00190     Trackdir new_trackdir = FindFirstTrackdir(reserved);
    00191
    00192     /* One-way signal against us. The reservation can't be ours as it is not
    00193      * a safe position from our direction and we can never pass the signal. */
    00194     if (!ignore_oneway && HasOnewaySignalBlockingTrackdir(ft.m_new_tile, new_trackdir)) break;
    00195
    00196     tile = ft.m_new_tile;
    00197     trackdir = new_trackdir;
    00198
    00199     if (first_loop) {
    00200       /* Update the start tile after we followed the track the first
    00201        * time. This is neccessary because the track follower can skip
    00202        * tiles (in stations for example) which means that we might
    00203        * never visit our original starting tile again. */
    00204       start_tile = tile;
    00205       start_trackdir = trackdir;
    00206       first_loop = false;
    00207     } else {
    00208       /* Loop encountered? */
    00209       if (tile == start_tile && trackdir == start_trackdir) break;
    00210     }
    00211     /* Depot tile? Can't continue. */
    00212     if (IsRailDepotTile(tile)) break;
    00213     /* Non-pbs signal? Reservation can't continue. */
    00214     if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) break;
    00215   }
    00216
    00217   return PBSTileInfo(tile, trackdir, false);
    00218 }
    00219
    00223 struct FindTrainOnTrackInfo {
    00224   PBSTileInfo res;
    00225   Vehicle *best;
    00226
    00228   FindTrainOnTrackInfo() : best(NULL) {}
    00229 };
    00230
    00232 static Vehicle *FindTrainOnTrackEnum(Vehicle *v, void *data)
    00233 {
    00234   FindTrainOnTrackInfo *info = (FindTrainOnTrackInfo *)data;
    00235
    00236   if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
    00237
    00238   if (v->u.rail.track == TRACK_BIT_WORMHOLE || HasBit((TrackBits)v->u.rail.track, TrackdirToTrack(info->res.trackdir))) {
    00239     v = v->First();
    00240
    00241     /* ALWAYS return the lowest ID (anti-desync!) */
    00242     if (info->best == NULL || v->index < info->best->index) info->best = v;
    00243     return v;
    00244   }
    00245
    00246   return NULL;
    00247 }
    00248
    00256 PBSTileInfo FollowTrainReservation(const Vehicle *v, bool *train_on_res)
    00257 {
    00258   assert(v->type == VEH_TRAIN);
    00259
    00260   TileIndex tile = v->tile;
    00261   Trackdir  trackdir = GetVehicleTrackdir(v);
    00262
    00263   if (IsRailDepotTile(tile) && !GetRailDepotReservation(tile)) return PBSTileInfo(tile, trackdir, false);
    00264
    00265   FindTrainOnTrackInfo ftoti;
    00266   ftoti.res = FollowReservation(v->owner, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes, tile, trackdir);
    00267   ftoti.res.okay = IsSafeWaitingPosition(v, ftoti.res.tile, ftoti.res.trackdir, true, _settings_game.pf.forbid_90_deg);
    00268   if (train_on_res != NULL) *train_on_res = HasVehicleOnPos(ftoti.res.tile, &ftoti, FindTrainOnTrackEnum);
    00269   return ftoti.res;
    00270 }
    00271
    00279 Vehicle *GetTrainForReservation(TileIndex tile, Track track)
    00280 {
    00281   assert(HasReservedTracks(tile, TrackToTrackBits(track)));
    00282   Trackdir  trackdir = TrackToTrackdir(track);
    00283
    00284   RailTypes rts = GetRailTypeInfo(GetTileRailType(tile))->compatible_railtypes;
    00285
    00286   /* Follow the path from tile to both ends, one of the end tiles should
    00287    * have a train on it. We need FollowReservation to ignore one-way signals
    00288    * here, as one of the two search directions will be the "wrong" way. */
    00289   for (int i = 0; i < 2; ++i, trackdir = ReverseTrackdir(trackdir)) {
    00290     FindTrainOnTrackInfo ftoti;
    00291     ftoti.res = FollowReservation(GetTileOwner(tile), rts, tile, trackdir, true);
    00292
    00293     FindVehicleOnPos(ftoti.res.tile, &ftoti, FindTrainOnTrackEnum);
    00294     if (ftoti.best != NULL) return ftoti.best;
    00295
    00296     /* Special case for stations: check the whole platform for a vehicle. */
    00297     if (IsRailwayStationTile(ftoti.res.tile)) {
    00298       TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti.res.trackdir)));
    00299       for (TileIndex st_tile = ftoti.res.tile + diff; IsCompatibleTrainStationTile(st_tile, ftoti.res.tile); st_tile += diff) {
    00300         FindVehicleOnPos(st_tile, &ftoti, FindTrainOnTrackEnum);
    00301         if (ftoti.best != NULL) return ftoti.best;
    00302       }
    00303     }
    00304
    00305     /* Special case for bridges/tunnels: check the other end as well. */
    00306     if (IsTileType(ftoti.res.tile, MP_TUNNELBRIDGE)) {
    00307       FindVehicleOnPos(GetOtherTunnelBridgeEnd(ftoti.res.tile), &ftoti, FindTrainOnTrackEnum);
    00308       if (ftoti.best != NULL) return ftoti.best;
    00309     }
    00310   }
    00311
    00312   return NULL;
    00313 }
    00314
    00325 bool IsSafeWaitingPosition(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
    00326 {
    00327   if (IsRailDepotTile(tile)) return true;
    00328
    00329   if (IsTileType(tile, MP_RAILWAY)) {
    00330     /* For non-pbs signals, stop on the signal tile. */
    00331     if (HasSignalOnTrackdir(tile, trackdir) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
    00332   }
    00333
    00334   /* Check next tile. For perfomance reasons, we check for 90 degree turns ourself. */
    00335   CFollowTrackRail ft(v, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes);
    00336
    00337   /* End of track? */
    00338   if (!ft.Follow(tile, trackdir)) {
    00339     /* Last tile of a terminus station is a safe position. */
    00340     if (include_line_end) return true;
    00341   }
    00342
    00343   /* Check for reachable tracks. */
    00344   ft.m_new_td_bits &= DiagdirReachesTrackdirs(ft.m_exitdir);
    00345   if (forbid_90deg) ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(trackdir);
    00346   if (ft.m_new_td_bits == TRACKDIR_BIT_NONE) return include_line_end;
    00347
    00348   if (ft.m_new_td_bits != TRACKDIR_BIT_NONE && KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
    00349     /* PBS signal on next trackdir? Safe position. */
    00350     if (HasPbsSignalOnTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) return true;
    00351   }
    00352
    00353   return false;
    00354 }
    00355
    00365 bool IsWaitingPositionFree(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
    00366 {
    00367   Track     track = TrackdirToTrack(trackdir);
    00368   TrackBits reserved = GetReservedTrackbits(tile);
    00369
    00370   /* Tile reserved? Can never be a free waiting position. */
    00371   if (TrackOverlapsTracks(reserved, track)) return false;
    00372
    00373   /* Not reserved and depot or not a pbs signal -> free. */
    00374   if (IsRailDepotTile(tile)) return true;
    00375   if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir) && !IsPbsSignal(GetSignalType(tile, track))) return true;
    00376
    00377   /* Check the next tile, if it's a PBS signal, it has to be free as well. */
    00378   CFollowTrackRail ft(v, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes);
    00379
    00380   if (!ft.Follow(tile, trackdir)) return true;
    00381
    00382   /* Check for reachable tracks. */
    00383   ft.m_new_td_bits &= DiagdirReachesTrackdirs(ft.m_exitdir);
    00384   if (forbid_90deg) ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(trackdir);
    00385
    00386   return !HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(ft.m_new_td_bits));
    00387 }