OpenTTD Source: src/rail.cpp Source File

OpenTTD Source 15.0-RC2

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
10#include "stdafx.h"
11#include "station_map.h"
12#include "tunnelbridge_map.h"
13#include "timer/timer_game_calendar.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 RailType RailTypeInfo::Index() const
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
37 RailType GetTileRailType(Tile tile)
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:
53 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) return GetRailType(tile);
54 break;
55
56 default:
57 break;
58 }
59 return INVALID_RAILTYPE;
60}
61
68 bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
69{
70 return !_railtypes_hidden_mask.Test(railtype) && Company::Get(company)->avail_railtypes.Test(railtype);
71}
72
78 bool HasAnyRailTypesAvail(const CompanyID company)
79{
80 RailTypes avail = Company::Get(company)->avail_railtypes;
81 avail.Reset(_railtypes_hidden_mask);
82 return avail.Any();
83}
84
90 bool ValParamRailType(const RailType rail)
91{
92 return rail < RAILTYPE_END && HasRailTypeAvail(_current_company, rail);
93}
94
102 RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
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? */
118 RailTypes required = rti->introduction_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
135 RailTypes 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
142 if (ei->climates.Test(_settings_game.game_creation.landscape) &&
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) {
149 rts.Set(GetAllIntroducesRailTypes(rvi->railtypes));
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
166 RailTypes GetRailTypes(bool introduces)
167{
168 RailTypes rts{};
169
170 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
171 const EngineInfo *ei = &e->info;
172 if (!ei->climates.Test(_settings_game.game_creation.landscape)) continue;
173
174 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
175 if (rvi->railveh_type != RAILVEH_WAGON) {
176 assert(rvi->railtypes.Any());
177 if (introduces) {
178 rts.Set(GetAllIntroducesRailTypes(rvi->railtypes));
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
195 RailType 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}
BaseBitSet::All
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
Definition base_bitset_type.hpp:151
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
Engine
Definition engine_base.h:38
Engine::IterateType
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
Definition engine_base.h:197
EnumBitSet< RailType, uint64_t >
FlatSet::contains
bool contains(const Tkey &key) const
Test if a key exists in the set.
Definition flatset_type.hpp:57
RailTypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:115
RailTypeInfo::Index
RailType Index() const
Get the RailType for this RailTypeInfo.
Definition rail.cpp:26
RailTypeInfo::introduction_date
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:244
RailTypeInfo::introduces_railtypes
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:255
RailTypeInfo::introduction_required_railtypes
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
RailTypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:225
RailTypeInfo::alternate_labels
FlatSet< RailTypeLabel > alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:230
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
TimerGameCalendar::date
static Date date
Current date in days (day counter).
Definition timer_game_calendar.h:33
TimerGameConst< struct Calendar >::MAX_DATE
static constexpr TimerGame< struct Calendar >::Date MAX_DATE
The date of the last day of the max year.
Definition timer_game_common.h:187
TimerGameConst< struct Calendar >::DAYS_IN_YEAR
static constexpr int DAYS_IN_YEAR
days per year
Definition timer_game_common.h:149
company_base.h
Definition of stuff that is very close to a company, like the company struct itself.
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
company_func.h
Functions related to companies.
engine_base.h
Base class for engines.
RAILVEH_WAGON
@ RAILVEH_WAGON
simple wagon, not motorized
Definition engine_type.h:34
IsInsideMM
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition math_func.hpp:267
GetCompanyRailTypes
RailTypes GetCompanyRailTypes(CompanyID company, bool introduces)
Get the rail types the given company can build.
Definition rail.cpp:135
HasAnyRailTypesAvail
bool HasAnyRailTypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition rail.cpp:78
HasRailTypeAvail
bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition rail.cpp:68
GetRailTypeByLabel
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition rail.cpp:195
GetTileRailType
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:37
AddDateIntroducedRailTypes
RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
Add the rail types that are to be introduced at the given date.
Definition rail.cpp:102
GetRailTypes
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition rail.cpp:166
ValParamRailType
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition rail.cpp:90
GetAllIntroducesRailTypes
RailTypes GetAllIntroducesRailTypes(RailTypes railtypes)
Returns all introduced railtypes for a set of railtypes.
Definition rail.h:336
GetRailTypeInfo
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:300
GetRailType
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
RailType
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
RAILTYPE_BEGIN
@ RAILTYPE_BEGIN
Used for iterations.
Definition rail_type.h:26
RAILTYPE_END
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:31
INVALID_RAILTYPE
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:32
IsLevelCrossing
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:69
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
station_map.h
Maps accessors for stations.
HasStationRail
bool HasStationRail(Tile t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint?
Definition station_map.h:135
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
EngineInfo
Information about a vehicle.
Definition engine_type.h:198
EngineInfo::climates
LandscapeTypes climates
Climates supported by the engine.
Definition engine_type.h:204
GameCreationSettings::landscape
LandscapeType landscape
the landscape we're currently in
Definition settings_type.h:395
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition settings_type.h:634
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
RailVehicleInfo
Information about a rail vehicle.
Definition engine_type.h:74
RailVehicleInfo::railtypes
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:78
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
GetTileType
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
MP_STATION
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
timer_game_calendar.h
Definition of the game-calendar-timer.
track_data.h
Data related to rail tracks.
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition transport_type.h:25
tunnelbridge_map.h
Functions that have tunnels and bridges in common.
GetTunnelBridgeTransportType
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Definition tunnelbridge_map.h:39
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition vehicle_type.h:25