OpenTTD Source: src/engine_base.h Source File

OpenTTD Source 13.1

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 enum class EngineDisplayFlags : byte {
26 None = 0,
27 HasVariants = (1U << 0),
28 IsFolded = (1U << 1),
29 Shaded = (1U << 2),
30 };
31 DECLARE_ENUM_AS_BIT_SET(EngineDisplayFlags)
32
33 typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
34 extern EnginePool _engine_pool;
35
36 struct Engine : EnginePool::PoolItem<&_engine_pool> {
37 std::string name;
38 Date intro_date;
39 Date age;
40 uint16 reliability;
41 uint16 reliability_spd_dec;
42 uint16 reliability_start;
43 uint16 reliability_max;
44 uint16 reliability_final;
45 uint16 duration_phase_1;
46 uint16 duration_phase_2;
47 uint16 duration_phase_3;
48 byte flags;
49 CompanyMask preview_asked;
50 CompanyID preview_company;
51 byte preview_wait;
52 CompanyMask company_avail;
53 CompanyMask company_hidden;
54 uint8 original_image_index;
55 VehicleType type;
56
57 EngineDisplayFlags display_flags;
58 EngineID display_last_variant;
59
60 EngineInfo info;
61
62 union {
63 RailVehicleInfo rail;
64 RoadVehicleInfo road;
65 ShipVehicleInfo ship;
66 AircraftVehicleInfo air;
67 } u;
68
69 /* NewGRF related data */
76 GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
77 std::vector<WagonOverride> overrides;
78 uint16 list_position;
79
80 Engine() {}
81 Engine(VehicleType type, EngineID base);
82 bool IsEnabled() const;
83
95 CargoID GetDefaultCargoType() const
96 {
97 return this->info.cargo_type;
98 }
99
100 uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = nullptr) const;
101
102 bool CanCarryCargo() const;
103
115 uint GetDisplayDefaultCapacity(uint16 *mail_capacity = nullptr) const
116 {
117 return this->DetermineCapacity(nullptr, mail_capacity);
118 }
119
120 Money GetRunningCost() const;
121 Money GetCost() const;
122 uint GetDisplayMaxSpeed() const;
123 uint GetPower() const;
124 uint GetDisplayWeight() const;
125 uint GetDisplayMaxTractiveEffort() const;
126 Date GetLifeLengthInDays() const;
127 uint16 GetRange() const;
128 StringID GetAircraftTypeText() const;
129
135 inline bool IsHidden(CompanyID c) const
136 {
137 return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
138 }
139
144 inline bool IsGroundVehicle() const
145 {
146 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
147 }
148
154 const GRFFile *GetGRF() const
155 {
156 return this->grf_prop.grffile;
157 }
158
159 uint32 GetGRFID() const;
160
161 struct EngineTypeFilter {
162 VehicleType vt;
163
164 bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
165 };
166
173 static Pool::IterateWrapperFiltered<Engine, EngineTypeFilter> IterateType(VehicleType vt, size_t from = 0)
174 {
175 return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
176 }
177 };
178
179 struct EngineIDMapping {
180 uint32 grfid;
181 uint16 internal_id;
182 VehicleType type;
183 uint8 substitute_id;
184 };
185
190 struct EngineOverrideManager : std::vector<EngineIDMapping> {
191 static const uint NUM_DEFAULT_ENGINES;
192
193 void ResetToDefaultMapping();
194 EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
195
196 static bool ResetToCurrentNewGRFConfig();
197 };
198
199 extern EngineOverrideManager _engine_mngr;
200
201 static inline const EngineInfo *EngInfo(EngineID e)
202 {
203 return &Engine::Get(e)->info;
204 }
205
206 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
207 {
208 return &Engine::Get(e)->u.rail;
209 }
210
211 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
212 {
213 return &Engine::Get(e)->u.road;
214 }
215
216 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
217 {
218 return &Engine::Get(e)->u.ship;
219 }
220
221 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
222 {
223 return &Engine::Get(e)->u.air;
224 }
225
226 #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:115
Engine::GetGRFID
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:153
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:173
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:519
EngineDisplayFlags::HasVariants
@ HasVariants
Set if engine has variants.
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:479
EngineIDMapping::grfid
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:180
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:43
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:47
newgrf_commons.h
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:41
EngineDisplayFlags::IsFolded
@ IsFolded
Set if display of variants should be folded (hidden).
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:52
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:420
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:45
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:275
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:42
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:50
Engine::GetLifeLengthInDays
Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:437
EngineInfo
Information about a vehicle.
Definition: engine_type.h:143
Engine
Definition: engine_base.h:36
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
EngineIDMapping::internal_id
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:181
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:95
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:53
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:502
Engine::EngineTypeFilter
Definition: engine_base.h:161
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:154
Engine::display_last_variant
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition: engine_base.h:58
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:352
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:113
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:49
Engine::display_flags
EngineDisplayFlags display_flags
NOSAVE client-side-only display flags for build engine list.
Definition: engine_base.h:57
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:402
EngineDisplayFlags::None
@ None
No flag set.
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
EngineIDMapping
Definition: engine_base.h:179
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:183
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:312
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:44
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:144
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:37
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:135
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:164
engine_type.h
EngineDisplayFlags
EngineDisplayFlags
Flags used client-side in the purchase/autorenew engine list.
Definition: engine_base.h:25
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:384
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:190
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:67
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:51
Engine::original_image_index
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:54
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:197
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:143
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:76
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:447
EngineIDMapping::type
VehicleType type
The engine type.
Definition: engine_base.h:182
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:48
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition: engine_type.h:99
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:38
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:461
SpriteGroup
Definition: newgrf_spritegroup.h:57
EngineOverrideManager::NUM_DEFAULT_ENGINES
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:191
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:106
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:40
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:55
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:46