OpenTTD
station_map.h
Go to the documentation of this file.
1 /* $Id: station_map.h 26878 2014-09-21 11:23:33Z rubidium $ */
2 
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  */
9 
12 #ifndef STATION_MAP_H
13 #define STATION_MAP_H
14 
15 #include "rail_map.h"
16 #include "road_map.h"
17 #include "water_map.h"
18 #include "station_func.h"
19 #include "rail.h"
20 
21 typedef byte StationGfx;
22 
29 static inline StationID GetStationIndex(TileIndex t)
30 {
31  assert(IsTileType(t, MP_STATION));
32  return (StationID)_m[t].m2;
33 }
34 
35 
36 static const int GFX_DOCK_BASE_WATER_PART = 4;
37 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET = 4;
38 
46 {
47  assert(IsTileType(t, MP_STATION));
48  return (StationType)GB(_me[t].m6, 3, 3);
49 }
50 
58 {
59  assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
60  return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
61 }
62 
70 {
71  assert(IsTileType(t, MP_STATION));
72  return _m[t].m5;
73 }
74 
81 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
82 {
83  assert(IsTileType(t, MP_STATION));
84  _m[t].m5 = gfx;
85 }
86 
93 static inline bool IsRailStation(TileIndex t)
94 {
95  return GetStationType(t) == STATION_RAIL;
96 }
97 
103 static inline bool IsRailStationTile(TileIndex t)
104 {
105  return IsTileType(t, MP_STATION) && IsRailStation(t);
106 }
107 
114 static inline bool IsRailWaypoint(TileIndex t)
115 {
116  return GetStationType(t) == STATION_WAYPOINT;
117 }
118 
124 static inline bool IsRailWaypointTile(TileIndex t)
125 {
126  return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
127 }
128 
136 static inline bool HasStationRail(TileIndex t)
137 {
138  return IsRailStation(t) || IsRailWaypoint(t);
139 }
140 
147 static inline bool HasStationTileRail(TileIndex t)
148 {
149  return IsTileType(t, MP_STATION) && HasStationRail(t);
150 }
151 
158 static inline bool IsAirport(TileIndex t)
159 {
160  return GetStationType(t) == STATION_AIRPORT;
161 }
162 
168 static inline bool IsAirportTile(TileIndex t)
169 {
170  return IsTileType(t, MP_STATION) && IsAirport(t);
171 }
172 
173 bool IsHangar(TileIndex t);
174 
181 static inline bool IsTruckStop(TileIndex t)
182 {
183  return GetStationType(t) == STATION_TRUCK;
184 }
185 
192 static inline bool IsBusStop(TileIndex t)
193 {
194  return GetStationType(t) == STATION_BUS;
195 }
196 
203 static inline bool IsRoadStop(TileIndex t)
204 {
205  assert(IsTileType(t, MP_STATION));
206  return IsTruckStop(t) || IsBusStop(t);
207 }
208 
214 static inline bool IsRoadStopTile(TileIndex t)
215 {
216  return IsTileType(t, MP_STATION) && IsRoadStop(t);
217 }
218 
224 static inline bool IsStandardRoadStopTile(TileIndex t)
225 {
227 }
228 
234 static inline bool IsDriveThroughStopTile(TileIndex t)
235 {
237 }
238 
246 {
247  assert(IsAirport(t));
250 }
251 
259 {
260  StationGfx gfx = GetStationGfx(t);
261  assert(IsRoadStopTile(t));
263  return (DiagDirection)(gfx);
264  } else {
266  }
267 }
268 
275 static inline bool IsOilRig(TileIndex t)
276 {
277  return GetStationType(t) == STATION_OILRIG;
278 }
279 
286 static inline bool IsDock(TileIndex t)
287 {
288  return GetStationType(t) == STATION_DOCK;
289 }
290 
296 static inline bool IsDockTile(TileIndex t)
297 {
298  return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
299 }
300 
307 static inline bool IsBuoy(TileIndex t)
308 {
309  return GetStationType(t) == STATION_BUOY;
310 }
311 
317 static inline bool IsBuoyTile(TileIndex t)
318 {
319  return IsTileType(t, MP_STATION) && IsBuoy(t);
320 }
321 
327 static inline bool IsHangarTile(TileIndex t)
328 {
329  return IsTileType(t, MP_STATION) && IsHangar(t);
330 }
331 
339 {
340  assert(HasStationRail(t));
341  return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
342 }
343 
351 {
352  return AxisToTrack(GetRailStationAxis(t));
353 }
354 
362 {
364 }
365 
379 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
380 {
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);
386 }
387 
394 static inline bool HasStationReservation(TileIndex t)
395 {
396  assert(HasStationRail(t));
397  return HasBit(_me[t].m6, 2);
398 }
399 
406 static inline void SetRailStationReservation(TileIndex t, bool b)
407 {
408  assert(HasStationRail(t));
409  SB(_me[t].m6, 2, 1, b ? 1 : 0);
410 }
411 
419 {
421 }
422 
431 {
432  StationGfx gfx = GetStationGfx(t);
433  assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
434  return (DiagDirection)(gfx);
435 }
436 
445 {
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},
453  };
454  assert(IsTileType(t, MP_STATION));
455 
456  if (IsBuoy(t)) return buoy_offset;
457  if (IsOilRig(t)) return oilrig_offset;
458 
459  assert(IsDock(t));
460 
461  return dock_offset[GetDockDirection(t)];
462 }
463 
470 static inline bool IsCustomStationSpecIndex(TileIndex t)
471 {
472  assert(HasStationTileRail(t));
473  return _m[t].m4 != 0;
474 }
475 
482 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
483 {
484  assert(HasStationTileRail(t));
485  _m[t].m4 = specindex;
486 }
487 
495 {
496  assert(HasStationTileRail(t));
497  return _m[t].m4;
498 }
499 
506 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
507 {
508  assert(IsTileType(t, MP_STATION));
509  SB(_m[t].m3, 4, 4, random_bits);
510 }
511 
518 static inline byte GetStationTileRandomBits(TileIndex t)
519 {
520  assert(IsTileType(t, MP_STATION));
521  return GB(_m[t].m3, 4, 4);
522 }
523 
533 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
534 {
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;
545 }
546 
556 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
557 {
558  MakeStation(t, o, sid, STATION_RAIL, section + a);
559  SetRailType(t, rt);
560  SetRailStationReservation(t, false);
561 }
562 
572 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
573 {
574  MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
575  SetRailType(t, rt);
576  SetRailStationReservation(t, false);
577 }
578 
588 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
589 {
590  MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
591  SetRoadTypes(t, rt);
594 }
595 
607 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
608 {
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);
613 }
614 
623 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
624 {
625  MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
626 }
627 
634 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
635 {
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);
640 }
641 
650 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
651 {
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);
654 }
655 
662 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
663 {
664  MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
665 }
666 
667 #endif /* STATION_MAP_H */
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:556
static void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
Make the given tile a dock tile.
Definition: station_map.h:650
static TrackBits GetStationReservationTrackBits(TileIndex t)
Get the reserved track bits for a waypoint.
Definition: station_map.h:418
static void SetCustomStationSpecIndex(TileIndex t, byte specindex)
Set the custom station spec for this tile.
Definition: station_map.h:482
A standard stop for trucks.
Definition: station_type.h:49
static bool IsCustomStationSpecIndex(TileIndex t)
Is there a custom rail station spec on this tile?
Definition: station_map.h:470
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:191
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
static bool IsAirportTile(TileIndex t)
Is this tile a station tile and an airport tile?
Definition: station_map.h:168
static bool IsRailStation(TileIndex t)
Is this station tile a rail station?
Definition: station_map.h:93
static RoadStopType GetRoadStopType(TileIndex t)
Get the road stop type of this tile.
Definition: station_map.h:57
Basic road type.
Definition: road_type.h:24
byte m7
Primarily used for newgrf support.
Definition: map_type.h:37
Map accessors for roads.
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:22
Track
These are used to specify a single track.
Definition: track_type.h:21
static TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:98
Rail specific functions.
Tile * _m
Tiles of the map.
Definition: map.cpp:32
static bool IsRoadStop(TileIndex t)
Is the station at t a road station?
Definition: station_map.h:203
static DiagDirection GetDockDirection(TileIndex t)
Get the direction of a dock.
Definition: station_map.h:430
A standard stop for buses.
Definition: station_type.h:48
static void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
Make the given tile an oilrig tile.
Definition: station_map.h:662
static StationGfx GetAirportGfx(TileIndex t)
Get the station graphics of this airport tile.
Definition: station_map.h:245
static bool IsRailWaypointTile(TileIndex t)
Is this tile a station tile and a rail waypoint?
Definition: station_map.h:124
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:406
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:76
static Track GetRailStationTrack(TileIndex t)
Get the rail track of a rail station tile.
Definition: station_map.h:350
static void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
Make the given tile a buoy tile.
Definition: station_map.h:634
static bool IsStandardRoadStopTile(TileIndex t)
Is tile t a standard (non-drive through) road stop station?
Definition: station_map.h:224
static void SetWaterClass(TileIndex t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:118
static bool IsDriveThroughStopTile(TileIndex t)
Is tile t a drive through road stop station?
Definition: station_map.h:234
static StationType GetStationType(TileIndex t)
Get the station type of this tile.
Definition: station_map.h:45
RoadStopType
Types of RoadStops.
Definition: station_type.h:47
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:494
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:136
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:379
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
static void SetStationTileRandomBits(TileIndex t, byte random_bits)
Set the random bits for a station tile.
Definition: station_map.h:506
static bool IsBuoy(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:307
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
static void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
Make the given tile an airport tile.
Definition: station_map.h:623
static const int GFX_DOCK_BASE_WATER_PART
The offset for the water parts.
Definition: station_map.h:36
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:572
static bool HasStationReservation(TileIndex t)
Get the reservation state of the rail station.
Definition: station_map.h:394
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:103
static void SetStationGfx(TileIndex t, StationGfx gfx)
Set the station graphics of this tile.
Definition: station_map.h:81
static void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
Set the owner of a specific road type.
Definition: road_map.h:220
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:49
static bool IsTruckStop(TileIndex t)
Is the station at t a truck stop?
Definition: station_map.h:181
static DiagDirection GetRoadStopDir(TileIndex t)
Gets the direction the road stop entrance points towards.
Definition: station_map.h:258
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:171
bool IsHangar(TileIndex t)
Check whether the given tile is a hangar.
Definition: station_cmd.cpp:74
static TrackBits GetRailStationTrackBits(TileIndex t)
Get the trackbits of a rail station tile.
Definition: station_map.h:361
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:147
static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
The offset for the drive through parts.
Definition: station_map.h:37
Trams.
Definition: road_type.h:25
static bool IsDockTile(TileIndex t)
Is tile t a dock tile?
Definition: station_map.h:296
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:33
static Axis GetRailStationAxis(TileIndex t)
Get the rail direction of a rail station.
Definition: station_map.h:338
byte m5
General purpose.
Definition: map_type.h:26
Used for iterations.
static bool IsBuoyTile(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:317
static bool IsAirport(TileIndex t)
Is this station tile an airport?
Definition: station_map.h:158
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:533
The tile has no ownership.
Definition: company_type.h:27
Functions related to stations.
byte StationGfx
Index of station graphics.
Definition: station_map.h:21
DiagDirection
Enumeration for diagonal directions.
static StationGfx GetStationGfx(TileIndex t)
Get the station graphics of this tile.
Definition: station_map.h:69
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
A pair-construct of a TileIndexDiff.
Definition: map_type.h:58
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:29
StationType
Station types.
Definition: station_type.h:35
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:80
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:275
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:124
A tile of a station.
Definition: tile_type.h:48
static void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
Make the given tile a roadstop tile.
Definition: station_map.h:588
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:311
static bool IsRailWaypoint(TileIndex t)
Is this station tile a rail waypoint?
Definition: station_map.h:114
static bool IsRoadStopTile(TileIndex t)
Is tile t a road stop station?
Definition: station_map.h:214
static TileIndexDiffC GetDockOffset(TileIndex t)
Get the tileoffset from this tile a ship should target to get to this dock.
Definition: station_map.h:444
static bool IsHangarTile(TileIndex t)
Is tile t an hangar tile?
Definition: station_map.h:327
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
StationGfx GetTranslatedAirportTileID(StationGfx gfx)
Do airporttile gfx ID translation for NewGRFs.
static void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadTypes rt, Axis a)
Make the given tile a drivethrough roadstop tile.
Definition: station_map.h:607
static void SetRoadTypes(TileIndex t, RoadTypes rt)
Set the present road types of a tile.
Definition: road_map.h:176
static bool IsDock(TileIndex t)
Is tile t a dock tile?
Definition: station_map.h:286
static byte GetStationTileRandomBits(TileIndex t)
Get the random bits of a station tile.
Definition: station_map.h:518
byte m3
General purpose.
Definition: map_type.h:24
static bool IsBusStop(TileIndex t)
Is the station at t a bus stop?
Definition: station_map.h:192
No track.
Definition: track_type.h:42
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:53
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:116
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:126
byte m4
General purpose.
Definition: map_type.h:25