# rail_map.h

Go to the documentation of this file.

    00001 /* $Id: rail_map.h 17346 2009-09-01 13:16:53Z rubidium $ */
    00002
    00005 #ifndef RAIL_MAP_H
    00006 #define RAIL_MAP_H
    00007
    00008 #include "rail_type.h"
    00009 #include "signal_func.h"
    00010 #include "direction_func.h"
    00011 #include "track_func.h"
    00012 #include "tile_map.h"
    00013 #include "signal_type.h"
    00014 #include "waypoint_type.h"
    00015
    00016
    00018 enum RailTileType {
    00019   RAIL_TILE_NORMAL   = 0,
    00020   RAIL_TILE_SIGNALS  = 1,
    00021   RAIL_TILE_WAYPOINT = 2,
    00022   RAIL_TILE_DEPOT    = 3,
    00023 };
    00024
    00032 static inline RailTileType GetRailTileType(TileIndex t)
    00033 {
    00034   assert(IsTileType(t, MP_RAILWAY));
    00035   return (RailTileType)GB(_m[t].m5, 6, 2);
    00036 }
    00037
    00045 static inline bool IsPlainRail(TileIndex t)
    00046 {
    00047   RailTileType rtt = GetRailTileType(t);
    00048   return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
    00049 }
    00050
    00056 static inline bool IsPlainRailTile(TileIndex t)
    00057 {
    00058   return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
    00059 }
    00060
    00061
    00068 static inline bool HasSignals(TileIndex t)
    00069 {
    00070   return GetRailTileType(t) == RAIL_TILE_SIGNALS;
    00071 }
    00072
    00079 static inline void SetHasSignals(TileIndex tile, bool signals)
    00080 {
    00081   assert(IsPlainRailTile(tile));
    00082   SB(_m[tile].m5, 6, 1, signals);
    00083 }
    00084
    00091 static inline bool IsRailWaypoint(TileIndex t)
    00092 {
    00093   return GetRailTileType(t) == RAIL_TILE_WAYPOINT;
    00094 }
    00095
    00101 static inline bool IsRailWaypointTile(TileIndex t)
    00102 {
    00103   return IsTileType(t, MP_RAILWAY) && IsRailWaypoint(t);
    00104 }
    00105
    00112 static inline bool IsRailDepot(TileIndex t)
    00113 {
    00114   return GetRailTileType(t) == RAIL_TILE_DEPOT;
    00115 }
    00116
    00122 static inline bool IsRailDepotTile(TileIndex t)
    00123 {
    00124   return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
    00125 }
    00126
    00132 static inline RailType GetRailType(TileIndex t)
    00133 {
    00134   return (RailType)GB(_m[t].m3, 0, 4);
    00135 }
    00136
    00142 static inline void SetRailType(TileIndex t, RailType r)
    00143 {
    00144   SB(_m[t].m3, 0, 4, r);
    00145 }
    00146
    00147
    00153 static inline TrackBits GetTrackBits(TileIndex tile)
    00154 {
    00155   assert(IsPlainRailTile(tile));
    00156   return (TrackBits)GB(_m[tile].m5, 0, 6);
    00157 }
    00158
    00164 static inline void SetTrackBits(TileIndex t, TrackBits b)
    00165 {
    00166   assert(IsPlainRailTile(t));
    00167   SB(_m[t].m5, 0, 6, b);
    00168 }
    00169
    00177 static inline bool HasTrack(TileIndex tile, Track track)
    00178 {
    00179   return HasBit(GetTrackBits(tile), track);
    00180 }
    00181
    00188 static inline DiagDirection GetRailDepotDirection(TileIndex t)
    00189 {
    00190   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00191 }
    00192
    00199 static inline Track GetRailDepotTrack(TileIndex t)
    00200 {
    00201   return DiagDirToDiagTrack(GetRailDepotDirection(t));
    00202 }
    00203
    00204
    00211 static inline Axis GetWaypointAxis(TileIndex t)
    00212 {
    00213   return (Axis)GB(_m[t].m5, 0, 1);
    00214 }
    00215
    00222 static inline Track GetRailWaypointTrack(TileIndex t)
    00223 {
    00224   return AxisToTrack(GetWaypointAxis(t));
    00225 }
    00226
    00233 static inline TrackBits GetRailWaypointBits(TileIndex t)
    00234 {
    00235   return TrackToTrackBits(GetRailWaypointTrack(t));
    00236 }
    00237
    00244 static inline WaypointID GetWaypointIndex(TileIndex t)
    00245 {
    00246   return (WaypointID)_m[t].m2;
    00247 }
    00248
    00249
    00256 static inline TrackBits GetTrackReservation(TileIndex t)
    00257 {
    00258   assert(IsPlainRailTile(t));
    00259   byte track_b = GB(_m[t].m2, 8, 3);
    00260   Track track = (Track)(track_b - 1);    // map array saves Track+1
    00261   if (track_b == 0) return TRACK_BIT_NONE;
    00262   return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
    00263 }
    00264
    00271 static inline void SetTrackReservation(TileIndex t, TrackBits b)
    00272 {
    00273   assert(IsPlainRailTile(t));
    00274   assert(b != INVALID_TRACK_BIT);
    00275   assert(!TracksOverlap(b));
    00276   Track track = RemoveFirstTrack(&b);
    00277   SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track+1);
    00278   SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
    00279 }
    00280
    00288 static inline bool TryReserveTrack(TileIndex tile, Track t)
    00289 {
    00290   assert(HasTrack(tile, t));
    00291   TrackBits bits = TrackToTrackBits(t);
    00292   TrackBits res = GetTrackReservation(tile);
    00293   if ((res & bits) != TRACK_BIT_NONE) return false;  // already reserved
    00294   res |= bits;
    00295   if (TracksOverlap(res)) return false;  // crossing reservation present
    00296   SetTrackReservation(tile, res);
    00297   return true;
    00298 }
    00299
    00306 static inline void UnreserveTrack(TileIndex tile, Track t)
    00307 {
    00308   assert(HasTrack(tile, t));
    00309   TrackBits res = GetTrackReservation(tile);
    00310   res &= ~TrackToTrackBits(t);
    00311   SetTrackReservation(tile, res);
    00312 }
    00313
    00321 static inline bool GetDepotWaypointReservation(TileIndex t)
    00322 {
    00323   assert(IsRailWaypoint(t) || IsRailDepot(t));
    00324   return HasBit(_m[t].m5, 4);
    00325 }
    00326
    00334 static inline void SetDepotWaypointReservation(TileIndex t, bool b)
    00335 {
    00336   assert(IsRailWaypoint(t) || IsRailDepot(t));
    00337   SB(_m[t].m5, 4, 1, (byte)b);
    00338 }
    00339
    00346 static inline TrackBits GetRailWaypointReservation(TileIndex t)
    00347 {
    00348   return GetDepotWaypointReservation(t) ? GetRailWaypointBits(t) : TRACK_BIT_NONE;
    00349 }
    00350
    00357 static inline TrackBits GetRailDepotReservation(TileIndex t)
    00358 {
    00359   return GetDepotWaypointReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
    00360 }
    00361
    00362
    00363 static inline bool IsPbsSignal(SignalType s)
    00364 {
    00365   return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
    00366 }
    00367
    00368 static inline SignalType GetSignalType(TileIndex t, Track track)
    00369 {
    00370   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00371   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00372   return (SignalType)GB(_m[t].m2, pos, 3);
    00373 }
    00374
    00375 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
    00376 {
    00377   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00378   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00379   SB(_m[t].m2, pos, 3, s);
    00380   if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
    00381 }
    00382
    00383 static inline bool IsPresignalEntry(TileIndex t, Track track)
    00384 {
    00385   return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
    00386 }
    00387
    00388 static inline bool IsPresignalExit(TileIndex t, Track track)
    00389 {
    00390   return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
    00391 }
    00392
    00394 static inline bool IsOnewaySignal(TileIndex t, Track track)
    00395 {
    00396   return GetSignalType(t, track) != SIGTYPE_PBS;
    00397 }
    00398
    00399 static inline void CycleSignalSide(TileIndex t, Track track)
    00400 {
    00401   byte sig;
    00402   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
    00403
    00404   sig = GB(_m[t].m3, pos, 2);
    00405   if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
    00406   SB(_m[t].m3, pos, 2, sig);
    00407 }
    00408
    00409 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
    00410 {
    00411   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00412   return (SignalVariant)GB(_m[t].m2, pos, 1);
    00413 }
    00414
    00415 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
    00416 {
    00417   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00418   SB(_m[t].m2, pos, 1, v);
    00419   if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
    00420 }
    00421
    00426 enum SignalState {
    00427   SIGNAL_STATE_RED   = 0,
    00428   SIGNAL_STATE_GREEN = 1,
    00429 };
    00430
    00436 static inline void SetSignalStates(TileIndex tile, uint state)
    00437 {
    00438   SB(_m[tile].m4, 4, 4, state);
    00439 }
    00440
    00446 static inline uint GetSignalStates(TileIndex tile)
    00447 {
    00448   return GB(_m[tile].m4, 4, 4);
    00449 }
    00450
    00457 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
    00458 {
    00459   return (SignalState)HasBit(GetSignalStates(t), signalbit);
    00460 }
    00461
    00467 static inline void SetPresentSignals(TileIndex tile, uint signals)
    00468 {
    00469   SB(_m[tile].m3, 4, 4, signals);
    00470 }
    00471
    00477 static inline uint GetPresentSignals(TileIndex tile)
    00478 {
    00479   return GB(_m[tile].m3, 4, 4);
    00480 }
    00481
    00488 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
    00489 {
    00490   return HasBit(GetPresentSignals(t), signalbit);
    00491 }
    00492
    00497 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
    00498 {
    00499   assert(IsValidTrack(track));
    00500   return
    00501     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00502     (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
    00503 }
    00504
    00512 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
    00513 {
    00514   assert (IsValidTrackdir(trackdir));
    00515   return
    00516     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00517     GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
    00518 }
    00519
    00526 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
    00527 {
    00528   assert(IsValidTrackdir(trackdir));
    00529   assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
    00530   return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
    00531     SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
    00532 }
    00533
    00537 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
    00538 {
    00539   if (state == SIGNAL_STATE_GREEN) { // set 1
    00540     SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
    00541   } else {
    00542     SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
    00543   }
    00544 }
    00545
    00551 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
    00552 {
    00553   return
    00554     IsTileType(tile, MP_RAILWAY) &&
    00555     HasSignalOnTrackdir(tile, td) &&
    00556     IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
    00557 }
    00558
    00565 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
    00566 {
    00567   return
    00568     IsTileType(tile, MP_RAILWAY) &&
    00569     HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
    00570     !HasSignalOnTrackdir(tile, td) &&
    00571     IsOnewaySignal(tile, TrackdirToTrack(td));
    00572 }
    00573
    00574
    00578 RailType GetTileRailType(TileIndex tile);
    00579
    00581 enum RailGroundType {
    00582   RAIL_GROUND_BARREN       =  0,
    00583   RAIL_GROUND_GRASS        =  1,
    00584   RAIL_GROUND_FENCE_NW     =  2,
    00585   RAIL_GROUND_FENCE_SE     =  3,
    00586   RAIL_GROUND_FENCE_SENW   =  4,
    00587   RAIL_GROUND_FENCE_NE     =  5,
    00588   RAIL_GROUND_FENCE_SW     =  6,
    00589   RAIL_GROUND_FENCE_NESW   =  7,
    00590   RAIL_GROUND_FENCE_VERT1  =  8,
    00591   RAIL_GROUND_FENCE_VERT2  =  9,
    00592   RAIL_GROUND_FENCE_HORIZ1 = 10,
    00593   RAIL_GROUND_FENCE_HORIZ2 = 11,
    00594   RAIL_GROUND_ICE_DESERT   = 12,
    00595   RAIL_GROUND_WATER        = 13,
    00596   RAIL_GROUND_HALF_SNOW    = 14,
    00597 };
    00598
    00599 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
    00600 {
    00601   SB(_m[t].m4, 0, 4, rgt);
    00602 }
    00603
    00604 static inline RailGroundType GetRailGroundType(TileIndex t)
    00605 {
    00606   return (RailGroundType)GB(_m[t].m4, 0, 4);
    00607 }
    00608
    00609 static inline bool IsSnowRailGround(TileIndex t)
    00610 {
    00611   return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
    00612 }
    00613
    00614
    00615 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
    00616 {
    00617   SetTileType(t, MP_RAILWAY);
    00618   SetTileOwner(t, o);
    00619   _m[t].m2 = 0;
    00620   _m[t].m3 = r;
    00621   _m[t].m4 = 0;
    00622   _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
    00623   SB(_m[t].m6, 2, 4, 0);
    00624   _me[t].m7 = 0;
    00625 }
    00626
    00627
    00628 static inline void MakeRailDepot(TileIndex t, Owner o, DiagDirection d, RailType r)
    00629 {
    00630   SetTileType(t, MP_RAILWAY);
    00631   SetTileOwner(t, o);
    00632   _m[t].m2 = 0;
    00633   _m[t].m3 = r;
    00634   _m[t].m4 = 0;
    00635   _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
    00636   SB(_m[t].m6, 2, 4, 0);
    00637   _me[t].m7 = 0;
    00638 }
    00639
    00640
    00641 static inline void MakeRailWaypoint(TileIndex t, Owner o, Axis a, RailType r, uint index)
    00642 {
    00643   SetTileType(t, MP_RAILWAY);
    00644   SetTileOwner(t, o);
    00645   _m[t].m2 = index;
    00646   _m[t].m3 = r;
    00647   _m[t].m4 = 0;
    00648   _m[t].m5 = RAIL_TILE_WAYPOINT << 6 | a;
    00649   SB(_m[t].m6, 2, 4, 0);
    00650   _me[t].m7 = 0;
    00651 }
    00652
    00653 #endif /* RAIL_MAP_H */