OpenTTD Source: src/engine_base.h Source File

OpenTTD Source 15.0-beta1

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#include "timer/timer_game_calendar.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};
32 DECLARE_ENUM_AS_BIT_SET(EngineDisplayFlags)
33
34typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
35extern EnginePool _engine_pool;
36
37 struct Engine : EnginePool::PoolItem<&_engine_pool> {
38 CompanyMask company_avail;
39 CompanyMask company_hidden;
40 CompanyMask preview_asked;
41
42 std::string name;
43
44 TimerGameCalendar::Date intro_date;
45 int32_t age;
46
47 uint16_t reliability;
48 uint16_t reliability_spd_dec;
49 uint16_t reliability_start;
50 uint16_t reliability_max;
51 uint16_t reliability_final;
52 uint16_t duration_phase_1;
53 uint16_t duration_phase_2;
54 uint16_t duration_phase_3;
55 uint8_t flags;
56
57 CompanyID preview_company;
58 uint8_t preview_wait;
59 uint8_t original_image_index;
60 VehicleType type;
61
62 EngineDisplayFlags display_flags;
63 EngineID display_last_variant;
64 EngineInfo info;
65
66 union {
67 RailVehicleInfo rail;
68 RoadVehicleInfo road;
69 ShipVehicleInfo ship;
70 AircraftVehicleInfo air;
71 } u;
72
73 uint16_t list_position;
74
75 /* NewGRF related data */
82 GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
83 std::vector<WagonOverride> overrides;
84
85 Engine() {}
86 Engine(VehicleType type, EngineID base);
87 bool IsEnabled() const;
88
100 CargoID GetDefaultCargoType() const
101 {
102 return this->info.cargo_type;
103 }
104
105 uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity = nullptr) const;
106
107 bool CanCarryCargo() const;
108
120 uint GetDisplayDefaultCapacity(uint16_t *mail_capacity = nullptr) const
121 {
122 return this->DetermineCapacity(nullptr, mail_capacity);
123 }
124
125 Money GetRunningCost() const;
126 Money GetCost() const;
127 uint GetDisplayMaxSpeed() const;
128 uint GetPower() const;
129 uint GetDisplayWeight() const;
130 uint GetDisplayMaxTractiveEffort() const;
131 TimerGameCalendar::Date GetLifeLengthInDays() const;
132 uint16_t GetRange() const;
133 StringID GetAircraftTypeText() const;
134
140 inline bool IsHidden(CompanyID c) const
141 {
142 return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
143 }
144
149 const Engine *GetDisplayVariant() const
150 {
151 if (this->display_last_variant == this->index || this->display_last_variant == INVALID_ENGINE) return this;
152 return Engine::Get(this->display_last_variant);
153 }
154
155 bool IsVariantHidden(CompanyID c) const;
156
161 inline bool IsGroundVehicle() const
162 {
163 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
164 }
165
171 const GRFFile *GetGRF() const
172 {
173 return this->grf_prop.grffile;
174 }
175
176 uint32_t GetGRFID() const;
177
178 struct EngineTypeFilter {
179 VehicleType vt;
180
181 bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
182 };
183
190 static Pool::IterateWrapperFiltered<Engine, EngineTypeFilter> IterateType(VehicleType vt, size_t from = 0)
191 {
192 return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
193 }
194};
195
196 struct EngineIDMapping {
197 uint32_t grfid;
198 uint16_t internal_id;
199 VehicleType type;
200 uint8_t substitute_id;
201 EngineID engine;
202
203 static inline uint64_t Key(uint32_t grfid, uint16_t internal_id) { return static_cast<uint64_t>(grfid) << 32 | internal_id; }
204
205 inline uint64_t Key() const { return Key(this->grfid, this->internal_id); }
206
207 EngineIDMapping() {}
208 EngineIDMapping(uint32_t grfid, uint16_t internal_id, VehicleType type, uint8_t substitute_id, EngineID engine)
209 : grfid(grfid), internal_id(internal_id), type(type), substitute_id(substitute_id), engine(engine) {}
210};
211
213 struct EngineIDMappingKeyProjection {
214 inline size_t operator()(const EngineIDMapping &eid) const { return eid.Key(); }
215};
216
221 struct EngineOverrideManager {
222 std::array<std::vector<EngineIDMapping>, VEH_COMPANY_END> mappings;
223
224 void ResetToDefaultMapping();
225 EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid);
226 EngineID UseUnreservedID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, bool static_access);
227 void SetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, uint8_t substitute_id, EngineID engine);
228
229 static bool ResetToCurrentNewGRFConfig();
230};
231
232extern EngineOverrideManager _engine_mngr;
233
234inline const EngineInfo *EngInfo(EngineID e)
235{
236 return &Engine::Get(e)->info;
237}
238
239inline const RailVehicleInfo *RailVehInfo(EngineID e)
240{
241 return &Engine::Get(e)->u.rail;
242}
243
244inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
245{
246 return &Engine::Get(e)->u.road;
247}
248
249inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
250{
251 return &Engine::Get(e)->u.ship;
252}
253
254inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
255{
256 return &Engine::Get(e)->u.air;
257}
258
259#endif /* ENGINE_BASE_H */
HasBit
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
OverflowSafeInt< int64_t >
Owner
Owner
Enum for all companies/owners.
Definition company_type.h:18
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition company_type.h:23
EngineDisplayFlags
EngineDisplayFlags
Flags used client-side in the purchase/autorenew engine list.
Definition engine_base.h:26
EngineDisplayFlags::HasVariants
@ HasVariants
Set if engine has variants.
EngineDisplayFlags::IsFolded
@ IsFolded
Set if display of variants should be folded (hidden).
EngineDisplayFlags::None
@ None
No flag set.
EngineDisplayFlags::Shaded
@ Shaded
Set if engine should be masked.
engine_type.h
Types related to engines.
INVALID_ENGINE
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition engine_type.h:210
EngineID
uint16_t EngineID
Unique identification number of an engine.
Definition engine_type.h:21
DECLARE_ENUM_AS_BIT_SET
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Definition enum_type.hpp:35
newgrf_commons.h
This file simplyfies and embeds a common mechanism of loading/saving and mapping of grf entities.
pool_type.hpp
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition engine_type.h:100
EngineIDMappingKeyProjection
Projection to get a unique key of an EngineIDMapping, used for sorting in EngineOverrideManager.
Definition engine_base.h:213
EngineIDMapping
Definition engine_base.h:196
EngineIDMapping::type
VehicleType type
The engine type.
Definition engine_base.h:199
EngineIDMapping::grfid
uint32_t grfid
The GRF ID of the file the entity belongs to.
Definition engine_base.h:197
EngineIDMapping::substitute_id
uint8_t substitute_id
The (original) entity ID to use if this GRF is not available (currently not used)
Definition engine_base.h:200
EngineIDMapping::internal_id
uint16_t internal_id
The internal ID within the GRF file.
Definition engine_base.h:198
EngineInfo
Information about a vehicle.
Definition engine_type.h:148
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition engine_base.h:221
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition engine.cpp:585
EngineOverrideManager::ResetToDefaultMapping
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition engine.cpp:509
EngineOverrideManager::UseUnreservedID
EngineID UseUnreservedID(VehicleType type, uint16_t grf_local_id, uint32_t grfid, bool static_access)
Look for an unreserved EngineID matching the local id, and reserve it if found.
Definition engine.cpp:549
EngineOverrideManager::GetID
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition engine.cpp:530
Engine::EngineTypeFilter
Definition engine_base.h:178
Engine
Definition engine_base.h:37
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition engine.cpp:467
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition engine.cpp:390
Engine::GetDisplayVariant
const Engine * GetDisplayVariant() const
Get the last display variant for an engine.
Definition engine_base.h:149
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition engine_base.h:100
Engine::GetRange
uint16_t GetRange() const
Get the range of an aircraft type.
Definition engine.cpp:453
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition engine_base.h:82
Engine::GetGRFID
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition engine.cpp:157
Engine::reliability_spd_dec
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:48
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition engine.cpp:318
Engine::reliability_start
uint16_t reliability_start
Initial reliability of the engine.
Definition engine_base.h:49
Engine::intro_date
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition engine_base.h:44
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition engine_base.h:171
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:190
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition engine.cpp:358
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:201
Engine::display_flags
EngineDisplayFlags display_flags
NOSAVE client-side-only display flags for build engine list.
Definition engine_base.h:62
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition engine_base.h:38
Engine::reliability_max
uint16_t reliability_max
Maximal reliability of the engine.
Definition engine_base.h:50
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition engine.cpp:408
Engine::original_image_index
uint8_t original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition engine_base.h:59
Engine::IsEnabled
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition engine.cpp:147
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition engine_base.h:60
Engine::IsVariantHidden
bool IsVariantHidden(CompanyID c) const
Check whether the engine variant chain is hidden in the GUI for the given company.
Definition engine.cpp:487
Engine::reliability_final
uint16_t reliability_final
Final reliability of the engine.
Definition engine_base.h:51
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition engine_base.h:57
Engine::GetLifeLengthInDays
TimerGameCalendar::Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition engine.cpp:443
Engine::GetDisplayDefaultCapacity
uint GetDisplayDefaultCapacity(uint16_t *mail_capacity=nullptr) const
Determines the default cargo capacity of an engine for display purposes.
Definition engine_base.h:120
Engine::duration_phase_3
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition engine_base.h:54
Engine::duration_phase_2
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition engine_base.h:53
Engine::preview_wait
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition engine_base.h:58
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:39
Engine::GetRunningCost
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition engine.cpp:281
Engine::reliability
uint16_t reliability
Current reliability of the engine.
Definition engine_base.h:47
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition engine_base.h:40
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition engine.cpp:426
Engine::age
int32_t age
Age of the engine in months.
Definition engine_base.h:45
Engine::IsHidden
bool IsHidden(CompanyID c) const
Check whether the engine is hidden in the GUI for the given company.
Definition engine_base.h:140
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition engine.cpp:168
Engine::name
std::string name
Custom name of engine.
Definition engine_base.h:42
Engine::display_last_variant
EngineID display_last_variant
NOSAVE client-side-only last variant selected.
Definition engine_base.h:63
Engine::duration_phase_1
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition engine_base.h:52
Engine::flags
uint8_t flags
Flags of the engine.
Definition engine_base.h:55
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition engine_base.h:161
GRFFilePropsBase
Data related to the handling of grf files.
Definition newgrf_commons.h:310
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition newgrf_commons.h:313
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:108
Pool::IterateWrapperFiltered
Definition pool_type.hpp:223
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:237
Pool::PoolItem<&_engine_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:238
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition pool_type.hpp:339
Pool
Base class for all pools.
Definition pool_type.hpp:80
RailVehicleInfo
Information about a rail vehicle.
Definition engine_type.h:42
RoadVehicleInfo
Information about a road vehicle.
Definition engine_type.h:114
ShipVehicleInfo
Information about a ship vehicle.
Definition engine_type.h:67
SpriteGroup
Definition newgrf_spritegroup.h:57
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:150
Vehicle
Vehicle data structure.
Definition vehicle_base.h:244
WagonOverride
Definition engine_base.h:19
timer_game_calendar.h
Definition of the game-calendar-timer.
vehicle_type.h
Types related to vehicles.
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:21
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition vehicle_type.h:25
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition vehicle_type.h:24
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition vehicle_type.h:29