OpenTTD

engine_base.h
Go to the documentation of this file.
1 /* $Id: engine_base.h 27797 2017-03-18 20:43:43Z alberth $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef ENGINE_BASE_H
13 #define ENGINE_BASE_H
14
15 #include "engine_type.h"
16 #include "vehicle_type.h"
17 #include "core/pool_type.hpp"
18 #include "newgrf_commons.h"
19
20 typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
21 extern EnginePool _engine_pool;
22
23 struct Engine : EnginePool::PoolItem<&_engine_pool> {
24 char *name;
25 Date intro_date;
26 Date age;
27 uint16 reliability;
28 uint16 reliability_spd_dec;
29 uint16 reliability_start;
30 uint16 reliability_max;
31 uint16 reliability_final;
32 uint16 duration_phase_1;
33 uint16 duration_phase_2;
34 uint16 duration_phase_3;
35 byte flags;
36 CompanyMask preview_asked;
37 CompanyByte preview_company;
38 byte preview_wait;
39 CompanyMask company_avail;
40 CompanyMask company_hidden;
41 uint8 original_image_index;
42 VehicleType type;
43
44 EngineInfo info;
45
46 union {
47 RailVehicleInfo rail;
48 RoadVehicleInfo road;
49 ShipVehicleInfo ship;
50 AircraftVehicleInfo air;
51 } u;
52
53 /* NewGRF related data */
60 GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
61 uint16 overrides_count;
62 struct WagonOverride *overrides;
63 uint16 list_position;
64
65 Engine();
66 Engine(VehicleType type, EngineID base);
67 ~Engine();
68 bool IsEnabled() const;
69
81 CargoID GetDefaultCargoType() const
82 {
83 return this->info.cargo_type;
84 }
85
86 uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = NULL) const;
87
88 bool CanCarryCargo() const;
89
101 uint GetDisplayDefaultCapacity(uint16 *mail_capacity = NULL) const
102 {
103 return this->DetermineCapacity(NULL, mail_capacity);
104 }
105
106 Money GetRunningCost() const;
107 Money GetCost() const;
108 uint GetDisplayMaxSpeed() const;
109 uint GetPower() const;
110 uint GetDisplayWeight() const;
111 uint GetDisplayMaxTractiveEffort() const;
112 Date GetLifeLengthInDays() const;
113 uint16 GetRange() const;
114 StringID GetAircraftTypeText() const;
115
121 inline bool IsHidden(CompanyByte c) const
122 {
123 return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
124 }
125
130 inline bool IsGroundVehicle() const
131 {
132 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
133 }
134
140 const GRFFile *GetGRF() const
141 {
142 return this->grf_prop.grffile;
143 }
144
145 uint32 GetGRFID() const;
146 };
147
148 struct EngineIDMapping {
149 uint32 grfid;
150 uint16 internal_id;
151 VehicleTypeByte type;
152 uint8 substitute_id;
153 };
154
159 struct EngineOverrideManager : SmallVector<EngineIDMapping, 256> {
160 static const uint NUM_DEFAULT_ENGINES;
161
162 void ResetToDefaultMapping();
163 EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
164
165 static bool ResetToCurrentNewGRFConfig();
166 };
167
168 extern EngineOverrideManager _engine_mngr;
169
170 #define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start)
171 #define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0)
172
173 #define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type)
174
175 static inline const EngineInfo *EngInfo(EngineID e)
176 {
177 return &Engine::Get(e)->info;
178 }
179
180 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
181 {
182 return &Engine::Get(e)->u.rail;
183 }
184
185 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
186 {
187 return &Engine::Get(e)->u.road;
188 }
189
190 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
191 {
192 return &Engine::Get(e)->u.ship;
193 }
194
195 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
196 {
197 return &Engine::Get(e)->u.air;
198 }
199
200 #endif /* ENGINE_BASE_H */
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:66
WagonOverride
Definition: newgrf_engine.cpp:30
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:29
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:27
Engine::GetDisplayDefaultCapacity
uint GetDisplayDefaultCapacity(uint16 *mail_capacity=NULL) const
Determines the default cargo capacity of an engine for display purposes.
Definition: engine_base.h:101
EngineIDMapping::grfid
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:149
Engine::IsEnabled
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:152
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:470
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:173
Engine::IsHidden
bool IsHidden(CompanyByte c) const
Check whether the engine is hidden in the GUI for the given company.
Definition: engine_base.h:121
EngineIDMapping
Definition: engine_base.h:148
Engine::GetRange
uint16 GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:456
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:60
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:25
engine_type.h
Types related to engines.
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:40
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:212
EngineOverrideManager::NUM_DEFAULT_ENGINES
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:160
SmallVector
Simple vector template class.
Definition: smallvec_type.hpp:29
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:35
pool_type.hpp
Defintion of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle...
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:81
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:33
Engine::GetGRFID
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:162
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:28
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:321
EngineInfo
Information about a vehicle.
Definition: engine_type.h:132
EngineIDMapping::internal_id
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:150
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase on months, decaying to reliability_final.
Definition: engine_base.h:34
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:159
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:32
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:42
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:39
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:30
VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Engine::reliability_final
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:31
Engine::name
char * name
Custom name of engine.
Definition: engine_base.h:24
TinyEnumT< Owner >
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:146
MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:25
SimpleTinyEnumT< VehicleType, byte >
Engine
Definition: engine_base.h:23
RailVehicleInfo
Information about a rail vehicle.
Definition: engine_type.h:43
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:361
Pool
Base class for all pools.
Definition: pool_type.hpp:83
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=NULL) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:206
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:393
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:38
RoadVehicleInfo
Information about a road vehicle.
Definition: engine_type.h:112
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:140
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition: engine_type.h:98
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:36
EngineIDMapping::type
VehicleTypeByte type
The engine type.
Definition: engine_base.h:151
vehicle_type.h
Types related to vehicles.
GRFFilePropsBase< NUM_CARGO+2 >
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:152
newgrf_commons.h
This file simplyfies and embeds a common mechanism of loading/saving and mapping of grf entities...
Engine::preview_company
CompanyByte preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:37
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:16
Engine::original_image_index
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:41
Engine::IsGroundVehicle
bool IsGroundVehicle() const
Check if the engine is a ground vehicle.
Definition: engine_base.h:130
Engine::GetRunningCost
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:284
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:320
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:105
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:411
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:429
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
Engine::GetLifeLengthInDays
Date GetLifeLengthInDays() const
Returns the vehicle&#39;s (not model&#39;s!) life length in days.
Definition: engine.cpp:446
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104
VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24