OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

rail.h
Go to the documentation of this file.
1 /* $Id: rail.h 27343 2015-07-26 12:28:34Z frosch $ */
2
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 */
9
12 #ifndef RAIL_H
13 #define RAIL_H
14
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"
24
26 enum RailTypeFlags {
27 RTF_CATENARY = 0,
28 RTF_NO_LEVEL_CROSSING = 1,
29
30 RTFB_NONE = 0,
31 RTFB_CATENARY = 1 << RTF_CATENARY,
32 RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING,
33 };
34 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags)
35
36 struct SpriteGroup;
37
39 enum RailTypeSpriteGroup {
40 RTSG_CURSORS,
41 RTSG_OVERLAY,
42 RTSG_GROUND,
43 RTSG_TUNNEL,
44 RTSG_WIRES,
45 RTSG_PYLONS,
46 RTSG_BRIDGE,
47 RTSG_CROSSING,
48 RTSG_DEPOT,
49 RTSG_FENCES,
50 RTSG_TUNNEL_PORTAL,
51 RTSG_SIGNALS,
52 RTSG_END,
53 };
54
59 enum RailTrackOffset {
60 RTO_X,
61 RTO_Y,
62 RTO_N,
63 RTO_S,
64 RTO_E,
65 RTO_W,
66 RTO_SLOPE_NE,
67 RTO_SLOPE_SE,
68 RTO_SLOPE_SW,
69 RTO_SLOPE_NW,
70 RTO_CROSSING_XY,
71 RTO_JUNCTION_SW,
72 RTO_JUNCTION_NE,
73 RTO_JUNCTION_SE,
74 RTO_JUNCTION_NW,
75 RTO_JUNCTION_NSEW,
76 };
77
81 enum RailTrackBridgeOffset {
82 RTBO_X,
83 RTBO_Y,
84 RTBO_SLOPE,
85 };
86
91 enum RailFenceOffset {
92 RFO_FLAT_X_NW,
93 RFO_FLAT_Y_NE,
94 RFO_FLAT_LEFT,
95 RFO_FLAT_UPPER,
96 RFO_SLOPE_SW_NW,
97 RFO_SLOPE_SE_NE,
98 RFO_SLOPE_NE_NW,
99 RFO_SLOPE_NW_NE,
100 RFO_FLAT_X_SE,
101 RFO_FLAT_Y_SW,
102 RFO_FLAT_RIGHT,
103 RFO_FLAT_LOWER,
104 RFO_SLOPE_SW_SE,
105 RFO_SLOPE_SE_SW,
106 RFO_SLOPE_NE_SE,
107 RFO_SLOPE_NW_SW,
108 };
109
111 typedef SmallVector<RailTypeLabel, 4> RailTypeLabelList;
112
116 struct RailtypeInfo {
121 struct {
122 SpriteID track_y;
123 SpriteID track_ns;
124 SpriteID ground;
125 SpriteID single_x;
126 SpriteID single_y;
127 SpriteID single_n;
128 SpriteID single_s;
129 SpriteID single_e;
130 SpriteID single_w;
131 SpriteID single_sloped;
132 SpriteID crossing;
133 SpriteID tunnel;
134 } base_sprites;
135
140 struct {
141 SpriteID build_ns_rail;
142 SpriteID build_x_rail;
143 SpriteID build_ew_rail;
144 SpriteID build_y_rail;
145 SpriteID auto_rail;
146 SpriteID build_depot;
147 SpriteID build_tunnel;
148 SpriteID convert_rail;
149 SpriteID signals[SIGTYPE_END][2][2];
150 } gui_sprites;
151
152 struct {
153 CursorID rail_ns;
154 CursorID rail_swne;
155 CursorID rail_ew;
156 CursorID rail_nwse;
157 CursorID autorail;
158 CursorID depot;
159 CursorID tunnel;
160 CursorID convert;
161 } cursor;
162
163 struct {
164 StringID name;
165 StringID toolbar_caption;
166 StringID menu_text;
167 StringID build_caption;
168 StringID replace_text;
169 StringID new_loco;
170 } strings;
171
173 SpriteID snow_offset;
174
176 RailTypes powered_railtypes;
177
179 RailTypes compatible_railtypes;
180
184 SpriteID bridge_offset;
185
189 byte fallback_railtype;
190
194 byte curve_speed;
195
199 RailTypeFlags flags;
200
204 uint16 cost_multiplier;
205
209 uint16 maintenance_multiplier;
210
214 uint8 acceleration_type;
215
219 uint16 max_speed;
220
224 RailTypeLabel label;
225
229 RailTypeLabelList alternate_labels;
230
234 byte map_colour;
235
243 Date introduction_date;
244
249 RailTypes introduction_required_railtypes;
250
254 RailTypes introduces_railtypes;
255
259 byte sorting_order;
260
264 const GRFFile *grffile[RTSG_END];
265
269 const SpriteGroup *group[RTSG_END];
270
271 inline bool UsesOverlay() const
272 {
273 return this->group[RTSG_GROUND] != NULL;
274 }
275
283 inline uint GetRailtypeSpriteOffset() const
284 {
285 return 82 * this->fallback_railtype;
286 }
287 };
288
289
295 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
296 {
297 extern RailtypeInfo _railtypes[RAILTYPE_END];
298 assert(railtype < RAILTYPE_END);
299 return &_railtypes[railtype];
300 }
301
310 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
311 {
312 return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
313 }
314
323 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
324 {
325 return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
326 }
327
333 static inline bool RailNoLevelCrossings(RailType rt)
334 {
335 return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
336 }
337
343 static inline Money RailBuildCost(RailType railtype)
344 {
345 assert(railtype < RAILTYPE_END);
346 return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
347 }
348
354 static inline Money RailClearCost(RailType railtype)
355 {
356 /* Clearing rail in fact earns money, but if the build cost is set
357 * very low then a loophole exists where money can be made.
358 * In this case we limit the removal earnings to 3/4s of the build
359 * cost.
360 */
361 assert(railtype < RAILTYPE_END);
362 return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
363 }
364
371 static inline Money RailConvertCost(RailType from, RailType to)
372 {
373 /* Get the costs for removing and building anew
374 * A conversion can never be more costly */
375 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
376
377 /* Conversion between somewhat compatible railtypes:
378 * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
379 * build costs, if the target type is more expensive (material upgrade costs).
380 * Upgrade can never be more expensive than re-building. */
381 if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
382 Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
383 return min(upgradecost, rebuildcost);
384 }
385
386 /* make the price the same as remove + build new type for rail types
387 * which are not compatible in any way */
388 return rebuildcost;
389 }
390
398 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
399 {
400 assert(railtype < RAILTYPE_END);
401 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 }
403
409 static inline Money SignalMaintenanceCost(uint32 num)
410 {
411 return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
412 }
413
414 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
415 int TicksToLeaveDepot(const Train *v);
416
417 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
418
419
420 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
421 bool ValParamRailtype(const RailType rail);
422
423 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
424
425 RailType GetBestRailtype(const CompanyID company);
426 RailTypes GetCompanyRailtypes(const CompanyID c);
427
428 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
429
430 void ResetRailTypes();
431 void InitRailTypes();
432 RailType AllocateRailType(RailTypeLabel label);
433
434 #endif /* RAIL_H */