OpenTTD Source 20250924-master-gbec4e71d53
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
26{
27 switch (GetTileType(tile)) {
28 case MP_RAILWAY:
29 return GetRailType(tile);
30
31 case MP_ROAD:
32 /* rail/road crossing */
33 if (IsLevelCrossing(tile)) return GetRailType(tile);
34 break;
35
36 case MP_STATION:
37 if (HasStationRail(tile)) return GetRailType(tile);
38 break;
39
40 case MP_TUNNELBRIDGE:
42 break;
43
44 default:
45 break;
46 }
47 return INVALID_RAILTYPE;
48}
49
56bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
57{
58 return !_railtypes_hidden_mask.Test(railtype) && Company::Get(company)->avail_railtypes.Test(railtype);
59}
60
67{
68 RailTypes avail = Company::Get(company)->avail_railtypes;
69 avail.Reset(_railtypes_hidden_mask);
70 return avail.Any();
71}
72
78bool ValParamRailType(const RailType rail)
79{
80 return rail < RAILTYPE_END && HasRailTypeAvail(_current_company, rail);
81}
82
91{
92 RailTypes rts = current;
93
94 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
95 const RailTypeInfo *rti = GetRailTypeInfo(rt);
96 /* Unused rail type. */
97 if (rti->label == 0) continue;
98
99 /* Not date introduced. */
100 if (!IsInsideMM(rti->introduction_date, 0, CalendarTime::MAX_DATE.base())) continue;
101
102 /* Not yet introduced at this date. */
103 if (rti->introduction_date > date) continue;
104
105 /* Have we introduced all required railtypes? */
107 if (!rts.All(required)) continue;
108
109 rts.Set(rti->introduces_railtypes);
110 }
111
112 /* When we added railtypes we need to run this method again; the added
113 * railtypes might enable more rail types to become introduced. */
114 return rts == current ? rts : AddDateIntroducedRailTypes(rts, date);
115}
116
123RailTypes GetCompanyRailTypes(CompanyID company, bool introduces)
124{
125 RailTypes rts{};
126
127 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
128 const EngineInfo *ei = &e->info;
129
131 (e->company_avail.Test(company) || TimerGameCalendar::date >= e->intro_date + CalendarTime::DAYS_IN_YEAR)) {
132 const RailVehicleInfo *rvi = &e->u.rail;
133
134 if (rvi->railveh_type != RAILVEH_WAGON) {
135 assert(rvi->railtypes.Any());
136 if (introduces) {
138 } else {
139 rts.Set(rvi->railtypes);
140 }
141 }
142 }
143 }
144
145 if (introduces) return AddDateIntroducedRailTypes(rts, TimerGameCalendar::date);
146 return rts;
147}
148
154RailTypes GetRailTypes(bool introduces)
155{
156 RailTypes rts{};
157
158 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
159 const EngineInfo *ei = &e->info;
161
162 const RailVehicleInfo *rvi = &e->u.rail;
163 if (rvi->railveh_type != RAILVEH_WAGON) {
164 assert(rvi->railtypes.Any());
165 if (introduces) {
167 } else {
168 rts.Set(rvi->railtypes);
169 }
170 }
171 }
172
173 if (introduces) return AddDateIntroducedRailTypes(rts, CalendarTime::MAX_DATE);
174 return rts;
175}
176
183RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
184{
185 if (label == 0) return INVALID_RAILTYPE;
186
187 /* Loop through each rail type until the label is found */
188 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
189 const RailTypeInfo *rti = GetRailTypeInfo(r);
190 if (rti->label == label) return r;
191 }
192
193 if (allow_alternate_labels) {
194 /* Test if any rail type defines the label as an alternate. */
195 for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
196 const RailTypeInfo *rti = GetRailTypeInfo(r);
197 if (std::ranges::find(rti->alternate_labels, label) != rti->alternate_labels.end()) return r;
198 }
199 }
200
201 /* No matching label was found, so it is invalid */
202 return INVALID_RAILTYPE;
203}
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.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:115
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:243
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:254
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition rail.h:249
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:224
std::vector< RailTypeLabel > alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:229
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:123
bool HasAnyRailTypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition rail.cpp:66
bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition rail.cpp:56
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition rail.cpp:183
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:25
RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
Add the rail types that are to be introduced at the given date.
Definition rail.cpp:90
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition rail.cpp:154
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition rail.cpp:78
RailTypes GetAllIntroducesRailTypes(RailTypes railtypes)
Returns all introduced railtypes for a set of railtypes.
Definition rail.h:346
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:297
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.
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
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:54
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:58
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.