1 /* $Id: engine_base.h 24900 2013-01-08 22:46:42Z planetmaker $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file engine_base.h Base class for engines. */
15 #include "engine_type.h"
16 #include "vehicle_type.h"
17 #include "core/pool_type.hpp"
18 #include "newgrf_commons.h"
20 typedef Pool
<Engine
, EngineID
, 64, 64000> EnginePool
;
21 extern EnginePool _engine_pool
;
23 struct Engine
: EnginePool::PoolItem
<&_engine_pool
> {
24 char *name
; ///< Custom name of engine.
25 Date intro_date
; ///< Date of introduction of the engine.
27 uint16 reliability
; ///< Current reliability of the engine.
28 uint16 reliability_spd_dec
; ///< Speed of reliability decay between services (per day).
29 uint16 reliability_start
; ///< Initial reliability of the engine.
30 uint16 reliability_max
; ///< Maximal reliability of the engine.
31 uint16 reliability_final
; ///< Final reliability of the engine.
32 uint16 duration_phase_1
; ///< First reliability phase in months, increasing reliability from #reliability_start to #reliability_max.
33 uint16 duration_phase_2
; ///< Second reliability phase in months, keeping #reliability_max.
34 uint16 duration_phase_3
; ///< Third reliability phase on months, decaying to #reliability_final.
35 byte flags
; ///< Flags of the engine. @see EngineFlags
36 CompanyMask preview_asked
; ///< Bit for each company which has already been offered a preview.
37 CompanyByte preview_company
;///< Company which is currently being offered a preview \c INVALID_COMPANY means no company.
38 byte preview_wait
; ///< Daily countdown timer for timeout of offering the engine to the #preview_company company.
39 CompanyMask company_avail
; ///< Bit for each company whether the engine is available for that company.
40 CompanyMask company_hidden
; ///< Bit for each company whether the engine is normally hidden in the build gui for that company.
41 uint8 original_image_index
; ///< Original vehicle image index, thus the image index of the overridden vehicle
42 VehicleType type
; ///< %Vehicle type, ie #VEH_ROAD, #VEH_TRAIN, etc.
50 AircraftVehicleInfo air
;
53 /* NewGRF related data */
55 * Properties related the the grf file.
56 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
57 * Used for obtaining the sprite offset of custom sprites, and for
58 * evaluating callbacks.
60 GRFFilePropsBase
<NUM_CARGO
+ 2> grf_prop
;
61 uint16 overrides_count
;
62 struct WagonOverride
*overrides
;
66 Engine(VehicleType type
, EngineID base
);
68 bool IsEnabled() const;
71 * Determines the default cargo type of an engine.
73 * Usually a valid cargo is returned, even though the vehicle has zero capacity, and can therefore not carry anything. But the cargotype is still used
74 * for livery selection etc..
76 * Vehicles with CT_INVALID as default cargo are usually not available, but it can appear as default cargo of articulated parts.
78 * @return The default cargo type.
81 CargoID
GetDefaultCargoType() const
83 return this->info
.cargo_type
;
86 uint
DetermineCapacity(const Vehicle
*v
, uint16
*mail_capacity
= NULL
) const;
88 bool CanCarryCargo() const;
91 * Determines the default cargo capacity of an engine for display purposes.
93 * For planes carrying both passenger and mail this is the passenger capacity.
94 * For multiheaded engines this is the capacity of both heads.
95 * For articulated engines use GetCapacityOfArticulatedParts
97 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
98 * @return The default capacity
99 * @see GetDefaultCargoType
101 uint
GetDisplayDefaultCapacity(uint16
*mail_capacity
= NULL
) const
103 return this->DetermineCapacity(NULL
, mail_capacity
);
106 Money
GetRunningCost() const;
107 Money
GetDisplayRunningCost() const;
108 Money
GetCost() const;
109 uint
GetDisplayMaxSpeed() const;
110 uint
GetPower() const;
111 uint
GetDisplayWeight() const;
112 uint
GetDisplayMaxTractiveEffort() const;
113 Date
GetLifeLengthInDays() const;
114 uint16
GetRange() const;
115 StringID
GetAircraftTypeText() const;
118 * Check whether the engine is hidden in the GUI for the given company.
119 * @param c Company to check.
120 * @return \c true iff the engine is hidden in the GUI for the given company.
122 inline bool IsHidden(CompanyByte c
) const
124 return c
< MAX_COMPANIES
&& HasBit(this->company_hidden
, c
);
128 * Check if the engine is a ground vehicle.
129 * @return True iff the engine is a train or a road vehicle.
131 inline bool IsGroundVehicle() const
133 return this->type
== VEH_TRAIN
|| this->type
== VEH_ROAD
;
137 * Retrieve the NewGRF the engine is tied to.
138 * This is the GRF providing the Action 3.
139 * @return NewGRF associated to the engine.
141 const GRFFile
*GetGRF() const
143 return this->grf_prop
.grffile
;
146 uint32
GetGRFID() const;
149 struct EngineIDMapping
{
150 uint32 grfid
; ///< The GRF ID of the file the entity belongs to
151 uint16 internal_id
; ///< The internal ID within the GRF file
152 VehicleTypeByte type
; ///< The engine type
153 uint8 substitute_id
; ///< The (original) entity ID to use if this GRF is not available (currently not used)
157 * Stores the mapping of EngineID to the internal id of newgrfs.
158 * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
160 struct EngineOverrideManager
: SmallVector
<EngineIDMapping
, 256> {
161 static const uint NUM_DEFAULT_ENGINES
; ///< Number of default entries
163 void ResetToDefaultMapping();
164 EngineID
GetID(VehicleType type
, uint16 grf_local_id
, uint32 grfid
);
166 static bool ResetToCurrentNewGRFConfig();
169 extern EngineOverrideManager _engine_mngr
;
171 #define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start)
172 #define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0)
174 #define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type)
176 static inline const EngineInfo
*EngInfo(EngineID e
)
178 return &Engine::Get(e
)->info
;
181 static inline const RailVehicleInfo
*RailVehInfo(EngineID e
)
183 return &Engine::Get(e
)->u
.rail
;
186 static inline const RoadVehicleInfo
*RoadVehInfo(EngineID e
)
188 return &Engine::Get(e
)->u
.road
;
191 static inline const ShipVehicleInfo
*ShipVehInfo(EngineID e
)
193 return &Engine::Get(e
)->u
.ship
;
196 static inline const AircraftVehicleInfo
*AircraftVehInfo(EngineID e
)
198 return &Engine::Get(e
)->u
.air
;
201 #endif /* ENGINE_BASE_H */