OpenTTD Source 15.0-beta2
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
437#define IsOnDesert IsOnSnow
443inline bool IsOnSnow(Tile t)
444{
445 return HasBit(t.m7(), 5);
446}
447
449#define ToggleDesert ToggleSnow
454inline void ToggleSnow(Tile t)
455{
456 ToggleBit(t.m7(), 5);
457}
458
459
471
478{
479 return (Roadside)GB(tile.m6(), 3, 3);
480}
481
487inline void SetRoadside(Tile tile, Roadside s)
488{
489 SB(tile.m6(), 3, 3, s);
490}
491
497inline bool HasRoadWorks(Tile t)
498{
500}
501
508{
509 AB(t.m7(), 0, 4, 1);
510
511 return GB(t.m7(), 0, 4) == 15;
512}
513
519inline void StartRoadWorks(Tile t)
520{
521 assert(!HasRoadWorks(t));
522 /* Remove any trees or lamps in case or roadwork */
523 switch (GetRoadside(t)) {
524 case ROADSIDE_BARREN:
526 default: SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
527 }
528}
529
536{
537 assert(HasRoadWorks(t));
539 /* Stop the counter */
540 SB(t.m7(), 0, 4, 0);
541}
542
543
550{
551 assert(IsRoadDepot(t));
552 return (DiagDirection)GB(t.m5(), 0, 2);
553}
554
555
556RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance = false);
557
563inline void SetRoadTypeRoad(Tile t, RoadType rt)
564{
565 assert(MayHaveRoad(t));
566 assert(rt == INVALID_ROADTYPE || RoadTypeIsRoad(rt));
567 SB(t.m4(), 0, 6, rt);
568}
569
575inline void SetRoadTypeTram(Tile t, RoadType rt)
576{
577 assert(MayHaveRoad(t));
578 assert(rt == INVALID_ROADTYPE || RoadTypeIsTram(rt));
579 SB(t.m8(), 6, 6, rt);
580}
581
588inline void SetRoadType(Tile t, RoadTramType rtt, RoadType rt)
589{
590 if (rtt == RTT_TRAM) {
591 SetRoadTypeTram(t, rt);
592 } else {
593 SetRoadTypeRoad(t, rt);
594 }
595}
596
603inline void SetRoadTypes(Tile t, RoadType road_rt, RoadType tram_rt)
604{
605 SetRoadTypeRoad(t, road_rt);
606 SetRoadTypeTram(t, tram_rt);
607}
608
619inline void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tram_rt, TownID town, Owner road, Owner tram)
620{
622 SetTileOwner(t, road);
623 t.m2() = town.base();
624 t.m3() = (tram_rt != INVALID_ROADTYPE ? bits : 0);
625 t.m5() = (road_rt != INVALID_ROADTYPE ? bits : 0) | ROAD_TILE_NORMAL << 6;
626 SB(t.m6(), 2, 4, 0);
627 t.m7() = 0;
628 SetRoadTypes(t, road_rt, tram_rt);
629 SetRoadOwner(t, RTT_TRAM, tram);
630}
631
644inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadType road_rt, RoadType tram_rt, TownID town)
645{
647 SetTileOwner(t, rail);
648 t.m2() = town.base();
649 t.m3() = 0;
650 t.m4() = INVALID_ROADTYPE;
651 t.m5() = ROAD_TILE_CROSSING << 6 | roaddir;
652 SB(t.m6(), 2, 4, 0);
653 t.m7() = road.base();
654 t.m8() = INVALID_ROADTYPE << 6 | rat;
655 SetRoadTypes(t, road_rt, tram_rt);
656 SetRoadOwner(t, RTT_TRAM, tram);
657}
658
665{
666 assert(IsRoadDepotTile(tile));
667 SB(tile.m5(), 0, 2, dir);
668}
669
678inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirection dir, RoadType rt)
679{
680 SetTileType(tile, MP_ROAD);
681 SetTileOwner(tile, owner);
682 tile.m2() = depot_id.base();
683 tile.m3() = 0;
684 tile.m4() = INVALID_ROADTYPE;
685 tile.m5() = ROAD_TILE_DEPOT << 6 | dir;
686 SB(tile.m6(), 2, 4, 0);
687 tile.m7() = owner.base();
688 tile.m8() = INVALID_ROADTYPE << 6;
689 SetRoadType(tile, GetRoadTramType(rt), rt);
690 SetRoadOwner(tile, RTT_TRAM, owner);
691}
692
693#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:664
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:563
void SetRoadTypeTram(Tile t, RoadType rt)
Set the tram road type of a tile.
Definition road_map.h:575
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:507
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:519
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:603
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:549
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:619
Roadside GetRoadside(Tile tile)
Get the decorations of a road.
Definition road_map.h:477
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:644
RoadBits GetAllRoadBits(Tile tile)
Get all set RoadBits on the given tile.
Definition road_map.h:125
void ToggleSnow(Tile t)
Toggle the snow/desert state of a road tile.
Definition road_map.h:454
bool IsOnSnow(Tile t)
Check if a road tile has snow/desert.
Definition road_map.h:443
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:535
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:588
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:487
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:678
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:461
@ ROADSIDE_GRASS_ROAD_WORKS
Road on grass with road works.
Definition road_map.h:468
@ ROADSIDE_PAVED
Road with paved sidewalks.
Definition road_map.h:464
@ ROADSIDE_PAVED_ROAD_WORKS
Road with sidewalks and road works.
Definition road_map.h:469
@ ROADSIDE_STREET_LIGHTS
Road with street lights on paved sidewalks.
Definition road_map.h:465
@ ROADSIDE_BARREN
Road on barren land.
Definition road_map.h:462
@ ROADSIDE_GRASS
Road on grass.
Definition road_map.h:463
@ ROADSIDE_TREES
Road with trees on paved sidewalks.
Definition road_map.h:467
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
bool HasRoadWorks(Tile t)
Check if a tile has road works.
Definition road_map.h:497
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