OpenTTD Source: src/engine_base.h Source File

OpenTTD Source 15.3

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
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"
18#include "timer/timer_game_calendar.h"
19
20 struct WagonOverride {
21 std::vector<EngineID> engines;
22 CargoType cargo;
23 const SpriteGroup *group;
24};
25
27 enum class EngineDisplayFlag : uint8_t {
28 HasVariants,
29 IsFolded,
30 Shaded,
31};
32
33using EngineDisplayFlags = EnumBitSet<EngineDisplayFlag, uint8_t>;
34
35typedef Pool<Engine, EngineID, 64> EnginePool;
36extern EnginePool _engine_pool;
37
38 class Engine : public EnginePool::PoolItem<&_engine_pool> {
39public:
40 CompanyMask company_avail{};
41 CompanyMask company_hidden{};
42 CompanyMask preview_asked{};
43
44 std::string name{};
45
46 TimerGameCalendar::Date intro_date{};
47 int32_t age = 0;
48
49 uint16_t reliability = 0;
50 uint16_t reliability_spd_dec = 0;
51 uint16_t reliability_start = 0;
52 uint16_t reliability_max = 0;
53 uint16_t reliability_final = 0;
54 uint16_t duration_phase_1 = 0;
55 uint16_t duration_phase_2 = 0;
56 uint16_t duration_phase_3 = 0;
57 EngineFlags flags{};
58
59 CompanyID preview_company = CompanyID::Invalid();
60 uint8_t preview_wait = 0;
61 uint8_t original_image_index = 0;
62 VehicleType type = VEH_INVALID;
63
64 EngineDisplayFlags display_flags{};
65 EngineID display_last_variant = EngineID::Invalid();
66 EngineInfo info{};
67
68 uint16_t list_position = 0;
69
70 /* NewGRF related data */
71 CargoGRFFileProps grf_prop{};
72 std::vector<WagonOverride> overrides{};
73 std::vector<BadgeID> badges{};
74
75private:
76 /* Vehicle-type specific information. */
77 std::variant<std::monostate, RailVehicleInfo, RoadVehicleInfo, ShipVehicleInfo, AircraftVehicleInfo> vehicle_info{};
78
79public:
80 Engine() {}
81 Engine(VehicleType type, uint16_t local_id);
82 bool IsEnabled() const;
83
95 CargoType GetDefaultCargoType() const
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;
126 TimerGameCalendar::Date GetLifeLengthInDays() const;
127 uint16_t GetRange() const;
128 StringID GetAircraftTypeText() 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 == VEH_TRAIN || this->type == VEH_ROAD;
159 }
160
166 const GRFFile *GetGRF() const
167 {
168 return this->grf_prop.grffile;
169 }
170
171 uint32_t GetGRFID() const;
172
173 struct EngineTypeFilter {
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
197 static Pool::IterateWrapperFiltered<Engine, EngineTypeFilter> IterateType(VehicleType vt, size_t from = 0)
198 {
199 return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
200 }
201};
202
203 struct EngineIDMapping {
204 uint32_t grfid = 0;
205 uint16_t internal_id = 0;
206 VehicleType type{};
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)
216 : grfid(grfid), internal_id(internal_id), type(type), substitute_id(substitute_id), engine(engine) {}
217};
218
220 struct EngineIDMappingKeyProjection {
221 inline uint64_t operator()(const EngineIDMapping &eid) const { return eid.Key(); }
222};
223
228 struct EngineOverrideManager {
229 std::array<std::vector<EngineIDMapping>, VEH_COMPANY_END> mappings;
230
231 void ResetToDefaultMapping();
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
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
CompanyMask
Definition company_type.h:43
Engine
Definition engine_base.h:38
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition engine.cpp:466
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition engine.cpp:389
Engine::GetDisplayVariant
const Engine * GetDisplayVariant() const
Get the last display variant for an engine.
Definition engine_base.h:144
Engine::GetRange
uint16_t GetRange() const
Get the range of an aircraft type.
Definition engine.cpp:452
Engine::GetGRFID
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition engine.cpp:156
Engine::reliability_spd_dec
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:50
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition engine.cpp:317
Engine::reliability_start
uint16_t reliability_start
Initial reliability of the engine.
Definition engine_base.h:51
Engine::intro_date
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition engine_base.h:46
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition engine_base.h:166
Engine::IterateType
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
Definition engine_base.h:197
Engine::grf_prop
CargoGRFFileProps grf_prop
Link to NewGRF.
Definition engine_base.h:71
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition engine.cpp:357
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:200
Engine::display_flags
EngineDisplayFlags display_flags
NOSAVE client-side-only display flags for build engine list.
Definition engine_base.h:64
Engine::flags
EngineFlags flags
Flags of the engine.
Definition engine_base.h:57
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition engine_base.h:40
Engine::reliability_max
uint16_t reliability_max
Maximal reliability of the engine.
Definition engine_base.h:52
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition engine.cpp:407
Engine::original_image_index
uint8_t original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition engine_base.h:61
Engine::IsEnabled
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition engine.cpp:146
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition engine_base.h:62
Engine::IsVariantHidden
bool IsVariantHidden(CompanyID c) const
Check whether the engine variant chain is hidden in the GUI for the given company.
Definition engine.cpp:486
Engine::reliability_final
uint16_t reliability_final
Final reliability of the engine.
Definition engine_base.h:53
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview CompanyID::Invalid() means no company.
Definition engine_base.h:59
Engine::GetLifeLengthInDays
TimerGameCalendar::Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition engine.cpp:442
Engine::GetDisplayDefaultCapacity
uint GetDisplayDefaultCapacity(uint16_t *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
Definition engine_base.h:115
Engine::duration_phase_3
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition engine_base.h:56
Engine::duration_phase_2
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition engine_base.h:55
Engine::preview_wait
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition engine_base.h:60
Engine::GetDefaultCargoType
CargoType GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition engine_base.h:95
Engine::company_hidden
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company.
Definition engine_base.h:41
Engine::GetRunningCost
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition engine.cpp:280
Engine::reliability
uint16_t reliability
Current reliability of the engine.
Definition engine_base.h:49
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition engine_base.h:42
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition engine.cpp:425
Engine::age
int32_t age
Age of the engine in months.
Definition engine_base.h:47
Engine::IsHidden
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
Definition engine_base.h:135
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition engine.cpp:167
Engine::name
std::string name
Custom name of engine.
Definition engine_base.h:44
Engine::display_last_variant
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition engine_base.h:65
Engine::duration_phase_1
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition engine_base.h:54
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition engine_base.h:156
EnumBitSet< EngineDisplayFlag, uint8_t >
OverflowSafeInt< int64_t >
EngineDisplayFlag
EngineDisplayFlag
Flags used client-side in the purchase/autorenew engine list.
Definition engine_base.h:27
EngineDisplayFlag::HasVariants
@ HasVariants
Set if engine has variants.
EngineDisplayFlag::IsFolded
@ IsFolded
Set if display of variants should be folded (hidden).
EngineDisplayFlag::Shaded
@ Shaded
Set if engine should be masked.
engine_type.h
Types related to engines.
enum_type.hpp
Type (helpers) for enums.
newgrf_commons.h
This file simplifies and embeds a common mechanism of loading/saving and mapping of grf entities.
pool_type.hpp
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition engine_type.h:132
CargoGRFFileProps
Sprite groups indexed by CargoType.
Definition newgrf_commons.h:455
EngineIDMappingKeyProjection
Projection to get a unique key of an EngineIDMapping, used for sorting in EngineOverrideManager.
Definition engine_base.h:220
EngineIDMapping
Definition engine_base.h:203
EngineIDMapping::type
VehicleType type
The engine type.
Definition engine_base.h:206
EngineIDMapping::grfid
uint32_t grfid
The GRF ID of the file the entity belongs to.
Definition engine_base.h:204
EngineIDMapping::substitute_id
uint8_t substitute_id
The (original) entity ID to use if this GRF is not available (currently not used)
Definition engine_base.h:207
EngineIDMapping::internal_id
uint16_t internal_id
The internal ID within the GRF file.
Definition engine_base.h:205
EngineInfo
Information about a vehicle.
Definition engine_type.h:198
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition engine_base.h:228
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition engine.cpp:584
EngineOverrideManager::ResetToDefaultMapping
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition engine.cpp:508
EngineOverrideManager::UseUnreservedID
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:548
EngineOverrideManager::GetID
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition engine.cpp:529
Engine::EngineTypeFilter
Definition engine_base.h:173
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition newgrf_commons.h:318
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::IterateWrapperFiltered
Definition pool_type.hpp:271
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_engine_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool
Base class for all pools.
Definition pool_type.hpp:133
RailVehicleInfo
Information about a rail vehicle.
Definition engine_type.h:74
RoadVehicleInfo
Information about a road vehicle.
Definition engine_type.h:146
ShipVehicleInfo
Information about a ship vehicle.
Definition engine_type.h:99
SpriteGroup
Definition newgrf_spritegroup.h:47
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
WagonOverride
Definition engine_base.h:20
timer_game_calendar.h
Definition of the game-calendar-timer.
vehicle_type.h
Types related to vehicles.
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
VEH_INVALID
@ VEH_INVALID
Non-existing type of vehicle.
Definition vehicle_type.h:36
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition vehicle_type.h:26
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition vehicle_type.h:25
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition vehicle_type.h:30