Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / engine_base.h
blob29cb238094057850c3c475e9200d5dff6ba6165b
1 /* $Id$ */
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 */
10 /** @file engine_base.h Base class for engines. */
12 #ifndef ENGINE_BASE_H
13 #define ENGINE_BASE_H
15 #include "engine_type.h"
16 #include "vehicle_type.h"
17 #include "core/pool_type.hpp"
18 #include "newgrf_commons.h"
20 /** Variable part of an engine. */
21 struct EngineState {
22 char *name; ///< Custom name of engine.
23 Date intro_date; ///< Date of introduction of the engine.
24 Date age;
25 uint16 reliability; ///< Current reliability of the engine.
26 uint16 reliability_spd_dec; ///< Speed of reliability decay between services (per day).
27 uint16 reliability_start; ///< Initial reliability of the engine.
28 uint16 reliability_max; ///< Maximal reliability of the engine.
29 uint16 reliability_final; ///< Final reliability of the engine.
30 uint16 duration_phase_1; ///< First reliability phase in months, increasing reliability from #reliability_start to #reliability_max.
31 uint16 duration_phase_2; ///< Second reliability phase in months, keeping #reliability_max.
32 uint16 duration_phase_3; ///< Third reliability phase on months, decaying to #reliability_final.
33 byte flags; ///< Flags of the engine. @see EngineFlags
34 CompanyMask preview_asked; ///< Bit for each company which has already been offered a preview.
35 CompanyByte preview_company;///< Company which is currently being offered a preview \c INVALID_COMPANY means no company.
36 byte preview_wait; ///< Daily countdown timer for timeout of offering the engine to the #preview_company company.
37 CompanyMask company_avail; ///< Bit for each company whether the engine is available for that company.
38 CompanyMask company_hidden; ///< Bit for each company whether the engine is normally hidden in the build gui for that company.
40 EngineState() : name(NULL) { }
42 EngineState (const EngineState &es)
43 : name (es.name != NULL ? xstrdup (es.name) : NULL),
44 intro_date (es.intro_date),
45 age (es.age),
46 reliability (es.reliability),
47 reliability_spd_dec (es.reliability_spd_dec),
48 reliability_start (es.reliability_start),
49 reliability_max (es.reliability_max),
50 reliability_final (es.reliability_final),
51 duration_phase_1 (es.duration_phase_1),
52 duration_phase_2 (es.duration_phase_2),
53 duration_phase_3 (es.duration_phase_3),
54 flags (es.flags),
55 preview_asked (es.preview_asked),
56 preview_company (es.preview_company),
57 preview_wait (es.preview_wait),
58 company_avail (es.company_avail),
59 company_hidden (es.company_hidden)
63 ~EngineState()
65 free (this->name);
68 void reset (const EngineInfo *ei, Date aging_date);
71 /** An engine, as used in the game. */
72 struct Engine : PooledItem <Engine, EngineID, 64, 64000>, EngineState {
73 uint8 original_image_index; ///< Original vehicle image index, thus the image index of the overridden vehicle
74 VehicleType type; ///< %Vehicle type, ie #VEH_ROAD, #VEH_TRAIN, etc.
76 EngineInfo info;
78 union {
79 RailVehicleInfo rail;
80 RoadVehicleInfo road;
81 ShipVehicleInfo ship;
82 AircraftVehicleInfo air;
83 } u;
85 /* NewGRF related data */
86 GRFFilePropsBase grf_prop; ///< Properties related the the grf file.
87 /**
88 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
89 * Used for obtaining the sprite offset of custom sprites, and for
90 * evaluating callbacks.
92 const struct SpriteGroup *spritegroup[NUM_CARGO + 2];
93 uint16 overrides_count;
94 struct WagonOverride *overrides;
95 uint16 list_position;
97 Engine();
98 Engine(VehicleType type, EngineID base);
99 ~Engine();
100 bool IsEnabled() const;
103 * Determines the default cargo type of an engine.
105 * 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
106 * for livery selection etc..
108 * Vehicles with CT_INVALID as default cargo are usually not available, but it can appear as default cargo of articulated parts.
110 * @return The default cargo type.
111 * @see CanCarryCargo
113 CargoID GetDefaultCargoType() const
115 return this->info.cargo_type;
118 uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = NULL) const;
120 bool CanCarryCargo() const;
123 * Determines the default cargo capacity of an engine for display purposes.
125 * For planes carrying both passenger and mail this is the passenger capacity.
126 * For multiheaded engines this is the capacity of both heads.
127 * For articulated engines use GetCapacityOfArticulatedParts
129 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
130 * @return The default capacity
131 * @see GetDefaultCargoType
133 uint GetDisplayDefaultCapacity(uint16 *mail_capacity = NULL) const
135 return this->DetermineCapacity(NULL, mail_capacity);
138 Money GetRunningCost() const;
139 Money GetCost() const;
140 uint GetDisplayMaxSpeed() const;
141 uint GetPower() const;
142 uint GetDisplayWeight() const;
143 uint GetDisplayMaxTractiveEffort() const;
144 Date GetLifeLengthInDays() const;
145 uint16 GetRange() const;
146 StringID GetAircraftTypeText() const;
149 * Check whether the engine is hidden in the GUI for the given company.
150 * @param c Company to check.
151 * @return \c true iff the engine is hidden in the GUI for the given company.
153 inline bool IsHidden(CompanyByte c) const
155 return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
159 * Check if the engine is a ground vehicle.
160 * @return True iff the engine is a train or a road vehicle.
162 inline bool IsGroundVehicle() const
164 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
168 * Retrieve the NewGRF the engine is tied to.
169 * This is the GRF providing the Action 3.
170 * @return NewGRF associated to the engine.
172 const GRFFile *GetGRF() const
174 return this->grf_prop.grffile;
177 uint32 GetGRFID() const;
180 struct EngineIDMapping {
181 uint32 grfid; ///< The GRF ID of the file the entity belongs to
182 uint16 internal_id; ///< The internal ID within the GRF file
183 VehicleTypeByte type; ///< The engine type
184 uint8 substitute_id; ///< The (original) entity ID to use if this GRF is not available (currently not used)
188 * Stores the mapping of EngineID to the internal id of newgrfs.
189 * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
191 struct EngineOverrideManager : SmallVector<EngineIDMapping, 256> {
192 static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries
194 void ResetToDefaultMapping();
195 EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
197 static bool ResetToCurrentNewGRFConfig();
200 extern EngineOverrideManager _engine_mngr;
202 #define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start)
203 #define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0)
205 #define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type)
207 static inline const EngineInfo *EngInfo(EngineID e)
209 return &Engine::Get(e)->info;
212 static inline const RailVehicleInfo *RailVehInfo(EngineID e)
214 return &Engine::Get(e)->u.rail;
217 static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
219 return &Engine::Get(e)->u.road;
222 static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
224 return &Engine::Get(e)->u.ship;
227 static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
229 return &Engine::Get(e)->u.air;
232 #endif /* ENGINE_BASE_H */