Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / station_map.h
blobb255da7a051b5c9bf7b4ad2816a1af182a16a81e
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file station_map.h Maps accessors for stations. */
10 #ifndef STATION_MAP_H
11 #define STATION_MAP_H
13 #include "rail_map.h"
14 #include "road_map.h"
15 #include "water_map.h"
16 #include "station_func.h"
17 #include "rail.h"
18 #include "road.h"
20 typedef byte StationGfx; ///< Index of station graphics. @see _station_display_datas
22 /**
23 * Get StationID from a tile
24 * @param t Tile to query station ID from
25 * @pre IsTileType(t, MP_STATION)
26 * @return Station ID of the station at \a t
28 inline StationID GetStationIndex(Tile t)
30 assert(IsTileType(t, MP_STATION));
31 return (StationID)t.m2();
35 static const int GFX_DOCK_BASE_WATER_PART = 4; ///< The offset for the water parts.
36 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET = 4; ///< The offset for the drive through parts.
38 /**
39 * Get the station type of this tile
40 * @param t the tile to query
41 * @pre IsTileType(t, MP_STATION)
42 * @return the station type
44 inline StationType GetStationType(Tile t)
46 assert(IsTileType(t, MP_STATION));
47 return (StationType)GB(t.m6(), 3, 3);
50 /**
51 * Get the road stop type of this tile
52 * @param t the tile to query
53 * @pre GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS
54 * @return the road stop type
56 inline RoadStopType GetRoadStopType(Tile t)
58 assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
59 return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
62 /**
63 * Get the station graphics of this tile
64 * @param t the tile to query
65 * @pre IsTileType(t, MP_STATION)
66 * @return the station graphics
68 inline StationGfx GetStationGfx(Tile t)
70 assert(IsTileType(t, MP_STATION));
71 return t.m5();
74 /**
75 * Set the station graphics of this tile
76 * @param t the tile to update
77 * @param gfx the new graphics
78 * @pre IsTileType(t, MP_STATION)
80 inline void SetStationGfx(Tile t, StationGfx gfx)
82 assert(IsTileType(t, MP_STATION));
83 t.m5() = gfx;
86 /**
87 * Is this station tile a rail station?
88 * @param t the tile to get the information from
89 * @pre IsTileType(t, MP_STATION)
90 * @return true if and only if the tile is a rail station
92 inline bool IsRailStation(Tile t)
94 return GetStationType(t) == STATION_RAIL;
97 /**
98 * Is this tile a station tile and a rail station?
99 * @param t the tile to get the information from
100 * @return true if and only if the tile is a rail station
102 inline bool IsRailStationTile(Tile t)
104 return IsTileType(t, MP_STATION) && IsRailStation(t);
108 * Is this station tile a rail waypoint?
109 * @param t the tile to get the information from
110 * @pre IsTileType(t, MP_STATION)
111 * @return true if and only if the tile is a rail waypoint
113 inline bool IsRailWaypoint(Tile t)
115 return GetStationType(t) == STATION_WAYPOINT;
119 * Is this tile a station tile and a rail waypoint?
120 * @param t the tile to get the information from
121 * @return true if and only if the tile is a rail waypoint
123 inline bool IsRailWaypointTile(Tile t)
125 return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
129 * Has this station tile a rail? In other words, is this station
130 * tile a rail station or rail waypoint?
131 * @param t the tile to check
132 * @pre IsTileType(t, MP_STATION)
133 * @return true if and only if the tile has rail
135 inline bool HasStationRail(Tile t)
137 return IsRailStation(t) || IsRailWaypoint(t);
141 * Has this station tile a rail? In other words, is this station
142 * tile a rail station or rail waypoint?
143 * @param t the tile to check
144 * @return true if and only if the tile is a station tile and has rail
146 inline bool HasStationTileRail(Tile t)
148 return IsTileType(t, MP_STATION) && HasStationRail(t);
152 * Is this station tile an airport?
153 * @param t the tile to get the information from
154 * @pre IsTileType(t, MP_STATION)
155 * @return true if and only if the tile is an airport
157 inline bool IsAirport(Tile t)
159 return GetStationType(t) == STATION_AIRPORT;
163 * Is this tile a station tile and an airport tile?
164 * @param t the tile to get the information from
165 * @return true if and only if the tile is an airport
167 inline bool IsAirportTile(Tile t)
169 return IsTileType(t, MP_STATION) && IsAirport(t);
172 bool IsHangar(Tile t);
175 * Is the station at \a t a truck stop?
176 * @param t Tile to check
177 * @pre IsTileType(t, MP_STATION)
178 * @return \c true if station is a truck stop, \c false otherwise
180 inline bool IsTruckStop(Tile t)
182 return GetStationType(t) == STATION_TRUCK;
186 * Is the station at \a t a bus stop?
187 * @param t Tile to check
188 * @pre IsTileType(t, MP_STATION)
189 * @return \c true if station is a bus stop, \c false otherwise
191 inline bool IsBusStop(Tile t)
193 return GetStationType(t) == STATION_BUS;
197 * Is the station at \a t a road station?
198 * @param t Tile to check
199 * @pre IsTileType(t, MP_STATION)
200 * @return \c true if station at the tile is a bus top or a truck stop, \c false otherwise
202 inline bool IsRoadStop(Tile t)
204 assert(IsTileType(t, MP_STATION));
205 return IsTruckStop(t) || IsBusStop(t);
209 * Is tile \a t a road stop station?
210 * @param t Tile to check
211 * @return \c true if the tile is a station tile and a road stop
213 inline bool IsRoadStopTile(Tile t)
215 return IsTileType(t, MP_STATION) && IsRoadStop(t);
219 * Is tile \a t a bay (non-drive through) road stop station?
220 * @param t Tile to check
221 * @return \c true if the tile is a station tile and a bay road stop
223 inline bool IsBayRoadStopTile(Tile t)
225 return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
229 * Is tile \a t a drive through road stop station?
230 * @param t Tile to check
231 * @return \c true if the tile is a station tile and a drive through road stop
233 inline bool IsDriveThroughStopTile(Tile t)
235 return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
238 StationGfx GetTranslatedAirportTileID(StationGfx gfx);
241 * Get the station graphics of this airport tile
242 * @param t the tile to query
243 * @pre IsAirport(t)
244 * @return the station graphics
246 inline StationGfx GetAirportGfx(Tile t)
248 assert(IsAirport(t));
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 inline DiagDirection GetRoadStopDir(Tile 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 inline bool IsOilRig(Tile 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 inline bool IsDock(Tile 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 inline bool IsDockTile(Tile 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 inline bool IsBuoy(Tile 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 inline bool IsBuoyTile(Tile 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 inline bool IsHangarTile(Tile t)
329 return IsTileType(t, MP_STATION) && IsHangar(t);
333 * Is tile \a t a blocked tile?
334 * @pre HasStationRail(t)
335 * @param t Tile to check
336 * @return \c true if the tile is blocked
338 inline bool IsStationTileBlocked(Tile t)
340 assert(HasStationRail(t));
341 return HasBit(t.m6(), 0);
345 * Set the blocked state of the rail station
346 * @pre HasStationRail(t)
347 * @param t the station tile
348 * @param b the blocked state
350 inline void SetStationTileBlocked(Tile t, bool b)
352 assert(HasStationRail(t));
353 SB(t.m6(), 0, 1, b ? 1 : 0);
357 * Can tile \a t have catenary wires?
358 * @pre HasStationRail(t)
359 * @param t Tile to check
360 * @return \c true if the tile can have catenary wires
362 inline bool CanStationTileHaveWires(Tile t)
364 assert(HasStationRail(t));
365 return HasBit(t.m6(), 6);
369 * Set the catenary wires state of the rail station
370 * @pre HasStationRail(t)
371 * @param t the station tile
372 * @param b the catenary wires state
374 inline void SetStationTileHaveWires(Tile t, bool b)
376 assert(HasStationRail(t));
377 SB(t.m6(), 6, 1, b ? 1 : 0);
381 * Can tile \a t have catenary pylons?
382 * @pre HasStationRail(t)
383 * @param t Tile to check
384 * @return \c true if the tile can have catenary pylons
386 inline bool CanStationTileHavePylons(Tile t)
388 assert(HasStationRail(t));
389 return HasBit(t.m6(), 7);
393 * Set the catenary pylon state of the rail station
394 * @pre HasStationRail(t)
395 * @param t the station tile
396 * @param b the catenary pylons state
398 inline void SetStationTileHavePylons(Tile t, bool b)
400 assert(HasStationRail(t));
401 SB(t.m6(), 7, 1, b ? 1 : 0);
405 * Get the rail direction of a rail station.
406 * @param t Tile to query
407 * @pre HasStationRail(t)
408 * @return The direction of the rails on tile \a t.
410 inline Axis GetRailStationAxis(Tile t)
412 assert(HasStationRail(t));
413 return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
417 * Get the rail track of a rail station tile.
418 * @param t Tile to query
419 * @pre HasStationRail(t)
420 * @return The rail track of the rails on tile \a t.
422 inline Track GetRailStationTrack(Tile t)
424 return AxisToTrack(GetRailStationAxis(t));
428 * Get the trackbits of a rail station tile.
429 * @param t Tile to query
430 * @pre HasStationRail(t)
431 * @return The trackbits of the rails on tile \a t.
433 inline TrackBits GetRailStationTrackBits(Tile t)
435 return AxisToTrackBits(GetRailStationAxis(t));
439 * Check if a tile is a valid continuation to a railstation tile.
440 * The tile \a test_tile is a valid continuation to \a station_tile, if all of the following are true:
441 * \li \a test_tile is a rail station tile
442 * \li the railtype of \a test_tile is compatible with the railtype of \a station_tile
443 * \li the tracks on \a test_tile and \a station_tile are in the same direction
444 * \li both tiles belong to the same station
445 * \li \a test_tile is not blocked (@see IsStationTileBlocked)
446 * @param test_tile Tile to test
447 * @param station_tile Station tile to compare with
448 * @pre IsRailStationTile(station_tile)
449 * @return true if the two tiles are compatible
451 inline bool IsCompatibleTrainStationTile(Tile test_tile, Tile station_tile)
453 assert(IsRailStationTile(station_tile));
454 return IsRailStationTile(test_tile) && !IsStationTileBlocked(test_tile) &&
455 IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
456 GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
457 GetStationIndex(test_tile) == GetStationIndex(station_tile);
461 * Get the reservation state of the rail station
462 * @pre HasStationRail(t)
463 * @param t the station tile
464 * @return reservation state
466 inline bool HasStationReservation(Tile t)
468 assert(HasStationRail(t));
469 return HasBit(t.m6(), 2);
473 * Set the reservation state of the rail station
474 * @pre HasStationRail(t)
475 * @param t the station tile
476 * @param b the reservation state
478 inline void SetRailStationReservation(Tile t, bool b)
480 assert(HasStationRail(t));
481 SB(t.m6(), 2, 1, b ? 1 : 0);
485 * Get the reserved track bits for a waypoint
486 * @pre HasStationRail(t)
487 * @param t the tile
488 * @return reserved track bits
490 inline TrackBits GetStationReservationTrackBits(Tile t)
492 return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
496 * Get the direction of a dock.
497 * @param t Tile to query
498 * @pre IsDock(t)
499 * @pre \a t is the land part of the dock
500 * @return The direction of the dock on tile \a t.
502 inline DiagDirection GetDockDirection(Tile t)
504 StationGfx gfx = GetStationGfx(t);
505 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
506 return (DiagDirection)(gfx);
510 * Check whether a dock tile is the tile on water.
512 inline bool IsDockWaterPart(Tile t)
514 assert(IsDockTile(t));
515 StationGfx gfx = GetStationGfx(t);
516 return gfx >= GFX_DOCK_BASE_WATER_PART;
520 * Is there a custom rail station spec on this tile?
521 * @param t Tile to query
522 * @pre HasStationTileRail(t)
523 * @return True if this station is part of a newgrf station.
525 inline bool IsCustomStationSpecIndex(Tile t)
527 assert(HasStationTileRail(t));
528 return t.m4() != 0;
532 * Set the custom station spec for this tile.
533 * @param t Tile to set the stationspec of.
534 * @param specindex The new spec.
535 * @pre HasStationTileRail(t)
537 inline void SetCustomStationSpecIndex(Tile t, byte specindex)
539 assert(HasStationTileRail(t));
540 t.m4() = specindex;
544 * Get the custom station spec for this tile.
545 * @param t Tile to query
546 * @pre HasStationTileRail(t)
547 * @return The custom station spec of this tile.
549 inline uint GetCustomStationSpecIndex(Tile t)
551 assert(HasStationTileRail(t));
552 return t.m4();
556 * Is there a custom road stop spec on this tile?
557 * @param t Tile to query
558 * @pre IsRoadStopTile(t)
559 * @return True if this station is part of a newgrf station.
561 inline bool IsCustomRoadStopSpecIndex(Tile t)
563 assert(IsRoadStopTile(t));
564 return GB(t.m8(), 0, 6) != 0;
568 * Set the custom road stop spec for this tile.
569 * @param t Tile to set the stationspec of.
570 * @param specindex The new spec.
571 * @pre IsRoadStopTile(t)
573 inline void SetCustomRoadStopSpecIndex(Tile t, byte specindex)
575 assert(IsRoadStopTile(t));
576 SB(t.m8(), 0, 6, specindex);
580 * Get the custom road stop spec for this tile.
581 * @param t Tile to query
582 * @pre IsRoadStopTile(t)
583 * @return The custom station spec of this tile.
585 inline uint GetCustomRoadStopSpecIndex(Tile t)
587 assert(IsRoadStopTile(t));
588 return GB(t.m8(), 0, 6);
592 * Set the random bits for a station tile.
593 * @param t Tile to set random bits for.
594 * @param random_bits The random bits.
595 * @pre IsTileType(t, MP_STATION)
597 inline void SetStationTileRandomBits(Tile t, byte random_bits)
599 assert(IsTileType(t, MP_STATION));
600 SB(t.m3(), 4, 4, random_bits);
604 * Get the random bits of a station tile.
605 * @param t Tile to query
606 * @pre IsTileType(t, MP_STATION)
607 * @return The random bits for this station tile.
609 inline byte GetStationTileRandomBits(Tile t)
611 assert(IsTileType(t, MP_STATION));
612 return GB(t.m3(), 4, 4);
616 * Make the given tile a station tile.
617 * @param t the tile to make a station tile
618 * @param o the owner of the station
619 * @param sid the station to which this tile belongs
620 * @param st the type this station tile
621 * @param section the StationGfx to be used for this tile
622 * @param wc The water class of the station
624 inline void MakeStation(Tile t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
626 SetTileType(t, MP_STATION);
627 SetTileOwner(t, o);
628 SetWaterClass(t, wc);
629 SetDockingTile(t, false);
630 t.m2() = sid;
631 t.m3() = 0;
632 t.m4() = 0;
633 t.m5() = section;
634 SB(t.m6(), 2, 1, 0);
635 SB(t.m6(), 3, 3, st);
636 t.m7() = 0;
637 t.m8() = 0;
641 * Make the given tile a rail station tile.
642 * @param t the tile to make a rail station tile
643 * @param o the owner of the station
644 * @param sid the station to which this tile belongs
645 * @param a the axis of this tile
646 * @param section the StationGfx to be used for this tile
647 * @param rt the railtype of this tile
649 inline void MakeRailStation(Tile t, Owner o, StationID sid, Axis a, byte section, RailType rt)
651 MakeStation(t, o, sid, STATION_RAIL, section + a);
652 SetRailType(t, rt);
653 SetRailStationReservation(t, false);
657 * Make the given tile a rail waypoint tile.
658 * @param t the tile to make a rail waypoint
659 * @param o the owner of the waypoint
660 * @param sid the waypoint to which this tile belongs
661 * @param a the axis of this tile
662 * @param section the StationGfx to be used for this tile
663 * @param rt the railtype of this tile
665 inline void MakeRailWaypoint(Tile t, Owner o, StationID sid, Axis a, byte section, RailType rt)
667 MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
668 SetRailType(t, rt);
669 SetRailStationReservation(t, false);
673 * Make the given tile a roadstop tile.
674 * @param t the tile to make a roadstop
675 * @param o the owner of the roadstop
676 * @param sid the station to which this tile belongs
677 * @param rst the type of roadstop to make this tile
678 * @param road_rt the road roadtype on this tile
679 * @param tram_rt the tram roadtype on this tile
680 * @param d the direction of the roadstop
682 inline void MakeRoadStop(Tile t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
684 MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
685 SetRoadTypes(t, road_rt, tram_rt);
686 SetRoadOwner(t, RTT_ROAD, o);
687 SetRoadOwner(t, RTT_TRAM, o);
691 * Make the given tile a drivethrough roadstop tile.
692 * @param t the tile to make a roadstop
693 * @param station the owner of the roadstop
694 * @param road the owner of the road
695 * @param tram the owner of the tram
696 * @param sid the station to which this tile belongs
697 * @param rst the type of roadstop to make this tile
698 * @param road_rt the road roadtype on this tile
699 * @param tram_rt the tram roadtype on this tile
700 * @param a the direction of the roadstop
702 inline void MakeDriveThroughRoadStop(Tile t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, Axis a)
704 MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
705 SetRoadTypes(t, road_rt, tram_rt);
706 SetRoadOwner(t, RTT_ROAD, road);
707 SetRoadOwner(t, RTT_TRAM, tram);
711 * Make the given tile an airport tile.
712 * @param t the tile to make a airport
713 * @param o the owner of the airport
714 * @param sid the station to which this tile belongs
715 * @param section the StationGfx to be used for this tile
716 * @param wc the type of water on this tile
718 inline void MakeAirport(Tile t, Owner o, StationID sid, byte section, WaterClass wc)
720 MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
724 * Make the given tile a buoy tile.
725 * @param t the tile to make a buoy
726 * @param sid the station to which this tile belongs
727 * @param wc the type of water on this tile
729 inline void MakeBuoy(Tile t, StationID sid, WaterClass wc)
731 /* Make the owner of the buoy tile the same as the current owner of the
732 * water tile. In this way, we can reset the owner of the water to its
733 * original state when the buoy gets removed. */
734 MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
738 * Make the given tile a dock tile.
739 * @param t the tile to make a dock
740 * @param o the owner of the dock
741 * @param sid the station to which this tile belongs
742 * @param d the direction of the dock
743 * @param wc the type of water on this tile
745 inline void MakeDock(Tile t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
747 MakeStation(t, o, sid, STATION_DOCK, d);
748 MakeStation(TileIndex(t) + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
752 * Make the given tile an oilrig tile.
753 * @param t the tile to make an oilrig
754 * @param sid the station to which this tile belongs
755 * @param wc the type of water on this tile
757 inline void MakeOilrig(Tile t, StationID sid, WaterClass wc)
759 MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
762 #endif /* STATION_MAP_H */