# rail.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef RAIL_H
    00013 #define RAIL_H
    00014
    00015 #include "rail_type.h"
    00016 #include "track_type.h"
    00017 #include "gfx_type.h"
    00018 #include "core/bitmath_func.hpp"
    00019 #include "economy_func.h"
    00020 #include "slope_type.h"
    00021 #include "strings_type.h"
    00022 #include "date_type.h"
    00023 #include "signal_type.h"
    00024
    00026 enum RailTypeFlags {
    00027   RTF_CATENARY          = 0,
    00028   RTF_NO_LEVEL_CROSSING = 1,
    00029
    00030   RTFB_NONE              = 0,
    00031   RTFB_CATENARY          = 1 << RTF_CATENARY,
    00032   RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING,
    00033 };
    00034 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags)
    00035
    00036 struct SpriteGroup;
    00037
    00039 enum RailTypeSpriteGroup {
    00040   RTSG_CURSORS,
    00041   RTSG_OVERLAY,
    00042   RTSG_GROUND,
    00043   RTSG_TUNNEL,
    00044   RTSG_WIRES,
    00045   RTSG_PYLONS,
    00046   RTSG_BRIDGE,
    00047   RTSG_CROSSING,
    00048   RTSG_DEPOT,
    00049   RTSG_FENCES,
    00050   RTSG_TUNNEL_PORTAL,
    00051   RTSG_SIGNALS,
    00052   RTSG_END,
    00053 };
    00054
    00059 enum RailTrackOffset {
    00060   RTO_X,
    00061   RTO_Y,
    00062   RTO_N,
    00063   RTO_S,
    00064   RTO_E,
    00065   RTO_W,
    00066   RTO_SLOPE_NE,
    00067   RTO_SLOPE_SE,
    00068   RTO_SLOPE_SW,
    00069   RTO_SLOPE_NW,
    00070   RTO_CROSSING_XY,
    00071   RTO_JUNCTION_SW,
    00072   RTO_JUNCTION_NE,
    00073   RTO_JUNCTION_SE,
    00074   RTO_JUNCTION_NW,
    00075   RTO_JUNCTION_NSEW,
    00076 };
    00077
    00081 enum RailTrackBridgeOffset {
    00082   RTBO_X,
    00083   RTBO_Y,
    00084   RTBO_SLOPE,
    00085 };
    00086
    00091 enum RailFenceOffset {
    00092   RFO_FLAT_X,
    00093   RFO_FLAT_Y,
    00094   RFO_FLAT_VERT,
    00095   RFO_FLAT_HORZ,
    00096   RFO_SLOPE_SW,
    00097   RFO_SLOPE_SE,
    00098   RFO_SLOPE_NE,
    00099   RFO_SLOPE_NW,
    00100 };
    00101
    00103 typedef SmallVector<RailTypeLabel, 4> RailTypeLabelList;
    00104
    00108 struct RailtypeInfo {
    00113   struct {
    00114     SpriteID track_y;
    00115     SpriteID track_ns;
    00116     SpriteID ground;
    00117     SpriteID single_x;
    00118     SpriteID single_y;
    00119     SpriteID single_n;
    00120     SpriteID single_s;
    00121     SpriteID single_e;
    00122     SpriteID single_w;
    00123     SpriteID single_sloped;
    00124     SpriteID crossing;
    00125     SpriteID tunnel;
    00126   } base_sprites;
    00127
    00132   struct {
    00133     SpriteID build_ns_rail;
    00134     SpriteID build_x_rail;
    00135     SpriteID build_ew_rail;
    00136     SpriteID build_y_rail;
    00137     SpriteID auto_rail;
    00138     SpriteID build_depot;
    00139     SpriteID build_tunnel;
    00140     SpriteID convert_rail;
    00141     SpriteID signals[SIGTYPE_END][2][2];
    00142   } gui_sprites;
    00143
    00144   struct {
    00145     CursorID rail_ns;
    00146     CursorID rail_swne;
    00147     CursorID rail_ew;
    00148     CursorID rail_nwse;
    00149     CursorID autorail;
    00150     CursorID depot;
    00151     CursorID tunnel;
    00152     CursorID convert;
    00153   } cursor;
    00154
    00155   struct {
    00156     StringID name;
    00157     StringID toolbar_caption;
    00158     StringID menu_text;
    00159     StringID build_caption;
    00160     StringID replace_text;
    00161     StringID new_loco;
    00162   } strings;
    00163
    00165   SpriteID snow_offset;
    00166
    00168   RailTypes powered_railtypes;
    00169
    00171   RailTypes compatible_railtypes;
    00172
    00176   SpriteID bridge_offset;
    00177
    00181   byte fallback_railtype;
    00182
    00186   byte curve_speed;
    00187
    00191   RailTypeFlags flags;
    00192
    00196   uint16 cost_multiplier;
    00197
    00201   uint16 maintenance_multiplier;
    00202
    00206   uint8 acceleration_type;
    00207
    00211   uint16 max_speed;
    00212
    00216   RailTypeLabel label;
    00217
    00221   RailTypeLabelList alternate_labels;
    00222
    00226   byte map_colour;
    00227
    00235   Date introduction_date;
    00236
    00241   RailTypes introduction_required_railtypes;
    00242
    00246   RailTypes introduces_railtypes;
    00247
    00251   byte sorting_order;
    00252
    00256   const GRFFile *grffile[RTSG_END];
    00257
    00261   const SpriteGroup *group[RTSG_END];
    00262
    00263   inline bool UsesOverlay() const
    00264   {
    00265     return this->group[RTSG_GROUND] != NULL;
    00266   }
    00267
    00275   inline uint GetRailtypeSpriteOffset() const
    00276   {
    00277     return 82 * this->fallback_railtype;
    00278   }
    00279 };
    00280
    00281
    00287 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
    00288 {
    00289   extern RailtypeInfo _railtypes[RAILTYPE_END];
    00290   assert(railtype < RAILTYPE_END);
    00291   return &_railtypes[railtype];
    00292 }
    00293
    00302 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
    00303 {
    00304   return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
    00305 }
    00306
    00315 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
    00316 {
    00317   return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
    00318 }
    00319
    00325 static inline bool RailNoLevelCrossings(RailType rt)
    00326 {
    00327   return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
    00328 }
    00329
    00335 static inline Money RailBuildCost(RailType railtype)
    00336 {
    00337   assert(railtype < RAILTYPE_END);
    00338   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
    00339 }
    00340
    00346 static inline Money RailClearCost(RailType railtype)
    00347 {
    00348   /* Clearing rail in fact earns money, but if the build cost is set
    00349    * very low then a loophole exists where money can be made.
    00350    * In this case we limit the removal earnings to 3/4s of the build
    00351    * cost.
    00352    */
    00353   assert(railtype < RAILTYPE_END);
    00354   return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
    00355 }
    00356
    00363 static inline Money RailConvertCost(RailType from, RailType to)
    00364 {
    00365   /* Get the costs for removing and building anew
    00366    * A conversion can never be more costly */
    00367   Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
    00368
    00369   /* Conversion between somewhat compatible railtypes:
    00370    * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
    00371    * build costs, if the target type is more expensive (material upgrade costs).
    00372    * Upgrade can never be more expensive than re-building. */
    00373   if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
    00374     Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
    00375     return min(upgradecost, rebuildcost);
    00376   }
    00377
    00378   /* make the price the same as remove + build new type for rail types
    00379    * which are not compatible in any way */
    00380   return rebuildcost;
    00381 }
    00382
    00390 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
    00391 {
    00392   assert(railtype < RAILTYPE_END);
    00393   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.
    00394 }
    00395
    00401 static inline Money SignalMaintenanceCost(uint32 num)
    00402 {
    00403   return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
    00404 }
    00405
    00406 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
    00407 int TicksToLeaveDepot(const Train *v);
    00408
    00409 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
    00410
    00411
    00412 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
    00413 bool ValParamRailtype(const RailType rail);
    00414
    00415 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
    00416
    00417 RailType GetBestRailtype(const CompanyID company);
    00418 RailTypes GetCompanyRailtypes(const CompanyID c);
    00419
    00420 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
    00421
    00422 void ResetRailTypes();
    00423 void InitRailTypes();
    00424 RailType AllocateRailType(RailTypeLabel label);
    00425
    00426 #endif /* RAIL_H */