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 
00025 enum RailTypeFlags {
00026   RTF_CATENARY          = 0,                           
00027   RTF_NO_LEVEL_CROSSING = 1,                           
00028 
00029   RTFB_NONE              = 0,                          
00030   RTFB_CATENARY          = 1 << RTF_CATENARY,          
00031   RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING, 
00032 };
00033 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags)
00034 
00035 struct SpriteGroup;
00036 
00038 enum RailTypeSpriteGroup {
00039   RTSG_CURSORS,     
00040   RTSG_OVERLAY,     
00041   RTSG_GROUND,      
00042   RTSG_TUNNEL,      
00043   RTSG_WIRES,       
00044   RTSG_PYLONS,      
00045   RTSG_BRIDGE,      
00046   RTSG_CROSSING,    
00047   RTSG_DEPOT,       
00048   RTSG_FENCES,      
00049   RTSG_END,
00050 };
00051 
00056 enum RailTrackOffset {
00057   RTO_X,            
00058   RTO_Y,            
00059   RTO_N,            
00060   RTO_S,            
00061   RTO_E,            
00062   RTO_W,            
00063   RTO_SLOPE_NE,     
00064   RTO_SLOPE_SE,     
00065   RTO_SLOPE_SW,     
00066   RTO_SLOPE_NW,     
00067   RTO_CROSSING_XY,  
00068   RTO_JUNCTION_SW,  
00069   RTO_JUNCTION_NE,  
00070   RTO_JUNCTION_SE,  
00071   RTO_JUNCTION_NW,  
00072   RTO_JUNCTION_NSEW,
00073 };
00074 
00078 enum RailTrackBridgeOffset {
00079   RTBO_X,     
00080   RTBO_Y,     
00081   RTBO_SLOPE, 
00082 };
00083 
00088 enum RailFenceOffset {
00089   RFO_FLAT_X,
00090   RFO_FLAT_Y,
00091   RFO_FLAT_VERT,
00092   RFO_FLAT_HORZ,
00093   RFO_SLOPE_SW,
00094   RFO_SLOPE_SE,
00095   RFO_SLOPE_NE,
00096   RFO_SLOPE_NW,
00097 };
00098 
00100 typedef SmallVector<RailTypeLabel, 4> RailTypeLabelList;
00101 
00105 struct RailtypeInfo {
00110   struct {
00111     SpriteID track_y;      
00112     SpriteID track_ns;     
00113     SpriteID ground;       
00114     SpriteID single_x;     
00115     SpriteID single_y;     
00116     SpriteID single_n;     
00117     SpriteID single_s;     
00118     SpriteID single_e;     
00119     SpriteID single_w;     
00120     SpriteID single_sloped;
00121     SpriteID crossing;     
00122     SpriteID tunnel;       
00123   } base_sprites;
00124 
00129   struct {
00130     SpriteID build_ns_rail;      
00131     SpriteID build_x_rail;       
00132     SpriteID build_ew_rail;      
00133     SpriteID build_y_rail;       
00134     SpriteID auto_rail;          
00135     SpriteID build_depot;        
00136     SpriteID build_tunnel;       
00137     SpriteID convert_rail;       
00138   } gui_sprites;
00139 
00140   struct {
00141     CursorID rail_ns;    
00142     CursorID rail_swne;  
00143     CursorID rail_ew;    
00144     CursorID rail_nwse;  
00145     CursorID autorail;   
00146     CursorID depot;      
00147     CursorID tunnel;     
00148     CursorID convert;    
00149   } cursor;
00150 
00151   struct {
00152     StringID name;
00153     StringID toolbar_caption;
00154     StringID menu_text;
00155     StringID build_caption;
00156     StringID replace_text;
00157     StringID new_loco;
00158   } strings;
00159 
00161   SpriteID snow_offset;
00162 
00164   RailTypes powered_railtypes;
00165 
00167   RailTypes compatible_railtypes;
00168 
00172   SpriteID bridge_offset;
00173 
00177   byte fallback_railtype;
00178 
00182   byte curve_speed;
00183 
00187   RailTypeFlags flags;
00188 
00192   uint16 cost_multiplier;
00193 
00197   uint16 maintenance_multiplier;
00198 
00202   uint8 acceleration_type;
00203 
00207   uint16 max_speed;
00208 
00212   RailTypeLabel label;
00213 
00217   RailTypeLabelList alternate_labels;
00218 
00222   byte map_colour;
00223 
00231   Date introduction_date;
00232 
00237   RailTypes introduction_required_railtypes;
00238 
00242   RailTypes introduces_railtypes;
00243 
00247   byte sorting_order;
00248 
00252   const GRFFile *grffile[RTSG_END];
00253 
00257   const SpriteGroup *group[RTSG_END];
00258 
00259   inline bool UsesOverlay() const
00260   {
00261     return this->group[RTSG_GROUND] != NULL;
00262   }
00263 
00271   inline uint GetRailtypeSpriteOffset() const
00272   {
00273     return 82 * this->fallback_railtype;
00274   }
00275 };
00276 
00277 
00283 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
00284 {
00285   extern RailtypeInfo _railtypes[RAILTYPE_END];
00286   assert(railtype < RAILTYPE_END);
00287   return &_railtypes[railtype];
00288 }
00289 
00298 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
00299 {
00300   return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
00301 }
00302 
00311 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
00312 {
00313   return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
00314 }
00315 
00321 static inline bool RailNoLevelCrossings(RailType rt)
00322 {
00323   return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
00324 }
00325 
00331 static inline Money RailBuildCost(RailType railtype)
00332 {
00333   assert(railtype < RAILTYPE_END);
00334   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
00335 }
00336 
00342 static inline Money RailClearCost(RailType railtype)
00343 {
00344   /* Clearing rail in fact earns money, but if the build cost is set
00345    * very low then a loophole exists where money can be made.
00346    * In this case we limit the removal earnings to 3/4s of the build
00347    * cost.
00348    */
00349   assert(railtype < RAILTYPE_END);
00350   return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
00351 }
00352 
00359 static inline Money RailConvertCost(RailType from, RailType to)
00360 {
00361   /* Get the costs for removing and building anew
00362    * A conversion can never be more costly */
00363   Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
00364 
00365   /* Conversion between somewhat compatible railtypes:
00366    * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
00367    * build costs, if the target type is more expensive (material upgrade costs).
00368    * Upgrade can never be more expensive than re-building. */
00369   if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
00370     Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
00371     return min(upgradecost, rebuildcost);
00372   }
00373 
00374   /* make the price the same as remove + build new type for rail types
00375    * which are not compatible in any way */
00376   return rebuildcost;
00377 }
00378 
00386 static inline Money RailMaintenanceCost(RailType railtype, uint32 num, uint32 total_num)
00387 {
00388   assert(railtype < RAILTYPE_END);
00389   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.
00390 }
00391 
00397 static inline Money SignalMaintenanceCost(uint32 num)
00398 {
00399   return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
00400 }
00401 
00402 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
00403 int TicksToLeaveDepot(const Train *v);
00404 
00405 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
00406 
00407 
00408 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
00409 bool ValParamRailtype(const RailType rail);
00410 
00411 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
00412 
00413 RailType GetBestRailtype(const CompanyID company);
00414 RailTypes GetCompanyRailtypes(const CompanyID c);
00415 
00416 RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels = true);
00417 
00418 void ResetRailTypes();
00419 void InitRailTypes();
00420 RailType AllocateRailType(RailTypeLabel label);
00421 
00422 #endif /* RAIL_H */