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/>.
8 /** @file road_map.h Map accessors for roads. */
13 #include "track_func.h"
14 #include "depot_type.h"
15 #include "rail_type.h"
16 #include "road_func.h"
18 #include "road_type.h"
21 /** The different types of road tiles. */
23 ROAD_TILE_NORMAL
, ///< Normal road
24 ROAD_TILE_CROSSING
, ///< Level crossing
25 ROAD_TILE_DEPOT
, ///< Depot (one entrance)
29 * Test whether a tile can have road/tram types.
30 * @param t Tile to query.
31 * @return true if tile can be queried about road/tram types.
33 inline bool MayHaveRoad(Tile t
)
35 switch (GetTileType(t
)) {
47 * Get the type of the road tile.
48 * @param t Tile to query.
49 * @pre IsTileType(t, MP_ROAD)
50 * @return The road tile type.
52 debug_inline
static RoadTileType
GetRoadTileType(Tile t
)
54 assert(IsTileType(t
, MP_ROAD
));
55 return (RoadTileType
)GB(t
.m5(), 6, 2);
59 * Return whether a tile is a normal road.
60 * @param t Tile to query.
61 * @pre IsTileType(t, MP_ROAD)
62 * @return True if normal road.
64 debug_inline
static bool IsNormalRoad(Tile t
)
66 return GetRoadTileType(t
) == ROAD_TILE_NORMAL
;
70 * Return whether a tile is a normal road tile.
71 * @param t Tile to query.
72 * @return True if normal road tile.
74 debug_inline
static bool IsNormalRoadTile(Tile t
)
76 return IsTileType(t
, MP_ROAD
) && IsNormalRoad(t
);
80 * Return whether a tile is a level crossing.
81 * @param t Tile to query.
82 * @pre IsTileType(t, MP_ROAD)
83 * @return True if level crossing.
85 inline bool IsLevelCrossing(Tile t
)
87 return GetRoadTileType(t
) == ROAD_TILE_CROSSING
;
91 * Return whether a tile is a level crossing tile.
92 * @param t Tile to query.
93 * @return True if level crossing tile.
95 inline bool IsLevelCrossingTile(Tile t
)
97 return IsTileType(t
, MP_ROAD
) && IsLevelCrossing(t
);
101 * Return whether a tile is a road depot.
102 * @param t Tile to query.
103 * @pre IsTileType(t, MP_ROAD)
104 * @return True if road depot.
106 debug_inline
static bool IsRoadDepot(Tile t
)
108 return GetRoadTileType(t
) == ROAD_TILE_DEPOT
;
112 * Return whether a tile is a road depot tile.
113 * @param t Tile to query.
114 * @return True if road depot tile.
116 debug_inline
static bool IsRoadDepotTile(Tile t
)
118 return IsTileType(t
, MP_ROAD
) && IsRoadDepot(t
);
122 * Get the present road bits for a specific road type.
123 * @param t The tile to query.
124 * @param rt Road type.
125 * @pre IsNormalRoad(t)
126 * @return The present road bits for the road type.
128 inline RoadBits
GetRoadBits(Tile t
, RoadTramType rtt
)
130 assert(IsNormalRoad(t
));
131 if (rtt
== RTT_TRAM
) return (RoadBits
)GB(t
.m3(), 0, 4);
132 return (RoadBits
)GB(t
.m5(), 0, 4);
136 * Get all set RoadBits on the given tile
138 * @param tile The tile from which we want to get the RoadBits
139 * @return all set RoadBits of the tile
141 inline RoadBits
GetAllRoadBits(Tile tile
)
143 return GetRoadBits(tile
, RTT_ROAD
) | GetRoadBits(tile
, RTT_TRAM
);
147 * Set the present road bits for a specific road type.
148 * @param t The tile to change.
149 * @param r The new road bits.
150 * @param rt Road type.
151 * @pre IsNormalRoad(t)
153 inline void SetRoadBits(Tile t
, RoadBits r
, RoadTramType rtt
)
155 assert(IsNormalRoad(t
)); // XXX incomplete
156 if (rtt
== RTT_TRAM
) {
163 inline RoadType
GetRoadTypeRoad(Tile t
)
165 assert(MayHaveRoad(t
));
166 return (RoadType
)GB(t
.m4(), 0, 6);
169 inline RoadType
GetRoadTypeTram(Tile t
)
171 assert(MayHaveRoad(t
));
172 return (RoadType
)GB(t
.m8(), 6, 6);
175 inline RoadType
GetRoadType(Tile t
, RoadTramType rtt
)
177 return (rtt
== RTT_TRAM
) ? GetRoadTypeTram(t
) : GetRoadTypeRoad(t
);
181 * Get the present road types of a tile.
182 * @param t The tile to query.
183 * @return Present road types.
185 inline RoadTypes
GetPresentRoadTypes(Tile t
)
187 RoadTypes result
= ROADTYPES_NONE
;
188 if (MayHaveRoad(t
)) {
189 if (GetRoadTypeRoad(t
) != INVALID_ROADTYPE
) SetBit(result
, GetRoadTypeRoad(t
));
190 if (GetRoadTypeTram(t
) != INVALID_ROADTYPE
) SetBit(result
, GetRoadTypeTram(t
));
195 inline bool HasRoadTypeRoad(Tile t
)
197 return GetRoadTypeRoad(t
) != INVALID_ROADTYPE
;
200 inline bool HasRoadTypeTram(Tile t
)
202 return GetRoadTypeTram(t
) != INVALID_ROADTYPE
;
206 * Check if a tile has a road or a tram road type.
207 * @param t The tile to check.
208 * @param tram True to check tram, false to check road.
209 * @return True if the tile has the specified road type.
211 inline bool HasTileRoadType(Tile t
, RoadTramType rtt
)
213 return GetRoadType(t
, rtt
) != INVALID_ROADTYPE
;
217 * Check if a tile has one of the specified road types.
218 * @param t The tile to check.
219 * @param rts Allowed road types.
220 * @return True if the tile has one of the specified road types.
222 inline bool HasTileAnyRoadType(Tile t
, RoadTypes rts
)
224 if (!MayHaveRoad(t
)) return false;
225 return (GetPresentRoadTypes(t
) & rts
);
229 * Get the owner of a specific road type.
230 * @param t The tile to query.
231 * @param rtt RoadTramType.
232 * @return Owner of the given road type.
234 inline Owner
GetRoadOwner(Tile t
, RoadTramType rtt
)
236 assert(MayHaveRoad(t
));
237 if (rtt
== RTT_ROAD
) return (Owner
)GB(IsNormalRoadTile(t
) ? t
.m1() : t
.m7(), 0, 5);
239 /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
240 * to OWNER_TOWN makes it use one bit less */
241 Owner o
= (Owner
)GB(t
.m3(), 4, 4);
242 return o
== OWNER_TOWN
? OWNER_NONE
: o
;
246 * Set the owner of a specific road type.
247 * @param t The tile to change.
248 * @param rtt RoadTramType.
249 * @param o New owner of the given road type.
251 inline void SetRoadOwner(Tile t
, RoadTramType rtt
, Owner o
)
253 if (rtt
== RTT_ROAD
) {
254 SB(IsNormalRoadTile(t
) ? t
.m1() : t
.m7(), 0, 5, o
);
256 SB(t
.m3(), 4, 4, o
== OWNER_NONE
? OWNER_TOWN
: o
);
261 * Check if a specific road type is owned by an owner.
262 * @param t The tile to query.
263 * @param tram True to check tram, false to check road.
264 * @param o Owner to compare with.
265 * @pre HasTileRoadType(t, rt)
266 * @return True if the road type is owned by the given owner.
268 inline bool IsRoadOwner(Tile t
, RoadTramType rtt
, Owner o
)
270 assert(HasTileRoadType(t
, rtt
));
271 return (GetRoadOwner(t
, rtt
) == o
);
275 * Checks if given tile has town owned road
276 * @param t tile to check
277 * @pre IsTileType(t, MP_ROAD)
278 * @return true iff tile has road and the road is owned by a town
280 inline bool HasTownOwnedRoad(Tile t
)
282 return HasTileRoadType(t
, RTT_ROAD
) && IsRoadOwner(t
, RTT_ROAD
, OWNER_TOWN
);
286 * Checks if a DisallowedRoadDirections is valid.
288 * @param wc The value to check
289 * @return true if the given value is a valid DisallowedRoadDirections.
291 inline bool IsValidDisallowedRoadDirections(DisallowedRoadDirections drt
)
293 return drt
< DRD_END
;
297 * Gets the disallowed directions
298 * @param t the tile to get the directions from
299 * @return the disallowed directions
301 inline DisallowedRoadDirections
GetDisallowedRoadDirections(Tile t
)
303 assert(IsNormalRoad(t
));
304 return (DisallowedRoadDirections
)GB(t
.m5(), 4, 2);
308 * Sets the disallowed directions
309 * @param t the tile to set the directions for
310 * @param drd the disallowed directions
312 inline void SetDisallowedRoadDirections(Tile t
, DisallowedRoadDirections drd
)
314 assert(IsNormalRoad(t
));
315 assert(drd
< DRD_END
);
316 SB(t
.m5(), 4, 2, drd
);
320 * Get the road axis of a level crossing.
321 * @param t The tile to query.
322 * @pre IsLevelCrossing(t)
323 * @return The axis of the road.
325 inline Axis
GetCrossingRoadAxis(Tile t
)
327 assert(IsLevelCrossing(t
));
328 return (Axis
)GB(t
.m5(), 0, 1);
332 * Get the rail axis of a level crossing.
333 * @param t The tile to query.
334 * @pre IsLevelCrossing(t)
335 * @return The axis of the rail.
337 inline Axis
GetCrossingRailAxis(Tile t
)
339 assert(IsLevelCrossing(t
));
340 return OtherAxis((Axis
)GetCrossingRoadAxis(t
));
344 * Get the road bits of a level crossing.
345 * @param tile The tile to query.
346 * @return The present road bits.
348 inline RoadBits
GetCrossingRoadBits(Tile tile
)
350 return GetCrossingRoadAxis(tile
) == AXIS_X
? ROAD_X
: ROAD_Y
;
354 * Get the rail track of a level crossing.
355 * @param tile The tile to query.
356 * @return The rail track.
358 inline Track
GetCrossingRailTrack(Tile tile
)
360 return AxisToTrack(GetCrossingRailAxis(tile
));
364 * Get the rail track bits of a level crossing.
365 * @param tile The tile to query.
366 * @return The rail track bits.
368 inline TrackBits
GetCrossingRailBits(Tile tile
)
370 return AxisToTrackBits(GetCrossingRailAxis(tile
));
375 * Get the reservation state of the rail crossing
376 * @param t the crossing tile
377 * @return reservation state
378 * @pre IsLevelCrossingTile(t)
380 inline bool HasCrossingReservation(Tile t
)
382 assert(IsLevelCrossingTile(t
));
383 return HasBit(t
.m5(), 4);
387 * Set the reservation state of the rail crossing
388 * @note Works for both waypoints and rail depots
389 * @param t the crossing tile
390 * @param b the reservation state
391 * @pre IsLevelCrossingTile(t)
393 inline void SetCrossingReservation(Tile t
, bool b
)
395 assert(IsLevelCrossingTile(t
));
396 AssignBit(t
.m5(), 4, b
);
400 * Get the reserved track bits for a rail crossing
402 * @pre IsLevelCrossingTile(t)
403 * @return reserved track bits
405 inline TrackBits
GetCrossingReservationTrackBits(Tile t
)
407 return HasCrossingReservation(t
) ? GetCrossingRailBits(t
) : TRACK_BIT_NONE
;
411 * Check if the level crossing is barred.
412 * @param t The tile to query.
413 * @pre IsLevelCrossing(t)
414 * @return True if the level crossing is barred.
416 inline bool IsCrossingBarred(Tile t
)
418 assert(IsLevelCrossing(t
));
419 return HasBit(t
.m5(), 5);
423 * Set the bar state of a level crossing.
424 * @param t The tile to modify.
425 * @param barred True if the crossing should be barred, false otherwise.
426 * @pre IsLevelCrossing(t)
428 inline void SetCrossingBarred(Tile t
, bool barred
)
430 assert(IsLevelCrossing(t
));
431 AssignBit(t
.m5(), 5, barred
);
435 * Unbar a level crossing.
436 * @param t The tile to change.
438 inline void UnbarCrossing(Tile t
)
440 SetCrossingBarred(t
, false);
444 * Bar a level crossing.
445 * @param t The tile to change.
447 inline void BarCrossing(Tile t
)
449 SetCrossingBarred(t
, true);
452 /** Check if a road tile has snow/desert. */
453 #define IsOnDesert IsOnSnow
455 * Check if a road tile has snow/desert.
456 * @param t The tile to query.
457 * @return True if the tile has snow/desert.
459 inline bool IsOnSnow(Tile t
)
461 return HasBit(t
.m7(), 5);
464 /** Toggle the snow/desert state of a road tile. */
465 #define ToggleDesert ToggleSnow
467 * Toggle the snow/desert state of a road tile.
468 * @param t The tile to change.
470 inline void ToggleSnow(Tile t
)
472 ToggleBit(t
.m7(), 5);
476 /** The possible road side decorations. */
477 enum Roadside
: uint8_t {
478 ROADSIDE_BARREN
= 0, ///< Road on barren land
479 ROADSIDE_GRASS
= 1, ///< Road on grass
480 ROADSIDE_PAVED
= 2, ///< Road with paved sidewalks
481 ROADSIDE_STREET_LIGHTS
= 3, ///< Road with street lights on paved sidewalks
482 // 4 is unused for historical reasons
483 ROADSIDE_TREES
= 5, ///< Road with trees on paved sidewalks
484 ROADSIDE_GRASS_ROAD_WORKS
= 6, ///< Road on grass with road works
485 ROADSIDE_PAVED_ROAD_WORKS
= 7, ///< Road with sidewalks and road works
489 * Get the decorations of a road.
490 * @param tile The tile to query.
491 * @return The road decoration of the tile.
493 inline Roadside
GetRoadside(Tile tile
)
495 return (Roadside
)GB(tile
.m6(), 3, 3);
499 * Set the decorations of a road.
500 * @param tile The tile to change.
501 * @param s The new road decoration of the tile.
503 inline void SetRoadside(Tile tile
, Roadside s
)
505 SB(tile
.m6(), 3, 3, s
);
509 * Check if a tile has road works.
510 * @param t The tile to check.
511 * @return True if the tile has road works in progress.
513 inline bool HasRoadWorks(Tile t
)
515 return GetRoadside(t
) >= ROADSIDE_GRASS_ROAD_WORKS
;
519 * Increase the progress counter of road works.
520 * @param t The tile to modify.
521 * @return True if the road works are in the last stage.
523 inline bool IncreaseRoadWorksCounter(Tile t
)
527 return GB(t
.m7(), 0, 4) == 15;
531 * Start road works on a tile.
532 * @param t The tile to start the work on.
533 * @pre !HasRoadWorks(t)
535 inline void StartRoadWorks(Tile t
)
537 assert(!HasRoadWorks(t
));
538 /* Remove any trees or lamps in case or roadwork */
539 switch (GetRoadside(t
)) {
540 case ROADSIDE_BARREN
:
541 case ROADSIDE_GRASS
: SetRoadside(t
, ROADSIDE_GRASS_ROAD_WORKS
); break;
542 default: SetRoadside(t
, ROADSIDE_PAVED_ROAD_WORKS
); break;
547 * Terminate road works on a tile.
548 * @param t Tile to stop the road works on.
549 * @pre HasRoadWorks(t)
551 inline void TerminateRoadWorks(Tile t
)
553 assert(HasRoadWorks(t
));
554 SetRoadside(t
, (Roadside
)(GetRoadside(t
) - ROADSIDE_GRASS_ROAD_WORKS
+ ROADSIDE_GRASS
));
555 /* Stop the counter */
561 * Get the direction of the exit of a road depot.
562 * @param t The tile to query.
563 * @return Diagonal direction of the depot exit.
565 inline DiagDirection
GetRoadDepotDirection(Tile t
)
567 assert(IsRoadDepot(t
));
568 return (DiagDirection
)GB(t
.m5(), 0, 2);
572 RoadBits
GetAnyRoadBits(Tile tile
, RoadTramType rtt
, bool straight_tunnel_bridge_entrance
= false);
575 * Set the road road type of a tile.
576 * @param t The tile to change.
577 * @param rt The road type to set.
579 inline void SetRoadTypeRoad(Tile t
, RoadType rt
)
581 assert(MayHaveRoad(t
));
582 assert(rt
== INVALID_ROADTYPE
|| RoadTypeIsRoad(rt
));
583 SB(t
.m4(), 0, 6, rt
);
587 * Set the tram road type of a tile.
588 * @param t The tile to change.
589 * @param rt The road type to set.
591 inline void SetRoadTypeTram(Tile t
, RoadType rt
)
593 assert(MayHaveRoad(t
));
594 assert(rt
== INVALID_ROADTYPE
|| RoadTypeIsTram(rt
));
595 SB(t
.m8(), 6, 6, rt
);
599 * Set the road type of a tile.
600 * @param t The tile to change.
601 * @param rtt Set road or tram type.
602 * @param rt The road type to set.
604 inline void SetRoadType(Tile t
, RoadTramType rtt
, RoadType rt
)
606 if (rtt
== RTT_TRAM
) {
607 SetRoadTypeTram(t
, rt
);
609 SetRoadTypeRoad(t
, rt
);
614 * Set the present road types of a tile.
615 * @param t The tile to change.
616 * @param road_rt The road roadtype to set for the tile.
617 * @param tram_rt The tram roadtype to set for the tile.
619 inline void SetRoadTypes(Tile t
, RoadType road_rt
, RoadType tram_rt
)
621 SetRoadTypeRoad(t
, road_rt
);
622 SetRoadTypeTram(t
, tram_rt
);
626 * Make a normal road tile.
627 * @param t Tile to make a normal road.
628 * @param bits Road bits to set for all present road types.
629 * @param road_rt The road roadtype to set for the tile.
630 * @param tram_rt The tram roadtype to set for the tile.
631 * @param town Town ID if the road is a town-owned road.
632 * @param road New owner of road.
633 * @param tram New owner of tram tracks.
635 inline void MakeRoadNormal(Tile t
, RoadBits bits
, RoadType road_rt
, RoadType tram_rt
, TownID town
, Owner road
, Owner tram
)
637 SetTileType(t
, MP_ROAD
);
638 SetTileOwner(t
, road
);
640 t
.m3() = (tram_rt
!= INVALID_ROADTYPE
? bits
: 0);
641 t
.m5() = (road_rt
!= INVALID_ROADTYPE
? bits
: 0) | ROAD_TILE_NORMAL
<< 6;
644 SetRoadTypes(t
, road_rt
, tram_rt
);
645 SetRoadOwner(t
, RTT_TRAM
, tram
);
649 * Make a level crossing.
650 * @param t Tile to make a level crossing.
651 * @param road New owner of road.
652 * @param tram New owner of tram tracks.
653 * @param rail New owner of the rail track.
654 * @param roaddir Axis of the road.
655 * @param rat New rail type.
656 * @param road_rt The road roadtype to set for the tile.
657 * @param tram_rt The tram roadtype to set for the tile.
658 * @param town Town ID if the road is a town-owned road.
660 inline void MakeRoadCrossing(Tile t
, Owner road
, Owner tram
, Owner rail
, Axis roaddir
, RailType rat
, RoadType road_rt
, RoadType tram_rt
, uint town
)
662 SetTileType(t
, MP_ROAD
);
663 SetTileOwner(t
, rail
);
666 t
.m4() = INVALID_ROADTYPE
;
667 t
.m5() = ROAD_TILE_CROSSING
<< 6 | roaddir
;
670 t
.m8() = INVALID_ROADTYPE
<< 6 | rat
;
671 SetRoadTypes(t
, road_rt
, tram_rt
);
672 SetRoadOwner(t
, RTT_TRAM
, tram
);
676 * Sets the exit direction of a road depot.
677 * @param tile Tile of the depot.
678 * @param dir Direction of the depot exit.
680 inline void SetRoadDepotExitDirection(Tile tile
, DiagDirection dir
)
682 assert(IsRoadDepotTile(tile
));
683 SB(tile
.m5(), 0, 2, dir
);
688 * @param tile Tile to make a depot on.
689 * @param owner New owner of the depot.
690 * @param depot_id New depot ID.
691 * @param dir Direction of the depot exit.
692 * @param rt Road type of the depot.
694 inline void MakeRoadDepot(Tile tile
, Owner owner
, DepotID depot_id
, DiagDirection dir
, RoadType rt
)
696 SetTileType(tile
, MP_ROAD
);
697 SetTileOwner(tile
, owner
);
698 tile
.m2() = depot_id
;
700 tile
.m4() = INVALID_ROADTYPE
;
701 tile
.m5() = ROAD_TILE_DEPOT
<< 6 | dir
;
702 SB(tile
.m6(), 2, 4, 0);
704 tile
.m8() = INVALID_ROADTYPE
<< 6;
705 SetRoadType(tile
, GetRoadTramType(rt
), rt
);
706 SetRoadOwner(tile
, RTT_TRAM
, owner
);
709 #endif /* ROAD_MAP_H */