station_map.h

Go to the documentation of this file.
00001 /* $Id: station_map.h 21273 2010-11-20 15:44:24Z alberth $ */
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 STATION_MAP_H
00013 #define STATION_MAP_H
00014 
00015 #include "rail_map.h"
00016 #include "road_map.h"
00017 #include "water_map.h"
00018 #include "station_func.h"
00019 #include "rail.h"
00020 
00021 typedef byte StationGfx; 
00022 
00029 static inline StationID GetStationIndex(TileIndex t)
00030 {
00031   assert(IsTileType(t, MP_STATION));
00032   return (StationID)_m[t].m2;
00033 }
00034 
00035 
00036 static const int GFX_DOCK_BASE_WATER_PART          =  4;
00037 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET =  4;
00038 
00045 static inline StationType GetStationType(TileIndex t)
00046 {
00047   assert(IsTileType(t, MP_STATION));
00048   return (StationType)GB(_m[t].m6, 3, 3);
00049 }
00050 
00057 static inline RoadStopType GetRoadStopType(TileIndex t)
00058 {
00059   assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
00060   return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
00061 }
00062 
00069 static inline StationGfx GetStationGfx(TileIndex t)
00070 {
00071   assert(IsTileType(t, MP_STATION));
00072   return _m[t].m5;
00073 }
00074 
00081 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
00082 {
00083   assert(IsTileType(t, MP_STATION));
00084   _m[t].m5 = gfx;
00085 }
00086 
00093 static inline bool IsRailStation(TileIndex t)
00094 {
00095   return GetStationType(t) == STATION_RAIL;
00096 }
00097 
00103 static inline bool IsRailStationTile(TileIndex t)
00104 {
00105   return IsTileType(t, MP_STATION) && IsRailStation(t);
00106 }
00107 
00114 static inline bool IsRailWaypoint(TileIndex t)
00115 {
00116   return GetStationType(t) == STATION_WAYPOINT;
00117 }
00118 
00124 static inline bool IsRailWaypointTile(TileIndex t)
00125 {
00126   return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
00127 }
00128 
00136 static inline bool HasStationRail(TileIndex t)
00137 {
00138   return IsRailStation(t) || IsRailWaypoint(t);
00139 }
00140 
00147 static inline bool HasStationTileRail(TileIndex t)
00148 {
00149   return IsTileType(t, MP_STATION) && HasStationRail(t);
00150 }
00151 
00158 static inline bool IsAirport(TileIndex t)
00159 {
00160   return GetStationType(t) == STATION_AIRPORT;
00161 }
00162 
00168 static inline bool IsAirportTile(TileIndex t)
00169 {
00170   return IsTileType(t, MP_STATION) && IsAirport(t);
00171 }
00172 
00173 bool IsHangar(TileIndex t);
00174 
00181 static inline bool IsTruckStop(TileIndex t)
00182 {
00183   return GetStationType(t) == STATION_TRUCK;
00184 }
00185 
00192 static inline bool IsBusStop(TileIndex t)
00193 {
00194   return GetStationType(t) == STATION_BUS;
00195 }
00196 
00203 static inline bool IsRoadStop(TileIndex t)
00204 {
00205   assert(IsTileType(t, MP_STATION));
00206   return IsTruckStop(t) || IsBusStop(t);
00207 }
00208 
00214 static inline bool IsRoadStopTile(TileIndex t)
00215 {
00216   return IsTileType(t, MP_STATION) && IsRoadStop(t);
00217 }
00218 
00224 static inline bool IsStandardRoadStopTile(TileIndex t)
00225 {
00226   return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
00227 }
00228 
00234 static inline bool IsDriveThroughStopTile(TileIndex t)
00235 {
00236   return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
00237 }
00238 
00245 static inline StationGfx GetAirportGfx(TileIndex t)
00246 {
00247   assert(IsAirport(t));
00248   extern StationGfx GetTranslatedAirportTileID(StationGfx gfx);
00249   return GetTranslatedAirportTileID(GetStationGfx(t));
00250 }
00251 
00258 static inline DiagDirection GetRoadStopDir(TileIndex t)
00259 {
00260   StationGfx gfx = GetStationGfx(t);
00261   assert(IsRoadStopTile(t));
00262   if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
00263     return (DiagDirection)(gfx);
00264   } else {
00265     return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
00266   }
00267 }
00268 
00275 static inline bool IsOilRig(TileIndex t)
00276 {
00277   return GetStationType(t) == STATION_OILRIG;
00278 }
00279 
00286 static inline bool IsDock(TileIndex t)
00287 {
00288   return GetStationType(t) == STATION_DOCK;
00289 }
00290 
00296 static inline bool IsDockTile(TileIndex t)
00297 {
00298   return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
00299 }
00300 
00307 static inline bool IsBuoy(TileIndex t)
00308 {
00309   return GetStationType(t) == STATION_BUOY;
00310 }
00311 
00317 static inline bool IsBuoyTile(TileIndex t)
00318 {
00319   return IsTileType(t, MP_STATION) && IsBuoy(t);
00320 }
00321 
00327 static inline bool IsHangarTile(TileIndex t)
00328 {
00329   return IsTileType(t, MP_STATION) && IsHangar(t);
00330 }
00331 
00338 static inline Axis GetRailStationAxis(TileIndex t)
00339 {
00340   assert(HasStationRail(t));
00341   return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
00342 }
00343 
00350 static inline Track GetRailStationTrack(TileIndex t)
00351 {
00352   return AxisToTrack(GetRailStationAxis(t));
00353 }
00354 
00361 static inline TrackBits GetRailStationTrackBits(TileIndex t)
00362 {
00363   return AxisToTrackBits(GetRailStationAxis(t));
00364 }
00365 
00379 static inline bool IsCompatibleTrainStationTile(TileIndex t1, TileIndex t2)
00380 {
00381   assert(IsRailStationTile(t2));
00382   return IsRailStationTile(t1) && IsCompatibleRail(GetRailType(t1), GetRailType(t2)) &&
00383       GetRailStationAxis(t1) == GetRailStationAxis(t2) &&
00384       GetStationIndex(t1) == GetStationIndex(t2) &&
00385       !IsStationTileBlocked(t1);
00386 }
00387 
00394 static inline bool HasStationReservation(TileIndex t)
00395 {
00396   assert(HasStationRail(t));
00397   return HasBit(_m[t].m6, 2);
00398 }
00399 
00406 static inline void SetRailStationReservation(TileIndex t, bool b)
00407 {
00408   assert(HasStationRail(t));
00409   SB(_m[t].m6, 2, 1, b ? 1 : 0);
00410 }
00411 
00418 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
00419 {
00420   return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
00421 }
00422 
00430 static inline DiagDirection GetDockDirection(TileIndex t)
00431 {
00432   StationGfx gfx = GetStationGfx(t);
00433   assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
00434   return (DiagDirection)(gfx);
00435 }
00436 
00444 static inline TileIndexDiffC GetDockOffset(TileIndex t)
00445 {
00446   static const TileIndexDiffC buoy_offset = {0, 0};
00447   static const TileIndexDiffC oilrig_offset = {2, 0};
00448   static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
00449     {-2,  0},
00450     { 0,  2},
00451     { 2,  0},
00452     { 0, -2},
00453   };
00454   assert(IsTileType(t, MP_STATION));
00455 
00456   if (IsBuoy(t)) return buoy_offset;
00457   if (IsOilRig(t)) return oilrig_offset;
00458 
00459   assert(IsDock(t));
00460 
00461   return dock_offset[GetDockDirection(t)];
00462 }
00463 
00470 static inline bool IsCustomStationSpecIndex(TileIndex t)
00471 {
00472   assert(HasStationTileRail(t));
00473   return _m[t].m4 != 0;
00474 }
00475 
00481 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
00482 {
00483   assert(HasStationTileRail(t));
00484   _m[t].m4 = specindex;
00485 }
00486 
00493 static inline uint GetCustomStationSpecIndex(TileIndex t)
00494 {
00495   assert(HasStationTileRail(t));
00496   return _m[t].m4;
00497 }
00498 
00504 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
00505 {
00506   assert(IsTileType(t, MP_STATION));
00507   SB(_m[t].m3, 4, 4, random_bits);
00508 }
00509 
00516 static inline byte GetStationTileRandomBits(TileIndex t)
00517 {
00518   assert(IsTileType(t, MP_STATION));
00519   return GB(_m[t].m3, 4, 4);
00520 }
00521 
00530 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
00531 {
00532   SetTileType(t, MP_STATION);
00533   SetTileOwner(t, o);
00534   SetWaterClass(t, wc);
00535   _m[t].m2 = sid;
00536   _m[t].m3 = 0;
00537   _m[t].m4 = 0;
00538   _m[t].m5 = section;
00539   SB(_m[t].m6, 2, 1, 0);
00540   SB(_m[t].m6, 3, 3, st);
00541   _me[t].m7 = 0;
00542 }
00543 
00553 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
00554 {
00555   MakeStation(t, o, sid, STATION_RAIL, section + a);
00556   SetRailType(t, rt);
00557   SetRailStationReservation(t, false);
00558 }
00559 
00569 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
00570 {
00571   MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
00572   SetRailType(t, rt);
00573   SetRailStationReservation(t, false);
00574 }
00575 
00585 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
00586 {
00587   MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
00588   SetRoadTypes(t, rt);
00589   SetRoadOwner(t, ROADTYPE_ROAD, o);
00590   SetRoadOwner(t, ROADTYPE_TRAM, o);
00591 }
00592 
00604 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
00605 {
00606   MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
00607   SetRoadTypes(t, rt);
00608   SetRoadOwner(t, ROADTYPE_ROAD, road);
00609   SetRoadOwner(t, ROADTYPE_TRAM, tram);
00610 }
00611 
00620 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
00621 {
00622   MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
00623 }
00624 
00631 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
00632 {
00633   /* Make the owner of the buoy tile the same as the current owner of the
00634    * water tile. In this way, we can reset the owner of the water to its
00635    * original state when the buoy gets removed. */
00636   MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
00637 }
00638 
00647 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
00648 {
00649   MakeStation(t, o, sid, STATION_DOCK, d);
00650   MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
00651 }
00652 
00659 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
00660 {
00661   MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
00662 }
00663 
00664 #endif /* STATION_MAP_H */

Generated on Sun Jan 9 16:02:02 2011 for OpenTTD by  doxygen 1.6.1