OpenTTD Source 20251005-master-ga617d009cc
rail.h
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#ifndef RAIL_H
11#define RAIL_H
12
13#include "rail_type.h"
14#include "track_type.h"
15#include "gfx_type.h"
16#include "core/bitmath_func.hpp"
17#include "core/enum_type.hpp"
18#include "core/flatset_type.hpp"
19#include "economy_func.h"
20#include "slope_type.h"
21#include "strings_type.h"
23#include "signal_type.h"
24#include "settings_type.h"
25#include "newgrf_badge_type.h"
26
28enum class RailTypeFlag : uint8_t {
29 Catenary = 0,
30 NoLevelCrossing = 1,
31 Hidden = 2,
32 NoSpriteCombine = 3,
33 Allow90Deg = 4,
34 Disallow90Deg = 5,
35};
37
38struct SpriteGroup;
39
57
80
89
112
117public:
122 struct {
137
142 struct {
151 SpriteID signals[SIGTYPE_END][2][2];
153
154 struct {
164
165 struct {
173
176
179
182
187
192
196 uint8_t curve_speed;
197
202
207
212
217
221 uint16_t max_speed;
222
226 RailTypeLabel label;
227
232
237
246
252
257
262
266 const GRFFile *grffile[RTSG_END];
267
271 const SpriteGroup *group[RTSG_END];
272
273 std::vector<BadgeID> badges;
274
275 inline bool UsesOverlay() const
276 {
277 return this->group[RTSG_GROUND] != nullptr;
278 }
279
287 inline uint GetRailtypeSpriteOffset() const
288 {
289 return 82 * this->fallback_railtype;
290 }
291
292 RailType Index() const;
293};
294
295
301inline const RailTypeInfo *GetRailTypeInfo(RailType railtype)
302{
303 extern RailTypeInfo _railtypes[RAILTYPE_END];
304 assert(railtype < RAILTYPE_END);
305 return &_railtypes[railtype];
306}
307
314{
315 RailTypes compatible{};
316 for (RailType rt : railtypes) compatible.Set(GetRailTypeInfo(rt)->compatible_railtypes);
317 return compatible;
318}
319
326{
327 RailTypes powered{};
328 for (RailType rt : railtypes) powered.Set(GetRailTypeInfo(rt)->powered_railtypes);
329 return powered;
330}
331
338{
339 RailTypes introduces{};
340 for (RailType rt : railtypes) introduces.Set(GetRailTypeInfo(rt)->introduces_railtypes);
341 return introduces;
342}
343
352inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
353{
354 return GetRailTypeInfo(enginetype)->compatible_railtypes.Test(tiletype);
355}
356
364inline bool IsCompatibleRail(RailTypes enginetype, RailType tiletype)
365{
366 return GetAllCompatibleRailTypes(enginetype).Test(tiletype);
367}
368
377inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
378{
379 return GetRailTypeInfo(enginetype)->powered_railtypes.Test(tiletype);
380}
381
389inline bool HasPowerOnRail(RailTypes enginetype, RailType tiletype)
390{
391 return GetAllPoweredRailTypes(enginetype).Test(tiletype);
392}
393
403
412{
413 if (rt1 == INVALID_RAILTYPE || rt2 == INVALID_RAILTYPE) return def;
414
415 const RailTypeInfo *rti1 = GetRailTypeInfo(rt1);
416 const RailTypeInfo *rti2 = GetRailTypeInfo(rt2);
417
418 bool rt1_90deg = rti1->flags.Test(RailTypeFlag::Disallow90Deg) || (!rti1->flags.Test(RailTypeFlag::Allow90Deg) && def);
419 bool rt2_90deg = rti2->flags.Test(RailTypeFlag::Disallow90Deg) || (!rti2->flags.Test(RailTypeFlag::Allow90Deg) && def);
420
421 return rt1_90deg || rt2_90deg;
422}
423
430{
431 assert(railtype < RAILTYPE_END);
432 return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
433}
434
441{
442 /* Clearing rail in fact earns money, but if the build cost is set
443 * very low then a loophole exists where money can be made.
444 * In this case we limit the removal earnings to 3/4s of the build
445 * cost.
446 */
447 assert(railtype < RAILTYPE_END);
448 return std::max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
449}
450
458{
459 /* Get the costs for removing and building anew
460 * A conversion can never be more costly */
461 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
462
463 /* Conversion between somewhat compatible railtypes:
464 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
465 * build costs, if the target type is more expensive (material upgrade costs).
466 * Upgrade can never be more expensive than re-building. */
467 if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
468 Money upgradecost = RailBuildCost(to) / 8 + std::max((Money)0, RailBuildCost(to) - RailBuildCost(from));
469 return std::min(upgradecost, rebuildcost);
470 }
471
472 /* make the price the same as remove + build new type for rail types
473 * which are not compatible in any way */
474 return rebuildcost;
475}
476
484inline Money RailMaintenanceCost(RailType railtype, uint32_t num, uint32_t total_num)
485{
486 assert(railtype < RAILTYPE_END);
487 return (_price[PR_INFRASTRUCTURE_RAIL] * GetRailTypeInfo(railtype)->maintenance_multiplier * num * (1 + IntSqrt(total_num))) >> 11; // 4 bits fraction for the multiplier and 7 bits scaling.
488}
489
495inline Money SignalMaintenanceCost(uint32_t num)
496{
497 return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
498}
499
500void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
501int TicksToLeaveDepot(const Train *v);
502
504
505
506bool HasRailTypeAvail(const CompanyID company, const RailType railtype);
507bool HasAnyRailTypesAvail(const CompanyID company);
508bool ValParamRailType(const RailType rail);
509
510RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date);
511
512RailTypes GetCompanyRailTypes(CompanyID company, bool introduces = true);
513RailTypes GetRailTypes(bool introduces);
514
515RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
516
517void ResetRailTypes();
518void InitRailTypes();
519RailType AllocateRailType(RailTypeLabel label);
520
521extern std::vector<RailType> _sorted_railtypes;
522extern RailTypes _railtypes_hidden_mask;
523
524#endif /* RAIL_H */
Functions related to bit mathematics.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
Flat set implementation that uses a sorted vector for storage.
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
SpriteID single_x
single piece of rail in X direction, without ground
Definition rail.h:126
uint16_t max_speed
Maximum speed for vehicles travelling on this rail type.
Definition rail.h:221
SpriteID build_tunnel
button for building a tunnel
Definition rail.h:149
CursorID rail_swne
Cursor for building rail in X direction.
Definition rail.h:156
SpriteID convert_rail
button for converting rail
Definition rail.h:150
CursorID convert
Cursor for converting track.
Definition rail.h:162
CursorID depot
Cursor for building a depot.
Definition rail.h:160
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:245
PixelColour map_colour
Colour on mini-map.
Definition rail.h:236
RailTypes powered_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype generates power
Definition rail.h:178
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:256
SpriteID ground
ground sprite for a 3-way switch
Definition rail.h:125
CursorID rail_nwse
Cursor for building rail in Y direction.
Definition rail.h:158
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
SpriteID build_x_rail
button for building single rail in X direction
Definition rail.h:144
uint8_t sorting_order
The sorting order of this railtype for the toolbar dropdown.
Definition rail.h:261
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:226
SpriteID single_n
single piece of rail in the northern corner
Definition rail.h:128
VehicleAccelerationModel acceleration_type
Acceleration type of this rail type.
Definition rail.h:216
CursorID tunnel
Cursor for building a tunnel.
Definition rail.h:161
uint16_t maintenance_multiplier
Cost multiplier for maintenance of this rail type.
Definition rail.h:211
SpriteID crossing
level crossing, rail in X direction
Definition rail.h:133
const SpriteGroup * group[RTSG_END]
Sprite groups for resolving sprites.
Definition rail.h:271
SpriteID bridge_deck
bridge deck sprites base
Definition rail.h:135
CursorID rail_ew
Cursor for building rail in E-W direction.
Definition rail.h:157
SpriteID auto_rail
button for the autorail construction
Definition rail.h:147
CursorID autorail
Cursor for autorail tool.
Definition rail.h:159
SpriteID single_y
single piece of rail in Y direction, without ground
Definition rail.h:127
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition rail.h:168
StringID name
Name of this rail type.
Definition rail.h:166
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel
Definition rail.h:181
uint8_t fallback_railtype
Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
Definition rail.h:191
uint GetRailtypeSpriteOffset() const
Offset between the current railtype and normal rail.
Definition rail.h:287
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition rail.h:167
SpriteID single_s
single piece of rail in the southern corner
Definition rail.h:129
RailTypeFlags flags
Bit mask of rail type flags.
Definition rail.h:201
uint8_t curve_speed
Multiplier for curve maximum speed advantage.
Definition rail.h:196
SpriteID signals[SIGTYPE_END][2][2]
signal GUI sprites (type, variant, state)
Definition rail.h:151
SpriteID build_ew_rail
button for building single rail in E-W direction
Definition rail.h:145
SpriteID build_y_rail
button for building single rail in Y direction
Definition rail.h:146
FlatSet< RailTypeLabel > alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:231
struct RailTypeInfo::@22 strings
Strings associated with the rail type.
uint16_t cost_multiplier
Cost multiplier for building this rail type.
Definition rail.h:206
uint8_t bridge_offset
Bridge offset.
Definition rail.h:186
struct RailTypeInfo::@19 base_sprites
Struct containing the main sprites.
SpriteID track_ns
two pieces of rail in North and South corner (East-West direction)
Definition rail.h:124
StringID replace_text
Text used in the autoreplace GUI.
Definition rail.h:170
struct RailTypeInfo::@21 cursor
Cursors associated with the rail type.
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition rail.h:169
SpriteID snow_offset
sprite number difference between a piece of track on a snowy ground and the corresponding one on norm...
Definition rail.h:175
SpriteID track_y
single piece of rail in Y direction, with ground
Definition rail.h:123
struct RailTypeInfo::@20 gui_sprites
struct containing the sprites for the rail GUI.
SpriteID build_depot
button for building depots
Definition rail.h:148
SpriteID single_w
single piece of rail in the western corner
Definition rail.h:131
SpriteID single_e
single piece of rail in the eastern corner
Definition rail.h:130
SpriteID build_ns_rail
button for building single rail in N-S direction
Definition rail.h:143
CursorID rail_ns
Cursor for building rail in N-S direction.
Definition rail.h:155
StringID new_loco
Name of an engine for this type of rail in the engine preview GUI.
Definition rail.h:171
const GRFFile * grffile[RTSG_END]
NewGRF providing the Action3 for the railtype.
Definition rail.h:266
SpriteID tunnel
tunnel sprites base
Definition rail.h:134
SpriteID single_sloped
single piece of rail for slopes
Definition rail.h:132
uint32_t IntSqrt(uint32_t num)
Compute the integer square root.
Definition math_func.cpp:42
Functions related to the economy.
VehicleAccelerationModel
Acceleration model of a vehicle.
Definition engine_type.h:47
Type (helpers) for enums.
Flat set container implementation.
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
uint32_t CursorID
The number of the cursor (sprite)
Definition gfx_type.h:19
Types related to NewGRF badges.
Money RailConvertCost(RailType from, RailType to)
Calculates the cost of rail conversion.
Definition rail.h:457
int TicksToLeaveDepot(const Train *v)
Compute number of ticks when next wagon will leave a depot.
RailTrackBridgeOffset
Offsets for sprites within a bridge surface overlay set.
Definition rail.h:84
@ RTBO_Y
Piece of rail in Y direction.
Definition rail.h:86
@ RTBO_X
Piece of rail in X direction.
Definition rail.h:85
@ RTBO_SLOPE
Sloped rail pieces, in order NE, SE, SW, NW.
Definition rail.h:87
Money RailClearCost(RailType railtype)
Returns the 'cost' of clearing the specified railtype.
Definition rail.h:440
Money RailBuildCost(RailType railtype)
Returns the cost of building the specified railtype.
Definition rail.h:429
RailType AllocateRailType(RailTypeLabel label)
Allocate a new rail type label.
Definition rail_cmd.cpp:148
std::vector< RailType > _sorted_railtypes
Sorted list of rail types.
Definition rail_cmd.cpp:47
bool HasAnyRailTypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition rail.cpp:78
RailFenceOffset
Offsets from base sprite for fence sprites.
Definition rail.h:94
@ RFO_FLAT_RIGHT
Slope FLAT, Track RIGHT, Fence W.
Definition rail.h:105
@ RFO_FLAT_Y_NE
Slope FLAT, Track Y, Fence NE.
Definition rail.h:96
@ RFO_SLOPE_NW_SW
Slope NW, Track Y, Fence SW.
Definition rail.h:110
@ RFO_FLAT_X_SE
Slope FLAT, Track X, Fence SE.
Definition rail.h:103
@ RFO_SLOPE_NW_NE
Slope NW, Track Y, Fence NE.
Definition rail.h:102
@ RFO_SLOPE_SE_SW
Slope SE, Track Y, Fence SW.
Definition rail.h:108
@ RFO_SLOPE_NE_SE
Slope NE, Track X, Fence SE.
Definition rail.h:109
@ RFO_FLAT_LOWER
Slope FLAT, Track LOWER, Fence N.
Definition rail.h:106
@ RFO_SLOPE_SW_SE
Slope SW, Track X, Fence SE.
Definition rail.h:107
@ RFO_FLAT_UPPER
Slope FLAT, Track UPPER, Fence S.
Definition rail.h:98
@ RFO_SLOPE_SE_NE
Slope SE, Track Y, Fence NE.
Definition rail.h:100
@ RFO_FLAT_Y_SW
Slope FLAT, Track Y, Fence SW.
Definition rail.h:104
@ RFO_FLAT_LEFT
Slope FLAT, Track LEFT, Fence E.
Definition rail.h:97
@ RFO_SLOPE_NE_NW
Slope NE, Track X, Fence NW.
Definition rail.h:101
@ RFO_FLAT_X_NW
Slope FLAT, Track X, Fence NW.
Definition rail.h:95
@ RFO_SLOPE_SW_NW
Slope SW, Track X, Fence NW.
Definition rail.h:99
RailTypes GetAllIntroducesRailTypes(RailTypes railtypes)
Returns all introduced railtypes for a set of railtypes.
Definition rail.h:337
Money RailMaintenanceCost(RailType railtype, uint32_t num, uint32_t total_num)
Calculates the maintenance cost of a number of track bits.
Definition rail.h:484
bool HasRailTypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition rail.cpp:68
RailTypes GetAllPoweredRailTypes(RailTypes railtypes)
Returns all powered railtypes for a set of railtypes.
Definition rail.h:325
bool IsCompatibleRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType can drive on a tile with a given RailType.
Definition rail.h:352
void InitRailTypes()
Resolve sprites of custom rail types.
Definition rail_cmd.cpp:130
bool HasPowerOnRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType got power on a tile with a given RailType.
Definition rail.h:377
RailTypes AddDateIntroducedRailTypes(RailTypes current, TimerGameCalendar::Date date)
Add the rail types that are to be introduced at the given date.
Definition rail.cpp:102
void ResetRailTypes()
Reset all rail type information to its default values.
Definition rail_cmd.cpp:65
Money SignalMaintenanceCost(uint32_t num)
Calculates the maintenance cost of a number of signals.
Definition rail.h:495
bool Rail90DegTurnDisallowed(RailType rt1, RailType rt2, bool def=_settings_game.pf.forbid_90_deg)
Test if 90 degree turns are disallowed between two railtypes.
Definition rail.h:411
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:301
RailTypes GetAllCompatibleRailTypes(RailTypes railtypes)
Returns all compatible railtypes for a set of railtypes.
Definition rail.h:313
RailTypeFlag
Railtype flag bit numbers.
Definition rail.h:28
@ Catenary
Bit number for drawing a catenary.
@ Allow90Deg
Bit number for always allowed 90 degree turns, regardless of setting.
@ Disallow90Deg
Bit number for never allowed 90 degree turns, regardless of setting.
@ Hidden
Bit number for hiding from selection.
@ NoSpriteCombine
Bit number for using non-combined junctions.
@ NoLevelCrossing
Bit number for disallowing level crossings.
RailTypes GetCompanyRailTypes(CompanyID company, bool introduces=true)
Get the rail types the given company can build.
Definition rail.cpp:135
RailTypeSpriteGroup
Sprite groups for a railtype.
Definition rail.h:41
@ RTSG_GROUND
Main group of ground images.
Definition rail.h:44
@ RTSG_PYLONS
Catenary pylons.
Definition rail.h:47
@ RTSG_CURSORS
Cursor and toolbar icon images.
Definition rail.h:42
@ RTSG_GROUND_COMPLETE
Complete ground images.
Definition rail.h:54
@ RTSG_CROSSING
Level crossing overlay images.
Definition rail.h:49
@ RTSG_TUNNEL
Main group of ground images for snow or desert.
Definition rail.h:45
@ RTSG_WIRES
Catenary wires.
Definition rail.h:46
@ RTSG_OVERLAY
Images for overlaying track.
Definition rail.h:43
@ RTSG_SIGNALS
Signal images.
Definition rail.h:53
@ RTSG_DEPOT
Depot images.
Definition rail.h:50
@ RTSG_BRIDGE
Bridge surface images.
Definition rail.h:48
@ RTSG_TUNNEL_PORTAL
Tunnel portal overlay.
Definition rail.h:52
@ RTSG_FENCES
Fence images.
Definition rail.h:51
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition rail.cpp:166
Foundation GetRailFoundation(Slope tileh, TrackBits bits)
Checks if a track combination is valid on a specific slope and returns the needed foundation.
Definition rail_cmd.cpp:309
RailTrackOffset
Offsets for sprites within an overlay/underlay set.
Definition rail.h:62
@ RTO_Y
Piece of rail in Y direction.
Definition rail.h:64
@ RTO_S
Piece of rail in southern corner.
Definition rail.h:66
@ RTO_JUNCTION_NE
Ballast for junction 'pointing' NE.
Definition rail.h:75
@ RTO_JUNCTION_NSEW
Ballast for full junction.
Definition rail.h:78
@ RTO_JUNCTION_SW
Ballast for junction 'pointing' SW.
Definition rail.h:74
@ RTO_SLOPE_SE
Piece of rail on slope with south-east raised.
Definition rail.h:70
@ RTO_E
Piece of rail in eastern corner.
Definition rail.h:67
@ RTO_W
Piece of rail in western corner.
Definition rail.h:68
@ RTO_CROSSING_XY
Crossing of X and Y rail, with ballast.
Definition rail.h:73
@ RTO_SLOPE_SW
Piece of rail on slope with south-west raised.
Definition rail.h:71
@ RTO_SLOPE_NW
Piece of rail on slope with north-west raised.
Definition rail.h:72
@ RTO_JUNCTION_NW
Ballast for junction 'pointing' NW.
Definition rail.h:77
@ RTO_JUNCTION_SE
Ballast for junction 'pointing' SE.
Definition rail.h:76
@ RTO_SLOPE_NE
Piece of rail on slope with north-east raised.
Definition rail.h:69
@ RTO_X
Piece of rail in X direction.
Definition rail.h:63
@ RTO_N
Piece of rail in northern corner.
Definition rail.h:65
bool RailNoLevelCrossings(RailType rt)
Test if a RailType disallows build of level crossings.
Definition rail.h:399
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels=true)
Get the rail type for a given label.
Definition rail.cpp:195
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition rail.cpp:90
The different types of rail.
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:31
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:32
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Types related to global configuration settings.
Types and classes related to signals.
Definitions of a slope.
Slope
Enumeration for the slope-type.
Definition slope_type.h:48
Foundation
Enumeration for Foundations.
Definition slope_type.h:93
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:115
PathfinderSettings pf
settings for all pathfinders
bool forbid_90_deg
forbid trains to make 90 deg turns
Colour for pixel/line drawing.
Definition gfx_type.h:405
Templated helper to make a type-safe 'typedef' representing a single POD value.
'Train' is either a loco or a wagon.
Definition train.h:91
Definition of the game-calendar-timer.
All types related to tracks.
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35