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 "road_func.h"
00019 #include "tile_map.h"
00020 
00021 
00023 enum RoadTileType {
00024   ROAD_TILE_NORMAL,   
00025   ROAD_TILE_CROSSING, 
00026   ROAD_TILE_DEPOT     
00027 };
00028 
00035 static inline RoadTileType GetRoadTileType(TileIndex t)
00036 {
00037   assert(IsTileType(t, MP_ROAD));
00038   return (RoadTileType)GB(_m[t].m5, 6, 2);
00039 }
00040 
00047 static inline bool IsNormalRoad(TileIndex t)
00048 {
00049   return GetRoadTileType(t) == ROAD_TILE_NORMAL;
00050 }
00051 
00057 static inline bool IsNormalRoadTile(TileIndex t)
00058 {
00059   return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
00060 }
00061 
00068 static inline bool IsLevelCrossing(TileIndex t)
00069 {
00070   return GetRoadTileType(t) == ROAD_TILE_CROSSING;
00071 }
00072 
00078 static inline bool IsLevelCrossingTile(TileIndex t)
00079 {
00080   return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
00081 }
00082 
00089 static inline bool IsRoadDepot(TileIndex t)
00090 {
00091   return GetRoadTileType(t) == ROAD_TILE_DEPOT;
00092 }
00093 
00099 static inline bool IsRoadDepotTile(TileIndex t)
00100 {
00101   return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
00102 }
00103 
00111 static inline RoadBits GetRoadBits(TileIndex t, RoadType rt)
00112 {
00113   assert(IsNormalRoad(t));
00114   switch (rt) {
00115     default: NOT_REACHED();
00116     case ROADTYPE_ROAD: return (RoadBits)GB(_m[t].m5, 0, 4);
00117     case ROADTYPE_TRAM: return (RoadBits)GB(_m[t].m3, 0, 4);
00118   }
00119 }
00120 
00128 static inline RoadBits GetOtherRoadBits(TileIndex t, RoadType rt)
00129 {
00130   return GetRoadBits(t, rt == ROADTYPE_ROAD ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00131 }
00132 
00139 static inline RoadBits GetAllRoadBits(TileIndex tile)
00140 {
00141   return GetRoadBits(tile, ROADTYPE_ROAD) | GetRoadBits(tile, ROADTYPE_TRAM);
00142 }
00143 
00151 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
00152 {
00153   assert(IsNormalRoad(t)); // XXX incomplete
00154   switch (rt) {
00155     default: NOT_REACHED();
00156     case ROADTYPE_ROAD: SB(_m[t].m5, 0, 4, r); break;
00157     case ROADTYPE_TRAM: SB(_m[t].m3, 0, 4, r); break;
00158   }
00159 }
00160 
00166 static inline RoadTypes GetRoadTypes(TileIndex t)
00167 {
00168   return (RoadTypes)GB(_me[t].m7, 6, 2);
00169 }
00170 
00176 static inline void SetRoadTypes(TileIndex t, RoadTypes rt)
00177 {
00178   assert(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
00179   SB(_me[t].m7, 6, 2, rt);
00180 }
00181 
00188 static inline bool HasTileRoadType(TileIndex t, RoadType rt)
00189 {
00190   return HasBit(GetRoadTypes(t), rt);
00191 }
00192 
00199 static inline Owner GetRoadOwner(TileIndex t, RoadType rt)
00200 {
00201   assert(IsTileType(t, MP_ROAD) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
00202   switch (rt) {
00203     default: NOT_REACHED();
00204     case ROADTYPE_ROAD: return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
00205     case ROADTYPE_TRAM: {
00206       /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
00207        * to OWNER_TOWN makes it use one bit less */
00208       Owner o = (Owner)GB(_m[t].m3, 4, 4);
00209       return o == OWNER_TOWN ? OWNER_NONE : o;
00210     }
00211   }
00212 }
00213 
00220 static inline void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
00221 {
00222   switch (rt) {
00223     default: NOT_REACHED();
00224     case ROADTYPE_ROAD: SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o); break;
00225     case ROADTYPE_TRAM: SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
00226   }
00227 }
00228 
00237 static inline bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
00238 {
00239   assert(HasTileRoadType(t, rt));
00240   return (GetRoadOwner(t, rt) == o);
00241 }
00242 
00249 static inline bool HasTownOwnedRoad(TileIndex t)
00250 {
00251   return HasTileRoadType(t, ROADTYPE_ROAD) && IsRoadOwner(t, ROADTYPE_ROAD, OWNER_TOWN);
00252 }
00253 
00254 static inline void MakeTrafficLights(TileIndex t)
00255 {
00256   assert(IsTileType(t, MP_ROAD));
00257   assert(GetRoadTileType(t) == ROAD_TILE_NORMAL);
00258   SetBit(_me[t].m7, 4);
00259 }
00260 
00261 static inline void ClearTrafficLights(TileIndex t)
00262 {
00263   assert(IsTileType(t, MP_ROAD));
00264   assert(GetRoadTileType(t) == ROAD_TILE_NORMAL);
00265   ClrBit(_me[t].m7, 4);
00266 }
00267 
00272 static inline bool HasTrafficLights(TileIndex t)
00273 {
00274   return (IsTileType(t, MP_ROAD) && (GetRoadTileType(t) == ROAD_TILE_NORMAL) && HasBit(_me[t].m7, 4));
00275 }
00276 
00278 enum DisallowedRoadDirections {
00279   DRD_NONE,       
00280   DRD_SOUTHBOUND, 
00281   DRD_NORTHBOUND, 
00282   DRD_BOTH,       
00283   DRD_END
00284 };
00285 DECLARE_ENUM_AS_BIT_SET(DisallowedRoadDirections)
00287 template <> struct EnumPropsT<DisallowedRoadDirections> : MakeEnumPropsT<DisallowedRoadDirections, byte, DRD_NONE, DRD_END, DRD_END, 2> {};
00288 
00294 static inline DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
00295 {
00296   assert(IsNormalRoad(t));
00297   return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
00298 }
00299 
00305 static inline void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
00306 {
00307   assert(IsNormalRoad(t));
00308   assert(drd < DRD_END);
00309   SB(_m[t].m5, 4, 2, drd);
00310 }
00311 
00318 static inline Axis GetCrossingRoadAxis(TileIndex t)
00319 {
00320   assert(IsLevelCrossing(t));
00321   return (Axis)GB(_m[t].m5, 0, 1);
00322 }
00323 
00330 static inline Axis GetCrossingRailAxis(TileIndex t)
00331 {
00332   assert(IsLevelCrossing(t));
00333   return OtherAxis((Axis)GetCrossingRoadAxis(t));
00334 }
00335 
00341 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
00342 {
00343   return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
00344 }
00345 
00351 static inline Track GetCrossingRailTrack(TileIndex tile)
00352 {
00353   return AxisToTrack(GetCrossingRailAxis(tile));
00354 }
00355 
00361 static inline TrackBits GetCrossingRailBits(TileIndex tile)
00362 {
00363   return AxisToTrackBits(GetCrossingRailAxis(tile));
00364 }
00365 
00366 
00373 static inline bool HasCrossingReservation(TileIndex t)
00374 {
00375   assert(IsLevelCrossingTile(t));
00376   return HasBit(_m[t].m5, 4);
00377 }
00378 
00386 static inline void SetCrossingReservation(TileIndex t, bool b)
00387 {
00388   assert(IsLevelCrossingTile(t));
00389   SB(_m[t].m5, 4, 1, b ? 1 : 0);
00390 }
00391 
00398 static inline TrackBits GetCrossingReservationTrackBits(TileIndex t)
00399 {
00400   return HasCrossingReservation(t) ? GetCrossingRailBits(t) : TRACK_BIT_NONE;
00401 }
00402 
00409 static inline bool IsCrossingBarred(TileIndex t)
00410 {
00411   assert(IsLevelCrossing(t));
00412   return HasBit(_m[t].m5, 5);
00413 }
00414 
00421 static inline void SetCrossingBarred(TileIndex t, bool barred)
00422 {
00423   assert(IsLevelCrossing(t));
00424   SB(_m[t].m5, 5, 1, barred ? 1 : 0);
00425 }
00426 
00431 static inline void UnbarCrossing(TileIndex t)
00432 {
00433   SetCrossingBarred(t, false);
00434 }
00435 
00440 static inline void BarCrossing(TileIndex t)
00441 {
00442   SetCrossingBarred(t, true);
00443 }
00444 
00446 #define IsOnDesert IsOnSnow
00447 
00452 static inline bool IsOnSnow(TileIndex t)
00453 {
00454   return HasBit(_me[t].m7, 5);
00455 }
00456 
00458 #define ToggleDesert ToggleSnow
00459 
00463 static inline void ToggleSnow(TileIndex t)
00464 {
00465   ToggleBit(_me[t].m7, 5);
00466 }
00467 
00468 
00470 enum Roadside {
00471   ROADSIDE_BARREN           = 0, 
00472   ROADSIDE_GRASS            = 1, 
00473   ROADSIDE_PAVED            = 2, 
00474   ROADSIDE_STREET_LIGHTS    = 3, 
00475   ROADSIDE_TREES            = 5, 
00476   ROADSIDE_GRASS_ROAD_WORKS = 6, 
00477   ROADSIDE_PAVED_ROAD_WORKS = 7  
00478 };
00479 
00485 static inline Roadside GetRoadside(TileIndex tile)
00486 {
00487   return (Roadside)GB(_m[tile].m6, 3, 3);
00488 }
00489 
00495 static inline void SetRoadside(TileIndex tile, Roadside s)
00496 {
00497   SB(_m[tile].m6, 3, 3, s);
00498 }
00499 
00505 static inline bool HasRoadWorks(TileIndex t)
00506 {
00507   return GetRoadside(t) >= ROADSIDE_GRASS_ROAD_WORKS;
00508 }
00509 
00515 static inline bool IncreaseRoadWorksCounter(TileIndex t)
00516 {
00517   AB(_me[t].m7, 0, 4, 1);
00518 
00519   return GB(_me[t].m7, 0, 4) == 15;
00520 }
00521 
00522 static inline byte GetRoadWorksCounter(TileIndex t)
00523 {
00524   return GB(_m[t].m3, 0, 4);
00525 }
00526 
00532 static inline void StartRoadWorks(TileIndex t)
00533 {
00534   assert(!HasRoadWorks(t));
00535   /* Remove any trees or lamps in case or roadwork */
00536   switch (GetRoadside(t)) {
00537     case ROADSIDE_BARREN:
00538     case ROADSIDE_GRASS:  SetRoadside(t, ROADSIDE_GRASS_ROAD_WORKS); break;
00539     default:              SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
00540   }
00541 }
00542 
00548 static inline void TerminateRoadWorks(TileIndex t)
00549 {
00550   assert(HasRoadWorks(t));
00551   SetRoadside(t, (Roadside)(GetRoadside(t) - ROADSIDE_GRASS_ROAD_WORKS + ROADSIDE_GRASS));
00552   /* Stop the counter */
00553   SB(_me[t].m7, 0, 4, 0);
00554 }
00555 
00556 
00562 static inline DiagDirection GetRoadDepotDirection(TileIndex t)
00563 {
00564   assert(IsRoadDepot(t));
00565   return (DiagDirection)GB(_m[t].m5, 0, 2);
00566 }
00567 
00568 
00569 RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance = false);
00570 
00571 
00581 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
00582 {
00583   SetTileType(t, MP_ROAD);
00584   SetTileOwner(t, road);
00585   _m[t].m2 = town;
00586   _m[t].m3 = (HasBit(rot, ROADTYPE_TRAM) ? bits : 0);
00587   _m[t].m4 = 0;
00588   _m[t].m5 = (HasBit(rot, ROADTYPE_ROAD) ? bits : 0) | ROAD_TILE_NORMAL << 6;
00589   SB(_m[t].m6, 2, 4, 0);
00590   _me[t].m7 = rot << 6;
00591   SetRoadOwner(t, ROADTYPE_TRAM, tram);
00592 }
00593 
00605 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
00606 {
00607   SetTileType(t, MP_ROAD);
00608   SetTileOwner(t, rail);
00609   _m[t].m2 = town;
00610   _m[t].m3 = rat;
00611   _m[t].m4 = 0;
00612   _m[t].m5 = ROAD_TILE_CROSSING << 6 | roaddir;
00613   SB(_m[t].m6, 2, 4, 0);
00614   _me[t].m7 = rot << 6 | road;
00615   SetRoadOwner(t, ROADTYPE_TRAM, tram);
00616 }
00617 
00626 static inline void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
00627 {
00628   SetTileType(t, MP_ROAD);
00629   SetTileOwner(t, owner);
00630   _m[t].m2 = did;
00631   _m[t].m3 = 0;
00632   _m[t].m4 = 0;
00633   _m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
00634   SB(_m[t].m6, 2, 4, 0);
00635   _me[t].m7 = RoadTypeToRoadTypes(rt) << 6 | owner;
00636   SetRoadOwner(t, ROADTYPE_TRAM, owner);
00637 }
00638 
00639 #endif /* ROAD_MAP_H */