OpenTTD
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 
29 
30  RTFB_NONE = 0,
33 };
35 
36 struct SpriteGroup;
37 
52  RTSG_END,
53 };
54 
76 };
77 
85 };
86 
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 
104 
108 struct RailtypeInfo {
113  struct {
126  } base_sprites;
127 
132  struct {
141  SpriteID signals[SIGTYPE_END][2][2];
142  } gui_sprites;
143 
144  struct {
153  } cursor;
154 
155  struct {
162  } strings;
163 
166 
169 
172 
177 
182 
187 
192 
197 
202 
207 
211  uint16 max_speed;
212 
216  RailTypeLabel label;
217 
222 
227 
236 
242 
247 
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 
410 
411 
412 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
413 bool ValParamRailtype(const RailType rail);
414 
416 
417 RailType GetBestRailtype(const CompanyID company);
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 */