OpenTTD
station_map.h
Go to the documentation of this file.
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  */
7 
10 #ifndef STATION_MAP_H
11 #define STATION_MAP_H
12 
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"
19 
20 typedef byte StationGfx;
21 
28 static inline StationID GetStationIndex(TileIndex t)
29 {
30  assert(IsTileType(t, MP_STATION));
31  return (StationID)_m[t].m2;
32 }
33 
34 
35 static const int GFX_DOCK_BASE_WATER_PART = 4;
36 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET = 4;
37 
45 {
46  assert(IsTileType(t, MP_STATION));
47  return (StationType)GB(_me[t].m6, 3, 3);
48 }
49 
57 {
58  assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
59  return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
60 }
61 
69 {
70  assert(IsTileType(t, MP_STATION));
71  return _m[t].m5;
72 }
73 
80 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
81 {
82  assert(IsTileType(t, MP_STATION));
83  _m[t].m5 = gfx;
84 }
85 
92 static inline bool IsRailStation(TileIndex t)
93 {
94  return GetStationType(t) == STATION_RAIL;
95 }
96 
102 static inline bool IsRailStationTile(TileIndex t)
103 {
104  return IsTileType(t, MP_STATION) && IsRailStation(t);
105 }
106 
113 static inline bool IsRailWaypoint(TileIndex t)
114 {
115  return GetStationType(t) == STATION_WAYPOINT;
116 }
117 
123 static inline bool IsRailWaypointTile(TileIndex t)
124 {
125  return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
126 }
127 
135 static inline bool HasStationRail(TileIndex t)
136 {
137  return IsRailStation(t) || IsRailWaypoint(t);
138 }
139 
146 static inline bool HasStationTileRail(TileIndex t)
147 {
148  return IsTileType(t, MP_STATION) && HasStationRail(t);
149 }
150 
157 static inline bool IsAirport(TileIndex t)
158 {
159  return GetStationType(t) == STATION_AIRPORT;
160 }
161 
167 static inline bool IsAirportTile(TileIndex t)
168 {
169  return IsTileType(t, MP_STATION) && IsAirport(t);
170 }
171 
172 bool IsHangar(TileIndex t);
173 
180 static inline bool IsTruckStop(TileIndex t)
181 {
182  return GetStationType(t) == STATION_TRUCK;
183 }
184 
191 static inline bool IsBusStop(TileIndex t)
192 {
193  return GetStationType(t) == STATION_BUS;
194 }
195 
202 static inline bool IsRoadStop(TileIndex t)
203 {
204  assert(IsTileType(t, MP_STATION));
205  return IsTruckStop(t) || IsBusStop(t);
206 }
207 
213 static inline bool IsRoadStopTile(TileIndex t)
214 {
215  return IsTileType(t, MP_STATION) && IsRoadStop(t);
216 }
217 
223 static inline bool IsStandardRoadStopTile(TileIndex t)
224 {
226 }
227 
233 static inline bool IsDriveThroughStopTile(TileIndex t)
234 {
236 }
237 
245 {
246  assert(IsAirport(t));
249 }
250 
258 {
259  StationGfx gfx = GetStationGfx(t);
260  assert(IsRoadStopTile(t));
262  return (DiagDirection)(gfx);
263  } else {
265  }
266 }
267 
274 static inline bool IsOilRig(TileIndex t)
275 {
276  return GetStationType(t) == STATION_OILRIG;
277 }
278 
285 static inline bool IsDock(TileIndex t)
286 {
287  return GetStationType(t) == STATION_DOCK;
288 }
289 
295 static inline bool IsDockTile(TileIndex t)
296 {
297  return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
298 }
299 
306 static inline bool IsBuoy(TileIndex t)
307 {
308  return GetStationType(t) == STATION_BUOY;
309 }
310 
316 static inline bool IsBuoyTile(TileIndex t)
317 {
318  return IsTileType(t, MP_STATION) && IsBuoy(t);
319 }
320 
326 static inline bool IsHangarTile(TileIndex t)
327 {
328  return IsTileType(t, MP_STATION) && IsHangar(t);
329 }
330 
338 {
339  assert(HasStationRail(t));
340  return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
341 }
342 
350 {
351  return AxisToTrack(GetRailStationAxis(t));
352 }
353 
361 {
363 }
364 
378 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
379 {
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);
385 }
386 
393 static inline bool HasStationReservation(TileIndex t)
394 {
395  assert(HasStationRail(t));
396  return HasBit(_me[t].m6, 2);
397 }
398 
405 static inline void SetRailStationReservation(TileIndex t, bool b)
406 {
407  assert(HasStationRail(t));
408  SB(_me[t].m6, 2, 1, b ? 1 : 0);
409 }
410 
418 {
420 }
421 
430 {
431  StationGfx gfx = GetStationGfx(t);
432  assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
433  return (DiagDirection)(gfx);
434 }
435 
444 {
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},
452  };
453  assert(IsTileType(t, MP_STATION));
454 
455  if (IsBuoy(t)) return buoy_offset;
456  if (IsOilRig(t)) return oilrig_offset;
457 
458  assert(IsDock(t));
459 
460  return dock_offset[GetDockDirection(t)];
461 }
462 
469 static inline bool IsCustomStationSpecIndex(TileIndex t)
470 {
471  assert(HasStationTileRail(t));
472  return _m[t].m4 != 0;
473 }
474 
481 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
482 {
483  assert(HasStationTileRail(t));
484  _m[t].m4 = specindex;
485 }
486 
494 {
495  assert(HasStationTileRail(t));
496  return _m[t].m4;
497 }
498 
505 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
506 {
507  assert(IsTileType(t, MP_STATION));
508  SB(_m[t].m3, 4, 4, random_bits);
509 }
510 
517 static inline byte GetStationTileRandomBits(TileIndex t)
518 {
519  assert(IsTileType(t, MP_STATION));
520  return GB(_m[t].m3, 4, 4);
521 }
522 
532 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
533 {
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;
546 }
547 
557 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
558 {
559  MakeStation(t, o, sid, STATION_RAIL, section + a);
560  SetRailType(t, rt);
561  SetRailStationReservation(t, false);
562 }
563 
573 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
574 {
575  MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
576  SetRailType(t, rt);
577  SetRailStationReservation(t, false);
578 }
579 
590 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
591 {
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);
596 }
597 
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)
611 {
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);
616 }
617 
626 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
627 {
628  MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
629 }
630 
637 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
638 {
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);
643 }
644 
653 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
654 {
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);
657 }
658 
665 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
666 {
667  MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
668 }
669 
670 #endif /* STATION_MAP_H */
Owner
Enum for all companies/owners.
Definition: company_type.h:18
static void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
Make the given tile a rail station tile.
Definition: station_map.h:557
static void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
Make the given tile a dock tile.
Definition: station_map.h:653
static TrackBits GetStationReservationTrackBits(TileIndex t)
Get the reserved track bits for a waypoint.
Definition: station_map.h:417
static void SetCustomStationSpecIndex(TileIndex t, byte specindex)
Set the custom station spec for this tile.
Definition: station_map.h:481
A standard stop for trucks.
Definition: station_type.h:46
static bool IsCustomStationSpecIndex(TileIndex t)
Is there a custom rail station spec on this tile?
Definition: station_map.h:469
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
static bool IsAirportTile(TileIndex t)
Is this tile a station tile and an airport tile?
Definition: station_map.h:167
static bool IsRailStation(TileIndex t)
Is this station tile a rail station?
Definition: station_map.h:92
static RoadStopType GetRoadStopType(TileIndex t)
Get the road stop type of this tile.
Definition: station_map.h:56
byte m7
Primarily used for newgrf support.
Definition: map_type.h:35
Map accessors for roads.
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:20
Used for iterations.
static TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:96
Rail specific functions.
Tile * _m
Tiles of the map.
Definition: map.cpp:30
Road specific functions.
static bool IsRoadStop(TileIndex t)
Is the station at t a road station?
Definition: station_map.h:202
static DiagDirection GetDockDirection(TileIndex t)
Get the direction of a dock.
Definition: station_map.h:429
A standard stop for buses.
Definition: station_type.h:45
static void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
Make the given tile an oilrig tile.
Definition: station_map.h:665
static StationGfx GetAirportGfx(TileIndex t)
Get the station graphics of this airport tile.
Definition: station_map.h:244
static bool IsRailWaypointTile(TileIndex t)
Is this tile a station tile and a rail waypoint?
Definition: station_map.h:123
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:405
static Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track AXIS_X -> TRACK_X AXIS_Y -> TRACK_Y Uses the fact that the...
Definition: track_func.h:74
static Track GetRailStationTrack(TileIndex t)
Get the rail track of a rail station tile.
Definition: station_map.h:349
static void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
Make the given tile a buoy tile.
Definition: station_map.h:637
static bool IsStandardRoadStopTile(TileIndex t)
Is tile t a standard (non-drive through) road stop station?
Definition: station_map.h:223
static void SetWaterClass(TileIndex t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:118
RoadType
The different roadtypes we support.
Definition: road_type.h:22
static bool IsDriveThroughStopTile(TileIndex t)
Is tile t a drive through road stop station?
Definition: station_map.h:233
static void SetRoadTypes(TileIndex t, RoadType road_rt, RoadType tram_rt)
Set the present road types of a tile.
Definition: road_map.h:619
static StationType GetStationType(TileIndex t)
Get the station type of this tile.
Definition: station_map.h:44
RoadStopType
Types of RoadStops.
Definition: station_type.h:44
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
static uint GetCustomStationSpecIndex(TileIndex t)
Get the custom station spec for this tile.
Definition: station_map.h:493
static bool HasStationRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:135
static bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
Check if a tile is a valid continuation to a railstation tile.
Definition: station_map.h:378
The tile has no ownership.
Definition: company_type.h:25
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
static void SetStationTileRandomBits(TileIndex t, byte random_bits)
Set the random bits for a station tile.
Definition: station_map.h:505
static bool IsBuoy(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:306
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
static void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
Make the given tile an airport tile.
Definition: station_map.h:626
static const int GFX_DOCK_BASE_WATER_PART
The offset for the water parts.
Definition: station_map.h:35
The y axis.
static void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
Make the given tile a rail waypoint tile.
Definition: station_map.h:573
static bool HasStationReservation(TileIndex t)
Get the reservation state of the rail station.
Definition: station_map.h:393
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:102
static void SetStationGfx(TileIndex t, StationGfx gfx)
Set the station graphics of this tile.
Definition: station_map.h:80
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
static bool IsTruckStop(TileIndex t)
Is the station at t a truck stop?
Definition: station_map.h:180
static DiagDirection GetRoadStopDir(TileIndex t)
Gets the direction the road stop entrance points towards.
Definition: station_map.h:257
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
bool IsHangar(TileIndex t)
Check whether the given tile is a hangar.
Definition: station_cmd.cpp:76
static TrackBits GetRailStationTrackBits(TileIndex t)
Get the trackbits of a rail station tile.
Definition: station_map.h:360
Map accessors for water tiles.
static bool HasStationTileRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:146
static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
The offset for the drive through parts.
Definition: station_map.h:36
static bool IsDockTile(TileIndex t)
Is tile t a dock tile?
Definition: station_map.h:295
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:31
static Axis GetRailStationAxis(TileIndex t)
Get the rail direction of a rail station.
Definition: station_map.h:337
byte m5
General purpose.
Definition: map_type.h:24
static bool IsBuoyTile(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:316
static bool IsAirport(TileIndex t)
Is this station tile an airport?
Definition: station_map.h:157
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:38
static void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc=WATER_CLASS_INVALID)
Make the given tile a station tile.
Definition: station_map.h:532
static void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
Make the given tile a roadstop tile.
Definition: station_map.h:590
Functions related to stations.
byte StationGfx
Index of station graphics.
Definition: station_map.h:20
static StationGfx GetStationGfx(TileIndex t)
Get the station graphics of this tile.
Definition: station_map.h:68
static void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, Axis a)
Make the given tile a drivethrough roadstop tile.
Definition: station_map.h:610
static void SetRoadOwner(TileIndex t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition: road_map.h:250
A pair-construct of a TileIndexDiff.
Definition: map_type.h:57
The X axis.
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
byte StationGfx
Copy from station_map.h.
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:28
No track.
Definition: track_type.h:39
StationType
Station types.
Definition: station_type.h:32
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
bool IsStationTileBlocked(TileIndex tile)
Check whether a rail station tile is NOT traversable.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Track
These are used to specify a single track.
Definition: track_type.h:19
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static bool IsOilRig(TileIndex t)
Is tile t part of an oilrig?
Definition: station_map.h:274
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
A tile of a station.
Definition: tile_type.h:46
static bool IsCompatibleRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType can drive on a tile with a given RailType.
Definition: rail.h:319
static bool IsRailWaypoint(TileIndex t)
Is this station tile a rail waypoint?
Definition: station_map.h:113
uint16 m8
General purpose.
Definition: map_type.h:36
static bool IsRoadStopTile(TileIndex t)
Is tile t a road stop station?
Definition: station_map.h:213
static TileIndexDiffC GetDockOffset(TileIndex t)
Get the tileoffset from this tile a ship should target to get to this dock.
Definition: station_map.h:443
static bool IsHangarTile(TileIndex t)
Is tile t an hangar tile?
Definition: station_map.h:326
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
DiagDirection
Enumeration for diagonal directions.
StationGfx GetTranslatedAirportTileID(StationGfx gfx)
Do airporttile gfx ID translation for NewGRFs.
static bool IsDock(TileIndex t)
Is tile t a dock tile?
Definition: station_map.h:285
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:355
static byte GetStationTileRandomBits(TileIndex t)
Get the random bits of a station tile.
Definition: station_map.h:517
byte m3
General purpose.
Definition: map_type.h:22
static bool IsBusStop(TileIndex t)
Is the station at t a bus stop?
Definition: station_map.h:191
Hides the direct accesses to the map array with map accessors.
Axis
Allow incrementing of DiagDirDiff variables.
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:51
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:125
byte m4
General purpose.
Definition: map_type.h:23