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 
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  switch (GetTileType(neighbor_tile)) {
61  /* Always connective ones */
62  case MP_CLEAR: case MP_TREES:
63  connective = true;
64  break;
65 
66  /* The conditionally connective ones */
67  case MP_TUNNELBRIDGE:
68  case MP_STATION:
69  case MP_ROAD: {
70  const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, ROADTYPE_ROAD) | GetAnyRoadBits(neighbor_tile, ROADTYPE_TRAM);
71 
72  /* Accept only connective tiles */
73  connective = (neighbor_rb & mirrored_rb) || // Neighbor has got the fitting RoadBit
74  HasExactlyOneBit(neighbor_rb); // Neighbor has got only one Roadbit
75 
76  break;
77  }
78 
79  case MP_RAILWAY:
80  connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
81  break;
82 
83  case MP_WATER:
84  /* Check for real water tile */
85  connective = !IsWater(neighbor_tile);
86  break;
87 
88  /* The definitely not connective ones */
89  default: break;
90  }
91 
92  /* If the neighbor tile is inconnective, remove the planed road connection to it */
93  if (!connective) org_rb ^= target_rb;
94 
95  }
96  }
97 
98  return org_rb;
99 }
100 
107 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
108 {
109  RoadTypes avail_roadtypes;
110 
111  if (company == OWNER_DEITY || company == OWNER_TOWN || _game_mode == GM_EDITOR || _generating_world) {
112  avail_roadtypes = ROADTYPES_ROAD;
113  } else {
114  Company *c = Company::GetIfValid(company);
115  if (c == NULL) return false;
116  avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody
117  }
118  return (rts & ~avail_roadtypes) == 0;
119 }
120 
127 {
129 }
130 
137 {
139 
140  Engine *e;
141  FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
142  const EngineInfo *ei = &e->info;
143 
145  (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
147  }
148  }
149 
150  return rt;
151 }