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 #include "core/math_func.hpp"
00024 
00026 enum RailTypeFlags {
00027   RTF_CATENARY          = 0,                           
00028   RTF_NO_LEVEL_CROSSING = 1,                           
00029 
00030   RTFB_NONE              = 0,                          
00031   RTFB_CATENARY          = 1 << RTF_CATENARY,          
00032   RTFB_NO_LEVEL_CROSSING = 1 << RTF_NO_LEVEL_CROSSING, 
00033 };
00034 DECLARE_ENUM_AS_BIT_SET(RailTypeFlags)
00035 
00036 struct SpriteGroup;
00037 
00039 enum RailTypeSpriteGroup {
00040   RTSG_CURSORS,     
00041   RTSG_OVERLAY,     
00042   RTSG_GROUND,      
00043   RTSG_TUNNEL,      
00044   RTSG_WIRES,       
00045   RTSG_PYLONS,      
00046   RTSG_BRIDGE,      
00047   RTSG_CROSSING,    
00048   RTSG_DEPOT,       
00049   RTSG_FENCES,      
00050   RTSG_END,
00051 };
00052 
00057 enum RailTrackOffset {
00058   RTO_X,            
00059   RTO_Y,            
00060   RTO_N,            
00061   RTO_S,            
00062   RTO_E,            
00063   RTO_W,            
00064   RTO_SLOPE_NE,     
00065   RTO_SLOPE_SE,     
00066   RTO_SLOPE_SW,     
00067   RTO_SLOPE_NW,     
00068   RTO_CROSSING_XY,  
00069   RTO_JUNCTION_SW,  
00070   RTO_JUNCTION_NE,  
00071   RTO_JUNCTION_SE,  
00072   RTO_JUNCTION_NW,  
00073   RTO_JUNCTION_NSEW,
00074 };
00075 
00079 enum RailTrackBridgeOffset {
00080   RTBO_X,     
00081   RTBO_Y,     
00082   RTBO_SLOPE, 
00083 };
00084 
00089 enum RailFenceOffset {
00090   RFO_FLAT_X,
00091   RFO_FLAT_Y,
00092   RFO_FLAT_VERT,
00093   RFO_FLAT_HORZ,
00094   RFO_SLOPE_SW,
00095   RFO_SLOPE_SE,
00096   RFO_SLOPE_NE,
00097   RFO_SLOPE_NW,
00098 };
00099 
00103 struct RailtypeInfo {
00108   struct {
00109     SpriteID track_y;      
00110     SpriteID track_ns;     
00111     SpriteID ground;       
00112     SpriteID single_x;     
00113     SpriteID single_y;     
00114     SpriteID single_n;     
00115     SpriteID single_s;     
00116     SpriteID single_e;     
00117     SpriteID single_w;     
00118     SpriteID single_sloped;
00119     SpriteID crossing;     
00120     SpriteID tunnel;       
00121   } base_sprites;
00122 
00127   struct {
00128     SpriteID build_ns_rail;      
00129     SpriteID build_x_rail;       
00130     SpriteID build_ew_rail;      
00131     SpriteID build_y_rail;       
00132     SpriteID auto_rail;          
00133     SpriteID build_depot;        
00134     SpriteID build_tunnel;       
00135     SpriteID convert_rail;       
00136   } gui_sprites;
00137 
00138   struct {
00139     CursorID rail_ns;    
00140     CursorID rail_swne;  
00141     CursorID rail_ew;    
00142     CursorID rail_nwse;  
00143     CursorID autorail;   
00144     CursorID depot;      
00145     CursorID tunnel;     
00146     CursorID convert;    
00147   } cursor;
00148 
00149   struct {
00150     StringID name;
00151     StringID toolbar_caption;
00152     StringID menu_text;
00153     StringID build_caption;
00154     StringID replace_text;
00155     StringID new_loco;
00156   } strings;
00157 
00159   SpriteID snow_offset;
00160 
00162   RailTypes powered_railtypes;
00163 
00165   RailTypes compatible_railtypes;
00166 
00170   SpriteID bridge_offset;
00171 
00175   byte fallback_railtype;
00176 
00180   byte curve_speed;
00181 
00185   RailTypeFlags flags;
00186 
00190   uint16 cost_multiplier;
00191 
00195   uint16 maintenance_multiplier;
00196 
00200   uint8 acceleration_type;
00201 
00205   uint16 max_speed;
00206 
00210   RailTypeLabel label;
00211 
00215   byte map_colour;
00216 
00224   Date introduction_date;
00225 
00230   RailTypes introduction_required_railtypes;
00231 
00235   RailTypes introduces_railtypes;
00236 
00240   byte sorting_order;
00241 
00245   const GRFFile *grffile[RTSG_END];
00246 
00250   const SpriteGroup *group[RTSG_END];
00251 
00252   inline bool UsesOverlay() const
00253   {
00254     return this->group[RTSG_GROUND] != NULL;
00255   }
00256 
00264   inline uint GetRailtypeSpriteOffset() const
00265   {
00266     return 82 * this->fallback_railtype;
00267   }
00268 };
00269 
00270 
00276 static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype)
00277 {
00278   extern RailtypeInfo _railtypes[RAILTYPE_END];
00279   assert(railtype < RAILTYPE_END);
00280   return &_railtypes[railtype];
00281 }
00282 
00291 static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype)
00292 {
00293   return HasBit(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype);
00294 }
00295 
00304 static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype)
00305 {
00306   return HasBit(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype);
00307 }
00308 
00314 static inline bool RailNoLevelCrossings(RailType rt)
00315 {
00316   return HasBit(GetRailTypeInfo(rt)->flags, RTF_NO_LEVEL_CROSSING);
00317 }
00318 
00324 static inline Money RailBuildCost(RailType railtype)
00325 {
00326   assert(railtype < RAILTYPE_END);
00327   return (_price[PR_BUILD_RAIL] * GetRailTypeInfo(railtype)->cost_multiplier) >> 3;
00328 }
00329 
00335 static inline Money RailClearCost(RailType railtype)
00336 {
00337   /* Clearing rail in fact earns money, but if the build cost is set
00338    * very low then a loophole exists where money can be made.
00339    * In this case we limit the removal earnings to 3/4s of the build
00340    * cost.
00341    */
00342   assert(railtype < RAILTYPE_END);
00343   return max(_price[PR_CLEAR_RAIL], -RailBuildCost(railtype) * 3 / 4);
00344 }
00345 
00352 static inline Money RailConvertCost(RailType from, RailType to)
00353 {
00354   /* Get the costs for removing and building anew
00355    * A conversion can never be more costly */
00356   Money rebuildcost = RailBuildCost(to) + RailClearCost(from);
00357 
00358   /* Conversion between somewhat compatible railtypes:
00359    * Pay 1/8 of the target rail cost (labour costs) and additionally any difference in the
00360    * build costs, if the target type is more expensive (material upgrade costs).
00361    * Upgrade can never be more expensive than re-building. */
00362   if (HasPowerOnRail(from, to) || HasPowerOnRail(to, from)) {
00363     Money upgradecost = RailBuildCost(to) / 8 + max((Money)0, RailBuildCost(to) - RailBuildCost(from));
00364     return min(upgradecost, rebuildcost);
00365   }
00366 
00367   /* make the price the same as remove + build new type for rail types
00368    * which are not compatible in any way */
00369   return rebuildcost;
00370 }
00371 
00378 static inline Money RailMaintenanceCost(RailType railtype, uint32 num)
00379 {
00380   assert(railtype < RAILTYPE_END);
00381   return (_price[PR_INFRASTRUCTURE_RAIL] * GetRailTypeInfo(railtype)->maintenance_multiplier * num * (1 + IntSqrt(num))) >> 11; // 4 bits fraction for the multiplier and 7 bits scaling.
00382 }
00383 
00389 static inline Money SignalMaintenanceCost(uint32 num)
00390 {
00391   return (_price[PR_INFRASTRUCTURE_RAIL] * 15 * num * (1 + IntSqrt(num))) >> 8; // 1 bit fraction for the multiplier and 7 bits scaling.
00392 }
00393 
00394 void DrawTrainDepotSprite(int x, int y, int image, RailType railtype);
00395 int TicksToLeaveDepot(const Train *v);
00396 
00397 Foundation GetRailFoundation(Slope tileh, TrackBits bits);
00398 
00399 
00400 bool HasRailtypeAvail(const CompanyID company, const RailType railtype);
00401 bool ValParamRailtype(const RailType rail);
00402 
00403 RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date);
00404 
00405 RailType GetBestRailtype(const CompanyID company);
00406 RailTypes GetCompanyRailtypes(const CompanyID c);
00407 
00408 RailType GetRailTypeByLabel(RailTypeLabel label);
00409 
00410 void ResetRailTypes();
00411 void InitRailTypes();
00412 RailType AllocateRailType(RailTypeLabel label);
00413 
00414 #endif /* RAIL_H */