Files @ r25754:d19207ab5c69
Branch filter:

Location: cpp/openttd-patchpack/source/src/engine_base.h - annotation

Patric Stout
Cleanup: remove SLE_NULL and friends

We no longer need them. If you want to remove a field .. just
remove it! Because of the headers in the savegame, on loading,
it will do the right thing and skip the field.

Do remember to bump the savegame version, as otherwise older
clients can still load the game, but will reset the field you
have removed .. that might be unintentially.
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r14248:a9050881acd7
r11969:3526c3cc75dc
r15753:7a5fae91288a
r9070:e059c65164f3
r25316:59902ba5b847
r25316:59902ba5b847
r25316:59902ba5b847
r25316:59902ba5b847
r25316:59902ba5b847
r25316:59902ba5b847
r11967:df0600d2c7e7
r11967:df0600d2c7e7
r9070:e059c65164f3
r11967:df0600d2c7e7
r24214:a65c412aafcc
r17474:de3bcebf8038
r9070:e059c65164f3
r17474:de3bcebf8038
r17474:de3bcebf8038
r17474:de3bcebf8038
r17474:de3bcebf8038
r17474:de3bcebf8038
r17474:de3bcebf8038
r17474:de3bcebf8038
r24225:887f73f568bf
r17474:de3bcebf8038
r19855:452c9b1a61e1
r23674:a5bafa1a02c5
r19855:452c9b1a61e1
r17474:de3bcebf8038
r21664:0999bd2815df
r12159:d9511457307a
r17474:de3bcebf8038
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r15753:7a5fae91288a
r15753:7a5fae91288a
r15753:7a5fae91288a
r15753:7a5fae91288a
r15753:7a5fae91288a
r15753:7a5fae91288a
r15753:7a5fae91288a
r25316:59902ba5b847
r9070:e059c65164f3
r9070:e059c65164f3
r25316:59902ba5b847
r9070:e059c65164f3
r17236:40bfbfc512f0
r9070:e059c65164f3
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r19944:25a78576fb5e
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r12733:799714c0c280
r23607:36c15679007d
r18327:3f476c73b5cb
r11188:143f3c64befd
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r18330:f0d616eef655
r23607:36c15679007d
r18330:f0d616eef655
r23607:36c15679007d
r18330:f0d616eef655
r18330:f0d616eef655
r10925:e45ad06a0739
r10927:eb69eaf0227d
r10967:2c98940d6c64
r10967:2c98940d6c64
r10967:2c98940d6c64
r11171:0ec43fa9bbfe
r12160:b49804528c72
r18646:67fc6c803d34
r22586:e1be37d1179e
r16774:e8f36b899894
r16774:e8f36b899894
r21664:0999bd2815df
r21664:0999bd2815df
r21664:0999bd2815df
r21664:0999bd2815df
r23674:a5bafa1a02c5
r21664:0999bd2815df
r21664:0999bd2815df
r21664:0999bd2815df
r21664:0999bd2815df
r21664:0999bd2815df
r16774:e8f36b899894
r16774:e8f36b899894
r16774:e8f36b899894
r18782:6453522c2154
r16774:e8f36b899894
r16774:e8f36b899894
r16774:e8f36b899894
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r18229:a35410711978
r23959:3272bfac7f4a
r24033:22cb1475e70a
r24033:22cb1475e70a
r24033:22cb1475e70a
r24033:22cb1475e70a
r24033:22cb1475e70a
r24033:22cb1475e70a
r23959:3272bfac7f4a
r23959:3272bfac7f4a
r23959:3272bfac7f4a
r23959:3272bfac7f4a
r23959:3272bfac7f4a
r23959:3272bfac7f4a
r24033:22cb1475e70a
r23959:3272bfac7f4a
r24033:22cb1475e70a
r23959:3272bfac7f4a
r9070:e059c65164f3
r9070:e059c65164f3
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r23661:3655a2df3316
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r23538:8df50944b27a
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r17225:dd046b807329
r17225:dd046b807329
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r11291:64ad346852e6
r9070:e059c65164f3
r9070:e059c65164f3
r11917:612c11f7ab47
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r11917:612c11f7ab47
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r11917:612c11f7ab47
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r11917:612c11f7ab47
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r9070:e059c65164f3
r11917:612c11f7ab47
r9070:e059c65164f3
r9070:e059c65164f3
r16849:d593358b4346
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file engine_base.h Base class for engines. */

#ifndef ENGINE_BASE_H
#define ENGINE_BASE_H

#include "engine_type.h"
#include "vehicle_type.h"
#include "core/pool_type.hpp"
#include "newgrf_commons.h"

struct WagonOverride {
	std::vector<EngineID> engines;
	CargoID cargo;
	const SpriteGroup *group;
};

typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
extern EnginePool _engine_pool;

struct Engine : EnginePool::PoolItem<&_engine_pool> {
	std::string name;           ///< Custom name of engine.
	Date intro_date;            ///< Date of introduction of the engine.
	Date age;
	uint16 reliability;         ///< Current reliability of the engine.
	uint16 reliability_spd_dec; ///< Speed of reliability decay between services (per day).
	uint16 reliability_start;   ///< Initial reliability of the engine.
	uint16 reliability_max;     ///< Maximal reliability of the engine.
	uint16 reliability_final;   ///< Final reliability of the engine.
	uint16 duration_phase_1;    ///< First reliability phase in months, increasing reliability from #reliability_start to #reliability_max.
	uint16 duration_phase_2;    ///< Second reliability phase in months, keeping #reliability_max.
	uint16 duration_phase_3;    ///< Third reliability phase in months, decaying to #reliability_final.
	byte flags;                 ///< Flags of the engine. @see EngineFlags
	CompanyMask preview_asked;  ///< Bit for each company which has already been offered a preview.
	CompanyID preview_company;  ///< Company which is currently being offered a preview \c INVALID_COMPANY means no company.
	byte preview_wait;          ///< Daily countdown timer for timeout of offering the engine to the #preview_company company.
	CompanyMask company_avail;  ///< Bit for each company whether the engine is available for that company.
	CompanyMask company_hidden; ///< Bit for each company whether the engine is normally hidden in the build gui for that company.
	uint8 original_image_index; ///< Original vehicle image index, thus the image index of the overridden vehicle
	VehicleType type;           ///< %Vehicle type, ie #VEH_ROAD, #VEH_TRAIN, etc.

	EngineInfo info;

	union {
		RailVehicleInfo rail;
		RoadVehicleInfo road;
		ShipVehicleInfo ship;
		AircraftVehicleInfo air;
	} u;

	/* NewGRF related data */
	/**
	 * Properties related the the grf file.
	 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
	 * Used for obtaining the sprite offset of custom sprites, and for
	 * evaluating callbacks.
	 */
	GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
	std::vector<WagonOverride> overrides;
	uint16 list_position;

	Engine() {}
	Engine(VehicleType type, EngineID base);
	bool IsEnabled() const;

	/**
	 * Determines the default cargo type of an engine.
	 *
	 * 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
	 * for livery selection etc..
	 *
	 * Vehicles with CT_INVALID as default cargo are usually not available, but it can appear as default cargo of articulated parts.
	 *
	 * @return The default cargo type.
	 * @see CanCarryCargo
	 */
	CargoID GetDefaultCargoType() const
	{
		return this->info.cargo_type;
	}

	uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = nullptr) const;

	bool CanCarryCargo() const;

	/**
	 * Determines the default cargo capacity of an engine for display purposes.
	 *
	 * For planes carrying both passenger and mail this is the passenger capacity.
	 * For multiheaded engines this is the capacity of both heads.
	 * For articulated engines use GetCapacityOfArticulatedParts
	 *
	 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
	 * @return The default capacity
	 * @see GetDefaultCargoType
	 */
	uint GetDisplayDefaultCapacity(uint16 *mail_capacity = nullptr) const
	{
		return this->DetermineCapacity(nullptr, mail_capacity);
	}

	Money GetRunningCost() const;
	Money GetCost() const;
	uint GetDisplayMaxSpeed() const;
	uint GetPower() const;
	uint GetDisplayWeight() const;
	uint GetDisplayMaxTractiveEffort() const;
	Date GetLifeLengthInDays() const;
	uint16 GetRange() const;
	StringID GetAircraftTypeText() const;

	/**
	 * Check whether the engine is hidden in the GUI for the given company.
	 * @param c Company to check.
	 * @return \c true iff the engine is hidden in the GUI for the given company.
	 */
	inline bool IsHidden(CompanyID c) const
	{
		return c < MAX_COMPANIES && HasBit(this->company_hidden, c);
	}

	/**
	 * Check if the engine is a ground vehicle.
	 * @return True iff the engine is a train or a road vehicle.
	 */
	inline bool IsGroundVehicle() const
	{
		return this->type == VEH_TRAIN || this->type == VEH_ROAD;
	}

	/**
	 * Retrieve the NewGRF the engine is tied to.
	 * This is the GRF providing the Action 3.
	 * @return NewGRF associated to the engine.
	 */
	const GRFFile *GetGRF() const
	{
		return this->grf_prop.grffile;
	}

	uint32 GetGRFID() const;

	struct EngineTypeFilter {
		VehicleType vt;

		bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
	};

	/**
	 * Returns an iterable ensemble of all valid engines of the given type
	 * @param vt the VehicleType for engines to be valid
	 * @param from index of the first engine to consider
	 * @return an iterable ensemble of all valid engines of the given type
	 */
	static Pool::IterateWrapperFiltered<Engine, EngineTypeFilter> IterateType(VehicleType vt, size_t from = 0)
	{
		return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
	}
};

struct EngineIDMapping {
	uint32 grfid;          ///< The GRF ID of the file the entity belongs to
	uint16 internal_id;    ///< The internal ID within the GRF file
	VehicleType type;      ///< The engine type
	uint8  substitute_id;  ///< The (original) entity ID to use if this GRF is not available (currently not used)
};

/**
 * Stores the mapping of EngineID to the internal id of newgrfs.
 * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
 */
struct EngineOverrideManager : std::vector<EngineIDMapping> {
	static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries

	void ResetToDefaultMapping();
	EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);

	static bool ResetToCurrentNewGRFConfig();
};

extern EngineOverrideManager _engine_mngr;

static inline const EngineInfo *EngInfo(EngineID e)
{
	return &Engine::Get(e)->info;
}

static inline const RailVehicleInfo *RailVehInfo(EngineID e)
{
	return &Engine::Get(e)->u.rail;
}

static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
{
	return &Engine::Get(e)->u.road;
}

static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
{
	return &Engine::Get(e)->u.ship;
}

static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
{
	return &Engine::Get(e)->u.air;
}

#endif /* ENGINE_BASE_H */