Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / station_map.h
blobdbb00ef2f5e93f2b31a6e9dd697aa2fad039a4c7
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 static inline StationID GetStationIndex(TileIndex t)
30 assert(IsTileType(t, MP_STATION));
31 return (StationID)_m[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 static inline StationType GetStationType(TileIndex t)
46 assert(IsTileType(t, MP_STATION));
47 return (StationType)GB(_me[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 static inline RoadStopType GetRoadStopType(TileIndex 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 static inline StationGfx GetStationGfx(TileIndex t)
70 assert(IsTileType(t, MP_STATION));
71 return _m[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 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
82 assert(IsTileType(t, MP_STATION));
83 _m[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 static inline bool IsRailStation(TileIndex 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 static inline bool IsRailStationTile(TileIndex 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 static inline bool IsRailWaypoint(TileIndex 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 static inline bool IsRailWaypointTile(TileIndex 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 static inline bool HasStationRail(TileIndex 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 static inline bool HasStationTileRail(TileIndex 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 static inline bool IsAirport(TileIndex 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 static inline bool IsAirportTile(TileIndex t)
169 return IsTileType(t, MP_STATION) && IsAirport(t);
172 bool IsHangar(TileIndex 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 static inline bool IsTruckStop(TileIndex 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 static inline bool IsBusStop(TileIndex 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 static inline bool IsRoadStop(TileIndex 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 static inline bool IsRoadStopTile(TileIndex t)
215 return IsTileType(t, MP_STATION) && IsRoadStop(t);
219 * Is tile \a t a standard (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 standard road stop
223 static inline bool IsStandardRoadStopTile(TileIndex 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 static inline bool IsDriveThroughStopTile(TileIndex t)
235 return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
239 * Get the station graphics of this airport tile
240 * @param t the tile to query
241 * @pre IsAirport(t)
242 * @return the station graphics
244 static inline StationGfx GetAirportGfx(TileIndex t)
246 assert(IsAirport(t));
247 extern StationGfx GetTranslatedAirportTileID(StationGfx gfx);
248 return GetTranslatedAirportTileID(GetStationGfx(t));
252 * Gets the direction the road stop entrance points towards.
253 * @param t the tile of the road stop
254 * @pre IsRoadStopTile(t)
255 * @return the direction of the entrance
257 static inline DiagDirection GetRoadStopDir(TileIndex t)
259 StationGfx gfx = GetStationGfx(t);
260 assert(IsRoadStopTile(t));
261 if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
262 return (DiagDirection)(gfx);
263 } else {
264 return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
269 * Is tile \a t part of an oilrig?
270 * @param t Tile to check
271 * @pre IsTileType(t, MP_STATION)
272 * @return \c true if the tile is an oilrig tile
274 static inline bool IsOilRig(TileIndex t)
276 return GetStationType(t) == STATION_OILRIG;
280 * Is tile \a t a dock tile?
281 * @param t Tile to check
282 * @pre IsTileType(t, MP_STATION)
283 * @return \c true if the tile is a dock
285 static inline bool IsDock(TileIndex t)
287 return GetStationType(t) == STATION_DOCK;
291 * Is tile \a t a dock tile?
292 * @param t Tile to check
293 * @return \c true if the tile is a dock
295 static inline bool IsDockTile(TileIndex t)
297 return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
301 * Is tile \a t a buoy tile?
302 * @param t Tile to check
303 * @pre IsTileType(t, MP_STATION)
304 * @return \c true if the tile is a buoy
306 static inline bool IsBuoy(TileIndex t)
308 return GetStationType(t) == STATION_BUOY;
312 * Is tile \a t a buoy tile?
313 * @param t Tile to check
314 * @return \c true if the tile is a buoy
316 static inline bool IsBuoyTile(TileIndex t)
318 return IsTileType(t, MP_STATION) && IsBuoy(t);
322 * Is tile \a t an hangar tile?
323 * @param t Tile to check
324 * @return \c true if the tile is an hangar
326 static inline bool IsHangarTile(TileIndex t)
328 return IsTileType(t, MP_STATION) && IsHangar(t);
332 * Get the rail direction of a rail station.
333 * @param t Tile to query
334 * @pre HasStationRail(t)
335 * @return The direction of the rails on tile \a t.
337 static inline Axis GetRailStationAxis(TileIndex t)
339 assert(HasStationRail(t));
340 return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
344 * Get the rail track of a rail station tile.
345 * @param t Tile to query
346 * @pre HasStationRail(t)
347 * @return The rail track of the rails on tile \a t.
349 static inline Track GetRailStationTrack(TileIndex t)
351 return AxisToTrack(GetRailStationAxis(t));
355 * Get the trackbits of a rail station tile.
356 * @param t Tile to query
357 * @pre HasStationRail(t)
358 * @return The trackbits of the rails on tile \a t.
360 static inline TrackBits GetRailStationTrackBits(TileIndex t)
362 return AxisToTrackBits(GetRailStationAxis(t));
366 * Check if a tile is a valid continuation to a railstation tile.
367 * The tile \a test_tile is a valid continuation to \a station_tile, if all of the following are true:
368 * \li \a test_tile is a rail station tile
369 * \li the railtype of \a test_tile is compatible with the railtype of \a station_tile
370 * \li the tracks on \a test_tile and \a station_tile are in the same direction
371 * \li both tiles belong to the same station
372 * \li \a test_tile is not blocked (@see IsStationTileBlocked)
373 * @param test_tile Tile to test
374 * @param station_tile Station tile to compare with
375 * @pre IsRailStationTile(station_tile)
376 * @return true if the two tiles are compatible
378 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
380 assert(IsRailStationTile(station_tile));
381 return IsRailStationTile(test_tile) && IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
382 GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
383 GetStationIndex(test_tile) == GetStationIndex(station_tile) &&
384 !IsStationTileBlocked(test_tile);
388 * Get the reservation state of the rail station
389 * @pre HasStationRail(t)
390 * @param t the station tile
391 * @return reservation state
393 static inline bool HasStationReservation(TileIndex t)
395 assert(HasStationRail(t));
396 return HasBit(_me[t].m6, 2);
400 * Set the reservation state of the rail station
401 * @pre HasStationRail(t)
402 * @param t the station tile
403 * @param b the reservation state
405 static inline void SetRailStationReservation(TileIndex t, bool b)
407 assert(HasStationRail(t));
408 SB(_me[t].m6, 2, 1, b ? 1 : 0);
412 * Get the reserved track bits for a waypoint
413 * @pre HasStationRail(t)
414 * @param t the tile
415 * @return reserved track bits
417 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
419 return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
423 * Get the direction of a dock.
424 * @param t Tile to query
425 * @pre IsDock(t)
426 * @pre \a t is the land part of the dock
427 * @return The direction of the dock on tile \a t.
429 static inline DiagDirection GetDockDirection(TileIndex t)
431 StationGfx gfx = GetStationGfx(t);
432 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
433 return (DiagDirection)(gfx);
437 * Get the tileoffset from this tile a ship should target to get to this dock.
438 * @param t Tile to query
439 * @pre IsTileType(t, MP_STATION)
440 * @pre IsBuoy(t) || IsOilRig(t) || IsDock(t)
441 * @return The offset from this tile that should be used as destination for ships.
443 static inline TileIndexDiffC GetDockOffset(TileIndex t)
445 static const TileIndexDiffC buoy_offset = {0, 0};
446 static const TileIndexDiffC oilrig_offset = {2, 0};
447 static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
448 {-2, 0},
449 { 0, 2},
450 { 2, 0},
451 { 0, -2},
453 assert(IsTileType(t, MP_STATION));
455 if (IsBuoy(t)) return buoy_offset;
456 if (IsOilRig(t)) return oilrig_offset;
458 assert(IsDock(t));
460 return dock_offset[GetDockDirection(t)];
464 * Is there a custom rail station spec on this tile?
465 * @param t Tile to query
466 * @pre HasStationTileRail(t)
467 * @return True if this station is part of a newgrf station.
469 static inline bool IsCustomStationSpecIndex(TileIndex t)
471 assert(HasStationTileRail(t));
472 return _m[t].m4 != 0;
476 * Set the custom station spec for this tile.
477 * @param t Tile to set the stationspec of.
478 * @param specindex The new spec.
479 * @pre HasStationTileRail(t)
481 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
483 assert(HasStationTileRail(t));
484 _m[t].m4 = specindex;
488 * Get the custom station spec for this tile.
489 * @param t Tile to query
490 * @pre HasStationTileRail(t)
491 * @return The custom station spec of this tile.
493 static inline uint GetCustomStationSpecIndex(TileIndex t)
495 assert(HasStationTileRail(t));
496 return _m[t].m4;
500 * Set the random bits for a station tile.
501 * @param t Tile to set random bits for.
502 * @param random_bits The random bits.
503 * @pre IsTileType(t, MP_STATION)
505 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
507 assert(IsTileType(t, MP_STATION));
508 SB(_m[t].m3, 4, 4, random_bits);
512 * Get the random bits of a station tile.
513 * @param t Tile to query
514 * @pre IsTileType(t, MP_STATION)
515 * @return The random bits for this station tile.
517 static inline byte GetStationTileRandomBits(TileIndex t)
519 assert(IsTileType(t, MP_STATION));
520 return GB(_m[t].m3, 4, 4);
524 * Make the given tile a station tile.
525 * @param t the tile to make a station tile
526 * @param o the owner of the station
527 * @param sid the station to which this tile belongs
528 * @param st the type this station tile
529 * @param section the StationGfx to be used for this tile
530 * @param wc The water class of the station
532 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
534 SetTileType(t, MP_STATION);
535 SetTileOwner(t, o);
536 SetWaterClass(t, wc);
537 SetDockingTile(t, false);
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;
545 _me[t].m8 = 0;
549 * Make the given tile a rail station tile.
550 * @param t the tile to make a rail station tile
551 * @param o the owner of the station
552 * @param sid the station to which this tile belongs
553 * @param a the axis of this tile
554 * @param section the StationGfx to be used for this tile
555 * @param rt the railtype of this tile
557 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
559 MakeStation(t, o, sid, STATION_RAIL, section + a);
560 SetRailType(t, rt);
561 SetRailStationReservation(t, false);
565 * Make the given tile a rail waypoint tile.
566 * @param t the tile to make a rail waypoint
567 * @param o the owner of the waypoint
568 * @param sid the waypoint to which this tile belongs
569 * @param a the axis of this tile
570 * @param section the StationGfx to be used for this tile
571 * @param rt the railtype of this tile
573 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
575 MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
576 SetRailType(t, rt);
577 SetRailStationReservation(t, false);
581 * Make the given tile a roadstop tile.
582 * @param t the tile to make a roadstop
583 * @param o the owner of the roadstop
584 * @param sid the station to which this tile belongs
585 * @param rst the type of roadstop to make this tile
586 * @param road_rt the road roadtype on this tile
587 * @param tram_rt the tram roadtype on this tile
588 * @param d the direction of the roadstop
590 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
592 MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
593 SetRoadTypes(t, road_rt, tram_rt);
594 SetRoadOwner(t, RTT_ROAD, o);
595 SetRoadOwner(t, RTT_TRAM, o);
599 * Make the given tile a drivethrough roadstop tile.
600 * @param t the tile to make a roadstop
601 * @param station the owner of the roadstop
602 * @param road the owner of the road
603 * @param tram the owner of the tram
604 * @param sid the station to which this tile belongs
605 * @param rst the type of roadstop to make this tile
606 * @param road_rt the road roadtype on this tile
607 * @param tram_rt the tram roadtype on this tile
608 * @param a the direction of the roadstop
610 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, Axis a)
612 MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
613 SetRoadTypes(t, road_rt, tram_rt);
614 SetRoadOwner(t, RTT_ROAD, road);
615 SetRoadOwner(t, RTT_TRAM, tram);
619 * Make the given tile an airport tile.
620 * @param t the tile to make a airport
621 * @param o the owner of the airport
622 * @param sid the station to which this tile belongs
623 * @param section the StationGfx to be used for this tile
624 * @param wc the type of water on this tile
626 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
628 MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
632 * Make the given tile a buoy tile.
633 * @param t the tile to make a buoy
634 * @param sid the station to which this tile belongs
635 * @param wc the type of water on this tile
637 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
639 /* Make the owner of the buoy tile the same as the current owner of the
640 * water tile. In this way, we can reset the owner of the water to its
641 * original state when the buoy gets removed. */
642 MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
646 * Make the given tile a dock tile.
647 * @param t the tile to make a dock
648 * @param o the owner of the dock
649 * @param sid the station to which this tile belongs
650 * @param d the direction of the dock
651 * @param wc the type of water on this tile
653 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
655 MakeStation(t, o, sid, STATION_DOCK, d);
656 MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
660 * Make the given tile an oilrig tile.
661 * @param t the tile to make an oilrig
662 * @param sid the station to which this tile belongs
663 * @param wc the type of water on this tile
665 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
667 MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
670 #endif /* STATION_MAP_H */