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
79 /** List of rail type labels. */
80 typedef SmallVector
<RailTypeLabel
, 4> RailTypeLabelList
;
83 * This struct contains all the info that is needed to draw and construct tracks.
88 * Struct containing the main sprites. @note not all sprites are listed, but only
89 * the ones used directly in the code
92 SpriteID track_y
; ///< single piece of rail in Y direction, with ground
93 SpriteID track_ns
; ///< two pieces of rail in North and South corner (East-West direction)
94 SpriteID ground
; ///< ground sprite for a 3-way switch
95 SpriteID single
[TRACK_END
]; ///< single piece of rail per track, without ground
96 SpriteID single_sloped
;///< single piece of rail for slopes
97 SpriteID crossing
; ///< level crossing, rail in X direction
98 SpriteID tunnel
; ///< tunnel sprites base
102 * struct containing the sprites for the rail GUI. @note only sprites referred to
103 * directly in the code are listed
106 SpriteID build_ns_rail
; ///< button for building single rail in N-S direction
107 SpriteID build_x_rail
; ///< button for building single rail in X direction
108 SpriteID build_ew_rail
; ///< button for building single rail in E-W direction
109 SpriteID build_y_rail
; ///< button for building single rail in Y direction
110 SpriteID auto_rail
; ///< button for the autorail construction
111 SpriteID build_depot
; ///< button for building depots
112 SpriteID build_tunnel
; ///< button for building a tunnel
113 SpriteID convert_rail
; ///< button for converting rail
114 SpriteID signals
[SIGTYPE_END
][2][2]; ///< signal GUI sprites (type, variant, state)
118 CursorID rail_ns
; ///< Cursor for building rail in N-S direction
119 CursorID rail_swne
; ///< Cursor for building rail in X direction
120 CursorID rail_ew
; ///< Cursor for building rail in E-W direction
121 CursorID rail_nwse
; ///< Cursor for building rail in Y direction
122 CursorID autorail
; ///< Cursor for autorail tool
123 CursorID depot
; ///< Cursor for building a depot
124 CursorID tunnel
; ///< Cursor for building a tunnel
125 CursorID convert
; ///< Cursor for converting track
126 } cursor
; ///< Cursors associated with the rail type.
129 StringID name
; ///< Name of this rail type.
130 StringID toolbar_caption
; ///< Caption in the construction toolbar GUI for this rail type.
131 StringID menu_text
; ///< Name of this rail type in the main toolbar dropdown.
132 StringID build_caption
; ///< Caption of the build vehicle GUI for this rail type.
133 StringID replace_text
; ///< Text used in the autoreplace GUI.
134 StringID new_loco
; ///< Name of an engine for this type of rail in the engine preview GUI.
135 } strings
; ///< Strings associated with the rail type.
137 /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */
138 SpriteID snow_offset
;
140 /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */
141 RailTypes powered_railtypes
;
143 /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */
144 RailTypes compatible_railtypes
;
149 SpriteID bridge_offset
;
152 * Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
154 byte fallback_railtype
;
157 * Multiplier for curve maximum speed advantage
162 * Bit mask of rail type flags
167 * Cost multiplier for building this rail type
169 uint16 cost_multiplier
;
172 * Cost multiplier for maintenance of this rail type
174 uint16 maintenance_multiplier
;
177 * Acceleration type of this rail type
179 uint8 acceleration_type
;
182 * Maximum speed for vehicles travelling on this rail type
187 * Unique 32 bit rail type identifier
192 * Rail type labels this type provides in addition to the main label.
194 RailTypeLabelList alternate_labels
;
203 * When #INVALID_DATE or a vehicle using this railtype gets introduced earlier,
204 * the vehicle's introduction date will be used instead for this railtype.
205 * The introduction at this date is furthermore limited by the
206 * #introduction_required_types.
208 Date introduction_date
;
211 * Bitmask of railtypes that are required for this railtype to be introduced
212 * at a given #introduction_date.
214 RailTypes introduction_required_railtypes
;
217 * Bitmask of which other railtypes are introduced when this railtype is introduced.
219 RailTypes introduces_railtypes
;
222 * The sorting order of this railtype for the toolbar dropdown.
227 * NewGRF providing the Action3 for the railtype. NULL if not available.
229 const GRFFile
*grffile
[RTSG_END
];
232 * Sprite groups for resolving sprites
234 const SpriteGroup
*group
[RTSG_END
];
236 inline bool UsesOverlay() const
238 return this->group
[RTSG_GROUND
] != NULL
;
242 * Offset between the current railtype and normal rail. This means that:<p>
243 * 1) All the sprites in a railset MUST be in the same order. This order
244 * is determined by normal rail. Check sprites 1005 and following for this order<p>
245 * 2) The position where the railtype is loaded must always be the same, otherwise
246 * the offset will fail.
248 inline uint
GetRailtypeSpriteOffset() const
250 return 82 * this->fallback_railtype
;
256 * Returns a pointer to the Railtype information for a given railtype
257 * @param railtype the rail type which the information is requested for
258 * @return The pointer to the RailtypeInfo
260 static inline const RailtypeInfo
*GetRailTypeInfo(RailType railtype
)
262 extern RailtypeInfo _railtypes
[RAILTYPE_END
];
263 assert(railtype
< RAILTYPE_END
);
264 return &_railtypes
[railtype
];
268 * Checks if an engine of the given RailType can drive on a tile with a given
269 * RailType. This would normally just be an equality check, but for electric
270 * rails (which also support non-electric engines).
271 * @return Whether the engine can drive on this tile.
272 * @param enginetype The RailType of the engine we are considering.
273 * @param tiletype The RailType of the tile we are considering.
275 static inline bool IsCompatibleRail(RailType enginetype
, RailType tiletype
)
277 return HasBit(GetRailTypeInfo(enginetype
)->compatible_railtypes
, tiletype
);
281 * Checks if an engine of the given RailType got power on a tile with a given
282 * RailType. This would normally just be an equality check, but for electric
283 * rails (which also support non-electric engines).
284 * @return Whether the engine got power on this tile.
285 * @param enginetype The RailType of the engine we are considering.
286 * @param tiletype The RailType of the tile we are considering.
288 static inline bool HasPowerOnRail(RailType enginetype
, RailType tiletype
)
290 return HasBit(GetRailTypeInfo(enginetype
)->powered_railtypes
, tiletype
);
294 * Test if a RailType disallows build of level crossings.
295 * @param rt The RailType to check.
296 * @return Whether level crossings are not allowed.
298 static inline bool RailNoLevelCrossings(RailType rt
)
300 return HasBit(GetRailTypeInfo(rt
)->flags
, RTF_NO_LEVEL_CROSSING
);
304 * Returns the cost of building the specified railtype.
305 * @param railtype The railtype being built.
306 * @return The cost multiplier.
308 static inline Money
RailBuildCost(RailType railtype
)
310 assert(railtype
< RAILTYPE_END
);
311 return (_price
[PR_BUILD_RAIL
] * GetRailTypeInfo(railtype
)->cost_multiplier
) >> 3;
315 * Returns the 'cost' of clearing the specified railtype.
316 * @param railtype The railtype being removed.
319 static inline Money
RailClearCost(RailType railtype
)
321 /* Clearing rail in fact earns money, but if the build cost is set
322 * very low then a loophole exists where money can be made.
323 * In this case we limit the removal earnings to 3/4s of the build
326 assert(railtype
< RAILTYPE_END
);
327 return max(_price
[PR_CLEAR_RAIL
], -RailBuildCost(railtype
) * 3 / 4);
331 * Calculates the cost of rail conversion
332 * @param from The railtype we are converting from
333 * @param to The railtype we are converting to
334 * @return Cost per TrackBit
336 static inline Money
RailConvertCost(RailType from
, RailType to
)
338 /* Get the costs for removing and building anew
339 * A conversion can never be more costly */
340 Money rebuildcost
= RailBuildCost(to
) + RailClearCost(from
);
342 /* Conversion between somewhat compatible railtypes:
343 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
344 * build costs, if the target type is more expensive (material upgrade costs).
345 * Upgrade can never be more expensive than re-building. */
346 if (HasPowerOnRail(from
, to
) || HasPowerOnRail(to
, from
)) {
347 Money upgradecost
= RailBuildCost(to
) / 8 + max((Money
)0, RailBuildCost(to
) - RailBuildCost(from
));
348 return min(upgradecost
, rebuildcost
);
351 /* make the price the same as remove + build new type for rail types
352 * which are not compatible in any way */
357 * Calculates the maintenance cost of a number of track bits.
358 * @param railtype The railtype to get the cost of.
359 * @param num Number of track bits of this railtype.
360 * @param total_num Total number of track bits of all railtypes.
361 * @return Total cost.
363 static inline Money
RailMaintenanceCost(RailType railtype
, uint32 num
, uint32 total_num
)
365 assert(railtype
< RAILTYPE_END
);
366 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.
370 * Calculates the maintenance cost of a number of signals.
371 * @param num Number of signals.
372 * @return Total cost.
374 static inline Money
SignalMaintenanceCost(uint32 num
)
376 return (_price
[PR_INFRASTRUCTURE_RAIL
] * 15 * num
* (1 + IntSqrt(num
))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
379 void DrawTrainDepotSprite (BlitArea
*dpi
, int x
, int y
, int image
, RailType railtype
);
380 int TicksToLeaveDepot(const Train
*v
);
382 Foundation
GetRailFoundation(Slope tileh
, TrackBits bits
);
385 bool HasRailtypeAvail(const CompanyID company
, const RailType railtype
);
386 bool ValParamRailtype(const RailType rail
);
388 RailTypes
AddDateIntroducedRailTypes(RailTypes current
, Date date
);
390 RailType
GetBestRailtype(const CompanyID company
);
391 RailTypes
GetCompanyRailtypes(const CompanyID c
);
393 RailType
GetRailTypeByLabel(RailTypeLabel label
, bool allow_alternate_labels
= true);
395 void ResetRailTypes();
396 void InitRailTypes();
397 RailType
AllocateRailType(RailTypeLabel label
);
399 extern RailType _sorted_railtypes
[RAILTYPE_END
];
400 extern uint8 _sorted_railtypes_size
;
403 * Loop header for iterating over railtypes, sorted by sortorder.
404 * @param var Railtype.
406 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)