(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / rail.h
blob1bc403d7fdc25840a7a53ae6a27e63ea3732583f
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_type.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_NW, //!< Slope FLAT, Track X, Fence NW
93 RFO_FLAT_Y_NE, //!< Slope FLAT, Track Y, Fence NE
94 RFO_FLAT_LEFT, //!< Slope FLAT, Track LEFT, Fence E
95 RFO_FLAT_UPPER, //!< Slope FLAT, Track UPPER, Fence S
96 RFO_SLOPE_SW_NW, //!< Slope SW, Track X, Fence NW
97 RFO_SLOPE_SE_NE, //!< Slope SE, Track Y, Fence NE
98 RFO_SLOPE_NE_NW, //!< Slope NE, Track X, Fence NW
99 RFO_SLOPE_NW_NE, //!< Slope NW, Track Y, Fence NE
100 RFO_FLAT_X_SE, //!< Slope FLAT, Track X, Fence SE
101 RFO_FLAT_Y_SW, //!< Slope FLAT, Track Y, Fence SW
102 RFO_FLAT_RIGHT, //!< Slope FLAT, Track RIGHT, Fence W
103 RFO_FLAT_LOWER, //!< Slope FLAT, Track LOWER, Fence N
104 RFO_SLOPE_SW_SE, //!< Slope SW, Track X, Fence SE
105 RFO_SLOPE_SE_SW, //!< Slope SE, Track Y, Fence SW
106 RFO_SLOPE_NE_SE, //!< Slope NE, Track X, Fence SE
107 RFO_SLOPE_NW_SW, //!< Slope NW, Track Y, Fence SW
110 /** List of rail type labels. */
111 typedef SmallVector<RailTypeLabel, 4> RailTypeLabelList;
114 * This struct contains all the info that is needed to draw and construct tracks.
116 class RailtypeInfo {
117 public:
119 * Struct containing the main sprites. @note not all sprites are listed, but only
120 * the ones used directly in the code
122 struct {
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_x; ///< single piece of rail in X direction, without ground
127 SpriteID single_y; ///< single piece of rail in Y direction, without ground
128 SpriteID single_n; ///< single piece of rail in the northern corner
129 SpriteID single_s; ///< single piece of rail in the southern corner
130 SpriteID single_e; ///< single piece of rail in the eastern corner
131 SpriteID single_w; ///< single piece of rail in the western corner
132 SpriteID single_sloped;///< single piece of rail for slopes
133 SpriteID crossing; ///< level crossing, rail in X direction
134 SpriteID tunnel; ///< tunnel sprites base
135 } base_sprites;
138 * struct containing the sprites for the rail GUI. @note only sprites referred to
139 * directly in the code are listed
141 struct {
142 SpriteID build_ns_rail; ///< button for building single rail in N-S direction
143 SpriteID build_x_rail; ///< button for building single rail in X direction
144 SpriteID build_ew_rail; ///< button for building single rail in E-W direction
145 SpriteID build_y_rail; ///< button for building single rail in Y direction
146 SpriteID auto_rail; ///< button for the autorail construction
147 SpriteID build_depot; ///< button for building depots
148 SpriteID build_tunnel; ///< button for building a tunnel
149 SpriteID convert_rail; ///< button for converting rail
150 SpriteID signals[SIGTYPE_END][2][2]; ///< signal GUI sprites (type, variant, state)
151 } gui_sprites;
153 struct {
154 CursorID rail_ns; ///< Cursor for building rail in N-S direction
155 CursorID rail_swne; ///< Cursor for building rail in X direction
156 CursorID rail_ew; ///< Cursor for building rail in E-W direction
157 CursorID rail_nwse; ///< Cursor for building rail in Y direction
158 CursorID autorail; ///< Cursor for autorail tool
159 CursorID depot; ///< Cursor for building a depot
160 CursorID tunnel; ///< Cursor for building a tunnel
161 CursorID convert; ///< Cursor for converting track
162 } cursor; ///< Cursors associated with the rail type.
164 struct {
165 StringID name; ///< Name of this rail type.
166 StringID toolbar_caption; ///< Caption in the construction toolbar GUI for this rail type.
167 StringID menu_text; ///< Name of this rail type in the main toolbar dropdown.
168 StringID build_caption; ///< Caption of the build vehicle GUI for this rail type.
169 StringID replace_text; ///< Text used in the autoreplace GUI.
170 StringID new_loco; ///< Name of an engine for this type of rail in the engine preview GUI.
171 } strings; ///< Strings associated with the rail type.
173 /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */
174 SpriteID snow_offset;
176 /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */
177 RailTypes powered_railtypes;
179 /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */
180 RailTypes compatible_railtypes;
183 * Bridge offset
185 SpriteID bridge_offset;
188 * Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
190 byte fallback_railtype;
193 * Multiplier for curve maximum speed advantage
195 byte curve_speed;
198 * Bit mask of rail type flags
200 RailTypeFlags flags;
203 * Cost multiplier for building this rail type
205 uint16 cost_multiplier;
208 * Cost multiplier for maintenance of this rail type
210 uint16 maintenance_multiplier;
213 * Acceleration type of this rail type
215 uint8 acceleration_type;
218 * Maximum speed for vehicles travelling on this rail type
220 uint16 max_speed;
223 * Unique 32 bit rail type identifier
225 RailTypeLabel label;
228 * Rail type labels this type provides in addition to the main label.
230 RailTypeLabelList alternate_labels;
233 * Colour on mini-map
235 byte map_colour;
238 * Introduction date.
239 * When #INVALID_DATE or a vehicle using this railtype gets introduced earlier,
240 * the vehicle's introduction date will be used instead for this railtype.
241 * The introduction at this date is furthermore limited by the
242 * #introduction_required_types.
244 Date introduction_date;
247 * Bitmask of railtypes that are required for this railtype to be introduced
248 * at a given #introduction_date.
250 RailTypes introduction_required_railtypes;
253 * Bitmask of which other railtypes are introduced when this railtype is introduced.
255 RailTypes introduces_railtypes;
258 * The sorting order of this railtype for the toolbar dropdown.
260 byte sorting_order;
263 * NewGRF providing the Action3 for the railtype. NULL if not available.
265 const GRFFile *grffile[RTSG_END];
268 * Sprite groups for resolving sprites
270 const SpriteGroup *group[RTSG_END];
272 inline bool UsesOverlay() const
274 return this->group[RTSG_GROUND] != NULL;
278 * Offset between the current railtype and normal rail. This means that:<p>
279 * 1) All the sprites in a railset MUST be in the same order. This order
280 * is determined by normal rail. Check sprites 1005 and following for this order<p>
281 * 2) The position where the railtype is loaded must always be the same, otherwise
282 * the offset will fail.
284 inline uint GetRailtypeSpriteOffset() const
286 return 82 * this->fallback_railtype;
292 * Returns a pointer to the Railtype information for a given railtype
293 * @param railtype the rail type which the information is requested for
294 * @return The pointer to the RailtypeInfo
296 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
298 extern RailtypeInfo _railtypes[RAILTYPE_END];
299 assert(railtype < RAILTYPE_END);
300 return &_railtypes[railtype];
304 * Checks if an engine of the given RailType can drive on a tile with a given
305 * RailType. This would normally just be an equality check, but for electric
306 * rails (which also support non-electric engines).
307 * @return Whether the engine can drive on this tile.
308 * @param enginetype The RailType of the engine we are considering.
309 * @param tiletype The RailType of the tile we are considering.
311 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
313 return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
317 * Checks if an engine of the given RailType got power on a tile with a given
318 * RailType. This would normally just be an equality check, but for electric
319 * rails (which also support non-electric engines).
320 * @return Whether the engine got power on this tile.
321 * @param enginetype The RailType of the engine we are considering.
322 * @param tiletype The RailType of the tile we are considering.
324 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
326 return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
330 * Test if a RailType disallows build of level crossings.
331 * @param rt The RailType to check.
332 * @return Whether level crossings are not allowed.
334 static inline bool RailNoLevelCrossings(RailType rt)
336 return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
340 * Returns the cost of building the specified railtype.
341 * @param railtype The railtype being built.
342 * @return The cost multiplier.
344 static inline Money RailBuildCost(RailType railtype)
346 assert(railtype < RAILTYPE_END);
347 return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
351 * Returns the 'cost' of clearing the specified railtype.
352 * @param railtype The railtype being removed.
353 * @return The cost.
355 static inline Money RailClearCost(RailType railtype)
357 /* Clearing rail in fact earns money, but if the build cost is set
358 * very low then a loophole exists where money can be made.
359 * In this case we limit the removal earnings to 3/4s of the build
360 * cost.
362 assert(railtype < RAILTYPE_END);
363 return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
367 * Calculates the cost of rail conversion
368 * @param from The railtype we are converting from
369 * @param to The railtype we are converting to
370 * @return Cost per TrackBit
372 static inline Money RailConvertCost(RailType from, RailType to)
374 /* Get the costs for removing and building anew
375 * A conversion can never be more costly */
376 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
378 /* Conversion between somewhat compatible railtypes:
379 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
380 * build costs, if the target type is more expensive (material upgrade costs).
381 * Upgrade can never be more expensive than re-building. */
382 if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
383 Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
384 return min(upgradecost, rebuildcost);
387 /* make the price the same as remove + build new type for rail types
388 * which are not compatible in any way */
389 return rebuildcost;
393 * Calculates the maintenance cost of a number of track bits.
394 * @param railtype The railtype to get the cost of.
395 * @param num Number of track bits of this railtype.
396 * @param total_num Total number of track bits of all railtypes.
397 * @return Total cost.
399 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
401 assert(railtype < RAILTYPE_END);
402 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.
406 * Calculates the maintenance cost of a number of signals.
407 * @param num Number of signals.
408 * @return Total cost.
410 static inline Money SignalMaintenanceCost(uint32 num)
412 return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
415 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
416 int TicksToLeaveDepot(const Train *v);
418 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
421 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
422 bool ValParamRailtype(const RailType rail);
424 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
426 RailType GetBestRailtype(const CompanyID company);
427 RailTypes GetCompanyRailtypes(const CompanyID c);
429 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
431 void ResetRailTypes();
432 void InitRailTypes();
433 RailType AllocateRailType(RailTypeLabel label);
435 extern RailType _sorted_railtypes[RAILTYPE_END];
436 extern uint8 _sorted_railtypes_size;
439 * Loop header for iterating over railtypes, sorted by sortorder.
440 * @param var Railtype.
442 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)
444 #endif /* RAIL_H */