OpenTTD
rail.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 "station_map.h"
14 #include "tunnelbridge_map.h"
15 #include "date_func.h"
16 #include "company_func.h"
17 #include "company_base.h"
18 #include "engine_base.h"
19 
20 #include "safeguards.h"
21 
22 /* XXX: Below 3 tables store duplicate data. Maybe remove some? */
23 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
24  * direction along with the trackdir */
25 extern const byte _signal_along_trackdir[TRACKDIR_END] = {
26  0x8, 0x8, 0x8, 0x2, 0x4, 0x1, 0, 0,
27  0x4, 0x4, 0x4, 0x1, 0x8, 0x2
28 };
29 
30 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
31  * direction against the trackdir */
32 extern const byte _signal_against_trackdir[TRACKDIR_END] = {
33  0x4, 0x4, 0x4, 0x1, 0x8, 0x2, 0, 0,
34  0x8, 0x8, 0x8, 0x2, 0x4, 0x1
35 };
36 
37 /* Maps a Track to the bits that store the status of the two signals that can
38  * be present on the given track */
39 extern const byte _signal_on_track[] = {
40  0xC, 0xC, 0xC, 0x3, 0xC, 0x3
41 };
42 
43 /* Maps a diagonal direction to the all trackdirs that are connected to any
44  * track entering in this direction (including those making 90 degree turns)
45  */
46 extern const TrackdirBits _exitdir_reaches_trackdirs[] = {
51 };
52 
53 extern const Trackdir _next_trackdir[TRACKDIR_END] = {
56 };
57 
58 /* Maps a trackdir to all trackdirs that make 90 deg turns with it. */
59 extern const TrackdirBits _track_crosses_trackdirs[TRACK_END] = {
66 };
67 
68 /* Maps a track to all tracks that make 90 deg turns with it. */
69 extern const TrackBits _track_crosses_tracks[] = {
70  TRACK_BIT_Y, // TRACK_X
71  TRACK_BIT_X, // TRACK_Y
72  TRACK_BIT_VERT, // TRACK_UPPER
73  TRACK_BIT_VERT, // TRACK_LOWER
74  TRACK_BIT_HORZ, // TRACK_LEFT
75  TRACK_BIT_HORZ // TRACK_RIGHT
76 };
77 
78 /* Maps a trackdir to the (4-way) direction the tile is exited when following
79  * that trackdir */
80 extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END] = {
83 };
84 
85 extern const Trackdir _track_exitdir_to_trackdir[][DIAGDIR_END] = {
86  {TRACKDIR_X_NE, INVALID_TRACKDIR, TRACKDIR_X_SW, INVALID_TRACKDIR},
92 };
93 
94 extern const Trackdir _track_enterdir_to_trackdir[][DIAGDIR_END] = {
95  {TRACKDIR_X_NE, INVALID_TRACKDIR, TRACKDIR_X_SW, INVALID_TRACKDIR},
99  {TRACKDIR_LEFT_N, TRACKDIR_LEFT_S, INVALID_TRACKDIR, INVALID_TRACKDIR},
101 };
102 
103 extern const Trackdir _track_direction_to_trackdir[][DIR_END] = {
110 };
111 
112 extern const Trackdir _dir_to_diag_trackdir[] = {
114 };
115 
116 extern const TrackBits _corner_to_trackbits[] = {
118 };
119 
120 extern const TrackdirBits _uphill_trackdirs[] = {
144  TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_SE,
148  TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_NW,
150  TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_NW,
151  TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_SE,
152 };
153 
158 {
159  switch (GetTileType(tile)) {
160  case MP_RAILWAY:
161  return GetRailType(tile);
162 
163  case MP_ROAD:
164  /* rail/road crossing */
165  if (IsLevelCrossing(tile)) return GetRailType(tile);
166  break;
167 
168  case MP_STATION:
169  if (HasStationRail(tile)) return GetRailType(tile);
170  break;
171 
172  case MP_TUNNELBRIDGE:
173  if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) return GetRailType(tile);
174  break;
175 
176  default:
177  break;
178  }
179  return INVALID_RAILTYPE;
180 }
181 
188 bool HasRailtypeAvail(const CompanyID company, const RailType railtype)
189 {
190  return !HasBit(_railtypes_hidden_mask, railtype) && HasBit(Company::Get(company)->avail_railtypes, railtype);
191 }
192 
198 bool HasAnyRailtypesAvail(const CompanyID company)
199 {
200  return (Company::Get(company)->avail_railtypes & ~_railtypes_hidden_mask) != 0;
201 }
202 
208 bool ValParamRailtype(const RailType rail)
209 {
210  return rail < RAILTYPE_END && HasRailtypeAvail(_current_company, rail);
211 }
212 
221 {
222  if (HasRailtypeAvail(company, RAILTYPE_MAGLEV)) return RAILTYPE_MAGLEV;
223  if (HasRailtypeAvail(company, RAILTYPE_MONO)) return RAILTYPE_MONO;
225  return RAILTYPE_RAIL;
226 }
227 
236 {
237  RailTypes rts = current;
238 
239  for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
240  const RailtypeInfo *rti = GetRailTypeInfo(rt);
241  /* Unused rail type. */
242  if (rti->label == 0) continue;
243 
244  /* Not date introduced. */
245  if (!IsInsideMM(rti->introduction_date, 0, MAX_DAY)) continue;
246 
247  /* Not yet introduced at this date. */
248  if (rti->introduction_date > date) continue;
249 
250  /* Have we introduced all required railtypes? */
252  if ((rts & required) != required) continue;
253 
254  rts |= rti->introduces_railtypes;
255  }
256 
257  /* When we added railtypes we need to run this method again; the added
258  * railtypes might enable more rail types to become introduced. */
259  return rts == current ? rts : AddDateIntroducedRailTypes(rts, date);
260 }
261 
268 RailTypes GetCompanyRailtypes(CompanyID company, bool introduces)
269 {
271 
272  const Engine *e;
273  FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
274  const EngineInfo *ei = &e->info;
275 
277  (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
278  const RailVehicleInfo *rvi = &e->u.rail;
279 
280  if (rvi->railveh_type != RAILVEH_WAGON) {
281  assert(rvi->railtype < RAILTYPE_END);
282  if (introduces) {
283  rts |= GetRailTypeInfo(rvi->railtype)->introduces_railtypes;
284  } else {
285  SetBit(rts, rvi->railtype);
286  }
287  }
288  }
289  }
290 
291  if (introduces) return AddDateIntroducedRailTypes(rts, _date);
292  return rts;
293 }
294 
300 RailTypes GetRailTypes(bool introduces)
301 {
303 
304  const Engine *e;
305  FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
306  const EngineInfo *ei = &e->info;
308 
309  const RailVehicleInfo *rvi = &e->u.rail;
310  if (rvi->railveh_type != RAILVEH_WAGON) {
311  assert(rvi->railtype < RAILTYPE_END);
312  if (introduces) {
313  rts |= GetRailTypeInfo(rvi->railtype)->introduces_railtypes;
314  } else {
315  SetBit(rts, rvi->railtype);
316  }
317  }
318  }
319 
320  if (introduces) return AddDateIntroducedRailTypes(rts, MAX_DAY);
321  return rts;
322 }
323 
330 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
331 {
332  /* Loop through each rail type until the label is found */
333  for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
334  const RailtypeInfo *rti = GetRailTypeInfo(r);
335  if (rti->label == label) return r;
336  }
337 
338  if (allow_alternate_labels) {
339  /* Test if any rail type defines the label as an alternate. */
340  for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
341  const RailtypeInfo *rti = GetRailTypeInfo(r);
342  if (std::find(rti->alternate_labels.begin(), rti->alternate_labels.end(), label) != rti->alternate_labels.end()) return r;
343  }
344  }
345 
346  /* No matching label was found, so it is invalid */
347  return INVALID_RAILTYPE;
348 }
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:98
Monorail.
Definition: rail_type.h:33
Owner
Enum for all companies/owners.
Definition: company_type.h:20
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 const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:306
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:31
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:103
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
No track build.
Definition: track_type.h:104
Track upper, direction west.
Definition: track_type.h:114
byte landscape
the landscape we&#39;re currently in
Maps accessors for stations.
Lower track and direction to west.
Definition: track_type.h:85
Train vehicle type.
Definition: vehicle_type.h:26
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Functions related to dates.
Northwest.
X-axis and direction to north-east.
Definition: track_type.h:74
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition: rail.h:260
Left track.
Definition: track_type.h:46
Used for iterations.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:72
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
Flag for invalid railtype.
Definition: rail_type.h:36
bool ValParamRailtype(const RailType rail)
Validate functions for rail building.
Definition: rail.cpp:208
X-axis track.
Definition: track_type.h:42
RailTypeLabelList alternate_labels
Rail type labels this type provides in addition to the main label.
Definition: rail.h:240
Upper track and direction to west.
Definition: track_type.h:84
A railway.
Definition: tile_type.h:44
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:86
Used for iterations.
Definition: track_type.h:29
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:126
No rail types.
Definition: rail_type.h:53
Right track.
Definition: track_type.h:47
RailType GetBestRailtype(const CompanyID company)
Returns the "best" railtype a company can build.
Definition: rail.cpp:220
Used to iterate.
static bool HasStationRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:137
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
Y-axis and direction to north-west.
Definition: track_type.h:83
Electric rails.
Definition: rail_type.h:32
Information about a vehicle.
Definition: engine_type.h:134
Southeast.
Y-axis and direction to south-east.
Definition: track_type.h:75
Used for iterations.
Definition: track_type.h:90
Right track and direction to south.
Definition: track_type.h:79
Track right, direction north.
Definition: track_type.h:117
simple wagon, not motorized
Definition: engine_type.h:31
Standard non-electric rails.
Definition: rail_type.h:31
Left and right track.
Definition: track_type.h:50
Track left, direction south.
Definition: track_type.h:109
Definition of base types and functions in a cross-platform compatible way.
Track y-axis, direction south-east.
Definition: track_type.h:106
A number of safeguards to prevent using unsafe methods.
Flag for an invalid trackdir.
Definition: track_type.h:91
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced. ...
Definition: rail.h:265
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition: rail.cpp:300
bool HasAnyRailtypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition: rail.cpp:198
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:39
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:40
Left track and direction to south.
Definition: track_type.h:78
Track left, direction north.
Definition: track_type.h:116
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:235
RailTypes
The different railtypes we support, but then a bitmask of them.
Definition: rail_type.h:52
Upper track and direction to east.
Definition: track_type.h:76
Information about a rail vehicle.
Definition: engine_type.h:44
Lower track.
Definition: track_type.h:45
Transport by train.
Functions related to companies.
RailType GetTileRailType(TileIndex tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition: rail.cpp:157
Date introduction_date
Introduction date.
Definition: rail.h:254
Base class for engines.
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
Track lower, direction east.
Definition: track_type.h:108
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
Upper track.
Definition: track_type.h:44
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
bool HasRailtypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition: rail.cpp:188
A tile of a station.
Definition: tile_type.h:48
#define MAX_DAY
The number of days till the last day.
Definition: date_type.h:97
Track x-axis, direction south-west.
Definition: track_type.h:112
Track y-axis, direction north-west.
Definition: track_type.h:113
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
X-axis and direction to south-west.
Definition: track_type.h:82
Functions that have tunnels and bridges in common.
int32 Date
The type to store our dates in.
Definition: date_type.h:16
Track lower, direction west.
Definition: track_type.h:115
Used for iterations.
Definition: rail_type.h:35
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
DiagDirection
Enumeration for diagonal directions.
Track upper, direction east.
Definition: track_type.h:107
Northeast, upper right on your monitor.
GameCreationSettings game_creation
settings used during the creation of a game (map)
RailTypes GetCompanyRailtypes(CompanyID company, bool introduces)
Get the rail types the given company can build.
Definition: rail.cpp:268
Track x-axis, direction north-east.
Definition: track_type.h:105
Left track and direction to north.
Definition: track_type.h:86
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition: rail.cpp:330
Right track and direction to north.
Definition: track_type.h:87
byte climates
Climates supported by the engine.
Definition: engine_type.h:140
Date _date
Current date in days (day counter)
Definition: date.cpp:28
Y-axis track.
Definition: track_type.h:43
RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date)
Add the rail types that are to be introduced at the given date.
Definition: rail.cpp:235
Used for iterations.
Definition: rail_type.h:30
Lower track and direction to east.
Definition: track_type.h:77
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:117
Track right, direction south.
Definition: track_type.h:110
Southwest.
Upper and lower track.
Definition: track_type.h:49
Maglev.
Definition: rail_type.h:34