1
0
Fork 0

Codechange: strongly type VehicleID

pull/13579/head
Rubidium 2025-02-02 10:41:00 +01:00 committed by rubidium42
parent 1003967267
commit 70c9f3963c
15 changed files with 31 additions and 33 deletions

View File

@ -354,7 +354,7 @@ static bool DisasterTick_Ufo(DisasterVehicle *v)
return false; return false;
} }
/* Target it. */ /* Target it. */
v->dest_tile = TileIndex{u->index}; v->dest_tile = TileIndex{u->index.base()};
v->age = CalendarTime::MIN_DATE; v->age = CalendarTime::MIN_DATE;
u->disaster_vehicle = v->index; u->disaster_vehicle = v->index;
break; break;

View File

@ -141,7 +141,7 @@ struct SelectGameWindow : public Window {
for (char c : match[2].str()) { for (char c : match[2].str()) {
if (isdigit(c)) { if (isdigit(c)) {
if (id_type == ID_VEHICLE) { if (id_type == ID_VEHICLE) {
vc.vehicle = static_cast<VehicleID>(vc.vehicle * 10 + (c - '0')); vc.vehicle = static_cast<VehicleID>(vc.vehicle.base() * 10 + (c - '0'));
} }
} else { } else {
id_type = ID_NONE; id_type = ID_NONE;
@ -155,7 +155,7 @@ struct SelectGameWindow : public Window {
case 'C': vc.align_h = IntroGameViewportCommand::CENTRE; break; case 'C': vc.align_h = IntroGameViewportCommand::CENTRE; break;
case 'R': vc.align_h = IntroGameViewportCommand::RIGHT; break; case 'R': vc.align_h = IntroGameViewportCommand::RIGHT; break;
case 'P': vc.pan_to_next = true; 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;
} }
} }
} }

View File

@ -335,7 +335,7 @@ struct NewGRFInspectWindow : Window {
assert(this->HasChainIndex()); assert(this->HasChainIndex());
const Vehicle *v = Vehicle::Get(index); const Vehicle *v = Vehicle::Get(index);
v = v->Move(this->chain_index); v = v->Move(this->chain_index);
if (v != nullptr) index = v->index; if (v != nullptr) index = v->index.base();
} }
return index; return index;
} }

View File

@ -726,8 +726,8 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec
case 0x01: return MapOldSubType(v); case 0x01: return MapOldSubType(v);
case 0x02: break; // not implemented case 0x02: break; // not implemented
case 0x03: break; // not implemented case 0x03: break; // not implemented
case 0x04: return v->index; case 0x04: return v->index.base();
case 0x05: return GB(v->index, 8, 8); case 0x05: return GB(v->index.base(), 8, 8);
case 0x06: break; // not implemented case 0x06: break; // not implemented
case 0x07: break; // not implemented case 0x07: break; // not implemented
case 0x08: 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<int32_t>(v->GetDisplayProfitLastYear()), 8, 24); case 0x57: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 8, 24);
case 0x58: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 16, 16); case 0x58: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 16, 16);
case 0x59: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 24, 8); case 0x59: return GB(ClampTo<int32_t>(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 0x5B: break; // not implemented
case 0x5C: return ClampTo<int32_t>(v->value); case 0x5C: return ClampTo<int32_t>(v->value);
case 0x5D: return GB(ClampTo<int32_t>(v->value), 8, 24); case 0x5D: return GB(ClampTo<int32_t>(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 0x75: return GB(t->gcache.cached_power, 8, 24);
case 0x76: return GB(t->gcache.cached_power, 16, 16); case 0x76: return GB(t->gcache.cached_power, 16, 16);
case 0x77: return GB(t->gcache.cached_power, 24, 8); case 0x77: return GB(t->gcache.cached_power, 24, 8);
case 0x7C: return t->First()->index; case 0x7C: return t->First()->index.base();
case 0x7D: return GB(t->First()->index, 8, 8); case 0x7D: return GB(t->First()->index.base(), 8, 8);
case 0x7F: return 0; // Used for vehicle reversing hack in TTDP case 0x7F: return 0; // Used for vehicle reversing hack in TTDP
} }
break; 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 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 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 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 case 0xF2: return 0; // Cargo subtype
} }

View File

@ -920,7 +920,7 @@ uint32_t SerialiseNewsReference(const NewsReference &reference)
struct visitor { struct visitor {
uint32_t operator()(const std::monostate &) { return 0; } uint32_t operator()(const std::monostate &) { return 0; }
uint32_t operator()(const TileIndex &t) { return t.base(); } 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 StationID s) { return s; }
uint32_t operator()(const IndustryID i) { return i.base(); } uint32_t operator()(const IndustryID i) { return i.base(); }
uint32_t operator()(const TownID t) { return t.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) { for (auto &ni : _news) {
ChangeObject(ni.ref1, from_index, to_index); ChangeObject(ni.ref1, from_index, to_index);
ChangeObject(ni.ref2, from_index, to_index); ChangeObject(ni.ref2, from_index, to_index);
if (ni.flags.Test(NewsFlag::VehicleParam0) && std::get<uint64_t>(ni.params[0]) == from_index) ni.params[0] = to_index; if (ni.flags.Test(NewsFlag::VehicleParam0) && std::get<uint64_t>(ni.params[0]) == from_index) ni.params[0] = to_index.base();
} }
} }

View File

@ -176,7 +176,7 @@ void FixOldVehicles(LoadgameState &ls)
/* Vehicle-subtype is different in TTD(Patch) */ /* Vehicle-subtype is different in TTD(Patch) */
if (v->type == VEH_EFFECT) v->subtype = v->subtype >> 1; 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 */ /* We haven't used this bit for stations for ages */
if (v->type == VEH_ROAD) { if (v->type == VEH_ROAD) {
@ -1048,7 +1048,7 @@ static bool LoadOldCompany(LoadgameState &ls, int num)
static uint32_t _old_order_ptr; static uint32_t _old_order_ptr;
static uint16_t _old_next_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[] = { static const OldChunks vehicle_train_chunk[] = {
OCL_SVAR( OC_UINT8, Train, track ), OCL_SVAR( OC_UINT8, Train, track ),
@ -1250,7 +1250,7 @@ bool LoadOldVehicle(LoadgameState &ls, int num)
ReadTTDPatchFlags(ls); ReadTTDPatchFlags(ls);
for (uint i = 0; i < ls.vehicle_multiplier; i++) { for (uint i = 0; i < ls.vehicle_multiplier; i++) {
_current_vehicle_id = static_cast<VehicleID>(num * ls.vehicle_multiplier + i); _current_vehicle_id = num * ls.vehicle_multiplier + i;
Vehicle *v; Vehicle *v;

View File

@ -633,7 +633,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
/* static */ bool ScriptOrder::SetOrderFlags(VehicleID vehicle_id, OrderPosition order_position, ScriptOrder::ScriptOrderFlags order_flags) /* 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(1, order_position);
ScriptObject::SetCallbackVariable(2, order_flags); ScriptObject::SetCallbackVariable(2, order_flags);
/* In case another client(s) change orders at the same time we could /* In case another client(s) change orders at the same time we could

View File

@ -84,7 +84,7 @@
if (!ScriptObject::Command<CMD_BUILD_VEHICLE>::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, engine_id, true, cargo, INVALID_CLIENT_ID)) return VEHICLE_INVALID; if (!ScriptObject::Command<CMD_BUILD_VEHICLE>::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, engine_id, true, cargo, INVALID_CLIENT_ID)) return VEHICLE_INVALID;
/* In case of test-mode, we return VehicleID 0 */ /* In case of test-mode, we return VehicleID 0 */
return ::VEHICLE_BEGIN; return VehicleID::Begin();
} }
/* static */ VehicleID ScriptVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) /* static */ VehicleID ScriptVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
@ -115,7 +115,7 @@
if (!ScriptObject::Command<CMD_CLONE_VEHICLE>::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, vehicle_id, share_orders)) return VEHICLE_INVALID; if (!ScriptObject::Command<CMD_CLONE_VEHICLE>::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, vehicle_id, share_orders)) return VEHICLE_INVALID;
/* In case of test-mode, we return VehicleID 0 */ /* 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) /* static */ bool ScriptVehicle::_MoveWagonInternal(VehicleID source_vehicle_id, SQInteger source_wagon, bool move_attached_wagons, SQInteger dest_vehicle_id, SQInteger dest_wagon)

View File

@ -95,7 +95,7 @@ public:
VS_INVALID = 0xFF, ///< An invalid vehicle state. 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. * Checks whether the given vehicle is valid and owned by you.

View File

@ -44,7 +44,7 @@ ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
FindVehiclesWithOrder( FindVehiclesWithOrder(
[is_deity, owner](const Vehicle *v) { return is_deity || v->owner == owner; }, [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; }, [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( FindVehiclesWithOrder(
[is_deity, owner, type](const Vehicle *v) { return (is_deity || v->owner == owner) && v->type == type; }, [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; }, [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; if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return;
for (const Vehicle *v = Vehicle::Get(vehicle_id)->FirstShared(); v != nullptr; v = v->NextShared()) { for (const Vehicle *v = Vehicle::Get(vehicle_id)->FirstShared(); v != nullptr; v = v->NextShared()) {
this->AddItem(v->index); this->AddItem(v->index.base());
} }
} }

View File

@ -655,7 +655,7 @@ struct TimetableWindow : Window {
this->change_timetable_all = _ctrl_pressed; this->change_timetable_all = _ctrl_pressed;
ShowQueryString({}, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED); ShowQueryString({}, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
} else { } else {
ShowSetDateWindow(this, v->index, TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void*>(static_cast<uintptr_t>(_ctrl_pressed))); ShowSetDateWindow(this, v->index.base(), TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void*>(static_cast<uintptr_t>(_ctrl_pressed)));
} }
break; break;

View File

@ -994,7 +994,7 @@ void CallVehicleTicks()
PerformanceAccumulator::Reset(PFE_GL_AIRCRAFT); PerformanceAccumulator::Reset(PFE_GL_AIRCRAFT);
for (Vehicle *v : Vehicle::Iterate()) { 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 */ /* Vehicle could be deleted in this tick */
if (!v->Tick()) { if (!v->Tick()) {
@ -3007,7 +3007,7 @@ void Vehicle::RemoveFromShared()
} else if (were_first) { } else if (were_first) {
/* If we were the first one, update to the new first one. /* If we were the first one, update to the new first one.
* Note: FirstShared() is already the new first */ * 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; this->next_shared = nullptr;

View File

@ -196,7 +196,7 @@ struct MutableSpriteCache {
}; };
/** A vehicle pool for a little over 1 million vehicles. */ /** A vehicle pool for a little over 1 million vehicles. */
typedef Pool<Vehicle, VehicleID, 512, VEHICLE_END> VehiclePool; typedef Pool<Vehicle, VehicleID, 512, VehicleID::End().base()> VehiclePool;
extern VehiclePool _vehicle_pool; extern VehiclePool _vehicle_pool;
/* Some declarations of functions, so we can make them friendly */ /* Some declarations of functions, so we can make them friendly */

View File

@ -1578,7 +1578,7 @@ static inline void ChangeVehicleWindow(WindowClass window_class, VehicleID from_
if (w->viewport != nullptr) w->viewport->follow_vehicle = to_index; if (w->viewport != nullptr) w->viewport->follow_vehicle = to_index;
/* Update vehicle drag data */ /* 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; _thd.window_number = to_index;
} }
@ -2335,7 +2335,7 @@ void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type)
void ShowVehicleListWindow(const Vehicle *v) 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) void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, StationID station)

View File

@ -11,13 +11,11 @@
#define VEHICLE_TYPE_H #define VEHICLE_TYPE_H
#include "core/enum_type.hpp" #include "core/enum_type.hpp"
#include "core/pool_type.hpp"
/** The type all our vehicle IDs have. */ /** The type all our vehicle IDs have. */
enum VehicleID : uint32_t { using VehicleID = PoolID<uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF>;
VEHICLE_BEGIN = 0, static constexpr VehicleID INVALID_VEHICLE = VehicleID::Invalid(); ///< Constant representing a non-existing vehicle.
VEHICLE_END = 0xFF000,
INVALID_VEHICLE = 0xFFFFF ///< Constant representing a non-existing vehicle.
};
static const int GROUND_ACCELERATION = 9800; ///< Acceleration due to gravity, 9.8 m/s^2 static const int GROUND_ACCELERATION = 9800; ///< Acceleration due to gravity, 9.8 m/s^2