OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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
40{
41 switch (GetTileType(tile)) {
43 return GetRailType(tile);
44
45 case TileType::Road:
46 /* rail/road crossing */
47 if (IsLevelCrossing(tile)) return GetRailType(tile);
48 break;
49
51 if (HasStationRail(tile)) return GetRailType(tile);
52 break;
53
56 break;
57
58 default:
59 break;
60 }
61 return INVALID_RAILTYPE;
62}
63
70bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
71{
72 return !_railtypes_hidden_mask.Test(railtype) && Company::Get(company)->avail_railtypes.Test(railtype);
73}
74
80bool HasAnyRailTypesAvail(const CompanyID company)
81{
82 RailTypes avail = Company::Get(company)->avail_railtypes;
83 avail.Reset(_railtypes_hidden_mask);
84 return avail.Any();
85}
86
92bool ValParamRailType(const RailType rail)
93{
94 return rail < RAILTYPE_END && HasRailTypeAvail(_current_company, rail);
95}
96
104RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
105{
106 RailTypes rts = current;
107
108 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
109 const RailTypeInfo *rti = GetRailTypeInfo(rt);
110 /* Unused rail type. */
111 if (rti->label == 0) continue;
112
113 /* Not date introduced. */
114 if (!IsInsideMM(rti->introduction_date, 0, CalendarTime::MAX_DATE.base())) continue;
115
116 /* Not yet introduced at this date. */
117 if (rti->introduction_date > date) continue;
118
119 /* Have we introduced all required railtypes? */
121 if (!rts.All(required)) continue;
122
123 rts.Set(rti->introduces_railtypes);
124 }
125
126 /* When we added railtypes we need to run this method again; the added
127 * railtypes might enable more rail types to become introduced. */
128 return rts == current ? rts : AddDateIntroducedRailTypes(rts, date);
129}
130
137RailTypes GetCompanyRailTypes(CompanyID company, bool introduces)
138{
139 RailTypes rts{};
140
141 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
142 const EngineInfo *ei = &e->info;
143
144 if (ei->climates.Test(_settings_game.game_creation.landscape) &&
145 (e->company_avail.Test(company) || TimerGameCalendar::date >= e->intro_date + CalendarTime::DAYS_IN_YEAR)) {
146 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
147
148 if (rvi->railveh_type != RAILVEH_WAGON) {
149 assert(rvi->railtypes.Any());
150 if (introduces) {
152 } else {
153 rts.Set(rvi->railtypes);
154 }
155 }
156 }
157 }
158
159 if (introduces) return AddDateIntroducedRailTypes(rts, TimerGameCalendar::date);
160 return rts;
161}
162
168RailTypes GetRailTypes(bool introduces)
169{
170 RailTypes rts{};
171
172 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
173 const EngineInfo *ei = &e->info;
174 if (!ei->climates.Test(_settings_game.game_creation.landscape)) continue;
175
176 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
177 if (rvi->railveh_type != RAILVEH_WAGON) {
178 assert(rvi->railtypes.Any());
179 if (introduces) {
181 } else {
182 rts.Set(rvi->railtypes);
183 }
184 }
185 }
186
187 if (introduces) return AddDateIntroducedRailTypes(rts, CalendarTime::MAX_DATE);
188 return rts;
189}
190
197RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
198{
199 extern RailTypeInfo _railtypes[RAILTYPE_END];
200 if (label == 0) return INVALID_RAILTYPE;
201
202 auto it = std::ranges::find(_railtypes, label, &RailTypeInfo::label);
203 if (it == std::end(_railtypes) && allow_alternate_labels) {
204 /* Test if any rail type defines the label as an alternate. */
205 it = std::ranges::find_if(_railtypes, [label](const RailTypeInfo &rti) { return rti.alternate_labels.contains(label); });
206 }
207
208 if (it != std::end(_railtypes)) return it->Index();
209
210 /* No matching label was found, so it is invalid */
211 return INVALID_RAILTYPE;
212}
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:115
RailType Index() const
Get the RailType for this RailTypeInfo.
Definition rail.cpp:26
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:244
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:255
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition rail.h:250
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:225
FlatSet< RailTypeLabel > alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:230
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
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:137
bool HasAnyRailTypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition rail.cpp:80
bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition rail.cpp:70
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition rail.cpp:197
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:39
RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
Add the rail types that are to be introduced at the given date.
Definition rail.cpp:104
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition rail.cpp:168
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition rail.cpp:92
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
EnumBitSet< RailType, uint64_t > RailTypes
Allow incrementing of Track variables.
Definition rail_type.h:38
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?
Definition of base types and functions in a cross-platform compatible way.
Information about a vehicle.
LandscapeTypes climates
Climates supported by the engine.
static Company * Get(auto index)
Information about a rail vehicle.
Definition engine_type.h:74
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:78
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
@ TunnelBridge
Tunnel entry/exit and bridge heads.
Definition tile_type.h:58
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Railway
A tile with railway.
Definition tile_type.h:50
@ Road
A tile with road and/or tram tracks.
Definition tile_type.h:51
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.