# road_map.h

Go to the documentation of this file.

    00001 /* $Id: road_map.h 18809 2010-01-15 16:41:15Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifndef ROAD_MAP_H
    00013 #define ROAD_MAP_H
    00014
    00015 #include "track_func.h"
    00016 #include "depot_type.h"
    00017 #include "rail_type.h"
    00018 #include "road_func.h"
    00019 #include "tile_map.h"
    00020
    00021
    00022 enum RoadTileType {
    00023   ROAD_TILE_NORMAL,
    00024   ROAD_TILE_CROSSING,
    00025   ROAD_TILE_DEPOT
    00026 };
    00027
    00028 static inline RoadTileType GetRoadTileType(TileIndex t)
    00029 {
    00030   assert(IsTileType(t, MP_ROAD));
    00031   return (RoadTileType)GB(_m[t].m5, 6, 2);
    00032 }
    00033
    00034 static inline bool IsNormalRoad(TileIndex t)
    00035 {
    00036   return GetRoadTileType(t) == ROAD_TILE_NORMAL;
    00037 }
    00038
    00039 static inline bool IsNormalRoadTile(TileIndex t)
    00040 {
    00041   return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
    00042 }
    00043
    00044 static inline bool IsLevelCrossing(TileIndex t)
    00045 {
    00046   return GetRoadTileType(t) == ROAD_TILE_CROSSING;
    00047 }
    00048
    00049 static inline bool IsLevelCrossingTile(TileIndex t)
    00050 {
    00051   return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
    00052 }
    00053
    00054 static inline bool IsRoadDepot(TileIndex t)
    00055 {
    00056   return GetRoadTileType(t) == ROAD_TILE_DEPOT;
    00057 }
    00058
    00059 static inline bool IsRoadDepotTile(TileIndex t)
    00060 {
    00061   return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
    00062 }
    00063
    00064 static inline RoadBits GetRoadBits(TileIndex t, RoadType rt)
    00065 {
    00066   assert(IsNormalRoad(t));
    00067   switch (rt) {
    00068     default: NOT_REACHED();
    00069     case ROADTYPE_ROAD: return (RoadBits)GB(_m[t].m5, 0, 4);
    00070     case ROADTYPE_TRAM: return (RoadBits)GB(_m[t].m3, 0, 4);
    00071   }
    00072 }
    00073
    00081 static inline RoadBits GetOtherRoadBits(TileIndex t, RoadType rt)
    00082 {
    00083   return GetRoadBits(t, rt == ROADTYPE_ROAD ? ROADTYPE_TRAM : ROADTYPE_ROAD);
    00084 }
    00085
    00092 static inline RoadBits GetAllRoadBits(TileIndex tile)
    00093 {
    00094   return GetRoadBits(tile, ROADTYPE_ROAD) | GetRoadBits(tile, ROADTYPE_TRAM);
    00095 }
    00096
    00097 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
    00098 {
    00099   assert(IsNormalRoad(t)); // XXX incomplete
    00100   switch (rt) {
    00101     default: NOT_REACHED();
    00102     case ROADTYPE_ROAD: SB(_m[t].m5, 0, 4, r); break;
    00103     case ROADTYPE_TRAM: SB(_m[t].m3, 0, 4, r); break;
    00104   }
    00105 }
    00106
    00107 static inline RoadTypes GetRoadTypes(TileIndex t)
    00108 {
    00109   return (RoadTypes)GB(_me[t].m7, 6, 2);
    00110 }
    00111
    00112 static inline void SetRoadTypes(TileIndex t, RoadTypes rt)
    00113 {
    00114   assert(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
    00115   SB(_me[t].m7, 6, 2, rt);
    00116 }
    00117
    00118 static inline bool HasTileRoadType(TileIndex t, RoadType rt)
    00119 {
    00120   return HasBit(GetRoadTypes(t), rt);
    00121 }
    00122
    00123 static inline Owner GetRoadOwner(TileIndex t, RoadType rt)
    00124 {
    00125   switch (rt) {
    00126     default: NOT_REACHED();
    00127     case ROADTYPE_ROAD: return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
    00128     case ROADTYPE_TRAM: {
    00129       /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
    00130        * to OWNER_TOWN makes it use one bit less */
    00131       Owner o = (Owner)GB(_m[t].m3, 4, 4);
    00132       return o == OWNER_TOWN ? OWNER_NONE : o;
    00133     }
    00134   }
    00135 }
    00136
    00137 static inline void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
    00138 {
    00139   switch (rt) {
    00140     default: NOT_REACHED();
    00141     case ROADTYPE_ROAD: SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o); break;
    00142     case ROADTYPE_TRAM: SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
    00143   }
    00144 }
    00145
    00146 static inline bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
    00147 {
    00148   assert(HasTileRoadType(t, rt));
    00149   return (GetRoadOwner(t, rt) == o);
    00150 }
    00151
    00157 static inline bool HasTownOwnedRoad(TileIndex t)
    00158 {
    00159   return HasTileRoadType(t, ROADTYPE_ROAD) && IsRoadOwner(t, ROADTYPE_ROAD, OWNER_TOWN);
    00160 }
    00161
    00163 enum DisallowedRoadDirections {
    00164   DRD_NONE,
    00165   DRD_SOUTHBOUND,
    00166   DRD_NORTHBOUND,
    00167   DRD_BOTH,
    00168   DRD_END
    00169 };
    00170 DECLARE_ENUM_AS_BIT_SET(DisallowedRoadDirections);
    00171
    00177 static inline DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
    00178 {
    00179   assert(IsNormalRoad(t));
    00180   return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
    00181 }
    00182
    00188 static inline void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
    00189 {
    00190   assert(IsNormalRoad(t));
    00191   assert(drd < DRD_END);
    00192   SB(_m[t].m5, 4, 2, drd);
    00193 }
    00194
    00195 static inline Axis GetCrossingRoadAxis(TileIndex t)
    00196 {
    00197   assert(IsLevelCrossing(t));
    00198   return (Axis)GB(_m[t].m5, 0, 1);
    00199 }
    00200
    00201 static inline Axis GetCrossingRailAxis(TileIndex t)
    00202 {
    00203   assert(IsLevelCrossing(t));
    00204   return OtherAxis((Axis)GetCrossingRoadAxis(t));
    00205 }
    00206
    00207 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
    00208 {
    00209   return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
    00210 }
    00211
    00212 static inline Track GetCrossingRailTrack(TileIndex tile)
    00213 {
    00214   return AxisToTrack(GetCrossingRailAxis(tile));
    00215 }
    00216
    00217 static inline TrackBits GetCrossingRailBits(TileIndex tile)
    00218 {
    00219   return AxisToTrackBits(GetCrossingRailAxis(tile));
    00220 }
    00221
    00222
    00229 static inline bool HasCrossingReservation(TileIndex t)
    00230 {
    00231   assert(IsLevelCrossingTile(t));
    00232   return HasBit(_m[t].m5, 4);
    00233 }
    00234
    00242 static inline void SetCrossingReservation(TileIndex t, bool b)
    00243 {
    00244   assert(IsLevelCrossingTile(t));
    00245   SB(_m[t].m5, 4, 1, b ? 1 : 0);
    00246 }
    00247
    00254 static inline TrackBits GetCrossingReservationTrackBits(TileIndex t)
    00255 {
    00256   return HasCrossingReservation(t) ? GetCrossingRailBits(t) : TRACK_BIT_NONE;
    00257 }
    00258
    00259 static inline bool IsCrossingBarred(TileIndex t)
    00260 {
    00261   assert(IsLevelCrossing(t));
    00262   return HasBit(_m[t].m5, 5);
    00263 }
    00264
    00265 static inline void SetCrossingBarred(TileIndex t, bool barred)
    00266 {
    00267   assert(IsLevelCrossing(t));
    00268   SB(_m[t].m5, 5, 1, barred ? 1 : 0);
    00269 }
    00270
    00271 static inline void UnbarCrossing(TileIndex t)
    00272 {
    00273   SetCrossingBarred(t, false);
    00274 }
    00275
    00276 static inline void BarCrossing(TileIndex t)
    00277 {
    00278   SetCrossingBarred(t, true);
    00279 }
    00280
    00281 #define IsOnDesert IsOnSnow
    00282 static inline bool IsOnSnow(TileIndex t)
    00283 {
    00284   return HasBit(_me[t].m7, 5);
    00285 }
    00286
    00287 #define ToggleDesert ToggleSnow
    00288 static inline void ToggleSnow(TileIndex t)
    00289 {
    00290   ToggleBit(_me[t].m7, 5);
    00291 }
    00292
    00293
    00294 enum Roadside {
    00295   ROADSIDE_BARREN           = 0,
    00296   ROADSIDE_GRASS            = 1,
    00297   ROADSIDE_PAVED            = 2,
    00298   ROADSIDE_STREET_LIGHTS    = 3,
    00299   ROADSIDE_TREES            = 5,
    00300   ROADSIDE_GRASS_ROAD_WORKS = 6,
    00301   ROADSIDE_PAVED_ROAD_WORKS = 7
    00302 };
    00303
    00304 static inline Roadside GetRoadside(TileIndex tile)
    00305 {
    00306   return (Roadside)GB(_m[tile].m6, 3, 3);
    00307 }
    00308
    00309 static inline void SetRoadside(TileIndex tile, Roadside s)
    00310 {
    00311   SB(_m[tile].m6, 3, 3, s);
    00312 }
    00313
    00314 static inline bool HasRoadWorks(TileIndex t)
    00315 {
    00316   return GetRoadside(t) >= ROADSIDE_GRASS_ROAD_WORKS;
    00317 }
    00318
    00319 static inline bool IncreaseRoadWorksCounter(TileIndex t)
    00320 {
    00321   AB(_me[t].m7, 0, 4, 1);
    00322
    00323   return GB(_me[t].m7, 0, 4) == 15;
    00324 }
    00325
    00326 static inline void StartRoadWorks(TileIndex t)
    00327 {
    00328   assert(!HasRoadWorks(t));
    00329   /* Remove any trees or lamps in case or roadwork */
    00330   switch (GetRoadside(t)) {
    00331     case ROADSIDE_BARREN:
    00332     case ROADSIDE_GRASS:  SetRoadside(t, ROADSIDE_GRASS_ROAD_WORKS); break;
    00333     default:              SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
    00334   }
    00335 }
    00336
    00337 static inline void TerminateRoadWorks(TileIndex t)
    00338 {
    00339   assert(HasRoadWorks(t));
    00340   SetRoadside(t, (Roadside)(GetRoadside(t) - ROADSIDE_GRASS_ROAD_WORKS + ROADSIDE_GRASS));
    00341   /* Stop the counter */
    00342   SB(_me[t].m7, 0, 4, 0);
    00343 }
    00344
    00345
    00346 static inline DiagDirection GetRoadDepotDirection(TileIndex t)
    00347 {
    00348   assert(IsRoadDepot(t));
    00349   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00350 }
    00351
    00352
    00369 RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance = false);
    00370
    00371
    00372 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
    00373 {
    00374   SetTileType(t, MP_ROAD);
    00375   SetTileOwner(t, road);
    00376   _m[t].m2 = town;
    00377   _m[t].m3 = (HasBit(rot, ROADTYPE_TRAM) ? bits : 0);
    00378   _m[t].m4 = 0;
    00379   _m[t].m5 = (HasBit(rot, ROADTYPE_ROAD) ? bits : 0) | ROAD_TILE_NORMAL << 6;
    00380   SB(_m[t].m6, 2, 4, 0);
    00381   _me[t].m7 = rot << 6;
    00382   SetRoadOwner(t, ROADTYPE_TRAM, tram);
    00383 }
    00384
    00385
    00386 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
    00387 {
    00388   SetTileType(t, MP_ROAD);
    00389   SetTileOwner(t, rail);
    00390   _m[t].m2 = town;
    00391   _m[t].m3 = rat;
    00392   _m[t].m4 = 0;
    00393   _m[t].m5 = ROAD_TILE_CROSSING << 6 | roaddir;
    00394   SB(_m[t].m6, 2, 4, 0);
    00395   _me[t].m7 = rot << 6 | road;
    00396   SetRoadOwner(t, ROADTYPE_TRAM, tram);
    00397 }
    00398
    00399
    00400 static inline void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
    00401 {
    00402   SetTileType(t, MP_ROAD);
    00403   SetTileOwner(t, owner);
    00404   _m[t].m2 = did;
    00405   _m[t].m3 = 0;
    00406   _m[t].m4 = 0;
    00407   _m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
    00408   SB(_m[t].m6, 2, 4, 0);
    00409   _me[t].m7 = RoadTypeToRoadTypes(rt) << 6 | owner;
    00410   SetRoadOwner(t, ROADTYPE_TRAM, owner);
    00411 }
    00412
    00413 #endif /* ROAD_MAP_H */