# rail_map.h

Go to the documentation of this file.

    00001 /* $Id: rail_map.h 14567 2008-11-07 18:02:46Z 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 IsPlainRailTile(TileIndex t)
    00046 {
    00047   RailTileType rtt = GetRailTileType(t);
    00048   return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
    00049 }
    00050
    00057 static inline bool HasSignals(TileIndex t)
    00058 {
    00059   return GetRailTileType(t) == RAIL_TILE_SIGNALS;
    00060 }
    00061
    00068 static inline void SetHasSignals(TileIndex tile, bool signals)
    00069 {
    00070   assert(IsPlainRailTile(tile));
    00071   SB(_m[tile].m5, 6, 1, signals);
    00072 }
    00073
    00080 static inline bool IsRailWaypoint(TileIndex t)
    00081 {
    00082   return GetRailTileType(t) == RAIL_TILE_WAYPOINT;
    00083 }
    00084
    00090 static inline bool IsRailWaypointTile(TileIndex t)
    00091 {
    00092   return IsTileType(t, MP_RAILWAY) && IsRailWaypoint(t);
    00093 }
    00094
    00101 static inline bool IsRailDepot(TileIndex t)
    00102 {
    00103   return GetRailTileType(t) == RAIL_TILE_DEPOT;
    00104 }
    00105
    00111 static inline bool IsRailDepotTile(TileIndex t)
    00112 {
    00113   return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
    00114 }
    00115
    00121 static inline RailType GetRailType(TileIndex t)
    00122 {
    00123   return (RailType)GB(_m[t].m3, 0, 4);
    00124 }
    00125
    00131 static inline void SetRailType(TileIndex t, RailType r)
    00132 {
    00133   SB(_m[t].m3, 0, 4, r);
    00134 }
    00135
    00136
    00142 static inline TrackBits GetTrackBits(TileIndex tile)
    00143 {
    00144   return (TrackBits)GB(_m[tile].m5, 0, 6);
    00145 }
    00146
    00152 static inline void SetTrackBits(TileIndex t, TrackBits b)
    00153 {
    00154   SB(_m[t].m5, 0, 6, b);
    00155 }
    00156
    00164 static inline bool HasTrack(TileIndex tile, Track track)
    00165 {
    00166   return HasBit(GetTrackBits(tile), track);
    00167 }
    00168
    00175 static inline DiagDirection GetRailDepotDirection(TileIndex t)
    00176 {
    00177   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00178 }
    00179
    00186 static inline Track GetRailDepotTrack(TileIndex t)
    00187 {
    00188   return DiagDirToDiagTrack(GetRailDepotDirection(t));
    00189 }
    00190
    00191
    00198 static inline Axis GetWaypointAxis(TileIndex t)
    00199 {
    00200   return (Axis)GB(_m[t].m5, 0, 1);
    00201 }
    00202
    00209 static inline Track GetRailWaypointTrack(TileIndex t)
    00210 {
    00211   return AxisToTrack(GetWaypointAxis(t));
    00212 }
    00213
    00220 static inline TrackBits GetRailWaypointBits(TileIndex t)
    00221 {
    00222   return TrackToTrackBits(GetRailWaypointTrack(t));
    00223 }
    00224
    00231 static inline WaypointID GetWaypointIndex(TileIndex t)
    00232 {
    00233   return (WaypointID)_m[t].m2;
    00234 }
    00235
    00236
    00243 static inline TrackBits GetTrackReservation(TileIndex t)
    00244 {
    00245   assert(IsPlainRailTile(t));
    00246   byte track_b = GB(_m[t].m2, 8, 3);
    00247   Track track = (Track)(track_b - 1);    // map array saves Track+1
    00248   if (track_b == 0) return TRACK_BIT_NONE;
    00249   return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
    00250 }
    00251
    00258 static inline void SetTrackReservation(TileIndex t, TrackBits b)
    00259 {
    00260   assert(IsPlainRailTile(t));
    00261   assert(b != INVALID_TRACK_BIT);
    00262   assert(!TracksOverlap(b));
    00263   Track track = RemoveFirstTrack(&b);
    00264   SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track+1);
    00265   SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
    00266 }
    00267
    00275 static inline bool TryReserveTrack(TileIndex tile, Track t)
    00276 {
    00277   assert(HasTrack(tile, t));
    00278   TrackBits bits = TrackToTrackBits(t);
    00279   TrackBits res = GetTrackReservation(tile);
    00280   if ((res & bits) != TRACK_BIT_NONE) return false;  // already reserved
    00281   res |= bits;
    00282   if (TracksOverlap(res)) return false;  // crossing reservation present
    00283   SetTrackReservation(tile, res);
    00284   return true;
    00285 }
    00286
    00293 static inline void UnreserveTrack(TileIndex tile, Track t)
    00294 {
    00295   assert(HasTrack(tile, t));
    00296   TrackBits res = GetTrackReservation(tile);
    00297   res &= ~TrackToTrackBits(t);
    00298   SetTrackReservation(tile, res);
    00299 }
    00300
    00308 static inline bool GetDepotWaypointReservation(TileIndex t)
    00309 {
    00310   assert(IsRailWaypoint(t) || IsRailDepot(t));
    00311   return HasBit(_m[t].m5, 4);
    00312 }
    00313
    00321 static inline void SetDepotWaypointReservation(TileIndex t, bool b)
    00322 {
    00323   assert(IsRailWaypoint(t) || IsRailDepot(t));
    00324   SB(_m[t].m5, 4, 1, (byte)b);
    00325 }
    00326
    00333 static inline TrackBits GetRailWaypointReservation(TileIndex t)
    00334 {
    00335   return GetDepotWaypointReservation(t) ? GetRailWaypointBits(t) : TRACK_BIT_NONE;
    00336 }
    00337
    00344 static inline TrackBits GetRailDepotReservation(TileIndex t)
    00345 {
    00346   return GetDepotWaypointReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
    00347 }
    00348
    00349
    00350 static inline bool IsPbsSignal(SignalType s)
    00351 {
    00352   return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
    00353 }
    00354
    00355 static inline SignalType GetSignalType(TileIndex t, Track track)
    00356 {
    00357   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00358   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00359   return (SignalType)GB(_m[t].m2, pos, 3);
    00360 }
    00361
    00362 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
    00363 {
    00364   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00365   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00366   SB(_m[t].m2, pos, 3, s);
    00367   if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
    00368 }
    00369
    00370 static inline bool IsPresignalEntry(TileIndex t, Track track)
    00371 {
    00372   return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
    00373 }
    00374
    00375 static inline bool IsPresignalExit(TileIndex t, Track track)
    00376 {
    00377   return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
    00378 }
    00379
    00381 static inline bool IsOnewaySignal(TileIndex t, Track track)
    00382 {
    00383   return GetSignalType(t, track) != SIGTYPE_PBS;
    00384 }
    00385
    00386 static inline void CycleSignalSide(TileIndex t, Track track)
    00387 {
    00388   byte sig;
    00389   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
    00390
    00391   sig = GB(_m[t].m3, pos, 2);
    00392   if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
    00393   SB(_m[t].m3, pos, 2, sig);
    00394 }
    00395
    00396 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
    00397 {
    00398   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00399   return (SignalVariant)GB(_m[t].m2, pos, 1);
    00400 }
    00401
    00402 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
    00403 {
    00404   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00405   SB(_m[t].m2, pos, 1, v);
    00406   if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
    00407 }
    00408
    00413 enum SignalState {
    00414   SIGNAL_STATE_RED   = 0,
    00415   SIGNAL_STATE_GREEN = 1,
    00416 };
    00417
    00423 static inline void SetSignalStates(TileIndex tile, uint state)
    00424 {
    00425   SB(_m[tile].m4, 4, 4, state);
    00426 }
    00427
    00433 static inline uint GetSignalStates(TileIndex tile)
    00434 {
    00435   return GB(_m[tile].m4, 4, 4);
    00436 }
    00437
    00444 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
    00445 {
    00446   return (SignalState)HasBit(GetSignalStates(t), signalbit);
    00447 }
    00448
    00454 static inline void SetPresentSignals(TileIndex tile, uint signals)
    00455 {
    00456   SB(_m[tile].m3, 4, 4, signals);
    00457 }
    00458
    00464 static inline uint GetPresentSignals(TileIndex tile)
    00465 {
    00466   return GB(_m[tile].m3, 4, 4);
    00467 }
    00468
    00475 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
    00476 {
    00477   return HasBit(GetPresentSignals(t), signalbit);
    00478 }
    00479
    00484 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
    00485 {
    00486   assert(IsValidTrack(track));
    00487   return
    00488     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00489     (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
    00490 }
    00491
    00499 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
    00500 {
    00501   assert (IsValidTrackdir(trackdir));
    00502   return
    00503     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00504     GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
    00505 }
    00506
    00513 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
    00514 {
    00515   assert(IsValidTrackdir(trackdir));
    00516   assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
    00517   return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
    00518     SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
    00519 }
    00520
    00524 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
    00525 {
    00526   if (state == SIGNAL_STATE_GREEN) { // set 1
    00527     SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
    00528   } else {
    00529     SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
    00530   }
    00531 }
    00532
    00538 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
    00539 {
    00540   return
    00541     IsTileType(tile, MP_RAILWAY) &&
    00542     HasSignalOnTrackdir(tile, td) &&
    00543     IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
    00544 }
    00545
    00552 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
    00553 {
    00554   return
    00555     IsTileType(tile, MP_RAILWAY) &&
    00556     HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
    00557     !HasSignalOnTrackdir(tile, td) &&
    00558     IsOnewaySignal(tile, TrackdirToTrack(td));
    00559 }
    00560
    00561
    00565 RailType GetTileRailType(TileIndex tile);
    00566
    00568 enum RailGroundType {
    00569   RAIL_GROUND_BARREN       =  0,
    00570   RAIL_GROUND_GRASS        =  1,
    00571   RAIL_GROUND_FENCE_NW     =  2,
    00572   RAIL_GROUND_FENCE_SE     =  3,
    00573   RAIL_GROUND_FENCE_SENW   =  4,
    00574   RAIL_GROUND_FENCE_NE     =  5,
    00575   RAIL_GROUND_FENCE_SW     =  6,
    00576   RAIL_GROUND_FENCE_NESW   =  7,
    00577   RAIL_GROUND_FENCE_VERT1  =  8,
    00578   RAIL_GROUND_FENCE_VERT2  =  9,
    00579   RAIL_GROUND_FENCE_HORIZ1 = 10,
    00580   RAIL_GROUND_FENCE_HORIZ2 = 11,
    00581   RAIL_GROUND_ICE_DESERT   = 12,
    00582   RAIL_GROUND_WATER        = 13,
    00583   RAIL_GROUND_HALF_SNOW    = 14,
    00584 };
    00585
    00586 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
    00587 {
    00588   SB(_m[t].m4, 0, 4, rgt);
    00589 }
    00590
    00591 static inline RailGroundType GetRailGroundType(TileIndex t)
    00592 {
    00593   return (RailGroundType)GB(_m[t].m4, 0, 4);
    00594 }
    00595
    00596 static inline bool IsSnowRailGround(TileIndex t)
    00597 {
    00598   return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
    00599 }
    00600
    00601
    00602 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
    00603 {
    00604   SetTileType(t, MP_RAILWAY);
    00605   SetTileOwner(t, o);
    00606   _m[t].m2 = 0;
    00607   _m[t].m3 = r;
    00608   _m[t].m4 = 0;
    00609   _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
    00610 }
    00611
    00612
    00613 static inline void MakeRailDepot(TileIndex t, Owner o, DiagDirection d, RailType r)
    00614 {
    00615   SetTileType(t, MP_RAILWAY);
    00616   SetTileOwner(t, o);
    00617   _m[t].m2 = 0;
    00618   _m[t].m3 = r;
    00619   _m[t].m4 = 0;
    00620   _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
    00621 }
    00622
    00623
    00624 static inline void MakeRailWaypoint(TileIndex t, Owner o, Axis a, RailType r, uint index)
    00625 {
    00626   SetTileType(t, MP_RAILWAY);
    00627   SetTileOwner(t, o);
    00628   _m[t].m2 = index;
    00629   _m[t].m3 = r;
    00630   _m[t].m4 = 0;
    00631   _m[t].m5 = RAIL_TILE_WAYPOINT << 6 | a;
    00632 }
    00633
    00634 #endif /* RAIL_MAP_H */