2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file engine_base.h Base class for engines. */
13 #include "engine_type.h"
14 #include "vehicle_type.h"
15 #include "core/pool_type.hpp"
16 #include "newgrf_commons.h"
18 struct WagonOverride
{
19 std::vector
<EngineID
> engines
;
21 const SpriteGroup
*group
;
24 typedef Pool
<Engine
, EngineID
, 64, 64000> EnginePool
;
25 extern EnginePool _engine_pool
;
27 struct Engine
: EnginePool::PoolItem
<&_engine_pool
> {
28 std::string name
; ///< Custom name of engine.
29 Date intro_date
; ///< Date of introduction of the engine.
31 uint16 reliability
; ///< Current reliability of the engine.
32 uint16 reliability_spd_dec
; ///< Speed of reliability decay between services (per day).
33 uint16 reliability_start
; ///< Initial reliability of the engine.
34 uint16 reliability_max
; ///< Maximal reliability of the engine.
35 uint16 reliability_final
; ///< Final reliability of the engine.
36 uint16 duration_phase_1
; ///< First reliability phase in months, increasing reliability from #reliability_start to #reliability_max.
37 uint16 duration_phase_2
; ///< Second reliability phase in months, keeping #reliability_max.
38 uint16 duration_phase_3
; ///< Third reliability phase in months, decaying to #reliability_final.
39 byte flags
; ///< Flags of the engine. @see EngineFlags
40 CompanyMask preview_asked
; ///< Bit for each company which has already been offered a preview.
41 CompanyID preview_company
; ///< Company which is currently being offered a preview \c INVALID_COMPANY means no company.
42 byte preview_wait
; ///< Daily countdown timer for timeout of offering the engine to the #preview_company company.
43 CompanyMask company_avail
; ///< Bit for each company whether the engine is available for that company.
44 CompanyMask company_hidden
; ///< Bit for each company whether the engine is normally hidden in the build gui for that company.
45 uint8 original_image_index
; ///< Original vehicle image index, thus the image index of the overridden vehicle
46 VehicleType type
; ///< %Vehicle type, ie #VEH_ROAD, #VEH_TRAIN, etc.
54 AircraftVehicleInfo air
;
57 /* NewGRF related data */
59 * Properties related the the grf file.
60 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
61 * Used for obtaining the sprite offset of custom sprites, and for
62 * evaluating callbacks.
64 GRFFilePropsBase
<NUM_CARGO
+ 2> grf_prop
;
65 std::vector
<WagonOverride
> overrides
;
69 Engine(VehicleType type
, EngineID base
);
70 bool IsEnabled() const;
73 * Determines the default cargo type of an engine.
75 * 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
76 * for livery selection etc..
78 * Vehicles with CT_INVALID as default cargo are usually not available, but it can appear as default cargo of articulated parts.
80 * @return The default cargo type.
83 CargoID
GetDefaultCargoType() const
85 return this->info
.cargo_type
;
88 uint
DetermineCapacity(const Vehicle
*v
, uint16
*mail_capacity
= nullptr) const;
90 bool CanCarryCargo() const;
93 * Determines the default cargo capacity of an engine for display purposes.
95 * For planes carrying both passenger and mail this is the passenger capacity.
96 * For multiheaded engines this is the capacity of both heads.
97 * For articulated engines use GetCapacityOfArticulatedParts
99 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
100 * @return The default capacity
101 * @see GetDefaultCargoType
103 uint
GetDisplayDefaultCapacity(uint16
*mail_capacity
= nullptr) const
105 return this->DetermineCapacity(nullptr, mail_capacity
);
108 Money
GetRunningCost() const;
109 Money
GetCost() const;
110 uint
GetDisplayMaxSpeed() const;
111 uint
GetPower() const;
112 uint
GetDisplayWeight() const;
113 uint
GetDisplayMaxTractiveEffort() const;
114 Date
GetLifeLengthInDays() const;
115 uint16
GetRange() const;
116 StringID
GetAircraftTypeText() const;
119 * Check whether the engine is hidden in the GUI for the given company.
120 * @param c Company to check.
121 * @return \c true iff the engine is hidden in the GUI for the given company.
123 inline bool IsHidden(CompanyID c
) const
125 return c
< MAX_COMPANIES
&& HasBit(this->company_hidden
, c
);
129 * Check if the engine is a ground vehicle.
130 * @return True iff the engine is a train or a road vehicle.
132 inline bool IsGroundVehicle() const
134 return this->type
== VEH_TRAIN
|| this->type
== VEH_ROAD
;
138 * Retrieve the NewGRF the engine is tied to.
139 * This is the GRF providing the Action 3.
140 * @return NewGRF associated to the engine.
142 const GRFFile
*GetGRF() const
144 return this->grf_prop
.grffile
;
147 uint32
GetGRFID() const;
149 struct EngineTypeFilter
{
152 bool operator() (size_t index
) { return Engine::Get(index
)->type
== this->vt
; }
156 * Returns an iterable ensemble of all valid engines of the given type
157 * @param vt the VehicleType for engines to be valid
158 * @param from index of the first engine to consider
159 * @return an iterable ensemble of all valid engines of the given type
161 static Pool::IterateWrapperFiltered
<Engine
, EngineTypeFilter
> IterateType(VehicleType vt
, size_t from
= 0)
163 return Pool::IterateWrapperFiltered
<Engine
, EngineTypeFilter
>(from
, EngineTypeFilter
{ vt
});
167 struct EngineIDMapping
{
168 uint32 grfid
; ///< The GRF ID of the file the entity belongs to
169 uint16 internal_id
; ///< The internal ID within the GRF file
170 VehicleType type
; ///< The engine type
171 uint8 substitute_id
; ///< The (original) entity ID to use if this GRF is not available (currently not used)
175 * Stores the mapping of EngineID to the internal id of newgrfs.
176 * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
178 struct EngineOverrideManager
: std::vector
<EngineIDMapping
> {
179 static const uint NUM_DEFAULT_ENGINES
; ///< Number of default entries
181 void ResetToDefaultMapping();
182 EngineID
GetID(VehicleType type
, uint16 grf_local_id
, uint32 grfid
);
184 static bool ResetToCurrentNewGRFConfig();
187 extern EngineOverrideManager _engine_mngr
;
189 static inline const EngineInfo
*EngInfo(EngineID e
)
191 return &Engine::Get(e
)->info
;
194 static inline const RailVehicleInfo
*RailVehInfo(EngineID e
)
196 return &Engine::Get(e
)->u
.rail
;
199 static inline const RoadVehicleInfo
*RoadVehInfo(EngineID e
)
201 return &Engine::Get(e
)->u
.road
;
204 static inline const ShipVehicleInfo
*ShipVehInfo(EngineID e
)
206 return &Engine::Get(e
)->u
.ship
;
209 static inline const AircraftVehicleInfo
*AircraftVehInfo(EngineID e
)
211 return &Engine::Get(e
)->u
.air
;
214 #endif /* ENGINE_BASE_H */