OpenTTD Source  1.11.0-beta2
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  std::string 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();
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 
146  VehicleType vt;
147 
148  bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
149  };
150 
158  {
160  }
161 };
162 
164  uint32 grfid;
165  uint16 internal_id;
168 };
169 
174 struct EngineOverrideManager : std::vector<EngineIDMapping> {
175  static const uint NUM_DEFAULT_ENGINES;
176 
177  void ResetToDefaultMapping();
178  EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
179 
180  static bool ResetToCurrentNewGRFConfig();
181 };
182 
183 extern EngineOverrideManager _engine_mngr;
184 
185 static inline const EngineInfo *EngInfo(EngineID e)
186 {
187  return &Engine::Get(e)->info;
188 }
189 
190 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
191 {
192  return &Engine::Get(e)->u.rail;
193 }
194 
195 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
196 {
197  return &Engine::Get(e)->u.road;
198 }
199 
200 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
201 {
202  return &Engine::Get(e)->u.ship;
203 }
204 
205 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
206 {
207  return &Engine::Get(e)->u.air;
208 }
209 
210 #endif /* ENGINE_BASE_H */
Engine::GetDisplayDefaultCapacity
uint GetDisplayDefaultCapacity(uint16 *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
Definition: engine_base.h:99
Engine::GetGRFID
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:158
WagonOverride
Definition: newgrf_engine.cpp:29
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:157
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:524
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
EngineOverrideManager::ResetToDefaultMapping
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition: engine.cpp:484
EngineIDMapping::grfid
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:164
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:28
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:32
newgrf_commons.h
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:26
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:37
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:425
Pool::PoolItem<&_engine_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
Pool::IterateWrapperFiltered
Definition: pool_type.hpp:212
Engine::duration_phase_1
uint16 duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition: engine_base.h:30
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
Engine::GetRunningCost
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:280
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:27
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:35
Engine::GetLifeLengthInDays
Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:442
EngineInfo
Information about a vehicle.
Definition: engine_type.h:132
Engine
Definition: engine_base.h:21
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
EngineIDMapping::internal_id
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:165
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:79
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:38
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
EngineOverrideManager::GetID
EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition: engine.cpp:507
Engine::EngineTypeFilter
Definition: engine_base.h:145
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:138
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:357
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
RailVehicleInfo
Information about a rail vehicle.
Definition: engine_type.h:42
RoadVehicleInfo
Information about a road vehicle.
Definition: engine_type.h:111
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:34
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:407
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
EngineIDMapping
Definition: engine_base.h:163
EngineIDMapping::substitute_id
uint8 substitute_id
The (original) entity ID to use if this GRF is not available (currently not used)
Definition: engine_base.h:167
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:317
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Engine::reliability_final
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:29
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:128
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
vehicle_type.h
Engine::name
std::string name
Custom name of engine.
Definition: engine_base.h:22
Pool
Base class for all pools.
Definition: pool_type.hpp:81
Engine::IsHidden
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
Definition: engine_base.h:119
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:169
engine_type.h
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:389
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:174
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:65
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:36
Engine::original_image_index
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:39
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:202
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
Engine::IsEnabled
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:148
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:58
pool_type.hpp
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:320
Engine::GetRange
uint16 GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:452
EngineIDMapping::type
VehicleType type
The engine type.
Definition: engine_base.h:166
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:33
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition: engine_type.h:97
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:226
GRFFilePropsBase< NUM_CARGO+2 >
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:23
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:466
EngineOverrideManager::NUM_DEFAULT_ENGINES
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:175
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:25
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:40
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:31