# rail.h

Go to the documentation of this file.

    00001 /* $Id: rail.h 19056 2010-02-07 22:22:54Z peter1138 $ */
    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 RailTypeFlag {
    00024   RTF_CATENARY = 0,
    00025 };
    00026
    00027 enum RailTypeFlags {
    00028   RTFB_NONE     = 0,
    00029   RTFB_CATENARY = 1 << RTF_CATENARY,
    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   uint8 cost_multiplier;
    00190
    00194   uint8 acceleration_type;
    00195
    00199   uint16 max_speed;
    00200
    00204   RailTypeLabel label;
    00205
    00209   const SpriteGroup *group[RTSG_END];
    00210
    00211   inline bool UsesOverlay() const
    00212   {
    00213     return this->group[RTSG_GROUND] != NULL;
    00214   }
    00215 };
    00216
    00217
    00223 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
    00224 {
    00225   extern RailtypeInfo _railtypes[RAILTYPE_END];
    00226   assert(railtype < RAILTYPE_END);
    00227   return &_railtypes[railtype];
    00228 }
    00229
    00238 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
    00239 {
    00240   return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
    00241 }
    00242
    00251 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
    00252 {
    00253   return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
    00254 }
    00255
    00261 static inline Money RailBuildCost(RailType railtype)
    00262 {
    00263   assert(railtype < RAILTYPE_END);
    00264   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
    00265 }
    00266
    00273 static inline Money RailConvertCost(RailType from, RailType to)
    00274 {
    00275   /* rail -> el. rail
    00276    * calculate the price as 5 / 4 of (cost build el. rail) - (cost build rail)
    00277    * (the price of workers to get to place is that 1/4)
    00278    */
    00279   if (HasPowerOnRail(from, to)) {
    00280     Money cost = ((RailBuildCost(to) - RailBuildCost(from)) * 5) >> 2;
    00281     if (cost != 0) return cost;
    00282   }
    00283
    00284   /* el. rail -> rail
    00285    * calculate the price as 1 / 4 of (cost build el. rail) - (cost build rail)
    00286    * (the price of workers is 1 / 4 + price of copper sold to a recycle center)
    00287    */
    00288   if (HasPowerOnRail(to, from)) {
    00289     Money cost = (RailBuildCost(from) - RailBuildCost(to)) >> 2;
    00290     if (cost != 0) return cost;
    00291   }
    00292
    00293   /* make the price the same as remove + build new type */
    00294   return RailBuildCost(to) + _price[PR_CLEAR_RAIL];
    00295 }
    00296
    00297 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
    00298 Vehicle *EnsureNoTrainOnTrackProc(Vehicle *v, void *data);
    00299 int TicksToLeaveDepot(const Train *v);
    00300
    00301 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
    00302
    00303
    00310 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
    00311
    00317 bool ValParamRailtype(const RailType rail);
    00318
    00326 RailType GetBestRailtype(const CompanyID company);
    00327
    00333 RailTypes GetCompanyRailtypes(const CompanyID c);
    00334
    00340 RailType GetRailTypeByLabel(RailTypeLabel label);
    00341
    00345 void ResetRailTypes();
    00346
    00350 void InitRailTypes();
    00351
    00355 RailType AllocateRailType(RailTypeLabel label);
    00356
    00357 #endif /* RAIL_H */