OpenTTD Source 20250529-master-g10c159a79f
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
27
28bool MayHaveRoad(Tile t);
29
36debug_inline static RoadTileType GetRoadTileType(Tile t)
37{
38 assert(IsTileType(t, MP_ROAD));
39 return (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
112inline RoadBits GetRoadBits(Tile t, RoadTramType rtt)
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
195inline bool HasTileRoadType(Tile t, RoadTramType rtt)
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
218inline Owner GetRoadOwner(Tile t, RoadTramType rtt)
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{
266 return HasTileRoadType(t, RTT_ROAD) && IsRoadOwner(t, RTT_ROAD, OWNER_TOWN);
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
467
474{
475 return (Roadside)GB(tile.m6(), 3, 3);
476}
477
483inline void SetRoadside(Tile tile, Roadside s)
484{
485 SB(tile.m6(), 3, 3, 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:
522 default: SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
523 }
524}
525
532{
533 assert(HasRoadWorks(t));
535 /* Stop the counter */
536 SB(t.m7(), 0, 4, 0);
537}
538
539
546{
547 assert(IsRoadDepot(t));
548 return (DiagDirection)GB(t.m5(), 0, 2);
549}
550
551
552RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance = false);
553
559inline void SetRoadTypeRoad(Tile t, RoadType rt)
560{
561 assert(MayHaveRoad(t));
562 assert(rt == INVALID_ROADTYPE || RoadTypeIsRoad(rt));
563 SB(t.m4(), 0, 6, rt);
564}
565
571inline void SetRoadTypeTram(Tile t, RoadType rt)
572{
573 assert(MayHaveRoad(t));
574 assert(rt == INVALID_ROADTYPE || RoadTypeIsTram(rt));
575 SB(t.m8(), 6, 6, rt);
576}
577
584inline void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
585{
586 if (rtt == RTT_TRAM) {
587 SetRoadTypeTram(t, rt);
588 } else {
589 SetRoadTypeRoad(t, rt);
590 }
591}
592
599inline void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
600{
601 SetRoadTypeRoad(t, road_rt);
602 SetRoadTypeTram(t, tram_rt);
603}
604
615inline void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
616{
618 SetTileOwner(t, road);
619 t.m2() = town.base();
620 t.m3() = (tram_rt != INVALID_ROADTYPE ? bits : 0);
621 t.m5() = (road_rt != INVALID_ROADTYPE ? bits : 0) | ROAD_TILE_NORMAL << 6;
622 SB(t.m6(), 2, 4, 0);
623 t.m7() = 0;
624 SetRoadTypes(t, road_rt, tram_rt);
625 SetRoadOwner(t, RTT_TRAM, tram);
626}
627
640inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, TownID town)
641{
643 SetTileOwner(t, rail);
644 t.m2() = town.base();
645 t.m3() = 0;
646 t.m4() = INVALID_ROADTYPE;
647 t.m5() = ROAD_TILE_CROSSING << 6 | roaddir;
648 SB(t.m6(), 2, 4, 0);
649 t.m7() = road.base();
650 t.m8() = INVALID_ROADTYPE << 6 | rat;
651 SetRoadTypes(t, road_rt, tram_rt);
652 SetRoadOwner(t, RTT_TRAM, tram);
653}
654
661{
662 assert(IsRoadDepotTile(tile));
663 SB(tile.m5(), 0, 2, dir);
664}
665
674inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
675{
676 SetTileType(tile, MP_ROAD);
677 SetTileOwner(tile, owner);
678 tile.m2() = depot_id.base();
679 tile.m3() = 0;
680 tile.m4() = INVALID_ROADTYPE;
681 tile.m5() = ROAD_TILE_DEPOT << 6 | dir;
682 SB(tile.m6(), 2, 4, 0);
683 tile.m7() = owner.base();
684 tile.m8() = INVALID_ROADTYPE << 6;
685 SetRoadType(tile, GetRoadTramType(rt), rt);
686 SetRoadOwner(tile, RTT_TRAM, owner);
687}
688
689#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.
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:660
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:559
void SetRoadTypeTram(Tile t, RoadType rt)
Set the tram road type of a tile.
Definition road_map.h:571
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:599
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:545
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:615
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
@ ROAD_TILE_NORMAL
Normal road.
Definition road_map.h:23
@ ROAD_TILE_DEPOT
Depot (one entrance)
Definition road_map.h:25
@ ROAD_TILE_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:640
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:531
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:584
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:674
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
@ ROADSIDE_GRASS_ROAD_WORKS
Road on grass with road works.
Definition road_map.h:464
@ ROADSIDE_PAVED
Road with paved sidewalks.
Definition road_map.h:460
@ ROADSIDE_PAVED_ROAD_WORKS
Road with sidewalks and road works.
Definition road_map.h:465
@ ROADSIDE_STREET_LIGHTS
Road with street lights on paved sidewalks.
Definition road_map.h:461
@ ROADSIDE_BARREN
Road on barren land.
Definition road_map.h:458
@ ROADSIDE_GRASS
Road on grass.
Definition road_map.h:459
@ ROADSIDE_TREES
Road with trees on paved sidewalks.
Definition road_map.h:463
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:40
@ ROAD_Y
Full road along the y-axis (north-west + south-east)
Definition road_type.h:47
@ ROAD_X
Full road along the x-axis (south-west + north-east)
Definition road_type.h:46
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:61
@ DRD_END
Sentinel.
Definition road_type.h:66
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