From 528c1ad0e6aa844c5f1599dd72047fe36b6b8ce9 Mon Sep 17 00:00:00 2001 From: Andrettin <6322423+Andrettin@users.noreply.github.com> Date: Tue, 12 Aug 2025 23:26:01 +0200 Subject: [PATCH] Use the size of the AllItemsList vector instead of IDI_LAST for getting how many items are there --- Source/items.cpp | 6 +++--- Source/lua/modules/dev/items.cpp | 4 ++-- Source/lua/modules/items.cpp | 1 - Source/pack.cpp | 2 +- test/items_test.cpp | 4 ++-- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Source/items.cpp b/Source/items.cpp index 3ca58c75a..fff24343d 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -1351,8 +1351,8 @@ _item_indexes GetItemIndexForDroppableItem(bool considerDropRate, tl::function_r ril.clear(); unsigned cumulativeWeight = 0; - for (std::underlying_type_t<_item_indexes> i = IDI_GOLD; i <= IDI_LAST; i++) { - if (!IsItemAvailable(i)) + for (size_t i = 0; i < AllItemsList.size(); i++) { + if (!IsItemAvailable(static_cast(i))) continue; const ItemData &item = AllItemsList[i]; if (item.dropRate == 0) @@ -2369,7 +2369,7 @@ std::string GetTranslatedItemNameMagical(const Item &item, bool hellfireItem, bo bool IsItemAvailable(int i) { - if (i < 0 || i > IDI_LAST) + if (i < 0 || i >= static_cast(AllItemsList.size())) return false; if (gbIsSpawn) { diff --git a/Source/lua/modules/dev/items.cpp b/Source/lua/modules/dev/items.cpp index 6a8dd46bc..c69cb11f8 100644 --- a/Source/lua/modules/dev/items.cpp +++ b/Source/lua/modules/dev/items.cpp @@ -135,8 +135,8 @@ std::string DebugSpawnUniqueItem(std::string itemName) if (!foundUnique) return "No unique item found!"; _item_indexes uniqueBaseIndex = IDI_GOLD; - for (std::underlying_type_t<_item_indexes> j = IDI_GOLD; j <= IDI_LAST; j++) { - if (!IsItemAvailable(j)) + for (size_t j = 0; j < AllItemsList.size(); j++) { + if (!IsItemAvailable(static_cast(j))) continue; if (AllItemsList[j].iItemId == uniqueItem.UIItemId) { uniqueBaseIndex = static_cast<_item_indexes>(j); diff --git a/Source/lua/modules/items.cpp b/Source/lua/modules/items.cpp index 999e3b6c8..8ca9c6497 100644 --- a/Source/lua/modules/items.cpp +++ b/Source/lua/modules/items.cpp @@ -346,7 +346,6 @@ void RegisterItemIndexEnum(sol::state_view &lua) { "RuneOfStone", IDI_RUNEOFSTONE }, { "SorcererDiablo", IDI_SORCERER_DIABLO }, { "ArenaPotion", IDI_ARENAPOT }, - { "Last", IDI_LAST }, { "None", IDI_NONE }, }); } diff --git a/Source/pack.cpp b/Source/pack.cpp index 20a348160..5aec1cd70 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -436,7 +436,7 @@ bool UnPackNetItem(const Player &player, const ItemNetPack &packedItem, Item &it { item = {}; const _item_indexes idx = static_cast<_item_indexes>(SDL_SwapLE16(packedItem.def.wIndx)); - if (idx < 0 || idx > IDI_LAST) + if (idx < 0 || idx >= static_cast<_item_indexes>(AllItemsList.size())) return true; if (idx == IDI_EAR) { RecreateEar(item, SDL_SwapLE16(packedItem.ear.wCI), SDL_SwapLE32(packedItem.ear.dwSeed), packedItem.ear.bCursval, packedItem.ear.heroname); diff --git a/test/items_test.cpp b/test/items_test.cpp index 0a1d235b1..c2293c857 100644 --- a/test/items_test.cpp +++ b/test/items_test.cpp @@ -62,8 +62,8 @@ void GenerateAllUniques(bool hellfire, const size_t expectedUniques) continue; _item_indexes uniqueBaseIndex = IDI_GOLD; - for (std::underlying_type_t<_item_indexes> j = IDI_GOLD; j <= IDI_LAST; j++) { - if (!IsItemAvailable(j)) + for (size_t j = 0; j < AllItemsList.size(); j++) { + if (!IsItemAvailable(static_cast(j))) continue; if (AllItemsList[j].iItemId != uniqueItem.UIItemId) continue;