station_map.h

Go to the documentation of this file.
00001 /* $Id: station_map.h 18857 2010-01-18 12:32:50Z 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 
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_DOCK_BASE_WATER_PART          =  4,
00036   GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET =  4,
00037 };
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 uint8 GetStationAnimationFrame(TileIndex t)
00094 {
00095   assert(IsTileType(t, MP_STATION));
00096   return _me[t].m7;
00097 }
00098 
00105 static inline void SetStationAnimationFrame(TileIndex t, uint8 frame)
00106 {
00107   assert(IsTileType(t, MP_STATION));
00108   _me[t].m7 = frame;
00109 }
00110 
00117 static inline bool IsRailStation(TileIndex t)
00118 {
00119   return GetStationType(t) == STATION_RAIL;
00120 }
00121 
00127 static inline bool IsRailStationTile(TileIndex t)
00128 {
00129   return IsTileType(t, MP_STATION) && IsRailStation(t);
00130 }
00131 
00138 static inline bool IsRailWaypoint(TileIndex t)
00139 {
00140   return GetStationType(t) == STATION_WAYPOINT;
00141 }
00142 
00148 static inline bool IsRailWaypointTile(TileIndex t)
00149 {
00150   return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
00151 }
00152 
00160 static inline bool HasStationRail(TileIndex t)
00161 {
00162   return IsRailStation(t) || IsRailWaypoint(t);
00163 }
00164 
00171 static inline bool HasStationTileRail(TileIndex t)
00172 {
00173   return IsTileType(t, MP_STATION) && HasStationRail(t);
00174 }
00175 
00182 static inline bool IsAirport(TileIndex t)
00183 {
00184   return GetStationType(t) == STATION_AIRPORT;
00185 }
00186 
00192 static inline bool IsAirportTile(TileIndex t)
00193 {
00194   return IsTileType(t, MP_STATION) && IsAirport(t);
00195 }
00196 
00197 bool IsHangar(TileIndex t);
00198 
00205 static inline bool IsTruckStop(TileIndex t)
00206 {
00207   return GetStationType(t) == STATION_TRUCK;
00208 }
00209 
00216 static inline bool IsBusStop(TileIndex t)
00217 {
00218   return GetStationType(t) == STATION_BUS;
00219 }
00220 
00227 static inline bool IsRoadStop(TileIndex t)
00228 {
00229   assert(IsTileType(t, MP_STATION));
00230   return IsTruckStop(t) || IsBusStop(t);
00231 }
00232 
00238 static inline bool IsRoadStopTile(TileIndex t)
00239 {
00240   return IsTileType(t, MP_STATION) && IsRoadStop(t);
00241 }
00242 
00248 static inline bool IsStandardRoadStopTile(TileIndex t)
00249 {
00250   return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
00251 }
00252 
00258 static inline bool IsDriveThroughStopTile(TileIndex t)
00259 {
00260   return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
00261 }
00262 
00269 static inline DiagDirection GetRoadStopDir(TileIndex t)
00270 {
00271   StationGfx gfx = GetStationGfx(t);
00272   assert(IsRoadStopTile(t));
00273   if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
00274     return (DiagDirection)(gfx);
00275   } else {
00276     return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
00277   }
00278 }
00279 
00280 static inline bool IsOilRig(TileIndex t)
00281 {
00282   return GetStationType(t) == STATION_OILRIG;
00283 }
00284 
00285 static inline bool IsDock(TileIndex t)
00286 {
00287   return GetStationType(t) == STATION_DOCK;
00288 }
00289 
00290 static inline bool IsDockTile(TileIndex t)
00291 {
00292   return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
00293 }
00294 
00295 static inline bool IsBuoy(TileIndex t)
00296 {
00297   return GetStationType(t) == STATION_BUOY;
00298 }
00299 
00300 static inline bool IsBuoyTile(TileIndex t)
00301 {
00302   return IsTileType(t, MP_STATION) && IsBuoy(t);
00303 }
00304 
00305 static inline bool IsHangarTile(TileIndex t)
00306 {
00307   return IsTileType(t, MP_STATION) && IsHangar(t);
00308 }
00309 
00310 
00311 static inline Axis GetRailStationAxis(TileIndex t)
00312 {
00313   assert(HasStationRail(t));
00314   return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
00315 }
00316 
00317 
00318 static inline Track GetRailStationTrack(TileIndex t)
00319 {
00320   return AxisToTrack(GetRailStationAxis(t));
00321 }
00322 
00323 static inline TrackBits GetRailStationTrackBits(TileIndex t)
00324 {
00325   return AxisToTrackBits(GetRailStationAxis(t));
00326 }
00327 
00328 static inline bool IsCompatibleTrainStationTile(TileIndex t1, TileIndex t2)
00329 {
00330   assert(IsRailStationTile(t2));
00331   return
00332     IsRailStationTile(t1) &&
00333     IsCompatibleRail(GetRailType(t1), GetRailType(t2)) &&
00334     GetRailStationAxis(t1) == GetRailStationAxis(t2) &&
00335     GetStationIndex(t1) == GetStationIndex(t2) &&
00336     !IsStationTileBlocked(t1);
00337 }
00338 
00345 static inline bool HasStationReservation(TileIndex t)
00346 {
00347   assert(HasStationRail(t));
00348   return HasBit(_m[t].m6, 2);
00349 }
00350 
00357 static inline void SetRailStationReservation(TileIndex t, bool b)
00358 {
00359   assert(HasStationRail(t));
00360   SB(_m[t].m6, 2, 1, b ? 1 : 0);
00361 }
00362 
00369 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
00370 {
00371   return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
00372 }
00373 
00374 
00375 static inline DiagDirection GetDockDirection(TileIndex t)
00376 {
00377   StationGfx gfx = GetStationGfx(t);
00378   assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
00379   return (DiagDirection)(gfx);
00380 }
00381 
00382 static inline TileIndexDiffC GetDockOffset(TileIndex t)
00383 {
00384   static const TileIndexDiffC buoy_offset = {0, 0};
00385   static const TileIndexDiffC oilrig_offset = {2, 0};
00386   static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
00387     {-2,  0},
00388     { 0,  2},
00389     { 2,  0},
00390     { 0, -2},
00391   };
00392   assert(IsTileType(t, MP_STATION));
00393 
00394   if (IsBuoy(t)) return buoy_offset;
00395   if (IsOilRig(t)) return oilrig_offset;
00396 
00397   assert(IsDock(t));
00398 
00399   return dock_offset[GetDockDirection(t)];
00400 }
00401 
00402 static inline bool IsCustomStationSpecIndex(TileIndex t)
00403 {
00404   assert(IsTileType(t, MP_STATION));
00405   return _m[t].m4 != 0;
00406 }
00407 
00408 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
00409 {
00410   assert(IsTileType(t, MP_STATION));
00411   _m[t].m4 = specindex;
00412 }
00413 
00414 static inline uint GetCustomStationSpecIndex(TileIndex t)
00415 {
00416   assert(IsTileType(t, MP_STATION));
00417   return _m[t].m4;
00418 }
00419 
00420 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
00421 {
00422   assert(IsTileType(t, MP_STATION));
00423   SB(_m[t].m3, 4, 4, random_bits);
00424 }
00425 
00426 static inline byte GetStationTileRandomBits(TileIndex t)
00427 {
00428   assert(IsTileType(t, MP_STATION));
00429   return GB(_m[t].m3, 4, 4);
00430 }
00431 
00432 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section)
00433 {
00434   SetTileType(t, MP_STATION);
00435   SetTileOwner(t, o);
00436   _m[t].m2 = sid;
00437   _m[t].m3 = 0;
00438   _m[t].m4 = 0;
00439   _m[t].m5 = section;
00440   SB(_m[t].m6, 2, 1, 0);
00441   SB(_m[t].m6, 3, 3, st);
00442   _me[t].m7 = 0;
00443 }
00444 
00445 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
00446 {
00447   MakeStation(t, o, sid, STATION_RAIL, section + a);
00448   SetRailType(t, rt);
00449   SetRailStationReservation(t, false);
00450 }
00451 
00452 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
00453 {
00454   MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
00455   SetRailType(t, rt);
00456   SetRailStationReservation(t, false);
00457 }
00458 
00459 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
00460 {
00461   MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
00462   SetRoadTypes(t, rt);
00463   SetRoadOwner(t, ROADTYPE_ROAD, o);
00464   SetRoadOwner(t, ROADTYPE_TRAM, o);
00465 }
00466 
00467 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
00468 {
00469   MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
00470   SetRoadTypes(t, rt);
00471   SetRoadOwner(t, ROADTYPE_ROAD, road);
00472   SetRoadOwner(t, ROADTYPE_TRAM, tram);
00473 }
00474 
00475 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section)
00476 {
00477   MakeStation(t, o, sid, STATION_AIRPORT, section);
00478 }
00479 
00480 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
00481 {
00482   /* Make the owner of the buoy tile the same as the current owner of the
00483    * water tile. In this way, we can reset the owner of the water to its
00484    * original state when the buoy gets removed. */
00485   MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0);
00486   SetWaterClass(t, wc);
00487 }
00488 
00489 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
00490 {
00491   MakeStation(t, o, sid, STATION_DOCK, d);
00492   MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d));
00493   SetWaterClass(t + TileOffsByDiagDir(d), wc);
00494 }
00495 
00496 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
00497 {
00498   MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0);
00499   SetWaterClass(t, wc);
00500 }
00501 
00502 #endif /* STATION_MAP_H */

Generated on Wed Jan 20 23:38:40 2010 for OpenTTD by  doxygen 1.5.6