# rail.h

Go to the documentation of this file.

    00001 /* $Id: rail.h 18509 2009-12-15 13:59:20Z 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 "vehicle_type.h"
    00018 #include "gfx_type.h"
    00019 #include "core/bitmath_func.hpp"
    00020 #include "economy_func.h"
    00021 #include "slope_type.h"
    00022 #include "strings_type.h"
    00023
    00024 enum RailTypeFlag {
    00025   RTF_CATENARY = 0,
    00026 };
    00027
    00028 enum RailTypeFlags {
    00029   RTFB_NONE     = 0,
    00030   RTFB_CATENARY = 1 << RTF_CATENARY,
    00031 };
    00032 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags);
    00033
    00037 enum RailFenceOffset {
    00038   RFO_FLAT_X,
    00039   RFO_FLAT_Y,
    00040   RFO_FLAT_VERT,
    00041   RFO_FLAT_HORZ,
    00042   RFO_SLOPE_SW,
    00043   RFO_SLOPE_SE,
    00044   RFO_SLOPE_NE,
    00045   RFO_SLOPE_NW,
    00046 };
    00047
    00050 struct RailtypeInfo {
    00053   struct {
    00054     SpriteID track_y;
    00055     SpriteID track_ns;
    00056     SpriteID ground;
    00057     SpriteID single_x;
    00058     SpriteID single_y;
    00059     SpriteID single_n;
    00060     SpriteID single_s;
    00061     SpriteID single_e;
    00062     SpriteID single_w;
    00063     SpriteID single_sloped;
    00064     SpriteID crossing;
    00065     SpriteID tunnel;
    00066   } base_sprites;
    00067
    00070   struct {
    00071     SpriteID build_ns_rail;
    00072     SpriteID build_x_rail;
    00073     SpriteID build_ew_rail;
    00074     SpriteID build_y_rail;
    00075     SpriteID auto_rail;
    00076     SpriteID build_depot;
    00077     SpriteID build_tunnel;
    00078     SpriteID convert_rail;
    00079   } gui_sprites;
    00080
    00081   struct {
    00082     CursorID rail_ns;
    00083     CursorID rail_swne;
    00084     CursorID rail_ew;
    00085     CursorID rail_nwse;
    00086     CursorID autorail;
    00087     CursorID depot;
    00088     CursorID tunnel;
    00089     CursorID convert;
    00090   } cursor;
    00091
    00092   struct {
    00093     StringID toolbar_caption;
    00094     StringID menu_text;
    00095     StringID build_caption;
    00096     StringID replace_text;
    00097     StringID new_loco;
    00098   } strings;
    00099
    00101   SpriteID snow_offset;
    00102
    00104   RailTypes powered_railtypes;
    00105
    00107   RailTypes compatible_railtypes;
    00108
    00117   SpriteID total_offset;
    00118
    00122   SpriteID bridge_offset;
    00123
    00127   byte custom_ground_offset;
    00128
    00132   byte curve_speed;
    00133
    00137   RailTypeFlags flags;
    00138
    00142   uint8 cost_multiplier;
    00143
    00147   RailTypeLabel label;
    00148 };
    00149
    00150
    00156 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
    00157 {
    00158   extern RailtypeInfo _railtypes[RAILTYPE_END];
    00159   assert(railtype < RAILTYPE_END);
    00160   return &_railtypes[railtype];
    00161 }
    00162
    00171 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
    00172 {
    00173   return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
    00174 }
    00175
    00184 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
    00185 {
    00186   return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
    00187 }
    00188
    00194 static inline Money RailBuildCost(RailType railtype)
    00195 {
    00196   assert(railtype < RAILTYPE_END);
    00197   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
    00198 }
    00199
    00206 static inline Money RailConvertCost(RailType from, RailType to)
    00207 {
    00208   /* rail -> el. rail
    00209    * calculate the price as 5 / 4 of (cost build el. rail) - (cost build rail)
    00210    * (the price of workers to get to place is that 1/4)
    00211    */
    00212   if (HasPowerOnRail(from, to)) {
    00213     Money cost = ((RailBuildCost(to) - RailBuildCost(from)) * 5) >> 2;
    00214     if (cost != 0) return cost;
    00215   }
    00216
    00217   /* el. rail -> rail
    00218    * calculate the price as 1 / 4 of (cost build el. rail) - (cost build rail)
    00219    * (the price of workers is 1 / 4 + price of copper sold to a recycle center)
    00220    */
    00221   if (HasPowerOnRail(to, from)) {
    00222     Money cost = (RailBuildCost(from) - RailBuildCost(to)) >> 2;
    00223     if (cost != 0) return cost;
    00224   }
    00225
    00226   /* make the price the same as remove + build new type */
    00227   return RailBuildCost(to) + _price[PR_CLEAR_RAIL];
    00228 }
    00229
    00230 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
    00231 Vehicle *EnsureNoTrainOnTrackProc(Vehicle *v, void *data);
    00232 int TicksToLeaveDepot(const Train *v);
    00233
    00234 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
    00235
    00236
    00243 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
    00244
    00250 bool ValParamRailtype(const RailType rail);
    00251
    00259 RailType GetBestRailtype(const CompanyID company);
    00260
    00266 RailTypes GetCompanyRailtypes(const CompanyID c);
    00267
    00273 RailType GetRailTypeByLabel(RailTypeLabel label);
    00274
    00278 void ResetRailTypes();
    00279
    00280 #endif /* RAIL_H */