diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index 54e8b22921..81c51d1fdb 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -354,7 +354,7 @@ static bool DisasterTick_Ufo(DisasterVehicle *v) return false; } /* Target it. */ - v->dest_tile = TileIndex{u->index}; + v->dest_tile = TileIndex{u->index.base()}; v->age = CalendarTime::MIN_DATE; u->disaster_vehicle = v->index; break; diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index 0cf2826bea..78b37cdf57 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -141,7 +141,7 @@ struct SelectGameWindow : public Window { for (char c : match[2].str()) { if (isdigit(c)) { if (id_type == ID_VEHICLE) { - vc.vehicle = static_cast(vc.vehicle * 10 + (c - '0')); + vc.vehicle = static_cast(vc.vehicle.base() * 10 + (c - '0')); } } else { id_type = ID_NONE; @@ -155,7 +155,7 @@ struct SelectGameWindow : public Window { case 'C': vc.align_h = IntroGameViewportCommand::CENTRE; break; case 'R': vc.align_h = IntroGameViewportCommand::RIGHT; break; case 'P': vc.pan_to_next = true; break; - case 'V': id_type = ID_VEHICLE; vc.vehicle = VEHICLE_BEGIN; break; + case 'V': id_type = ID_VEHICLE; vc.vehicle = VehicleID::Begin(); break; } } } diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index 9ccb0f4b54..19ca491418 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -335,7 +335,7 @@ struct NewGRFInspectWindow : Window { assert(this->HasChainIndex()); const Vehicle *v = Vehicle::Get(index); v = v->Move(this->chain_index); - if (v != nullptr) index = v->index; + if (v != nullptr) index = v->index.base(); } return index; } diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 5f7b1ad06b..3a9824c50a 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -726,8 +726,8 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec case 0x01: return MapOldSubType(v); case 0x02: break; // not implemented case 0x03: break; // not implemented - case 0x04: return v->index; - case 0x05: return GB(v->index, 8, 8); + case 0x04: return v->index.base(); + case 0x05: return GB(v->index.base(), 8, 8); case 0x06: break; // not implemented case 0x07: break; // not implemented case 0x08: break; // not implemented @@ -839,7 +839,7 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec case 0x57: return GB(ClampTo(v->GetDisplayProfitLastYear()), 8, 24); case 0x58: return GB(ClampTo(v->GetDisplayProfitLastYear()), 16, 16); case 0x59: return GB(ClampTo(v->GetDisplayProfitLastYear()), 24, 8); - case 0x5A: return v->Next() == nullptr ? INVALID_VEHICLE : v->Next()->index; + case 0x5A: return (v->Next() == nullptr ? INVALID_VEHICLE : v->Next()->index).base(); case 0x5B: break; // not implemented case 0x5C: return ClampTo(v->value); case 0x5D: return GB(ClampTo(v->value), 8, 24); @@ -891,8 +891,8 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec case 0x75: return GB(t->gcache.cached_power, 8, 24); case 0x76: return GB(t->gcache.cached_power, 16, 16); case 0x77: return GB(t->gcache.cached_power, 24, 8); - case 0x7C: return t->First()->index; - case 0x7D: return GB(t->First()->index, 8, 8); + case 0x7C: return t->First()->index.base(); + case 0x7D: return GB(t->First()->index.base(), 8, 8); case 0x7F: return 0; // Used for vehicle reversing hack in TTDP } break; @@ -964,7 +964,7 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec case 0xC4: return (Clamp(TimerGameCalendar::year, CalendarTime::ORIGINAL_BASE_YEAR, CalendarTime::ORIGINAL_MAX_YEAR) - CalendarTime::ORIGINAL_BASE_YEAR).base(); // Build year case 0xC6: return Engine::Get(this->self_type)->grf_prop.local_id; case 0xC7: return GB(Engine::Get(this->self_type)->grf_prop.local_id, 8, 8); - case 0xDA: return INVALID_VEHICLE; // Next vehicle + case 0xDA: return INVALID_VEHICLE.base(); // Next vehicle case 0xF2: return 0; // Cargo subtype } diff --git a/src/news_gui.cpp b/src/news_gui.cpp index 22e535e0f2..011f8f624f 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -920,7 +920,7 @@ uint32_t SerialiseNewsReference(const NewsReference &reference) struct visitor { uint32_t operator()(const std::monostate &) { return 0; } uint32_t operator()(const TileIndex &t) { return t.base(); } - uint32_t operator()(const VehicleID v) { return v; } + uint32_t operator()(const VehicleID v) { return v.base(); } uint32_t operator()(const StationID s) { return s; } uint32_t operator()(const IndustryID i) { return i.base(); } uint32_t operator()(const TownID t) { return t.base(); } @@ -1084,7 +1084,7 @@ void ChangeVehicleNews(VehicleID from_index, VehicleID to_index) for (auto &ni : _news) { ChangeObject(ni.ref1, from_index, to_index); ChangeObject(ni.ref2, from_index, to_index); - if (ni.flags.Test(NewsFlag::VehicleParam0) && std::get(ni.params[0]) == from_index) ni.params[0] = to_index; + if (ni.flags.Test(NewsFlag::VehicleParam0) && std::get(ni.params[0]) == from_index) ni.params[0] = to_index.base(); } } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 9640e85c8b..20a84db9ac 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -176,7 +176,7 @@ void FixOldVehicles(LoadgameState &ls) /* Vehicle-subtype is different in TTD(Patch) */ if (v->type == VEH_EFFECT) v->subtype = v->subtype >> 1; - v->name = CopyFromOldName(ls.vehicle_names[v->index]); + v->name = CopyFromOldName(ls.vehicle_names[v->index.base()]); /* We haven't used this bit for stations for ages */ if (v->type == VEH_ROAD) { @@ -1048,7 +1048,7 @@ static bool LoadOldCompany(LoadgameState &ls, int num) static uint32_t _old_order_ptr; static uint16_t _old_next_ptr; -static VehicleID _current_vehicle_id; +static typename VehicleID::BaseType _current_vehicle_id; static const OldChunks vehicle_train_chunk[] = { OCL_SVAR( OC_UINT8, Train, track ), @@ -1250,7 +1250,7 @@ bool LoadOldVehicle(LoadgameState &ls, int num) ReadTTDPatchFlags(ls); for (uint i = 0; i < ls.vehicle_multiplier; i++) { - _current_vehicle_id = static_cast(num * ls.vehicle_multiplier + i); + _current_vehicle_id = num * ls.vehicle_multiplier + i; Vehicle *v; diff --git a/src/script/api/script_order.cpp b/src/script/api/script_order.cpp index 17df83d166..9fe3195075 100644 --- a/src/script/api/script_order.cpp +++ b/src/script/api/script_order.cpp @@ -633,7 +633,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance) /* static */ bool ScriptOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, ScriptOrder::ScriptOrderFlags order_flags) { - ScriptObject::SetCallbackVariable(0, vehicle_id); + ScriptObject::SetCallbackVariable(0, vehicle_id.base()); ScriptObject::SetCallbackVariable(1, order_position); ScriptObject::SetCallbackVariable(2, order_flags); /* In case another client(s) change orders at the same time we could diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp index 2cafdfa825..a36a22aad7 100644 --- a/src/script/api/script_vehicle.cpp +++ b/src/script/api/script_vehicle.cpp @@ -84,7 +84,7 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, engine_id, true, cargo, INVALID_CLIENT_ID)) return VEHICLE_INVALID; /* In case of test-mode, we return VehicleID 0 */ - return ::VEHICLE_BEGIN; + return VehicleID::Begin(); } /* static */ VehicleID ScriptVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) @@ -115,7 +115,7 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, vehicle_id, share_orders)) return VEHICLE_INVALID; /* In case of test-mode, we return VehicleID 0 */ - return ::VEHICLE_BEGIN; + return VehicleID::Begin(); } /* static */ bool ScriptVehicle::_MoveWagonInternal(VehicleID source_vehicle_id, SQInteger source_wagon, bool move_attached_wagons, SQInteger dest_vehicle_id, SQInteger dest_wagon) diff --git a/src/script/api/script_vehicle.hpp b/src/script/api/script_vehicle.hpp index bc687929ec..b2d3f35e55 100644 --- a/src/script/api/script_vehicle.hpp +++ b/src/script/api/script_vehicle.hpp @@ -95,7 +95,7 @@ public: VS_INVALID = 0xFF, ///< An invalid vehicle state. }; - static const VehicleID VEHICLE_INVALID = ::INVALID_VEHICLE; ///< Invalid VehicleID. + static constexpr VehicleID VEHICLE_INVALID = ::INVALID_VEHICLE; ///< Invalid VehicleID. /** * Checks whether the given vehicle is valid and owned by you. diff --git a/src/script/api/script_vehiclelist.cpp b/src/script/api/script_vehiclelist.cpp index 32cb6e185e..68113654e5 100644 --- a/src/script/api/script_vehiclelist.cpp +++ b/src/script/api/script_vehiclelist.cpp @@ -44,7 +44,7 @@ ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id) FindVehiclesWithOrder( [is_deity, owner](const Vehicle *v) { return is_deity || v->owner == owner; }, [station_id](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id; }, - [this](const Vehicle *v) { this->AddItem(v->index); } + [this](const Vehicle *v) { this->AddItem(v->index.base()); } ); } @@ -91,7 +91,7 @@ ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile) FindVehiclesWithOrder( [is_deity, owner, type](const Vehicle *v) { return (is_deity || v->owner == owner) && v->type == type; }, [dest](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest; }, - [this](const Vehicle *v) { this->AddItem(v->index); } + [this](const Vehicle *v) { this->AddItem(v->index.base()); } ); } @@ -100,7 +100,7 @@ ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return; for (const Vehicle *v = Vehicle::Get(vehicle_id)->FirstShared(); v != nullptr; v = v->NextShared()) { - this->AddItem(v->index); + this->AddItem(v->index.base()); } } diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index a6adca7626..8f22ed1769 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -655,7 +655,7 @@ struct TimetableWindow : Window { this->change_timetable_all = _ctrl_pressed; ShowQueryString({}, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED); } else { - ShowSetDateWindow(this, v->index, TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast(static_cast(_ctrl_pressed))); + ShowSetDateWindow(this, v->index.base(), TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast(static_cast(_ctrl_pressed))); } break; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 33e372f8ec..2bc7d1a421 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -994,7 +994,7 @@ void CallVehicleTicks() PerformanceAccumulator::Reset(PFE_GL_AIRCRAFT); for (Vehicle *v : Vehicle::Iterate()) { - [[maybe_unused]] size_t vehicle_index = v->index; + [[maybe_unused]] VehicleID vehicle_index = v->index; /* Vehicle could be deleted in this tick */ if (!v->Tick()) { @@ -3007,7 +3007,7 @@ void Vehicle::RemoveFromShared() } else if (were_first) { /* If we were the first one, update to the new first one. * Note: FirstShared() is already the new first */ - InvalidateWindowData(GetWindowClassForVehicleType(this->type), vli.ToWindowNumber(), this->FirstShared()->index | (1U << 31)); + InvalidateWindowData(GetWindowClassForVehicleType(this->type), vli.ToWindowNumber(), this->FirstShared()->index.base() | (1U << 31)); } this->next_shared = nullptr; diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 6fa7fb165d..cd5bcccabc 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -196,7 +196,7 @@ struct MutableSpriteCache { }; /** A vehicle pool for a little over 1 million vehicles. */ -typedef Pool VehiclePool; +typedef Pool VehiclePool; extern VehiclePool _vehicle_pool; /* Some declarations of functions, so we can make them friendly */ diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 5b7054f0d3..4725b4909e 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1578,7 +1578,7 @@ static inline void ChangeVehicleWindow(WindowClass window_class, VehicleID from_ if (w->viewport != nullptr) w->viewport->follow_vehicle = to_index; /* Update vehicle drag data */ - if (_thd.window_class == window_class && _thd.window_number == (WindowNumber)from_index) { + if (_thd.window_class == window_class && _thd.window_number == from_index) { _thd.window_number = to_index; } @@ -2335,7 +2335,7 @@ void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type) void ShowVehicleListWindow(const Vehicle *v) { - ShowVehicleListWindowLocal(v->owner, VL_SHARED_ORDERS, v->type, v->FirstShared()->index); + ShowVehicleListWindowLocal(v->owner, VL_SHARED_ORDERS, v->type, v->FirstShared()->index.base()); } void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, StationID station) diff --git a/src/vehicle_type.h b/src/vehicle_type.h index b4e900b790..4a8fc419b8 100644 --- a/src/vehicle_type.h +++ b/src/vehicle_type.h @@ -11,13 +11,11 @@ #define VEHICLE_TYPE_H #include "core/enum_type.hpp" +#include "core/pool_type.hpp" /** The type all our vehicle IDs have. */ -enum VehicleID : uint32_t { - VEHICLE_BEGIN = 0, - VEHICLE_END = 0xFF000, - INVALID_VEHICLE = 0xFFFFF ///< Constant representing a non-existing vehicle. -}; +using VehicleID = PoolID; +static constexpr VehicleID INVALID_VEHICLE = VehicleID::Invalid(); ///< Constant representing a non-existing vehicle. static const int GROUND_ACCELERATION = 9800; ///< Acceleration due to gravity, 9.8 m/s^2