OpenTTD
rail.h
Go to the documentation of this file.
1 /* $Id: rail.h 27687 2016-12-10 13:26:29Z 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 
29 
30  RTFB_NONE = 0,
33 };
35 
36 struct SpriteGroup;
37 
52  RTSG_END,
53 };
54 
76 };
77 
85 };
86 
108 };
109 
112 
117 public:
122  struct {
135  } base_sprites;
136 
141  struct {
150  SpriteID signals[SIGTYPE_END][2][2];
151  } gui_sprites;
152 
153  struct {
162  } cursor;
163 
164  struct {
171  } strings;
172 
175 
178 
181 
186 
191 
196 
201 
206 
211 
216 
220  uint16 max_speed;
221 
225  RailTypeLabel label;
226 
231 
236 
245 
251 
256 
261 
265  const GRFFile *grffile[RTSG_END];
266 
270  const SpriteGroup *group[RTSG_END];
271 
272  inline bool UsesOverlay() const
273  {
274  return this->group[RTSG_GROUND] != NULL;
275  }
276 
284  inline uint GetRailtypeSpriteOffset() const
285  {
286  return 82 * this->fallback_railtype;
287  }
288 };
289 
290 
296 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
297 {
298  extern RailtypeInfo _railtypes[RAILTYPE_END];
299  assert(railtype < RAILTYPE_END);
300  return &_railtypes[railtype];
301 }
302 
311 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
312 {
313  return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
314 }
315 
324 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
325 {
326  return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
327 }
328 
334 static inline bool RailNoLevelCrossings(RailType rt)
335 {
336  return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
337 }
338 
344 static inline Money RailBuildCost(RailType railtype)
345 {
346  assert(railtype < RAILTYPE_END);
347  return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
348 }
349 
355 static inline Money RailClearCost(RailType railtype)
356 {
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.
361  */
362  assert(railtype < RAILTYPE_END);
363  return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
364 }
365 
372 static inline Money RailConvertCost(RailType from, RailType to)
373 {
374  /* Get the costs for removing and building anew
375  * A conversion can never be more costly */
376  Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
377 
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);
385  }
386 
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;
390 }
391 
399 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
400 {
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.
403 }
404 
410 static inline Money SignalMaintenanceCost(uint32 num)
411 {
412  return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
413 }
414 
415 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
416 int TicksToLeaveDepot(const Train *v);
417 
419 
420 
421 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
422 bool ValParamRailtype(const RailType rail);
423 
425 
426 RailType GetBestRailtype(const CompanyID company);
428 
429 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
430 
431 void ResetRailTypes();
432 void InitRailTypes();
433 RailType AllocateRailType(RailTypeLabel label);
434 
435 extern RailType _sorted_railtypes[RAILTYPE_END];
436 extern uint8 _sorted_railtypes_size;
437 
442 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)
443 
444 #endif /* RAIL_H */