OpenTTD
station_map.h
Go to the documentation of this file.
1 /* $Id$ */
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 #include "road.h"
21 
22 typedef byte StationGfx;
23 
30 static inline StationID GetStationIndex(TileIndex t)
31 {
32  assert(IsTileType(t, MP_STATION));
33  return (StationID)_m[t].m2;
34 }
35 
36 
37 static const int GFX_DOCK_BASE_WATER_PART = 4;
38 static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET = 4;
39 
47 {
48  assert(IsTileType(t, MP_STATION));
49  return (StationType)GB(_me[t].m6, 3, 3);
50 }
51 
59 {
60  assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
61  return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
62 }
63 
71 {
72  assert(IsTileType(t, MP_STATION));
73  return _m[t].m5;
74 }
75 
82 static inline void SetStationGfx(TileIndex t, StationGfx gfx)
83 {
84  assert(IsTileType(t, MP_STATION));
85  _m[t].m5 = gfx;
86 }
87 
94 static inline bool IsRailStation(TileIndex t)
95 {
96  return GetStationType(t) == STATION_RAIL;
97 }
98 
104 static inline bool IsRailStationTile(TileIndex t)
105 {
106  return IsTileType(t, MP_STATION) && IsRailStation(t);
107 }
108 
115 static inline bool IsRailWaypoint(TileIndex t)
116 {
117  return GetStationType(t) == STATION_WAYPOINT;
118 }
119 
125 static inline bool IsRailWaypointTile(TileIndex t)
126 {
127  return IsTileType(t, MP_STATION) && IsRailWaypoint(t);
128 }
129 
137 static inline bool HasStationRail(TileIndex t)
138 {
139  return IsRailStation(t) || IsRailWaypoint(t);
140 }
141 
148 static inline bool HasStationTileRail(TileIndex t)
149 {
150  return IsTileType(t, MP_STATION) && HasStationRail(t);
151 }
152 
159 static inline bool IsAirport(TileIndex t)
160 {
161  return GetStationType(t) == STATION_AIRPORT;
162 }
163 
169 static inline bool IsAirportTile(TileIndex t)
170 {
171  return IsTileType(t, MP_STATION) && IsAirport(t);
172 }
173 
174 bool IsHangar(TileIndex t);
175 
182 static inline bool IsTruckStop(TileIndex t)
183 {
184  return GetStationType(t) == STATION_TRUCK;
185 }
186 
193 static inline bool IsBusStop(TileIndex t)
194 {
195  return GetStationType(t) == STATION_BUS;
196 }
197 
204 static inline bool IsRoadStop(TileIndex t)
205 {
206  assert(IsTileType(t, MP_STATION));
207  return IsTruckStop(t) || IsBusStop(t);
208 }
209 
215 static inline bool IsRoadStopTile(TileIndex t)
216 {
217  return IsTileType(t, MP_STATION) && IsRoadStop(t);
218 }
219 
225 static inline bool IsStandardRoadStopTile(TileIndex t)
226 {
228 }
229 
235 static inline bool IsDriveThroughStopTile(TileIndex t)
236 {
238 }
239 
247 {
248  assert(IsAirport(t));
251 }
252 
260 {
261  StationGfx gfx = GetStationGfx(t);
262  assert(IsRoadStopTile(t));
264  return (DiagDirection)(gfx);
265  } else {
267  }
268 }
269 
276 static inline bool IsOilRig(TileIndex t)
277 {
278  return GetStationType(t) == STATION_OILRIG;
279 }
280 
287 static inline bool IsDock(TileIndex t)
288 {
289  return GetStationType(t) == STATION_DOCK;
290 }
291 
297 static inline bool IsDockTile(TileIndex t)
298 {
299  return IsTileType(t, MP_STATION) && GetStationType(t) == STATION_DOCK;
300 }
301 
308 static inline bool IsBuoy(TileIndex t)
309 {
310  return GetStationType(t) == STATION_BUOY;
311 }
312 
318 static inline bool IsBuoyTile(TileIndex t)
319 {
320  return IsTileType(t, MP_STATION) && IsBuoy(t);
321 }
322 
328 static inline bool IsHangarTile(TileIndex t)
329 {
330  return IsTileType(t, MP_STATION) && IsHangar(t);
331 }
332 
340 {
341  assert(HasStationRail(t));
342  return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
343 }
344 
352 {
353  return AxisToTrack(GetRailStationAxis(t));
354 }
355 
363 {
365 }
366 
380 static inline bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
381 {
382  assert(IsRailStationTile(station_tile));
383  return IsRailStationTile(test_tile) && IsCompatibleRail(GetRailType(test_tile), GetRailType(station_tile)) &&
384  GetRailStationAxis(test_tile) == GetRailStationAxis(station_tile) &&
385  GetStationIndex(test_tile) == GetStationIndex(station_tile) &&
386  !IsStationTileBlocked(test_tile);
387 }
388 
395 static inline bool HasStationReservation(TileIndex t)
396 {
397  assert(HasStationRail(t));
398  return HasBit(_me[t].m6, 2);
399 }
400 
407 static inline void SetRailStationReservation(TileIndex t, bool b)
408 {
409  assert(HasStationRail(t));
410  SB(_me[t].m6, 2, 1, b ? 1 : 0);
411 }
412 
420 {
422 }
423 
432 {
433  StationGfx gfx = GetStationGfx(t);
434  assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
435  return (DiagDirection)(gfx);
436 }
437 
446 {
447  static const TileIndexDiffC buoy_offset = {0, 0};
448  static const TileIndexDiffC oilrig_offset = {2, 0};
449  static const TileIndexDiffC dock_offset[DIAGDIR_END] = {
450  {-2, 0},
451  { 0, 2},
452  { 2, 0},
453  { 0, -2},
454  };
455  assert(IsTileType(t, MP_STATION));
456 
457  if (IsBuoy(t)) return buoy_offset;
458  if (IsOilRig(t)) return oilrig_offset;
459 
460  assert(IsDock(t));
461 
462  return dock_offset[GetDockDirection(t)];
463 }
464 
471 static inline bool IsCustomStationSpecIndex(TileIndex t)
472 {
473  assert(HasStationTileRail(t));
474  return _m[t].m4 != 0;
475 }
476 
483 static inline void SetCustomStationSpecIndex(TileIndex t, byte specindex)
484 {
485  assert(HasStationTileRail(t));
486  _m[t].m4 = specindex;
487 }
488 
496 {
497  assert(HasStationTileRail(t));
498  return _m[t].m4;
499 }
500 
507 static inline void SetStationTileRandomBits(TileIndex t, byte random_bits)
508 {
509  assert(IsTileType(t, MP_STATION));
510  SB(_m[t].m3, 4, 4, random_bits);
511 }
512 
519 static inline byte GetStationTileRandomBits(TileIndex t)
520 {
521  assert(IsTileType(t, MP_STATION));
522  return GB(_m[t].m3, 4, 4);
523 }
524 
534 static inline void MakeStation(TileIndex t, Owner o, StationID sid, StationType st, byte section, WaterClass wc = WATER_CLASS_INVALID)
535 {
537  SetTileOwner(t, o);
538  SetWaterClass(t, wc);
539  SetDockingTile(t, false);
540  _m[t].m2 = sid;
541  _m[t].m3 = 0;
542  _m[t].m4 = 0;
543  _m[t].m5 = section;
544  SB(_me[t].m6, 2, 1, 0);
545  SB(_me[t].m6, 3, 3, st);
546  _me[t].m7 = 0;
547  _me[t].m8 = 0;
548 }
549 
559 static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
560 {
561  MakeStation(t, o, sid, STATION_RAIL, section + a);
562  SetRailType(t, rt);
563  SetRailStationReservation(t, false);
564 }
565 
575 static inline void MakeRailWaypoint(TileIndex t, Owner o, StationID sid, Axis a, byte section, RailType rt)
576 {
577  MakeStation(t, o, sid, STATION_WAYPOINT, section + a);
578  SetRailType(t, rt);
579  SetRailStationReservation(t, false);
580 }
581 
592 static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, DiagDirection d)
593 {
594  MakeStation(t, o, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), d);
595  SetRoadTypes(t, road_rt, tram_rt);
596  SetRoadOwner(t, RTT_ROAD, o);
597  SetRoadOwner(t, RTT_TRAM, o);
598 }
599 
612 static inline void MakeDriveThroughRoadStop(TileIndex t, Owner station, Owner road, Owner tram, StationID sid, RoadStopType rst, RoadType road_rt, RoadType tram_rt, Axis a)
613 {
614  MakeStation(t, station, sid, (rst == ROADSTOP_BUS ? STATION_BUS : STATION_TRUCK), GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET + a);
615  SetRoadTypes(t, road_rt, tram_rt);
616  SetRoadOwner(t, RTT_ROAD, road);
617  SetRoadOwner(t, RTT_TRAM, tram);
618 }
619 
628 static inline void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
629 {
630  MakeStation(t, o, sid, STATION_AIRPORT, section, wc);
631 }
632 
639 static inline void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
640 {
641  /* Make the owner of the buoy tile the same as the current owner of the
642  * water tile. In this way, we can reset the owner of the water to its
643  * original state when the buoy gets removed. */
644  MakeStation(t, GetTileOwner(t), sid, STATION_BUOY, 0, wc);
645 }
646 
655 static inline void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
656 {
657  MakeStation(t, o, sid, STATION_DOCK, d);
658  MakeStation(t + TileOffsByDiagDir(d), o, sid, STATION_DOCK, GFX_DOCK_BASE_WATER_PART + DiagDirToAxis(d), wc);
659 }
660 
667 static inline void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
668 {
669  MakeStation(t, OWNER_NONE, sid, STATION_OILRIG, 0, wc);
670 }
671 
672 #endif /* STATION_MAP_H */
Owner
Enum for all companies/owners.
Definition: company_type.h:20
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:559
static void MakeDock(TileIndex t, Owner o, StationID sid, DiagDirection d, WaterClass wc)
Make the given tile a dock tile.
Definition: station_map.h:655
static TrackBits GetStationReservationTrackBits(TileIndex t)
Get the reserved track bits for a waypoint.
Definition: station_map.h:419
static void SetCustomStationSpecIndex(TileIndex t, byte specindex)
Set the custom station spec for this tile.
Definition: station_map.h:483
A standard stop for trucks.
Definition: station_type.h:48
static bool IsCustomStationSpecIndex(TileIndex t)
Is there a custom rail station spec on this tile?
Definition: station_map.h:471
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:200
static bool IsAirportTile(TileIndex t)
Is this tile a station tile and an airport tile?
Definition: station_map.h:169
static bool IsRailStation(TileIndex t)
Is this station tile a rail station?
Definition: station_map.h:94
static RoadStopType GetRoadStopType(TileIndex t)
Get the road stop type of this tile.
Definition: station_map.h:58
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
Used for iterations.
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
Road specific functions.
static bool IsRoadStop(TileIndex t)
Is the station at t a road station?
Definition: station_map.h:204
static DiagDirection GetDockDirection(TileIndex t)
Get the direction of a dock.
Definition: station_map.h:431
A standard stop for buses.
Definition: station_type.h:47
static void MakeOilrig(TileIndex t, StationID sid, WaterClass wc)
Make the given tile an oilrig tile.
Definition: station_map.h:667
static StationGfx GetAirportGfx(TileIndex t)
Get the station graphics of this airport tile.
Definition: station_map.h:246
static bool IsRailWaypointTile(TileIndex t)
Is this tile a station tile and a rail waypoint?
Definition: station_map.h:125
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:407
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:351
static void MakeBuoy(TileIndex t, StationID sid, WaterClass wc)
Make the given tile a buoy tile.
Definition: station_map.h:639
static bool IsStandardRoadStopTile(TileIndex t)
Is tile t a standard (non-drive through) road stop station?
Definition: station_map.h:225
static void SetWaterClass(TileIndex t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:120
RoadType
The different roadtypes we support.
Definition: road_type.h:27
static bool IsDriveThroughStopTile(TileIndex t)
Is tile t a drive through road stop station?
Definition: station_map.h:235
static void SetRoadTypes(TileIndex t, RoadType road_rt, RoadType tram_rt)
Set the present road types of a tile.
Definition: road_map.h:621
static StationType GetStationType(TileIndex t)
Get the station type of this tile.
Definition: station_map.h:46
RoadStopType
Types of RoadStops.
Definition: station_type.h:46
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:495
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:137
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:380
The tile has no ownership.
Definition: company_type.h:27
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:507
static bool IsBuoy(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:308
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
static void MakeAirport(TileIndex t, Owner o, StationID sid, byte section, WaterClass wc)
Make the given tile an airport tile.
Definition: station_map.h:628
static const int GFX_DOCK_BASE_WATER_PART
The offset for the water parts.
Definition: station_map.h:37
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:575
static bool HasStationReservation(TileIndex t)
Get the reservation state of the rail station.
Definition: station_map.h:395
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:104
static void SetStationGfx(TileIndex t, StationGfx gfx)
Set the station graphics of this tile.
Definition: station_map.h:82
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:182
static DiagDirection GetRoadStopDir(TileIndex t)
Gets the direction the road stop entrance points towards.
Definition: station_map.h:259
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:180
bool IsHangar(TileIndex t)
Check whether the given tile is a hangar.
Definition: station_cmd.cpp:78
static TrackBits GetRailStationTrackBits(TileIndex t)
Get the trackbits of a rail station tile.
Definition: station_map.h:362
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:148
static const int GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET
The offset for the drive through parts.
Definition: station_map.h:38
static bool IsDockTile(TileIndex t)
Is tile t a dock tile?
Definition: station_map.h:297
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:339
byte m5
General purpose.
Definition: map_type.h:26
static bool IsBuoyTile(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:318
static bool IsAirport(TileIndex t)
Is this station tile an airport?
Definition: station_map.h:159
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:40
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:534
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:592
Functions related to stations.
byte StationGfx
Index of station graphics.
Definition: station_map.h:22
static StationGfx GetStationGfx(TileIndex t)
Get the station graphics of this tile.
Definition: station_map.h:70
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:612
static void SetRoadOwner(TileIndex t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition: road_map.h:252
A pair-construct of a TileIndexDiff.
Definition: map_type.h:59
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:30
No track.
Definition: track_type.h:41
StationType
Station types.
Definition: station_type.h:34
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
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
Track
These are used to specify a single track.
Definition: track_type.h:21
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:276
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:133
A tile of a station.
Definition: tile_type.h:48
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:321
static bool IsRailWaypoint(TileIndex t)
Is this station tile a rail waypoint?
Definition: station_map.h:115
uint16 m8
General purpose.
Definition: map_type.h:38
static bool IsRoadStopTile(TileIndex t)
Is tile t a road stop station?
Definition: station_map.h:215
static TileIndexDiffC GetDockOffset(TileIndex t)
Get the tileoffset from this tile a ship should target to get to this dock.
Definition: station_map.h:445
static bool IsHangarTile(TileIndex t)
Is tile t an hangar tile?
Definition: station_map.h:328
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:287
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:357
static byte GetStationTileRandomBits(TileIndex t)
Get the random bits of a station tile.
Definition: station_map.h:519
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:193
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:117
static void SetRailType(TileIndex t, RailType r)
Sets the rail type of the given tile.
Definition: rail_map.h:127
byte m4
General purpose.
Definition: map_type.h:25