(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / station_map.h
blob7ca9bd7204afa826e0652df2d9342d4a4a19a1f4
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file station_map.h Maps accessors for stations. */
12 #ifndef STATION_MAP_H
13 #define STATION_MAP_H
15 #include "rail_map.h"
16 #include "road_map.h"
17 #include "water_map.h"
18 #include "station_func.h"
19 #include "rail.h"
21 typedef byte StationGfx; ///< Index of station graphics. @see _station_display_datas
23 /**
24 * Get StationID from a tile
25 * @param t Tile to query station ID from
26 * @pre IsTileType(t, MP_STATION)
27 * @return Station ID of the station at \a t
29 static inline StationID GetStationIndex(TileIndex t)
31 assert(IsTileType(t, MP_STATION));
32 return (StationID)_m[t].m2;
36 static const int GFX_DOCK_BASE_WATER_PART = 4; ///< The offset for the water parts.
37 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET = 4; ///< The offset for the drive through parts.
39 /**
40 * Get the station type of this tile
41 * @param t the tile to query
42 * @pre IsTileType(t, MP_STATION)
43 * @return the station type
45 static inline StationType GetStationType(TileIndex t)
47 assert(IsTileType(t, MP_STATION));
48 return (StationType)GB(_me[t].m6, 3, 3);
51 /**
52 * Get the road stop type of this tile
53 * @param t the tile to query
54 * @pre GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS
55 * @return the road stop type
57 static inline RoadStopType GetRoadStopType(TileIndex t)
59 assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
60 return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
63 /**
64 * Get the station graphics of this tile
65 * @param t the tile to query
66 * @pre IsTileType(t, MP_STATION)
67 * @return the station graphics
69 static inline StationGfx GetStationGfx(TileIndex t)
71 assert(IsTileType(t, MP_STATION));
72 return _m[t].m5;
75 /**
76 * Set the station graphics of this tile
77 * @param t the tile to update
78 * @param gfx the new graphics
79 * @pre IsTileType(t, MP_STATION)
81 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
83 assert(IsTileType(t, MP_STATION));
84 _m[t].m5 = gfx;
87 /**
88 * Is this station tile a rail station?
89 * @param t the tile to get the information from
90 * @pre IsTileType(t, MP_STATION)
91 * @return true if and only if the tile is a rail station
93 static inline bool IsRailStation(TileIndex t)
95 return GetStationType(t) == STATION_RAIL;
98 /**
99 * Is this tile a station tile and a rail station?
100 * @param t the tile to get the information from
101 * @return true if and only if the tile is a rail station
103 static inline bool IsRailStationTile(TileIndex t)
105 return IsTileType(t, MP_STATION) && IsRailStation(t);
109 * Is this station tile a rail waypoint?
110 * @param t the tile to get the information from
111 * @pre IsTileType(t, MP_STATION)
112 * @return true if and only if the tile is a rail waypoint
114 static inline bool IsRailWaypoint(TileIndex t)
116 return GetStationType(t) == STATION_WAYPOINT;
120 * Is this tile a station tile and a rail waypoint?
121 * @param t the tile to get the information from
122 * @return true if and only if the tile is a rail waypoint
124 static inline bool IsRailWaypointTile(TileIndex t)
126 return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
130 * Has this station tile a rail? In other words, is this station
131 * tile a rail station or rail waypoint?
132 * @param t the tile to check
133 * @pre IsTileType(t, MP_STATION)
134 * @return true if and only if the tile has rail
136 static inline bool HasStationRail(TileIndex t)
138 return IsRailStation(t) || IsRailWaypoint(t);
142 * Has this station tile a rail? In other words, is this station
143 * tile a rail station or rail waypoint?
144 * @param t the tile to check
145 * @return true if and only if the tile is a station tile and has rail
147 static inline bool HasStationTileRail(TileIndex t)
149 return IsTileType(t, MP_STATION) && HasStationRail(t);
153 * Is this station tile an airport?
154 * @param t the tile to get the information from
155 * @pre IsTileType(t, MP_STATION)
156 * @return true if and only if the tile is an airport
158 static inline bool IsAirport(TileIndex t)
160 return GetStationType(t) == STATION_AIRPORT;
164 * Is this tile a station tile and an airport tile?
165 * @param t the tile to get the information from
166 * @return true if and only if the tile is an airport
168 static inline bool IsAirportTile(TileIndex t)
170 return IsTileType(t, MP_STATION) && IsAirport(t);
173 bool IsHangar(TileIndex t);
176 * Is the station at \a t a truck stop?
177 * @param t Tile to check
178 * @pre IsTileType(t, MP_STATION)
179 * @return \c true if station is a truck stop, \c false otherwise
181 static inline bool IsTruckStop(TileIndex t)
183 return GetStationType(t) == STATION_TRUCK;
187 * Is the station at \a t a bus stop?
188 * @param t Tile to check
189 * @pre IsTileType(t, MP_STATION)
190 * @return \c true if station is a bus stop, \c false otherwise
192 static inline bool IsBusStop(TileIndex t)
194 return GetStationType(t) == STATION_BUS;
198 * Is the station at \a t a road station?
199 * @param t Tile to check
200 * @pre IsTileType(t, MP_STATION)
201 * @return \c true if station at the tile is a bus top or a truck stop, \c false otherwise
203 static inline bool IsRoadStop(TileIndex t)
205 assert(IsTileType(t, MP_STATION));
206 return IsTruckStop(t) || IsBusStop(t);
210 * Is tile \a t a road stop station?
211 * @param t Tile to check
212 * @return \c true if the tile is a station tile and a road stop
214 static inline bool IsRoadStopTile(TileIndex t)
216 return IsTileType(t, MP_STATION) && IsRoadStop(t);
220 * Is tile \a t a standard (non-drive through) road stop station?
221 * @param t Tile to check
222 * @return \c true if the tile is a station tile and a standard road stop
224 static inline bool IsStandardRoadStopTile(TileIndex t)
226 return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
230 * Is tile \a t a drive through road stop station?
231 * @param t Tile to check
232 * @return \c true if the tile is a station tile and a drive through road stop
234 static inline bool IsDriveThroughStopTile(TileIndex t)
236 return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
240 * Get the station graphics of this airport tile
241 * @param t the tile to query
242 * @pre IsAirport(t)
243 * @return the station graphics
245 static inline StationGfx GetAirportGfx(TileIndex t)
247 assert(IsAirport(t));
248 extern StationGfx GetTranslatedAirportTileID(StationGfx gfx);
249 return GetTranslatedAirportTileID(GetStationGfx(t));
253 * Gets the direction the road stop entrance points towards.
254 * @param t the tile of the road stop
255 * @pre IsRoadStopTile(t)
256 * @return the direction of the entrance
258 static inline DiagDirection GetRoadStopDir(TileIndex t)
260 StationGfx gfx = GetStationGfx(t);
261 assert(IsRoadStopTile(t));
262 if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
263 return (DiagDirection)(gfx);
264 } else {
265 return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
270 * Is tile \a t part of an oilrig?
271 * @param t Tile to check
272 * @pre IsTileType(t, MP_STATION)
273 * @return \c true if the tile is an oilrig tile
275 static inline bool IsOilRig(TileIndex t)
277 return GetStationType(t) == STATION_OILRIG;
281 * Is tile \a t a dock tile?
282 * @param t Tile to check
283 * @pre IsTileType(t, MP_STATION)
284 * @return \c true if the tile is a dock
286 static inline bool IsDock(TileIndex t)
288 return GetStationType(t) == STATION_DOCK;
292 * Is tile \a t a dock tile?
293 * @param t Tile to check
294 * @return \c true if the tile is a dock
296 static inline bool IsDockTile(TileIndex t)
298 return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
302 * Is tile \a t a buoy tile?
303 * @param t Tile to check
304 * @pre IsTileType(t, MP_STATION)
305 * @return \c true if the tile is a buoy
307 static inline bool IsBuoy(TileIndex t)
309 return GetStationType(t) == STATION_BUOY;
313 * Is tile \a t a buoy tile?
314 * @param t Tile to check
315 * @return \c true if the tile is a buoy
317 static inline bool IsBuoyTile(TileIndex t)
319 return IsTileType(t, MP_STATION) && IsBuoy(t);
323 * Is tile \a t an hangar tile?
324 * @param t Tile to check
325 * @return \c true if the tile is an hangar
327 static inline bool IsHangarTile(TileIndex t)
329 return IsTileType(t, MP_STATION) && IsHangar(t);
333 * Get the rail direction of a rail station.
334 * @param t Tile to query
335 * @pre HasStationRail(t)
336 * @return The direction of the rails on tile \a t.
338 static inline Axis GetRailStationAxis(TileIndex t)
340 assert(HasStationRail(t));
341 return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
345 * Get the rail track of a rail station tile.
346 * @param t Tile to query
347 * @pre HasStationRail(t)
348 * @return The rail track of the rails on tile \a t.
350 static inline Track GetRailStationTrack(TileIndex t)
352 return AxisToTrack(GetRailStationAxis(t));
356 * Get the trackbits of a rail station tile.
357 * @param t Tile to query
358 * @pre HasStationRail(t)
359 * @return The trackbits of the rails on tile \a t.
361 static inline TrackBits GetRailStationTrackBits(TileIndex t)
363 return AxisToTrackBits(GetRailStationAxis(t));
367 * Check if a tile is a valid continuation to a railstation tile.
368 * The tile \a test_tile is a valid continuation to \a station_tile, if all of the following are true:
369 * \li \a test_tile is a rail station tile
370 * \li the railtype of \a test_tile is compatible with the railtype of \a station_tile
371 * \li the tracks on \a test_tile and \a station_tile are in the same direction
372 * \li both tiles belong to the same station
373 * \li \a test_tile is not blocked (@see IsStationTileBlocked)
374 * @param test_tile Tile to test
375 * @param station_tile Station tile to compare with
376 * @pre IsRailStationTile(station_tile)
377 * @return true if the two tiles are compatible
379 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
381 assert(IsRailStationTile(station_tile));
382 return IsRailStationTile(test_tile) && IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
383 GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
384 GetStationIndex(test_tile) == GetStationIndex(station_tile) &&
385 !IsStationTileBlocked(test_tile);
389 * Get the reservation state of the rail station
390 * @pre HasStationRail(t)
391 * @param t the station tile
392 * @return reservation state
394 static inline bool HasStationReservation(TileIndex t)
396 assert(HasStationRail(t));
397 return HasBit(_me[t].m6, 2);
401 * Set the reservation state of the rail station
402 * @pre HasStationRail(t)
403 * @param t the station tile
404 * @param b the reservation state
406 static inline void SetRailStationReservation(TileIndex t, bool b)
408 assert(HasStationRail(t));
409 SB(_me[t].m6, 2, 1, b ? 1 : 0);
413 * Get the reserved track bits for a waypoint
414 * @pre HasStationRail(t)
415 * @param t the tile
416 * @return reserved track bits
418 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
420 return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
424 * Get the direction of a dock.
425 * @param t Tile to query
426 * @pre IsDock(t)
427 * @pre \a t is the land part of the dock
428 * @return The direction of the dock on tile \a t.
430 static inline DiagDirection GetDockDirection(TileIndex t)
432 StationGfx gfx = GetStationGfx(t);
433 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
434 return (DiagDirection)(gfx);
438 * Get the tileoffset from this tile a ship should target to get to this dock.
439 * @param t Tile to query
440 * @pre IsTileType(t, MP_STATION)
441 * @pre IsBuoy(t) || IsOilRig(t) || IsDock(t)
442 * @return The offset from this tile that should be used as destination for ships.
444 static inline TileIndexDiffC GetDockOffset(TileIndex t)
446 static const TileIndexDiffC buoy_offset = {0, 0};
447 static const TileIndexDiffC oilrig_offset = {2, 0};
448 static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
449 {-2, 0},
450 { 0, 2},
451 { 2, 0},
452 { 0, -2},
454 assert(IsTileType(t, MP_STATION));
456 if (IsBuoy(t)) return buoy_offset;
457 if (IsOilRig(t)) return oilrig_offset;
459 assert(IsDock(t));
461 return dock_offset[GetDockDirection(t)];
465 * Is there a custom rail station spec on this tile?
466 * @param t Tile to query
467 * @pre HasStationTileRail(t)
468 * @return True if this station is part of a newgrf station.
470 static inline bool IsCustomStationSpecIndex(TileIndex t)
472 assert(HasStationTileRail(t));
473 return _m[t].m4 != 0;
477 * Set the custom station spec for this tile.
478 * @param t Tile to set the stationspec of.
479 * @param specindex The new spec.
480 * @pre HasStationTileRail(t)
482 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
484 assert(HasStationTileRail(t));
485 _m[t].m4 = specindex;
489 * Get the custom station spec for this tile.
490 * @param t Tile to query
491 * @pre HasStationTileRail(t)
492 * @return The custom station spec of this tile.
494 static inline uint GetCustomStationSpecIndex(TileIndex t)
496 assert(HasStationTileRail(t));
497 return _m[t].m4;
501 * Set the random bits for a station tile.
502 * @param t Tile to set random bits for.
503 * @param random_bits The random bits.
504 * @pre IsTileType(t, MP_STATION)
506 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
508 assert(IsTileType(t, MP_STATION));
509 SB(_m[t].m3, 4, 4, random_bits);
513 * Get the random bits of a station tile.
514 * @param t Tile to query
515 * @pre IsTileType(t, MP_STATION)
516 * @return The random bits for this station tile.
518 static inline byte GetStationTileRandomBits(TileIndex t)
520 assert(IsTileType(t, MP_STATION));
521 return GB(_m[t].m3, 4, 4);
525 * Make the given tile a station tile.
526 * @param t the tile to make a station tile
527 * @param o the owner of the station
528 * @param sid the station to which this tile belongs
529 * @param st the type this station tile
530 * @param section the StationGfx to be used for this tile
531 * @param wc The water class of the station
533 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
535 SetTileType(t, MP_STATION);
536 SetTileOwner(t, o);
537 SetWaterClass(t, wc);
538 _m[t].m2 = sid;
539 _m[t].m3 = 0;
540 _m[t].m4 = 0;
541 _m[t].m5 = section;
542 SB(_me[t].m6, 2, 1, 0);
543 SB(_me[t].m6, 3, 3, st);
544 _me[t].m7 = 0;
548 * Make the given tile a rail station tile.
549 * @param t the tile to make a rail station tile
550 * @param o the owner of the station
551 * @param sid the station to which this tile belongs
552 * @param a the axis of this tile
553 * @param section the StationGfx to be used for this tile
554 * @param rt the railtype of this tile
556 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
558 MakeStation(t, o, sid, STATION_RAIL, section + a);
559 SetRailType(t, rt);
560 SetRailStationReservation(t, false);
564 * Make the given tile a rail waypoint tile.
565 * @param t the tile to make a rail waypoint
566 * @param o the owner of the waypoint
567 * @param sid the waypoint to which this tile belongs
568 * @param a the axis of this tile
569 * @param section the StationGfx to be used for this tile
570 * @param rt the railtype of this tile
572 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
574 MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
575 SetRailType(t, rt);
576 SetRailStationReservation(t, false);
580 * Make the given tile a roadstop tile.
581 * @param t the tile to make a roadstop
582 * @param o the owner of the roadstop
583 * @param sid the station to which this tile belongs
584 * @param rst the type of roadstop to make this tile
585 * @param rt the roadtypes on this tile
586 * @param d the direction of the roadstop
588 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
590 MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
591 SetRoadTypes(t, rt);
592 SetRoadOwner(t, ROADTYPE_ROAD, o);
593 SetRoadOwner(t, ROADTYPE_TRAM, o);
597 * Make the given tile a drivethrough roadstop tile.
598 * @param t the tile to make a roadstop
599 * @param station the owner of the roadstop
600 * @param road the owner of the road
601 * @param tram the owner of the tram
602 * @param sid the station to which this tile belongs
603 * @param rst the type of roadstop to make this tile
604 * @param rt the roadtypes on this tile
605 * @param a the direction of the roadstop
607 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
609 MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
610 SetRoadTypes(t, rt);
611 SetRoadOwner(t, ROADTYPE_ROAD, road);
612 SetRoadOwner(t, ROADTYPE_TRAM, tram);
616 * Make the given tile an airport tile.
617 * @param t the tile to make a airport
618 * @param o the owner of the airport
619 * @param sid the station to which this tile belongs
620 * @param section the StationGfx to be used for this tile
621 * @param wc the type of water on this tile
623 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
625 MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
629 * Make the given tile a buoy tile.
630 * @param t the tile to make a buoy
631 * @param sid the station to which this tile belongs
632 * @param wc the type of water on this tile
634 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
636 /* Make the owner of the buoy tile the same as the current owner of the
637 * water tile. In this way, we can reset the owner of the water to its
638 * original state when the buoy gets removed. */
639 MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
643 * Make the given tile a dock tile.
644 * @param t the tile to make a dock
645 * @param o the owner of the dock
646 * @param sid the station to which this tile belongs
647 * @param d the direction of the dock
648 * @param wc the type of water on this tile
650 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
652 MakeStation(t, o, sid, STATION_DOCK, d);
653 MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
657 * Make the given tile an oilrig tile.
658 * @param t the tile to make an oilrig
659 * @param sid the station to which this tile belongs
660 * @param wc the type of water on this tile
662 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
664 MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
667 #endif /* STATION_MAP_H */