OpenTTD Source 20241224-master-gee860a5c8e
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
33inline bool MayHaveRoad(Tile t)
34{
35 switch (GetTileType(t)) {
36 case MP_ROAD:
37 case MP_STATION:
38 case MP_TUNNELBRIDGE:
39 return true;
40
41 default:
42 return false;
43 }
44}
45
52debug_inline static RoadTileType GetRoadTileType(Tile t)
53{
54 assert(IsTileType(t, MP_ROAD));
55 return (RoadTileType)GB(t.m5(), 6, 2);
56}
57
64debug_inline static bool IsNormalRoad(Tile t)
65{
67}
68
74debug_inline static bool IsNormalRoadTile(Tile t)
75{
76 return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
77}
78
85inline bool IsLevelCrossing(Tile t)
86{
88}
89
96{
97 return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
98}
99
106debug_inline static bool IsRoadDepot(Tile t)
107{
108 return GetRoadTileType(t) == ROAD_TILE_DEPOT;
109}
110
116debug_inline static bool IsRoadDepotTile(Tile t)
117{
118 return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
119}
120
128inline RoadBits GetRoadBits(Tile t, RoadTramType rtt)
129{
130 assert(IsNormalRoad(t));
131 if (rtt == RTT_TRAM) return (RoadBits)GB(t.m3(), 0, 4);
132 return (RoadBits)GB(t.m5(), 0, 4);
133}
134
142{
143 return GetRoadBits(tile, RTT_ROAD) | GetRoadBits(tile, RTT_TRAM);
144}
145
153inline void SetRoadBits(Tile t, RoadBits r, RoadTramType rtt)
154{
155 assert(IsNormalRoad(t)); // XXX incomplete
156 if (rtt == RTT_TRAM) {
157 SB(t.m3(), 0, 4, r);
158 } else {
159 SB(t.m5(), 0, 4, r);
160 }
161}
162
163inline RoadType GetRoadTypeRoad(Tile t)
164{
165 assert(MayHaveRoad(t));
166 return (RoadType)GB(t.m4(), 0, 6);
167}
168
169inline RoadType GetRoadTypeTram(Tile t)
170{
171 assert(MayHaveRoad(t));
172 return (RoadType)GB(t.m8(), 6, 6);
173}
174
175inline RoadType GetRoadType(Tile t, RoadTramType rtt)
176{
177 return (rtt == RTT_TRAM) ? GetRoadTypeTram(t) : GetRoadTypeRoad(t);
178}
179
186{
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));
191 }
192 return result;
193}
194
195inline bool HasRoadTypeRoad(Tile t)
196{
197 return GetRoadTypeRoad(t) != INVALID_ROADTYPE;
198}
199
200inline bool HasRoadTypeTram(Tile t)
201{
202 return GetRoadTypeTram(t) != INVALID_ROADTYPE;
203}
204
211inline bool HasTileRoadType(Tile t, RoadTramType rtt)
212{
213 return GetRoadType(t, rtt) != INVALID_ROADTYPE;
214}
215
223{
224 if (!MayHaveRoad(t)) return false;
225 return (GetPresentRoadTypes(t) & rts);
226}
227
234inline Owner GetRoadOwner(Tile t, RoadTramType rtt)
235{
236 assert(MayHaveRoad(t));
237 if (rtt == RTT_ROAD) return (Owner)GB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5);
238
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;
243}
244
251inline void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
252{
253 if (rtt == RTT_ROAD) {
254 SB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5, o);
255 } else {
256 SB(t.m3(), 4, 4, o == OWNER_NONE ? OWNER_TOWN : o);
257 }
258}
259
268inline bool IsRoadOwner(Tile t, RoadTramType rtt, Owner o)
269{
270 assert(HasTileRoadType(t, rtt));
271 return (GetRoadOwner(t, rtt) == o);
272}
273
280inline bool HasTownOwnedRoad(Tile t)
281{
282 return HasTileRoadType(t, RTT_ROAD) && IsRoadOwner(t, RTT_ROAD, OWNER_TOWN);
283}
284
292{
293 return drt < DRD_END;
294}
295
302{
303 assert(IsNormalRoad(t));
304 return (DisallowedRoadDirections)GB(t.m5(), 4, 2);
305}
306
313{
314 assert(IsNormalRoad(t));
315 assert(drd < DRD_END);
316 SB(t.m5(), 4, 2, drd);
317}
318
326{
327 assert(IsLevelCrossing(t));
328 return (Axis)GB(t.m5(), 0, 1);
329}
330
338{
339 assert(IsLevelCrossing(t));
341}
342
349{
350 return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
351}
352
359{
360 return AxisToTrack(GetCrossingRailAxis(tile));
361}
362
369{
371}
372
373
381{
382 assert(IsLevelCrossingTile(t));
383 return HasBit(t.m5(), 4);
384}
385
393inline void SetCrossingReservation(Tile t, bool b)
394{
395 assert(IsLevelCrossingTile(t));
396 AssignBit(t.m5(), 4, b);
397}
398
409
416inline bool IsCrossingBarred(Tile t)
417{
418 assert(IsLevelCrossing(t));
419 return HasBit(t.m5(), 5);
420}
421
428inline void SetCrossingBarred(Tile t, bool barred)
429{
430 assert(IsLevelCrossing(t));
431 AssignBit(t.m5(), 5, barred);
432}
433
438inline void UnbarCrossing(Tile t)
439{
440 SetCrossingBarred(t, false);
441}
442
447inline void BarCrossing(Tile t)
448{
449 SetCrossingBarred(t, true);
450}
451
453#define IsOnDesert IsOnSnow
459inline bool IsOnSnow(Tile t)
460{
461 return HasBit(t.m7(), 5);
462}
463
465#define ToggleDesert ToggleSnow
470inline void ToggleSnow(Tile t)
471{
472 ToggleBit(t.m7(), 5);
473}
474
475
487
494{
495 return (Roadside)GB(tile.m6(), 3, 3);
496}
497
503inline void SetRoadside(Tile tile, Roadside s)
504{
505 SB(tile.m6(), 3, 3, s);
506}
507
513inline bool HasRoadWorks(Tile t)
514{
516}
517
524{
525 AB(t.m7(), 0, 4, 1);
526
527 return GB(t.m7(), 0, 4) == 15;
528}
529
535inline void StartRoadWorks(Tile t)
536{
537 assert(!HasRoadWorks(t));
538 /* Remove any trees or lamps in case or roadwork */
539 switch (GetRoadside(t)) {
540 case ROADSIDE_BARREN:
542 default: SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); 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 (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 == RTT_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;
640 t.m3() = (tram_rt != INVALID_ROADTYPE ? bits : 0);
641 t.m5() = (road_rt != INVALID_ROADTYPE ? bits : 0) | ROAD_TILE_NORMAL << 6;
642 SB(t.m6(), 2, 4, 0);
643 t.m7() = 0;
644 SetRoadTypes(t, road_rt, tram_rt);
645 SetRoadOwner(t, RTT_TRAM, tram);
646}
647
660inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, uint town)
661{
663 SetTileOwner(t, rail);
664 t.m2() = town;
665 t.m3() = 0;
666 t.m4() = INVALID_ROADTYPE;
667 t.m5() = ROAD_TILE_CROSSING << 6 | roaddir;
668 SB(t.m6(), 2, 4, 0);
669 t.m7() = road;
670 t.m8() = INVALID_ROADTYPE << 6 | rat;
671 SetRoadTypes(t, road_rt, tram_rt);
672 SetRoadOwner(t, RTT_TRAM, tram);
673}
674
681{
682 assert(IsRoadDepotTile(tile));
683 SB(tile.m5(), 0, 2, dir);
684}
685
694inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
695{
696 SetTileType(tile, MP_ROAD);
697 SetTileOwner(tile, owner);
698 tile.m2() = depot_id;
699 tile.m3() = 0;
700 tile.m4() = INVALID_ROADTYPE;
701 tile.m5() = ROAD_TILE_DEPOT << 6 | dir;
702 SB(tile.m6(), 2, 4, 0);
703 tile.m7() = owner;
704 tile.m8() = INVALID_ROADTYPE << 6;
705 SetRoadType(tile, GetRoadTramType(rt), rt);
706 SetRoadOwner(tile, RTT_TRAM, owner);
707}
708
709#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 SetBit(T &x, const uint8_t y)
Set a bit in a variable.
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.
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
Owner
Enum for all companies/owners.
@ OWNER_NONE
The tile has no ownership.
@ OWNER_TOWN
A town owns the tile, or a town is expanding.
Header files for depots (not hangars)
uint16_t DepotID
Type for the unique identifier of depots.
Definition depot_type.h:13
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:27
Functions related to roads.
void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition road_map.h:251
bool HasCrossingReservation(Tile t)
Get the reservation state of the rail crossing.
Definition road_map.h:380
bool HasTownOwnedRoad(Tile t)
Checks if given tile has town owned road.
Definition road_map.h:280
static debug_inline bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition road_map.h:74
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:106
void SetRoadDepotExitDirection(Tile tile, DiagDirection dir)
Sets the exit direction of a road depot.
Definition road_map.h:680
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:33
bool IsLevelCrossingTile(Tile t)
Return whether a tile is a level crossing tile.
Definition road_map.h:95
bool IncreaseRoadWorksCounter(Tile t)
Increase the progress counter of road works.
Definition road_map.h:523
RoadBits GetRoadBits(Tile t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition road_map.h:128
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
bool MayHaveRoad(Tile t)
Test whether a tile can have road/tram types.
Definition road_map.h:33
bool HasTileAnyRoadType(Tile t, RoadTypes rts)
Check if a tile has one of the specified road types.
Definition road_map.h:222
bool IsValidDisallowedRoadDirections(DisallowedRoadDirections drt)
Checks if a DisallowedRoadDirections is valid.
Definition road_map.h:291
Axis GetCrossingRoadAxis(Tile t)
Get the road axis of a level crossing.
Definition road_map.h:325
TrackBits GetCrossingRailBits(Tile tile)
Get the rail track bits of a level crossing.
Definition road_map.h:368
void SetDisallowedRoadDirections(Tile t, DisallowedRoadDirections drd)
Sets the disallowed directions.
Definition road_map.h:312
Track GetCrossingRailTrack(Tile tile)
Get the rail track of a level crossing.
Definition road_map.h:358
void StartRoadWorks(Tile t)
Start road works on a tile.
Definition road_map.h:535
void SetCrossingBarred(Tile t, bool barred)
Set the bar state of a level crossing.
Definition road_map.h:428
DisallowedRoadDirections GetDisallowedRoadDirections(Tile t)
Gets the disallowed directions.
Definition road_map.h:301
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:211
DiagDirection GetRoadDepotDirection(Tile t)
Get the direction of the exit of a road depot.
Definition road_map.h:565
static debug_inline bool IsRoadDepotTile(Tile t)
Return whether a tile is a road depot tile.
Definition road_map.h:116
void BarCrossing(Tile t)
Bar a level crossing.
Definition road_map.h:447
void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, uint town)
Make a level crossing.
Definition road_map.h:660
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:493
RoadBits GetAllRoadBits(Tile tile)
Get all set RoadBits on the given tile.
Definition road_map.h:141
void ToggleSnow(Tile t)
Toggle the snow/desert state of a road tile.
Definition road_map.h:470
bool IsOnSnow(Tile t)
Check if a road tile has snow/desert.
Definition road_map.h:459
Axis GetCrossingRailAxis(Tile t)
Get the rail axis of a level crossing.
Definition road_map.h:337
TrackBits GetCrossingReservationTrackBits(Tile t)
Get the reserved track bits for a rail crossing.
Definition road_map.h:405
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:348
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:234
static debug_inline RoadTileType GetRoadTileType(Tile t)
Get the type of the road tile.
Definition road_map.h:52
bool IsCrossingBarred(Tile t)
Check if the level crossing is barred.
Definition road_map.h:416
void SetRoadside(Tile tile, Roadside s)
Set the decorations of a road.
Definition road_map.h:503
void UnbarCrossing(Tile t)
Unbar a level crossing.
Definition road_map.h:438
bool IsRoadOwner(Tile t, RoadTramType rtt, Owner o)
Check if a specific road type is owned by an owner.
Definition road_map.h:268
void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
Make a road depot.
Definition road_map.h:694
void SetCrossingReservation(Tile t, bool b)
Set the reservation state of the rail crossing.
Definition road_map.h:393
void SetRoadBits(Tile t, RoadBits r, RoadTramType rtt)
Set the present road bits for a specific road type.
Definition road_map.h:153
Roadside
The possible road side decorations.
Definition road_map.h:477
@ ROADSIDE_GRASS_ROAD_WORKS
Road on grass with road works.
Definition road_map.h:484
@ ROADSIDE_PAVED
Road with paved sidewalks.
Definition road_map.h:480
@ ROADSIDE_PAVED_ROAD_WORKS
Road with sidewalks and road works.
Definition road_map.h:485
@ ROADSIDE_STREET_LIGHTS
Road with street lights on paved sidewalks.
Definition road_map.h:481
@ ROADSIDE_BARREN
Road on barren land.
Definition road_map.h:478
@ ROADSIDE_GRASS
Road on grass.
Definition road_map.h:479
@ ROADSIDE_TREES
Road with trees on paved sidewalks.
Definition road_map.h:483
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:85
bool HasRoadWorks(Tile t)
Check if a tile has road works.
Definition road_map.h:513
static debug_inline bool IsNormalRoad(Tile t)
Return whether a tile is a normal road.
Definition road_map.h:64
Enums and other types related to roads.
RoadBits
Enumeration for the road parts on a tile.
Definition road_type.h:52
@ ROAD_Y
Full road along the y-axis (north-west + south-east)
Definition road_type.h:59
@ ROAD_X
Full road along the x-axis (south-west + north-east)
Definition road_type.h:58
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition road_type.h:38
@ ROADTYPES_NONE
No roadtypes.
Definition road_type.h:39
RoadType
The different roadtypes we support.
Definition road_type.h:25
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:30
DisallowedRoadDirections
Which directions are disallowed ?
Definition road_type.h:73
@ DRD_END
Sentinel.
Definition road_type.h:78
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 TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
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
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
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