rail.h

Go to the documentation of this file.
00001 /* $Id: rail.h 19439 2010-03-16 20:54:26Z 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 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   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 Money RailBuildCost(RailType railtype)
00267 {
00268   assert(railtype < RAILTYPE_END);
00269   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
00270 }
00271 
00277 static inline Money RailClearCost(RailType railtype)
00278 {
00279   /* Clearing rail in fact earns money, but if the build cost is set
00280    * very low then a loophole exists where money can be made.
00281    * In this case we limit the removal earnings to 3/4s of the build
00282    * cost.
00283    */
00284   assert(railtype < RAILTYPE_END);
00285   return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
00286 }
00287 
00294 static inline Money RailConvertCost(RailType from, RailType to)
00295 {
00296   /* rail -> el. rail
00297    * calculate the price as 5 / 4 of (cost build el. rail) - (cost build rail)
00298    * (the price of workers to get to place is that 1/4)
00299    */
00300   if (HasPowerOnRail(from, to)) {
00301     Money cost = ((RailBuildCost(to) - RailBuildCost(from)) * 5) >> 2;
00302     if (cost != 0) return cost;
00303   }
00304 
00305   /* el. rail -> rail
00306    * calculate the price as 1 / 4 of (cost build el. rail) - (cost build rail)
00307    * (the price of workers is 1 / 4 + price of copper sold to a recycle center)
00308    */
00309   if (HasPowerOnRail(to, from)) {
00310     Money cost = (RailBuildCost(from) - RailBuildCost(to)) >> 2;
00311     if (cost != 0) return cost;
00312   }
00313 
00314   /* make the price the same as remove + build new type */
00315   return RailBuildCost(to) + RailClearCost(from);
00316 }
00317 
00318 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
00319 Vehicle *EnsureNoTrainOnTrackProc(Vehicle *v, void *data);
00320 int TicksToLeaveDepot(const Train *v);
00321 
00322 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
00323 
00324 
00331 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
00332 
00338 bool ValParamRailtype(const RailType rail);
00339 
00347 RailType GetBestRailtype(const CompanyID company);
00348 
00354 RailTypes GetCompanyRailtypes(const CompanyID c);
00355 
00361 RailType GetRailTypeByLabel(RailTypeLabel label);
00362 
00366 void ResetRailTypes();
00367 
00371 void InitRailTypes();
00372 
00376 RailType AllocateRailType(RailTypeLabel label);
00377 
00378 #endif /* RAIL_H */

Generated on Sat Apr 17 23:24:51 2010 for OpenTTD by  doxygen 1.6.1