# 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 "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
    00136 static inline TrackBits GetTrackBits(TileIndex tile)
    00137 {
    00138   assert(IsPlainRailTile(tile));
    00139   return (TrackBits)GB(_m[tile].m5, 0, 6);
    00140 }
    00141
    00147 static inline void SetTrackBits(TileIndex t, TrackBits b)
    00148 {
    00149   assert(IsPlainRailTile(t));
    00150   SB(_m[t].m5, 0, 6, b);
    00151 }
    00152
    00160 static inline bool HasTrack(TileIndex tile, Track track)
    00161 {
    00162   return HasBit(GetTrackBits(tile), track);
    00163 }
    00164
    00171 static inline DiagDirection GetRailDepotDirection(TileIndex t)
    00172 {
    00173   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00174 }
    00175
    00182 static inline Track GetRailDepotTrack(TileIndex t)
    00183 {
    00184   return DiagDirToDiagTrack(GetRailDepotDirection(t));
    00185 }
    00186
    00193 static inline TrackBits GetRailReservationTrackBits(TileIndex t)
    00194 {
    00195   assert(IsPlainRailTile(t));
    00196   byte track_b = GB(_m[t].m2, 8, 3);
    00197   Track track = (Track)(track_b - 1);    // map array saves Track+1
    00198   if (track_b == 0) return TRACK_BIT_NONE;
    00199   return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
    00200 }
    00201
    00208 static inline void SetTrackReservation(TileIndex t, TrackBits b)
    00209 {
    00210   assert(IsPlainRailTile(t));
    00211   assert(b != INVALID_TRACK_BIT);
    00212   assert(!TracksOverlap(b));
    00213   Track track = RemoveFirstTrack(&b);
    00214   SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track + 1);
    00215   SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
    00216 }
    00217
    00225 static inline bool TryReserveTrack(TileIndex tile, Track t)
    00226 {
    00227   assert(HasTrack(tile, t));
    00228   TrackBits bits = TrackToTrackBits(t);
    00229   TrackBits res = GetRailReservationTrackBits(tile);
    00230   if ((res & bits) != TRACK_BIT_NONE) return false;  // already reserved
    00231   res |= bits;
    00232   if (TracksOverlap(res)) return false;  // crossing reservation present
    00233   SetTrackReservation(tile, res);
    00234   return true;
    00235 }
    00236
    00243 static inline void UnreserveTrack(TileIndex tile, Track t)
    00244 {
    00245   assert(HasTrack(tile, t));
    00246   TrackBits res = GetRailReservationTrackBits(tile);
    00247   res &= ~TrackToTrackBits(t);
    00248   SetTrackReservation(tile, res);
    00249 }
    00250
    00257 static inline bool HasDepotReservation(TileIndex t)
    00258 {
    00259   assert(IsRailDepot(t));
    00260   return HasBit(_m[t].m5, 4);
    00261 }
    00262
    00269 static inline void SetDepotReservation(TileIndex t, bool b)
    00270 {
    00271   assert(IsRailDepot(t));
    00272   SB(_m[t].m5, 4, 1, (byte)b);
    00273 }
    00274
    00281 static inline TrackBits GetDepotReservationTrackBits(TileIndex t)
    00282 {
    00283   return HasDepotReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
    00284 }
    00285
    00286 static inline SignalType GetSignalType(TileIndex t, Track track)
    00287 {
    00288   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00289   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00290   return (SignalType)GB(_m[t].m2, pos, 3);
    00291 }
    00292
    00293 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
    00294 {
    00295   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00296   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00297   SB(_m[t].m2, pos, 3, s);
    00298   if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
    00299 }
    00300
    00301 static inline bool IsPresignalEntry(TileIndex t, Track track)
    00302 {
    00303   return IsEntrySignal(GetSignalType(t, track));
    00304 }
    00305
    00306 static inline bool IsPresignalExit(TileIndex t, Track track)
    00307 {
    00308   return IsExitSignal(GetSignalType(t, track));
    00309 }
    00310
    00311 static inline bool IsPresignalCombo(TileIndex t, Track track)
    00312 {
    00313   return IsComboSignal(GetSignalType(t, track));
    00314 }
    00315
    00316 static inline bool IsPresignalProgrammable(TileIndex t, Track track)
    00317 {
    00318   return IsProgrammableSignal(GetSignalType(t, track));
    00319 }
    00320
    00321 static inline bool IsSpeedSignal(TileIndex t, Track track)
    00322 {
    00323   return IsSpeedSignal(GetSignalType(t, track));
    00324 }
    00325
    00327 static inline bool IsOnewaySignal(TileIndex t, Track track)
    00328 {
    00329   return GetSignalType(t, track) != SIGTYPE_PBS &&
    00330     GetSignalType(t, track) != SIGTYPE_SPEED;
    00331 }
    00332
    00333 static inline void CycleSignalSide(TileIndex t, Track track)
    00334 {
    00335   byte sig;
    00336   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
    00337
    00338   sig = GB(_m[t].m3, pos, 2);
    00339   if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
    00340   SB(_m[t].m3, pos, 2, sig);
    00341 }
    00342
    00343 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
    00344 {
    00345   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00346   return (SignalVariant)GB(_m[t].m2, pos, 1);
    00347 }
    00348
    00349 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
    00350 {
    00351   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00352   SB(_m[t].m2, pos, 1, v);
    00353   if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
    00354 }
    00355
    00361 enum SignalState {
    00362   SIGNAL_STATE_RED   = 0,
    00363   SIGNAL_STATE_GREEN = 1,
    00364
    00365   SIGNAL_STATE_MAX = SIGNAL_STATE_GREEN
    00366 };
    00367
    00373 static inline void SetSignalStates(TileIndex tile, uint state)
    00374 {
    00375   SB(_m[tile].m4, 4, 4, state);
    00376 }
    00377
    00383 static inline uint GetSignalStates(TileIndex tile)
    00384 {
    00385   return GB(_m[tile].m4, 4, 4);
    00386 }
    00387
    00394 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
    00395 {
    00396   return (SignalState)HasBit(GetSignalStates(t), signalbit);
    00397 }
    00398
    00404 static inline void SetPresentSignals(TileIndex tile, uint signals)
    00405 {
    00406   SB(_m[tile].m3, 4, 4, signals);
    00407 }
    00408
    00414 static inline uint GetPresentSignals(TileIndex tile)
    00415 {
    00416   return GB(_m[tile].m3, 4, 4);
    00417 }
    00418
    00425 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
    00426 {
    00427   return HasBit(GetPresentSignals(t), signalbit);
    00428 }
    00429
    00434 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
    00435 {
    00436   assert(IsValidTrack(track));
    00437   return GetRailTileType(tile) == RAIL_TILE_SIGNALS && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
    00438 }
    00439
    00447 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
    00448 {
    00449   assert (IsValidTrackdir(trackdir));
    00450   return GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
    00451 }
    00452
    00459 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
    00460 {
    00461   assert(IsValidTrackdir(trackdir));
    00462   assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
    00463   return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
    00464     SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
    00465 }
    00466
    00470 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
    00471 {
    00472   if (state == SIGNAL_STATE_GREEN) { // set 1
    00473     SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
    00474   } else {
    00475     SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
    00476   }
    00477 }
    00478
    00484 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
    00485 {
    00486   return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, td) &&
    00487       IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
    00488 }
    00489
    00496 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
    00497 {
    00498   return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
    00499       !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td)) && !IsSpeedSignal(tile, TrackdirToTrack(td));
    00500 }
    00501
    00502 RailType GetTileRailType(TileIndex tile);
    00503
    00505 enum RailGroundType {
    00506   RAIL_GROUND_BARREN       =  0,
    00507   RAIL_GROUND_GRASS        =  1,
    00508   RAIL_GROUND_FENCE_NW     =  2,
    00509   RAIL_GROUND_FENCE_SE     =  3,
    00510   RAIL_GROUND_FENCE_SENW   =  4,
    00511   RAIL_GROUND_FENCE_NE     =  5,
    00512   RAIL_GROUND_FENCE_SW     =  6,
    00513   RAIL_GROUND_FENCE_NESW   =  7,
    00514   RAIL_GROUND_FENCE_VERT1  =  8,
    00515   RAIL_GROUND_FENCE_VERT2  =  9,
    00516   RAIL_GROUND_FENCE_HORIZ1 = 10,
    00517   RAIL_GROUND_FENCE_HORIZ2 = 11,
    00518   RAIL_GROUND_ICE_DESERT   = 12,
    00519   RAIL_GROUND_WATER        = 13,
    00520   RAIL_GROUND_HALF_SNOW    = 14,
    00521 };
    00522
    00523 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
    00524 {
    00525   SB(_m[t].m4, 0, 4, rgt);
    00526 }
    00527
    00528 static inline RailGroundType GetRailGroundType(TileIndex t)
    00529 {
    00530   return (RailGroundType)GB(_m[t].m4, 0, 4);
    00531 }
    00532
    00533 static inline bool IsSnowRailGround(TileIndex t)
    00534 {
    00535   return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
    00536 }
    00537
    00538 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
    00539 {
    00540   SetTileType(t, MP_RAILWAY);
    00541   SetTileOwner(t, o);
    00542   _m[t].m2 = 0;
    00543   _m[t].m3 = r;
    00544   _m[t].m4 = 0;
    00545   _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
    00546   SB(_m[t].m6, 2, 4, 0);
    00547   _me[t].m7 = 0;
    00548 }
    00549
    00550 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
    00551 {
    00552   SetTileType(t, MP_RAILWAY);
    00553   SetTileOwner(t, o);
    00554   _m[t].m2 = did;
    00555   _m[t].m3 = r;
    00556   _m[t].m4 = 0;
    00557   _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
    00558   SB(_m[t].m6, 2, 4, 0);
    00559   _me[t].m7 = 0;
    00560 }
    00561
    00562 static inline void IncreaseStuckCounter(TileIndex t)
    00563 {
    00564   if (!IsTileType(t, MP_RAILWAY)) return;
    00565   if (_me[t].m7 == 255) return;
    00566   _me[t].m7++;
    00567 }
    00568
    00569 static inline void ReduceStuckCounter(TileIndex t)
    00570 {
    00571   if (!IsTileType(t, MP_RAILWAY)) return;
    00572   _me[t].m7 -= _me[t].m7/4;
    00573 }
    00574
    00575 static inline byte GetStuckCounter(TileIndex t)
    00576 {
    00577   if (!IsTileType(t, MP_RAILWAY)) return 0;
    00578   return _me[t].m7;
    00579
    00580 }
    00581
    00582 #endif /* RAIL_MAP_H */