OpenTTD
rail.h
Go to the documentation of this file.
1 /* $Id: rail.h 27427 2015-10-30 17:24:30Z 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 
116 struct RailtypeInfo {
121  struct {
134  } base_sprites;
135 
140  struct {
149  SpriteID signals[SIGTYPE_END][2][2];
150  } gui_sprites;
151 
152  struct {
161  } cursor;
162 
163  struct {
170  } strings;
171 
174 
177 
180 
185 
190 
195 
200 
205 
210 
215 
219  uint16 max_speed;
220 
224  RailTypeLabel label;
225 
230 
235 
244 
250 
255 
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 
418 
419 
420 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
421 bool ValParamRailtype(const RailType rail);
422 
424 
425 RailType GetBestRailtype(const CompanyID company);
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 extern RailType _sorted_railtypes[RAILTYPE_END];
435 extern uint8 _sorted_railtypes_size;
436 
441 #define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++)
442 
443 #endif /* RAIL_H */