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/>.
8 /** @file rail.h Rail specific functions. */
13 #include "rail_type.h"
14 #include "track_type.h"
16 #include "core/bitmath_func.hpp"
17 #include "economy_func.h"
18 #include "slope_type.h"
19 #include "strings_type.h"
20 #include "timer/timer_game_calendar.h"
21 #include "signal_type.h"
22 #include "settings_type.h"
24 /** Railtype flag bit numbers. */
26 RTF_CATENARY
= 0, ///< Bit number for drawing a catenary.
27 RTF_NO_LEVEL_CROSSING
= 1, ///< Bit number for disallowing level crossings.
28 RTF_HIDDEN
= 2, ///< Bit number for hiding from selection.
29 RTF_NO_SPRITE_COMBINE
= 3, ///< Bit number for using non-combined junctions.
30 RTF_ALLOW_90DEG
= 4, ///< Bit number for always allowed 90 degree turns, regardless of setting.
31 RTF_DISALLOW_90DEG
= 5, ///< Bit number for never allowed 90 degree turns, regardless of setting.
34 /** Railtype flags. */
35 enum RailTypeFlags
: uint8_t {
36 RTFB_NONE
= 0, ///< All flags cleared.
37 RTFB_CATENARY
= 1 << RTF_CATENARY
, ///< Value for drawing a catenary.
38 RTFB_NO_LEVEL_CROSSING
= 1 << RTF_NO_LEVEL_CROSSING
, ///< Value for disallowing level crossings.
39 RTFB_HIDDEN
= 1 << RTF_HIDDEN
, ///< Value for hiding from selection.
40 RTFB_NO_SPRITE_COMBINE
= 1 << RTF_NO_SPRITE_COMBINE
, ///< Value for using non-combined junctions.
41 RTFB_ALLOW_90DEG
= 1 << RTF_ALLOW_90DEG
, ///< Value for always allowed 90 degree turns, regardless of setting.
42 RTFB_DISALLOW_90DEG
= 1 << RTF_DISALLOW_90DEG
, ///< Value for never allowed 90 degree turns, regardless of setting.
44 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags
)
48 /** Sprite groups for a railtype. */
49 enum RailTypeSpriteGroup
{
50 RTSG_CURSORS
, ///< Cursor and toolbar icon images
51 RTSG_OVERLAY
, ///< Images for overlaying track
52 RTSG_GROUND
, ///< Main group of ground images
53 RTSG_TUNNEL
, ///< Main group of ground images for snow or desert
54 RTSG_WIRES
, ///< Catenary wires
55 RTSG_PYLONS
, ///< Catenary pylons
56 RTSG_BRIDGE
, ///< Bridge surface images
57 RTSG_CROSSING
, ///< Level crossing overlay images
58 RTSG_DEPOT
, ///< Depot images
59 RTSG_FENCES
, ///< Fence images
60 RTSG_TUNNEL_PORTAL
, ///< Tunnel portal overlay
61 RTSG_SIGNALS
, ///< Signal images
62 RTSG_GROUND_COMPLETE
, ///< Complete ground images
67 * Offsets for sprites within an overlay/underlay set.
68 * These are the same for overlay and underlay sprites.
70 enum RailTrackOffset
{
71 RTO_X
, ///< Piece of rail in X direction
72 RTO_Y
, ///< Piece of rail in Y direction
73 RTO_N
, ///< Piece of rail in northern corner
74 RTO_S
, ///< Piece of rail in southern corner
75 RTO_E
, ///< Piece of rail in eastern corner
76 RTO_W
, ///< Piece of rail in western corner
77 RTO_SLOPE_NE
, ///< Piece of rail on slope with north-east raised
78 RTO_SLOPE_SE
, ///< Piece of rail on slope with south-east raised
79 RTO_SLOPE_SW
, ///< Piece of rail on slope with south-west raised
80 RTO_SLOPE_NW
, ///< Piece of rail on slope with north-west raised
81 RTO_CROSSING_XY
, ///< Crossing of X and Y rail, with ballast
82 RTO_JUNCTION_SW
, ///< Ballast for junction 'pointing' SW
83 RTO_JUNCTION_NE
, ///< Ballast for junction 'pointing' NE
84 RTO_JUNCTION_SE
, ///< Ballast for junction 'pointing' SE
85 RTO_JUNCTION_NW
, ///< Ballast for junction 'pointing' NW
86 RTO_JUNCTION_NSEW
,///< Ballast for full junction
90 * Offsets for sprites within a bridge surface overlay set.
92 enum RailTrackBridgeOffset
{
93 RTBO_X
, ///< Piece of rail in X direction
94 RTBO_Y
, ///< Piece of rail in Y direction
95 RTBO_SLOPE
, ///< Sloped rail pieces, in order NE, SE, SW, NW
99 * Offsets from base sprite for fence sprites. These are in the order of
100 * the sprites in the original data files.
102 enum RailFenceOffset
{
103 RFO_FLAT_X_NW
, //!< Slope FLAT, Track X, Fence NW
104 RFO_FLAT_Y_NE
, //!< Slope FLAT, Track Y, Fence NE
105 RFO_FLAT_LEFT
, //!< Slope FLAT, Track LEFT, Fence E
106 RFO_FLAT_UPPER
, //!< Slope FLAT, Track UPPER, Fence S
107 RFO_SLOPE_SW_NW
, //!< Slope SW, Track X, Fence NW
108 RFO_SLOPE_SE_NE
, //!< Slope SE, Track Y, Fence NE
109 RFO_SLOPE_NE_NW
, //!< Slope NE, Track X, Fence NW
110 RFO_SLOPE_NW_NE
, //!< Slope NW, Track Y, Fence NE
111 RFO_FLAT_X_SE
, //!< Slope FLAT, Track X, Fence SE
112 RFO_FLAT_Y_SW
, //!< Slope FLAT, Track Y, Fence SW
113 RFO_FLAT_RIGHT
, //!< Slope FLAT, Track RIGHT, Fence W
114 RFO_FLAT_LOWER
, //!< Slope FLAT, Track LOWER, Fence N
115 RFO_SLOPE_SW_SE
, //!< Slope SW, Track X, Fence SE
116 RFO_SLOPE_SE_SW
, //!< Slope SE, Track Y, Fence SW
117 RFO_SLOPE_NE_SE
, //!< Slope NE, Track X, Fence SE
118 RFO_SLOPE_NW_SW
, //!< Slope NW, Track Y, Fence SW
121 /** List of rail type labels. */
122 typedef std::vector
<RailTypeLabel
> RailTypeLabelList
;
125 * This struct contains all the info that is needed to draw and construct tracks.
130 * Struct containing the main sprites. @note not all sprites are listed, but only
131 * the ones used directly in the code
134 SpriteID track_y
; ///< single piece of rail in Y direction, with ground
135 SpriteID track_ns
; ///< two pieces of rail in North and South corner (East-West direction)
136 SpriteID ground
; ///< ground sprite for a 3-way switch
137 SpriteID single_x
; ///< single piece of rail in X direction, without ground
138 SpriteID single_y
; ///< single piece of rail in Y direction, without ground
139 SpriteID single_n
; ///< single piece of rail in the northern corner
140 SpriteID single_s
; ///< single piece of rail in the southern corner
141 SpriteID single_e
; ///< single piece of rail in the eastern corner
142 SpriteID single_w
; ///< single piece of rail in the western corner
143 SpriteID single_sloped
;///< single piece of rail for slopes
144 SpriteID crossing
; ///< level crossing, rail in X direction
145 SpriteID tunnel
; ///< tunnel sprites base
149 * struct containing the sprites for the rail GUI. @note only sprites referred to
150 * directly in the code are listed
153 SpriteID build_ns_rail
; ///< button for building single rail in N-S direction
154 SpriteID build_x_rail
; ///< button for building single rail in X direction
155 SpriteID build_ew_rail
; ///< button for building single rail in E-W direction
156 SpriteID build_y_rail
; ///< button for building single rail in Y direction
157 SpriteID auto_rail
; ///< button for the autorail construction
158 SpriteID build_depot
; ///< button for building depots
159 SpriteID build_tunnel
; ///< button for building a tunnel
160 SpriteID convert_rail
; ///< button for converting rail
161 SpriteID signals
[SIGTYPE_END
][2][2]; ///< signal GUI sprites (type, variant, state)
165 CursorID rail_ns
; ///< Cursor for building rail in N-S direction
166 CursorID rail_swne
; ///< Cursor for building rail in X direction
167 CursorID rail_ew
; ///< Cursor for building rail in E-W direction
168 CursorID rail_nwse
; ///< Cursor for building rail in Y direction
169 CursorID autorail
; ///< Cursor for autorail tool
170 CursorID depot
; ///< Cursor for building a depot
171 CursorID tunnel
; ///< Cursor for building a tunnel
172 CursorID convert
; ///< Cursor for converting track
173 } cursor
; ///< Cursors associated with the rail type.
176 StringID name
; ///< Name of this rail type.
177 StringID toolbar_caption
; ///< Caption in the construction toolbar GUI for this rail type.
178 StringID menu_text
; ///< Name of this rail type in the main toolbar dropdown.
179 StringID build_caption
; ///< Caption of the build vehicle GUI for this rail type.
180 StringID replace_text
; ///< Text used in the autoreplace GUI.
181 StringID new_loco
; ///< Name of an engine for this type of rail in the engine preview GUI.
182 } strings
; ///< Strings associated with the rail type.
184 /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */
185 SpriteID snow_offset
;
187 /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */
188 RailTypes powered_railtypes
;
190 /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */
191 RailTypes compatible_railtypes
;
196 SpriteID bridge_offset
;
199 * Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
201 uint8_t fallback_railtype
;
204 * Multiplier for curve maximum speed advantage
209 * Bit mask of rail type flags
214 * Cost multiplier for building this rail type
216 uint16_t cost_multiplier
;
219 * Cost multiplier for maintenance of this rail type
221 uint16_t maintenance_multiplier
;
224 * Acceleration type of this rail type
226 uint8_t acceleration_type
;
229 * Maximum speed for vehicles travelling on this rail type
234 * Unique 32 bit rail type identifier
239 * Rail type labels this type provides in addition to the main label.
241 RailTypeLabelList alternate_labels
;
250 * When #INVALID_DATE or a vehicle using this railtype gets introduced earlier,
251 * the vehicle's introduction date will be used instead for this railtype.
252 * The introduction at this date is furthermore limited by the
253 * #introduction_required_railtypes.
255 TimerGameCalendar::Date introduction_date
;
258 * Bitmask of railtypes that are required for this railtype to be introduced
259 * at a given #introduction_date.
261 RailTypes introduction_required_railtypes
;
264 * Bitmask of which other railtypes are introduced when this railtype is introduced.
266 RailTypes introduces_railtypes
;
269 * The sorting order of this railtype for the toolbar dropdown.
271 uint8_t sorting_order
;
274 * NewGRF providing the Action3 for the railtype. nullptr if not available.
276 const GRFFile
*grffile
[RTSG_END
];
279 * Sprite groups for resolving sprites
281 const SpriteGroup
*group
[RTSG_END
];
283 inline bool UsesOverlay() const
285 return this->group
[RTSG_GROUND
] != nullptr;
289 * Offset between the current railtype and normal rail. This means that:<p>
290 * 1) All the sprites in a railset MUST be in the same order. This order
291 * is determined by normal rail. Check sprites 1005 and following for this order<p>
292 * 2) The position where the railtype is loaded must always be the same, otherwise
293 * the offset will fail.
295 inline uint
GetRailtypeSpriteOffset() const
297 return 82 * this->fallback_railtype
;
303 * Returns a pointer to the Railtype information for a given railtype
304 * @param railtype the rail type which the information is requested for
305 * @return The pointer to the RailTypeInfo
307 inline const RailTypeInfo
*GetRailTypeInfo(RailType railtype
)
309 extern RailTypeInfo _railtypes
[RAILTYPE_END
];
310 assert(railtype
< RAILTYPE_END
);
311 return &_railtypes
[railtype
];
315 * Checks if an engine of the given RailType can drive on a tile with a given
316 * RailType. This would normally just be an equality check, but for electric
317 * rails (which also support non-electric engines).
318 * @param enginetype The RailType of the engine we are considering.
319 * @param tiletype The RailType of the tile we are considering.
320 * @return Whether the engine can drive on this tile.
322 inline bool IsCompatibleRail(RailType enginetype
, RailType tiletype
)
324 return HasBit(GetRailTypeInfo(enginetype
)->compatible_railtypes
, tiletype
);
328 * Checks if an engine of the given RailType got power on a tile with a given
329 * RailType. This would normally just be an equality check, but for electric
330 * rails (which also support non-electric engines).
331 * @param enginetype The RailType of the engine we are considering.
332 * @param tiletype The RailType of the tile we are considering.
333 * @return Whether the engine got power on this tile.
335 inline bool HasPowerOnRail(RailType enginetype
, RailType tiletype
)
337 return HasBit(GetRailTypeInfo(enginetype
)->powered_railtypes
, tiletype
);
341 * Test if a RailType disallows build of level crossings.
342 * @param rt The RailType to check.
343 * @return Whether level crossings are not allowed.
345 inline bool RailNoLevelCrossings(RailType rt
)
347 return HasBit(GetRailTypeInfo(rt
)->flags
, RTF_NO_LEVEL_CROSSING
);
351 * Test if 90 degree turns are disallowed between two railtypes.
352 * @param rt1 First railtype to test for.
353 * @param rt2 Second railtype to test for.
354 * @param def Default value to use if the rail type doesn't specify anything.
355 * @return True if 90 degree turns are disallowed between the two rail types.
357 inline bool Rail90DegTurnDisallowed(RailType rt1
, RailType rt2
, bool def
= _settings_game
.pf
.forbid_90_deg
)
359 if (rt1
== INVALID_RAILTYPE
|| rt2
== INVALID_RAILTYPE
) return def
;
361 const RailTypeInfo
*rti1
= GetRailTypeInfo(rt1
);
362 const RailTypeInfo
*rti2
= GetRailTypeInfo(rt2
);
364 bool rt1_90deg
= HasBit(rti1
->flags
, RTF_DISALLOW_90DEG
) || (!HasBit(rti1
->flags
, RTF_ALLOW_90DEG
) && def
);
365 bool rt2_90deg
= HasBit(rti2
->flags
, RTF_DISALLOW_90DEG
) || (!HasBit(rti2
->flags
, RTF_ALLOW_90DEG
) && def
);
367 return rt1_90deg
|| rt2_90deg
;
371 * Returns the cost of building the specified railtype.
372 * @param railtype The railtype being built.
373 * @return The cost multiplier.
375 inline Money
RailBuildCost(RailType railtype
)
377 assert(railtype
< RAILTYPE_END
);
378 return (_price
[PR_BUILD_RAIL
] * GetRailTypeInfo(railtype
)->cost_multiplier
) >> 3;
382 * Returns the 'cost' of clearing the specified railtype.
383 * @param railtype The railtype being removed.
386 inline Money
RailClearCost(RailType railtype
)
388 /* Clearing rail in fact earns money, but if the build cost is set
389 * very low then a loophole exists where money can be made.
390 * In this case we limit the removal earnings to 3/4s of the build
393 assert(railtype
< RAILTYPE_END
);
394 return std::max(_price
[PR_CLEAR_RAIL
], -RailBuildCost(railtype
) * 3 / 4);
398 * Calculates the cost of rail conversion
399 * @param from The railtype we are converting from
400 * @param to The railtype we are converting to
401 * @return Cost per TrackBit
403 inline Money
RailConvertCost(RailType from
, RailType to
)
405 /* Get the costs for removing and building anew
406 * A conversion can never be more costly */
407 Money rebuildcost
= RailBuildCost(to
) + RailClearCost(from
);
409 /* Conversion between somewhat compatible railtypes:
410 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
411 * build costs, if the target type is more expensive (material upgrade costs).
412 * Upgrade can never be more expensive than re-building. */
413 if (HasPowerOnRail(from
, to
) || HasPowerOnRail(to
, from
)) {
414 Money upgradecost
= RailBuildCost(to
) / 8 + std::max((Money
)0, RailBuildCost(to
) - RailBuildCost(from
));
415 return std::min(upgradecost
, rebuildcost
);
418 /* make the price the same as remove + build new type for rail types
419 * which are not compatible in any way */
424 * Calculates the maintenance cost of a number of track bits.
425 * @param railtype The railtype to get the cost of.
426 * @param num Number of track bits of this railtype.
427 * @param total_num Total number of track bits of all railtypes.
428 * @return Total cost.
430 inline Money
RailMaintenanceCost(RailType railtype
, uint32_t num
, uint32_t total_num
)
432 assert(railtype
< RAILTYPE_END
);
433 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.
437 * Calculates the maintenance cost of a number of signals.
438 * @param num Number of signals.
439 * @return Total cost.
441 inline Money
SignalMaintenanceCost(uint32_t num
)
443 return (_price
[PR_INFRASTRUCTURE_RAIL
] * 15 * num
* (1 + IntSqrt(num
))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
446 void DrawTrainDepotSprite(int x
, int y
, int image
, RailType railtype
);
447 int TicksToLeaveDepot(const Train
*v
);
449 Foundation
GetRailFoundation(Slope tileh
, TrackBits bits
);
452 bool HasRailTypeAvail(const CompanyID company
, const RailType railtype
);
453 bool HasAnyRailTypesAvail(const CompanyID company
);
454 bool ValParamRailType(const RailType rail
);
456 RailTypes
AddDateIntroducedRailTypes(RailTypes current
, TimerGameCalendar::Date date
);
458 RailTypes
GetCompanyRailTypes(CompanyID company
, bool introduces
= true);
459 RailTypes
GetRailTypes(bool introduces
);
461 RailType
GetRailTypeByLabel(RailTypeLabel label
, bool allow_alternate_labels
= true);
463 void ResetRailTypes();
464 void InitRailTypes();
465 RailType
AllocateRailType(RailTypeLabel label
);
467 extern std::vector
<RailType
> _sorted_railtypes
;
468 extern RailTypes _railtypes_hidden_mask
;