# station_map.h

Go to the documentation of this file.

    00001 /* $Id: station_map.h 17248 2009-08-21 20:21:05Z 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 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
    00027 static inline StationID GetStationIndex(TileIndex t)
    00028 {
    00029   assert(IsTileType(t, MP_STATION));
    00030   return (StationID)_m[t].m2;
    00031 }
    00032
    00033
    00034 enum {
    00035   GFX_RADAR_LARGE_FIRST             =  31,
    00036   GFX_RADAR_LARGE_LAST              =  42,
    00037   GFX_WINDSACK_FIRST                =  50,
    00038   GFX_WINDSACK_LAST                 =  53,
    00039
    00040   GFX_DOCK_BASE_WATER_PART          =  4,
    00041   GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET =  4,
    00042
    00043   GFX_RADAR_INTERNATIONAL_FIRST     =  66,
    00044   GFX_RADAR_INTERNATIONAL_LAST      =  77,
    00045   GFX_RADAR_METROPOLITAN_FIRST      =  78,
    00046   GFX_RADAR_METROPOLITAN_LAST       =  89,
    00047   GFX_RADAR_DISTRICTWE_FIRST        = 121,
    00048   GFX_RADAR_DISTRICTWE_LAST         = 132,
    00049   GFX_WINDSACK_INTERCON_FIRST       = 140,
    00050   GFX_WINDSACK_INTERCON_LAST        = 143,
    00051 };
    00052
    00059 static inline StationType GetStationType(TileIndex t)
    00060 {
    00061   assert(IsTileType(t, MP_STATION));
    00062   return (StationType)GB(_m[t].m6, 3, 3);
    00063 }
    00064
    00071 static inline RoadStopType GetRoadStopType(TileIndex t)
    00072 {
    00073   assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
    00074   return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
    00075 }
    00076
    00083 static inline StationGfx GetStationGfx(TileIndex t)
    00084 {
    00085   assert(IsTileType(t, MP_STATION));
    00086   return _m[t].m5;
    00087 }
    00088
    00095 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
    00096 {
    00097   assert(IsTileType(t, MP_STATION));
    00098   _m[t].m5 = gfx;
    00099 }
    00100
    00107 static inline uint8 GetStationAnimationFrame(TileIndex t)
    00108 {
    00109   assert(IsTileType(t, MP_STATION));
    00110   return _me[t].m7;
    00111 }
    00112
    00119 static inline void SetStationAnimationFrame(TileIndex t, uint8 frame)
    00120 {
    00121   assert(IsTileType(t, MP_STATION));
    00122   _me[t].m7 = frame;
    00123 }
    00124
    00131 static inline bool IsRailStation(TileIndex t)
    00132 {
    00133   return GetStationType(t) == STATION_RAIL;
    00134 }
    00135
    00141 static inline bool IsRailStationTile(TileIndex t)
    00142 {
    00143   return IsTileType(t, MP_STATION) && IsRailStation(t);
    00144 }
    00145
    00152 static inline bool IsRailWaypoint(TileIndex t)
    00153 {
    00154   return GetStationType(t) == STATION_WAYPOINT;
    00155 }
    00156
    00162 static inline bool IsRailWaypointTile(TileIndex t)
    00163 {
    00164   return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
    00165 }
    00166
    00174 static inline bool HasStationRail(TileIndex t)
    00175 {
    00176   return IsRailStation(t) || IsRailWaypoint(t);
    00177 }
    00178
    00185 static inline bool HasStationTileRail(TileIndex t)
    00186 {
    00187   return IsTileType(t, MP_STATION) && HasStationRail(t);
    00188 }
    00189
    00196 static inline bool IsAirport(TileIndex t)
    00197 {
    00198   return GetStationType(t) == STATION_AIRPORT;
    00199 }
    00200
    00201 bool IsHangar(TileIndex t);
    00202
    00209 static inline bool IsTruckStop(TileIndex t)
    00210 {
    00211   return GetStationType(t) == STATION_TRUCK;
    00212 }
    00213
    00220 static inline bool IsBusStop(TileIndex t)
    00221 {
    00222   return GetStationType(t) == STATION_BUS;
    00223 }
    00224
    00231 static inline bool IsRoadStop(TileIndex t)
    00232 {
    00233   assert(IsTileType(t, MP_STATION));
    00234   return IsTruckStop(t) || IsBusStop(t);
    00235 }
    00236
    00242 static inline bool IsRoadStopTile(TileIndex t)
    00243 {
    00244   return IsTileType(t, MP_STATION) && IsRoadStop(t);
    00245 }
    00246
    00252 static inline bool IsStandardRoadStopTile(TileIndex t)
    00253 {
    00254   return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
    00255 }
    00256
    00262 static inline bool IsDriveThroughStopTile(TileIndex t)
    00263 {
    00264   return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
    00265 }
    00266
    00273 static inline DiagDirection GetRoadStopDir(TileIndex t)
    00274 {
    00275   StationGfx gfx = GetStationGfx(t);
    00276   assert(IsRoadStopTile(t));
    00277   if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
    00278     return (DiagDirection)(gfx);
    00279   } else {
    00280     return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
    00281   }
    00282 }
    00283
    00284 static inline bool IsOilRig(TileIndex t)
    00285 {
    00286   return GetStationType(t) == STATION_OILRIG;
    00287 }
    00288
    00289 static inline bool IsDock(TileIndex t)
    00290 {
    00291   return GetStationType(t) == STATION_DOCK;
    00292 }
    00293
    00294 static inline bool IsDockTile(TileIndex t)
    00295 {
    00296   return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
    00297 }
    00298
    00299 static inline bool IsBuoy(TileIndex t)
    00300 {
    00301   return GetStationType(t) == STATION_BUOY;
    00302 }
    00303
    00304 static inline bool IsBuoyTile(TileIndex t)
    00305 {
    00306   return IsTileType(t, MP_STATION) && IsBuoy(t);
    00307 }
    00308
    00309 static inline bool IsHangarTile(TileIndex t)
    00310 {
    00311   return IsTileType(t, MP_STATION) && IsHangar(t);
    00312 }
    00313
    00314
    00315 static inline Axis GetRailStationAxis(TileIndex t)
    00316 {
    00317   assert(HasStationRail(t));
    00318   return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
    00319 }
    00320
    00321
    00322 static inline Track GetRailStationTrack(TileIndex t)
    00323 {
    00324   return AxisToTrack(GetRailStationAxis(t));
    00325 }
    00326
    00327 static inline TrackBits GetRailStationTrackBits(TileIndex t)
    00328 {
    00329   return AxisToTrackBits(GetRailStationAxis(t));
    00330 }
    00331
    00332 static inline bool IsCompatibleTrainStationTile(TileIndex t1, TileIndex t2)
    00333 {
    00334   assert(IsRailStationTile(t2));
    00335   return
    00336     IsRailStationTile(t1) &&
    00337     IsCompatibleRail(GetRailType(t1), GetRailType(t2)) &&
    00338     GetRailStationAxis(t1) == GetRailStationAxis(t2) &&
    00339     GetStationIndex(t1) == GetStationIndex(t2) &&
    00340     !IsStationTileBlocked(t1);
    00341 }
    00342
    00349 static inline bool HasStationReservation(TileIndex t)
    00350 {
    00351   assert(HasStationRail(t));
    00352   return HasBit(_m[t].m6, 2);
    00353 }
    00354
    00361 static inline void SetRailStationReservation(TileIndex t, bool b)
    00362 {
    00363   assert(HasStationRail(t));
    00364   SB(_m[t].m6, 2, 1, b ? 1 : 0);
    00365 }
    00366
    00373 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
    00374 {
    00375   return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
    00376 }
    00377
    00378
    00379 static inline DiagDirection GetDockDirection(TileIndex t)
    00380 {
    00381   StationGfx gfx = GetStationGfx(t);
    00382   assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
    00383   return (DiagDirection)(gfx);
    00384 }
    00385
    00386 static inline TileIndexDiffC GetDockOffset(TileIndex t)
    00387 {
    00388   static const TileIndexDiffC buoy_offset = {0, 0};
    00389   static const TileIndexDiffC oilrig_offset = {2, 0};
    00390   static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
    00391     {-2,  0},
    00392     { 0,  2},
    00393     { 2,  0},
    00394     { 0, -2},
    00395   };
    00396   assert(IsTileType(t, MP_STATION));
    00397
    00398   if (IsBuoy(t)) return buoy_offset;
    00399   if (IsOilRig(t)) return oilrig_offset;
    00400
    00401   assert(IsDock(t));
    00402
    00403   return dock_offset[GetDockDirection(t)];
    00404 }
    00405
    00406 static inline bool IsCustomStationSpecIndex(TileIndex t)
    00407 {
    00408   assert(IsTileType(t, MP_STATION));
    00409   return _m[t].m4 != 0;
    00410 }
    00411
    00412 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
    00413 {
    00414   assert(IsTileType(t, MP_STATION));
    00415   _m[t].m4 = specindex;
    00416 }
    00417
    00418 static inline uint GetCustomStationSpecIndex(TileIndex t)
    00419 {
    00420   assert(IsTileType(t, MP_STATION));
    00421   return _m[t].m4;
    00422 }
    00423
    00424 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
    00425 {
    00426   assert(IsTileType(t, MP_STATION));
    00427   SB(_m[t].m3, 4, 4, random_bits);
    00428 }
    00429
    00430 static inline byte GetStationTileRandomBits(TileIndex t)
    00431 {
    00432   assert(IsTileType(t, MP_STATION));
    00433   return GB(_m[t].m3, 4, 4);
    00434 }
    00435
    00436 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section)
    00437 {
    00438   SetTileType(t, MP_STATION);
    00439   SetTileOwner(t, o);
    00440   _m[t].m2 = sid;
    00441   _m[t].m3 = 0;
    00442   _m[t].m4 = 0;
    00443   _m[t].m5 = section;
    00444   SB(_m[t].m6, 2, 1, 0);
    00445   SB(_m[t].m6, 3, 3, st);
    00446   _me[t].m7 = 0;
    00447 }
    00448
    00449 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
    00450 {
    00451   MakeStation(t, o, sid, STATION_RAIL, section + a);
    00452   SetRailType(t, rt);
    00453   SetRailStationReservation(t, false);
    00454 }
    00455
    00456 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
    00457 {
    00458   MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
    00459   SetRailType(t, rt);
    00460   SetRailStationReservation(t, false);
    00461 }
    00462
    00463 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
    00464 {
    00465   MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
    00466   SetRoadTypes(t, rt);
    00467   SetRoadOwner(t, ROADTYPE_ROAD, o);
    00468   SetRoadOwner(t, ROADTYPE_TRAM, o);
    00469 }
    00470
    00471 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
    00472 {
    00473   MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
    00474   SetRoadTypes(t, rt);
    00475   SetRoadOwner(t, ROADTYPE_ROAD, road);
    00476   SetRoadOwner(t, ROADTYPE_TRAM, tram);
    00477 }
    00478
    00479 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section)
    00480 {
    00481   MakeStation(t, o, sid, STATION_AIRPORT, section);
    00482 }
    00483
    00484 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
    00485 {
    00486   /* Make the owner of the buoy tile the same as the current owner of the
    00487    * water tile. In this way, we can reset the owner of the water to its
    00488    * original state when the buoy gets removed. */
    00489   MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0);
    00490   SetWaterClass(t, wc);
    00491 }
    00492
    00493 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
    00494 {
    00495   MakeStation(t, o, sid, STATION_DOCK, d);
    00496   MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d));
    00497   SetWaterClass(t + TileOffsByDiagDir(d), wc);
    00498 }
    00499
    00500 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
    00501 {
    00502   MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0);
    00503   SetWaterClass(t, wc);
    00504 }
    00505
    00506 #endif /* STATION_MAP_H */