OpenTTD
rail.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef RAIL_H
13 #define RAIL_H
14 
15 #include "rail_type.h"
16 #include "track_type.h"
17 #include "gfx_type.h"
18 #include "core/bitmath_func.hpp"
19 #include "economy_func.h"
20 #include "slope_type.h"
21 #include "strings_type.h"
22 #include "date_type.h"
23 #include "signal_type.h"
24 #include "settings_type.h"
25 
30  RTF_HIDDEN = 2,
34 
35  RTFB_NONE = 0,
42 };
44 
45 struct SpriteGroup;
46 
62  RTSG_END,
63 };
64 
86 };
87 
95 };
96 
118 };
119 
121 typedef std::vector<RailTypeLabel> RailTypeLabelList;
122 
127 public:
132  struct {
145  } base_sprites;
146 
151  struct {
160  SpriteID signals[SIGTYPE_END][2][2];
161  } gui_sprites;
162 
163  struct {
172  } cursor;
173 
174  struct {
181  } strings;
182 
185 
188 
191 
196 
201 
206 
211 
216 
221 
226 
230  uint16 max_speed;
231 
235  RailTypeLabel label;
236 
241 
246 
255 
261 
266 
271 
275  const GRFFile *grffile[RTSG_END];
276 
280  const SpriteGroup *group[RTSG_END];
281 
282  inline bool UsesOverlay() const
283  {
284  return this->group[RTSG_GROUND] != nullptr;
285  }
286 
294  inline uint GetRailtypeSpriteOffset() const
295  {
296  return 82 * this->fallback_railtype;
297  }
298 };
299 
300 
306 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
307 {
308  extern RailtypeInfo _railtypes[RAILTYPE_END];
309  assert(railtype < RAILTYPE_END);
310  return &_railtypes[railtype];
311 }
312 
321 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
322 {
323  return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
324 }
325 
334 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
335 {
336  return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
337 }
338 
344 static inline bool RailNoLevelCrossings(RailType rt)
345 {
347 }
348 
356 static inline bool Rail90DegTurnDisallowed(RailType rt1, RailType rt2, bool def = _settings_game.pf.forbid_90_deg)
357 {
358  if (rt1 == INVALID_RAILTYPE || rt2 == INVALID_RAILTYPE) return def;
359 
360  const RailtypeInfo *rti1 = GetRailTypeInfo(rt1);
361  const RailtypeInfo *rti2 = GetRailTypeInfo(rt2);
362 
363  bool rt1_90deg = HasBit(rti1->flags, RTF_DISALLOW_90DEG) || (!HasBit(rti1->flags, RTF_ALLOW_90DEG) && def);
364  bool rt2_90deg = HasBit(rti2->flags, RTF_DISALLOW_90DEG) || (!HasBit(rti2->flags, RTF_ALLOW_90DEG) && def);
365 
366  return rt1_90deg || rt2_90deg;
367 }
368 
374 static inline Money RailBuildCost(RailType railtype)
375 {
376  assert(railtype < RAILTYPE_END);
377  return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
378 }
379 
385 static inline Money RailClearCost(RailType railtype)
386 {
387  /* Clearing rail in fact earns money, but if the build cost is set
388  * very low then a loophole exists where money can be made.
389  * In this case we limit the removal earnings to 3/4s of the build
390  * cost.
391  */
392  assert(railtype < RAILTYPE_END);
393  return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
394 }
395 
402 static inline Money RailConvertCost(RailType from, RailType to)
403 {
404  /* Get the costs for removing and building anew
405  * A conversion can never be more costly */
406  Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
407 
408  /* Conversion between somewhat compatible railtypes:
409  * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
410  * build costs, if the target type is more expensive (material upgrade costs).
411  * Upgrade can never be more expensive than re-building. */
412  if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
413  Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
414  return min(upgradecost, rebuildcost);
415  }
416 
417  /* make the price the same as remove + build new type for rail types
418  * which are not compatible in any way */
419  return rebuildcost;
420 }
421 
429 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
430 {
431  assert(railtype < RAILTYPE_END);
432  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.
433 }
434 
440 static inline Money SignalMaintenanceCost(uint32 num)
441 {
442  return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
443 }
444 
445 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
446 int TicksToLeaveDepot(const Train *v);
447 
449 
450 
451 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
452 bool HasAnyRailtypesAvail(const CompanyID company);
453 bool ValParamRailtype(const RailType rail);
454 
456 
457 RailType GetBestRailtype(const CompanyID company);
458 RailTypes GetCompanyRailtypes(CompanyID company, bool introduces = true);
459 RailTypes GetRailTypes(bool introduces);
460 
461 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
462 
463 void ResetRailTypes();
464 void InitRailTypes();
465 RailType AllocateRailType(RailTypeLabel label);
466 
467 extern std::vector<RailType> _sorted_railtypes;
468 extern RailTypes _railtypes_hidden_mask;
469 
474 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes.size() && (var = _sorted_railtypes[index], true) ; index++)
475 
476 #endif /* RAIL_H */
All types related to tracks.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Piece of rail on slope with north-west raised.
Definition: rail.h:79
static 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:334
SpriteID crossing
level crossing, rail in X direction
Definition: rail.h:143
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
const SpriteGroup * group[RTSG_END]
Sprite groups for resolving sprites.
Definition: rail.h:280
struct RailtypeInfo::@36 base_sprites
Struct containing the main sprites.
RailTypeFlags
Railtype flags.
Definition: rail.h:27
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:81
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:306
const GRFFile * grffile[RTSG_END]
NewGRF providing the Action3 for the railtype.
Definition: rail.h:275
Definitions of a slope.
Slope NW, Track Y, Fence NE.
Definition: rail.h:109
static Money RailConvertCost(RailType from, RailType to)
Calculates the cost of rail conversion.
Definition: rail.h:402
SpriteID single_sloped
single piece of rail for slopes
Definition: rail.h:142
SpriteID auto_rail
button for the autorail construction
Definition: rail.h:156
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition: rail.h:176
Slope FLAT, Track Y, Fence NE.
Definition: rail.h:103
int TicksToLeaveDepot(const Train *v)
Compute number of ticks when next wagon will leave a depot.
Definition: rail_cmd.cpp:2932
byte curve_speed
Multiplier for curve maximum speed advantage.
Definition: rail.h:205
SpriteID build_y_rail
button for building single rail in Y direction
Definition: rail.h:155
Piece of rail in southern corner.
Definition: rail.h:73
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition: rail.h:260
RailTypeFlags flags
Bit mask of rail type flags.
Definition: rail.h:210
Bridge surface images.
Definition: rail.h:55
Ballast for junction &#39;pointing&#39; NE.
Definition: rail.h:82
Flag for invalid railtype.
Definition: rail_type.h:36
Ballast for junction &#39;pointing&#39; NW.
Definition: rail.h:84
bool ValParamRailtype(const RailType rail)
Validate functions for rail building.
Definition: rail.cpp:208
Piece of rail on slope with north-east raised.
Definition: rail.h:76
Piece of rail in western corner.
Definition: rail.h:75
SpriteID single_e
single piece of rail in the eastern corner
Definition: rail.h:140
Fence images.
Definition: rail.h:58
PathfinderSettings pf
settings for all pathfinders
RailTypeLabelList alternate_labels
Rail type labels this type provides in addition to the main label.
Definition: rail.h:240
static Money SignalMaintenanceCost(uint32 num)
Calculates the maintenance cost of a number of signals.
Definition: rail.h:440
Functions related to bit mathematics.
Slope FLAT, Track LOWER, Fence N.
Definition: rail.h:113
struct RailtypeInfo::@38 cursor
Cursors associated with the rail type.
bool forbid_90_deg
forbid trains to make 90 deg turns
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel ...
Definition: rail.h:190
Value for never allowed 90 degree turns, regardless of setting.
Definition: rail.h:41
Slope FLAT, Track LEFT, Fence E.
Definition: rail.h:104
CursorID autorail
Cursor for autorail tool.
Definition: rail.h:168
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
static bool RailNoLevelCrossings(RailType rt)
Test if a RailType disallows build of level crossings.
Definition: rail.h:344
struct RailtypeInfo::@39 strings
Strings associated with the rail type.
SpriteID track_y
single piece of rail in Y direction, with ground
Definition: rail.h:133
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition: rail.h:177
StringID new_loco
Name of an engine for this type of rail in the engine preview GUI.
Definition: rail.h:180
CursorID rail_ew
Cursor for building rail in E-W direction.
Definition: rail.h:166
Catenary pylons.
Definition: rail.h:54
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition: rail.h:178
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:126
SpriteID signals[SIGTYPE_END][2][2]
signal GUI sprites (type, variant, state)
Definition: rail.h:160
RailFenceOffset
Offsets from base sprite for fence sprites.
Definition: rail.h:101
Value for always allowed 90 degree turns, regardless of setting.
Definition: rail.h:40
SpriteID single_y
single piece of rail in Y direction, without ground
Definition: rail.h:137
CursorID tunnel
Cursor for building a tunnel.
Definition: rail.h:170
Value for using non-combined junctions.
Definition: rail.h:39
Types and classes related to signals.
Foundation
Enumeration for Foundations.
Definition: slope_type.h:95
SpriteID ground
ground sprite for a 3-way switch
Definition: rail.h:135
RailType AllocateRailType(RailTypeLabel label)
Allocate a new rail type label.
Definition: rail_cmd.cpp:160
Sloped rail pieces, in order NE, SE, SW, NW.
Definition: rail.h:94
Piece of rail in X direction.
Definition: rail.h:92
Level crossing overlay images.
Definition: rail.h:56
Images for overlaying track.
Definition: rail.h:50
Slope SE, Track Y, Fence SW.
Definition: rail.h:115
Slope FLAT, Track UPPER, Fence S.
Definition: rail.h:105
Piece of rail on slope with south-west raised.
Definition: rail.h:78
Piece of rail in X direction.
Definition: rail.h:70
Types related to global configuration settings.
SpriteID build_ns_rail
button for building single rail in N-S direction
Definition: rail.h:152
uint8 acceleration_type
Acceleration type of this rail type.
Definition: rail.h:225
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced. ...
Definition: rail.h:265
byte sorting_order
The sorting order of this railtype for the toolbar dropdown.
Definition: rail.h:270
Value for drawing a catenary.
Definition: rail.h:36
Slope SW, Track X, Fence NW.
Definition: rail.h:106
Bit number for using non-combined junctions.
Definition: rail.h:31
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:40
RailTypes GetCompanyRailtypes(CompanyID company, bool introduces=true)
Get the rail types the given company can build.
Definition: rail.cpp:268
Value for hiding from selection.
Definition: rail.h:38
uint16 max_speed
Maximum speed for vehicles travelling on this rail type.
Definition: rail.h:230
SpriteID single_w
single piece of rail in the western corner
Definition: rail.h:141
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:331
bool HasRailtypeAvail(const CompanyID company, const RailType railtype)
Finds out if a company has a certain buildable railtype available.
Definition: rail.cpp:188
CursorID rail_nwse
Cursor for building rail in Y direction.
Definition: rail.h:167
Bit number for drawing a catenary.
Definition: rail.h:28
Crossing of X and Y rail, with ballast.
Definition: rail.h:80
SpriteID single_x
single piece of rail in X direction, without ground
Definition: rail.h:136
SpriteID convert_rail
button for converting rail
Definition: rail.h:159
static Money RailClearCost(RailType railtype)
Returns the &#39;cost&#39; of clearing the specified railtype.
Definition: rail.h:385
Depot images.
Definition: rail.h:57
RailTrackBridgeOffset
Offsets for sprites within a bridge surface overlay set.
Definition: rail.h:91
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:235
RailTypes
The different railtypes we support, but then a bitmask of them.
Definition: rail_type.h:52
All flags cleared.
Definition: rail.h:35
RailTypes GetRailTypes(bool introduces)
Get list of rail types, regardless of company availability.
Definition: rail.cpp:300
Piece of rail in Y direction.
Definition: rail.h:71
Bit number for never allowed 90 degree turns, regardless of setting.
Definition: rail.h:33
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Piece of rail on slope with south-east raised.
Definition: rail.h:77
Piece of rail in Y direction.
Definition: rail.h:93
Slope FLAT, Track X, Fence SE.
Definition: rail.h:110
SpriteID single_n
single piece of rail in the northern corner
Definition: rail.h:138
Piece of rail in eastern corner.
Definition: rail.h:74
SpriteID single_s
single piece of rail in the southern corner
Definition: rail.h:139
Piece of rail in northern corner.
Definition: rail.h:72
StringID replace_text
Text used in the autoreplace GUI.
Definition: rail.h:179
Slope NE, Track X, Fence NW.
Definition: rail.h:108
SpriteID build_depot
button for building depots
Definition: rail.h:157
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:87
CursorID depot
Cursor for building a depot.
Definition: rail.h:169
Complete ground images.
Definition: rail.h:61
Bit number for hiding from selection.
Definition: rail.h:30
CursorID convert
Cursor for converting track.
Definition: rail.h:171
Date introduction_date
Introduction date.
Definition: rail.h:254
uint16 cost_multiplier
Cost multiplier for building this rail type.
Definition: rail.h:215
RailTypeSpriteGroup
Sprite groups for a railtype.
Definition: rail.h:48
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date)
Add the rail types that are to be introduced at the given date.
Definition: rail.cpp:235
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
CursorID rail_swne
Cursor for building rail in X direction.
Definition: rail.h:165
Slope FLAT, Track RIGHT, Fence W.
Definition: rail.h:112
byte fallback_railtype
Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations...
Definition: rail.h:200
static Money RailBuildCost(RailType railtype)
Returns the cost of building the specified railtype.
Definition: rail.h:374
Functions related to the economy.
Value for disallowing level crossings.
Definition: rail.h:37
Cursor and toolbar icon images.
Definition: rail.h:49
Tunnel portal overlay.
Definition: rail.h:59
byte map_colour
Colour on mini-map.
Definition: rail.h:245
void InitRailTypes()
Resolve sprites of custom rail types.
Definition: rail_cmd.cpp:140
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
SpriteID build_tunnel
button for building a tunnel
Definition: rail.h:158
Ballast for junction &#39;pointing&#39; SE.
Definition: rail.h:83
Ballast for junction &#39;pointing&#39; SW.
Definition: rail.h:81
Main group of ground images.
Definition: rail.h:51
Slope SE, Track Y, Fence NE.
Definition: rail.h:107
static 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:321
RailTypes powered_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype generates power ...
Definition: rail.h:187
Catenary wires.
Definition: rail.h:53
uint GetRailtypeSpriteOffset() const
Offset between the current railtype and normal rail.
Definition: rail.h:294
void ResetRailTypes()
Reset all rail type information to its default values.
Definition: rail_cmd.cpp:65
uint32 CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:21
struct RailtypeInfo::@37 gui_sprites
struct containing the sprites for the rail GUI.
Slope SW, Track X, Fence SE.
Definition: rail.h:114
RailType GetBestRailtype(const CompanyID company)
Returns the "best" railtype a company can build.
Definition: rail.cpp:220
SpriteID bridge_offset
Bridge offset.
Definition: rail.h:195
Types related to strings.
Types related to the dates in OpenTTD.
int32 Date
The type to store our dates in.
Definition: date_type.h:16
StringID name
Name of this rail type.
Definition: rail.h:175
RailTrackOffset
Offsets for sprites within an overlay/underlay set.
Definition: rail.h:69
Used for iterations.
Definition: rail_type.h:35
uint32 IntSqrt(uint32 num)
Compute the integer square root.
Definition: math_func.cpp:79
SpriteID tunnel
tunnel sprites base
Definition: rail.h:144
CursorID rail_ns
Cursor for building rail in N-S direction.
Definition: rail.h:164
Slope NE, Track X, Fence SE.
Definition: rail.h:116
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Main group of ground images for snow or desert.
Definition: rail.h:52
SpriteID track_ns
two pieces of rail in North and South corner (East-West direction)
Definition: rail.h:134
Ballast for full junction.
Definition: rail.h:85
Slope FLAT, Track X, Fence NW.
Definition: rail.h:102
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels=true)
Get the rail type for a given label.
Definition: rail.cpp:330
Signal images.
Definition: rail.h:60
SpriteID build_x_rail
button for building single rail in X direction
Definition: rail.h:153
Slope NW, Track Y, Fence SW.
Definition: rail.h:117
static Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
Calculates the maintenance cost of a number of track bits.
Definition: rail.h:429
SpriteID build_ew_rail
button for building single rail in E-W direction
Definition: rail.h:154
static 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:356
Bit number for disallowing level crossings.
Definition: rail.h:29
bool HasAnyRailtypesAvail(const CompanyID company)
Test if any buildable railtype is available for a company.
Definition: rail.cpp:198
Types related to the graphics and/or input devices.
Slope FLAT, Track Y, Fence SW.
Definition: rail.h:111
SpriteID snow_offset
sprite number difference between a piece of track on a snowy ground and the corresponding one on norm...
Definition: rail.h:184
Bit number for always allowed 90 degree turns, regardless of setting.
Definition: rail.h:32
std::vector< RailTypeLabel > RailTypeLabelList
List of rail type labels.
Definition: rail.h:121
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
uint16 maintenance_multiplier
Cost multiplier for maintenance of this rail type.
Definition: rail.h:220
The different types of rail.