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 26803 2014-09-07 16:13:29Z alberth $ */
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,
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,
100 };
101
103 typedef SmallVector<RailTypeLabel, 4> RailTypeLabelList;
104
108 struct RailtypeInfo {
113 struct {
114 SpriteID track_y;
115 SpriteID track_ns;
116 SpriteID ground;
117 SpriteID single_x;
118 SpriteID single_y;
119 SpriteID single_n;
120 SpriteID single_s;
121 SpriteID single_e;
122 SpriteID single_w;
123 SpriteID single_sloped;
124 SpriteID crossing;
125 SpriteID tunnel;
126 } base_sprites;
127
132 struct {
133 SpriteID build_ns_rail;
134 SpriteID build_x_rail;
135 SpriteID build_ew_rail;
136 SpriteID build_y_rail;
137 SpriteID auto_rail;
138 SpriteID build_depot;
139 SpriteID build_tunnel;
140 SpriteID convert_rail;
141 SpriteID signals[SIGTYPE_END][2][2];
142 } gui_sprites;
143
144 struct {
145 CursorID rail_ns;
146 CursorID rail_swne;
147 CursorID rail_ew;
148 CursorID rail_nwse;
149 CursorID autorail;
150 CursorID depot;
151 CursorID tunnel;
152 CursorID convert;
153 } cursor;
154
155 struct {
156 StringID name;
157 StringID toolbar_caption;
158 StringID menu_text;
159 StringID build_caption;
160 StringID replace_text;
161 StringID new_loco;
162 } strings;
163
165 SpriteID snow_offset;
166
168 RailTypes powered_railtypes;
169
171 RailTypes compatible_railtypes;
172
176 SpriteID bridge_offset;
177
181 byte fallback_railtype;
182
186 byte curve_speed;
187
191 RailTypeFlags flags;
192
196 uint16 cost_multiplier;
197
201 uint16 maintenance_multiplier;
202
206 uint8 acceleration_type;
207
211 uint16 max_speed;
212
216 RailTypeLabel label;
217
221 RailTypeLabelList alternate_labels;
222
226 byte map_colour;
227
235 Date introduction_date;
236
241 RailTypes introduction_required_railtypes;
242
246 RailTypes introduces_railtypes;
247
251 byte sorting_order;
252
256 const GRFFile *grffile[RTSG_END];
257
261 const SpriteGroup *group[RTSG_END];
262
263 inline bool UsesOverlay() const
264 {
265 return this->group[RTSG_GROUND] != NULL;
266 }
267
275 inline uint GetRailtypeSpriteOffset() const
276 {
277 return 82 * this->fallback_railtype;
278 }
279 };
280
281
287 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
288 {
289 extern RailtypeInfo _railtypes[RAILTYPE_END];
290 assert(railtype < RAILTYPE_END);
291 return &_railtypes[railtype];
292 }
293
302 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
303 {
304 return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
305 }
306
315 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
316 {
317 return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
318 }
319
325 static inline bool RailNoLevelCrossings(RailType rt)
326 {
327 return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
328 }
329
335 static inline Money RailBuildCost(RailType railtype)
336 {
337 assert(railtype < RAILTYPE_END);
338 return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
339 }
340
346 static inline Money RailClearCost(RailType railtype)
347 {
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.
352 */
353 assert(railtype < RAILTYPE_END);
354 return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
355 }
356
363 static inline Money RailConvertCost(RailType from, RailType to)
364 {
365 /* Get the costs for removing and building anew
366 * A conversion can never be more costly */
367 Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
368
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);
376 }
377
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;
381 }
382
390 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
391 {
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.
394 }
395
401 static inline Money SignalMaintenanceCost(uint32 num)
402 {
403 return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
404 }
405
406 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
407 int TicksToLeaveDepot(const Train *v);
408
409 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
410
411
412 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
413 bool ValParamRailtype(const RailType rail);
414
415 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
416
417 RailType GetBestRailtype(const CompanyID company);
418 RailTypes GetCompanyRailtypes(const CompanyID c);
419
420 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
421
422 void ResetRailTypes();
423 void InitRailTypes();
424 RailType AllocateRailType(RailTypeLabel label);
425
426 #endif /* RAIL_H */