OpenTTD
road.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 #include "stdafx.h"
13 #include "rail_map.h"
14 #include "road_map.h"
15 #include "water_map.h"
16 #include "genworld.h"
17 #include "company_func.h"
18 #include "company_base.h"
19 #include "engine_base.h"
20 #include "date_func.h"
21 #include "landscape.h"
22 #include "road.h"
23 #include "road_func.h"
24 #include "roadveh.h"
25 
26 #include "safeguards.h"
27 
35 static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
36 {
37  return (IsTileType(tile, MP_RAILWAY) &&
39  GetTrackBits(tile) == (ax == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) &&
41 }
42 
50 {
51  if (!IsValidTile(tile)) return ROAD_NONE;
52  for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
53  const TileIndex neighbor_tile = TileAddByDiagDir(tile, dir);
54 
55  /* Get the Roadbit pointing to the neighbor_tile */
56  const RoadBits target_rb = DiagDirToRoadBits(dir);
57 
58  /* If the roadbit is in the current plan */
59  if (org_rb & target_rb) {
60  bool connective = false;
61  const RoadBits mirrored_rb = MirrorRoadBits(target_rb);
62 
63  if (IsValidTile(neighbor_tile)) {
64  switch (GetTileType(neighbor_tile)) {
65  /* Always connective ones */
66  case MP_CLEAR: case MP_TREES:
67  connective = true;
68  break;
69 
70  /* The conditionally connective ones */
71  case MP_TUNNELBRIDGE:
72  case MP_STATION:
73  case MP_ROAD:
74  if (IsNormalRoadTile(neighbor_tile)) {
75  /* Always connective */
76  connective = true;
77  } else {
78  const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, RTT_ROAD) | GetAnyRoadBits(neighbor_tile, RTT_TRAM);
79 
80  /* Accept only connective tiles */
81  connective = (neighbor_rb & mirrored_rb) != ROAD_NONE;
82  }
83  break;
84 
85  case MP_RAILWAY:
86  connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
87  break;
88 
89  case MP_WATER:
90  /* Check for real water tile */
91  connective = !IsWater(neighbor_tile);
92  break;
93 
94  /* The definitely not connective ones */
95  default: break;
96  }
97  }
98 
99  /* If the neighbor tile is inconnective, remove the planned road connection to it */
100  if (!connective) org_rb ^= target_rb;
101  }
102  }
103 
104  return org_rb;
105 }
106 
113 bool HasRoadTypeAvail(const CompanyID company, RoadType roadtype)
114 {
115  if (company == OWNER_DEITY || company == OWNER_TOWN || _game_mode == GM_EDITOR || _generating_world) {
116  return true; // TODO: should there be a proper check?
117  } else {
118  const Company *c = Company::GetIfValid(company);
119  if (c == nullptr) return false;
120  return HasBit(c->avail_roadtypes & ~_roadtypes_hidden_mask, roadtype);
121  }
122 }
123 
124 static RoadTypes GetMaskForRoadTramType(RoadTramType rtt)
125 {
126  return rtt == RTT_TRAM ? _roadtypes_type : ~_roadtypes_type;
127 }
128 
134 bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
135 {
136  return (Company::Get(company)->avail_roadtypes & ~_roadtypes_hidden_mask & GetMaskForRoadTramType(rtt)) != ROADTYPES_NONE;
137 }
138 
145 {
146  return roadtype != INVALID_ROADTYPE && HasRoadTypeAvail(_current_company, roadtype);
147 }
148 
158 {
159  RoadTypes rts = current;
160 
161  for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
162  const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
163  /* Unused road type. */
164  if (rti->label == 0) continue;
165 
166  /* Not date introduced. */
167  if (!IsInsideMM(rti->introduction_date, 0, MAX_DAY)) continue;
168 
169  /* Not yet introduced at this date. */
170  if (rti->introduction_date > date) continue;
171 
172  /* Have we introduced all required roadtypes? */
174  if ((rts & required) != required) continue;
175 
176  rts |= rti->introduces_roadtypes;
177  }
178 
179  /* When we added roadtypes we need to run this method again; the added
180  * roadtypes might enable more rail types to become introduced. */
181  return rts == current ? rts : AddDateIntroducedRoadTypes(rts, date);
182 }
183 
190 RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
191 {
193 
194  const Engine *e;
195  FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
196  const EngineInfo *ei = &e->info;
197 
199  (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
200  const RoadVehicleInfo *rvi = &e->u.road;
201  assert(rvi->roadtype < ROADTYPE_END);
202  if (introduces) {
204  } else {
205  SetBit(rts, rvi->roadtype);
206  }
207  }
208  }
209 
210  if (introduces) return AddDateIntroducedRoadTypes(rts, _date);
211  return rts;
212 }
213 
219 RoadTypes GetRoadTypes(bool introduces)
220 {
222 
223  const Engine *e;
224  FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
225  const EngineInfo *ei = &e->info;
227 
228  const RoadVehicleInfo *rvi = &e->u.road;
229  assert(rvi->roadtype < ROADTYPE_END);
230  if (introduces) {
232  } else {
233  SetBit(rts, rvi->roadtype);
234  }
235  }
236 
237  if (introduces) return AddDateIntroducedRoadTypes(rts, MAX_DAY);
238  return rts;
239 }
240 
247 RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
248 {
249  /* Loop through each road type until the label is found */
250  for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
251  const RoadTypeInfo *rti = GetRoadTypeInfo(r);
252  if (rti->label == label) return r;
253  }
254 
255  if (allow_alternate_labels) {
256  /* Test if any road type defines the label as an alternate. */
257  for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
258  const RoadTypeInfo *rti = GetRoadTypeInfo(r);
259  if (std::find(rti->alternate_labels.begin(), rti->alternate_labels.end(), label) != rti->alternate_labels.end()) return r;
260  }
261  }
262 
263  /* No matching label was found, so it is invalid */
264  return INVALID_ROADTYPE;
265 }
266 
277 {
278  /* Check only players which can actually own vehicles, editor and gamescripts are considered deities */
279  if (c < OWNER_END) {
280  const Company *company = Company::GetIfValid(c);
281  if (company != nullptr) return company->avail_roadtypes;
282  }
283 
284  RoadTypes known_roadtypes = ROADTYPES_NONE;
285 
286  /* Find used roadtypes */
287  Engine *e;
288  FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
289  /* Check if the roadtype can be used in the current climate */
290  if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
291 
292  /* Check whether available for all potential companies */
293  if (e->company_avail != (CompanyMask)-1) continue;
294 
295  known_roadtypes |= GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes;
296  }
297 
298  /* Get the date introduced roadtypes as well. */
299  known_roadtypes = AddDateIntroducedRoadTypes(known_roadtypes, MAX_DAY);
300 
301  return known_roadtypes;
302 }
303 
313 {
314  if (_game_mode != GM_EDITOR && !Company::IsValidID(company)) return false;
316  if (!HasAnyRoadTypesAvail(company, GetRoadTramType(roadtype))) return false;
317 
318  RoadTypes roadtypes = ExistingRoadTypes(company);
319 
320  /* Check if the filtered roadtypes does have the roadtype we are checking for
321  * and if we can build new ones */
322  if (_settings_game.vehicle.max_roadveh > 0 && HasBit(roadtypes, roadtype)) {
323  /* Can we actually build the vehicle type? */
324  const Engine *e;
325  FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
326  if (!HasBit(e->company_avail, company)) continue;
327  if (HasPowerOnRoad(e->u.road.roadtype, roadtype) || HasPowerOnRoad(roadtype, e->u.road.roadtype)) return true;
328  }
329  return false;
330  }
331 
332  /* We should be able to build infrastructure when we have the actual vehicle type */
333  const Vehicle *v;
334  FOR_ALL_VEHICLES(v) {
335  if (v->type == VEH_ROAD && (company == OWNER_DEITY || v->owner == company) &&
336  HasBit(roadtypes, RoadVehicle::From(v)->roadtype) && HasPowerOnRoad(RoadVehicle::From(v)->roadtype, roadtype)) return true;
337  }
338 
339  return false;
340 }
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:98
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Road vehicle states.
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
Get the road type for a given label.
Definition: road.cpp:247
VehicleSettings vehicle
options for vehicles
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:81
Definition of stuff that is very close to a company, like the company struct itself.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:31
byte landscape
the landscape we&#39;re currently in
bool CanBuildRoadTypeInfrastructure(RoadType roadtype, CompanyID company)
Check whether we can build infrastructure for the given RoadType.
Definition: road.cpp:312
RoadTypes _roadtypes_type
Bitmap of road/tram types.
Definition: road_cmd.cpp:59
Functions related to roads.
RoadBits GetAnyRoadBits(TileIndex tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition: road_map.cpp:35
static bool IsWater(TileIndex t)
Is it a plain water tile?
Definition: water_map.h:143
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Functions related to dates.
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:41
Map accessors for roads.
Used for iterations.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
A tile with road (or tram tracks)
Definition: tile_type.h:45
bool HasRoadTypeAvail(const CompanyID company, RoadType roadtype)
Finds out, whether given company has a given RoadType available for construction. ...
Definition: road.cpp:113
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:25
A town owns the tile, or a town is expanding.
Definition: company_type.h:26
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
Get the road types the given company can build.
Definition: road.cpp:190
Used for iterations.
Definition: road_type.h:31
Road specific functions.
X-axis track.
Definition: track_type.h:42
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:384
Vehicle data structure.
Definition: vehicle_base.h:212
flag for invalid roadtype
Definition: road_type.h:32
A railway.
Definition: tile_type.h:44
Functions related to world/map generation.
RoadType
The different roadtypes we support.
Definition: road_type.h:27
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
a flat tile
Definition: slope_type.h:51
RoadType roadtype
Road type.
Definition: engine_type.h:127
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:163
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:266
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:226
static RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:98
bool ValParamRoadType(RoadType roadtype)
Validate functions for rail building.
Definition: road.cpp:144
Information about a vehicle.
Definition: engine_type.h:134
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, Date date)
Add the road types that are to be introduced at the given date.
Definition: road.cpp:157
UnitID max_roadveh
max trucks in game per company
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
Map accessors for water tiles.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Water tile.
Definition: tile_type.h:49
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:55
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:39
No road-part is build.
Definition: road_type.h:56
Last + 1 owner.
Definition: company_type.h:30
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
Clean up unnecessary RoadBits of a planned tile.
Definition: road.cpp:49
bool HasAnyRoadTypesAvail(CompanyID company, RoadTramType rtt)
Test if any buildable RoadType is available for a company.
Definition: road.cpp:134
static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
Return if the tile is a valid tile for a crossing.
Definition: road.cpp:35
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:38
RoadTypes ExistingRoadTypes(CompanyID c)
Returns the available RoadSubTypes for the provided RoadType If the given company is valid then will ...
Definition: road.cpp:276
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:138
The X axis.
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
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, the function returns the same as GetTileSlope.
Definition: landscape.cpp:424
Functions related to companies.
static bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
Checks if an engine of the given RoadType got power on a tile with a given RoadType.
Definition: road.h:241
Tile got trees.
Definition: tile_type.h:47
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:62
Used for iterations.
Definition: road_type.h:28
Base class for engines.
static bool IsNormalRoadTile(TileIndex t)
Return whether a tile is a normal road tile.
Definition: road_map.h:75
Information about a road vehicle.
Definition: engine_type.h:113
GUISettings gui
settings related to the GUI
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
static RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:53
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Used for iterations.
Date introduction_date
Introduction date.
Definition: road.h:165
RoadTypes avail_roadtypes
Road types available to this company.
Definition: company_base.h:123
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:54
A tile of a station.
Definition: tile_type.h:48
Normal rail tile without signals.
Definition: rail_map.h:26
#define MAX_DAY
The number of days till the last day.
Definition: date_type.h:97
Functions related to OTTD&#39;s landscape.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:235
int32 Date
The type to store our dates in.
Definition: date_type.h:16
bool disable_unsuitable_building
disable infrastructure building when no suitable vehicles are available
RoadTypes introduction_required_roadtypes
Bitmask of roadtypes that are required for this roadtype to be introduced at a given introduction_dat...
Definition: road.h:171
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
DiagDirection
Enumeration for diagonal directions.
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:987
GameCreationSettings game_creation
settings used during the creation of a game (map)
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:43
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced. ...
Definition: road.h:176
RoadTypeLabelList alternate_labels
Road type labels this type provides in addition to the main label.
Definition: road.h:151
RoadTypes GetRoadTypes(bool introduces)
Get list of road types, regardless of company availability.
Definition: road.cpp:219
byte climates
Climates supported by the engine.
Definition: engine_type.h:140
Date _date
Current date in days (day counter)
Definition: date.cpp:28
The object is owned by a superuser / goal script.
Definition: company_type.h:29
Y-axis track.
Definition: track_type.h:43
Hides the direct accesses to the map array with map accessors.
Axis
Allow incrementing of DiagDirDiff variables.
Road vehicle type.
Definition: vehicle_type.h:27
No roadtypes.
Definition: road_type.h:42
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition: road.h:146