# rail.h

Go to the documentation of this file.

    00001 /* $Id: rail.h 20095 2010-07-08 19:59:13Z rubidium $ */
    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
    00023 enum RailTypeFlags {
    00024   RTF_CATENARY          = 0,
    00025   RTF_NO_LEVEL_CROSSING = 1,
    00026
    00027   RTFB_NONE              = 0,
    00028   RTFB_CATENARY          = 1 << RTF_CATENARY,
    00029   RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING,
    00030 };
    00031 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags);
    00032
    00033 struct SpriteGroup;
    00034
    00035 enum RailTypeSpriteGroup {
    00036   RTSG_CURSORS,
    00037   RTSG_OVERLAY,
    00038   RTSG_GROUND,
    00039   RTSG_TUNNEL,
    00040   RTSG_WIRES,
    00041   RTSG_PYLONS,
    00042   RTSG_BRIDGE,
    00043   RTSG_CROSSING,
    00044   RTSG_DEPOT,
    00045   RTSG_FENCES,
    00046   RTSG_END,
    00047 };
    00048
    00053 enum RailTrackOffset {
    00054   RTO_X,
    00055   RTO_Y,
    00056   RTO_N,
    00057   RTO_S,
    00058   RTO_E,
    00059   RTO_W,
    00060   RTO_SLOPE_NE,
    00061   RTO_SLOPE_SE,
    00062   RTO_SLOPE_SW,
    00063   RTO_SLOPE_NW,
    00064   RTO_CROSSING_XY,
    00065   RTO_JUNCTION_SW,
    00066   RTO_JUNCTION_NE,
    00067   RTO_JUNCTION_SE,
    00068   RTO_JUNCTION_NW,
    00069   RTO_JUNCTION_NSEW,
    00070 };
    00071
    00075 enum RailTrackBridgeOffset {
    00076   RTBO_X,
    00077   RTBO_Y,
    00078   RTBO_SLOPE,
    00079 };
    00080
    00084 enum RailFenceOffset {
    00085   RFO_FLAT_X,
    00086   RFO_FLAT_Y,
    00087   RFO_FLAT_VERT,
    00088   RFO_FLAT_HORZ,
    00089   RFO_SLOPE_SW,
    00090   RFO_SLOPE_SE,
    00091   RFO_SLOPE_NE,
    00092   RFO_SLOPE_NW,
    00093 };
    00094
    00097 struct RailtypeInfo {
    00100   struct {
    00101     SpriteID track_y;
    00102     SpriteID track_ns;
    00103     SpriteID ground;
    00104     SpriteID single_x;
    00105     SpriteID single_y;
    00106     SpriteID single_n;
    00107     SpriteID single_s;
    00108     SpriteID single_e;
    00109     SpriteID single_w;
    00110     SpriteID single_sloped;
    00111     SpriteID crossing;
    00112     SpriteID tunnel;
    00113   } base_sprites;
    00114
    00117   struct {
    00118     SpriteID build_ns_rail;
    00119     SpriteID build_x_rail;
    00120     SpriteID build_ew_rail;
    00121     SpriteID build_y_rail;
    00122     SpriteID auto_rail;
    00123     SpriteID build_depot;
    00124     SpriteID build_tunnel;
    00125     SpriteID convert_rail;
    00126   } gui_sprites;
    00127
    00128   struct {
    00129     CursorID rail_ns;
    00130     CursorID rail_swne;
    00131     CursorID rail_ew;
    00132     CursorID rail_nwse;
    00133     CursorID autorail;
    00134     CursorID depot;
    00135     CursorID tunnel;
    00136     CursorID convert;
    00137   } cursor;
    00138
    00139   struct {
    00140     StringID toolbar_caption;
    00141     StringID menu_text;
    00142     StringID build_caption;
    00143     StringID replace_text;
    00144     StringID new_loco;
    00145   } strings;
    00146
    00148   SpriteID snow_offset;
    00149
    00151   RailTypes powered_railtypes;
    00152
    00154   RailTypes compatible_railtypes;
    00155
    00164   SpriteID total_offset;
    00165
    00169   SpriteID bridge_offset;
    00170
    00174   byte custom_ground_offset;
    00175
    00179   byte curve_speed;
    00180
    00184   RailTypeFlags flags;
    00185
    00189   uint16 cost_multiplier;
    00190
    00194   uint8 acceleration_type;
    00195
    00199   uint16 max_speed;
    00200
    00204   RailTypeLabel label;
    00205
    00209   byte map_colour;
    00210
    00214   const SpriteGroup *group[RTSG_END];
    00215
    00216   inline bool UsesOverlay() const
    00217   {
    00218     return this->group[RTSG_GROUND] != NULL;
    00219   }
    00220 };
    00221
    00222
    00228 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
    00229 {
    00230   extern RailtypeInfo _railtypes[RAILTYPE_END];
    00231   assert(railtype < RAILTYPE_END);
    00232   return &_railtypes[railtype];
    00233 }
    00234
    00243 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
    00244 {
    00245   return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
    00246 }
    00247
    00256 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
    00257 {
    00258   return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
    00259 }
    00260
    00266 static inline bool RailNoLevelCrossings(RailType rt)
    00267 {
    00268   return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
    00269 }
    00270
    00276 static inline Money RailBuildCost(RailType railtype)
    00277 {
    00278   assert(railtype < RAILTYPE_END);
    00279   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
    00280 }
    00281
    00287 static inline Money RailClearCost(RailType railtype)
    00288 {
    00289   /* Clearing rail in fact earns money, but if the build cost is set
    00290    * very low then a loophole exists where money can be made.
    00291    * In this case we limit the removal earnings to 3/4s of the build
    00292    * cost.
    00293    */
    00294   assert(railtype < RAILTYPE_END);
    00295   return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
    00296 }
    00297
    00304 static inline Money RailConvertCost(RailType from, RailType to)
    00305 {
    00306   /* rail -> el. rail
    00307    * calculate the price as 5 / 4 of (cost build el. rail) - (cost build rail)
    00308    * (the price of workers to get to place is that 1/4)
    00309    */
    00310   if (HasPowerOnRail(from, to)) {
    00311     Money cost = ((RailBuildCost(to) - RailBuildCost(from)) * 5) >> 2;
    00312     if (cost != 0) return cost;
    00313   }
    00314
    00315   /* el. rail -> rail
    00316    * calculate the price as 1 / 4 of (cost build el. rail) - (cost build rail)
    00317    * (the price of workers is 1 / 4 + price of copper sold to a recycle center)
    00318    */
    00319   if (HasPowerOnRail(to, from)) {
    00320     Money cost = (RailBuildCost(from) - RailBuildCost(to)) >> 2;
    00321     if (cost != 0) return cost;
    00322   }
    00323
    00324   /* make the price the same as remove + build new type */
    00325   return RailBuildCost(to) + RailClearCost(from);
    00326 }
    00327
    00328 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
    00329 Vehicle *EnsureNoTrainOnTrackProc(Vehicle *v, void *data);
    00330 int TicksToLeaveDepot(const Train *v);
    00331
    00332 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
    00333
    00334
    00341 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
    00342
    00348 bool ValParamRailtype(const RailType rail);
    00349
    00357 RailType GetBestRailtype(const CompanyID company);
    00358
    00364 RailTypes GetCompanyRailtypes(const CompanyID c);
    00365
    00371 RailType GetRailTypeByLabel(RailTypeLabel label);
    00372
    00376 void ResetRailTypes();
    00377
    00381 void InitRailTypes();
    00382
    00386 RailType AllocateRailType(RailTypeLabel label);
    00387
    00388 #endif /* RAIL_H */