OpenTTD
road_map.h
Go to the documentation of this file.
1 /* $Id: road_map.h 26878 2014-09-21 11:23:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef ROAD_MAP_H
13 #define ROAD_MAP_H
14 
15 #include "track_func.h"
16 #include "depot_type.h"
17 #include "rail_type.h"
18 #include "road_func.h"
19 #include "tile_map.h"
20 
21 
27 };
28 
36 {
37  assert(IsTileType(t, MP_ROAD));
38  return (RoadTileType)GB(_m[t].m5, 6, 2);
39 }
40 
47 static inline bool IsNormalRoad(TileIndex t)
48 {
49  return GetRoadTileType(t) == ROAD_TILE_NORMAL;
50 }
51 
57 static inline bool IsNormalRoadTile(TileIndex t)
58 {
59  return IsTileType(t, MP_ROAD) && IsNormalRoad(t);
60 }
61 
68 static inline bool IsLevelCrossing(TileIndex t)
69 {
71 }
72 
78 static inline bool IsLevelCrossingTile(TileIndex t)
79 {
80  return IsTileType(t, MP_ROAD) && IsLevelCrossing(t);
81 }
82 
89 static inline bool IsRoadDepot(TileIndex t)
90 {
91  return GetRoadTileType(t) == ROAD_TILE_DEPOT;
92 }
93 
99 static inline bool IsRoadDepotTile(TileIndex t)
100 {
101  return IsTileType(t, MP_ROAD) && IsRoadDepot(t);
102 }
103 
112 {
113  assert(IsNormalRoad(t));
114  switch (rt) {
115  default: NOT_REACHED();
116  case ROADTYPE_ROAD: return (RoadBits)GB(_m[t].m5, 0, 4);
117  case ROADTYPE_TRAM: return (RoadBits)GB(_m[t].m3, 0, 4);
118  }
119 }
120 
129 {
131 }
132 
139 static inline RoadBits GetAllRoadBits(TileIndex tile)
140 {
141  return GetRoadBits(tile, ROADTYPE_ROAD) | GetRoadBits(tile, ROADTYPE_TRAM);
142 }
143 
151 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
152 {
153  assert(IsNormalRoad(t)); // XXX incomplete
154  switch (rt) {
155  default: NOT_REACHED();
156  case ROADTYPE_ROAD: SB(_m[t].m5, 0, 4, r); break;
157  case ROADTYPE_TRAM: SB(_m[t].m3, 0, 4, r); break;
158  }
159 }
160 
167 {
168  return (RoadTypes)GB(_me[t].m7, 6, 2);
169 }
170 
176 static inline void SetRoadTypes(TileIndex t, RoadTypes rt)
177 {
179  SB(_me[t].m7, 6, 2, rt);
180 }
181 
188 static inline bool HasTileRoadType(TileIndex t, RoadType rt)
189 {
190  return HasBit(GetRoadTypes(t), rt);
191 }
192 
199 static inline Owner GetRoadOwner(TileIndex t, RoadType rt)
200 {
202  switch (rt) {
203  default: NOT_REACHED();
204  case ROADTYPE_ROAD: return (Owner)GB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5);
205  case ROADTYPE_TRAM: {
206  /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
207  * to OWNER_TOWN makes it use one bit less */
208  Owner o = (Owner)GB(_m[t].m3, 4, 4);
209  return o == OWNER_TOWN ? OWNER_NONE : o;
210  }
211  }
212 }
213 
220 static inline void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
221 {
222  switch (rt) {
223  default: NOT_REACHED();
224  case ROADTYPE_ROAD: SB(IsNormalRoadTile(t) ? _m[t].m1 : _me[t].m7, 0, 5, o); break;
225  case ROADTYPE_TRAM: SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
226  }
227 }
228 
237 static inline bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
238 {
239  assert(HasTileRoadType(t, rt));
240  return (GetRoadOwner(t, rt) == o);
241 }
242 
249 static inline bool HasTownOwnedRoad(TileIndex t)
250 {
252 }
253 
261 };
265 
272 {
273  assert(IsNormalRoad(t));
274  return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
275 }
276 
283 {
284  assert(IsNormalRoad(t));
285  assert(drd < DRD_END);
286  SB(_m[t].m5, 4, 2, drd);
287 }
288 
296 {
297  assert(IsLevelCrossing(t));
298  return (Axis)GB(_m[t].m5, 0, 1);
299 }
300 
308 {
309  assert(IsLevelCrossing(t));
310  return OtherAxis((Axis)GetCrossingRoadAxis(t));
311 }
312 
319 {
320  return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
321 }
322 
329 {
330  return AxisToTrack(GetCrossingRailAxis(tile));
331 }
332 
339 {
340  return AxisToTrackBits(GetCrossingRailAxis(tile));
341 }
342 
343 
350 static inline bool HasCrossingReservation(TileIndex t)
351 {
352  assert(IsLevelCrossingTile(t));
353  return HasBit(_m[t].m5, 4);
354 }
355 
363 static inline void SetCrossingReservation(TileIndex t, bool b)
364 {
365  assert(IsLevelCrossingTile(t));
366  SB(_m[t].m5, 4, 1, b ? 1 : 0);
367 }
368 
376 {
378 }
379 
386 static inline bool IsCrossingBarred(TileIndex t)
387 {
388  assert(IsLevelCrossing(t));
389  return HasBit(_m[t].m5, 5);
390 }
391 
398 static inline void SetCrossingBarred(TileIndex t, bool barred)
399 {
400  assert(IsLevelCrossing(t));
401  SB(_m[t].m5, 5, 1, barred ? 1 : 0);
402 }
403 
408 static inline void UnbarCrossing(TileIndex t)
409 {
410  SetCrossingBarred(t, false);
411 }
412 
417 static inline void BarCrossing(TileIndex t)
418 {
419  SetCrossingBarred(t, true);
420 }
421 
423 #define IsOnDesert IsOnSnow
424 
429 static inline bool IsOnSnow(TileIndex t)
430 {
431  return HasBit(_me[t].m7, 5);
432 }
433 
435 #define ToggleDesert ToggleSnow
436 
440 static inline void ToggleSnow(TileIndex t)
441 {
442  ToggleBit(_me[t].m7, 5);
443 }
444 
445 
447 enum Roadside {
455 };
456 
462 static inline Roadside GetRoadside(TileIndex tile)
463 {
464  return (Roadside)GB(_me[tile].m6, 3, 3);
465 }
466 
472 static inline void SetRoadside(TileIndex tile, Roadside s)
473 {
474  SB(_me[tile].m6, 3, 3, s);
475 }
476 
482 static inline bool HasRoadWorks(TileIndex t)
483 {
485 }
486 
492 static inline bool IncreaseRoadWorksCounter(TileIndex t)
493 {
494  AB(_me[t].m7, 0, 4, 1);
495 
496  return GB(_me[t].m7, 0, 4) == 15;
497 }
498 
504 static inline void StartRoadWorks(TileIndex t)
505 {
506  assert(!HasRoadWorks(t));
507  /* Remove any trees or lamps in case or roadwork */
508  switch (GetRoadside(t)) {
509  case ROADSIDE_BARREN:
511  default: SetRoadside(t, ROADSIDE_PAVED_ROAD_WORKS); break;
512  }
513 }
514 
520 static inline void TerminateRoadWorks(TileIndex t)
521 {
522  assert(HasRoadWorks(t));
524  /* Stop the counter */
525  SB(_me[t].m7, 0, 4, 0);
526 }
527 
528 
535 {
536  assert(IsRoadDepot(t));
537  return (DiagDirection)GB(_m[t].m5, 0, 2);
538 }
539 
540 
541 RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance = false);
542 
543 
553 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
554 {
555  SetTileType(t, MP_ROAD);
556  SetTileOwner(t, road);
557  _m[t].m2 = town;
558  _m[t].m3 = (HasBit(rot, ROADTYPE_TRAM) ? bits : 0);
559  _m[t].m4 = 0;
560  _m[t].m5 = (HasBit(rot, ROADTYPE_ROAD) ? bits : 0) | ROAD_TILE_NORMAL << 6;
561  SB(_me[t].m6, 2, 4, 0);
562  _me[t].m7 = rot << 6;
563  SetRoadOwner(t, ROADTYPE_TRAM, tram);
564 }
565 
577 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
578 {
579  SetTileType(t, MP_ROAD);
580  SetTileOwner(t, rail);
581  _m[t].m2 = town;
582  _m[t].m3 = rat;
583  _m[t].m4 = 0;
584  _m[t].m5 = ROAD_TILE_CROSSING << 6 | roaddir;
585  SB(_me[t].m6, 2, 4, 0);
586  _me[t].m7 = rot << 6 | road;
587  SetRoadOwner(t, ROADTYPE_TRAM, tram);
588 }
589 
598 static inline void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
599 {
600  SetTileType(t, MP_ROAD);
601  SetTileOwner(t, owner);
602  _m[t].m2 = did;
603  _m[t].m3 = 0;
604  _m[t].m4 = 0;
605  _m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
606  SB(_me[t].m6, 2, 4, 0);
607  _me[t].m7 = RoadTypeToRoadTypes(rt) << 6 | owner;
608  SetRoadOwner(t, ROADTYPE_TRAM, owner);
609 }
610 
611 #endif /* ROAD_MAP_H */
Helper template class that makes basic properties of given enumeration type visible from outsize...
Definition: enum_type.hpp:66
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
uint16 DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:15
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:191
Level crossing.
Definition: road_map.h:25
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
static void StartRoadWorks(TileIndex t)
Start road works on a tile.
Definition: road_map.h:504
Functions related to roads.
static RoadBits GetAllRoadBits(TileIndex tile)
Get all set RoadBits on the given tile.
Definition: road_map.h:139
static bool IsRoadOwner(TileIndex t, RoadType rt, Owner o)
Check if a specific road type is owned by an owner.
Definition: road_map.h:237
static T AB(T &x, const uint8 s, const uint8 n, const U i)
Add i to n bits of x starting at bit s.
static T ToggleBit(T &x, const uint8 y)
Toggles a bit in a variable.
Basic road type.
Definition: road_type.h:24
static bool HasCrossingReservation(TileIndex t)
Get the reservation state of the rail crossing.
Definition: road_map.h:350
byte m7
Primarily used for newgrf support.
Definition: map_type.h:37
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:22
Track
These are used to specify a single track.
Definition: track_type.h:21
static TrackBits GetCrossingRailBits(TileIndex tile)
Get the rail track bits of a level crossing.
Definition: road_map.h:338
A tile with road (or tram tracks)
Definition: tile_type.h:45
static TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:98
Full road along the x-axis (south-west + north-east)
Definition: road_type.h:61
Tile * _m
Tiles of the map.
Definition: map.cpp:32
static void MakeRoadDepot(TileIndex t, Owner owner, DepotID did, DiagDirection dir, RoadType rt)
Make a road depot.
Definition: road_map.h:598
static RoadTypes RoadTypeToRoadTypes(RoadType rt)
Maps a RoadType to the corresponding RoadTypes value.
Definition: road_func.h:56
static bool HasTileRoadType(TileIndex t, RoadType rt)
Check if a tile has a specific road type.
Definition: road_map.h:188
static RoadBits GetRoadBits(TileIndex t, RoadType rt)
Get the present road bits for a specific road type.
Definition: road_map.h:111
static Roadside GetRoadside(TileIndex tile)
Get the decorations of a road.
Definition: road_map.h:462
static void TerminateRoadWorks(TileIndex t)
Terminate road works on a tile.
Definition: road_map.h:520
static DiagDirection GetRoadDepotDirection(TileIndex t)
Get the direction of the exit of a road depot.
Definition: road_map.h:534
static 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:76
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:68
static void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
Sets the disallowed directions.
Definition: road_map.h:282
RoadType
The different roadtypes we support.
Definition: road_type.h:22
Road with paved sidewalks.
Definition: road_map.h:450
Road on grass.
Definition: road_map.h:449
RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance=false)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition: road_map.cpp:35
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Roadside
The possible road side decorations.
Definition: road_map.h:447
static bool HasRoadWorks(TileIndex t)
Check if a tile has road works.
Definition: road_map.h:482
static void SetCrossingBarred(TileIndex t, bool barred)
Set the bar state of a level crossing.
Definition: road_map.h:398
Normal road.
Definition: road_map.h:24
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
static bool IsOnSnow(TileIndex t)
Check if a road tile has snow/desert.
Definition: road_map.h:429
static void SetCrossingReservation(TileIndex t, bool b)
Set the reservation state of the rail crossing.
Definition: road_map.h:363
Header files for depots (not hangars)
None of the directions are disallowed.
Definition: road_map.h:256
All northbound traffic is disallowed.
Definition: road_map.h:258
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
static TrackBits GetCrossingReservationTrackBits(TileIndex t)
Get the reserved track bits for a rail crossing.
Definition: road_map.h:375
static Owner GetRoadOwner(TileIndex t, RoadType rt)
Get the owner of a specific road type.
Definition: road_map.h:199
Road with trees on paved sidewalks.
Definition: road_map.h:452
RoadTileType
The different types of road tiles.
Definition: road_map.h:23
Sentinel.
Definition: road_map.h:260
static void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
Set the owner of a specific road type.
Definition: road_map.h:220
static bool HasTownOwnedRoad(TileIndex t)
Checks if given tile has town owned road.
Definition: road_map.h:249
Road on barren land.
Definition: road_map.h:448
Trams.
Definition: road_type.h:25
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:52
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:33
byte m5
General purpose.
Definition: map_type.h:26
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:55
static bool IsRoadDepotTile(TileIndex t)
Return whether a tile is a road depot tile.
Definition: road_map.h:99
static Axis GetCrossingRailAxis(TileIndex t)
Get the rail axis of a level crossing.
Definition: road_map.h:307
static Axis GetCrossingRoadAxis(TileIndex t)
Get the road axis of a level crossing.
Definition: road_map.h:295
Road with street lights on paved sidewalks.
Definition: road_map.h:451
static void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
Set the present road bits for a specific road type.
Definition: road_map.h:151
The tile has no ownership.
Definition: company_type.h:27
DiagDirection
Enumeration for diagonal directions.
static void ToggleSnow(TileIndex t)
Toggle the snow/desert state of a road tile.
Definition: road_map.h:440
All directions are disallowed.
Definition: road_map.h:259
static void BarCrossing(TileIndex t)
Bar a level crossing.
Definition: road_map.h:417
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
static void SetRoadside(TileIndex tile, Roadside s)
Set the decorations of a road.
Definition: road_map.h:472
static RoadBits GetCrossingRoadBits(TileIndex tile)
Get the road bits of a level crossing.
Definition: road_map.h:318
The X axis.
static bool IsNormalRoadTile(TileIndex t)
Return whether a tile is a normal road tile.
Definition: road_map.h:57
static RoadTileType GetRoadTileType(TileIndex t)
Get the type of the road tile.
Definition: road_map.h:35
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
Road with sidewalks and road works.
Definition: road_map.h:454
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
A town owns the tile, or a town is expanding.
Definition: company_type.h:26
static bool IsLevelCrossingTile(TileIndex t)
Return whether a tile is a level crossing tile.
Definition: road_map.h:78
static void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
Make a level crossing.
Definition: road_map.h:577
static bool IncreaseRoadWorksCounter(TileIndex t)
Increase the progress counter of road works.
Definition: road_map.h:492
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:124
A tile of a station.
Definition: tile_type.h:48
static void UnbarCrossing(TileIndex t)
Unbar a level crossing.
Definition: road_map.h:408
Road on grass with road works.
Definition: road_map.h:453
static RoadBits GetOtherRoadBits(TileIndex t, RoadType rt)
Get all RoadBits set on a tile except from the given RoadType.
Definition: road_map.h:128
static Axis OtherAxis(Axis a)
Select the other axis as provided.
DisallowedRoadDirections
Which directions are disallowed ?
Definition: road_map.h:255
static void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram)
Make a normal road tile.
Definition: road_map.h:553
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable...
Definition: window_gui.h:319
static DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
Gets the disallowed directions.
Definition: road_map.h:271
static RoadTypes GetRoadTypes(TileIndex t)
Get the present road types of a tile.
Definition: road_map.h:166
static bool IsNormalRoad(TileIndex t)
Return whether a tile is a normal road.
Definition: road_map.h:47
All southbound traffic is disallowed.
Definition: road_map.h:257
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:386
Full road along the y-axis (north-west + south-east)
Definition: road_type.h:62
static Track GetCrossingRailTrack(TileIndex tile)
Get the rail track of a level crossing.
Definition: road_map.h:328
Map writing/reading functions for tiles.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Different conversion functions from one kind of track to another.
static void SetRoadTypes(TileIndex t, RoadTypes rt)
Set the present road types of a tile.
Definition: road_map.h:176
byte m3
General purpose.
Definition: map_type.h:24
Depot (one entrance)
Definition: road_map.h:26
No track.
Definition: track_type.h:42
Axis
Allow incrementing of DiagDirDiff variables.
static bool IsRoadDepot(TileIndex t)
Return whether a tile is a road depot.
Definition: road_map.h:89
The different types of rail.
byte m4
General purpose.
Definition: map_type.h:25