OpenTTD Source 20251126-master-g67ded4f980
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 <http://www.gnu.org/licenses/>.
6 */
7
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,
24 Crossing = 1,
25 Depot = 2,
26};
27
28bool MayHaveRoad(Tile t);
29
36debug_inline static RoadTileType GetRoadTileType(Tile t)
37{
38 assert(IsTileType(t, MP_ROAD));
39 return static_cast<RoadTileType>(GB(t.m5(), 6, 2));
40}
41
48debug_inline static bool IsNormalRoad(Tile t)
49{
51}
52
58debug_inline static bool IsNormalRoadTile(Tile t)
59{
60 return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
61}
62
69inline bool IsLevelCrossing(Tile t)
70{
72}
73
80{
81 return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
82}
83
90debug_inline static bool IsRoadDepot(Tile t)
91{
93}
94
100debug_inline static bool IsRoadDepotTile(Tile t)
101{
102 return IsTileType(t, MP_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
147inline RoadType GetRoadTypeRoad(Tile t)
148{
149 assert(MayHaveRoad(t));
150 return (RoadType)GB(t.m4(), 0, 6);
151}
152
153inline RoadType GetRoadTypeTram(Tile t)
154{
155 assert(MayHaveRoad(t));
156 return (RoadType)GB(t.m8(), 6, 6);
157}
158
159inline RoadType GetRoadType(Tile t, RoadTramType rtt)
160{
161 return (rtt == RTT_TRAM) ? GetRoadTypeTram(t) : GetRoadTypeRoad(t);
162}
163
170{
171 RoadTypes result{};
172 if (MayHaveRoad(t)) {
173 if (GetRoadTypeRoad(t) != INVALID_ROADTYPE) result.Set(GetRoadTypeRoad(t));
174 if (GetRoadTypeTram(t) != INVALID_ROADTYPE) result.Set(GetRoadTypeTram(t));
175 }
176 return result;
177}
178
179inline bool HasRoadTypeRoad(Tile t)
180{
181 return GetRoadTypeRoad(t) != INVALID_ROADTYPE;
182}
183
184inline bool HasRoadTypeTram(Tile t)
185{
186 return GetRoadTypeTram(t) != INVALID_ROADTYPE;
187}
188
196{
197 return GetRoadType(t, rtt) != INVALID_ROADTYPE;
198}
199
207{
208 if (!MayHaveRoad(t)) return false;
209 return GetPresentRoadTypes(t).Any(rts);
210}
211
219{
220 assert(MayHaveRoad(t));
221 if (rtt == RTT_ROAD) return (Owner)GB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5);
222
223 /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
224 * to OWNER_TOWN makes it use one bit less */
225 Owner o = (Owner)GB(t.m3(), 4, 4);
226 return o == OWNER_TOWN ? OWNER_NONE : o;
227}
228
235inline void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
236{
237 if (rtt == RTT_ROAD) {
238 SB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5, o.base());
239 } else {
240 SB(t.m3(), 4, 4, (o == OWNER_NONE ? OWNER_TOWN : o).base());
241 }
242}
243
252inline bool IsRoadOwner(Tile t, RoadTramType rtt, Owner o)
253{
254 assert(HasTileRoadType(t, rtt));
255 return (GetRoadOwner(t, rtt) == o);
256}
257
264inline bool HasTownOwnedRoad(Tile t)
265{
267}
268
276{
277 return drt < DRD_END;
278}
279
286{
287 assert(IsNormalRoad(t));
288 return (DisallowedRoadDirections)GB(t.m5(), 4, 2);
289}
290
297{
298 assert(IsNormalRoad(t));
299 assert(drd < DRD_END);
300 SB(t.m5(), 4, 2, drd);
301}
302
310{
311 assert(IsLevelCrossing(t));
312 return (Axis)GB(t.m5(), 0, 1);
313}
314
322{
323 assert(IsLevelCrossing(t));
325}
326
333{
334 return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
335}
336
343{
344 return AxisToTrack(GetCrossingRailAxis(tile));
345}
346
353{
355}
356
357
365{
366 assert(IsLevelCrossingTile(t));
367 return HasBit(t.m5(), 4);
368}
369
377inline void SetCrossingReservation(Tile t, bool b)
378{
379 assert(IsLevelCrossingTile(t));
380 AssignBit(t.m5(), 4, b);
381}
382
393
400inline bool IsCrossingBarred(Tile t)
401{
402 assert(IsLevelCrossing(t));
403 return HasBit(t.m5(), 5);
404}
405
412inline void SetCrossingBarred(Tile t, bool barred)
413{
414 assert(IsLevelCrossing(t));
415 AssignBit(t.m5(), 5, barred);
416}
417
422inline void UnbarCrossing(Tile t)
423{
424 SetCrossingBarred(t, false);
425}
426
431inline void BarCrossing(Tile t)
432{
433 SetCrossingBarred(t, true);
434}
435
441inline bool IsOnSnowOrDesert(Tile t)
442{
443 return HasBit(t.m7(), 5);
444}
445
451{
452 ToggleBit(t.m7(), 5);
453}
454
455
457enum class Roadside : uint8_t {
458 Barren = 0,
459 Grass = 1,
460 Paved = 2,
461 StreetLights = 3,
462 /* 4 is unused for historical reasons */
463 Trees = 5,
464 GrassRoadWorks = 6,
465 PavedRoadWorks = 7,
466};
467
474{
475 return static_cast<Roadside>(GB(tile.m6(), 3, 3));
476}
477
483inline void SetRoadside(Tile tile, Roadside s)
484{
485 SB(tile.m6(), 3, 3, to_underlying(s));
486}
487
493inline bool HasRoadWorks(Tile t)
494{
496}
497
504{
505 AB(t.m7(), 0, 4, 1);
506
507 return GB(t.m7(), 0, 4) == 15;
508}
509
515inline void StartRoadWorks(Tile t)
516{
517 assert(!HasRoadWorks(t));
518 /* Remove any trees or lamps in case or roadwork */
519 switch (GetRoadside(t)) {
520 case Roadside::Barren:
521 case Roadside::Grass:
523 break;
524
525 default:
527 break;
528 }
529}
530
537{
538 assert(HasRoadWorks(t));
540 /* Stop the counter */
541 SB(t.m7(), 0, 4, 0);
542}
543
544
551{
552 assert(IsRoadDepot(t));
553 return (DiagDirection)GB(t.m5(), 0, 2);
554}
555
556
557RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance = false);
558
564inline void SetRoadTypeRoad(Tile t, RoadType rt)
565{
566 assert(MayHaveRoad(t));
567 assert(rt == INVALID_ROADTYPE || RoadTypeIsRoad(rt));
568 SB(t.m4(), 0, 6, rt);
569}
570
576inline void SetRoadTypeTram(Tile t, RoadType rt)
577{
578 assert(MayHaveRoad(t));
579 assert(rt == INVALID_ROADTYPE || RoadTypeIsTram(rt));
580 SB(t.m8(), 6, 6, rt);
581}
582
589inline void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
590{
591 if (rtt == RTT_TRAM) {
592 SetRoadTypeTram(t, rt);
593 } else {
594 SetRoadTypeRoad(t, rt);
595 }
596}
597
604inline void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
605{
606 SetRoadTypeRoad(t, road_rt);
607 SetRoadTypeTram(t, tram_rt);
608}
609
620inline void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
621{
623 SetTileOwner(t, road);
624 t.m2() = town.base();
625 t.m3() = (tram_rt != INVALID_ROADTYPE ? bits : 0);
626 t.m5() = (road_rt != INVALID_ROADTYPE ? bits : 0) | to_underlying(RoadTileType::Normal) << 6;
627 SB(t.m6(), 2, 6, 0);
628 t.m7() = 0;
629 t.m8() = 0;
630 SetRoadTypes(t, road_rt, tram_rt);
631 SetRoadOwner(t, RTT_TRAM, tram);
632}
633
646inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, TownID town)
647{
649 SetTileOwner(t, rail);
650 t.m2() = town.base();
651 t.m3() = 0;
652 t.m4() = INVALID_ROADTYPE;
653 t.m5() = to_underlying(RoadTileType::Crossing) << 6 | roaddir;
654 SB(t.m6(), 2, 6, 0);
655 t.m7() = road.base();
656 t.m8() = INVALID_ROADTYPE << 6 | rat;
657 SetRoadTypes(t, road_rt, tram_rt);
658 SetRoadOwner(t, RTT_TRAM, tram);
659}
660
667{
668 assert(IsRoadDepotTile(tile));
669 SB(tile.m5(), 0, 2, dir);
670}
671
680inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
681{
682 SetTileType(tile, MP_ROAD);
683 SetTileOwner(tile, owner);
684 tile.m2() = depot_id.base();
685 tile.m3() = 0;
686 tile.m4() = INVALID_ROADTYPE;
687 tile.m5() = to_underlying(RoadTileType::Depot) << 6 | dir;
688 SB(tile.m6(), 2, 6, 0);
689 tile.m7() = owner.base();
690 tile.m8() = INVALID_ROADTYPE << 6;
691 SetRoadType(tile, GetRoadTramType(rt), rt);
692 SetRoadOwner(tile, RTT_TRAM, owner);
693}
694
695#endif /* ROAD_MAP_H */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
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.
debug_inline 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 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
debug_inline uint16_t & m8()
General purpose.
Definition map_func.h:197
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
debug_inline uint8_t & m4()
General purpose.
Definition map_func.h:149
debug_inline uint8_t & m6()
General purpose.
Definition map_func.h:173
debug_inline uint8_t & m3()
General purpose.
Definition map_func.h:137
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
debug_inline uint8_t & m5()
General purpose.
Definition map_func.h:161
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)
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:17
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:235
bool HasCrossingReservation(Tile t)
Get the reservation state of the rail crossing.
Definition road_map.h:364
bool HasTownOwnedRoad(Tile t)
Checks if given tile has town owned road.
Definition road_map.h:264
static debug_inline bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition road_map.h:58
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:90
void SetRoadDepotExitDirection(Tile tile, DiagDirection dir)
Sets the exit direction of a road depot.
Definition road_map.h:666
RoadTypes GetPresentRoadTypes(Tile t)
Get the present road types of a tile.
Definition road_map.h:169
void SetRoadTypeRoad(Tile t, RoadType rt)
Set the road road type of a tile.
Definition road_map.h:564
void SetRoadTypeTram(Tile t, RoadType rt)
Set the tram road type of a tile.
Definition road_map.h:576
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:503
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
bool HasTileAnyRoadType(Tile t, RoadTypes rts)
Check if a tile has one of the specified road types.
Definition road_map.h:206
bool IsValidDisallowedRoadDirections(DisallowedRoadDirections drt)
Checks if a DisallowedRoadDirections is valid.
Definition road_map.h:275
Axis GetCrossingRoadAxis(Tile t)
Get the road axis of a level crossing.
Definition road_map.h:309
TrackBits GetCrossingRailBits(Tile tile)
Get the rail track bits of a level crossing.
Definition road_map.h:352
void SetDisallowedRoadDirections(Tile t, DisallowedRoadDirections drd)
Sets the disallowed directions.
Definition road_map.h:296
Track GetCrossingRailTrack(Tile tile)
Get the rail track of a level crossing.
Definition road_map.h:342
void StartRoadWorks(Tile t)
Start road works on a tile.
Definition road_map.h:515
void SetCrossingBarred(Tile t, bool barred)
Set the bar state of a level crossing.
Definition road_map.h:412
DisallowedRoadDirections GetDisallowedRoadDirections(Tile t)
Gets the disallowed directions.
Definition road_map.h:285
void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
Set the present road types of a tile.
Definition road_map.h:604
bool HasTileRoadType(Tile t, RoadTramType rtt)
Check if a tile has a road or a tram road type.
Definition road_map.h:195
DiagDirection GetRoadDepotDirection(Tile t)
Get the direction of the exit of a road depot.
Definition road_map.h:550
static debug_inline bool IsRoadDepotTile(Tile t)
Return whether a tile is a road depot tile.
Definition road_map.h:100
void BarCrossing(Tile t)
Bar a level crossing.
Definition road_map.h:431
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:620
Roadside GetRoadside(Tile tile)
Get the decorations of a road.
Definition road_map.h:473
RoadTileType
The different types of road tiles.
Definition road_map.h:22
@ Normal
Normal road.
@ Depot
Depot (one entrance)
@ Crossing
Level crossing.
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:646
void ToggleSnowOrDesert(Tile t)
Toggle the snow/desert state of a road tile.
Definition road_map.h:450
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:321
TrackBits GetCrossingReservationTrackBits(Tile t)
Get the reserved track bits for a rail crossing.
Definition road_map.h:389
void TerminateRoadWorks(Tile t)
Terminate road works on a tile.
Definition road_map.h:536
RoadBits GetCrossingRoadBits(Tile tile)
Get the road bits of a level crossing.
Definition road_map.h:332
void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
Set the road type of a tile.
Definition road_map.h:589
Owner GetRoadOwner(Tile t, RoadTramType rtt)
Get the owner of a specific road type.
Definition road_map.h:218
static debug_inline RoadTileType GetRoadTileType(Tile t)
Get the type of the road tile.
Definition road_map.h:36
bool IsCrossingBarred(Tile t)
Check if the level crossing is barred.
Definition road_map.h:400
void SetRoadside(Tile tile, Roadside s)
Set the decorations of a road.
Definition road_map.h:483
void UnbarCrossing(Tile t)
Unbar a level crossing.
Definition road_map.h:422
bool IsRoadOwner(Tile t, RoadTramType rtt, Owner o)
Check if a specific road type is owned by an owner.
Definition road_map.h:252
void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
Make a road depot.
Definition road_map.h:680
void SetCrossingReservation(Tile t, bool b)
Set the reservation state of the rail crossing.
Definition road_map.h:377
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:457
@ Paved
Road with paved sidewalks.
@ Barren
Road on barren land.
@ GrassRoadWorks
Road on grass with road works.
@ StreetLights
Road with street lights on paved sidewalks.
@ Trees
Road with trees on paved sidewalks.
@ Grass
Road on grass.
@ PavedRoadWorks
Road with sidewalks and road works.
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
bool IsOnSnowOrDesert(Tile t)
Check if a road tile has snow/desert.
Definition road_map.h:441
bool HasRoadWorks(Tile t)
Check if a tile has road works.
Definition road_map.h:493
static debug_inline bool IsNormalRoad(Tile t)
Return whether a tile is a normal road.
Definition road_map.h:48
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.
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
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
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