1
0
Fork 0

Codechange: strongly type EngineID

pull/13579/head
Rubidium 2025-02-04 20:55:28 +01:00 committed by rubidium42
parent 17b97e5d1b
commit 7e04651220
10 changed files with 27 additions and 31 deletions

View File

@ -508,11 +508,11 @@ bool Engine::IsVariantHidden(CompanyID c) const
*/
void EngineOverrideManager::ResetToDefaultMapping()
{
EngineID id = ENGINE_BEGIN;
EngineID id = EngineID::Begin();
for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
auto &map = this->mappings[type];
map.clear();
for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++, id++) {
for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++, ++id) {
map.emplace_back(INVALID_GRFID, internal_id, type, internal_id, id);
}
}
@ -610,7 +610,7 @@ void SetupEngines()
assert(std::size(mapping) >= _engine_counts[type]);
for (const EngineIDMapping &eid : mapping) {
new (eid.engine) Engine(type, eid.internal_id);
new (eid.engine.base()) Engine(type, eid.internal_id);
}
}
}
@ -755,7 +755,7 @@ void StartupOneEngine(Engine *e, const TimerGameCalendar::YearMonthDay &aging_ym
}
SetRandomSeed(_settings_game.game_creation.generation_seed ^ seed ^
(re->index << 16) ^ (re->info.base_intro.base() << 12) ^ (re->info.decay_speed << 8) ^
(re->index.base() << 16) ^ (re->info.base_intro.base() << 12) ^ (re->info.decay_speed << 8) ^
(re->info.lifelength.base() << 4) ^ re->info.retire_early ^
e->type ^
e->GetGRFID());

View File

@ -32,7 +32,7 @@ enum class EngineDisplayFlag : uint8_t {
using EngineDisplayFlags = EnumBitSet<EngineDisplayFlag, uint8_t>;
typedef Pool<Engine, EngineID, 64, ENGINE_END> EnginePool;
typedef Pool<Engine, EngineID, 64, EngineID::End().base()> EnginePool;
extern EnginePool _engine_pool;
struct Engine : EnginePool::PoolItem<&_engine_pool> {

View File

@ -21,12 +21,8 @@
#include "strings_type.h"
/** Unique identification number of an engine. */
enum EngineID : uint16_t {
ENGINE_BEGIN = 0,
ENGINE_END = 64000,
INVALID_ENGINE = 0xFFFF ///< Constant denoting an invalid engine.
};
DECLARE_INCREMENT_DECREMENT_OPERATORS(EngineID)
using EngineID = PoolID<uint16_t, struct EngineIDTag, 64000, 0xFFFF>;
static constexpr EngineID INVALID_ENGINE = EngineID::Invalid(); ///< Constant denoting an invalid engine.
struct Engine;
@ -214,7 +210,7 @@ enum class EngineNameContext : uint8_t {
/** Combine an engine ID and a name context to an engine name dparam. */
inline uint64_t PackEngineNameDParam(EngineID engine_id, EngineNameContext context, uint32_t extra_data = 0)
{
return engine_id | (static_cast<uint64_t>(context) << 32) | (static_cast<uint64_t>(extra_data) << 40);
return engine_id.base() | (static_cast<uint64_t>(context) << 32) | (static_cast<uint64_t>(extra_data) << 40);
}
static const uint MAX_LENGTH_ENGINE_NAME_CHARS = 32; ///< The maximum length of an engine name in characters including '\0'

View File

@ -345,7 +345,7 @@ struct GRFTempEngineData {
}
};
static std::vector<GRFTempEngineData> _gted; ///< Temporary engine data used during NewGRF loading
static ReferenceThroughBaseContainer<std::vector<GRFTempEngineData>> _gted; ///< Temporary engine data used during NewGRF loading
/**
* Contains the GRF ID of the owner of a vehicle if it has been reserved.
@ -6378,7 +6378,7 @@ static void FeatureNewName(ByteReader &buf)
if (!generic) {
Engine *e = GetNewEngine(_cur.grffile, (VehicleType)feature, id, _cur.grfconfig->flags.Test(GRFConfigFlag::Static));
if (e == nullptr) break;
StringID string = AddGRFString(_cur.grffile->grfid, GRFStringID{e->index}, lang, new_scheme, false, name, e->info.string_id);
StringID string = AddGRFString(_cur.grffile->grfid, GRFStringID{e->index.base()}, lang, new_scheme, false, name, e->info.string_id);
e->info.string_id = string;
} else {
AddGRFString(_cur.grffile->grfid, GRFStringID{id}, lang, new_scheme, true, name, STR_UNDEFINED);
@ -9245,7 +9245,7 @@ static void FinaliseEngineArray()
/* Do final mapping on variant engine ID. */
if (e->info.variant_id != INVALID_ENGINE) {
e->info.variant_id = GetNewEngineID(e->grf_prop.grffile, e->type, e->info.variant_id);
e->info.variant_id = GetNewEngineID(e->grf_prop.grffile, e->type, e->info.variant_id.base());
}
if (!e->info.climates.Test(_settings_game.game_creation.landscape)) continue;

View File

@ -1332,7 +1332,7 @@ void CommitVehicleListOrderChanges()
{
/* Build a list of EngineIDs. EngineIDs are sequential from 0 up to the number of pool items with no gaps. */
std::vector<EngineID> ordering(Engine::GetNumItems());
std::iota(std::begin(ordering), std::end(ordering), ENGINE_BEGIN);
std::iota(std::begin(ordering), std::end(ordering), EngineID::Begin());
/* Pre-sort engines by scope-grfid and local index */
std::ranges::sort(ordering, EnginePreSort);

View File

@ -924,7 +924,7 @@ uint32_t SerialiseNewsReference(const NewsReference &reference)
uint32_t operator()(const StationID s) { return s; }
uint32_t operator()(const IndustryID i) { return i; }
uint32_t operator()(const TownID t) { return t; }
uint32_t operator()(const EngineID e) { return e; }
uint32_t operator()(const EngineID e) { return e.base(); }
};
return std::visit(visitor{}, reference);

View File

@ -41,7 +41,7 @@ static const SaveLoad _engine_desc[] = {
SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION),
};
static std::vector<Engine> _temp_engine;
static ReferenceThroughBaseContainer<std::vector<Engine>> _temp_engine;
Engine *GetTempDataEngine(EngineID index)
{
@ -134,12 +134,12 @@ struct ENGSChunkHandler : ChunkHandler {
{
/* Load old separate String ID list into a temporary array. This
* was always 256 entries. */
StringID names[256];
ReferenceThroughBaseContainer<std::array<StringID, 256>> names{};
SlCopy(names, lengthof(names), SLE_STRINGID);
SlCopy(names.data(), std::size(names), SLE_STRINGID);
/* Copy each string into the temporary engine array. */
for (EngineID engine = ENGINE_BEGIN; engine < lengthof(names); engine++) {
for (EngineID engine = EngineID::Begin(); engine < std::size(names); ++engine) {
Engine *e = GetTempDataEngine(engine);
e->name = CopyFromOldName(names[engine]);
}

View File

@ -373,23 +373,23 @@ static bool FixTTOEngines()
for (Vehicle *v : Vehicle::Iterate()) {
if (v->engine_type >= lengthof(tto_to_ttd)) return false;
v->engine_type = static_cast<EngineID>(tto_to_ttd[v->engine_type]);
v->engine_type = static_cast<EngineID>(tto_to_ttd[v->engine_type.base()]);
}
/* Load the default engine set. Many of them will be overridden later */
{
EngineID j = ENGINE_BEGIN;
for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_TRAIN, i);
for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_ROAD, i);
for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_SHIP, i);
for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_AIRCRAFT, i);
EngineID j = EngineID::Begin();
for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_TRAIN, i);
for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_ROAD, i);
for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_SHIP, i);
for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_AIRCRAFT, i);
}
TimerGameCalendar::Date aging_date = std::min(TimerGameCalendar::date + CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR, TimerGameCalendar::ConvertYMDToDate(TimerGameCalendar::Year{2050}, 0, 1));
TimerGameCalendar::YearMonthDay aging_ymd = TimerGameCalendar::ConvertDateToYMD(aging_date);
for (EngineID i = ENGINE_BEGIN; i < 256; i++) {
OldEngineID oi = ttd_to_tto[i];
for (EngineID i = EngineID::Begin(); i < 256; ++i) {
OldEngineID oi = ttd_to_tto[i.base()];
Engine *e = GetTempDataEngine(i);
if (oi == 255) {

View File

@ -19,6 +19,6 @@ ScriptEngineList::ScriptEngineList(ScriptVehicle::VehicleType vehicle_type)
bool is_deity = ScriptCompanyMode::IsDeity();
::CompanyID owner = ScriptObject::GetCompany();
for (const Engine *e : Engine::IterateType((::VehicleType)vehicle_type)) {
if (is_deity || e->company_avail.Test(owner)) this->AddItem(e->index);
if (is_deity || e->company_avail.Test(owner)) this->AddItem(e->index.base());
}
}

View File

@ -1519,7 +1519,7 @@ static bool VehicleMaxSpeedSorter(const Vehicle * const &a, const Vehicle * cons
/** Sort vehicles by model */
static bool VehicleModelSorter(const Vehicle * const &a, const Vehicle * const &b)
{
int r = a->engine_type - b->engine_type;
int r = a->engine_type.base() - b->engine_type.base();
return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
}