# road_map.h

Go to the documentation of this file.

    00001 /* $Id: road_map.h 19665 2010-04-17 22:27:49Z 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 template <> struct EnumPropsT<DisallowedRoadDirections> : MakeEnumPropsT<DisallowedRoadDirections, byte, DRD_NONE, DRD_END, DRD_END, 2> {};
    00172
    00178 static inline DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
    00179 {
    00180   assert(IsNormalRoad(t));
    00181   return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
    00182 }
    00183
    00189 static inline void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
    00190 {
    00191   assert(IsNormalRoad(t));
    00192   assert(drd < DRD_END);
    00193   SB(_m[t].m5, 4, 2, drd);
    00194 }
    00195
    00196 static inline Axis GetCrossingRoadAxis(TileIndex t)
    00197 {
    00198   assert(IsLevelCrossing(t));
    00199   return (Axis)GB(_m[t].m5, 0, 1);
    00200 }
    00201
    00202 static inline Axis GetCrossingRailAxis(TileIndex t)
    00203 {
    00204   assert(IsLevelCrossing(t));
    00205   return OtherAxis((Axis)GetCrossingRoadAxis(t));
    00206 }
    00207
    00208 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
    00209 {
    00210   return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
    00211 }
    00212
    00213 static inline Track GetCrossingRailTrack(TileIndex tile)
    00214 {
    00215   return AxisToTrack(GetCrossingRailAxis(tile));
    00216 }
    00217
    00218 static inline TrackBits GetCrossingRailBits(TileIndex tile)
    00219 {
    00220   return AxisToTrackBits(GetCrossingRailAxis(tile));
    00221 }
    00222
    00223
    00230 static inline bool HasCrossingReservation(TileIndex t)
    00231 {
    00232   assert(IsLevelCrossingTile(t));
    00233   return HasBit(_m[t].m5, 4);
    00234 }
    00235
    00243 static inline void SetCrossingReservation(TileIndex t, bool b)
    00244 {
    00245   assert(IsLevelCrossingTile(t));
    00246   SB(_m[t].m5, 4, 1, b ? 1 : 0);
    00247 }
    00248
    00255 static inline TrackBits GetCrossingReservationTrackBits(TileIndex t)
    00256 {
    00257   return HasCrossingReservation(t) ? GetCrossingRailBits(t) : TRACK_BIT_NONE;
    00258 }
    00259
    00260 static inline bool IsCrossingBarred(TileIndex t)
    00261 {
    00262   assert(IsLevelCrossing(t));
    00263   return HasBit(_m[t].m5, 5);
    00264 }
    00265
    00266 static inline void SetCrossingBarred(TileIndex t, bool barred)
    00267 {
    00268   assert(IsLevelCrossing(t));
    00269   SB(_m[t].m5, 5, 1, barred ? 1 : 0);
    00270 }
    00271
    00272 static inline void UnbarCrossing(TileIndex t)
    00273 {
    00274   SetCrossingBarred(t, false);
    00275 }
    00276
    00277 static inline void BarCrossing(TileIndex t)
    00278 {
    00279   SetCrossingBarred(t, true);
    00280 }
    00281
    00282 #define IsOnDesert IsOnSnow
    00283 static inline bool IsOnSnow(TileIndex t)
    00284 {
    00285   return HasBit(_me[t].m7, 5);
    00286 }
    00287
    00288 #define ToggleDesert ToggleSnow
    00289 static inline void ToggleSnow(TileIndex t)
    00290 {
    00291   ToggleBit(_me[t].m7, 5);
    00292 }
    00293
    00294
    00295 enum Roadside {
    00296   ROADSIDE_BARREN           = 0,
    00297   ROADSIDE_GRASS            = 1,
    00298   ROADSIDE_PAVED            = 2,
    00299   ROADSIDE_STREET_LIGHTS    = 3,
    00300   ROADSIDE_TREES            = 5,
    00301   ROADSIDE_GRASS_ROAD_WORKS = 6,
    00302   ROADSIDE_PAVED_ROAD_WORKS = 7
    00303 };
    00304
    00305 static inline Roadside GetRoadside(TileIndex tile)
    00306 {
    00307   return (Roadside)GB(_m[tile].m6, 3, 3);
    00308 }
    00309
    00310 static inline void SetRoadside(TileIndex tile, Roadside s)
    00311 {
    00312   SB(_m[tile].m6, 3, 3, s);
    00313 }
    00314
    00315 static inline bool HasRoadWorks(TileIndex t)
    00316 {
    00317   return GetRoadside(t) >= ROADSIDE_GRASS_ROAD_WORKS;
    00318 }
    00319
    00320 static inline bool IncreaseRoadWorksCounter(TileIndex t)
    00321 {
    00322   AB(_me[t].m7, 0, 4, 1);
    00323
    00324   return GB(_me[t].m7, 0, 4) == 15;
    00325 }
    00326
    00327 static inline void StartRoadWorks(TileIndex t)
    00328 {
    00329   assert(!HasRoadWorks(t));
    00330   /* Remove any trees or lamps in case or roadwork */
    00331   switch (GetRoadside(t)) {
    00332     case ROADSIDE_BARREN:
    00333     case ROADSIDE_GRASS:  SetRoadside(t, ROADSIDE_GRASS_ROAD_WORKS); break;
    00334     default:              SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
    00335   }
    00336 }
    00337
    00338 static inline void TerminateRoadWorks(TileIndex t)
    00339 {
    00340   assert(HasRoadWorks(t));
    00341   SetRoadside(t, (Roadside)(GetRoadside(t) - ROADSIDE_GRASS_ROAD_WORKS + ROADSIDE_GRASS));
    00342   /* Stop the counter */
    00343   SB(_me[t].m7, 0, 4, 0);
    00344 }
    00345
    00346
    00347 static inline DiagDirection GetRoadDepotDirection(TileIndex t)
    00348 {
    00349   assert(IsRoadDepot(t));
    00350   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00351 }
    00352
    00353
    00370 RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance = false);
    00371
    00372
    00373 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
    00374 {
    00375   SetTileType(t, MP_ROAD);
    00376   SetTileOwner(t, road);
    00377   _m[t].m2 = town;
    00378   _m[t].m3 = (HasBit(rot, ROADTYPE_TRAM) ? bits : 0);
    00379   _m[t].m4 = 0;
    00380   _m[t].m5 = (HasBit(rot, ROADTYPE_ROAD) ? bits : 0) | ROAD_TILE_NORMAL << 6;
    00381   SB(_m[t].m6, 2, 4, 0);
    00382   _me[t].m7 = rot << 6;
    00383   SetRoadOwner(t, ROADTYPE_TRAM, tram);
    00384 }
    00385
    00386
    00387 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
    00388 {
    00389   SetTileType(t, MP_ROAD);
    00390   SetTileOwner(t, rail);
    00391   _m[t].m2 = town;
    00392   _m[t].m3 = rat;
    00393   _m[t].m4 = 0;
    00394   _m[t].m5 = ROAD_TILE_CROSSING << 6 | roaddir;
    00395   SB(_m[t].m6, 2, 4, 0);
    00396   _me[t].m7 = rot << 6 | road;
    00397   SetRoadOwner(t, ROADTYPE_TRAM, tram);
    00398 }
    00399
    00400
    00401 static inline void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
    00402 {
    00403   SetTileType(t, MP_ROAD);
    00404   SetTileOwner(t, owner);
    00405   _m[t].m2 = did;
    00406   _m[t].m3 = 0;
    00407   _m[t].m4 = 0;
    00408   _m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
    00409   SB(_m[t].m6, 2, 4, 0);
    00410   _me[t].m7 = RoadTypeToRoadTypes(rt) << 6 | owner;
    00411   SetRoadOwner(t, ROADTYPE_TRAM, owner);
    00412 }
    00413
    00414 #endif /* ROAD_MAP_H */