# station_map.h

Go to the documentation of this file.

    00001 /* $Id: station_map.h 18922 2010-01-26 23:25:28Z yexo $ */
    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 enum {
    00037   GFX_DOCK_BASE_WATER_PART          =  4,
    00038   GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET =  4,
    00039 };
    00040
    00047 static inline StationType GetStationType(TileIndex t)
    00048 {
    00049   assert(IsTileType(t, MP_STATION));
    00050   return (StationType)GB(_m[t].m6, 3, 3);
    00051 }
    00052
    00059 static inline RoadStopType GetRoadStopType(TileIndex t)
    00060 {
    00061   assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
    00062   return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
    00063 }
    00064
    00071 static inline StationGfx GetStationGfx(TileIndex t)
    00072 {
    00073   assert(IsTileType(t, MP_STATION));
    00074   return _m[t].m5;
    00075 }
    00076
    00083 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
    00084 {
    00085   assert(IsTileType(t, MP_STATION));
    00086   _m[t].m5 = gfx;
    00087 }
    00088
    00095 static inline uint8 GetStationAnimationFrame(TileIndex t)
    00096 {
    00097   assert(IsTileType(t, MP_STATION));
    00098   return _me[t].m7;
    00099 }
    00100
    00107 static inline void SetStationAnimationFrame(TileIndex t, uint8 frame)
    00108 {
    00109   assert(IsTileType(t, MP_STATION));
    00110   _me[t].m7 = frame;
    00111 }
    00112
    00119 static inline bool IsRailStation(TileIndex t)
    00120 {
    00121   return GetStationType(t) == STATION_RAIL;
    00122 }
    00123
    00129 static inline bool IsRailStationTile(TileIndex t)
    00130 {
    00131   return IsTileType(t, MP_STATION) && IsRailStation(t);
    00132 }
    00133
    00140 static inline bool IsRailWaypoint(TileIndex t)
    00141 {
    00142   return GetStationType(t) == STATION_WAYPOINT;
    00143 }
    00144
    00150 static inline bool IsRailWaypointTile(TileIndex t)
    00151 {
    00152   return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
    00153 }
    00154
    00162 static inline bool HasStationRail(TileIndex t)
    00163 {
    00164   return IsRailStation(t) || IsRailWaypoint(t);
    00165 }
    00166
    00173 static inline bool HasStationTileRail(TileIndex t)
    00174 {
    00175   return IsTileType(t, MP_STATION) && HasStationRail(t);
    00176 }
    00177
    00184 static inline bool IsAirport(TileIndex t)
    00185 {
    00186   return GetStationType(t) == STATION_AIRPORT;
    00187 }
    00188
    00194 static inline bool IsAirportTile(TileIndex t)
    00195 {
    00196   return IsTileType(t, MP_STATION) && IsAirport(t);
    00197 }
    00198
    00199 bool IsHangar(TileIndex t);
    00200
    00207 static inline bool IsTruckStop(TileIndex t)
    00208 {
    00209   return GetStationType(t) == STATION_TRUCK;
    00210 }
    00211
    00218 static inline bool IsBusStop(TileIndex t)
    00219 {
    00220   return GetStationType(t) == STATION_BUS;
    00221 }
    00222
    00229 static inline bool IsRoadStop(TileIndex t)
    00230 {
    00231   assert(IsTileType(t, MP_STATION));
    00232   return IsTruckStop(t) || IsBusStop(t);
    00233 }
    00234
    00240 static inline bool IsRoadStopTile(TileIndex t)
    00241 {
    00242   return IsTileType(t, MP_STATION) && IsRoadStop(t);
    00243 }
    00244
    00250 static inline bool IsStandardRoadStopTile(TileIndex t)
    00251 {
    00252   return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
    00253 }
    00254
    00260 static inline bool IsDriveThroughStopTile(TileIndex t)
    00261 {
    00262   return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
    00263 }
    00264
    00271 static inline DiagDirection GetRoadStopDir(TileIndex t)
    00272 {
    00273   StationGfx gfx = GetStationGfx(t);
    00274   assert(IsRoadStopTile(t));
    00275   if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
    00276     return (DiagDirection)(gfx);
    00277   } else {
    00278     return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
    00279   }
    00280 }
    00281
    00288 static inline bool IsOilRig(TileIndex t)
    00289 {
    00290   return GetStationType(t) == STATION_OILRIG;
    00291 }
    00292
    00299 static inline bool IsDock(TileIndex t)
    00300 {
    00301   return GetStationType(t) == STATION_DOCK;
    00302 }
    00303
    00309 static inline bool IsDockTile(TileIndex t)
    00310 {
    00311   return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
    00312 }
    00313
    00320 static inline bool IsBuoy(TileIndex t)
    00321 {
    00322   return GetStationType(t) == STATION_BUOY;
    00323 }
    00324
    00330 static inline bool IsBuoyTile(TileIndex t)
    00331 {
    00332   return IsTileType(t, MP_STATION) && IsBuoy(t);
    00333 }
    00334
    00340 static inline bool IsHangarTile(TileIndex t)
    00341 {
    00342   return IsTileType(t, MP_STATION) && IsHangar(t);
    00343 }
    00344
    00351 static inline Axis GetRailStationAxis(TileIndex t)
    00352 {
    00353   assert(HasStationRail(t));
    00354   return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
    00355 }
    00356
    00363 static inline Track GetRailStationTrack(TileIndex t)
    00364 {
    00365   return AxisToTrack(GetRailStationAxis(t));
    00366 }
    00367
    00374 static inline TrackBits GetRailStationTrackBits(TileIndex t)
    00375 {
    00376   return AxisToTrackBits(GetRailStationAxis(t));
    00377 }
    00378
    00392 static inline bool IsCompatibleTrainStationTile(TileIndex t1, TileIndex t2)
    00393 {
    00394   assert(IsRailStationTile(t2));
    00395   return
    00396     IsRailStationTile(t1) &&
    00397     IsCompatibleRail(GetRailType(t1), GetRailType(t2)) &&
    00398     GetRailStationAxis(t1) == GetRailStationAxis(t2) &&
    00399     GetStationIndex(t1) == GetStationIndex(t2) &&
    00400     !IsStationTileBlocked(t1);
    00401 }
    00402
    00409 static inline bool HasStationReservation(TileIndex t)
    00410 {
    00411   assert(HasStationRail(t));
    00412   return HasBit(_m[t].m6, 2);
    00413 }
    00414
    00421 static inline void SetRailStationReservation(TileIndex t, bool b)
    00422 {
    00423   assert(HasStationRail(t));
    00424   SB(_m[t].m6, 2, 1, b ? 1 : 0);
    00425 }
    00426
    00433 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
    00434 {
    00435   return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
    00436 }
    00437
    00445 static inline DiagDirection GetDockDirection(TileIndex t)
    00446 {
    00447   StationGfx gfx = GetStationGfx(t);
    00448   assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
    00449   return (DiagDirection)(gfx);
    00450 }
    00451
    00459 static inline TileIndexDiffC GetDockOffset(TileIndex t)
    00460 {
    00461   static const TileIndexDiffC buoy_offset = {0, 0};
    00462   static const TileIndexDiffC oilrig_offset = {2, 0};
    00463   static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
    00464     {-2,  0},
    00465     { 0,  2},
    00466     { 2,  0},
    00467     { 0, -2},
    00468   };
    00469   assert(IsTileType(t, MP_STATION));
    00470
    00471   if (IsBuoy(t)) return buoy_offset;
    00472   if (IsOilRig(t)) return oilrig_offset;
    00473
    00474   assert(IsDock(t));
    00475
    00476   return dock_offset[GetDockDirection(t)];
    00477 }
    00478
    00485 static inline bool IsCustomStationSpecIndex(TileIndex t)
    00486 {
    00487   assert(HasStationTileRail(t));
    00488   return _m[t].m4 != 0;
    00489 }
    00490
    00496 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
    00497 {
    00498   assert(HasStationTileRail(t));
    00499   _m[t].m4 = specindex;
    00500 }
    00501
    00508 static inline uint GetCustomStationSpecIndex(TileIndex t)
    00509 {
    00510   assert(HasStationTileRail(t));
    00511   return _m[t].m4;
    00512 }
    00513
    00519 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
    00520 {
    00521   assert(IsTileType(t, MP_STATION));
    00522   SB(_m[t].m3, 4, 4, random_bits);
    00523 }
    00524
    00531 static inline byte GetStationTileRandomBits(TileIndex t)
    00532 {
    00533   assert(IsTileType(t, MP_STATION));
    00534   return GB(_m[t].m3, 4, 4);
    00535 }
    00536
    00545 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section)
    00546 {
    00547   SetTileType(t, MP_STATION);
    00548   SetTileOwner(t, o);
    00549   _m[t].m2 = sid;
    00550   _m[t].m3 = 0;
    00551   _m[t].m4 = 0;
    00552   _m[t].m5 = section;
    00553   SB(_m[t].m6, 2, 1, 0);
    00554   SB(_m[t].m6, 3, 3, st);
    00555   _me[t].m7 = 0;
    00556 }
    00557
    00567 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
    00568 {
    00569   MakeStation(t, o, sid, STATION_RAIL, section + a);
    00570   SetRailType(t, rt);
    00571   SetRailStationReservation(t, false);
    00572 }
    00573
    00583 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
    00584 {
    00585   MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
    00586   SetRailType(t, rt);
    00587   SetRailStationReservation(t, false);
    00588 }
    00589
    00599 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
    00600 {
    00601   MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
    00602   SetRoadTypes(t, rt);
    00603   SetRoadOwner(t, ROADTYPE_ROAD, o);
    00604   SetRoadOwner(t, ROADTYPE_TRAM, o);
    00605 }
    00606
    00618 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
    00619 {
    00620   MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
    00621   SetRoadTypes(t, rt);
    00622   SetRoadOwner(t, ROADTYPE_ROAD, road);
    00623   SetRoadOwner(t, ROADTYPE_TRAM, tram);
    00624 }
    00625
    00633 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section)
    00634 {
    00635   MakeStation(t, o, sid, STATION_AIRPORT, section);
    00636 }
    00637
    00644 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
    00645 {
    00646   /* Make the owner of the buoy tile the same as the current owner of the
    00647    * water tile. In this way, we can reset the owner of the water to its
    00648    * original state when the buoy gets removed. */
    00649   MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0);
    00650   SetWaterClass(t, wc);
    00651 }
    00652
    00661 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
    00662 {
    00663   MakeStation(t, o, sid, STATION_DOCK, d);
    00664   MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d));
    00665   SetWaterClass(t + TileOffsByDiagDir(d), wc);
    00666 }
    00667
    00674 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
    00675 {
    00676   MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0);
    00677   SetWaterClass(t, wc);
    00678 }
    00679
    00680 #endif /* STATION_MAP_H */