OpenTTD Source 20260721-master-g25ec12c62d
engine_base.h
Go to the documentation of this file.
1/*
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef ENGINE_BASE_H
11#define ENGINE_BASE_H
12
13#include "engine_type.h"
14#include "newgrf.h"
15#include "vehicle_type.h"
16#include "core/enum_type.hpp"
17#include "core/pool_type.hpp"
18#include "newgrf_commons.h"
20
22 std::vector<EngineID> engines;
23 CargoType cargo;
24 const SpriteGroup *group;
25};
26
33
36
37typedef Pool<Engine, EngineID, 64> EnginePool;
38extern EnginePool _engine_pool;
39
40class Engine : public EnginePool::PoolItem<&_engine_pool> {
41public:
45
46 std::string name{};
47
49 int32_t age = 0;
50
51 uint16_t reliability = 0;
52 uint16_t reliability_spd_dec = 0;
53 uint16_t reliability_start = 0;
54 uint16_t reliability_max = 0;
55 uint16_t reliability_final = 0;
56 uint16_t duration_phase_1 = 0;
57 uint16_t duration_phase_2 = 0;
58 uint16_t duration_phase_3 = 0;
60
61 CompanyID preview_company = CompanyID::Invalid();
62 uint8_t preview_wait = 0;
65
67 EngineID display_last_variant = EngineID::Invalid();
68 EngineInfo info{};
69
70 uint16_t list_position = 0;
71
72 /* NewGRF related data */
74 std::vector<WagonOverride> overrides{};
75 std::vector<BadgeID> badges{};
76
77private:
79 std::variant<std::monostate, RailVehicleInfo, RoadVehicleInfo, ShipVehicleInfo, AircraftVehicleInfo> vehicle_info{};
80
81public:
82 Engine(EngineID index, VehicleType type, uint16_t local_id);
83 bool IsEnabled() const;
84
97 {
98 return this->info.cargo_type;
99 }
100
101 uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity = nullptr) const;
102
103 bool CanCarryCargo() const;
104
116 uint GetDisplayDefaultCapacity(uint16_t *mail_capacity = nullptr) const
117 {
118 return this->DetermineCapacity(nullptr, mail_capacity);
119 }
120
121 Money GetRunningCost() const;
122 Money GetCost() const;
123 uint GetDisplayMaxSpeed() const;
124 uint GetPower() const;
125 uint GetDisplayWeight() const;
126 uint GetDisplayMaxTractiveEffort() const;
128 uint16_t GetRange() const;
130
136 inline bool IsHidden(CompanyID c) const
137 {
138 return c < MAX_COMPANIES && this->company_hidden.Test(c);
139 }
140
145 const Engine *GetDisplayVariant() const
146 {
147 if (this->display_last_variant == this->index || this->display_last_variant == EngineID::Invalid()) return this;
148 return Engine::Get(this->display_last_variant);
149 }
150
151 bool IsVariantHidden(CompanyID c) const;
152
157 inline bool IsGroundVehicle() const
158 {
159 return this->type == VehicleType::Train || this->type == VehicleType::Road;
160 }
161
167 const GRFFile *GetGRF() const
168 {
169 return this->grf_prop.grffile;
170 }
171
172 GrfID GetGRFID() const;
173
175 VehicleType vt;
176
177 bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
178 };
179
180 template <typename T>
181 inline T &VehInfo()
182 {
183 return std::get<T>(this->vehicle_info);
184 }
185
186 template <typename T>
187 inline const T &VehInfo() const
188 {
189 return std::get<T>(this->vehicle_info);
190 }
191
202};
203
206 uint16_t internal_id = 0;
208 uint8_t substitute_id = 0;
209 EngineID engine{};
210
217 static inline uint64_t Key(GrfID grfid, uint16_t internal_id) { return static_cast<uint64_t>(FlattenNewGRFLabel(grfid)) << 32 | internal_id; }
218
223 inline uint64_t Key() const { return Key(this->grfid, this->internal_id); }
224
227
238};
239
242 inline uint64_t operator()(const EngineIDMapping &eid) const { return eid.Key(); }
243};
244
251
253 EngineID GetID(VehicleType type, uint16_t grf_local_id, GrfID grfid);
254 EngineID UseUnreservedID(VehicleType type, uint16_t grf_local_id, GrfID grfid, bool static_access);
255 void SetID(VehicleType type, uint16_t grf_local_id, GrfID grfid, uint8_t substitute_id, EngineID engine);
256
257 static bool ResetToCurrentNewGRFConfig();
258};
259
260extern EngineOverrideManager _engine_mngr;
261
262inline const EngineInfo *EngInfo(EngineID e)
263{
264 return &Engine::Get(e)->info;
265}
266
267inline const RailVehicleInfo *RailVehInfo(EngineID e)
268{
269 return &Engine::Get(e)->VehInfo<RailVehicleInfo>();
270}
271
272inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
273{
274 return &Engine::Get(e)->VehInfo<RoadVehicleInfo>();
275}
276
277inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
278{
279 return &Engine::Get(e)->VehInfo<ShipVehicleInfo>();
280}
281
282inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
283{
284 return &Engine::Get(e)->VehInfo<AircraftVehicleInfo>();
285}
286
287#endif /* ENGINE_BASE_H */
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition engine.cpp:493
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition engine.cpp:415
const Engine * GetDisplayVariant() const
Get the last display variant for an engine.
uint16_t GetRange() const
Get the range of an aircraft type.
Definition engine.cpp:478
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:52
Money GetCost() const
Return how much a new engine costs.
Definition engine.cpp:343
uint16_t reliability_start
Initial reliability of the engine.
Definition engine_base.h:53
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition engine_base.h:48
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
CargoGRFFileProps grf_prop
Link to NewGRF.
Definition engine_base.h:73
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition engine.cpp:383
GrfID GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition engine.cpp:182
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:226
EngineDisplayFlags display_flags
NOSAVE client-side-only display flags for build engine list.
Definition engine_base.h:66
EngineFlags flags
Flags of the engine.
Definition engine_base.h:59
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition engine_base.h:42
uint16_t reliability_max
Maximal reliability of the engine.
Definition engine_base.h:54
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition engine.cpp:433
uint8_t original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition engine_base.h:63
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition engine.cpp:172
VehicleType type
Vehicle type, ie VehicleType::Road, VehicleType::Train, etc.
Definition engine_base.h:64
bool IsVariantHidden(CompanyID c) const
Check whether the engine variant chain is hidden in the GUI for the given company.
Definition engine.cpp:513
uint16_t reliability_final
Final reliability of the engine.
Definition engine_base.h:55
CompanyID preview_company
Company which is currently being offered a preview CompanyID::Invalid() means no company.
Definition engine_base.h:61
TimerGameCalendar::Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition engine.cpp:468
uint GetDisplayDefaultCapacity(uint16_t *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition engine_base.h:58
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition engine_base.h:57
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition engine_base.h:62
CargoType GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition engine_base.h:96
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company.
Definition engine_base.h:43
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition engine.cpp:306
uint16_t reliability
Current reliability of the engine.
Definition engine_base.h:51
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition engine_base.h:44
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition engine.cpp:451
int32_t age
Age of the engine in months.
Definition engine_base.h:49
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition engine.cpp:193
std::string name
Custom name of engine.
Definition engine_base.h:46
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition engine_base.h:67
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition engine_base.h:56
std::variant< std::monostate, RailVehicleInfo, RoadVehicleInfo, ShipVehicleInfo, AircraftVehicleInfo > vehicle_info
Vehicle-type specific information.
Definition engine_base.h:79
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Enum-as-bit-set wrapper.
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
EnumBitSet< EngineDisplayFlag, uint8_t > EngineDisplayFlags
Bitset of EngineDisplayFlag elements.
Definition engine_base.h:35
EngineDisplayFlag
Flags used client-side in the purchase/autorenew engine list.
Definition engine_base.h:28
@ HasVariants
Set if engine has variants.
Definition engine_base.h:29
@ IsFolded
Set if display of variants should be folded (hidden).
Definition engine_base.h:30
@ Shaded
Set if engine should be masked.
Definition engine_base.h:31
Types related to engines.
EnumBitSet< EngineFlag, uint8_t > EngineFlags
Bitset of EngineFlag elements.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
#define T
Climate temperate.
Definition engines.h:91
Type (helpers) for enums.
Base for the NewGRF implementation.
constexpr uint32_t FlattenNewGRFLabel(T label)
Flatten a NewGRF related label to a 32 bits integer.
Definition newgrf.h:259
This file simplifies and embeds a common mechanism of loading/saving and mapping of grf entities.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Information about a aircraft vehicle.
Sprite groups indexed by CargoType.
Projection to get a unique key of an EngineIDMapping, used for sorting in EngineOverrideManager.
static uint64_t Key(GrfID grfid, uint16_t internal_id)
Create a 64 bit key from the GRFID and internal ID for mappings.
VehicleType type
The engine type.
EngineIDMapping(GrfID grfid, uint16_t internal_id, VehicleType type, uint8_t substitute_id, EngineID engine)
Create the mapping.
EngineIDMapping()
Create a new mapping.
uint64_t Key() const
Create a 64 bit key from this mapping.
GrfID grfid
The GRF ID of the file the entity belongs to.
uint8_t substitute_id
The (original) entity ID to use if this GRF is not available (currently not used).
uint16_t internal_id
The internal ID within the GRF file.
Information about a vehicle.
Stores the mapping of EngineID to the internal id of newgrfs.
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition engine.cpp:619
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition engine.cpp:535
EngineID GetID(VehicleType type, uint16_t grf_local_id, GrfID grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition engine.cpp:556
void SetID(VehicleType type, uint16_t grf_local_id, GrfID grfid, uint8_t substitute_id, EngineID engine)
Create an override to the given engine.
Definition engine.cpp:602
EngineID UseUnreservedID(VehicleType type, uint16_t grf_local_id, GrfID grfid, bool static_access)
Look for an unreserved EngineID matching the local id, and reserve it if found.
Definition engine.cpp:575
const struct GRFFile * grffile
grf file that introduced this entity
Dynamic data of a loaded NewGRF.
Definition newgrf.h:124
static Engine * Get(auto index)
Base class for all pools.
Information about a rail vehicle.
Definition engine_type.h:74
Information about a road vehicle.
Information about a ship vehicle.
Definition engine_type.h:99
Common wrapper for all the different sprite group types.
Vehicle data structure.
Definition of the game-calendar-timer.
Types related to vehicles.
VehicleType
Available vehicle types.
@ Invalid
Non-existing type of vehicle.
@ Road
Road vehicle type.
@ Train
Train vehicle type.
EnumIndexArray< T, VehicleType, Tend > VehicleTypeIndexArray
Array with VehicleType as index.