OpenTTD Source 20260218-master-g2123fca5ea
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
36[[debug_inline]] inline static RoadTileType GetRoadTileType(Tile t)
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 == RTT_TRAM) return (RoadBits)GB(t.m3(), 0, 4);
116 return (RoadBits)GB(t.m5(), 0, 4);
117}
118
126{
127 return GetRoadBits(tile, RTT_ROAD) | GetRoadBits(tile, RTT_TRAM);
128}
129
137inline void SetRoadBits(Tile t, RoadBits r, RoadTramType rtt)
138{
139 assert(IsNormalRoad(t)); // XXX incomplete
140 if (rtt == RTT_TRAM) {
141 SB(t.m3(), 0, 4, r);
142 } else {
143 SB(t.m5(), 0, 4, r);
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 == RTT_TRAM) ? GetRoadTypeTram(t) : GetRoadTypeRoad(t);
178}
179
185inline RoadTypes GetPresentRoadTypes(Tile t)
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
232inline bool HasTileAnyRoadType(Tile t, RoadTypes rts)
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 == RTT_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 == RTT_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
290inline bool HasTownOwnedRoad(Tile t)
291{
293}
294
302{
303 return drt < DRD_END;
304}
305
312{
313 assert(IsNormalRoad(t));
314 return (DisallowedRoadDirections)GB(t.m5(), 4, 2);
315}
316
323{
324 assert(IsNormalRoad(t));
325 assert(drd < DRD_END);
326 SB(t.m5(), 4, 2, drd);
327}
328
336{
337 assert(IsLevelCrossing(t));
338 return (Axis)GB(t.m5(), 0, 1);
339}
340
348{
349 assert(IsLevelCrossing(t));
351}
352
359{
360 return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
361}
362
369{
370 return AxisToTrack(GetCrossingRailAxis(tile));
371}
372
379{
381}
382
383
391{
392 assert(IsLevelCrossingTile(t));
393 return HasBit(t.m5(), 4);
394}
395
403inline void SetCrossingReservation(Tile t, bool b)
404{
405 assert(IsLevelCrossingTile(t));
406 AssignBit(t.m5(), 4, b);
407}
408
419
426inline bool IsCrossingBarred(Tile t)
427{
428 assert(IsLevelCrossing(t));
429 return HasBit(t.m5(), 5);
430}
431
438inline void SetCrossingBarred(Tile t, bool barred)
439{
440 assert(IsLevelCrossing(t));
441 AssignBit(t.m5(), 5, barred);
442}
443
448inline void UnbarCrossing(Tile t)
449{
450 SetCrossingBarred(t, false);
451}
452
457inline void BarCrossing(Tile t)
458{
459 SetCrossingBarred(t, true);
460}
461
467inline bool IsOnSnowOrDesert(Tile t)
468{
469 return HasBit(t.m7(), 5);
470}
471
477{
478 ToggleBit(t.m7(), 5);
479}
480
481
483enum class Roadside : uint8_t {
484 Barren = 0,
485 Grass = 1,
486 Paved = 2,
488 /* 4 is unused for historical reasons */
489 Trees = 5,
492};
493
500{
501 return static_cast<Roadside>(GB(tile.m6(), 3, 3));
502}
503
509inline void SetRoadside(Tile tile, Roadside s)
510{
511 SB(tile.m6(), 3, 3, to_underlying(s));
512}
513
519inline bool HasRoadWorks(Tile t)
520{
522}
523
530{
531 AB(t.m7(), 0, 4, 1);
532
533 return GB(t.m7(), 0, 4) == 15;
534}
535
541inline void StartRoadWorks(Tile t)
542{
543 assert(!HasRoadWorks(t));
544 /* Remove any trees or lamps in case or roadwork */
545 switch (GetRoadside(t)) {
546 case Roadside::Barren:
547 case Roadside::Grass:
549 break;
550
551 default:
553 break;
554 }
555}
556
563{
564 assert(HasRoadWorks(t));
566 /* Stop the counter */
567 SB(t.m7(), 0, 4, 0);
568}
569
570
577{
578 assert(IsRoadDepot(t));
579 return (DiagDirection)GB(t.m5(), 0, 2);
580}
581
582
583RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance = false);
584
590inline void SetRoadTypeRoad(Tile t, RoadType rt)
591{
592 assert(MayHaveRoad(t));
593 assert(rt == INVALID_ROADTYPE || RoadTypeIsRoad(rt));
594 SB(t.m4(), 0, 6, rt);
595}
596
602inline void SetRoadTypeTram(Tile t, RoadType rt)
603{
604 assert(MayHaveRoad(t));
605 assert(rt == INVALID_ROADTYPE || RoadTypeIsTram(rt));
606 SB(t.m8(), 6, 6, rt);
607}
608
615inline void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
616{
617 if (rtt == RTT_TRAM) {
618 SetRoadTypeTram(t, rt);
619 } else {
620 SetRoadTypeRoad(t, rt);
621 }
622}
623
630inline void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
631{
632 SetRoadTypeRoad(t, road_rt);
633 SetRoadTypeTram(t, tram_rt);
634}
635
646inline void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
647{
649 SetTileOwner(t, road);
650 t.m2() = town.base();
651 t.m3() = (tram_rt != INVALID_ROADTYPE ? bits : 0);
652 t.m5() = (road_rt != INVALID_ROADTYPE ? bits : 0) | to_underlying(RoadTileType::Normal) << 6;
653 SB(t.m6(), 2, 6, 0);
654 t.m7() = 0;
655 t.m8() = 0;
656 SetRoadTypes(t, road_rt, tram_rt);
657 SetRoadOwner(t, RTT_TRAM, tram);
658}
659
672inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, TownID town)
673{
675 SetTileOwner(t, rail);
676 t.m2() = town.base();
677 t.m3() = 0;
678 t.m4() = INVALID_ROADTYPE;
679 t.m5() = to_underlying(RoadTileType::Crossing) << 6 | roaddir;
680 SB(t.m6(), 2, 6, 0);
681 t.m7() = road.base();
682 t.m8() = INVALID_ROADTYPE << 6 | rat;
683 SetRoadTypes(t, road_rt, tram_rt);
684 SetRoadOwner(t, RTT_TRAM, tram);
685}
686
693{
694 assert(IsRoadDepotTile(tile));
695 SB(tile.m5(), 0, 2, dir);
696}
697
706inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
707{
709 SetTileOwner(tile, owner);
710 tile.m2() = depot_id.base();
711 tile.m3() = 0;
712 tile.m4() = INVALID_ROADTYPE;
713 tile.m5() = to_underlying(RoadTileType::Depot) << 6 | dir;
714 SB(tile.m6(), 2, 6, 0);
715 tile.m7() = owner.base();
716 tile.m8() = INVALID_ROADTYPE << 6;
717 SetRoadType(tile, GetRoadTramType(rt), rt);
718 SetRoadOwner(tile, RTT_TRAM, owner);
719}
720
721#endif /* ROAD_MAP_H */
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 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
Allow incrementing of DiagDirDiff variables.
@ AXIS_X
The X axis.
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.
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:390
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 RTT_ROAD.
Definition road_map.h:152
void SetRoadDepotExitDirection(Tile tile, DiagDirection dir)
Sets the exit direction of a road depot.
Definition road_map.h:692
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:590
void SetRoadTypeTram(Tile t, RoadType rt)
Set the tram road type of a tile.
Definition road_map.h:602
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:529
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
TrackBits GetCrossingRailBits(Tile tile)
Get the rail track bits of a level crossing.
Definition road_map.h:378
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 RTT_TRAM.
Definition road_map.h:210
void StartRoadWorks(Tile t)
Start road works on a tile.
Definition road_map.h:541
void SetCrossingBarred(Tile t, bool barred)
Set the bar state of a level crossing.
Definition road_map.h:438
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:630
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:576
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:457
RoadType GetRoadTypeTram(Tile t)
Get the road type for RoadTramType being RTT_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:646
Roadside GetRoadside(Tile tile)
Get the decorations of a road.
Definition road_map.h:499
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:672
void ToggleSnowOrDesert(Tile t)
Toggle the snow/desert state of a road tile.
Definition road_map.h:476
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:415
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:562
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:615
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:426
void SetRoadside(Tile tile, Roadside s)
Set the decorations of a road.
Definition road_map.h:509
void UnbarCrossing(Tile t)
Unbar a level crossing.
Definition road_map.h:448
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:706
void SetCrossingReservation(Tile t, bool b)
Set the reservation state of the rail crossing.
Definition road_map.h:403
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:483
@ Paved
Road with paved sidewalks.
Definition road_map.h:486
@ Barren
Road on barren land.
Definition road_map.h:484
@ GrassRoadWorks
Road on grass with road works.
Definition road_map.h:490
@ StreetLights
Road with street lights on paved sidewalks.
Definition road_map.h:487
@ Trees
Road with trees on paved sidewalks.
Definition road_map.h:489
@ Grass
Road on grass.
Definition road_map.h:485
@ PavedRoadWorks
Road with sidewalks and road works.
Definition road_map.h:491
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:467
bool HasRoadWorks(Tile t)
Check if a tile has road works.
Definition road_map.h:519
bool HasRoadTypeRoad(Tile t)
Check if a tile has a road type when RoadTramType is RTT_ROAD.
Definition road_map.h:200
Enums and other types related to roads.
RoadBits
Enumeration for the road parts on a tile.
Definition road_type.h:56
@ ROAD_Y
Full road along the y-axis (north-west + south-east).
Definition road_type.h:63
@ ROAD_X
Full road along the x-axis (south-west + north-east).
Definition road_type.h:62
RoadTramType
The different types of road type.
Definition road_type.h:37
@ RTT_ROAD
Road road type.
Definition road_type.h:38
@ RTT_TRAM
Tram road type.
Definition road_type.h:39
RoadType
The different roadtypes we support.
Definition road_type.h:23
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:28
DisallowedRoadDirections
Which directions are disallowed ?
Definition road_type.h:77
@ DRD_END
Sentinel.
Definition road_type.h:82
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.
TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition track_func.h:88
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:66
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
@ TRACK_BIT_NONE
No track.
Definition track_type.h:36
Track
These are used to specify a single track.
Definition track_type.h:19