# rail_map.h

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 #ifndef RAIL_MAP_H
    00013 #define RAIL_MAP_H
    00014
    00015 #include "rail_type.h"
    00016 #include "depot_type.h"
    00017 #include "signal_func.h"
    00018 #include "direction_func.h"
    00019 #include "track_func.h"
    00020 #include "tile_map.h"
    00021 #include "signal_type.h"
    00022
    00023
    00025 enum RailTileType {
    00026   RAIL_TILE_NORMAL   = 0,
    00027   RAIL_TILE_SIGNALS  = 1,
    00028   RAIL_TILE_DEPOT    = 3,
    00029 };
    00030
    00038 static inline RailTileType GetRailTileType(TileIndex t)
    00039 {
    00040   assert(IsTileType(t, MP_RAILWAY));
    00041   return (RailTileType)GB(_m[t].m5, 6, 2);
    00042 }
    00043
    00051 static inline bool IsPlainRail(TileIndex t)
    00052 {
    00053   RailTileType rtt = GetRailTileType(t);
    00054   return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
    00055 }
    00056
    00062 static inline bool IsPlainRailTile(TileIndex t)
    00063 {
    00064   return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
    00065 }
    00066
    00067
    00074 static inline bool HasSignals(TileIndex t)
    00075 {
    00076   return GetRailTileType(t) == RAIL_TILE_SIGNALS;
    00077 }
    00078
    00085 static inline void SetHasSignals(TileIndex tile, bool signals)
    00086 {
    00087   assert(IsPlainRailTile(tile));
    00088   SB(_m[tile].m5, 6, 1, signals);
    00089 }
    00090
    00097 static inline bool IsRailDepot(TileIndex t)
    00098 {
    00099   return GetRailTileType(t) == RAIL_TILE_DEPOT;
    00100 }
    00101
    00107 static inline bool IsRailDepotTile(TileIndex t)
    00108 {
    00109   return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
    00110 }
    00111
    00117 static inline RailType GetRailType(TileIndex t)
    00118 {
    00119   return (RailType)GB(_m[t].m3, 0, 4);
    00120 }
    00121
    00127 static inline void SetRailType(TileIndex t, RailType r)
    00128 {
    00129   SB(_m[t].m3, 0, 4, r);
    00130 }
    00131
    00132
    00138 static inline TrackBits GetTrackBits(TileIndex tile)
    00139 {
    00140   assert(IsPlainRailTile(tile));
    00141   return (TrackBits)GB(_m[tile].m5, 0, 6);
    00142 }
    00143
    00149 static inline void SetTrackBits(TileIndex t, TrackBits b)
    00150 {
    00151   assert(IsPlainRailTile(t));
    00152   SB(_m[t].m5, 0, 6, b);
    00153 }
    00154
    00162 static inline bool HasTrack(TileIndex tile, Track track)
    00163 {
    00164   return HasBit(GetTrackBits(tile), track);
    00165 }
    00166
    00173 static inline DiagDirection GetRailDepotDirection(TileIndex t)
    00174 {
    00175   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00176 }
    00177
    00184 static inline Track GetRailDepotTrack(TileIndex t)
    00185 {
    00186   return DiagDirToDiagTrack(GetRailDepotDirection(t));
    00187 }
    00188
    00189
    00196 static inline TrackBits GetRailReservationTrackBits(TileIndex t)
    00197 {
    00198   assert(IsPlainRailTile(t));
    00199   byte track_b = GB(_m[t].m2, 8, 3);
    00200   Track track = (Track)(track_b - 1);    // map array saves Track+1
    00201   if (track_b == 0) return TRACK_BIT_NONE;
    00202   return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
    00203 }
    00204
    00211 static inline void SetTrackReservation(TileIndex t, TrackBits b)
    00212 {
    00213   assert(IsPlainRailTile(t));
    00214   assert(b != INVALID_TRACK_BIT);
    00215   assert(!TracksOverlap(b));
    00216   Track track = RemoveFirstTrack(&b);
    00217   SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track+1);
    00218   SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
    00219 }
    00220
    00228 static inline bool TryReserveTrack(TileIndex tile, Track t)
    00229 {
    00230   assert(HasTrack(tile, t));
    00231   TrackBits bits = TrackToTrackBits(t);
    00232   TrackBits res = GetRailReservationTrackBits(tile);
    00233   if ((res & bits) != TRACK_BIT_NONE) return false;  // already reserved
    00234   res |= bits;
    00235   if (TracksOverlap(res)) return false;  // crossing reservation present
    00236   SetTrackReservation(tile, res);
    00237   return true;
    00238 }
    00239
    00246 static inline void UnreserveTrack(TileIndex tile, Track t)
    00247 {
    00248   assert(HasTrack(tile, t));
    00249   TrackBits res = GetRailReservationTrackBits(tile);
    00250   res &= ~TrackToTrackBits(t);
    00251   SetTrackReservation(tile, res);
    00252 }
    00253
    00260 static inline bool HasDepotReservation(TileIndex t)
    00261 {
    00262   assert(IsRailDepot(t));
    00263   return HasBit(_m[t].m5, 4);
    00264 }
    00265
    00272 static inline void SetDepotReservation(TileIndex t, bool b)
    00273 {
    00274   assert(IsRailDepot(t));
    00275   SB(_m[t].m5, 4, 1, (byte)b);
    00276 }
    00277
    00284 static inline TrackBits GetDepotReservationTrackBits(TileIndex t)
    00285 {
    00286   return HasDepotReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
    00287 }
    00288
    00289
    00290 static inline bool IsPbsSignal(SignalType s)
    00291 {
    00292   return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
    00293 }
    00294
    00295 static inline SignalType GetSignalType(TileIndex t, Track track)
    00296 {
    00297   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00298   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00299   return (SignalType)GB(_m[t].m2, pos, 3);
    00300 }
    00301
    00302 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
    00303 {
    00304   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00305   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00306   SB(_m[t].m2, pos, 3, s);
    00307   if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
    00308 }
    00309
    00310 static inline bool IsPresignalEntry(TileIndex t, Track track)
    00311 {
    00312   return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
    00313 }
    00314
    00315 static inline bool IsPresignalExit(TileIndex t, Track track)
    00316 {
    00317   return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
    00318 }
    00319
    00321 static inline bool IsOnewaySignal(TileIndex t, Track track)
    00322 {
    00323   return GetSignalType(t, track) != SIGTYPE_PBS;
    00324 }
    00325
    00326 static inline void CycleSignalSide(TileIndex t, Track track)
    00327 {
    00328   byte sig;
    00329   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
    00330
    00331   sig = GB(_m[t].m3, pos, 2);
    00332   if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
    00333   SB(_m[t].m3, pos, 2, sig);
    00334 }
    00335
    00336 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
    00337 {
    00338   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00339   return (SignalVariant)GB(_m[t].m2, pos, 1);
    00340 }
    00341
    00342 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
    00343 {
    00344   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00345   SB(_m[t].m2, pos, 1, v);
    00346   if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
    00347 }
    00348
    00353 enum SignalState {
    00354   SIGNAL_STATE_RED   = 0,
    00355   SIGNAL_STATE_GREEN = 1,
    00356 };
    00357
    00363 static inline void SetSignalStates(TileIndex tile, uint state)
    00364 {
    00365   SB(_m[tile].m4, 4, 4, state);
    00366 }
    00367
    00373 static inline uint GetSignalStates(TileIndex tile)
    00374 {
    00375   return GB(_m[tile].m4, 4, 4);
    00376 }
    00377
    00384 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
    00385 {
    00386   return (SignalState)HasBit(GetSignalStates(t), signalbit);
    00387 }
    00388
    00394 static inline void SetPresentSignals(TileIndex tile, uint signals)
    00395 {
    00396   SB(_m[tile].m3, 4, 4, signals);
    00397 }
    00398
    00404 static inline uint GetPresentSignals(TileIndex tile)
    00405 {
    00406   return GB(_m[tile].m3, 4, 4);
    00407 }
    00408
    00415 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
    00416 {
    00417   return HasBit(GetPresentSignals(t), signalbit);
    00418 }
    00419
    00424 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
    00425 {
    00426   assert(IsValidTrack(track));
    00427   return
    00428     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00429     (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
    00430 }
    00431
    00439 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
    00440 {
    00441   assert (IsValidTrackdir(trackdir));
    00442   return
    00443     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00444     GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
    00445 }
    00446
    00453 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
    00454 {
    00455   assert(IsValidTrackdir(trackdir));
    00456   assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
    00457   return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
    00458     SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
    00459 }
    00460
    00464 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
    00465 {
    00466   if (state == SIGNAL_STATE_GREEN) { // set 1
    00467     SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
    00468   } else {
    00469     SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
    00470   }
    00471 }
    00472
    00478 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
    00479 {
    00480   return
    00481     IsTileType(tile, MP_RAILWAY) &&
    00482     HasSignalOnTrackdir(tile, td) &&
    00483     IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
    00484 }
    00485
    00492 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
    00493 {
    00494   return
    00495     IsTileType(tile, MP_RAILWAY) &&
    00496     HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
    00497     !HasSignalOnTrackdir(tile, td) &&
    00498     IsOnewaySignal(tile, TrackdirToTrack(td));
    00499 }
    00500
    00501
    00505 RailType GetTileRailType(TileIndex tile);
    00506
    00508 enum RailGroundType {
    00509   RAIL_GROUND_BARREN       =  0,
    00510   RAIL_GROUND_GRASS        =  1,
    00511   RAIL_GROUND_FENCE_NW     =  2,
    00512   RAIL_GROUND_FENCE_SE     =  3,
    00513   RAIL_GROUND_FENCE_SENW   =  4,
    00514   RAIL_GROUND_FENCE_NE     =  5,
    00515   RAIL_GROUND_FENCE_SW     =  6,
    00516   RAIL_GROUND_FENCE_NESW   =  7,
    00517   RAIL_GROUND_FENCE_VERT1  =  8,
    00518   RAIL_GROUND_FENCE_VERT2  =  9,
    00519   RAIL_GROUND_FENCE_HORIZ1 = 10,
    00520   RAIL_GROUND_FENCE_HORIZ2 = 11,
    00521   RAIL_GROUND_ICE_DESERT   = 12,
    00522   RAIL_GROUND_WATER        = 13,
    00523   RAIL_GROUND_HALF_SNOW    = 14,
    00524 };
    00525
    00526 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
    00527 {
    00528   SB(_m[t].m4, 0, 4, rgt);
    00529 }
    00530
    00531 static inline RailGroundType GetRailGroundType(TileIndex t)
    00532 {
    00533   return (RailGroundType)GB(_m[t].m4, 0, 4);
    00534 }
    00535
    00536 static inline bool IsSnowRailGround(TileIndex t)
    00537 {
    00538   return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
    00539 }
    00540
    00541
    00542 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
    00543 {
    00544   SetTileType(t, MP_RAILWAY);
    00545   SetTileOwner(t, o);
    00546   _m[t].m2 = 0;
    00547   _m[t].m3 = r;
    00548   _m[t].m4 = 0;
    00549   _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
    00550   SB(_m[t].m6, 2, 4, 0);
    00551   _me[t].m7 = 0;
    00552 }
    00553
    00554
    00555 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
    00556 {
    00557   SetTileType(t, MP_RAILWAY);
    00558   SetTileOwner(t, o);
    00559   _m[t].m2 = did;
    00560   _m[t].m3 = r;
    00561   _m[t].m4 = 0;
    00562   _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
    00563   SB(_m[t].m6, 2, 4, 0);
    00564   _me[t].m7 = 0;
    00565 }
    00566
    00567 #endif /* RAIL_MAP_H */