OpenTTD Source  1.11.0-beta2
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"
18 #include "date_func.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) &&
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  return true; // TODO: should there be a proper check?
115  } else {
116  const Company *c = Company::GetIfValid(company);
117  if (c == nullptr) return false;
118  return HasBit(c->avail_roadtypes & ~_roadtypes_hidden_mask, roadtype);
119  }
120 }
121 
122 static RoadTypes GetMaskForRoadTramType(RoadTramType rtt)
123 {
124  return rtt == RTT_TRAM ? _roadtypes_type : ~_roadtypes_type;
125 }
126 
132 bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
133 {
134  return (Company::Get(company)->avail_roadtypes & ~_roadtypes_hidden_mask & GetMaskForRoadTramType(rtt)) != ROADTYPES_NONE;
135 }
136 
143 {
144  return roadtype != INVALID_ROADTYPE && HasRoadTypeAvail(_current_company, roadtype);
145 }
146 
156 {
157  RoadTypes rts = current;
158 
159  for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
160  const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
161  /* Unused road type. */
162  if (rti->label == 0) continue;
163 
164  /* Not date introduced. */
165  if (!IsInsideMM(rti->introduction_date, 0, MAX_DAY)) continue;
166 
167  /* Not yet introduced at this date. */
168  if (rti->introduction_date > date) continue;
169 
170  /* Have we introduced all required roadtypes? */
172  if ((rts & required) != required) continue;
173 
174  rts |= rti->introduces_roadtypes;
175  }
176 
177  /* When we added roadtypes we need to run this method again; the added
178  * roadtypes might enable more rail types to become introduced. */
179  return rts == current ? rts : AddDateIntroducedRoadTypes(rts, date);
180 }
181 
188 RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
189 {
191 
192  for (const Engine *e : Engine::IterateType(VEH_ROAD)) {
193  const EngineInfo *ei = &e->info;
194 
196  (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
197  const RoadVehicleInfo *rvi = &e->u.road;
198  assert(rvi->roadtype < ROADTYPE_END);
199  if (introduces) {
201  } else {
202  SetBit(rts, rvi->roadtype);
203  }
204  }
205  }
206 
207  if (introduces) return AddDateIntroducedRoadTypes(rts, _date);
208  return rts;
209 }
210 
216 RoadTypes GetRoadTypes(bool introduces)
217 {
219 
220  for (const Engine *e : Engine::IterateType(VEH_ROAD)) {
221  const EngineInfo *ei = &e->info;
223 
224  const RoadVehicleInfo *rvi = &e->u.road;
225  assert(rvi->roadtype < ROADTYPE_END);
226  if (introduces) {
228  } else {
229  SetBit(rts, rvi->roadtype);
230  }
231  }
232 
233  if (introduces) return AddDateIntroducedRoadTypes(rts, MAX_DAY);
234  return rts;
235 }
236 
243 RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
244 {
245  /* Loop through each road type until the label is found */
246  for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
247  const RoadTypeInfo *rti = GetRoadTypeInfo(r);
248  if (rti->label == label) return r;
249  }
250 
251  if (allow_alternate_labels) {
252  /* Test if any road type defines the label as an alternate. */
253  for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
254  const RoadTypeInfo *rti = GetRoadTypeInfo(r);
255  if (std::find(rti->alternate_labels.begin(), rti->alternate_labels.end(), label) != rti->alternate_labels.end()) return r;
256  }
257  }
258 
259  /* No matching label was found, so it is invalid */
260  return INVALID_ROADTYPE;
261 }
262 
273 {
274  /* Check only players which can actually own vehicles, editor and gamescripts are considered deities */
275  if (c < OWNER_END) {
276  const Company *company = Company::GetIfValid(c);
277  if (company != nullptr) return company->avail_roadtypes;
278  }
279 
280  RoadTypes known_roadtypes = ROADTYPES_NONE;
281 
282  /* Find used roadtypes */
283  for (Engine *e : Engine::IterateType(VEH_ROAD)) {
284  /* Check if the roadtype can be used in the current climate */
285  if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
286 
287  /* Check whether available for all potential companies */
288  if (e->company_avail != (CompanyMask)-1) continue;
289 
290  known_roadtypes |= GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes;
291  }
292 
293  /* Get the date introduced roadtypes as well. */
294  known_roadtypes = AddDateIntroducedRoadTypes(known_roadtypes, MAX_DAY);
295 
296  return known_roadtypes;
297 }
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:41
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
RoadTypeInfo
Definition: road.h:75
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:157
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
GetRoadTypeByLabel
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
Get the road type for a given label.
Definition: road.cpp:243
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:340
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:295
RoadTypeInfo::introduces_roadtypes
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition: road.h:174
Company::avail_roadtypes
RoadTypes avail_roadtypes
Road types available to this company.
Definition: company_base.h:116
company_base.h
INVALID_ROADTYPE
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition: road_type.h:27
DiagDirToAxis
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
road_func.h
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:83
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:42
HasRoadTypeAvail
bool HasRoadTypeAvail(const CompanyID company, RoadType roadtype)
Finds out, whether given company has a given RoadType available for construction.
Definition: road.cpp:111
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:26
GetTrackBits
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:136
AddDateIntroducedRoadTypes
RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, Date date)
Add the road types that are to be introduced at the given date.
Definition: road.cpp:155
GetCompanyRoadTypes
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
Get the road types the given company can build.
Definition: road.cpp:188
EngineInfo
Information about a vehicle.
Definition: engine_type.h:132
Engine
Definition: engine_base.h:21
MAX_DAY
#define MAX_DAY
The number of days till the last day.
Definition: date_type.h:97
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
RoadVehicleInfo::roadtype
RoadType roadtype
Road type.
Definition: engine_type.h:125
RoadTypeInfo::introduction_date
Date introduction_date
Introduction date.
Definition: road.h:163
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:43
genworld.h
DiagDirToRoadBits
static RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:96
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:550
IsInsideMM
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:204
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
RoadBits
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:50
ROAD_NONE
@ ROAD_NONE
No road-part is build.
Definition: road_type.h:51
_roadtypes_type
RoadTypes _roadtypes_type
Bitmap of road/tram types.
Definition: road_cmd.cpp:57
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
GetAnyRoadBits
RoadBits GetAnyRoadBits(TileIndex tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition: road_map.cpp:33
GetRailTileType
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:36
RoadVehicleInfo
Information about a road vehicle.
Definition: engine_type.h:111
rail_map.h
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:47
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
HasAnyRoadTypesAvail
bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
Test if any buildable RoadType is available for a company.
Definition: road.cpp:132
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
RoadTypeInfo::alternate_labels
RoadTypeLabelList alternate_labels
Road type labels this type provides in addition to the main label.
Definition: road.h:149
TRACK_BIT_X
@ TRACK_BIT_X
X-axis track.
Definition: track_type.h:40
safeguards.h
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:161
TileAddByDiagDir
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:382
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:50
road.h
ValParamRoadType
bool ValParamRoadType(RoadType roadtype)
Validate functions for rail building.
Definition: road.cpp:142
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
GetFoundationSlope
Slope GetFoundationSlope(TileIndex tile, int *z)
Get slope of a tile on top of a (possible) foundation If a tile does not have a foundation,...
Definition: landscape.cpp:422
ROADTYPE_BEGIN
@ ROADTYPE_BEGIN
Used for iterations.
Definition: road_type.h:23
date_func.h
stdafx.h
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:22
landscape.h
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
road_map.h
MP_TREES
@ MP_TREES
Tile got trees.
Definition: tile_type.h:45
water_map.h
_generating_world
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:60
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
RoadTypeInfo::label
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition: road.h:144
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:46
RAIL_TILE_NORMAL
@ RAIL_TILE_NORMAL
Normal rail tile without signals.
Definition: rail_map.h:24
DIAGDIR_BEGIN
@ DIAGDIR_BEGIN
Used for iterations.
Definition: direction_type.h:78
DAYS_IN_YEAR
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:29
GetRoadTypeInfo
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:224
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
company_func.h
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
ExistingRoadTypes
RoadTypes ExistingRoadTypes(CompanyID c)
Returns the available RoadSubTypes for the provided RoadType If the given company is valid then will ...
Definition: road.cpp:272
IsNormalRoadTile
static bool IsNormalRoadTile(TileIndex t)
Return whether a tile is a normal road tile.
Definition: road_map.h:73
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:123
engine_base.h
OWNER_END
@ OWNER_END
Last + 1 owner.
Definition: company_type.h:28
TRACK_BIT_Y
@ TRACK_BIT_Y
Y-axis track.
Definition: track_type.h:41
MirrorRoadBits
static RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:51
EngineInfo::climates
byte climates
Climates supported by the engine.
Definition: engine_type.h:138
GetRoadTypes
RoadTypes GetRoadTypes(bool introduces)
Get list of road types, regardless of company availability.
Definition: road.cpp:216
GetTileType
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
IsWater
static bool IsWater(TileIndex t)
Is it a plain water tile?
Definition: water_map.h:141
ROADTYPES_NONE
@ ROADTYPES_NONE
No roadtypes.
Definition: road_type.h:37
Company
Definition: company_base.h:110
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:169
OWNER_TOWN
@ OWNER_TOWN
A town owns the tile, or a town is expanding.
Definition: company_type.h:24
roadveh.h
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:124