Translations update
[openttd/fttd.git] / src / rail.h
blob97a568e4eb4d18359412fbcf366d3613ddc87eb1
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"
25 /** Railtype flags. */
26 enum RailTypeFlags {
27 RTF_CATENARY = 0, ///< Bit number for drawing a catenary.
28 RTF_NO_LEVEL_CROSSING = 1, ///< Bit number for disallowing level crossings.
30 RTFB_NONE = 0, ///< All flags cleared.
31 RTFB_CATENARY = 1 << RTF_CATENARY, ///< Value for drawing a catenary.
32 RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING, ///< Value for disallowing level crossings.
34 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags)
36 struct SpriteGroup;
38 /** Sprite groups for a railtype. */
39 enum RailTypeSpriteGroup {
40 RTSG_CURSORS, ///< Cursor and toolbar icon images
41 RTSG_OVERLAY, ///< Images for overlaying track
42 RTSG_GROUND, ///< Main group of ground images
43 RTSG_TUNNEL, ///< Main group of ground images for snow or desert
44 RTSG_WIRES, ///< Catenary wires
45 RTSG_PYLONS, ///< Catenary pylons
46 RTSG_BRIDGE, ///< Bridge surface images
47 RTSG_CROSSING, ///< Level crossing overlay images
48 RTSG_DEPOT, ///< Depot images
49 RTSG_FENCES, ///< Fence images
50 RTSG_TUNNEL_PORTAL, ///< Tunnel portal overlay
51 RTSG_SIGNALS, ///< Signal images
52 RTSG_END,
55 /**
56 * Offsets for sprites within an overlay/underlay set.
57 * These are the same for overlay and underlay sprites.
59 enum RailTrackOffset {
60 RTO_X, ///< Piece of rail in X direction
61 RTO_Y, ///< Piece of rail in Y direction
62 RTO_N, ///< Piece of rail in northern corner
63 RTO_S, ///< Piece of rail in southern corner
64 RTO_E, ///< Piece of rail in eastern corner
65 RTO_W, ///< Piece of rail in western corner
66 RTO_SLOPE_NE, ///< Piece of rail on slope with north-east raised
67 RTO_SLOPE_SE, ///< Piece of rail on slope with south-east raised
68 RTO_SLOPE_SW, ///< Piece of rail on slope with south-west raised
69 RTO_SLOPE_NW, ///< Piece of rail on slope with north-west raised
70 RTO_CROSSING_XY, ///< Crossing of X and Y rail, with ballast
71 RTO_JUNCTION_SW, ///< Ballast for junction 'pointing' SW
72 RTO_JUNCTION_NE, ///< Ballast for junction 'pointing' NE
73 RTO_JUNCTION_SE, ///< Ballast for junction 'pointing' SE
74 RTO_JUNCTION_NW, ///< Ballast for junction 'pointing' NW
75 RTO_JUNCTION_NSEW,///< Ballast for full junction
78 /**
79 * Offsets for sprites within a bridge surface overlay set.
81 enum RailTrackBridgeOffset {
82 RTBO_X, ///< Piece of rail in X direction
83 RTBO_Y, ///< Piece of rail in Y direction
84 RTBO_SLOPE, ///< Sloped rail pieces, in order NE, SE, SW, NW
87 /**
88 * Offsets from base sprite for fence sprites. These are in the order of
89 * the sprites in the original data files.
91 enum RailFenceOffset {
92 RFO_FLAT_X,
93 RFO_FLAT_Y,
94 RFO_FLAT_VERT,
95 RFO_FLAT_HORZ,
96 RFO_SLOPE_SW,
97 RFO_SLOPE_SE,
98 RFO_SLOPE_NE,
99 RFO_SLOPE_NW,
102 /** List of rail type labels. */
103 typedef SmallVector<RailTypeLabel, 4> RailTypeLabelList;
106 * This struct contains all the info that is needed to draw and construct tracks.
108 struct RailtypeInfo {
110 * Struct containing the main sprites. @note not all sprites are listed, but only
111 * the ones used directly in the code
113 struct {
114 SpriteID track_y; ///< single piece of rail in Y direction, with ground
115 SpriteID track_ns; ///< two pieces of rail in North and South corner (East-West direction)
116 SpriteID ground; ///< ground sprite for a 3-way switch
117 SpriteID single_x; ///< single piece of rail in X direction, without ground
118 SpriteID single_y; ///< single piece of rail in Y direction, without ground
119 SpriteID single_n; ///< single piece of rail in the northern corner
120 SpriteID single_s; ///< single piece of rail in the southern corner
121 SpriteID single_e; ///< single piece of rail in the eastern corner
122 SpriteID single_w; ///< single piece of rail in the western corner
123 SpriteID single_sloped;///< single piece of rail for slopes
124 SpriteID crossing; ///< level crossing, rail in X direction
125 SpriteID tunnel; ///< tunnel sprites base
126 } base_sprites;
129 * struct containing the sprites for the rail GUI. @note only sprites referred to
130 * directly in the code are listed
132 struct {
133 SpriteID build_ns_rail; ///< button for building single rail in N-S direction
134 SpriteID build_x_rail; ///< button for building single rail in X direction
135 SpriteID build_ew_rail; ///< button for building single rail in E-W direction
136 SpriteID build_y_rail; ///< button for building single rail in Y direction
137 SpriteID auto_rail; ///< button for the autorail construction
138 SpriteID build_depot; ///< button for building depots
139 SpriteID build_tunnel; ///< button for building a tunnel
140 SpriteID convert_rail; ///< button for converting rail
141 SpriteID signals[SIGTYPE_END][2][2]; ///< signal GUI sprites (type, variant, state)
142 } gui_sprites;
144 struct {
145 CursorID rail_ns; ///< Cursor for building rail in N-S direction
146 CursorID rail_swne; ///< Cursor for building rail in X direction
147 CursorID rail_ew; ///< Cursor for building rail in E-W direction
148 CursorID rail_nwse; ///< Cursor for building rail in Y direction
149 CursorID autorail; ///< Cursor for autorail tool
150 CursorID depot; ///< Cursor for building a depot
151 CursorID tunnel; ///< Cursor for building a tunnel
152 CursorID convert; ///< Cursor for converting track
153 } cursor; ///< Cursors associated with the rail type.
155 struct {
156 StringID name; ///< Name of this rail type.
157 StringID toolbar_caption; ///< Caption in the construction toolbar GUI for this rail type.
158 StringID menu_text; ///< Name of this rail type in the main toolbar dropdown.
159 StringID build_caption; ///< Caption of the build vehicle GUI for this rail type.
160 StringID replace_text; ///< Text used in the autoreplace GUI.
161 StringID new_loco; ///< Name of an engine for this type of rail in the engine preview GUI.
162 } strings; ///< Strings associated with the rail type.
164 /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */
165 SpriteID snow_offset;
167 /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */
168 RailTypes powered_railtypes;
170 /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */
171 RailTypes compatible_railtypes;
174 * Bridge offset
176 SpriteID bridge_offset;
179 * Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
181 byte fallback_railtype;
184 * Multiplier for curve maximum speed advantage
186 byte curve_speed;
189 * Bit mask of rail type flags
191 RailTypeFlags flags;
194 * Cost multiplier for building this rail type
196 uint16 cost_multiplier;
199 * Cost multiplier for maintenance of this rail type
201 uint16 maintenance_multiplier;
204 * Acceleration type of this rail type
206 uint8 acceleration_type;
209 * Maximum speed for vehicles travelling on this rail type
211 uint16 max_speed;
214 * Unique 32 bit rail type identifier
216 RailTypeLabel label;
219 * Rail type labels this type provides in addition to the main label.
221 RailTypeLabelList alternate_labels;
224 * Colour on mini-map
226 byte map_colour;
229 * Introduction date.
230 * When #INVALID_DATE or a vehicle using this railtype gets introduced earlier,
231 * the vehicle's introduction date will be used instead for this railtype.
232 * The introduction at this date is furthermore limited by the
233 * #introduction_required_types.
235 Date introduction_date;
238 * Bitmask of railtypes that are required for this railtype to be introduced
239 * at a given #introduction_date.
241 RailTypes introduction_required_railtypes;
244 * Bitmask of which other railtypes are introduced when this railtype is introduced.
246 RailTypes introduces_railtypes;
249 * The sorting order of this railtype for the toolbar dropdown.
251 byte sorting_order;
254 * NewGRF providing the Action3 for the railtype. NULL if not available.
256 const GRFFile *grffile[RTSG_END];
259 * Sprite groups for resolving sprites
261 const SpriteGroup *group[RTSG_END];
263 inline bool UsesOverlay() const
265 return this->group[RTSG_GROUND] != NULL;
269 * Offset between the current railtype and normal rail. This means that:<p>
270 * 1) All the sprites in a railset MUST be in the same order. This order
271 * is determined by normal rail. Check sprites 1005 and following for this order<p>
272 * 2) The position where the railtype is loaded must always be the same, otherwise
273 * the offset will fail.
275 inline uint GetRailtypeSpriteOffset() const
277 return 82 * this->fallback_railtype;
283 * Returns a pointer to the Railtype information for a given railtype
284 * @param railtype the rail type which the information is requested for
285 * @return The pointer to the RailtypeInfo
287 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
289 extern RailtypeInfo _railtypes[RAILTYPE_END];
290 assert(railtype < RAILTYPE_END);
291 return &_railtypes[railtype];
295 * Checks if an engine of the given RailType can drive on a tile with a given
296 * RailType. This would normally just be an equality check, but for electric
297 * rails (which also support non-electric engines).
298 * @return Whether the engine can drive on this tile.
299 * @param enginetype The RailType of the engine we are considering.
300 * @param tiletype The RailType of the tile we are considering.
302 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
304 return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
308 * Checks if an engine of the given RailType got power on a tile with a given
309 * RailType. This would normally just be an equality check, but for electric
310 * rails (which also support non-electric engines).
311 * @return Whether the engine got power on this tile.
312 * @param enginetype The RailType of the engine we are considering.
313 * @param tiletype The RailType of the tile we are considering.
315 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
317 return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
321 * Test if a RailType disallows build of level crossings.
322 * @param rt The RailType to check.
323 * @return Whether level crossings are not allowed.
325 static inline bool RailNoLevelCrossings(RailType rt)
327 return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
331 * Returns the cost of building the specified railtype.
332 * @param railtype The railtype being built.
333 * @return The cost multiplier.
335 static inline Money RailBuildCost(RailType railtype)
337 assert(railtype < RAILTYPE_END);
338 return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
342 * Returns the 'cost' of clearing the specified railtype.
343 * @param railtype The railtype being removed.
344 * @return The cost.
346 static inline Money RailClearCost(RailType railtype)
348 /* Clearing rail in fact earns money, but if the build cost is set
349 * very low then a loophole exists where money can be made.
350 * In this case we limit the removal earnings to 3/4s of the build
351 * cost.
353 assert(railtype < RAILTYPE_END);
354 return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
358 * Calculates the cost of rail conversion
359 * @param from The railtype we are converting from
360 * @param to The railtype we are converting to
361 * @return Cost per TrackBit
363 static inline Money RailConvertCost(RailType from, RailType to)
365 /* Get the costs for removing and building anew
366 * A conversion can never be more costly */
367 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
369 /* Conversion between somewhat compatible railtypes:
370 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
371 * build costs, if the target type is more expensive (material upgrade costs).
372 * Upgrade can never be more expensive than re-building. */
373 if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
374 Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
375 return min(upgradecost, rebuildcost);
378 /* make the price the same as remove + build new type for rail types
379 * which are not compatible in any way */
380 return rebuildcost;
384 * Calculates the maintenance cost of a number of track bits.
385 * @param railtype The railtype to get the cost of.
386 * @param num Number of track bits of this railtype.
387 * @param total_num Total number of track bits of all railtypes.
388 * @return Total cost.
390 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
392 assert(railtype < RAILTYPE_END);
393 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.
397 * Calculates the maintenance cost of a number of signals.
398 * @param num Number of signals.
399 * @return Total cost.
401 static inline Money SignalMaintenanceCost(uint32 num)
403 return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
406 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
407 int TicksToLeaveDepot(const Train *v);
409 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
412 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
413 bool ValParamRailtype(const RailType rail);
415 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
417 RailType GetBestRailtype(const CompanyID company);
418 RailTypes GetCompanyRailtypes(const CompanyID c);
420 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
422 void ResetRailTypes();
423 void InitRailTypes();
424 RailType AllocateRailType(RailTypeLabel label);
426 #endif /* RAIL_H */