OpenTTD Source 20260621-master-g720d10536d
road_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef ROAD_MAP_H
11#define ROAD_MAP_H
12
13#include "track_func.h"
14#include "depot_type.h"
15#include "rail_type.h"
16#include "road_func.h"
17#include "tile_map.h"
18#include "road_type.h"
19
20
22enum class RoadTileType : uint8_t {
23 Normal = 0,
25 Depot = 2,
26};
27
28bool MayHaveRoad(Tile t);
29
37{
38 assert(IsTileType(t, TileType::Road));
39 return static_cast<RoadTileType>(GB(t.m5(), 6, 2));
40}
41
48[[debug_inline]] inline static bool IsNormalRoad(Tile t)
49{
51}
52
58[[debug_inline]] inline static bool IsNormalRoadTile(Tile t)
59{
60 return IsTileType(t, TileType::Road) && IsNormalRoad(t);
61}
62
69inline bool IsLevelCrossing(Tile t)
70{
72}
73
80{
82}
83
90[[debug_inline]] inline static bool IsRoadDepot(Tile t)
91{
93}
94
100[[debug_inline]] inline static bool IsRoadDepotTile(Tile t)
101{
102 return IsTileType(t, TileType::Road) && IsRoadDepot(t);
103}
104
113{
114 assert(IsNormalRoad(t));
115 if (rtt == RoadTramType::Tram) return (RoadBits)GB(t.m3(), 0, 4);
116 return (RoadBits)GB(t.m5(), 0, 4);
117}
118
129
137inline void SetRoadBits(Tile t, RoadBits r, RoadTramType rtt)
138{
139 assert(IsNormalRoad(t)); // XXX incomplete
140 if (rtt == RoadTramType::Tram) {
141 SB(t.m3(), 0, 4, r.base());
142 } else {
143 SB(t.m5(), 0, 4, r.base());
144 }
145}
146
153{
154 assert(MayHaveRoad(t));
155 return (RoadType)GB(t.m4(), 0, 6);
156}
157
164{
165 assert(MayHaveRoad(t));
166 return (RoadType)GB(t.m8(), 6, 6);
167}
168
176{
177 return (rtt == RoadTramType::Tram) ? GetRoadTypeTram(t) : GetRoadTypeRoad(t);
178}
179
186{
187 RoadTypes result{};
188 if (MayHaveRoad(t)) {
191 }
192 return result;
193}
194
200inline bool HasRoadTypeRoad(Tile t)
201{
203}
204
210inline bool HasRoadTypeTram(Tile t)
211{
213}
214
222{
223 return GetRoadType(t, rtt) != INVALID_ROADTYPE;
224}
225
233{
234 if (!MayHaveRoad(t)) return false;
235 return GetPresentRoadTypes(t).Any(rts);
236}
237
244inline Owner GetRoadOwner(Tile t, RoadTramType rtt)
245{
246 assert(MayHaveRoad(t));
247 if (rtt == RoadTramType::Road) return (Owner)GB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5);
248
249 /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
250 * to OWNER_TOWN makes it use one bit less */
251 Owner o = (Owner)GB(t.m3(), 4, 4);
252 return o == OWNER_TOWN ? OWNER_NONE : o;
253}
254
261inline void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
262{
263 if (rtt == RoadTramType::Road) {
264 SB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5, o.base());
265 } else {
266 SB(t.m3(), 4, 4, (o == OWNER_NONE ? OWNER_TOWN : o).base());
267 }
268}
269
278inline bool IsRoadOwner(Tile t, RoadTramType rtt, Owner o)
279{
280 assert(HasTileRoadType(t, rtt));
281 return (GetRoadOwner(t, rtt) == o);
282}
283
294
305
312{
313 assert(IsNormalRoad(t));
314 return static_cast<DisallowedRoadDirections>(GB(t.m5(), 4, 2));
315}
316
323{
324 assert(IsNormalRoad(t));
326 SB(t.m5(), 4, 2, drd.base());
327}
328
336{
337 assert(IsLevelCrossing(t));
338 return static_cast<Axis>(GB(t.m5(), 0, 1));
339}
340
348{
349 assert(IsLevelCrossing(t));
351}
352
359{
361}
362
369{
370 return AxisToTrack(GetCrossingRailAxis(tile));
371}
372
380{
381 assert(IsLevelCrossingTile(t));
382 return HasBit(t.m5(), 4);
383}
384
392inline void SetCrossingReservation(Tile t, bool b)
393{
394 assert(IsLevelCrossingTile(t));
395 AssignBit(t.m5(), 4, b);
396}
397
408
415inline bool IsCrossingBarred(Tile t)
416{
417 assert(IsLevelCrossing(t));
418 return HasBit(t.m5(), 5);
419}
420
427inline void SetCrossingBarred(Tile t, bool barred)
428{
429 assert(IsLevelCrossing(t));
430 AssignBit(t.m5(), 5, barred);
431}
432
437inline void UnbarCrossing(Tile t)
438{
439 SetCrossingBarred(t, false);
440}
441
446inline void BarCrossing(Tile t)
447{
448 SetCrossingBarred(t, true);
449}
450
456inline bool IsOnSnowOrDesert(Tile t)
457{
458 return HasBit(t.m7(), 5);
459}
460
466{
467 ToggleBit(t.m7(), 5);
468}
469
470
472enum class Roadside : uint8_t {
473 Barren = 0,
474 Grass = 1,
475 Paved = 2,
477 /* 4 is unused for historical reasons */
478 Trees = 5,
481};
482
489{
490 return static_cast<Roadside>(GB(tile.m6(), 3, 3));
491}
492
498inline void SetRoadside(Tile tile, Roadside s)
499{
500 SB(tile.m6(), 3, 3, to_underlying(s));
501}
502
508inline bool HasRoadWorks(Tile t)
509{
511}
512
519{
520 AB(t.m7(), 0, 4, 1);
521
522 return GB(t.m7(), 0, 4) == 15;
523}
524
530inline void StartRoadWorks(Tile t)
531{
532 assert(!HasRoadWorks(t));
533 /* Remove any trees or lamps in case or roadwork */
534 switch (GetRoadside(t)) {
535 case Roadside::Barren:
536 case Roadside::Grass:
538 break;
539
540 default:
542 break;
543 }
544}
545
552{
553 assert(HasRoadWorks(t));
555 /* Stop the counter */
556 SB(t.m7(), 0, 4, 0);
557}
558
559
566{
567 assert(IsRoadDepot(t));
568 return static_cast<DiagDirection>(GB(t.m5(), 0, 2));
569}
570
571
572RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance = false);
573
579inline void SetRoadTypeRoad(Tile t, RoadType rt)
580{
581 assert(MayHaveRoad(t));
582 assert(rt == INVALID_ROADTYPE || RoadTypeIsRoad(rt));
583 SB(t.m4(), 0, 6, rt);
584}
585
591inline void SetRoadTypeTram(Tile t, RoadType rt)
592{
593 assert(MayHaveRoad(t));
594 assert(rt == INVALID_ROADTYPE || RoadTypeIsTram(rt));
595 SB(t.m8(), 6, 6, rt);
596}
597
604inline void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
605{
606 if (rtt == RoadTramType::Tram) {
607 SetRoadTypeTram(t, rt);
608 } else {
609 SetRoadTypeRoad(t, rt);
610 }
611}
612
619inline void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
620{
621 SetRoadTypeRoad(t, road_rt);
622 SetRoadTypeTram(t, tram_rt);
623}
624
635inline void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
636{
638 SetTileOwner(t, road);
639 t.m2() = town.base();
640 t.m3() = (tram_rt != INVALID_ROADTYPE ? bits.base() : 0);
641 t.m5() = (road_rt != INVALID_ROADTYPE ? bits.base() : 0) | to_underlying(RoadTileType::Normal) << 6;
642 SB(t.m6(), 2, 6, 0);
643 t.m7() = 0;
644 t.m8() = 0;
645 SetRoadTypes(t, road_rt, tram_rt);
647}
648
661inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, TownID town)
662{
664 SetTileOwner(t, rail);
665 t.m2() = town.base();
666 t.m3() = 0;
667 t.m4() = INVALID_ROADTYPE;
669 SB(t.m6(), 2, 6, 0);
670 t.m7() = road.base();
671 t.m8() = INVALID_ROADTYPE << 6 | rat;
672 SetRoadTypes(t, road_rt, tram_rt);
674}
675
682{
683 assert(IsRoadDepotTile(tile));
684 SB(tile.m5(), 0, 2, to_underlying(dir));
685}
686
695inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
696{
698 SetTileOwner(tile, owner);
699 tile.m2() = depot_id.base();
700 tile.m3() = 0;
701 tile.m4() = INVALID_ROADTYPE;
703 SB(tile.m6(), 2, 6, 0);
704 tile.m7() = owner.base();
705 tile.m8() = INVALID_ROADTYPE << 6;
706 SetRoadType(tile, GetRoadTramType(rt), rt);
707 SetRoadOwner(tile, RoadTramType::Tram, owner);
708}
709
710#endif /* ROAD_MAP_H */
@ None
Tile is not animated.
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T AB(T &x, const uint8_t s, const uint8_t n, const U i)
Add i to n bits of x starting at bit s.
constexpr T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
uint8_t & m5()
General purpose.
Definition map_func.h:156
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:112
uint8_t & m4()
General purpose.
Definition map_func.h:145
uint8_t & m6()
General purpose.
Definition map_func.h:167
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:178
uint8_t & m3()
General purpose.
Definition map_func.h:134
uint16_t & m8()
General purpose.
Definition map_func.h:189
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:123
static constexpr Owner OWNER_TOWN
A town owns the tile, or a town is expanding.
static constexpr Owner OWNER_NONE
The tile has no ownership.
Header files for depots (not hangars).
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
Axis OtherAxis(Axis a)
Select the other axis as provided.
Axis
Enumeration for the two axis X and Y.
DiagDirection
Enumeration for diagonal directions.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
The different types of rail.
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
Functions related to roads.
RoadBits AxisToRoadBits(Axis a)
Create the road-part which belongs to the given Axis.
Definition road_func.h:93
void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition road_map.h:261
bool HasCrossingReservation(Tile t)
Get the reservation state of the rail crossing.
Definition road_map.h:379
static RoadTileType GetRoadTileType(Tile t)
Get the type of the road tile.
Definition road_map.h:36
bool HasTownOwnedRoad(Tile t)
Checks if given tile has town owned road.
Definition road_map.h:290
RoadType GetRoadTypeRoad(Tile t)
Get the road type for RoadTramType being RoadTramType::Road.
Definition road_map.h:152
void SetRoadDepotExitDirection(Tile tile, DiagDirection dir)
Sets the exit direction of a road depot.
Definition road_map.h:681
RoadTypes GetPresentRoadTypes(Tile t)
Get the present road types of a tile.
Definition road_map.h:185
void SetRoadTypeRoad(Tile t, RoadType rt)
Set the road road type of a tile.
Definition road_map.h:579
void SetRoadTypeTram(Tile t, RoadType rt)
Set the tram road type of a tile.
Definition road_map.h:591
RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance=false)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition road_map.cpp:54
bool IsLevelCrossingTile(Tile t)
Return whether a tile is a level crossing tile.
Definition road_map.h:79
bool IncreaseRoadWorksCounter(Tile t)
Increase the progress counter of road works.
Definition road_map.h:518
RoadBits GetRoadBits(Tile t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition road_map.h:112
bool MayHaveRoad(Tile t)
Test whether a tile can have road/tram types.
Definition road_map.cpp:21
static bool IsRoadDepotTile(Tile t)
Return whether a tile is a road depot tile.
Definition road_map.h:100
bool HasTileAnyRoadType(Tile t, RoadTypes rts)
Check if a tile has one of the specified road types.
Definition road_map.h:232
bool IsValidDisallowedRoadDirections(DisallowedRoadDirections drt)
Checks if a DisallowedRoadDirections is valid.
Definition road_map.h:301
Axis GetCrossingRoadAxis(Tile t)
Get the road axis of a level crossing.
Definition road_map.h:335
void SetDisallowedRoadDirections(Tile t, DisallowedRoadDirections drd)
Sets the disallowed directions.
Definition road_map.h:322
Track GetCrossingRailTrack(Tile tile)
Get the rail track of a level crossing.
Definition road_map.h:368
bool HasRoadTypeTram(Tile t)
Check if a tile has a road type when RoadTramType is RoadTramType::Tram.
Definition road_map.h:210
void StartRoadWorks(Tile t)
Start road works on a tile.
Definition road_map.h:530
void SetCrossingBarred(Tile t, bool barred)
Set the bar state of a level crossing.
Definition road_map.h:427
DisallowedRoadDirections GetDisallowedRoadDirections(Tile t)
Gets the disallowed directions.
Definition road_map.h:311
void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
Set the present road types of a tile.
Definition road_map.h:619
bool HasTileRoadType(Tile t, RoadTramType rtt)
Check if a tile has a road or a tram road type.
Definition road_map.h:221
DiagDirection GetRoadDepotDirection(Tile t)
Get the direction of the exit of a road depot.
Definition road_map.h:565
static bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:90
void BarCrossing(Tile t)
Bar a level crossing.
Definition road_map.h:446
RoadType GetRoadTypeTram(Tile t)
Get the road type for RoadTramType being RoadTramType::Tram.
Definition road_map.h:163
void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
Make a normal road tile.
Definition road_map.h:635
Roadside GetRoadside(Tile tile)
Get the decorations of a road.
Definition road_map.h:488
RoadTileType
The different types of road tiles.
Definition road_map.h:22
@ Normal
Normal road.
Definition road_map.h:23
@ Depot
Depot (one entrance).
Definition road_map.h:25
@ Crossing
Level crossing.
Definition road_map.h:24
void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, TownID town)
Make a level crossing.
Definition road_map.h:661
void ToggleSnowOrDesert(Tile t)
Toggle the snow/desert state of a road tile.
Definition road_map.h:465
RoadBits GetAllRoadBits(Tile tile)
Get all set RoadBits on the given tile.
Definition road_map.h:125
Axis GetCrossingRailAxis(Tile t)
Get the rail axis of a level crossing.
Definition road_map.h:347
TrackBits GetCrossingReservationTrackBits(Tile t)
Get the reserved track bits for a rail crossing.
Definition road_map.h:404
RoadType GetRoadType(Tile t, RoadTramType rtt)
Get the road type for the given RoadTramType.
Definition road_map.h:175
static bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition road_map.h:58
void TerminateRoadWorks(Tile t)
Terminate road works on a tile.
Definition road_map.h:551
RoadBits GetCrossingRoadBits(Tile tile)
Get the road bits of a level crossing.
Definition road_map.h:358
void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
Set the road type of a tile.
Definition road_map.h:604
Owner GetRoadOwner(Tile t, RoadTramType rtt)
Get the owner of a specific road type.
Definition road_map.h:244
bool IsCrossingBarred(Tile t)
Check if the level crossing is barred.
Definition road_map.h:415
void SetRoadside(Tile tile, Roadside s)
Set the decorations of a road.
Definition road_map.h:498
void UnbarCrossing(Tile t)
Unbar a level crossing.
Definition road_map.h:437
bool IsRoadOwner(Tile t, RoadTramType rtt, Owner o)
Check if a specific road type is owned by an owner.
Definition road_map.h:278
void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
Make a road depot.
Definition road_map.h:695
void SetCrossingReservation(Tile t, bool b)
Set the reservation state of the rail crossing.
Definition road_map.h:392
void SetRoadBits(Tile t, RoadBits r, RoadTramType rtt)
Set the present road bits for a specific road type.
Definition road_map.h:137
Roadside
The possible road side decorations.
Definition road_map.h:472
@ Paved
Road with paved sidewalks.
Definition road_map.h:475
@ Barren
Road on barren land.
Definition road_map.h:473
@ GrassRoadWorks
Road on grass with road works.
Definition road_map.h:479
@ StreetLights
Road with street lights on paved sidewalks.
Definition road_map.h:476
@ Trees
Road with trees on paved sidewalks.
Definition road_map.h:478
@ Grass
Road on grass.
Definition road_map.h:474
@ PavedRoadWorks
Road with sidewalks and road works.
Definition road_map.h:480
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
static bool IsNormalRoad(Tile t)
Return whether a tile is a normal road.
Definition road_map.h:48
bool IsOnSnowOrDesert(Tile t)
Check if a road tile has snow/desert.
Definition road_map.h:456
bool HasRoadWorks(Tile t)
Check if a tile has road works.
Definition road_map.h:508
bool HasRoadTypeRoad(Tile t)
Check if a tile has a road type when RoadTramType is RoadTramType::Road.
Definition road_map.h:200
Enums and other types related to roads.
EnumBitSet< RoadType, uint64_t > RoadTypes
Bitset of RoadType elements.
Definition road_type.h:32
@ Northbound
All northbound traffic is disallowed.
Definition road_type.h:79
@ Southbound
All southbound traffic is disallowed.
Definition road_type.h:78
EnumBitSet< RoadBit, uint8_t > RoadBits
Bitset of RoadBit elements.
Definition road_type.h:64
EnumBitSet< DisallowedRoadDirection, uint8_t > DisallowedRoadDirections
Bitset of DisallowedRoadDirection elements.
Definition road_type.h:84
RoadType
The different roadtypes we support.
Definition road_type.h:23
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:28
RoadTramType
The different types of road type.
Definition road_type.h:37
@ Tram
Tram type.
Definition road_type.h:39
@ Road
Road type.
Definition road_type.h:38
#define debug_inline
When making a (pure) debug build, the compiler will by default disable inlining of functions.
Definition stdafx.h:222
Map writing/reading functions for tiles.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
@ Road
A tile with road and/or tram tracks.
Definition tile_type.h:51
Different conversion functions from one kind of track to another.
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track Axis::X -> Track::X Axis::Y -> Track::Y Uses the fact that...
Definition track_func.h:62
EnumBitSet< Track, uint8_t > TrackBits
Bitset of Track elements.
Definition track_type.h:43
Track
These are used to specify a single track.
Definition track_type.h:19