OpenTTD Source  20241120-master-g6d3adc6169
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 */
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
EngineDisplayFlags
Flags used client-side in the purchase/autorenew engine list.
Definition: engine_base.h:26
@ HasVariants
Set if engine has variants.
@ IsFolded
Set if display of variants should be folded (hidden).
@ None
No flag set.
@ Shaded
Set if engine should be masked.
Types related to engines.
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:206
uint16_t EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
This file simplyfies 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.
Definition: strings_type.h:16
Information about a aircraft vehicle.
Definition: engine_type.h:100
VehicleType type
The engine type.
Definition: engine_base.h:195
uint32_t grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:193
uint8_t substitute_id
The (original) entity ID to use if this GRF is not available (currently not used)
Definition: engine_base.h:196
uint16_t internal_id
The internal ID within the GRF file.
Definition: engine_base.h:194
Information about a vehicle.
Definition: engine_type.h:144
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:203
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:549
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition: engine.cpp:509
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition: engine.cpp:532
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:204
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:467
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:390
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:96
uint16_t GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:453
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:77
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:157
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:42
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:318
uint16_t reliability_start
Initial reliability of the engine.
Definition: engine_base.h:43
const Engine * GetDisplayVariant() const
Get the last display variant for an engine.
Definition: engine_base.h:145
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:39
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:358
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:201
EngineDisplayFlags display_flags
NOSAVE client-side-only display flags for build engine list.
Definition: engine_base.h:58
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:53
uint16_t reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:44
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:408
uint8_t original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:55
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:147
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:56
bool IsVariantHidden(CompanyID c) const
Check whether the engine variant chain is hidden in the GUI for the given company.
Definition: engine.cpp:487
uint16_t reliability_final
Final reliability of the engine.
Definition: engine_base.h:45
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:167
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:51
TimerGameCalendar::Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:443
uint GetDisplayDefaultCapacity(uint16_t *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
Definition: engine_base.h:116
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:48
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:47
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:52
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
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:281
uint16_t reliability
Current reliability of the engine.
Definition: engine_base.h:41
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:50
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:426
int32_t age
Age of the engine in months.
Definition: engine_base.h:40
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
Definition: engine_base.h:136
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
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:168
std::string name
Custom name of engine.
Definition: engine_base.h:38
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition: engine_base.h:59
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition: engine_base.h:46
uint8_t flags
Flags of the engine.
Definition: engine_base.h:49
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:157
const struct GRFFile * grffile
grf file that introduced this entity
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
Base class for all PoolItems.
Definition: pool_type.hpp:237
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
Base class for all pools.
Definition: pool_type.hpp:80
Information about a rail vehicle.
Definition: engine_type.h:42
Information about a road vehicle.
Definition: engine_type.h:114
Information about a ship vehicle.
Definition: engine_type.h:67
Templated helper to make a type-safe 'typedef' representing a single POD value.
Vehicle data structure.
Definition: vehicle_base.h:244
Definition of the game-calendar-timer.
Types related to vehicles.
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24