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"
17 #include "timer/timer_game_calendar.h"
19 struct WagonOverride
{
20 std::vector
<EngineID
> engines
;
22 const SpriteGroup
*group
;
25 /** Flags used client-side in the purchase/autorenew engine list. */
26 enum class EngineDisplayFlags
: uint8_t {
27 None
= 0, ///< No flag set.
28 HasVariants
= (1U << 0), ///< Set if engine has variants.
29 IsFolded
= (1U << 1), ///< Set if display of variants should be folded (hidden).
30 Shaded
= (1U << 2), ///< Set if engine should be masked.
32 DECLARE_ENUM_AS_BIT_SET(EngineDisplayFlags
)
34 typedef Pool
<Engine
, EngineID
, 64, 64000> EnginePool
;
35 extern EnginePool _engine_pool
;
37 struct Engine
: EnginePool::PoolItem
<&_engine_pool
> {
38 std::string name
; ///< Custom name of engine.
39 TimerGameCalendar::Date intro_date
; ///< Date of introduction of the engine.
40 int32_t age
; ///< Age of the engine in months.
41 uint16_t reliability
; ///< Current reliability of the engine.
42 uint16_t reliability_spd_dec
; ///< Speed of reliability decay between services (per day).
43 uint16_t reliability_start
; ///< Initial reliability of the engine.
44 uint16_t reliability_max
; ///< Maximal reliability of the engine.
45 uint16_t reliability_final
; ///< Final reliability of the engine.
46 uint16_t duration_phase_1
; ///< First reliability phase in months, increasing reliability from #reliability_start to #reliability_max.
47 uint16_t duration_phase_2
; ///< Second reliability phase in months, keeping #reliability_max.
48 uint16_t duration_phase_3
; ///< Third reliability phase in months, decaying to #reliability_final.
49 uint8_t flags
; ///< Flags of the engine. @see EngineFlags
50 CompanyMask preview_asked
; ///< Bit for each company which has already been offered a preview.
51 CompanyID preview_company
; ///< Company which is currently being offered a preview \c INVALID_COMPANY means no company.
52 uint8_t preview_wait
; ///< Daily countdown timer for timeout of offering the engine to the #preview_company company.
53 CompanyMask company_avail
; ///< Bit for each company whether the engine is available for that company.
54 CompanyMask company_hidden
; ///< Bit for each company whether the engine is normally hidden in the build gui for that company.
55 uint8_t original_image_index
; ///< Original vehicle image index, thus the image index of the overridden vehicle
56 VehicleType type
; ///< %Vehicle type, ie #VEH_ROAD, #VEH_TRAIN, etc.
58 EngineDisplayFlags display_flags
; ///< NOSAVE client-side-only display flags for build engine list.
59 EngineID display_last_variant
; ///< NOSAVE client-side-only last variant selected.
67 AircraftVehicleInfo air
;
70 /* NewGRF related data */
72 * Properties related the the grf file.
73 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
74 * Used for obtaining the sprite offset of custom sprites, and for
75 * evaluating callbacks.
77 GRFFilePropsBase
<NUM_CARGO
+ 2> grf_prop
;
78 std::vector
<WagonOverride
> overrides
;
79 uint16_t list_position
;
82 Engine(VehicleType type
, EngineID base
);
83 bool IsEnabled() const;
86 * Determines the default cargo type of an engine.
88 * 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
89 * for livery selection etc..
91 * Vehicles with INVALID_CARGO as default cargo are usually not available, but it can appear as default cargo of articulated parts.
93 * @return The default cargo type.
96 CargoID
GetDefaultCargoType() const
98 return this->info
.cargo_type
;
101 uint
DetermineCapacity(const Vehicle
*v
, uint16_t *mail_capacity
= nullptr) const;
103 bool CanCarryCargo() const;
106 * Determines the default cargo capacity of an engine for display purposes.
108 * For planes carrying both passenger and mail this is the passenger capacity.
109 * For multiheaded engines this is the capacity of both heads.
110 * For articulated engines use GetCapacityOfArticulatedParts
112 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
113 * @return The default capacity
114 * @see GetDefaultCargoType
116 uint
GetDisplayDefaultCapacity(uint16_t *mail_capacity
= nullptr) const
118 return this->DetermineCapacity(nullptr, mail_capacity
);
121 Money
GetRunningCost() const;
122 Money
GetCost() const;
123 uint
GetDisplayMaxSpeed() const;
124 uint
GetPower() const;
125 uint
GetDisplayWeight() const;
126 uint
GetDisplayMaxTractiveEffort() const;
127 TimerGameCalendar::Date
GetLifeLengthInDays() const;
128 uint16_t GetRange() const;
129 StringID
GetAircraftTypeText() const;
132 * Check whether the engine is hidden in the GUI for the given company.
133 * @param c Company to check.
134 * @return \c true iff the engine is hidden in the GUI for the given company.
136 inline bool IsHidden(CompanyID c
) const
138 return c
< MAX_COMPANIES
&& HasBit(this->company_hidden
, c
);
142 * Get the last display variant for an engine.
143 * @return Engine's last display variant or engine itself if no last display variant is set.
145 const Engine
*GetDisplayVariant() const
147 if (this->display_last_variant
== this->index
|| this->display_last_variant
== INVALID_ENGINE
) return this;
148 return Engine::Get(this->display_last_variant
);
151 bool IsVariantHidden(CompanyID c
) const;
154 * Check if the engine is a ground vehicle.
155 * @return True iff the engine is a train or a road vehicle.
157 inline bool IsGroundVehicle() const
159 return this->type
== VEH_TRAIN
|| this->type
== VEH_ROAD
;
163 * Retrieve the NewGRF the engine is tied to.
164 * This is the GRF providing the Action 3.
165 * @return NewGRF associated to the engine.
167 const GRFFile
*GetGRF() const
169 return this->grf_prop
.grffile
;
172 uint32_t GetGRFID() const;
174 struct EngineTypeFilter
{
177 bool operator() (size_t index
) { return Engine::Get(index
)->type
== this->vt
; }
181 * Returns an iterable ensemble of all valid engines of the given type
182 * @param vt the VehicleType for engines to be valid
183 * @param from index of the first engine to consider
184 * @return an iterable ensemble of all valid engines of the given type
186 static Pool::IterateWrapperFiltered
<Engine
, EngineTypeFilter
> IterateType(VehicleType vt
, size_t from
= 0)
188 return Pool::IterateWrapperFiltered
<Engine
, EngineTypeFilter
>(from
, EngineTypeFilter
{ vt
});
192 struct EngineIDMapping
{
193 uint32_t grfid
; ///< The GRF ID of the file the entity belongs to
194 uint16_t internal_id
; ///< The internal ID within the GRF file
195 VehicleType type
; ///< The engine type
196 uint8_t substitute_id
; ///< The (original) entity ID to use if this GRF is not available (currently not used)
200 * Stores the mapping of EngineID to the internal id of newgrfs.
201 * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
203 struct EngineOverrideManager
: std::vector
<EngineIDMapping
> {
204 static const uint NUM_DEFAULT_ENGINES
; ///< Number of default entries
206 void ResetToDefaultMapping();
207 EngineID
GetID(VehicleType type
, uint16_t grf_local_id
, uint32_t grfid
);
209 static bool ResetToCurrentNewGRFConfig();
212 extern EngineOverrideManager _engine_mngr
;
214 inline const EngineInfo
*EngInfo(EngineID e
)
216 return &Engine::Get(e
)->info
;
219 inline const RailVehicleInfo
*RailVehInfo(EngineID e
)
221 return &Engine::Get(e
)->u
.rail
;
224 inline const RoadVehicleInfo
*RoadVehInfo(EngineID e
)
226 return &Engine::Get(e
)->u
.road
;
229 inline const ShipVehicleInfo
*ShipVehInfo(EngineID e
)
231 return &Engine::Get(e
)->u
.ship
;
234 inline const AircraftVehicleInfo
*AircraftVehInfo(EngineID e
)
236 return &Engine::Get(e
)->u
.air
;
239 #endif /* ENGINE_BASE_H */