OpenTTD Source  20240917-master-g9ab0a47812
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 <http://www.gnu.org/licenses/>.
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/pool_type.hpp"
16 #include "newgrf_commons.h"
18 
19 struct WagonOverride {
20  std::vector<EngineID> engines;
21  CargoID cargo;
22  const SpriteGroup *group;
23 };
24 
26 enum class EngineDisplayFlags : uint8_t {
27  None = 0,
28  HasVariants = (1U << 0),
29  IsFolded = (1U << 1),
30  Shaded = (1U << 2),
31 };
33 
35 extern EnginePool _engine_pool;
36 
37 struct Engine : EnginePool::PoolItem<&_engine_pool> {
38  std::string name;
40  int32_t age;
41  uint16_t reliability;
43  uint16_t reliability_start;
44  uint16_t reliability_max;
45  uint16_t reliability_final;
46  uint16_t duration_phase_1;
47  uint16_t duration_phase_2;
48  uint16_t duration_phase_3;
49  uint8_t flags;
50  CompanyMask preview_asked;
52  uint8_t preview_wait;
53  CompanyMask company_avail;
54  CompanyMask company_hidden;
57 
60 
61  EngineInfo info;
62 
63  union {
64  RailVehicleInfo rail;
65  RoadVehicleInfo road;
66  ShipVehicleInfo ship;
68  } u;
69 
70  /* NewGRF related data */
78  std::vector<WagonOverride> overrides;
79  uint16_t list_position;
80 
81  Engine() {}
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;
127  TimerGameCalendar::Date GetLifeLengthInDays() const;
128  uint16_t GetRange() const;
130 
136  inline bool IsHidden(CompanyID c) const
137  {
138  return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
139  }
140 
145  const Engine *GetDisplayVariant() const
146  {
147  if (this->display_last_variant == this->index || this->display_last_variant == INVALID_ENGINE) 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 == VEH_TRAIN || this->type == VEH_ROAD;
160  }
161 
167  const GRFFile *GetGRF() const
168  {
169  return this->grf_prop.grffile;
170  }
171 
172  uint32_t GetGRFID() const;
173 
175  VehicleType vt;
176 
177  bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
178  };
179 
187  {
189  }
190 };
191 
193  uint32_t grfid;
194  uint16_t internal_id;
196  uint8_t substitute_id;
197 };
198 
203 struct EngineOverrideManager : std::vector<EngineIDMapping> {
204  static const uint NUM_DEFAULT_ENGINES;
205 
206  void ResetToDefaultMapping();
207  EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid);
208 
209  static bool ResetToCurrentNewGRFConfig();
210 };
211 
212 extern EngineOverrideManager _engine_mngr;
213 
214 inline const EngineInfo *EngInfo(EngineID e)
215 {
216  return &Engine::Get(e)->info;
217 }
218 
219 inline const RailVehicleInfo *RailVehInfo(EngineID e)
220 {
221  return &Engine::Get(e)->u.rail;
222 }
223 
224 inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
225 {
226  return &Engine::Get(e)->u.road;
227 }
228 
229 inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
230 {
231  return &Engine::Get(e)->u.ship;
232 }
233 
234 inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
235 {
236  return &Engine::Get(e)->u.air;
237 }
238 
239 #endif /* ENGINE_BASE_H */
Engine::reliability_max
uint16_t reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:44
INVALID_ENGINE
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:206
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:487
WagonOverride
Definition: engine_base.h:19
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:186
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:549
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
EngineOverrideManager::ResetToDefaultMapping
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition: engine.cpp:509
newgrf_commons.h
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:116
Engine::reliability_spd_dec
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:42
timer_game_calendar.h
Engine::GetLifeLengthInDays
TimerGameCalendar::Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:443
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:53
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:426
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Pool::PoolItem<&_engine_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Pool::IterateWrapperFiltered
Definition: pool_type.hpp:223
Engine::reliability_final
uint16_t reliability_final
Final reliability of the engine.
Definition: engine_base.h:45
Engine::GetRunningCost
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:281
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:51
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:150
EngineInfo
Information about a vehicle.
Definition: engine_type.h:144
Engine
Definition: engine_base.h:37
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:244
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:96
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:54
Engine::GetGRFID
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:157
Engine::EngineTypeFilter
Definition: engine_base.h:174
Engine::flags
uint8_t flags
Flags of the engine.
Definition: engine_base.h:49
EngineDisplayFlags::None
@ None
No flag set.
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:167
Engine::display_last_variant
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition: engine_base.h:59
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:201
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:358
RailVehicleInfo
Information about a rail vehicle.
Definition: engine_type.h:42
Engine::intro_date
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:39
RoadVehicleInfo
Information about a road vehicle.
Definition: engine_type.h:114
EngineIDMapping::grfid
uint32_t grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:193
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:50
Engine::display_flags
EngineDisplayFlags display_flags
NOSAVE client-side-only display flags for build engine list.
Definition: engine_base.h:58
EngineDisplayFlags::Shaded
@ Shaded
Set if engine should be masked.
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:408
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:196
EngineDisplayFlags::IsFolded
@ IsFolded
Set if display of variants should be folded (hidden).
EngineDisplayFlags
EngineDisplayFlags
Flags used client-side in the purchase/autorenew engine list.
Definition: engine_base.h:26
EngineIDMapping
Definition: engine_base.h:192
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:46
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:52
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:318
Engine::reliability
uint16_t reliability
Current reliability of the engine.
Definition: engine_base.h:41
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:157
Engine::reliability_start
uint16_t reliability_start
Initial reliability of the engine.
Definition: engine_base.h:43
vehicle_type.h
Engine::name
std::string name
Custom name of engine.
Definition: engine_base.h:38
Pool
Base class for all pools.
Definition: pool_type.hpp:80
Engine::IsHidden
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
Definition: engine_base.h:136
EngineIDMapping::internal_id
uint16_t internal_id
The internal ID within the GRF file.
Definition: engine_base.h:194
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:168
engine_type.h
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:390
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:203
EngineOverrideManager::GetID
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition: engine.cpp:532
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:67
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
OverflowSafeInt< int64_t >
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Engine::IsEnabled
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:147
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:55
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:77
EngineID
uint16_t EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
pool_type.hpp
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:312
EngineIDMapping::type
VehicleType type
The engine type.
Definition: engine_base.h:195
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition: engine_type.h:100
Engine::duration_phase_2
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:47
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:237
Engine::duration_phase_3
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:48
GRFFilePropsBase< NUM_CARGO+2 >
EngineDisplayFlags::HasVariants
@ HasVariants
Set if engine has variants.
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:467
SpriteGroup
Definition: newgrf_spritegroup.h:57
EngineOverrideManager::NUM_DEFAULT_ENGINES
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:204
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:56
Engine::GetRange
uint16_t GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:453
Engine::GetDisplayVariant
const Engine * GetDisplayVariant() const
Get the last display variant for an engine.
Definition: engine_base.h:145
Engine::age
int32_t age
Age of the engine in months.
Definition: engine_base.h:40
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103