Update readme and changelog for v1.26.0
[openttd-joker.git] / src / station_map.h
blob98171bab8414cf7bf7172c914d1fb13a72ae995c
1 /* $Id: station_map.h 25221 2013-05-06 13:05:04Z frosch $ */
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 station tile an seaplane airport?
165 * @param t the tile to get the information from
166 * @pre IsTileType(t, MP_STATION)
167 * @return true if and only if the tile is an airport
169 static inline bool IsSeaplanePort(TileIndex t)
171 return GetStationType(t) == STATION_AIRPORT && GetWaterClass(t) != WATER_CLASS_INVALID;
175 * Is this tile a station tile and an airport tile?
176 * @param t the tile to get the information from
177 * @return true if and only if the tile is an airport
179 static inline bool IsAirportTile(TileIndex t)
181 return IsTileType(t, MP_STATION) && IsAirport(t);
184 bool IsHangar(TileIndex t);
187 * Is the station at \a t a truck stop?
188 * @param t Tile to check
189 * @pre IsTileType(t, MP_STATION)
190 * @return \c true if station is a truck stop, \c false otherwise
192 static inline bool IsTruckStop(TileIndex t)
194 return GetStationType(t) == STATION_TRUCK;
198 * Is the station at \a t a bus stop?
199 * @param t Tile to check
200 * @pre IsTileType(t, MP_STATION)
201 * @return \c true if station is a bus stop, \c false otherwise
203 static inline bool IsBusStop(TileIndex t)
205 return GetStationType(t) == STATION_BUS;
209 * Is the station at \a t a road station?
210 * @param t Tile to check
211 * @pre IsTileType(t, MP_STATION)
212 * @return \c true if station at the tile is a bus top or a truck stop, \c false otherwise
214 static inline bool IsRoadStop(TileIndex t)
216 assert(IsTileType(t, MP_STATION));
217 return IsTruckStop(t) || IsBusStop(t);
221 * Is tile \a t a road stop station?
222 * @param t Tile to check
223 * @return \c true if the tile is a station tile and a road stop
225 static inline bool IsRoadStopTile(TileIndex t)
227 return IsTileType(t, MP_STATION) && IsRoadStop(t);
231 * Is tile \a t a standard (non-drive through) road stop station?
232 * @param t Tile to check
233 * @return \c true if the tile is a station tile and a standard road stop
235 static inline bool IsStandardRoadStopTile(TileIndex t)
237 return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
241 * Is tile \a t a drive through road stop station?
242 * @param t Tile to check
243 * @return \c true if the tile is a station tile and a drive through road stop
245 static inline bool IsDriveThroughStopTile(TileIndex t)
247 return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
251 * Get the station graphics of this airport tile
252 * @param t the tile to query
253 * @pre IsAirport(t)
254 * @return the station graphics
256 static inline StationGfx GetAirportGfx(TileIndex t)
258 assert(IsAirport(t));
259 extern StationGfx GetTranslatedAirportTileID(StationGfx gfx);
260 return GetTranslatedAirportTileID(GetStationGfx(t));
264 * Gets the direction the road stop entrance points towards.
265 * @param t the tile of the road stop
266 * @pre IsRoadStopTile(t)
267 * @return the direction of the entrance
269 static inline DiagDirection GetRoadStopDir(TileIndex t)
271 StationGfx gfx = GetStationGfx(t);
272 assert(IsRoadStopTile(t));
273 if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
274 return (DiagDirection)(gfx);
275 } else {
276 return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
281 * Is tile \a t part of an oilrig?
282 * @param t Tile to check
283 * @pre IsTileType(t, MP_STATION)
284 * @return \c true if the tile is an oilrig tile
286 static inline bool IsOilRig(TileIndex t)
288 return GetStationType(t) == STATION_OILRIG;
292 * Is tile \a t a dock tile?
293 * @param t Tile to check
294 * @pre IsTileType(t, MP_STATION)
295 * @return \c true if the tile is a dock
297 static inline bool IsDock(TileIndex t)
299 return GetStationType(t) == STATION_DOCK;
303 * Is tile \a t a dock tile?
304 * @param t Tile to check
305 * @return \c true if the tile is a dock
307 static inline bool IsDockTile(TileIndex t)
309 return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
313 * Is tile \a t a buoy tile?
314 * @param t Tile to check
315 * @pre IsTileType(t, MP_STATION)
316 * @return \c true if the tile is a buoy
318 static inline bool IsBuoy(TileIndex t)
320 return GetStationType(t) == STATION_BUOY;
324 * Is tile \a t a buoy tile?
325 * @param t Tile to check
326 * @return \c true if the tile is a buoy
328 static inline bool IsBuoyTile(TileIndex t)
330 return IsTileType(t, MP_STATION) && IsBuoy(t);
334 * Is tile \a t an hangar tile?
335 * @param t Tile to check
336 * @return \c true if the tile is an hangar
338 static inline bool IsHangarTile(TileIndex t)
340 return IsTileType(t, MP_STATION) && IsHangar(t);
344 * Get the rail direction of a rail station.
345 * @param t Tile to query
346 * @pre HasStationRail(t)
347 * @return The direction of the rails on tile \a t.
349 static inline Axis GetRailStationAxis(TileIndex t)
351 assert(HasStationRail(t));
352 return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
356 * Get the rail track of a rail station tile.
357 * @param t Tile to query
358 * @pre HasStationRail(t)
359 * @return The rail track of the rails on tile \a t.
361 static inline Track GetRailStationTrack(TileIndex t)
363 return AxisToTrack(GetRailStationAxis(t));
367 * Get the trackbits of a rail station tile.
368 * @param t Tile to query
369 * @pre HasStationRail(t)
370 * @return The trackbits of the rails on tile \a t.
372 static inline TrackBits GetRailStationTrackBits(TileIndex t)
374 return AxisToTrackBits(GetRailStationAxis(t));
378 * Check if a tile is a valid continuation to a railstation tile.
379 * The tile \a test_tile is a valid continuation to \a station_tile, if all of the following are true:
380 * \li \a test_tile is a rail station tile
381 * \li the railtype of \a test_tile is compatible with the railtype of \a station_tile
382 * \li the tracks on \a test_tile and \a station_tile are in the same direction
383 * \li both tiles belong to the same station
384 * \li \a test_tile is not blocked (@see IsStationTileBlocked)
385 * @param test_tile Tile to test
386 * @param station_tile Station tile to compare with
387 * @pre IsRailStationTile(station_tile)
388 * @return true if the two tiles are compatible
390 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
392 assert(IsRailStationTile(station_tile));
393 return IsRailStationTile(test_tile) && IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
394 GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
395 GetStationIndex(test_tile) == GetStationIndex(station_tile) &&
396 !IsStationTileBlocked(test_tile);
400 * Get the reservation state of the rail station
401 * @pre HasStationRail(t)
402 * @param t the station tile
403 * @return reservation state
405 static inline bool HasStationReservation(TileIndex t)
407 assert(HasStationRail(t));
408 return HasBit(_me[t].m6, 2);
412 * Set the reservation state of the rail station
413 * @pre HasStationRail(t)
414 * @param t the station tile
415 * @param b the reservation state
417 static inline void SetRailStationReservation(TileIndex t, bool b)
419 assert(HasStationRail(t));
420 SB(_me[t].m6, 2, 1, b ? 1 : 0);
424 * Get the reserved track bits for a waypoint
425 * @pre HasStationRail(t)
426 * @param t the tile
427 * @return reserved track bits
429 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
431 return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
435 * Get the direction of a dock.
436 * @param t Tile to query
437 * @pre IsDock(t)
438 * @pre \a t is the land part of the dock
439 * @return The direction of the dock on tile \a t.
441 static inline DiagDirection GetDockDirection(TileIndex t)
443 StationGfx gfx = GetStationGfx(t);
444 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
445 return (DiagDirection)(gfx);
449 * Get the tileoffset from this tile a ship should target to get to this dock.
450 * @param t Tile to query
451 * @pre IsTileType(t, MP_STATION)
452 * @pre IsBuoy(t) || IsOilRig(t) || IsDock(t)
453 * @return The offset from this tile that should be used as destination for ships.
455 static inline TileIndexDiffC GetDockOffset(TileIndex t)
457 static const TileIndexDiffC buoy_offset = {0, 0};
458 static const TileIndexDiffC oilrig_offset = {2, 0};
459 static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
460 {-2, 0},
461 { 0, 2},
462 { 2, 0},
463 { 0, -2},
465 assert(IsTileType(t, MP_STATION));
467 if (IsBuoy(t)) return buoy_offset;
468 if (IsOilRig(t)) return oilrig_offset;
470 assert(IsDock(t));
472 return dock_offset[GetDockDirection(t)];
476 * Is there a custom rail station spec on this tile?
477 * @param t Tile to query
478 * @pre HasStationTileRail(t)
479 * @return True if this station is part of a newgrf station.
481 static inline bool IsCustomStationSpecIndex(TileIndex t)
483 assert(HasStationTileRail(t));
484 return _m[t].m4 != 0;
488 * Set the custom station spec for this tile.
489 * @param t Tile to set the stationspec of.
490 * @param specindex The new spec.
491 * @pre HasStationTileRail(t)
493 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
495 assert(HasStationTileRail(t));
496 _m[t].m4 = specindex;
500 * Get the custom station spec for this tile.
501 * @param t Tile to query
502 * @pre HasStationTileRail(t)
503 * @return The custom station spec of this tile.
505 static inline uint GetCustomStationSpecIndex(TileIndex t)
507 assert(HasStationTileRail(t));
508 return _m[t].m4;
512 * Set the random bits for a station tile.
513 * @param t Tile to set random bits for.
514 * @param random_bits The random bits.
515 * @pre IsTileType(t, MP_STATION)
517 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
519 assert(IsTileType(t, MP_STATION));
520 SB(_m[t].m3, 4, 4, random_bits);
524 * Get the random bits of a station tile.
525 * @param t Tile to query
526 * @pre IsTileType(t, MP_STATION)
527 * @return The random bits for this station tile.
529 static inline byte GetStationTileRandomBits(TileIndex t)
531 assert(IsTileType(t, MP_STATION));
532 return GB(_m[t].m3, 4, 4);
536 * Make the given tile a station tile.
537 * @param t the tile to make a station tile
538 * @param o the owner of the station
539 * @param sid the station to which this tile belongs
540 * @param st the type this station tile
541 * @param section the StationGfx to be used for this tile
542 * @param wc The water class of the station
544 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
546 SetTileType(t, MP_STATION);
547 SetTileOwner(t, o);
548 SetWaterClass(t, wc);
549 _m[t].m2 = sid;
550 _m[t].m3 = 0;
551 _m[t].m4 = 0;
552 _m[t].m5 = section;
553 SB(_me[t].m6, 2, 1, 0);
554 SB(_me[t].m6, 3, 3, st);
555 _me[t].m7 = 0;
559 * Make the given tile a rail station tile.
560 * @param t the tile to make a rail station tile
561 * @param o the owner of the station
562 * @param sid the station to which this tile belongs
563 * @param a the axis of this tile
564 * @param section the StationGfx to be used for this tile
565 * @param rt the railtype of this tile
567 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
569 MakeStation(t, o, sid, STATION_RAIL, section + a);
570 SetRailType(t, rt);
571 SetRailStationReservation(t, false);
575 * Make the given tile a rail waypoint tile.
576 * @param t the tile to make a rail waypoint
577 * @param o the owner of the waypoint
578 * @param sid the waypoint to which this tile belongs
579 * @param a the axis of this tile
580 * @param section the StationGfx to be used for this tile
581 * @param rt the railtype of this tile
583 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
585 MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
586 SetRailType(t, rt);
587 SetRailStationReservation(t, false);
591 * Make the given tile a roadstop tile.
592 * @param t the tile to make a roadstop
593 * @param o the owner of the roadstop
594 * @param sid the station to which this tile belongs
595 * @param rst the type of roadstop to make this tile
596 * @param rt the roadtypes on this tile
597 * @param d the direction of the roadstop
599 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
601 MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
602 SetRoadTypes(t, rt);
603 SetRoadOwner(t, ROADTYPE_ROAD, o);
604 SetRoadOwner(t, ROADTYPE_TRAM, o);
608 * Make the given tile a drivethrough roadstop tile.
609 * @param t the tile to make a roadstop
610 * @param station the owner of the roadstop
611 * @param road the owner of the road
612 * @param tram the owner of the tram
613 * @param sid the station to which this tile belongs
614 * @param rst the type of roadstop to make this tile
615 * @param rt the roadtypes on this tile
616 * @param a the direction of the roadstop
618 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
620 MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
621 SetRoadTypes(t, rt);
622 SetRoadOwner(t, ROADTYPE_ROAD, road);
623 SetRoadOwner(t, ROADTYPE_TRAM, tram);
627 * Make the given tile an airport tile.
628 * @param t the tile to make a airport
629 * @param o the owner of the airport
630 * @param sid the station to which this tile belongs
631 * @param section the StationGfx to be used for this tile
632 * @param wc the type of water on this tile
634 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
636 MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
640 * Make the given tile a buoy tile.
641 * @param t the tile to make a buoy
642 * @param sid the station to which this tile belongs
643 * @param wc the type of water on this tile
645 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
647 /* Make the owner of the buoy tile the same as the current owner of the
648 * water tile. In this way, we can reset the owner of the water to its
649 * original state when the buoy gets removed. */
650 MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
654 * Make the given tile a dock tile.
655 * @param t the tile to make a dock
656 * @param o the owner of the dock
657 * @param sid the station to which this tile belongs
658 * @param d the direction of the dock
659 * @param wc the type of water on this tile
661 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
663 MakeStation(t, o, sid, STATION_DOCK, d);
664 MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
668 * Make the given tile an oilrig tile.
669 * @param t the tile to make an oilrig
670 * @param sid the station to which this tile belongs
671 * @param wc the type of water on this tile
673 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
675 MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
678 #endif /* STATION_MAP_H */