OpenTTD Source 20251019-master-g9f7f314f81
rail.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#include "stdafx.h"
11#include "station_map.h"
12#include "tunnelbridge_map.h"
14#include "company_func.h"
15#include "company_base.h"
16#include "engine_base.h"
17
18#include "table/track_data.h"
19
20#include "safeguards.h"
21
27{
28 extern RailTypeInfo _railtypes[RAILTYPE_END];
29 size_t index = this - _railtypes;
30 assert(index < RAILTYPE_END);
31 return static_cast<RailType>(index);
32}
33
38{
39 switch (GetTileType(tile)) {
40 case MP_RAILWAY:
41 return GetRailType(tile);
42
43 case MP_ROAD:
44 /* rail/road crossing */
45 if (IsLevelCrossing(tile)) return GetRailType(tile);
46 break;
47
48 case MP_STATION:
49 if (HasStationRail(tile)) return GetRailType(tile);
50 break;
51
52 case MP_TUNNELBRIDGE:
54 break;
55
56 default:
57 break;
58 }
59 return INVALID_RAILTYPE;
60}
61
68bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
69{
70 return !_railtypes_hidden_mask.Test(railtype) && Company::Get(company)->avail_railtypes.Test(railtype);
71}
72
79{
80 RailTypes avail = Company::Get(company)->avail_railtypes;
81 avail.Reset(_railtypes_hidden_mask);
82 return avail.Any();
83}
84
90bool ValParamRailType(const RailType rail)
91{
92 return rail < RAILTYPE_END && HasRailTypeAvail(_current_company, rail);
93}
94
103{
104 RailTypes rts = current;
105
106 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
107 const RailTypeInfo *rti = GetRailTypeInfo(rt);
108 /* Unused rail type. */
109 if (rti->label == 0) continue;
110
111 /* Not date introduced. */
112 if (!IsInsideMM(rti->introduction_date, 0, CalendarTime::MAX_DATE.base())) continue;
113
114 /* Not yet introduced at this date. */
115 if (rti->introduction_date > date) continue;
116
117 /* Have we introduced all required railtypes? */
119 if (!rts.All(required)) continue;
120
121 rts.Set(rti->introduces_railtypes);
122 }
123
124 /* When we added railtypes we need to run this method again; the added
125 * railtypes might enable more rail types to become introduced. */
126 return rts == current ? rts : AddDateIntroducedRailTypes(rts, date);
127}
128
135RailTypes GetCompanyRailTypes(CompanyID company, bool introduces)
136{
137 RailTypes rts{};
138
139 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
140 const EngineInfo *ei = &e->info;
141
143 (e->company_avail.Test(company) || TimerGameCalendar::date >= e->intro_date + CalendarTime::DAYS_IN_YEAR)) {
144 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
145
146 if (rvi->railveh_type != RAILVEH_WAGON) {
147 assert(rvi->railtypes.Any());
148 if (introduces) {
150 } else {
151 rts.Set(rvi->railtypes);
152 }
153 }
154 }
155 }
156
157 if (introduces) return AddDateIntroducedRailTypes(rts, TimerGameCalendar::date);
158 return rts;
159}
160
166RailTypes GetRailTypes(bool introduces)
167{
168 RailTypes rts{};
169
170 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
171 const EngineInfo *ei = &e->info;
173
174 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
175 if (rvi->railveh_type != RAILVEH_WAGON) {
176 assert(rvi->railtypes.Any());
177 if (introduces) {
179 } else {
180 rts.Set(rvi->railtypes);
181 }
182 }
183 }
184
185 if (introduces) return AddDateIntroducedRailTypes(rts, CalendarTime::MAX_DATE);
186 return rts;
187}
188
195RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
196{
197 extern RailTypeInfo _railtypes[RAILTYPE_END];
198 if (label == 0) return INVALID_RAILTYPE;
199
200 auto it = std::ranges::find(_railtypes, label, &RailTypeInfo::label);
201 if (it == std::end(_railtypes) && allow_alternate_labels) {
202 /* Test if any rail type defines the label as an alternate. */
203 it = std::ranges::find_if(_railtypes, [label](const RailTypeInfo &rti) { return rti.alternate_labels.contains(label); });
204 }
205
206 if (it != std::end(_railtypes)) return it->Index();
207
208 /* No matching label was found, so it is invalid */
209 return INVALID_RAILTYPE;
210}
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
bool contains(const Tkey &key) const
Test if a key exists in the set.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:116
RailType Index() const
Get the RailType for this RailTypeInfo.
Definition rail.cpp:26
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:245
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:256
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition rail.h:251
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:226
FlatSet< RailTypeLabel > alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:231
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
static Date date
Current date in days (day counter).
static constexpr TimerGame< struct Calendar >::Date MAX_DATE
The date of the last day of the max year.
static constexpr int DAYS_IN_YEAR
days per year
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
Base class for engines.
@ RAILVEH_WAGON
simple wagon, not motorized
Definition engine_type.h:34
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
RailTypes GetCompanyRailTypes(CompanyID company, bool introduces)
Get the rail types the given company can build.
Definition rail.cpp:135
bool HasAnyRailTypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition rail.cpp:78
bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition rail.cpp:68
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition rail.cpp:195
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:37
RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
Add the rail types that are to be introduced at the given date.
Definition rail.cpp:102
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition rail.cpp:166
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition rail.cpp:90
RailTypes GetAllIntroducesRailTypes(RailTypes railtypes)
Returns all introduced railtypes for a set of railtypes.
Definition rail.h:337
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:301
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
@ RAILTYPE_BEGIN
Used for iterations.
Definition rail_type.h:26
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:31
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:32
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Maps accessors for stations.
bool HasStationRail(Tile t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint?
Definition of base types and functions in a cross-platform compatible way.
Information about a vehicle.
LandscapeTypes climates
Climates supported by the engine.
LandscapeType landscape
the landscape we're currently in
GameCreationSettings game_creation
settings used during the creation of a game (map)
static Titem * Get(auto index)
Returns Titem with given index.
Information about a rail vehicle.
Definition engine_type.h:74
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:78
Templated helper to make a type-safe 'typedef' representing a single POD value.
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
Definition of the game-calendar-timer.
Data related to rail tracks.
@ TRANSPORT_RAIL
Transport by train.
Functions that have tunnels and bridges in common.
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
@ VEH_TRAIN
Train vehicle type.