OpenTTD: src/station_map.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
45 static inline StationType GetStationType(TileIndex t)
46 {
47 assert(IsTileType(t, MP_STATION));
48 return (StationType)GB(_me[t].m6, 3, 3);
49 }
50
57 static inline RoadStopType GetRoadStopType(TileIndex t)
58 {
59 assert(GetStationType(t) == STATION_TRUCK || GetStationType(t) == STATION_BUS);
60 return GetStationType(t) == STATION_TRUCK ? ROADSTOP_TRUCK : ROADSTOP_BUS;
61 }
62
69 static inline StationGfx GetStationGfx(TileIndex t)
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 {
226 return IsRoadStopTile(t) && GetStationGfx(t) < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
227 }
228
234 static inline bool IsDriveThroughStopTile(TileIndex t)
235 {
236 return IsRoadStopTile(t) && GetStationGfx(t) >= GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET;
237 }
238
245 static inline StationGfx GetAirportGfx(TileIndex t)
246 {
247 assert(IsAirport(t));
248 extern StationGfx GetTranslatedAirportTileID(StationGfx gfx);
249 return GetTranslatedAirportTileID(GetStationGfx(t));
250 }
251
258 static inline DiagDirection GetRoadStopDir(TileIndex t)
259 {
260 StationGfx gfx = GetStationGfx(t);
261 assert(IsRoadStopTile(t));
262 if (gfx < GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET) {
263 return (DiagDirection)(gfx);
264 } else {
265 return (DiagDirection)(gfx - GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
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
338 static inline Axis GetRailStationAxis(TileIndex t)
339 {
340 assert(HasStationRail(t));
341 return HasBit(GetStationGfx(t), 0) ? AXIS_Y : AXIS_X;
342 }
343
350 static inline Track GetRailStationTrack(TileIndex t)
351 {
352 return AxisToTrack(GetRailStationAxis(t));
353 }
354
361 static inline TrackBits GetRailStationTrackBits(TileIndex t)
362 {
363 return AxisToTrackBits(GetRailStationAxis(t));
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
418 static inline TrackBits GetStationReservationTrackBits(TileIndex t)
419 {
420 return HasStationReservation(t) ? GetRailStationTrackBits(t) : TRACK_BIT_NONE;
421 }
422
430 static inline DiagDirection GetDockDirection(TileIndex t)
431 {
432 StationGfx gfx = GetStationGfx(t);
433 assert(IsDock(t) && gfx < GFX_DOCK_BASE_WATER_PART);
434 return (DiagDirection)(gfx);
435 }
436
444 static inline TileIndexDiffC GetDockOffset(TileIndex t)
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
494 static inline uint GetCustomStationSpecIndex(TileIndex t)
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 {
535 SetTileType(t, MP_STATION);
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);
592 SetRoadOwner(t, ROADTYPE_ROAD, o);
593 SetRoadOwner(t, ROADTYPE_TRAM, o);
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 */