1 /* $Id: station_map.h 25221 2013-05-06 13:05:04Z frosch $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file station_map.h Maps accessors for stations. */
17 #include "water_map.h"
18 #include "station_func.h"
22 typedef byte StationGfx
; ///< Index of station graphics. @see _station_display_datas
25 * Get StationID from a tile
26 * @param t Tile to query station ID from
27 * @pre IsTileType(t, MP_STATION)
28 * @return Station ID of the station at \a t
30 static inline StationID
GetStationIndex(TileIndex t
)
32 assert(IsTileType(t
, MP_STATION
));
33 return (StationID
)_m
[t
].m2
;
37 static const int GFX_DOCK_BASE_WATER_PART
= 4; ///< The offset for the water parts.
38 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
= 4; ///< The offset for the drive through parts.
41 * Get the station type of this tile
42 * @param t the tile to query
43 * @pre IsTileType(t, MP_STATION)
44 * @return the station type
46 static inline StationType
GetStationType(TileIndex t
)
48 assert(IsTileType(t
, MP_STATION
));
49 return (StationType
)GB(_me
[t
].m6
, 3, 3);
53 * Get the road stop type of this tile
54 * @param t the tile to query
55 * @pre GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS
56 * @return the road stop type
58 static inline RoadStopType
GetRoadStopType(TileIndex t
)
60 assert(GetStationType(t
) == STATION_TRUCK
|| GetStationType(t
) == STATION_BUS
);
61 return GetStationType(t
) == STATION_TRUCK
? ROADSTOP_TRUCK
: ROADSTOP_BUS
;
65 * Get the station graphics of this tile
66 * @param t the tile to query
67 * @pre IsTileType(t, MP_STATION)
68 * @return the station graphics
70 static inline StationGfx
GetStationGfx(TileIndex t
)
72 assert(IsTileType(t
, MP_STATION
));
77 * Set the station graphics of this tile
78 * @param t the tile to update
79 * @param gfx the new graphics
80 * @pre IsTileType(t, MP_STATION)
82 static inline void SetStationGfx(TileIndex t
, StationGfx gfx
)
84 assert(IsTileType(t
, MP_STATION
));
89 * Is this station tile a rail station?
90 * @param t the tile to get the information from
91 * @pre IsTileType(t, MP_STATION)
92 * @return true if and only if the tile is a rail station
94 static inline bool IsRailStation(TileIndex t
)
96 return GetStationType(t
) == STATION_RAIL
;
100 * Is this tile a station tile and a rail station?
101 * @param t the tile to get the information from
102 * @return true if and only if the tile is a rail station
104 static inline bool IsRailStationTile(TileIndex t
)
106 return IsTileType(t
, MP_STATION
) && IsRailStation(t
);
110 * Is this station tile a rail waypoint?
111 * @param t the tile to get the information from
112 * @pre IsTileType(t, MP_STATION)
113 * @return true if and only if the tile is a rail waypoint
115 static inline bool IsRailWaypoint(TileIndex t
)
117 return GetStationType(t
) == STATION_WAYPOINT
;
121 * Is this tile a station tile and a rail waypoint?
122 * @param t the tile to get the information from
123 * @return true if and only if the tile is a rail waypoint
125 static inline bool IsRailWaypointTile(TileIndex t
)
127 return IsTileType(t
, MP_STATION
) && IsRailWaypoint(t
);
131 * Has this station tile a rail? In other words, is this station
132 * tile a rail station or rail waypoint?
133 * @param t the tile to check
134 * @pre IsTileType(t, MP_STATION)
135 * @return true if and only if the tile has rail
137 static inline bool HasStationRail(TileIndex t
)
139 return IsRailStation(t
) || IsRailWaypoint(t
);
143 * Has this station tile a rail? In other words, is this station
144 * tile a rail station or rail waypoint?
145 * @param t the tile to check
146 * @return true if and only if the tile is a station tile and has rail
148 static inline bool HasStationTileRail(TileIndex t
)
150 return IsTileType(t
, MP_STATION
) && HasStationRail(t
);
154 * Is this station tile an airport?
155 * @param t the tile to get the information from
156 * @pre IsTileType(t, MP_STATION)
157 * @return true if and only if the tile is an airport
159 static inline bool IsAirport(TileIndex t
)
161 return GetStationType(t
) == STATION_AIRPORT
;
165 * Is this station tile an seaplane airport?
166 * @param t the tile to get the information from
167 * @pre IsTileType(t, MP_STATION)
168 * @return true if and only if the tile is an airport
170 static inline bool IsSeaplanePort(TileIndex t
)
172 return GetStationType(t
) == STATION_AIRPORT
&& GetWaterClass(t
) != WATER_CLASS_INVALID
;
176 * Is this tile a station tile and an airport tile?
177 * @param t the tile to get the information from
178 * @return true if and only if the tile is an airport
180 static inline bool IsAirportTile(TileIndex t
)
182 return IsTileType(t
, MP_STATION
) && IsAirport(t
);
185 bool IsHangar(TileIndex t
);
188 * Is the station at \a t a truck stop?
189 * @param t Tile to check
190 * @pre IsTileType(t, MP_STATION)
191 * @return \c true if station is a truck stop, \c false otherwise
193 static inline bool IsTruckStop(TileIndex t
)
195 return GetStationType(t
) == STATION_TRUCK
;
199 * Is the station at \a t a bus stop?
200 * @param t Tile to check
201 * @pre IsTileType(t, MP_STATION)
202 * @return \c true if station is a bus stop, \c false otherwise
204 static inline bool IsBusStop(TileIndex t
)
206 return GetStationType(t
) == STATION_BUS
;
210 * Is the station at \a t a road station?
211 * @param t Tile to check
212 * @pre IsTileType(t, MP_STATION)
213 * @return \c true if station at the tile is a bus top or a truck stop, \c false otherwise
215 static inline bool IsRoadStop(TileIndex t
)
217 assert(IsTileType(t
, MP_STATION
));
218 return IsTruckStop(t
) || IsBusStop(t
);
222 * Is tile \a t a road stop station?
223 * @param t Tile to check
224 * @return \c true if the tile is a station tile and a road stop
226 static inline bool IsRoadStopTile(TileIndex t
)
228 return IsTileType(t
, MP_STATION
) && IsRoadStop(t
);
232 * Is tile \a t a standard (non-drive through) road stop station?
233 * @param t Tile to check
234 * @return \c true if the tile is a station tile and a standard road stop
236 static inline bool IsStandardRoadStopTile(TileIndex t
)
238 return IsRoadStopTile(t
) && GetStationGfx(t
) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
;
242 * Is tile \a t a drive through road stop station?
243 * @param t Tile to check
244 * @return \c true if the tile is a station tile and a drive through road stop
246 static inline bool IsDriveThroughStopTile(TileIndex t
)
248 return IsRoadStopTile(t
) && GetStationGfx(t
) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
;
252 * Get the station graphics of this airport tile
253 * @param t the tile to query
255 * @return the station graphics
257 static inline StationGfx
GetAirportGfx(TileIndex t
)
259 assert(IsAirport(t
));
260 extern StationGfx
GetTranslatedAirportTileID(StationGfx gfx
);
261 return GetTranslatedAirportTileID(GetStationGfx(t
));
265 * Gets the direction the road stop entrance points towards.
266 * @param t the tile of the road stop
267 * @pre IsRoadStopTile(t)
268 * @return the direction of the entrance
270 static inline DiagDirection
GetRoadStopDir(TileIndex t
)
272 StationGfx gfx
= GetStationGfx(t
);
273 assert(IsRoadStopTile(t
));
274 if (gfx
< GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
) {
275 return (DiagDirection
)(gfx
);
277 return (DiagDirection
)(gfx
- GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
);
282 * Is tile \a t part of an oilrig?
283 * @param t Tile to check
284 * @pre IsTileType(t, MP_STATION)
285 * @return \c true if the tile is an oilrig tile
287 static inline bool IsOilRig(TileIndex t
)
289 return GetStationType(t
) == STATION_OILRIG
;
293 * Is tile \a t a dock tile?
294 * @param t Tile to check
295 * @pre IsTileType(t, MP_STATION)
296 * @return \c true if the tile is a dock
298 static inline bool IsDock(TileIndex t
)
300 return GetStationType(t
) == STATION_DOCK
;
304 * Is tile \a t a dock tile?
305 * @param t Tile to check
306 * @return \c true if the tile is a dock
308 static inline bool IsDockTile(TileIndex t
)
310 return IsTileType(t
, MP_STATION
) && GetStationType(t
) == STATION_DOCK
;
314 * Is tile \a t a buoy tile?
315 * @param t Tile to check
316 * @pre IsTileType(t, MP_STATION)
317 * @return \c true if the tile is a buoy
319 static inline bool IsBuoy(TileIndex t
)
321 return GetStationType(t
) == STATION_BUOY
;
325 * Is tile \a t a buoy tile?
326 * @param t Tile to check
327 * @return \c true if the tile is a buoy
329 static inline bool IsBuoyTile(TileIndex t
)
331 return IsTileType(t
, MP_STATION
) && IsBuoy(t
);
335 * Is tile \a t an hangar tile?
336 * @param t Tile to check
337 * @return \c true if the tile is an hangar
339 static inline bool IsHangarTile(TileIndex t
)
341 return IsTileType(t
, MP_STATION
) && IsHangar(t
);
345 * Get the rail direction of a rail station.
346 * @param t Tile to query
347 * @pre HasStationRail(t)
348 * @return The direction of the rails on tile \a t.
350 static inline Axis
GetRailStationAxis(TileIndex t
)
352 assert(HasStationRail(t
));
353 return HasBit(GetStationGfx(t
), 0) ? AXIS_Y
: AXIS_X
;
357 * Get the rail track of a rail station tile.
358 * @param t Tile to query
359 * @pre HasStationRail(t)
360 * @return The rail track of the rails on tile \a t.
362 static inline Track
GetRailStationTrack(TileIndex t
)
364 return AxisToTrack(GetRailStationAxis(t
));
368 * Get the trackbits of a rail station tile.
369 * @param t Tile to query
370 * @pre HasStationRail(t)
371 * @return The trackbits of the rails on tile \a t.
373 static inline TrackBits
GetRailStationTrackBits(TileIndex t
)
375 return AxisToTrackBits(GetRailStationAxis(t
));
379 * Check if a tile is a valid continuation to a railstation tile.
380 * The tile \a test_tile is a valid continuation to \a station_tile, if all of the following are true:
381 * \li \a test_tile is a rail station tile
382 * \li the railtype of \a test_tile is compatible with the railtype of \a station_tile
383 * \li the tracks on \a test_tile and \a station_tile are in the same direction
384 * \li both tiles belong to the same station
385 * \li \a test_tile is not blocked (@see IsStationTileBlocked)
386 * @param test_tile Tile to test
387 * @param station_tile Station tile to compare with
388 * @pre IsRailStationTile(station_tile)
389 * @return true if the two tiles are compatible
391 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile
, TileIndex station_tile
)
393 assert(IsRailStationTile(station_tile
));
394 return IsRailStationTile(test_tile
) && IsCompatibleRail(GetRailType(test_tile
), GetRailType(station_tile
)) &&
395 GetRailStationAxis(test_tile
) == GetRailStationAxis(station_tile
) &&
396 GetStationIndex(test_tile
) == GetStationIndex(station_tile
) &&
397 !IsStationTileBlocked(test_tile
);
401 * Get the reservation state of the rail station
402 * @pre HasStationRail(t)
403 * @param t the station tile
404 * @return reservation state
406 static inline bool HasStationReservation(TileIndex t
)
408 assert(HasStationRail(t
));
409 return HasBit(_me
[t
].m6
, 2);
413 * Set the reservation state of the rail station
414 * @pre HasStationRail(t)
415 * @param t the station tile
416 * @param b the reservation state
418 static inline void SetRailStationReservation(TileIndex t
, bool b
)
420 assert(HasStationRail(t
));
421 SB(_me
[t
].m6
, 2, 1, b
? 1 : 0);
425 * Get the reserved track bits for a waypoint
426 * @pre HasStationRail(t)
428 * @return reserved track bits
430 static inline TrackBits
GetStationReservationTrackBits(TileIndex t
)
432 return HasStationReservation(t
) ? GetRailStationTrackBits(t
) : TRACK_BIT_NONE
;
436 * Get the direction of a dock.
437 * @param t Tile to query
439 * @pre \a t is the land part of the dock
440 * @return The direction of the dock on tile \a t.
442 static inline DiagDirection
GetDockDirection(TileIndex t
)
444 StationGfx gfx
= GetStationGfx(t
);
445 assert(IsDock(t
) && gfx
< GFX_DOCK_BASE_WATER_PART
);
446 return (DiagDirection
)(gfx
);
450 * Get the tileoffset from this tile a ship should target to get to this dock.
451 * @param t Tile to query
452 * @pre IsTileType(t, MP_STATION)
453 * @pre IsBuoy(t) || IsOilRig(t) || IsDock(t)
454 * @return The offset from this tile that should be used as destination for ships.
456 static inline TileIndexDiffC
GetDockOffset(TileIndex t
)
458 static const TileIndexDiffC buoy_offset
= {0, 0};
459 static const TileIndexDiffC oilrig_offset
= {2, 0};
460 static const TileIndexDiffC dock_offset
[DIAGDIR_END
] = {
466 assert(IsTileType(t
, MP_STATION
));
468 if (IsBuoy(t
)) return buoy_offset
;
469 if (IsOilRig(t
)) return oilrig_offset
;
473 return dock_offset
[GetDockDirection(t
)];
477 * Is there a custom rail station spec on this tile?
478 * @param t Tile to query
479 * @pre HasStationTileRail(t)
480 * @return True if this station is part of a newgrf station.
482 static inline bool IsCustomStationSpecIndex(TileIndex t
)
484 assert(HasStationTileRail(t
));
485 return _m
[t
].m4
!= 0;
489 * Set the custom station spec for this tile.
490 * @param t Tile to set the stationspec of.
491 * @param specindex The new spec.
492 * @pre HasStationTileRail(t)
494 static inline void SetCustomStationSpecIndex(TileIndex t
, byte specindex
)
496 assert(HasStationTileRail(t
));
497 _m
[t
].m4
= specindex
;
501 * Get the custom station spec for this tile.
502 * @param t Tile to query
503 * @pre HasStationTileRail(t)
504 * @return The custom station spec of this tile.
506 static inline uint
GetCustomStationSpecIndex(TileIndex t
)
508 assert(HasStationTileRail(t
));
513 * Set the random bits for a station tile.
514 * @param t Tile to set random bits for.
515 * @param random_bits The random bits.
516 * @pre IsTileType(t, MP_STATION)
518 static inline void SetStationTileRandomBits(TileIndex t
, byte random_bits
)
520 assert(IsTileType(t
, MP_STATION
));
521 SB(_m
[t
].m3
, 4, 4, random_bits
);
525 * Get the random bits of a station tile.
526 * @param t Tile to query
527 * @pre IsTileType(t, MP_STATION)
528 * @return The random bits for this station tile.
530 static inline byte
GetStationTileRandomBits(TileIndex t
)
532 assert(IsTileType(t
, MP_STATION
));
533 return GB(_m
[t
].m3
, 4, 4);
537 * Make the given tile a station tile.
538 * @param t the tile to make a station tile
539 * @param o the owner of the station
540 * @param sid the station to which this tile belongs
541 * @param st the type this station tile
542 * @param section the StationGfx to be used for this tile
543 * @param wc The water class of the station
545 static inline void MakeStation(TileIndex t
, Owner o
, StationID sid
, StationType st
, byte section
, WaterClass wc
= WATER_CLASS_INVALID
)
547 SetTileType(t
, MP_STATION
);
549 SetWaterClass(t
, wc
);
554 SB(_me
[t
].m6
, 2, 1, 0);
555 SB(_me
[t
].m6
, 3, 3, st
);
560 * Make the given tile a rail station tile.
561 * @param t the tile to make a rail station tile
562 * @param o the owner of the station
563 * @param sid the station to which this tile belongs
564 * @param a the axis of this tile
565 * @param section the StationGfx to be used for this tile
566 * @param rt the railtype of this tile
568 static inline void MakeRailStation(TileIndex t
, Owner o
, StationID sid
, Axis a
, byte section
, RailType rt
)
570 MakeStation(t
, o
, sid
, STATION_RAIL
, section
+ a
);
572 SetRailStationReservation(t
, false);
576 * Make the given tile a rail waypoint tile.
577 * @param t the tile to make a rail waypoint
578 * @param o the owner of the waypoint
579 * @param sid the waypoint to which this tile belongs
580 * @param a the axis of this tile
581 * @param section the StationGfx to be used for this tile
582 * @param rt the railtype of this tile
584 static inline void MakeRailWaypoint(TileIndex t
, Owner o
, StationID sid
, Axis a
, byte section
, RailType rt
)
586 MakeStation(t
, o
, sid
, STATION_WAYPOINT
, section
+ a
);
588 SetRailStationReservation(t
, false);
592 * Make the given tile a roadstop tile.
593 * @param t the tile to make a roadstop
594 * @param o the owner of the roadstop
595 * @param sid the station to which this tile belongs
596 * @param rst the type of roadstop to make this tile
597 * @param rtids the roadtypes on this tile
598 * @param d the direction of the roadstop
600 static inline void MakeRoadStop(TileIndex t
, Owner o
, StationID sid
, RoadStopType rst
, RoadTypeIdentifiers rtids
, DiagDirection d
)
602 MakeStation(t
, o
, sid
, (rst
== ROADSTOP_BUS
? STATION_BUS
: STATION_TRUCK
), d
);
603 SetRoadTypes(t
, rtids
);
604 SetRoadOwner(t
, ROADTYPE_ROAD
, o
);
605 SetRoadOwner(t
, ROADTYPE_TRAM
, o
);
609 * Make the given tile a drivethrough roadstop tile.
610 * @param t the tile to make a roadstop
611 * @param station the owner of the roadstop
612 * @param road the owner of the road
613 * @param tram the owner of the tram
614 * @param sid the station to which this tile belongs
615 * @param rst the type of roadstop to make this tile
616 * @param rtids the roadtypes on this tile
617 * @param a the direction of the roadstop
619 static inline void MakeDriveThroughRoadStop(TileIndex t
, Owner station
, Owner road
, Owner tram
, StationID sid
, RoadStopType rst
, RoadTypeIdentifiers rtids
, Axis a
)
621 MakeStation(t
, station
, sid
, (rst
== ROADSTOP_BUS
? STATION_BUS
: STATION_TRUCK
), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
+ a
);
622 SetRoadTypes(t
, rtids
);
623 SetRoadOwner(t
, ROADTYPE_ROAD
, road
);
624 SetRoadOwner(t
, ROADTYPE_TRAM
, tram
);
628 * Make the given tile an airport tile.
629 * @param t the tile to make a airport
630 * @param o the owner of the airport
631 * @param sid the station to which this tile belongs
632 * @param section the StationGfx to be used for this tile
633 * @param wc the type of water on this tile
635 static inline void MakeAirport(TileIndex t
, Owner o
, StationID sid
, byte section
, WaterClass wc
)
637 MakeStation(t
, o
, sid
, STATION_AIRPORT
, section
, wc
);
641 * Make the given tile a buoy tile.
642 * @param t the tile to make a buoy
643 * @param sid the station to which this tile belongs
644 * @param wc the type of water on this tile
646 static inline void MakeBuoy(TileIndex t
, StationID sid
, WaterClass wc
)
648 /* Make the owner of the buoy tile the same as the current owner of the
649 * water tile. In this way, we can reset the owner of the water to its
650 * original state when the buoy gets removed. */
651 MakeStation(t
, GetTileOwner(t
), sid
, STATION_BUOY
, 0, wc
);
655 * Make the given tile a dock tile.
656 * @param t the tile to make a dock
657 * @param o the owner of the dock
658 * @param sid the station to which this tile belongs
659 * @param d the direction of the dock
660 * @param wc the type of water on this tile
662 static inline void MakeDock(TileIndex t
, Owner o
, StationID sid
, DiagDirection d
, WaterClass wc
)
664 MakeStation(t
, o
, sid
, STATION_DOCK
, d
);
665 MakeStation(t
+ TileOffsByDiagDir(d
), o
, sid
, STATION_DOCK
, GFX_DOCK_BASE_WATER_PART
+ DiagDirToAxis(d
), wc
);
669 * Make the given tile an oilrig tile.
670 * @param t the tile to make an oilrig
671 * @param sid the station to which this tile belongs
672 * @param wc the type of water on this tile
674 static inline void MakeOilrig(TileIndex t
, StationID sid
, WaterClass wc
)
676 MakeStation(t
, OWNER_NONE
, sid
, STATION_OILRIG
, 0, wc
);
679 #endif /* STATION_MAP_H */