engine_base.h

Go to the documentation of this file.
00001 /* $Id: engine_base.h 24900 2013-01-08 22:46:42Z planetmaker $ */
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 ENGINE_BASE_H
00013 #define ENGINE_BASE_H
00014 
00015 #include "engine_type.h"
00016 #include "vehicle_type.h"
00017 #include "core/pool_type.hpp"
00018 #include "newgrf_commons.h"
00019 
00020 typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
00021 extern EnginePool _engine_pool;
00022 
00023 struct Engine : EnginePool::PoolItem<&_engine_pool> {
00024   char *name;                 
00025   Date intro_date;            
00026   Date age;
00027   uint16 reliability;         
00028   uint16 reliability_spd_dec; 
00029   uint16 reliability_start;   
00030   uint16 reliability_max;     
00031   uint16 reliability_final;   
00032   uint16 duration_phase_1;    
00033   uint16 duration_phase_2;    
00034   uint16 duration_phase_3;    
00035   byte flags;                 
00036   CompanyMask preview_asked;  
00037   CompanyByte preview_company;
00038   byte preview_wait;          
00039   CompanyMask company_avail;  
00040   uint8 original_image_index; 
00041   VehicleType type;           
00042 
00043   EngineInfo info;
00044 
00045   union {
00046     RailVehicleInfo rail;
00047     RoadVehicleInfo road;
00048     ShipVehicleInfo ship;
00049     AircraftVehicleInfo air;
00050   } u;
00051 
00052   /* NewGRF related data */
00059   GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
00060   uint16 overrides_count;
00061   struct WagonOverride *overrides;
00062   uint16 list_position;
00063 
00064   Engine();
00065   Engine(VehicleType type, EngineID base);
00066   ~Engine();
00067   bool IsEnabled() const;
00068 
00080   CargoID GetDefaultCargoType() const
00081   {
00082     return this->info.cargo_type;
00083   }
00084 
00085   uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = NULL) const;
00086 
00087   bool CanCarryCargo() const;
00088 
00100   uint GetDisplayDefaultCapacity(uint16 *mail_capacity = NULL) const
00101   {
00102     return this->DetermineCapacity(NULL, mail_capacity);
00103   }
00104 
00105   Money GetRunningCost() const;
00106   Money GetCost() const;
00107   uint GetDisplayMaxSpeed() const;
00108   uint GetPower() const;
00109   uint GetDisplayWeight() const;
00110   uint GetDisplayMaxTractiveEffort() const;
00111   Date GetLifeLengthInDays() const;
00112   uint16 GetRange() const;
00113 
00118   inline bool IsGroundVehicle() const
00119   {
00120     return this->type == VEH_TRAIN || this->type == VEH_ROAD;
00121   }
00122 
00128   const GRFFile *GetGRF() const
00129   {
00130     return this->grf_prop.grffile;
00131   }
00132 
00133   uint32 GetGRFID() const;
00134 };
00135 
00136 struct EngineIDMapping {
00137   uint32 grfid;          
00138   uint16 internal_id;    
00139   VehicleTypeByte type;  
00140   uint8  substitute_id;  
00141 };
00142 
00147 struct EngineOverrideManager : SmallVector<EngineIDMapping, 256> {
00148   static const uint NUM_DEFAULT_ENGINES; 
00149 
00150   void ResetToDefaultMapping();
00151   EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
00152 
00153   static bool ResetToCurrentNewGRFConfig();
00154 };
00155 
00156 extern EngineOverrideManager _engine_mngr;
00157 
00158 #define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start)
00159 #define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0)
00160 
00161 #define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type)
00162 
00163 static inline const EngineInfo *EngInfo(EngineID e)
00164 {
00165   return &Engine::Get(e)->info;
00166 }
00167 
00168 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
00169 {
00170   return &Engine::Get(e)->u.rail;
00171 }
00172 
00173 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
00174 {
00175   return &Engine::Get(e)->u.road;
00176 }
00177 
00178 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
00179 {
00180   return &Engine::Get(e)->u.ship;
00181 }
00182 
00183 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
00184 {
00185   return &Engine::Get(e)->u.air;
00186 }
00187 
00188 #endif /* ENGINE_BASE_H */