# rail_map.h

Go to the documentation of this file.

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