# rail_map.h

Go to the documentation of this file.

    00001 /* $Id: rail_map.h 12239 2008-02-24 22:20:31Z glx $ */
    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
    00014
    00016 enum RailTileType {
    00017   RAIL_TILE_NORMAL   = 0,
    00018   RAIL_TILE_SIGNALS  = 1,
    00019   RAIL_TILE_WAYPOINT = 2,
    00020   RAIL_TILE_DEPOT    = 3,
    00021 };
    00022
    00030 static inline RailTileType GetRailTileType(TileIndex t)
    00031 {
    00032   assert(IsTileType(t, MP_RAILWAY));
    00033   return (RailTileType)GB(_m[t].m5, 6, 2);
    00034 }
    00035
    00043 static inline bool IsPlainRailTile(TileIndex t)
    00044 {
    00045   RailTileType rtt = GetRailTileType(t);
    00046   return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
    00047 }
    00048
    00055 static inline bool HasSignals(TileIndex t)
    00056 {
    00057   return GetRailTileType(t) == RAIL_TILE_SIGNALS;
    00058 }
    00059
    00066 static inline void SetHasSignals(TileIndex tile, bool signals)
    00067 {
    00068   assert(IsPlainRailTile(tile));
    00069   SB(_m[tile].m5, 6, 1, signals);
    00070 }
    00071
    00078 static inline bool IsRailDepot(TileIndex t)
    00079 {
    00080   return GetRailTileType(t) == RAIL_TILE_DEPOT;
    00081 }
    00082
    00089 static inline bool IsRailWaypoint(TileIndex t)
    00090 {
    00091   return GetRailTileType(t) == RAIL_TILE_WAYPOINT;
    00092 }
    00093
    00094
    00100 static inline RailType GetRailType(TileIndex t)
    00101 {
    00102   return (RailType)GB(_m[t].m3, 0, 4);
    00103 }
    00104
    00110 static inline void SetRailType(TileIndex t, RailType r)
    00111 {
    00112   SB(_m[t].m3, 0, 4, r);
    00113 }
    00114
    00115
    00121 static inline TrackBits GetTrackBits(TileIndex tile)
    00122 {
    00123   return (TrackBits)GB(_m[tile].m5, 0, 6);
    00124 }
    00125
    00131 static inline void SetTrackBits(TileIndex t, TrackBits b)
    00132 {
    00133   SB(_m[t].m5, 0, 6, b);
    00134 }
    00135
    00143 static inline bool HasTrack(TileIndex tile, Track track)
    00144 {
    00145   return HasBit(GetTrackBits(tile), track);
    00146 }
    00147
    00154 static inline DiagDirection GetRailDepotDirection(TileIndex t)
    00155 {
    00156   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00157 }
    00158
    00159
    00166 static inline Axis GetWaypointAxis(TileIndex t)
    00167 {
    00168   return (Axis)GB(_m[t].m5, 0, 1);
    00169 }
    00170
    00177 static inline Track GetRailWaypointTrack(TileIndex t)
    00178 {
    00179   return AxisToTrack(GetWaypointAxis(t));
    00180 }
    00181
    00188 static inline TrackBits GetRailWaypointBits(TileIndex t)
    00189 {
    00190   return TrackToTrackBits(GetRailWaypointTrack(t));
    00191 }
    00192
    00199 static inline WaypointID GetWaypointIndex(TileIndex t)
    00200 {
    00201   return (WaypointID)_m[t].m2;
    00202 }
    00203
    00205 enum SignalType {
    00206   SIGTYPE_NORMAL  = 0,
    00207   SIGTYPE_ENTRY   = 1,
    00208   SIGTYPE_EXIT    = 2,
    00209   SIGTYPE_COMBO   = 3
    00210 };
    00211
    00212 static inline SignalType GetSignalType(TileIndex t, Track track)
    00213 {
    00214   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00215   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00216   return (SignalType)GB(_m[t].m2, pos, 2);
    00217 }
    00218
    00219 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
    00220 {
    00221   assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
    00222   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
    00223   SB(_m[t].m2, pos, 2, s);
    00224   if (track == INVALID_TRACK) SB(_m[t].m2, 4, 2, s);
    00225 }
    00226
    00227 static inline bool IsPresignalEntry(TileIndex t, Track track)
    00228 {
    00229   return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
    00230 }
    00231
    00232 static inline bool IsPresignalExit(TileIndex t, Track track)
    00233 {
    00234   return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
    00235 }
    00236
    00237 static inline void CycleSignalSide(TileIndex t, Track track)
    00238 {
    00239   byte sig;
    00240   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
    00241
    00242   sig = GB(_m[t].m3, pos, 2);
    00243   if (--sig == 0) sig = 3;
    00244   SB(_m[t].m3, pos, 2, sig);
    00245 }
    00246
    00248 enum SignalVariant {
    00249   SIG_ELECTRIC  = 0,
    00250   SIG_SEMAPHORE = 1
    00251 };
    00252
    00253 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
    00254 {
    00255   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 6 : 2;
    00256   return (SignalVariant)GB(_m[t].m2, pos, 1);
    00257 }
    00258
    00259 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
    00260 {
    00261   byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 6 : 2;
    00262   SB(_m[t].m2, pos, 1, v);
    00263   if (track == INVALID_TRACK) SB(_m[t].m2, 6, 1, v);
    00264 }
    00265
    00270 enum SignalState {
    00271   SIGNAL_STATE_RED   = 0,
    00272   SIGNAL_STATE_GREEN = 1,
    00273 };
    00274
    00280 static inline void SetSignalStates(TileIndex tile, uint state)
    00281 {
    00282   SB(_m[tile].m4, 4, 4, state);
    00283 }
    00284
    00290 static inline uint GetSignalStates(TileIndex tile)
    00291 {
    00292   return GB(_m[tile].m4, 4, 4);
    00293 }
    00294
    00301 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
    00302 {
    00303   return (SignalState)HasBit(GetSignalStates(t), signalbit);
    00304 }
    00305
    00311 static inline void SetPresentSignals(TileIndex tile, uint signals)
    00312 {
    00313   SB(_m[tile].m3, 4, 4, signals);
    00314 }
    00315
    00321 static inline uint GetPresentSignals(TileIndex tile)
    00322 {
    00323   return GB(_m[tile].m3, 4, 4);
    00324 }
    00325
    00332 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
    00333 {
    00334   return HasBit(GetPresentSignals(t), signalbit);
    00335 }
    00336
    00341 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
    00342 {
    00343   assert(IsValidTrack(track));
    00344   return
    00345     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00346     (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
    00347 }
    00348
    00356 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
    00357 {
    00358   assert (IsValidTrackdir(trackdir));
    00359   return
    00360     GetRailTileType(tile) == RAIL_TILE_SIGNALS &&
    00361     GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
    00362 }
    00363
    00370 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
    00371 {
    00372   assert(IsValidTrackdir(trackdir));
    00373   assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
    00374   return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
    00375     SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
    00376 }
    00377
    00381 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
    00382 {
    00383   if (state == SIGNAL_STATE_GREEN) { // set 1
    00384     SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
    00385   } else {
    00386     SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
    00387   }
    00388 }
    00389
    00390
    00394 RailType GetTileRailType(TileIndex tile);
    00395
    00397 enum RailGroundType {
    00398   RAIL_GROUND_BARREN       =  0,
    00399   RAIL_GROUND_GRASS        =  1,
    00400   RAIL_GROUND_FENCE_NW     =  2,
    00401   RAIL_GROUND_FENCE_SE     =  3,
    00402   RAIL_GROUND_FENCE_SENW   =  4,
    00403   RAIL_GROUND_FENCE_NE     =  5,
    00404   RAIL_GROUND_FENCE_SW     =  6,
    00405   RAIL_GROUND_FENCE_NESW   =  7,
    00406   RAIL_GROUND_FENCE_VERT1  =  8,
    00407   RAIL_GROUND_FENCE_VERT2  =  9,
    00408   RAIL_GROUND_FENCE_HORIZ1 = 10,
    00409   RAIL_GROUND_FENCE_HORIZ2 = 11,
    00410   RAIL_GROUND_ICE_DESERT   = 12,
    00411   RAIL_GROUND_WATER        = 13,
    00412   RAIL_GROUND_HALF_SNOW    = 14,
    00413 };
    00414
    00415 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
    00416 {
    00417   SB(_m[t].m4, 0, 4, rgt);
    00418 }
    00419
    00420 static inline RailGroundType GetRailGroundType(TileIndex t)
    00421 {
    00422   return (RailGroundType)GB(_m[t].m4, 0, 4);
    00423 }
    00424
    00425 static inline bool IsSnowRailGround(TileIndex t)
    00426 {
    00427   return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
    00428 }
    00429
    00430
    00431 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
    00432 {
    00433   SetTileType(t, MP_RAILWAY);
    00434   SetTileOwner(t, o);
    00435   _m[t].m2 = 0;
    00436   _m[t].m3 = r;
    00437   _m[t].m4 = 0;
    00438   _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
    00439 }
    00440
    00441
    00442 static inline void MakeRailDepot(TileIndex t, Owner o, DiagDirection d, RailType r)
    00443 {
    00444   SetTileType(t, MP_RAILWAY);
    00445   SetTileOwner(t, o);
    00446   _m[t].m2 = 0;
    00447   _m[t].m3 = r;
    00448   _m[t].m4 = 0;
    00449   _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
    00450 }
    00451
    00452
    00453 static inline void MakeRailWaypoint(TileIndex t, Owner o, Axis a, RailType r, uint index)
    00454 {
    00455   SetTileType(t, MP_RAILWAY);
    00456   SetTileOwner(t, o);
    00457   _m[t].m2 = index;
    00458   _m[t].m3 = r;
    00459   _m[t].m4 = 0;
    00460   _m[t].m5 = RAIL_TILE_WAYPOINT << 6 | a;
    00461 }
    00462
    00463 #endif /* RAIL_MAP_H */