OpenTTD Source  20240917-master-g9ab0a47812
road.cpp
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 #include "stdafx.h"
11 #include "rail_map.h"
12 #include "road_map.h"
13 #include "water_map.h"
14 #include "genworld.h"
15 #include "company_func.h"
16 #include "company_base.h"
17 #include "engine_base.h"
19 #include "landscape.h"
20 #include "road.h"
21 #include "road_func.h"
22 #include "roadveh.h"
23 
24 #include "safeguards.h"
25 
33 static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
34 {
35  return (IsTileType(tile, MP_RAILWAY) &&
37  GetTrackBits(tile) == (ax == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) &&
38  std::get<0>(GetFoundationSlope(tile)) == SLOPE_FLAT);
39 }
40 
48 {
49  if (!IsValidTile(tile)) return ROAD_NONE;
50  for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
51  const TileIndex neighbor_tile = TileAddByDiagDir(tile, dir);
52 
53  /* Get the Roadbit pointing to the neighbor_tile */
54  const RoadBits target_rb = DiagDirToRoadBits(dir);
55 
56  /* If the roadbit is in the current plan */
57  if (org_rb & target_rb) {
58  bool connective = false;
59  const RoadBits mirrored_rb = MirrorRoadBits(target_rb);
60 
61  if (IsValidTile(neighbor_tile)) {
62  switch (GetTileType(neighbor_tile)) {
63  /* Always connective ones */
64  case MP_CLEAR: case MP_TREES:
65  connective = true;
66  break;
67 
68  /* The conditionally connective ones */
69  case MP_TUNNELBRIDGE:
70  case MP_STATION:
71  case MP_ROAD:
72  if (IsNormalRoadTile(neighbor_tile)) {
73  /* Always connective */
74  connective = true;
75  } else {
76  const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, RTT_ROAD) | GetAnyRoadBits(neighbor_tile, RTT_TRAM);
77 
78  /* Accept only connective tiles */
79  connective = (neighbor_rb & mirrored_rb) != ROAD_NONE;
80  }
81  break;
82 
83  case MP_RAILWAY:
84  connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
85  break;
86 
87  case MP_WATER:
88  /* Check for real water tile */
89  connective = !IsWater(neighbor_tile);
90  break;
91 
92  /* The definitely not connective ones */
93  default: break;
94  }
95  }
96 
97  /* If the neighbor tile is inconnective, remove the planned road connection to it */
98  if (!connective) org_rb ^= target_rb;
99  }
100  }
101 
102  return org_rb;
103 }
104 
111 bool HasRoadTypeAvail(const CompanyID company, RoadType roadtype)
112 {
113  if (company == OWNER_DEITY || company == OWNER_TOWN || _game_mode == GM_EDITOR || _generating_world) {
114  const RoadTypeInfo *rti = GetRoadTypeInfo(roadtype);
115  if (rti->label == 0) return false;
116 
117  /* Not yet introduced at this date. */
119 
120  /*
121  * Do not allow building hidden road types, except when a town may build it.
122  * The GS under deity mode, as well as anybody in the editor builds roads that are
123  * owned by towns. So if a town may build it, it should be buildable by them too.
124  */
125  return (rti->flags & ROTFB_HIDDEN) == 0 || (rti->flags & ROTFB_TOWN_BUILD) != 0;
126  } else {
127  const Company *c = Company::GetIfValid(company);
128  if (c == nullptr) return false;
129  return HasBit(c->avail_roadtypes & ~_roadtypes_hidden_mask, roadtype);
130  }
131 }
132 
133 static RoadTypes GetMaskForRoadTramType(RoadTramType rtt)
134 {
135  return rtt == RTT_TRAM ? _roadtypes_type : ~_roadtypes_type;
136 }
137 
143 bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
144 {
145  return (Company::Get(company)->avail_roadtypes & ~_roadtypes_hidden_mask & GetMaskForRoadTramType(rtt)) != ROADTYPES_NONE;
146 }
147 
154 {
155  return roadtype < ROADTYPE_END && HasRoadTypeAvail(_current_company, roadtype);
156 }
157 
166 RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, TimerGameCalendar::Date date)
167 {
168  RoadTypes rts = current;
169 
170  for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
171  const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
172  /* Unused road type. */
173  if (rti->label == 0) continue;
174 
175  /* Not date introduced. */
176  if (!IsInsideMM(rti->introduction_date, 0, CalendarTime::MAX_DATE.base())) continue;
177 
178  /* Not yet introduced at this date. */
179  if (rti->introduction_date > date) continue;
180 
181  /* Have we introduced all required roadtypes? */
183  if ((rts & required) != required) continue;
184 
185  rts |= rti->introduces_roadtypes;
186  }
187 
188  /* When we added roadtypes we need to run this method again; the added
189  * roadtypes might enable more rail types to become introduced. */
190  return rts == current ? rts : AddDateIntroducedRoadTypes(rts, date);
191 }
192 
199 RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
200 {
202 
203  for (const Engine *e : Engine::IterateType(VEH_ROAD)) {
204  const EngineInfo *ei = &e->info;
205 
207  (HasBit(e->company_avail, company) || TimerGameCalendar::date >= e->intro_date + CalendarTime::DAYS_IN_YEAR)) {
208  const RoadVehicleInfo *rvi = &e->u.road;
209  assert(rvi->roadtype < ROADTYPE_END);
210  if (introduces) {
212  } else {
213  SetBit(rts, rvi->roadtype);
214  }
215  }
216  }
217 
218  if (introduces) return AddDateIntroducedRoadTypes(rts, TimerGameCalendar::date);
219  return rts;
220 }
221 
227 RoadTypes GetRoadTypes(bool introduces)
228 {
230 
231  for (const Engine *e : Engine::IterateType(VEH_ROAD)) {
232  const EngineInfo *ei = &e->info;
234 
235  const RoadVehicleInfo *rvi = &e->u.road;
236  assert(rvi->roadtype < ROADTYPE_END);
237  if (introduces) {
239  } else {
240  SetBit(rts, rvi->roadtype);
241  }
242  }
243 
244  if (introduces) return AddDateIntroducedRoadTypes(rts, CalendarTime::MAX_DATE);
245  return rts;
246 }
247 
254 RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
255 {
256  /* Loop through each road type until the label is found */
257  for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
258  const RoadTypeInfo *rti = GetRoadTypeInfo(r);
259  if (rti->label == label) return r;
260  }
261 
262  if (allow_alternate_labels) {
263  /* Test if any road type defines the label as an alternate. */
264  for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
265  const RoadTypeInfo *rti = GetRoadTypeInfo(r);
266  if (std::find(rti->alternate_labels.begin(), rti->alternate_labels.end(), label) != rti->alternate_labels.end()) return r;
267  }
268  }
269 
270  /* No matching label was found, so it is invalid */
271  return INVALID_ROADTYPE;
272 }
RoadTypeInfo::flags
RoadTypeFlags flags
Bit mask of road type flags.
Definition: road.h:127
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:48
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
RoadTypeInfo
Definition: road.h:78
Engine::IterateType
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
Definition: engine_base.h:186
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
RoadBits
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:52
GetRoadTypeByLabel
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
Get the road type for a given label.
Definition: road.cpp:254
IsInsideMM
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:268
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:350
TRACK_BIT_X
@ TRACK_BIT_X
X-axis track.
Definition: track_type.h:37
RoadTypeInfo::introduction_date
TimerGameCalendar::Date introduction_date
Introduction date.
Definition: road.h:166
ROTFB_HIDDEN
@ ROTFB_HIDDEN
Value for hidden from construction.
Definition: road.h:51
RoadTypeInfo::introduces_roadtypes
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition: road.h:177
Company::avail_roadtypes
RoadTypes avail_roadtypes
Road types available to this company.
Definition: company_base.h:138
company_base.h
timer_game_calendar.h
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
road_func.h
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DiagDirToAxis
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
ROADTYPE_BEGIN
@ ROADTYPE_BEGIN
Used for iterations.
Definition: road_type.h:26
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
HasRoadTypeAvail
bool HasRoadTypeAvail(const CompanyID company, RoadType roadtype)
Finds out, whether given company has a given RoadType available for construction.
Definition: road.cpp:111
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
GetCompanyRoadTypes
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
Get the road types the given company can build.
Definition: road.cpp:199
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
EngineInfo
Information about a vehicle.
Definition: engine_type.h:144
Engine
Definition: engine_base.h:37
RoadVehicleInfo::roadtype
RoadType roadtype
Road type.
Definition: engine_type.h:128
GameCreationSettings::landscape
uint8_t landscape
the landscape we're currently in
Definition: settings_type.h:368
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:50
genworld.h
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:594
GetTileType
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
TimerGameConst< struct Calendar >::MAX_DATE
static constexpr TimerGame< struct Calendar >::Date MAX_DATE
The date of the last day of the max year.
Definition: timer_game_common.h:187
ROADTYPES_NONE
@ ROADTYPES_NONE
No roadtypes.
Definition: road_type.h:39
_roadtypes_type
RoadTypes _roadtypes_type
Bitmap of road/tram types.
Definition: road_cmd.cpp:62
IsWater
bool IsWater(Tile t)
Is it a plain water tile?
Definition: water_map.h:150
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:38
ROTFB_TOWN_BUILD
@ ROTFB_TOWN_BUILD
Value for allowing towns to build this roadtype.
Definition: road.h:52
DIAGDIR_BEGIN
@ DIAGDIR_BEGIN
Used for iterations.
Definition: direction_type.h:74
RoadVehicleInfo
Information about a road vehicle.
Definition: engine_type.h:114
rail_map.h
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
HasAnyRoadTypesAvail
bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
Test if any buildable RoadType is available for a company.
Definition: road.cpp:143
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
RoadTypeInfo::alternate_labels
RoadTypeLabelList alternate_labels
Road type labels this type provides in addition to the main label.
Definition: road.h:152
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:25
EngineInfo::climates
uint8_t climates
Climates supported by the engine.
Definition: engine_type.h:150
safeguards.h
IsNormalRoadTile
static debug_inline bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition: road_map.h:74
CleanUpRoadBits
RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
Clean up unnecessary RoadBits of a planned tile.
Definition: road.cpp:47
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
road.h
INVALID_ROADTYPE
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition: road_type.h:30
ValParamRoadType
bool ValParamRoadType(RoadType roadtype)
Validate functions for rail building.
Definition: road.cpp:153
DiagDirToRoadBits
RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:96
GetRailTileType
static debug_inline RailTileType GetRailTileType(Tile t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:36
stdafx.h
landscape.h
OWNER_TOWN
@ OWNER_TOWN
A town owns the tile, or a town is expanding.
Definition: company_type.h:24
road_map.h
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:117
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition: tile_map.h:161
GetTrackBits
TrackBits GetTrackBits(Tile tile)
Gets the track bits of the given tile.
Definition: rail_map.h:136
MP_TREES
@ MP_TREES
Tile got trees.
Definition: tile_type.h:52
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:227
water_map.h
_generating_world
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:67
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:29
AddDateIntroducedRoadTypes
RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, TimerGameCalendar::Date date)
Add the road types that are to be introduced at the given date.
Definition: road.cpp:166
GetAnyRoadBits
RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition: road_map.cpp:33
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:53
RoadTypeInfo::label
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition: road.h:147
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:116
IsPossibleCrossing
static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
Return if the tile is a valid tile for a crossing.
Definition: road.cpp:33
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
RAIL_TILE_NORMAL
@ RAIL_TILE_NORMAL
Normal rail tile without signals.
Definition: rail_map.h:24
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:79
GetFoundationSlope
std::tuple< Slope, int > GetFoundationSlope(TileIndex tile)
Get slope of a tile on top of a (possible) foundation If a tile does not have a foundation,...
Definition: landscape.cpp:382
TRACK_BIT_Y
@ TRACK_BIT_Y
Y-axis track.
Definition: track_type.h:38
company_func.h
ROAD_NONE
@ ROAD_NONE
No road-part is build.
Definition: road_type.h:53
engine_base.h
TimerGameCalendar::date
static Date date
Current date in days (day counter).
Definition: timer_game_calendar.h:34
MirrorRoadBits
RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:51
GetRoadTypes
RoadTypes GetRoadTypes(bool introduces)
Get list of road types, regardless of company availability.
Definition: road.cpp:227
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
Company
Definition: company_base.h:133
TimerGameConst< struct Calendar >::DAYS_IN_YEAR
static constexpr int DAYS_IN_YEAR
days per year
Definition: timer_game_common.h:149
TileAddByDiagDir
TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:606
RoadTypeInfo::introduction_required_roadtypes
RoadTypes introduction_required_roadtypes
Bitmask of roadtypes that are required for this roadtype to be introduced at a given introduction_dat...
Definition: road.h:172
roadveh.h
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103