engine_base.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 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   uint8 preview_company_rank; 
00037   byte preview_wait;          
00038   CompanyMask company_avail;  
00039   uint8 original_image_index; 
00040   VehicleType type;           
00041 
00042   EngineInfo info;
00043 
00044   union {
00045     RailVehicleInfo rail;
00046     RoadVehicleInfo road;
00047     ShipVehicleInfo ship;
00048     AircraftVehicleInfo air;
00049   } u;
00050 
00051   /* NewGRF related data */
00058   GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
00059   uint16 overrides_count;
00060   struct WagonOverride *overrides;
00061   uint16 list_position;
00062 
00063   Engine();
00064   Engine(VehicleType type, EngineID base);
00065   ~Engine();
00066   bool IsEnabled() const;
00067 
00079   CargoID GetDefaultCargoType() const
00080   {
00081     return this->info.cargo_type;
00082   }
00083 
00084   uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = NULL) const;
00085 
00086   bool CanCarryCargo() const;
00087 
00099   uint GetDisplayDefaultCapacity(uint16 *mail_capacity = NULL) const
00100   {
00101     return this->DetermineCapacity(NULL, mail_capacity);
00102   }
00103 
00104   Money GetRunningCost() const;
00105   Money GetCost() const;
00106   uint GetDisplayMaxSpeed() const;
00107   uint GetPower() const;
00108   uint GetDisplayWeight() const;
00109   uint GetDisplayMaxTractiveEffort() const;
00110   Date GetLifeLengthInDays() const;
00111   uint16 GetRange() const;
00112 
00117   inline bool IsGroundVehicle() const
00118   {
00119     return this->type == VEH_TRAIN || this->type == VEH_ROAD;
00120   }
00121 
00127   const GRFFile *GetGRF() const
00128   {
00129     return this->grf_prop.grffile;
00130   }
00131 
00132   uint32 GetGRFID() const;
00133 };
00134 
00135 struct EngineIDMapping {
00136   uint32 grfid;          
00137   uint16 internal_id;    
00138   VehicleTypeByte type;  
00139   uint8  substitute_id;  
00140 };
00141 
00146 struct EngineOverrideManager : SmallVector<EngineIDMapping, 256> {
00147   static const uint NUM_DEFAULT_ENGINES; 
00148 
00149   void ResetToDefaultMapping();
00150   EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
00151 
00152   static bool ResetToCurrentNewGRFConfig();
00153 };
00154 
00155 extern EngineOverrideManager _engine_mngr;
00156 
00157 #define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start)
00158 #define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0)
00159 
00160 #define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type)
00161 
00162 static inline const EngineInfo *EngInfo(EngineID e)
00163 {
00164   return &Engine::Get(e)->info;
00165 }
00166 
00167 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
00168 {
00169   return &Engine::Get(e)->u.rail;
00170 }
00171 
00172 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
00173 {
00174   return &Engine::Get(e)->u.road;
00175 }
00176 
00177 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
00178 {
00179   return &Engine::Get(e)->u.ship;
00180 }
00181 
00182 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
00183 {
00184   return &Engine::Get(e)->u.air;
00185 }
00186 
00187 #endif /* ENGINE_BASE_H */