Translations update
[openttd/fttd.git] / src / rail.h
blobfafce14f98d784f4d9e35f0cb272e06299698d5f
1 /* $Id$ */
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 */
10 /** @file rail.h Rail specific functions. */
12 #ifndef RAIL_H
13 #define RAIL_H
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.h"
24 #include "gfx_func.h"
26 /** Railtype flags. */
27 enum RailTypeFlags {
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)
37 struct SpriteGroup;
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
53 RTSG_END,
56 /**
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 /**
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
88 /**
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 class RailtypeInfo {
118 public:
120 * Struct containing the main sprites. @note not all sprites are listed, but only
121 * the ones used directly in the code
123 struct {
124 SpriteID track_y; ///< single piece of rail in Y direction, with ground
125 SpriteID track_ns; ///< two pieces of rail in North and South corner (East-West direction)
126 SpriteID ground; ///< ground sprite for a 3-way switch
127 SpriteID single[TRACK_END]; ///< single piece of rail per track, without ground
128 SpriteID single_sloped;///< single piece of rail for slopes
129 SpriteID crossing; ///< level crossing, rail in X direction
130 SpriteID tunnel; ///< tunnel sprites base
131 } base_sprites;
134 * struct containing the sprites for the rail GUI. @note only sprites referred to
135 * directly in the code are listed
137 struct {
138 SpriteID build_ns_rail; ///< button for building single rail in N-S direction
139 SpriteID build_x_rail; ///< button for building single rail in X direction
140 SpriteID build_ew_rail; ///< button for building single rail in E-W direction
141 SpriteID build_y_rail; ///< button for building single rail in Y direction
142 SpriteID auto_rail; ///< button for the autorail construction
143 SpriteID build_depot; ///< button for building depots
144 SpriteID build_tunnel; ///< button for building a tunnel
145 SpriteID convert_rail; ///< button for converting rail
146 SpriteID signals[SIGTYPE_END][2][2]; ///< signal GUI sprites (type, variant, state)
147 } gui_sprites;
149 struct {
150 CursorID rail_ns; ///< Cursor for building rail in N-S direction
151 CursorID rail_swne; ///< Cursor for building rail in X direction
152 CursorID rail_ew; ///< Cursor for building rail in E-W direction
153 CursorID rail_nwse; ///< Cursor for building rail in Y direction
154 CursorID autorail; ///< Cursor for autorail tool
155 CursorID depot; ///< Cursor for building a depot
156 CursorID tunnel; ///< Cursor for building a tunnel
157 CursorID convert; ///< Cursor for converting track
158 } cursor; ///< Cursors associated with the rail type.
160 struct {
161 StringID name; ///< Name of this rail type.
162 StringID toolbar_caption; ///< Caption in the construction toolbar GUI for this rail type.
163 StringID menu_text; ///< Name of this rail type in the main toolbar dropdown.
164 StringID build_caption; ///< Caption of the build vehicle GUI for this rail type.
165 StringID replace_text; ///< Text used in the autoreplace GUI.
166 StringID new_loco; ///< Name of an engine for this type of rail in the engine preview GUI.
167 } strings; ///< Strings associated with the rail type.
169 /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */
170 SpriteID snow_offset;
172 /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */
173 RailTypes powered_railtypes;
175 /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */
176 RailTypes compatible_railtypes;
179 * Bridge offset
181 SpriteID bridge_offset;
184 * Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
186 byte fallback_railtype;
189 * Multiplier for curve maximum speed advantage
191 byte curve_speed;
194 * Bit mask of rail type flags
196 RailTypeFlags flags;
199 * Cost multiplier for building this rail type
201 uint16 cost_multiplier;
204 * Cost multiplier for maintenance of this rail type
206 uint16 maintenance_multiplier;
209 * Acceleration type of this rail type
211 uint8 acceleration_type;
214 * Maximum speed for vehicles travelling on this rail type
216 uint16 max_speed;
219 * Unique 32 bit rail type identifier
221 RailTypeLabel label;
224 * Rail type labels this type provides in addition to the main label.
226 RailTypeLabelList alternate_labels;
229 * Colour on mini-map
231 byte map_colour;
234 * Introduction date.
235 * When #INVALID_DATE or a vehicle using this railtype gets introduced earlier,
236 * the vehicle's introduction date will be used instead for this railtype.
237 * The introduction at this date is furthermore limited by the
238 * #introduction_required_types.
240 Date introduction_date;
243 * Bitmask of railtypes that are required for this railtype to be introduced
244 * at a given #introduction_date.
246 RailTypes introduction_required_railtypes;
249 * Bitmask of which other railtypes are introduced when this railtype is introduced.
251 RailTypes introduces_railtypes;
254 * The sorting order of this railtype for the toolbar dropdown.
256 byte sorting_order;
259 * NewGRF providing the Action3 for the railtype. NULL if not available.
261 const GRFFile *grffile[RTSG_END];
264 * Sprite groups for resolving sprites
266 const SpriteGroup *group[RTSG_END];
268 inline bool UsesOverlay() const
270 return this->group[RTSG_GROUND] != NULL;
274 * Offset between the current railtype and normal rail. This means that:<p>
275 * 1) All the sprites in a railset MUST be in the same order. This order
276 * is determined by normal rail. Check sprites 1005 and following for this order<p>
277 * 2) The position where the railtype is loaded must always be the same, otherwise
278 * the offset will fail.
280 inline uint GetRailtypeSpriteOffset() const
282 return 82 * this->fallback_railtype;
288 * Returns a pointer to the Railtype information for a given railtype
289 * @param railtype the rail type which the information is requested for
290 * @return The pointer to the RailtypeInfo
292 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
294 extern RailtypeInfo _railtypes[RAILTYPE_END];
295 assert(railtype < RAILTYPE_END);
296 return &_railtypes[railtype];
300 * Checks if an engine of the given RailType can drive on a tile with a given
301 * RailType. This would normally just be an equality check, but for electric
302 * rails (which also support non-electric engines).
303 * @return Whether the engine can drive on this tile.
304 * @param enginetype The RailType of the engine we are considering.
305 * @param tiletype The RailType of the tile we are considering.
307 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
309 return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
313 * Checks if an engine of the given RailType got power on a tile with a given
314 * RailType. This would normally just be an equality check, but for electric
315 * rails (which also support non-electric engines).
316 * @return Whether the engine got power on this tile.
317 * @param enginetype The RailType of the engine we are considering.
318 * @param tiletype The RailType of the tile we are considering.
320 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
322 return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
326 * Test if a RailType disallows build of level crossings.
327 * @param rt The RailType to check.
328 * @return Whether level crossings are not allowed.
330 static inline bool RailNoLevelCrossings(RailType rt)
332 return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
336 * Returns the cost of building the specified railtype.
337 * @param railtype The railtype being built.
338 * @return The cost multiplier.
340 static inline Money RailBuildCost(RailType railtype)
342 assert(railtype < RAILTYPE_END);
343 return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
347 * Returns the 'cost' of clearing the specified railtype.
348 * @param railtype The railtype being removed.
349 * @return The cost.
351 static inline Money RailClearCost(RailType railtype)
353 /* Clearing rail in fact earns money, but if the build cost is set
354 * very low then a loophole exists where money can be made.
355 * In this case we limit the removal earnings to 3/4s of the build
356 * cost.
358 assert(railtype < RAILTYPE_END);
359 return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
363 * Calculates the cost of rail conversion
364 * @param from The railtype we are converting from
365 * @param to The railtype we are converting to
366 * @return Cost per TrackBit
368 static inline Money RailConvertCost(RailType from, RailType to)
370 /* Get the costs for removing and building anew
371 * A conversion can never be more costly */
372 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
374 /* Conversion between somewhat compatible railtypes:
375 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
376 * build costs, if the target type is more expensive (material upgrade costs).
377 * Upgrade can never be more expensive than re-building. */
378 if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
379 Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
380 return min(upgradecost, rebuildcost);
383 /* make the price the same as remove + build new type for rail types
384 * which are not compatible in any way */
385 return rebuildcost;
389 * Calculates the maintenance cost of a number of track bits.
390 * @param railtype The railtype to get the cost of.
391 * @param num Number of track bits of this railtype.
392 * @param total_num Total number of track bits of all railtypes.
393 * @return Total cost.
395 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
397 assert(railtype < RAILTYPE_END);
398 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.
402 * Calculates the maintenance cost of a number of signals.
403 * @param num Number of signals.
404 * @return Total cost.
406 static inline Money SignalMaintenanceCost(uint32 num)
408 return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
411 void DrawTrainDepotSprite (BlitArea *dpi, int x, int y, int image, RailType railtype);
412 int TicksToLeaveDepot(const Train *v);
414 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
417 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
418 bool ValParamRailtype(const RailType rail);
420 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
422 RailType GetBestRailtype(const CompanyID company);
423 RailTypes GetCompanyRailtypes(const CompanyID c);
425 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
427 void ResetRailTypes();
428 void InitRailTypes();
429 RailType AllocateRailType(RailTypeLabel label);
431 extern RailType _sorted_railtypes[RAILTYPE_END];
432 extern uint8 _sorted_railtypes_size;
435 * Loop header for iterating over railtypes, sorted by sortorder.
436 * @param var Railtype.
438 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)
440 #endif /* RAIL_H */