OpenTTD Source 20260621-master-g720d10536d
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 "vehicle_type.h"
15#include "core/enum_type.hpp"
16#include "core/pool_type.hpp"
17#include "newgrf_commons.h"
19
21 std::vector<EngineID> engines;
22 CargoType cargo;
23 const SpriteGroup *group;
24};
25
32
35
36typedef Pool<Engine, EngineID, 64> EnginePool;
37extern EnginePool _engine_pool;
38
39class Engine : public EnginePool::PoolItem<&_engine_pool> {
40public:
44
45 std::string name{};
46
48 int32_t age = 0;
49
50 uint16_t reliability = 0;
51 uint16_t reliability_spd_dec = 0;
52 uint16_t reliability_start = 0;
53 uint16_t reliability_max = 0;
54 uint16_t reliability_final = 0;
55 uint16_t duration_phase_1 = 0;
56 uint16_t duration_phase_2 = 0;
57 uint16_t duration_phase_3 = 0;
59
60 CompanyID preview_company = CompanyID::Invalid();
61 uint8_t preview_wait = 0;
64
66 EngineID display_last_variant = EngineID::Invalid();
67 EngineInfo info{};
68
69 uint16_t list_position = 0;
70
71 /* NewGRF related data */
73 std::vector<WagonOverride> overrides{};
74 std::vector<BadgeID> badges{};
75
76private:
78 std::variant<std::monostate, RailVehicleInfo, RoadVehicleInfo, ShipVehicleInfo, AircraftVehicleInfo> vehicle_info{};
79
80public:
81 Engine(EngineID index, VehicleType type, uint16_t local_id);
82 bool IsEnabled() const;
83
96 {
97 return this->info.cargo_type;
98 }
99
100 uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity = nullptr) const;
101
102 bool CanCarryCargo() const;
103
115 uint GetDisplayDefaultCapacity(uint16_t *mail_capacity = nullptr) const
116 {
117 return this->DetermineCapacity(nullptr, mail_capacity);
118 }
119
120 Money GetRunningCost() const;
121 Money GetCost() const;
122 uint GetDisplayMaxSpeed() const;
123 uint GetPower() const;
124 uint GetDisplayWeight() const;
125 uint GetDisplayMaxTractiveEffort() const;
127 uint16_t GetRange() const;
129
135 inline bool IsHidden(CompanyID c) const
136 {
137 return c < MAX_COMPANIES && this->company_hidden.Test(c);
138 }
139
144 const Engine *GetDisplayVariant() const
145 {
146 if (this->display_last_variant == this->index || this->display_last_variant == EngineID::Invalid()) return this;
147 return Engine::Get(this->display_last_variant);
148 }
149
150 bool IsVariantHidden(CompanyID c) const;
151
156 inline bool IsGroundVehicle() const
157 {
158 return this->type == VehicleType::Train || this->type == VehicleType::Road;
159 }
160
166 const GRFFile *GetGRF() const
167 {
168 return this->grf_prop.grffile;
169 }
170
171 uint32_t GetGRFID() const;
172
174 VehicleType vt;
175
176 bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
177 };
178
179 template <typename T>
180 inline T &VehInfo()
181 {
182 return std::get<T>(this->vehicle_info);
183 }
184
185 template <typename T>
186 inline const T &VehInfo() const
187 {
188 return std::get<T>(this->vehicle_info);
189 }
190
201};
202
203struct EngineIDMapping {
204 uint32_t grfid = 0;
205 uint16_t internal_id = 0;
207 uint8_t substitute_id = 0;
208 EngineID engine{};
209
210 static inline uint64_t Key(uint32_t grfid, uint16_t internal_id) { return static_cast<uint64_t>(grfid) << 32 | internal_id; }
211
212 inline uint64_t Key() const { return Key(this->grfid, this->internal_id); }
213
214 EngineIDMapping() {}
215 EngineIDMapping(uint32_t grfid, uint16_t internal_id, VehicleType type, uint8_t substitute_id, EngineID engine)
217};
218
221 inline uint64_t operator()(const EngineIDMapping &eid) const { return eid.Key(); }
222};
223
230
232 EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid);
233 EngineID UseUnreservedID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, bool static_access);
234 void SetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, uint8_t substitute_id, EngineID engine);
235
236 static bool ResetToCurrentNewGRFConfig();
237};
238
239extern EngineOverrideManager _engine_mngr;
240
241inline const EngineInfo *EngInfo(EngineID e)
242{
243 return &Engine::Get(e)->info;
244}
245
246inline const RailVehicleInfo *RailVehInfo(EngineID e)
247{
248 return &Engine::Get(e)->VehInfo<RailVehicleInfo>();
249}
250
251inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
252{
253 return &Engine::Get(e)->VehInfo<RoadVehicleInfo>();
254}
255
256inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
257{
258 return &Engine::Get(e)->VehInfo<ShipVehicleInfo>();
259}
260
261inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
262{
263 return &Engine::Get(e)->VehInfo<AircraftVehicleInfo>();
264}
265
266#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
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition engine.cpp:182
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:51
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:52
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition engine_base.h:47
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:72
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition engine.cpp:383
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:65
EngineFlags flags
Flags of the engine.
Definition engine_base.h:58
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition engine_base.h:41
uint16_t reliability_max
Maximal reliability of the engine.
Definition engine_base.h:53
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:62
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:63
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:54
CompanyID preview_company
Company which is currently being offered a preview CompanyID::Invalid() means no company.
Definition engine_base.h:60
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:57
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition engine_base.h:56
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition engine_base.h:61
CargoType GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition engine_base.h:95
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company.
Definition engine_base.h:42
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:50
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition engine_base.h:43
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:48
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:45
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition engine_base.h:66
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition engine_base.h:55
std::variant< std::monostate, RailVehicleInfo, RoadVehicleInfo, ShipVehicleInfo, AircraftVehicleInfo > vehicle_info
Vehicle-type specific information.
Definition engine_base.h:78
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:34
EngineDisplayFlag
Flags used client-side in the purchase/autorenew engine list.
Definition engine_base.h:27
@ HasVariants
Set if engine has variants.
Definition engine_base.h:28
@ IsFolded
Set if display of variants should be folded (hidden).
Definition engine_base.h:29
@ Shaded
Set if engine should be masked.
Definition engine_base.h:30
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.
This file simplifies and embeds a common mechanism of loading/saving and mapping of grf entities.
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.
VehicleType type
The engine type.
uint32_t 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:611
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition engine.cpp:535
EngineID UseUnreservedID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, bool static_access)
Look for an unreserved EngineID matching the local id, and reserve it if found.
Definition engine.cpp:575
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition engine.cpp:556
const struct GRFFile * grffile
grf file that introduced this entity
Dynamic data of a loaded NewGRF.
Definition newgrf.h:128
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.