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/>.
10 /** @file rail.h Rail specific functions. */
15 #include "rail_type.h"
16 #include "track_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"
26 /** Railtype flags. */
28 RTF_CATENARY
= 0, ///< Bit number for drawing a catenary.
29 RTF_NO_LEVEL_CROSSING
= 1, ///< Bit number for disallowing level crossings.
31 RTFB_NONE
= 0, ///< All flags cleared.
32 RTFB_CATENARY
= 1 << RTF_CATENARY
, ///< Value for drawing a catenary.
33 RTFB_NO_LEVEL_CROSSING
= 1 << RTF_NO_LEVEL_CROSSING
, ///< Value for disallowing level crossings.
35 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags
)
39 /** Sprite groups for a railtype. */
40 enum RailTypeSpriteGroup
{
41 RTSG_CURSORS
, ///< Cursor and toolbar icon images
42 RTSG_OVERLAY
, ///< Images for overlaying track
43 RTSG_GROUND
, ///< Main group of ground images
44 RTSG_TUNNEL
, ///< Main group of ground images for snow or desert
45 RTSG_WIRES
, ///< Catenary wires
46 RTSG_PYLONS
, ///< Catenary pylons
47 RTSG_BRIDGE
, ///< Bridge surface images
48 RTSG_CROSSING
, ///< Level crossing overlay images
49 RTSG_DEPOT
, ///< Depot images
50 RTSG_FENCES
, ///< Fence images
51 RTSG_TUNNEL_PORTAL
, ///< Tunnel portal overlay
52 RTSG_SIGNALS
, ///< Signal images
57 * Offsets for sprites within an overlay/underlay set.
58 * These are the same for overlay and underlay sprites.
60 enum RailTrackOffset
{
61 RTO_X
, ///< Piece of rail in X direction
62 RTO_Y
, ///< Piece of rail in Y direction
63 RTO_N
, ///< Piece of rail in northern corner
64 RTO_S
, ///< Piece of rail in southern corner
65 RTO_E
, ///< Piece of rail in eastern corner
66 RTO_W
, ///< Piece of rail in western corner
67 RTO_SLOPE_NE
, ///< Piece of rail on slope with north-east raised
68 RTO_SLOPE_SE
, ///< Piece of rail on slope with south-east raised
69 RTO_SLOPE_SW
, ///< Piece of rail on slope with south-west raised
70 RTO_SLOPE_NW
, ///< Piece of rail on slope with north-west raised
71 RTO_CROSSING_XY
, ///< Crossing of X and Y rail, with ballast
72 RTO_JUNCTION_SW
, ///< Ballast for junction 'pointing' SW
73 RTO_JUNCTION_NE
, ///< Ballast for junction 'pointing' NE
74 RTO_JUNCTION_SE
, ///< Ballast for junction 'pointing' SE
75 RTO_JUNCTION_NW
, ///< Ballast for junction 'pointing' NW
76 RTO_JUNCTION_NSEW
,///< Ballast for full junction
80 * Offsets for sprites within a bridge surface overlay set.
82 enum RailTrackBridgeOffset
{
83 RTBO_X
, ///< Piece of rail in X direction
84 RTBO_Y
, ///< Piece of rail in Y direction
85 RTBO_SLOPE
, ///< Sloped rail pieces, in order NE, SE, SW, NW
89 * Offsets from base sprite for fence sprites. These are in the order of
90 * the sprites in the original data files.
92 enum RailFenceOffset
{
93 RFO_FLAT_X_NW
, //!< Slope FLAT, Track X, Fence NW
94 RFO_FLAT_Y_NE
, //!< Slope FLAT, Track Y, Fence NE
95 RFO_FLAT_LEFT
, //!< Slope FLAT, Track LEFT, Fence E
96 RFO_FLAT_UPPER
, //!< Slope FLAT, Track UPPER, Fence S
97 RFO_SLOPE_SW_NW
, //!< Slope SW, Track X, Fence NW
98 RFO_SLOPE_SE_NE
, //!< Slope SE, Track Y, Fence NE
99 RFO_SLOPE_NE_NW
, //!< Slope NE, Track X, Fence NW
100 RFO_SLOPE_NW_NE
, //!< Slope NW, Track Y, Fence NE
101 RFO_FLAT_X_SE
, //!< Slope FLAT, Track X, Fence SE
102 RFO_FLAT_Y_SW
, //!< Slope FLAT, Track Y, Fence SW
103 RFO_FLAT_RIGHT
, //!< Slope FLAT, Track RIGHT, Fence W
104 RFO_FLAT_LOWER
, //!< Slope FLAT, Track LOWER, Fence N
105 RFO_SLOPE_SW_SE
, //!< Slope SW, Track X, Fence SE
106 RFO_SLOPE_SE_SW
, //!< Slope SE, Track Y, Fence SW
107 RFO_SLOPE_NE_SE
, //!< Slope NE, Track X, Fence SE
108 RFO_SLOPE_NW_SW
, //!< Slope NW, Track Y, Fence SW
111 /** List of rail type labels. */
112 typedef SmallVector
<RailTypeLabel
, 4> RailTypeLabelList
;
115 * This struct contains all the info that is needed to draw and construct tracks.
117 struct RailtypeInfo
{
119 * Struct containing the main sprites. @note not all sprites are listed, but only
120 * the ones used directly in the code
123 SpriteID track_y
; ///< single piece of rail in Y direction, with ground
124 SpriteID track_ns
; ///< two pieces of rail in North and South corner (East-West direction)
125 SpriteID ground
; ///< ground sprite for a 3-way switch
126 SpriteID single
[TRACK_END
]; ///< single piece of rail per track, without ground
127 SpriteID single_sloped
;///< single piece of rail for slopes
128 SpriteID crossing
; ///< level crossing, rail in X direction
129 SpriteID tunnel
; ///< tunnel sprites base
133 * struct containing the sprites for the rail GUI. @note only sprites referred to
134 * directly in the code are listed
137 SpriteID build_ns_rail
; ///< button for building single rail in N-S direction
138 SpriteID build_x_rail
; ///< button for building single rail in X direction
139 SpriteID build_ew_rail
; ///< button for building single rail in E-W direction
140 SpriteID build_y_rail
; ///< button for building single rail in Y direction
141 SpriteID auto_rail
; ///< button for the autorail construction
142 SpriteID build_depot
; ///< button for building depots
143 SpriteID build_tunnel
; ///< button for building a tunnel
144 SpriteID convert_rail
; ///< button for converting rail
145 SpriteID signals
[SIGTYPE_END
][2][2]; ///< signal GUI sprites (type, variant, state)
149 CursorID rail_ns
; ///< Cursor for building rail in N-S direction
150 CursorID rail_swne
; ///< Cursor for building rail in X direction
151 CursorID rail_ew
; ///< Cursor for building rail in E-W direction
152 CursorID rail_nwse
; ///< Cursor for building rail in Y direction
153 CursorID autorail
; ///< Cursor for autorail tool
154 CursorID depot
; ///< Cursor for building a depot
155 CursorID tunnel
; ///< Cursor for building a tunnel
156 CursorID convert
; ///< Cursor for converting track
157 } cursor
; ///< Cursors associated with the rail type.
160 StringID name
; ///< Name of this rail type.
161 StringID toolbar_caption
; ///< Caption in the construction toolbar GUI for this rail type.
162 StringID menu_text
; ///< Name of this rail type in the main toolbar dropdown.
163 StringID build_caption
; ///< Caption of the build vehicle GUI for this rail type.
164 StringID replace_text
; ///< Text used in the autoreplace GUI.
165 StringID new_loco
; ///< Name of an engine for this type of rail in the engine preview GUI.
166 } strings
; ///< Strings associated with the rail type.
168 /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */
169 SpriteID snow_offset
;
171 /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */
172 RailTypes powered_railtypes
;
174 /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */
175 RailTypes compatible_railtypes
;
180 SpriteID bridge_offset
;
183 * Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
185 byte fallback_railtype
;
188 * Multiplier for curve maximum speed advantage
193 * Bit mask of rail type flags
198 * Cost multiplier for building this rail type
200 uint16 cost_multiplier
;
203 * Cost multiplier for maintenance of this rail type
205 uint16 maintenance_multiplier
;
208 * Acceleration type of this rail type
210 uint8 acceleration_type
;
213 * Maximum speed for vehicles travelling on this rail type
218 * Unique 32 bit rail type identifier
223 * Rail type labels this type provides in addition to the main label.
225 RailTypeLabelList alternate_labels
;
234 * When #INVALID_DATE or a vehicle using this railtype gets introduced earlier,
235 * the vehicle's introduction date will be used instead for this railtype.
236 * The introduction at this date is furthermore limited by the
237 * #introduction_required_types.
239 Date introduction_date
;
242 * Bitmask of railtypes that are required for this railtype to be introduced
243 * at a given #introduction_date.
245 RailTypes introduction_required_railtypes
;
248 * Bitmask of which other railtypes are introduced when this railtype is introduced.
250 RailTypes introduces_railtypes
;
253 * The sorting order of this railtype for the toolbar dropdown.
258 * NewGRF providing the Action3 for the railtype. NULL if not available.
260 const GRFFile
*grffile
[RTSG_END
];
263 * Sprite groups for resolving sprites
265 const SpriteGroup
*group
[RTSG_END
];
267 inline bool UsesOverlay() const
269 return this->group
[RTSG_GROUND
] != NULL
;
273 * Offset between the current railtype and normal rail. This means that:<p>
274 * 1) All the sprites in a railset MUST be in the same order. This order
275 * is determined by normal rail. Check sprites 1005 and following for this order<p>
276 * 2) The position where the railtype is loaded must always be the same, otherwise
277 * the offset will fail.
279 inline uint
GetRailtypeSpriteOffset() const
281 return 82 * this->fallback_railtype
;
287 * Returns a pointer to the Railtype information for a given railtype
288 * @param railtype the rail type which the information is requested for
289 * @return The pointer to the RailtypeInfo
291 static inline const RailtypeInfo
*GetRailTypeInfo(RailType railtype
)
293 extern RailtypeInfo _railtypes
[RAILTYPE_END
];
294 assert(railtype
< RAILTYPE_END
);
295 return &_railtypes
[railtype
];
299 * Checks if an engine of the given RailType can drive on a tile with a given
300 * RailType. This would normally just be an equality check, but for electric
301 * rails (which also support non-electric engines).
302 * @return Whether the engine can drive on this tile.
303 * @param enginetype The RailType of the engine we are considering.
304 * @param tiletype The RailType of the tile we are considering.
306 static inline bool IsCompatibleRail(RailType enginetype
, RailType tiletype
)
308 return HasBit(GetRailTypeInfo(enginetype
)->compatible_railtypes
, tiletype
);
312 * Checks if an engine of the given RailType got power on a tile with a given
313 * RailType. This would normally just be an equality check, but for electric
314 * rails (which also support non-electric engines).
315 * @return Whether the engine got power on this tile.
316 * @param enginetype The RailType of the engine we are considering.
317 * @param tiletype The RailType of the tile we are considering.
319 static inline bool HasPowerOnRail(RailType enginetype
, RailType tiletype
)
321 return HasBit(GetRailTypeInfo(enginetype
)->powered_railtypes
, tiletype
);
325 * Test if a RailType disallows build of level crossings.
326 * @param rt The RailType to check.
327 * @return Whether level crossings are not allowed.
329 static inline bool RailNoLevelCrossings(RailType rt
)
331 return HasBit(GetRailTypeInfo(rt
)->flags
, RTF_NO_LEVEL_CROSSING
);
335 * Returns the cost of building the specified railtype.
336 * @param railtype The railtype being built.
337 * @return The cost multiplier.
339 static inline Money
RailBuildCost(RailType railtype
)
341 assert(railtype
< RAILTYPE_END
);
342 return (_price
[PR_BUILD_RAIL
] * GetRailTypeInfo(railtype
)->cost_multiplier
) >> 3;
346 * Returns the 'cost' of clearing the specified railtype.
347 * @param railtype The railtype being removed.
350 static inline Money
RailClearCost(RailType railtype
)
352 /* Clearing rail in fact earns money, but if the build cost is set
353 * very low then a loophole exists where money can be made.
354 * In this case we limit the removal earnings to 3/4s of the build
357 assert(railtype
< RAILTYPE_END
);
358 return max(_price
[PR_CLEAR_RAIL
], -RailBuildCost(railtype
) * 3 / 4);
362 * Calculates the cost of rail conversion
363 * @param from The railtype we are converting from
364 * @param to The railtype we are converting to
365 * @return Cost per TrackBit
367 static inline Money
RailConvertCost(RailType from
, RailType to
)
369 /* Get the costs for removing and building anew
370 * A conversion can never be more costly */
371 Money rebuildcost
= RailBuildCost(to
) + RailClearCost(from
);
373 /* Conversion between somewhat compatible railtypes:
374 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
375 * build costs, if the target type is more expensive (material upgrade costs).
376 * Upgrade can never be more expensive than re-building. */
377 if (HasPowerOnRail(from
, to
) || HasPowerOnRail(to
, from
)) {
378 Money upgradecost
= RailBuildCost(to
) / 8 + max((Money
)0, RailBuildCost(to
) - RailBuildCost(from
));
379 return min(upgradecost
, rebuildcost
);
382 /* make the price the same as remove + build new type for rail types
383 * which are not compatible in any way */
388 * Calculates the maintenance cost of a number of track bits.
389 * @param railtype The railtype to get the cost of.
390 * @param num Number of track bits of this railtype.
391 * @param total_num Total number of track bits of all railtypes.
392 * @return Total cost.
394 static inline Money
RailMaintenanceCost(RailType railtype
, uint32 num
, uint32 total_num
)
396 assert(railtype
< RAILTYPE_END
);
397 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.
401 * Calculates the maintenance cost of a number of signals.
402 * @param num Number of signals.
403 * @return Total cost.
405 static inline Money
SignalMaintenanceCost(uint32 num
)
407 return (_price
[PR_INFRASTRUCTURE_RAIL
] * 15 * num
* (1 + IntSqrt(num
))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
410 void DrawTrainDepotSprite (BlitArea
*dpi
, int x
, int y
, int image
, RailType railtype
);
411 int TicksToLeaveDepot(const Train
*v
);
413 Foundation
GetRailFoundation(Slope tileh
, TrackBits bits
);
416 bool HasRailtypeAvail(const CompanyID company
, const RailType railtype
);
417 bool ValParamRailtype(const RailType rail
);
419 RailTypes
AddDateIntroducedRailTypes(RailTypes current
, Date date
);
421 RailType
GetBestRailtype(const CompanyID company
);
422 RailTypes
GetCompanyRailtypes(const CompanyID c
);
424 RailType
GetRailTypeByLabel(RailTypeLabel label
, bool allow_alternate_labels
= true);
426 void ResetRailTypes();
427 void InitRailTypes();
428 RailType
AllocateRailType(RailTypeLabel label
);
430 extern RailType _sorted_railtypes
[RAILTYPE_END
];
431 extern uint8 _sorted_railtypes_size
;
434 * Loop header for iterating over railtypes, sorted by sortorder.
435 * @param var Railtype.
437 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)