OpenTTD: src/road.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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) &&
35 GetRailTileType(tile) == RAIL_TILE_NORMAL &&
36 GetTrackBits(tile) == (ax == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) &&
37 GetFoundationSlope(tile) == SLOPE_FLAT);
38 }
39
46 RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
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
129 bool ValParamRoadType(const RoadType rt)
130 {
131 return HasRoadTypesAvail(_current_company, RoadTypeToRoadTypes(rt));
132 }
133
139 RoadTypes GetCompanyRoadtypes(CompanyID company)
140 {
141 RoadTypes rt = ROADTYPES_NONE;
142
143 Engine *e;
144 FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
145 const EngineInfo *ei = &e->info;
146
147 if (HasBit(ei->climates, _settings_game.game_creation.landscape) &&
148 (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
149 SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
150 }
151 }
152
153 return rt;
154 }