OpenTTD
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"
17 
19 extern EnginePool _engine_pool;
20 
21 struct Engine : EnginePool::PoolItem<&_engine_pool> {
22  char *name;
24  Date age;
25  uint16 reliability;
28  uint16 reliability_max;
33  byte flags;
34  CompanyMask preview_asked;
36  byte preview_wait;
37  CompanyMask company_avail;
38  CompanyMask company_hidden;
41 
42  EngineInfo info;
43 
44  union {
45  RailVehicleInfo rail;
46  RoadVehicleInfo road;
47  ShipVehicleInfo ship;
49  } u;
50 
51  /* NewGRF related data */
59  uint16 overrides_count;
60  struct WagonOverride *overrides;
61  uint16 list_position;
62 
63  Engine();
64  Engine(VehicleType type, EngineID base);
65  ~Engine();
66  bool IsEnabled() const;
67 
80  {
81  return this->info.cargo_type;
82  }
83 
84  uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = nullptr) const;
85 
86  bool CanCarryCargo() const;
87 
99  uint GetDisplayDefaultCapacity(uint16 *mail_capacity = nullptr) const
100  {
101  return this->DetermineCapacity(nullptr, mail_capacity);
102  }
103 
104  Money GetRunningCost() const;
105  Money GetCost() const;
106  uint GetDisplayMaxSpeed() const;
107  uint GetPower() const;
108  uint GetDisplayWeight() const;
109  uint GetDisplayMaxTractiveEffort() const;
110  Date GetLifeLengthInDays() const;
111  uint16 GetRange() const;
113 
119  inline bool IsHidden(CompanyID c) const
120  {
121  return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
122  }
123 
128  inline bool IsGroundVehicle() const
129  {
130  return this->type == VEH_TRAIN || this->type == VEH_ROAD;
131  }
132 
138  const GRFFile *GetGRF() const
139  {
140  return this->grf_prop.grffile;
141  }
142 
143  uint32 GetGRFID() const;
144 
152  {
153  return Pool::IterateWrapper<Engine>(from, [vt](size_t index) { return Engine::Get(index)->type == vt; });
154  }
155 };
156 
158  uint32 grfid;
159  uint16 internal_id;
162 };
163 
168 struct EngineOverrideManager : std::vector<EngineIDMapping> {
169  static const uint NUM_DEFAULT_ENGINES;
170 
171  void ResetToDefaultMapping();
172  EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
173 
174  static bool ResetToCurrentNewGRFConfig();
175 };
176 
177 extern EngineOverrideManager _engine_mngr;
178 
179 static inline const EngineInfo *EngInfo(EngineID e)
180 {
181  return &Engine::Get(e)->info;
182 }
183 
184 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
185 {
186  return &Engine::Get(e)->u.rail;
187 }
188 
189 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
190 {
191  return &Engine::Get(e)->u.road;
192 }
193 
194 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
195 {
196  return &Engine::Get(e)->u.ship;
197 }
198 
199 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
200 {
201  return &Engine::Get(e)->u.air;
202 }
203 
204 #endif /* ENGINE_BASE_H */
Information about a ship vehicle.
Definition: engine_type.h:65
Owner
Enum for all companies/owners.
Definition: company_type.h:18
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:27
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:25
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:158
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:150
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:468
Train vehicle type.
Definition: vehicle_type.h:24
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:171
uint16 GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:454
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:58
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:23
Types related to engines.
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company...
Definition: engine_base.h:38
Vehicle data structure.
Definition: vehicle_base.h:210
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:169
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
byte flags
Flags of the engine.
Definition: engine_base.h:33
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle...
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:79
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:31
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:160
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:26
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:319
Information about a vehicle.
Definition: engine_type.h:132
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:159
uint16 duration_phase_3
Third reliability phase on months, decaying to reliability_final.
Definition: engine_base.h:32
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:168
uint16 duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max...
Definition: engine_base.h:30
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:40
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:37
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:28
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:29
char * name
Custom name of engine.
Definition: engine_base.h:22
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Base class for all PoolItems.
Definition: pool_type.hpp:188
Information about a rail vehicle.
Definition: engine_type.h:42
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:359
Base class for all pools.
Definition: pool_type.hpp:82
VehicleType type
The engine type.
Definition: engine_base.h:160
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
Definition: engine_base.h:119
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:35
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:391
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:36
Information about a road vehicle.
Definition: engine_type.h:111
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:138
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
Information about a aircraft vehicle.
Definition: engine_type.h:97
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:34
Types related to vehicles.
Maximum number of companies.
Definition: company_type.h:23
uint GetDisplayDefaultCapacity(uint16 *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
Definition: engine_base.h:99
uint8 substitute_id
The (original) entity ID to use if this GRF is not available (currently not used) ...
Definition: engine_base.h:161
This file simplyfies and embeds a common mechanism of loading/saving and mapping of grf entities...
int32 Date
The type to store our dates in.
Definition: date_type.h:14
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:39
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:128
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:282
const struct GRFFile * grffile
grf file that introduced this entity
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:409
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:204
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:427
Road vehicle type.
Definition: vehicle_type.h:25
Date GetLifeLengthInDays() const
Returns the vehicle&#39;s (not model&#39;s!) life length in days.
Definition: engine.cpp:444
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
static Pool::IterateWrapper< Engine > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
Definition: engine_base.h:151