road_map.h

Go to the documentation of this file.
00001 /* $Id$ */
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 "town_type.h"
00019 #include "road_func.h"
00020 #include "tile_map.h"
00021 
00022 
00023 enum RoadTileType {
00024   ROAD_TILE_NORMAL,
00025   ROAD_TILE_CROSSING,
00026   ROAD_TILE_DEPOT
00027 };
00028 
00029 static inline RoadTileType GetRoadTileType(TileIndex t)
00030 {
00031   assert(IsTileType(t, MP_ROAD));
00032   return (RoadTileType)GB(_m[t].m5, 6, 2);
00033 }
00034 
00035 static inline bool IsNormalRoad(TileIndex t)
00036 {
00037   return GetRoadTileType(t) == ROAD_TILE_NORMAL;
00038 }
00039 
00040 static inline bool IsNormalRoadTile(TileIndex t)
00041 {
00042   return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
00043 }
00044 
00045 static inline bool IsLevelCrossing(TileIndex t)
00046 {
00047   return GetRoadTileType(t) == ROAD_TILE_CROSSING;
00048 }
00049 
00050 static inline bool IsLevelCrossingTile(TileIndex t)
00051 {
00052   return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
00053 }
00054 
00055 static inline bool IsRoadDepot(TileIndex t)
00056 {
00057   return GetRoadTileType(t) == ROAD_TILE_DEPOT;
00058 }
00059 
00060 static inline bool IsRoadDepotTile(TileIndex t)
00061 {
00062   return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
00063 }
00064 
00065 static inline RoadBits GetRoadBits(TileIndex t, RoadType rt)
00066 {
00067   assert(IsNormalRoad(t));
00068   switch (rt) {
00069     default: NOT_REACHED();
00070     case ROADTYPE_ROAD: return (RoadBits)GB(_m[t].m5, 0, 4);
00071     case ROADTYPE_TRAM: return (RoadBits)GB(_m[t].m3, 0, 4);
00072   }
00073 }
00074 
00082 static inline RoadBits GetOtherRoadBits(TileIndex t, RoadType rt)
00083 {
00084   return GetRoadBits(t, rt == ROADTYPE_ROAD ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00085 }
00086 
00093 static inline RoadBits GetAllRoadBits(TileIndex tile)
00094 {
00095   return GetRoadBits(tile, ROADTYPE_ROAD) | GetRoadBits(tile, ROADTYPE_TRAM);
00096 }
00097 
00098 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
00099 {
00100   assert(IsNormalRoad(t)); // XXX incomplete
00101   switch (rt) {
00102     default: NOT_REACHED();
00103     case ROADTYPE_ROAD: SB(_m[t].m5, 0, 4, r); break;
00104     case ROADTYPE_TRAM: SB(_m[t].m3, 0, 4, r); break;
00105   }
00106 }
00107 
00108 static inline RoadTypes GetRoadTypes(TileIndex t)
00109 {
00110   return (RoadTypes)GB(_me[t].m7, 6, 2);
00111 }
00112 
00113 static inline void SetRoadTypes(TileIndex t, RoadTypes rt)
00114 {
00115   assert(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
00116   SB(_me[t].m7, 6, 2, rt);
00117 }
00118 
00119 static inline bool HasTileRoadType(TileIndex t, RoadType rt)
00120 {
00121   return HasBit(GetRoadTypes(t), rt);
00122 }
00123 
00124 static inline Owner GetRoadOwner(TileIndex t, RoadType rt)
00125 {
00126   switch (rt) {
00127     default: NOT_REACHED();
00128     case ROADTYPE_ROAD: return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
00129     case ROADTYPE_TRAM: {
00130       /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
00131        * to OWNER_TOWN makes it use one bit less */
00132       Owner o = (Owner)GB(_m[t].m3, 4, 4);
00133       return o == OWNER_TOWN ? OWNER_NONE : o;
00134     }
00135   }
00136 }
00137 
00138 static inline void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
00139 {
00140   switch (rt) {
00141     default: NOT_REACHED();
00142     case ROADTYPE_ROAD: SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o); break;
00143     case ROADTYPE_TRAM: SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
00144   }
00145 }
00146 
00147 static inline bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
00148 {
00149   assert(HasTileRoadType(t, rt));
00150   return (GetRoadOwner(t, rt) == o);
00151 }
00152 
00158 static inline bool HasTownOwnedRoad(TileIndex t)
00159 {
00160   return HasTileRoadType(t, ROADTYPE_ROAD) && IsRoadOwner(t, ROADTYPE_ROAD, OWNER_TOWN);
00161 }
00162 
00164 enum DisallowedRoadDirections {
00165   DRD_NONE,       
00166   DRD_SOUTHBOUND, 
00167   DRD_NORTHBOUND, 
00168   DRD_BOTH,       
00169   DRD_END
00170 };
00171 DECLARE_ENUM_AS_BIT_SET(DisallowedRoadDirections);
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 */

Generated on Sat Dec 26 20:06:04 2009 for OpenTTD by  doxygen 1.5.6