# rail_map.h

Go to the documentation of this file.

    00001 /* $Id: rail_map.h 16324 2009-05-16 20:29: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 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   assert(IsPlainRailTile(tile));
    00145   return (TrackBits)GB(_m[tile].m5, 0, 6);
    00146 }
    00147
    00153 static inline void SetTrackBits(TileIndex t, TrackBits b)
    00154 {
    00155   assert(IsPlainRailTile(t));
    00156   SB(_m[t].m5, 0, 6, b);
    00157 }
    00158
    00166 static inline bool HasTrack(TileIndex tile, Track track)
    00167 {
    00168   return HasBit(GetTrackBits(tile), track);
    00169 }
    00170
    00177 static inline DiagDirection GetRailDepotDirection(TileIndex t)
    00178 {
    00179   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00180 }
    00181
    00188 static inline Track GetRailDepotTrack(TileIndex t)
    00189 {
    00190   return DiagDirToDiagTrack(GetRailDepotDirection(t));
    00191 }
    00192
    00193
    00200 static inline Axis GetWaypointAxis(TileIndex t)
    00201 {
    00202   return (Axis)GB(_m[t].m5, 0, 1);
    00203 }
    00204
    00211 static inline Track GetRailWaypointTrack(TileIndex t)
    00212 {
    00213   return AxisToTrack(GetWaypointAxis(t));
    00214 }
    00215
    00222 static inline TrackBits GetRailWaypointBits(TileIndex t)
    00223 {
    00224   return TrackToTrackBits(GetRailWaypointTrack(t));
    00225 }
    00226
    00233 static inline WaypointID GetWaypointIndex(TileIndex t)
    00234 {
    00235   return (WaypointID)_m[t].m2;
    00236 }
    00237
    00238
    00245 static inline TrackBits GetTrackReservation(TileIndex t)
    00246 {
    00247   assert(IsPlainRailTile(t));
    00248   byte track_b = GB(_m[t].m2, 8, 3);
    00249   Track track = (Track)(track_b - 1);    // map array saves Track+1
    00250   if (track_b == 0) return TRACK_BIT_NONE;
    00251   return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
    00252 }
    00253
    00260 static inline void SetTrackReservation(TileIndex t, TrackBits b)
    00261 {
    00262   assert(IsPlainRailTile(t));
    00263   assert(b != INVALID_TRACK_BIT);
    00264   assert(!TracksOverlap(b));
    00265   Track track = RemoveFirstTrack(&b);
    00266   SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track+1);
    00267   SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
    00268 }
    00269
    00277 static inline bool TryReserveTrack(TileIndex tile, Track t)
    00278 {
    00279   assert(HasTrack(tile, t));
    00280   TrackBits bits = TrackToTrackBits(t);
    00281   TrackBits res = GetTrackReservation(tile);
    00282   if ((res & bits) != TRACK_BIT_NONE) return false;  // already reserved
    00283   res |= bits;
    00284   if (TracksOverlap(res)) return false;  // crossing reservation present
    00285   SetTrackReservation(tile, res);
    00286   return true;
    00287 }
    00288
    00295 static inline void UnreserveTrack(TileIndex tile, Track t)
    00296 {
    00297   assert(HasTrack(tile, t));
    00298   TrackBits res = GetTrackReservation(tile);
    00299   res &= ~TrackToTrackBits(t);
    00300   SetTrackReservation(tile, res);
    00301 }
    00302
    00310 static inline bool GetDepotWaypointReservation(TileIndex t)
    00311 {
    00312   assert(IsRailWaypoint(t) || IsRailDepot(t));
    00313   return HasBit(_m[t].m5, 4);
    00314 }
    00315
    00323 static inline void SetDepotWaypointReservation(TileIndex t, bool b)
    00324 {
    00325   assert(IsRailWaypoint(t) || IsRailDepot(t));
    00326   SB(_m[t].m5, 4, 1, (byte)b);
    00327 }
    00328
    00335 static inline TrackBits GetRailWaypointReservation(TileIndex t)
    00336 {
    00337   return GetDepotWaypointReservation(t) ? GetRailWaypointBits(t) : TRACK_BIT_NONE;
    00338 }
    00339
    00346 static inline TrackBits GetRailDepotReservation(TileIndex t)
    00347 {
    00348   return GetDepotWaypointReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
    00349 }
    00350
    00351
    00352 static inline bool IsPbsSignal(SignalType s)
    00353 {
    00354   return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
    00355 }
    00356
    00357 static inline SignalType GetSignalType(TileIndex t, Track track)
    00358 {
    00359   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00360   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00361   return (SignalType)GB(_m[t].m2, pos, 3);
    00362 }
    00363
    00364 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
    00365 {
    00366   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00367   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00368   SB(_m[t].m2, pos, 3, s);
    00369   if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
    00370 }
    00371
    00372 static inline bool IsPresignalEntry(TileIndex t, Track track)
    00373 {
    00374   return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
    00375 }
    00376
    00377 static inline bool IsPresignalExit(TileIndex t, Track track)
    00378 {
    00379   return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
    00380 }
    00381
    00383 static inline bool IsOnewaySignal(TileIndex t, Track track)
    00384 {
    00385   return GetSignalType(t, track) != SIGTYPE_PBS;
    00386 }
    00387
    00388 static inline void CycleSignalSide(TileIndex t, Track track)
    00389 {
    00390   byte sig;
    00391   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
    00392
    00393   sig = GB(_m[t].m3, pos, 2);
    00394   if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
    00395   SB(_m[t].m3, pos, 2, sig);
    00396 }
    00397
    00398 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
    00399 {
    00400   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00401   return (SignalVariant)GB(_m[t].m2, pos, 1);
    00402 }
    00403
    00404 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
    00405 {
    00406   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
    00407   SB(_m[t].m2, pos, 1, v);
    00408   if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
    00409 }
    00410
    00415 enum SignalState {
    00416   SIGNAL_STATE_RED   = 0,
    00417   SIGNAL_STATE_GREEN = 1,
    00418 };
    00419
    00425 static inline void SetSignalStates(TileIndex tile, uint state)
    00426 {
    00427   SB(_m[tile].m4, 4, 4, state);
    00428 }
    00429
    00435 static inline uint GetSignalStates(TileIndex tile)
    00436 {
    00437   return GB(_m[tile].m4, 4, 4);
    00438 }
    00439
    00446 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
    00447 {
    00448   return (SignalState)HasBit(GetSignalStates(t), signalbit);
    00449 }
    00450
    00456 static inline void SetPresentSignals(TileIndex tile, uint signals)
    00457 {
    00458   SB(_m[tile].m3, 4, 4, signals);
    00459 }
    00460
    00466 static inline uint GetPresentSignals(TileIndex tile)
    00467 {
    00468   return GB(_m[tile].m3, 4, 4);
    00469 }
    00470
    00477 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
    00478 {
    00479   return HasBit(GetPresentSignals(t), signalbit);
    00480 }
    00481
    00486 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
    00487 {
    00488   assert(IsValidTrack(track));
    00489   return
    00490     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00491     (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
    00492 }
    00493
    00501 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
    00502 {
    00503   assert (IsValidTrackdir(trackdir));
    00504   return
    00505     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00506     GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
    00507 }
    00508
    00515 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
    00516 {
    00517   assert(IsValidTrackdir(trackdir));
    00518   assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
    00519   return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
    00520     SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
    00521 }
    00522
    00526 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
    00527 {
    00528   if (state == SIGNAL_STATE_GREEN) { // set 1
    00529     SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
    00530   } else {
    00531     SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
    00532   }
    00533 }
    00534
    00540 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
    00541 {
    00542   return
    00543     IsTileType(tile, MP_RAILWAY) &&
    00544     HasSignalOnTrackdir(tile, td) &&
    00545     IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
    00546 }
    00547
    00554 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
    00555 {
    00556   return
    00557     IsTileType(tile, MP_RAILWAY) &&
    00558     HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
    00559     !HasSignalOnTrackdir(tile, td) &&
    00560     IsOnewaySignal(tile, TrackdirToTrack(td));
    00561 }
    00562
    00563
    00567 RailType GetTileRailType(TileIndex tile);
    00568
    00570 enum RailGroundType {
    00571   RAIL_GROUND_BARREN       =  0,
    00572   RAIL_GROUND_GRASS        =  1,
    00573   RAIL_GROUND_FENCE_NW     =  2,
    00574   RAIL_GROUND_FENCE_SE     =  3,
    00575   RAIL_GROUND_FENCE_SENW   =  4,
    00576   RAIL_GROUND_FENCE_NE     =  5,
    00577   RAIL_GROUND_FENCE_SW     =  6,
    00578   RAIL_GROUND_FENCE_NESW   =  7,
    00579   RAIL_GROUND_FENCE_VERT1  =  8,
    00580   RAIL_GROUND_FENCE_VERT2  =  9,
    00581   RAIL_GROUND_FENCE_HORIZ1 = 10,
    00582   RAIL_GROUND_FENCE_HORIZ2 = 11,
    00583   RAIL_GROUND_ICE_DESERT   = 12,
    00584   RAIL_GROUND_WATER        = 13,
    00585   RAIL_GROUND_HALF_SNOW    = 14,
    00586 };
    00587
    00588 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
    00589 {
    00590   SB(_m[t].m4, 0, 4, rgt);
    00591 }
    00592
    00593 static inline RailGroundType GetRailGroundType(TileIndex t)
    00594 {
    00595   return (RailGroundType)GB(_m[t].m4, 0, 4);
    00596 }
    00597
    00598 static inline bool IsSnowRailGround(TileIndex t)
    00599 {
    00600   return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
    00601 }
    00602
    00603
    00604 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
    00605 {
    00606   SetTileType(t, MP_RAILWAY);
    00607   SetTileOwner(t, o);
    00608   _m[t].m2 = 0;
    00609   _m[t].m3 = r;
    00610   _m[t].m4 = 0;
    00611   _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
    00612   SB(_m[t].m6, 2, 4, 0);
    00613   _me[t].m7 = 0;
    00614 }
    00615
    00616
    00617 static inline void MakeRailDepot(TileIndex t, Owner o, DiagDirection d, RailType r)
    00618 {
    00619   SetTileType(t, MP_RAILWAY);
    00620   SetTileOwner(t, o);
    00621   _m[t].m2 = 0;
    00622   _m[t].m3 = r;
    00623   _m[t].m4 = 0;
    00624   _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
    00625   SB(_m[t].m6, 2, 4, 0);
    00626   _me[t].m7 = 0;
    00627 }
    00628
    00629
    00630 static inline void MakeRailWaypoint(TileIndex t, Owner o, Axis a, RailType r, uint index)
    00631 {
    00632   SetTileType(t, MP_RAILWAY);
    00633   SetTileOwner(t, o);
    00634   _m[t].m2 = index;
    00635   _m[t].m3 = r;
    00636   _m[t].m4 = 0;
    00637   _m[t].m5 = RAIL_TILE_WAYPOINT << 6 | a;
    00638   SB(_m[t].m6, 2, 4, 0);
    00639   _me[t].m7 = 0;
    00640 }
    00641
    00642 #endif /* RAIL_MAP_H */