OpenTTD Source  12.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 
18 struct WagonOverride {
19  std::vector<EngineID> engines;
20  CargoID cargo;
21  const SpriteGroup *group;
22 };
23 
25 extern EnginePool _engine_pool;
26 
27 struct Engine : EnginePool::PoolItem<&_engine_pool> {
28  std::string name;
30  Date age;
31  uint16 reliability;
34  uint16 reliability_max;
39  byte flags;
40  CompanyMask preview_asked;
42  byte preview_wait;
43  CompanyMask company_avail;
44  CompanyMask company_hidden;
47 
48  EngineInfo info;
49 
50  union {
51  RailVehicleInfo rail;
52  RoadVehicleInfo road;
53  ShipVehicleInfo ship;
55  } u;
56 
57  /* NewGRF related data */
65  std::vector<WagonOverride> overrides;
66  uint16 list_position;
67 
68  Engine() {}
70  bool IsEnabled() const;
71 
84  {
85  return this->info.cargo_type;
86  }
87 
88  uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = nullptr) const;
89 
90  bool CanCarryCargo() const;
91 
103  uint GetDisplayDefaultCapacity(uint16 *mail_capacity = nullptr) const
104  {
105  return this->DetermineCapacity(nullptr, mail_capacity);
106  }
107 
108  Money GetRunningCost() const;
109  Money GetCost() const;
110  uint GetDisplayMaxSpeed() const;
111  uint GetPower() const;
112  uint GetDisplayWeight() const;
113  uint GetDisplayMaxTractiveEffort() const;
114  Date GetLifeLengthInDays() const;
115  uint16 GetRange() const;
117 
123  inline bool IsHidden(CompanyID c) const
124  {
125  return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
126  }
127 
132  inline bool IsGroundVehicle() const
133  {
134  return this->type == VEH_TRAIN || this->type == VEH_ROAD;
135  }
136 
142  const GRFFile *GetGRF() const
143  {
144  return this->grf_prop.grffile;
145  }
146 
147  uint32 GetGRFID() const;
148 
150  VehicleType vt;
151 
152  bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
153  };
154 
162  {
164  }
165 };
166 
168  uint32 grfid;
169  uint16 internal_id;
172 };
173 
178 struct EngineOverrideManager : std::vector<EngineIDMapping> {
179  static const uint NUM_DEFAULT_ENGINES;
180 
181  void ResetToDefaultMapping();
182  EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
183 
184  static bool ResetToCurrentNewGRFConfig();
185 };
186 
187 extern EngineOverrideManager _engine_mngr;
188 
189 static inline const EngineInfo *EngInfo(EngineID e)
190 {
191  return &Engine::Get(e)->info;
192 }
193 
194 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
195 {
196  return &Engine::Get(e)->u.rail;
197 }
198 
199 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
200 {
201  return &Engine::Get(e)->u.road;
202 }
203 
204 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
205 {
206  return &Engine::Get(e)->u.ship;
207 }
208 
209 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
210 {
211  return &Engine::Get(e)->u.air;
212 }
213 
214 #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:103
Engine::GetGRFID
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:147
WagonOverride
Definition: engine_base.h:18
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:161
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:513
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
EngineOverrideManager::ResetToDefaultMapping
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition: engine.cpp:473
EngineIDMapping::grfid
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:168
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:34
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:38
newgrf_commons.h
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:32
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:43
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:414
Pool::PoolItem<&_engine_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
Pool::IterateWrapperFiltered
Definition: pool_type.hpp:220
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:36
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:269
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:33
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:41
Engine::GetLifeLengthInDays
Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:431
EngineInfo
Information about a vehicle.
Definition: engine_type.h:133
Engine
Definition: engine_base.h:27
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
EngineIDMapping::internal_id
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:169
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:83
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:44
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:496
Engine::EngineTypeFilter
Definition: engine_base.h:149
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:142
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:346
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:112
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:40
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:396
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
EngineIDMapping
Definition: engine_base.h:167
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:171
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:306
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:35
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:132
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:28
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:123
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:158
engine_type.h
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:378
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:178
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:66
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:42
Engine::original_image_index
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:45
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:191
OverflowSafeInt< int64 >
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:137
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:64
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:441
EngineIDMapping::type
VehicleType type
The engine type.
Definition: engine_base.h:170
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:39
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition: engine_type.h:98
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:234
GRFFilePropsBase< NUM_CARGO+2 >
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:29
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:455
SpriteGroup
Definition: newgrf_spritegroup.h:57
EngineOverrideManager::NUM_DEFAULT_ENGINES
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:179
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:31
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:46
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:37