00001
00002
00005 #ifndef ROAD_MAP_H
00006 #define ROAD_MAP_H
00007
00008 #include "track_func.h"
00009 #include "rail_type.h"
00010 #include "town_type.h"
00011 #include "road_func.h"
00012 #include "tile_map.h"
00013
00014
00015 enum RoadTileType {
00016 ROAD_TILE_NORMAL,
00017 ROAD_TILE_CROSSING,
00018 ROAD_TILE_DEPOT
00019 };
00020
00021 static inline RoadTileType GetRoadTileType(TileIndex t)
00022 {
00023 assert(IsTileType(t, MP_ROAD));
00024 return (RoadTileType)GB(_m[t].m5, 6, 2);
00025 }
00026
00027 static inline bool IsNormalRoad(TileIndex t)
00028 {
00029 return GetRoadTileType(t) == ROAD_TILE_NORMAL;
00030 }
00031
00032 static inline bool IsNormalRoadTile(TileIndex t)
00033 {
00034 return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
00035 }
00036
00037 static inline bool IsLevelCrossing(TileIndex t)
00038 {
00039 return GetRoadTileType(t) == ROAD_TILE_CROSSING;
00040 }
00041
00042 static inline bool IsLevelCrossingTile(TileIndex t)
00043 {
00044 return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
00045 }
00046
00047 static inline bool IsRoadDepot(TileIndex t)
00048 {
00049 return GetRoadTileType(t) == ROAD_TILE_DEPOT;
00050 }
00051
00052 static inline bool IsRoadDepotTile(TileIndex t)
00053 {
00054 return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
00055 }
00056
00057 static inline RoadBits GetRoadBits(TileIndex t, RoadType rt)
00058 {
00059 assert(IsNormalRoad(t));
00060 switch (rt) {
00061 default: NOT_REACHED();
00062 case ROADTYPE_ROAD: return (RoadBits)GB(_m[t].m5, 0, 4);
00063 case ROADTYPE_TRAM: return (RoadBits)GB(_m[t].m3, 0, 4);
00064 }
00065 }
00066
00074 static inline RoadBits GetOtherRoadBits(TileIndex t, RoadType rt)
00075 {
00076 return GetRoadBits(t, rt == ROADTYPE_ROAD ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00077 }
00078
00085 static inline RoadBits GetAllRoadBits(TileIndex tile)
00086 {
00087 return GetRoadBits(tile, ROADTYPE_ROAD) | GetRoadBits(tile, ROADTYPE_TRAM);
00088 }
00089
00090 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
00091 {
00092 assert(IsNormalRoad(t));
00093 switch (rt) {
00094 default: NOT_REACHED();
00095 case ROADTYPE_ROAD: SB(_m[t].m5, 0, 4, r); break;
00096 case ROADTYPE_TRAM: SB(_m[t].m3, 0, 4, r); break;
00097 }
00098 }
00099
00100 static inline RoadTypes GetRoadTypes(TileIndex t)
00101 {
00102 return (RoadTypes)GB(_me[t].m7, 6, 2);
00103 }
00104
00105 static inline void SetRoadTypes(TileIndex t, RoadTypes rt)
00106 {
00107 assert(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
00108 SB(_me[t].m7, 6, 2, rt);
00109 }
00110
00111 static inline bool HasTileRoadType(TileIndex t, RoadType rt)
00112 {
00113 return HasBit(GetRoadTypes(t), rt);
00114 }
00115
00116 static inline Owner GetRoadOwner(TileIndex t, RoadType rt)
00117 {
00118 switch (rt) {
00119 default: NOT_REACHED();
00120 case ROADTYPE_ROAD: return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
00121 case ROADTYPE_TRAM: {
00122
00123
00124 Owner o = (Owner)GB(_m[t].m3, 4, 4);
00125 return o == OWNER_TOWN ? OWNER_NONE : o;
00126 }
00127 }
00128 }
00129
00130 static inline void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
00131 {
00132 switch (rt) {
00133 default: NOT_REACHED();
00134 case ROADTYPE_ROAD: SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o); break;
00135 case ROADTYPE_TRAM: SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
00136 }
00137 }
00138
00139 static inline bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
00140 {
00141 assert(HasTileRoadType(t, rt));
00142 return (GetRoadOwner(t, rt) == o);
00143 }
00144
00150 static inline bool HasTownOwnedRoad(TileIndex t)
00151 {
00152 return HasTileRoadType(t, ROADTYPE_ROAD) && IsRoadOwner(t, ROADTYPE_ROAD, OWNER_TOWN);
00153 }
00154
00156 enum DisallowedRoadDirections {
00157 DRD_NONE,
00158 DRD_SOUTHBOUND,
00159 DRD_NORTHBOUND,
00160 DRD_BOTH,
00161 DRD_END
00162 };
00163 DECLARE_ENUM_AS_BIT_SET(DisallowedRoadDirections);
00164
00170 static inline DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
00171 {
00172 assert(IsNormalRoad(t));
00173 return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
00174 }
00175
00181 static inline void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
00182 {
00183 assert(IsNormalRoad(t));
00184 assert(drd < DRD_END);
00185 SB(_m[t].m5, 4, 2, drd);
00186 }
00187
00188 static inline Axis GetCrossingRoadAxis(TileIndex t)
00189 {
00190 assert(IsLevelCrossing(t));
00191 return (Axis)GB(_m[t].m5, 0, 1);
00192 }
00193
00194 static inline Axis GetCrossingRailAxis(TileIndex t)
00195 {
00196 assert(IsLevelCrossing(t));
00197 return OtherAxis((Axis)GetCrossingRoadAxis(t));
00198 }
00199
00200 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
00201 {
00202 return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
00203 }
00204
00205 static inline Track GetCrossingRailTrack(TileIndex tile)
00206 {
00207 return AxisToTrack(GetCrossingRailAxis(tile));
00208 }
00209
00210 static inline TrackBits GetCrossingRailBits(TileIndex tile)
00211 {
00212 return AxisToTrackBits(GetCrossingRailAxis(tile));
00213 }
00214
00215
00222 static inline bool GetCrossingReservation(TileIndex t)
00223 {
00224 assert(IsLevelCrossingTile(t));
00225 return HasBit(_m[t].m5, 4);
00226 }
00227
00235 static inline void SetCrossingReservation(TileIndex t, bool b)
00236 {
00237 assert(IsLevelCrossingTile(t));
00238 SB(_m[t].m5, 4, 1, b ? 1 : 0);
00239 }
00240
00247 static inline TrackBits GetRailCrossingReservation(TileIndex t)
00248 {
00249 return GetCrossingReservation(t) ? GetCrossingRailBits(t) : TRACK_BIT_NONE;
00250 }
00251
00252 static inline bool IsCrossingBarred(TileIndex t)
00253 {
00254 assert(IsLevelCrossing(t));
00255 return HasBit(_m[t].m5, 5);
00256 }
00257
00258 static inline void SetCrossingBarred(TileIndex t, bool barred)
00259 {
00260 assert(IsLevelCrossing(t));
00261 SB(_m[t].m5, 5, 1, barred ? 1 : 0);
00262 }
00263
00264 static inline void UnbarCrossing(TileIndex t)
00265 {
00266 SetCrossingBarred(t, false);
00267 }
00268
00269 static inline void BarCrossing(TileIndex t)
00270 {
00271 SetCrossingBarred(t, true);
00272 }
00273
00274 #define IsOnDesert IsOnSnow
00275 static inline bool IsOnSnow(TileIndex t)
00276 {
00277 return HasBit(_me[t].m7, 5);
00278 }
00279
00280 #define ToggleDesert ToggleSnow
00281 static inline void ToggleSnow(TileIndex t)
00282 {
00283 ToggleBit(_me[t].m7, 5);
00284 }
00285
00286
00287 enum Roadside {
00288 ROADSIDE_BARREN = 0,
00289 ROADSIDE_GRASS = 1,
00290 ROADSIDE_PAVED = 2,
00291 ROADSIDE_STREET_LIGHTS = 3,
00292 ROADSIDE_TREES = 5,
00293 ROADSIDE_GRASS_ROAD_WORKS = 6,
00294 ROADSIDE_PAVED_ROAD_WORKS = 7
00295 };
00296
00297 static inline Roadside GetRoadside(TileIndex tile)
00298 {
00299 return (Roadside)GB(_m[tile].m6, 3, 3);
00300 }
00301
00302 static inline void SetRoadside(TileIndex tile, Roadside s)
00303 {
00304 SB(_m[tile].m6, 3, 3, s);
00305 }
00306
00307 static inline bool HasRoadWorks(TileIndex t)
00308 {
00309 return GetRoadside(t) >= ROADSIDE_GRASS_ROAD_WORKS;
00310 }
00311
00312 static inline bool IncreaseRoadWorksCounter(TileIndex t)
00313 {
00314 AB(_me[t].m7, 0, 4, 1);
00315
00316 return GB(_me[t].m7, 0, 4) == 15;
00317 }
00318
00319 static inline void StartRoadWorks(TileIndex t)
00320 {
00321 assert(!HasRoadWorks(t));
00322
00323 switch (GetRoadside(t)) {
00324 case ROADSIDE_BARREN:
00325 case ROADSIDE_GRASS: SetRoadside(t, ROADSIDE_GRASS_ROAD_WORKS); break;
00326 default: SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
00327 }
00328 }
00329
00330 static inline void TerminateRoadWorks(TileIndex t)
00331 {
00332 assert(HasRoadWorks(t));
00333 SetRoadside(t, (Roadside)(GetRoadside(t) - ROADSIDE_GRASS_ROAD_WORKS + ROADSIDE_GRASS));
00334
00335 SB(_me[t].m7, 0, 4, 0);
00336 }
00337
00338
00339 static inline DiagDirection GetRoadDepotDirection(TileIndex t)
00340 {
00341 assert(IsRoadDepot(t));
00342 return (DiagDirection)GB(_m[t].m5, 0, 2);
00343 }
00344
00345
00362 RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance = false);
00363
00372 bool IsPossibleCrossing(const TileIndex tile, Axis ax);
00373
00374
00375 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
00376 {
00377 SetTileType(t, MP_ROAD);
00378 SetTileOwner(t, road);
00379 _m[t].m2 = town;
00380 _m[t].m3 = (HasBit(rot, ROADTYPE_TRAM) ? bits : 0);
00381 _m[t].m4 = 0;
00382 _m[t].m5 = (HasBit(rot, ROADTYPE_ROAD) ? bits : 0) | ROAD_TILE_NORMAL << 6;
00383 SB(_m[t].m6, 2, 4, 0);
00384 _me[t].m7 = rot << 6;
00385 SetRoadOwner(t, ROADTYPE_TRAM, tram);
00386 }
00387
00388
00389 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
00390 {
00391 SetTileType(t, MP_ROAD);
00392 SetTileOwner(t, rail);
00393 _m[t].m2 = town;
00394 _m[t].m3 = rat;
00395 _m[t].m4 = 0;
00396 _m[t].m5 = ROAD_TILE_CROSSING << 6 | roaddir;
00397 SB(_m[t].m6, 2, 4, 0);
00398 _me[t].m7 = rot << 6 | road;
00399 SetRoadOwner(t, ROADTYPE_TRAM, tram);
00400 }
00401
00402
00403 static inline void MakeRoadDepot(TileIndex t, Owner owner, DiagDirection dir, RoadType rt, TownID town)
00404 {
00405 SetTileType(t, MP_ROAD);
00406 SetTileOwner(t, owner);
00407 _m[t].m2 = town;
00408 _m[t].m3 = 0;
00409 _m[t].m4 = 0;
00410 _m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
00411 SB(_m[t].m6, 2, 4, 0);
00412 _me[t].m7 = RoadTypeToRoadTypes(rt) << 6 | owner;
00413 SetRoadOwner(t, ROADTYPE_TRAM, owner);
00414 }
00415
00416 #endif