OpenTTD
road.cpp
Go to the documentation of this file.
1 /* $Id: road.cpp 27424 2015-10-30 17:19:01Z frosch $ */
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 
23 #include "safeguards.h"
24 
32 static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
33 {
34  return (IsTileType(tile, MP_RAILWAY) &&
36  GetTrackBits(tile) == (ax == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) &&
38 }
39 
47 {
48  if (!IsValidTile(tile)) return ROAD_NONE;
49  for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
50  const TileIndex neighbor_tile = TileAddByDiagDir(tile, dir);
51 
52  /* Get the Roadbit pointing to the neighbor_tile */
53  const RoadBits target_rb = DiagDirToRoadBits(dir);
54 
55  /* If the roadbit is in the current plan */
56  if (org_rb & target_rb) {
57  bool connective = false;
58  const RoadBits mirrored_rb = MirrorRoadBits(target_rb);
59 
60  if (IsValidTile(neighbor_tile)) {
61  switch (GetTileType(neighbor_tile)) {
62  /* Always connective ones */
63  case MP_CLEAR: case MP_TREES:
64  connective = true;
65  break;
66 
67  /* The conditionally connective ones */
68  case MP_TUNNELBRIDGE:
69  case MP_STATION:
70  case MP_ROAD:
71  if (IsNormalRoadTile(neighbor_tile)) {
72  /* Always connective */
73  connective = true;
74  } else {
75  const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, ROADTYPE_ROAD) | GetAnyRoadBits(neighbor_tile, ROADTYPE_TRAM);
76 
77  /* Accept only connective tiles */
78  connective = (neighbor_rb & mirrored_rb) != ROAD_NONE;
79  }
80  break;
81 
82  case MP_RAILWAY:
83  connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
84  break;
85 
86  case MP_WATER:
87  /* Check for real water tile */
88  connective = !IsWater(neighbor_tile);
89  break;
90 
91  /* The definitely not connective ones */
92  default: break;
93  }
94  }
95 
96  /* If the neighbor tile is inconnective, remove the planed road connection to it */
97  if (!connective) org_rb ^= target_rb;
98  }
99  }
100 
101  return org_rb;
102 }
103 
110 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
111 {
112  RoadTypes avail_roadtypes;
113 
114  if (company == OWNER_DEITY || company == OWNER_TOWN || _game_mode == GM_EDITOR || _generating_world) {
115  avail_roadtypes = ROADTYPES_ROAD;
116  } else {
117  Company *c = Company::GetIfValid(company);
118  if (c == NULL) return false;
119  avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody
120  }
121  return (rts & ~avail_roadtypes) == 0;
122 }
123 
130 {
132 }
133 
140 {
142 
143  Engine *e;
144  FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
145  const EngineInfo *ei = &e->info;
146 
148  (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
150  }
151  }
152 
153  return rt;
154 }
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:89
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
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 ValParamRoadType(const RoadType rt)
Validate functions for rail building.
Definition: road.cpp:129
static bool IsWater(TileIndex t)
Is it a plain water tile?
Definition: water_map.h:141
Functions related to dates.
Basic road type.
Definition: road_type.h:24
Map accessors for roads.
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
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:25
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:372
static RoadTypes RoadTypeToRoadTypes(RoadType rt)
Maps a RoadType to the corresponding RoadTypes value.
Definition: road_func.h:56
A railway.
Definition: tile_type.h:44
Functions related to world/map generation.
RoadTypes GetCompanyRoadtypes(CompanyID company)
Get the road types the given company can build.
Definition: road.cpp:139
Used for iterations.
RoadType
The different roadtypes we support.
Definition: road_type.h:22
a flat tile
Definition: slope_type.h:51
The object is owned by a superuser / goal script.
Definition: company_type.h:29
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:154
static RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:144
Information about a vehicle.
Definition: engine_type.h:132
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a give tiletype.
Definition: tile_map.h:143
X-axis track.
Definition: track_type.h:43
bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
Finds out, whether given company has all given RoadTypes available.
Definition: road.cpp:110
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.
Trams.
Definition: road_type.h:25
Water tile.
Definition: tile_type.h:49
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:55
Used for iterations.
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
byte misc_flags
Miscellaneous flags.
Definition: engine_type.h:142
DiagDirection
Enumeration for diagonal directions.
Road vehicle type.
Definition: vehicle_type.h:25
RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt, bool straight_tunnel_bridge_entrance)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition: road_map.cpp:35
RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
Clean up unnecessary RoadBits of a planed tile.
Definition: road.cpp:46
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
Return if the tile is a valid tile for a crossing.
Definition: road.cpp:32
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:37
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:137
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:342
Functions related to companies.
Tile got trees.
Definition: tile_type.h:47
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:61
Base class for engines.
static bool IsNormalRoadTile(TileIndex t)
Return whether a tile is a normal road tile.
Definition: road_map.h:57
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
static RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:99
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
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
RoadTypes avail_roadtypes
Road types available to this company.
Definition: company_base.h:115
A tile of a station.
Definition: tile_type.h:48
Normal rail tile without signals.
Definition: rail_map.h:25
Functions related to OTTD&#39;s landscape.
No roadtypes.
Definition: road_type.h:37
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
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
Road vehicle is a tram/light rail vehicle.
Definition: engine_type.h:154
Owner
Enum for all companies/owners.
Definition: company_type.h:20
byte climates
Climates supported by the engine.
Definition: engine_type.h:138
Date _date
Current date in days (day counter)
Definition: date.cpp:28
Hides the direct accesses to the map array with map accessors.
Axis
Allow incrementing of DiagDirDiff variables.
Y-axis track.
Definition: track_type.h:44