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 
8 /** @file engine_base.h Base class for engines. */
9 
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 
24 typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
25 extern EnginePool _engine_pool;
26 
27 struct Engine : EnginePool::PoolItem<&_engine_pool> {
28 	std::string name;           ///< Custom name of engine.
29 	Date intro_date;            ///< Date of introduction of the engine.
30 	Date age;
31 	uint16 reliability;         ///< Current reliability of the engine.
32 	uint16 reliability_spd_dec; ///< Speed of reliability decay between services (per day).
33 	uint16 reliability_start;   ///< Initial reliability of the engine.
34 	uint16 reliability_max;     ///< Maximal reliability of the engine.
35 	uint16 reliability_final;   ///< Final reliability of the engine.
36 	uint16 duration_phase_1;    ///< First reliability phase in months, increasing reliability from #reliability_start to #reliability_max.
37 	uint16 duration_phase_2;    ///< Second reliability phase in months, keeping #reliability_max.
38 	uint16 duration_phase_3;    ///< Third reliability phase in months, decaying to #reliability_final.
39 	byte flags;                 ///< Flags of the engine. @see EngineFlags
40 	CompanyMask preview_asked;  ///< Bit for each company which has already been offered a preview.
41 	CompanyID preview_company;  ///< Company which is currently being offered a preview \c INVALID_COMPANY means no company.
42 	byte preview_wait;          ///< Daily countdown timer for timeout of offering the engine to the #preview_company company.
43 	CompanyMask company_avail;  ///< Bit for each company whether the engine is available for that company.
44 	CompanyMask company_hidden; ///< Bit for each company whether the engine is normally hidden in the build gui for that company.
45 	uint8 original_image_index; ///< Original vehicle image index, thus the image index of the overridden vehicle
46 	VehicleType type;           ///< %Vehicle type, ie #VEH_ROAD, #VEH_TRAIN, etc.
47 
48 	EngineInfo info;
49 
50 	union {
51 		RailVehicleInfo rail;
52 		RoadVehicleInfo road;
53 		ShipVehicleInfo ship;
54 		AircraftVehicleInfo air;
55 	} u;
56 
57 	/* NewGRF related data */
58 	/**
59 	 * Properties related the the grf file.
60 	 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
61 	 * Used for obtaining the sprite offset of custom sprites, and for
62 	 * evaluating callbacks.
63 	 */
64 	GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
65 	std::vector<WagonOverride> overrides;
66 	uint16 list_position;
67 
EngineEngine68 	Engine() {}
69 	Engine(VehicleType type, EngineID base);
70 	bool IsEnabled() const;
71 
72 	/**
73 	 * Determines the default cargo type of an engine.
74 	 *
75 	 * Usually a valid cargo is returned, even though the vehicle has zero capacity, and can therefore not carry anything. But the cargotype is still used
76 	 * for livery selection etc..
77 	 *
78 	 * Vehicles with CT_INVALID as default cargo are usually not available, but it can appear as default cargo of articulated parts.
79 	 *
80 	 * @return The default cargo type.
81 	 * @see CanCarryCargo
82 	 */
GetDefaultCargoTypeEngine83 	CargoID GetDefaultCargoType() const
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 
92 	/**
93 	 * Determines the default cargo capacity of an engine for display purposes.
94 	 *
95 	 * For planes carrying both passenger and mail this is the passenger capacity.
96 	 * For multiheaded engines this is the capacity of both heads.
97 	 * For articulated engines use GetCapacityOfArticulatedParts
98 	 *
99 	 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
100 	 * @return The default capacity
101 	 * @see GetDefaultCargoType
102 	 */
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;
116 	StringID GetAircraftTypeText() const;
117 
118 	/**
119 	 * Check whether the engine is hidden in the GUI for the given company.
120 	 * @param c Company to check.
121 	 * @return \c true iff the engine is hidden in the GUI for the given company.
122 	 */
IsHiddenEngine123 	inline bool IsHidden(CompanyID c) const
124 	{
125 		return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
126 	}
127 
128 	/**
129 	 * Check if the engine is a ground vehicle.
130 	 * @return True iff the engine is a train or a road vehicle.
131 	 */
IsGroundVehicleEngine132 	inline bool IsGroundVehicle() const
133 	{
134 		return this->type == VEH_TRAIN || this->type == VEH_ROAD;
135 	}
136 
137 	/**
138 	 * Retrieve the NewGRF the engine is tied to.
139 	 * This is the GRF providing the Action 3.
140 	 * @return NewGRF associated to the engine.
141 	 */
GetGRFEngine142 	const GRFFile *GetGRF() const
143 	{
144 		return this->grf_prop.grffile;
145 	}
146 
147 	uint32 GetGRFID() const;
148 
149 	struct EngineTypeFilter {
150 		VehicleType vt;
151 
operatorEngine::EngineTypeFilter152 		bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
153 	};
154 
155 	/**
156 	 * Returns an iterable ensemble of all valid engines of the given type
157 	 * @param vt the VehicleType for engines to be valid
158 	 * @param from index of the first engine to consider
159 	 * @return an iterable ensemble of all valid engines of the given type
160 	 */
161 	static Pool::IterateWrapperFiltered<Engine, EngineTypeFilter> IterateType(VehicleType vt, size_t from = 0)
162 	{
163 		return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
164 	}
165 };
166 
167 struct EngineIDMapping {
168 	uint32 grfid;          ///< The GRF ID of the file the entity belongs to
169 	uint16 internal_id;    ///< The internal ID within the GRF file
170 	VehicleType type;      ///< The engine type
171 	uint8  substitute_id;  ///< The (original) entity ID to use if this GRF is not available (currently not used)
172 };
173 
174 /**
175  * Stores the mapping of EngineID to the internal id of newgrfs.
176  * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
177  */
178 struct EngineOverrideManager : std::vector<EngineIDMapping> {
179 	static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries
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 
EngInfo(EngineID e)189 static inline const EngineInfo *EngInfo(EngineID e)
190 {
191 	return &Engine::Get(e)->info;
192 }
193 
RailVehInfo(EngineID e)194 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
195 {
196 	return &Engine::Get(e)->u.rail;
197 }
198 
RoadVehInfo(EngineID e)199 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
200 {
201 	return &Engine::Get(e)->u.road;
202 }
203 
ShipVehInfo(EngineID e)204 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
205 {
206 	return &Engine::Get(e)->u.ship;
207 }
208 
AircraftVehInfo(EngineID e)209 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
210 {
211 	return &Engine::Get(e)->u.air;
212 }
213 
214 #endif /* ENGINE_BASE_H */
215