From 64a88343e84f743818589ff81872433599316236 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 24 Sep 2021 18:41:48 +0100 Subject: [PATCH] Refactor: Use item iterators in 7 more places --- Source/inv.cpp | 26 +++++++++-------- Source/inv_iterators.hpp | 5 ++++ Source/items.cpp | 16 +++++------ Source/objects.cpp | 62 +++++++--------------------------------- Source/player.cpp | 10 +++---- 5 files changed, 42 insertions(+), 77 deletions(-) diff --git a/Source/inv.cpp b/Source/inv.cpp index 618525ae5..94e3a2b8c 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -2006,21 +2006,23 @@ int8_t CheckInvHLight() void RemoveScroll(Player &player) { - for (int i = 0; i < player._pNumInv; i++) { - if (!player.InvList[i].isEmpty() - && IsAnyOf(player.InvList[i]._iMiscId, IMISC_SCROLL, IMISC_SCROLLT) - && player.InvList[i]._iSpell == player._pSpell) { - player.RemoveInvItem(i); - player.CalcScrolls(); + const spell_id spellId = player._pSpell; + const auto isCurrentSpell = [spellId](const Item &item) { + return item.IsScrollOf(spellId); + }; + { + const InventoryPlayerItemsRange items { player }; + const auto scrollIt = std::find_if(items.begin(), items.end(), isCurrentSpell); + if (scrollIt != items.end()) { + player.RemoveInvItem(static_cast(scrollIt.Index())); return; } } - for (int i = 0; i < MAXBELTITEMS; i++) { - if (!player.SpdList[i].isEmpty() - && IsAnyOf(player.SpdList[i]._iMiscId, IMISC_SCROLL, IMISC_SCROLLT) - && player.SpdList[i]._iSpell == player._pSpell) { - player.RemoveSpdBarItem(i); - player.CalcScrolls(); + { + const BeltPlayerItemsRange items { player }; + const auto scrollIt = std::find_if(items.begin(), items.end(), isCurrentSpell); + if (scrollIt != items.end()) { + player.RemoveSpdBarItem(static_cast(scrollIt.Index())); return; } } diff --git a/Source/inv_iterators.hpp b/Source/inv_iterators.hpp index 168cd2413..3277ec187 100644 --- a/Source/inv_iterators.hpp +++ b/Source/inv_iterators.hpp @@ -73,6 +73,11 @@ public: return index_ == count_; } + [[nodiscard]] std::size_t Index() const + { + return index_; + } + private: void AdvancePastEmpty() { diff --git a/Source/items.cpp b/Source/items.cpp index dc6ad5f14..0ada2a0fa 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -568,20 +568,20 @@ void CalcPlrBookVals(Player &player) } } - for (int i = 0; i < player._pNumInv; i++) { - if (player.InvList[i]._itype == ItemType::Misc && player.InvList[i]._iMiscId == IMISC_BOOK) { - player.InvList[i]._iMinMag = spelldata[player.InvList[i]._iSpell].sMinInt; - int8_t spellLevel = player._pSplLvl[player.InvList[i]._iSpell]; + for (Item &item : InventoryPlayerItemsRange { player }) { + if (item._itype == ItemType::Misc && item._iMiscId == IMISC_BOOK) { + item._iMinMag = spelldata[item._iSpell].sMinInt; + int8_t spellLevel = player._pSplLvl[item._iSpell]; while (spellLevel != 0) { - player.InvList[i]._iMinMag += 20 * player.InvList[i]._iMinMag / 100; + item._iMinMag += 20 * item._iMinMag / 100; spellLevel--; - if (player.InvList[i]._iMinMag + 20 * player.InvList[i]._iMinMag / 100 > 255) { - player.InvList[i]._iMinMag = 255; + if (item._iMinMag + 20 * item._iMinMag / 100 > 255) { + item._iMinMag = 255; spellLevel = 0; } } - player.InvList[i]._iStatFlag = ItemMinStats(player, player.InvList[i]); + item._iStatFlag = ItemMinStats(player, item); } } } diff --git a/Source/objects.cpp b/Source/objects.cpp index 80a5be037..96f199ccc 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -2725,14 +2725,14 @@ bool OperateShrineWeird(int pnum) if (!player.InvBody[INVLOC_HAND_RIGHT].isEmpty() && player.InvBody[INVLOC_HAND_RIGHT]._itype != ItemType::Shield) player.InvBody[INVLOC_HAND_RIGHT]._iMaxDam++; - for (int j = 0; j < player._pNumInv; j++) { - switch (player.InvList[j]._itype) { + for (Item &item : InventoryPlayerItemsRange { player }) { + switch (item._itype) { case ItemType::Sword: case ItemType::Axe: case ItemType::Bow: case ItemType::Mace: case ItemType::Staff: - player.InvList[j]._iMaxDam++; + item._iMaxDam++; break; default: break; @@ -2776,17 +2776,7 @@ bool OperateShrineStone(int pnum) if (pnum != MyPlayerId) return true; - auto &player = Players[pnum]; - - for (auto &item : player.InvBody) { - if (item._itype == ItemType::Staff) - item._iCharges = item._iMaxCharges; - } - for (int j = 0; j < player._pNumInv; j++) { - if (player.InvList[j]._itype == ItemType::Staff) - player.InvList[j]._iCharges = player.InvList[j]._iMaxCharges; - } - for (auto &item : player.SpdList) { + for (Item &item : PlayerItemsRange { Players[pnum] }) { if (item._itype == ItemType::Staff) item._iCharges = item._iMaxCharges; // belt items don't have charges? } @@ -2803,14 +2793,9 @@ bool OperateShrineReligious(int pnum) if (pnum != MyPlayerId) return true; - auto &player = Players[pnum]; - - for (auto &item : player.InvBody) - item._iDurability = item._iMaxDur; - for (int j = 0; j < player._pNumInv; j++) - player.InvList[j]._iDurability = player.InvList[j]._iMaxDur; - for (auto &item : player.SpdList) + for (Item &item : PlayerItemsRange { Players[pnum] }) { item._iDurability = item._iMaxDur; // belt items don't have durability? + } InitDiabloMsg(EMSG_SHRINE_RELIGIOUS); @@ -2957,25 +2942,7 @@ bool OperateShrineEldritch(int pnum) auto &player = Players[pnum]; - for (int j = 0; j < player._pNumInv; j++) { - if (player.InvList[j]._itype == ItemType::Misc) { - if (player.InvList[j]._iMiscId == IMISC_HEAL - || player.InvList[j]._iMiscId == IMISC_MANA) { - SetPlrHandItem(player.HoldItem, ItemMiscIdIdx(IMISC_REJUV)); - GetPlrHandSeed(&player.HoldItem); - player.HoldItem._iStatFlag = true; - player.InvList[j] = player.HoldItem; - } - if (player.InvList[j]._iMiscId == IMISC_FULLHEAL - || player.InvList[j]._iMiscId == IMISC_FULLMANA) { - SetPlrHandItem(player.HoldItem, ItemMiscIdIdx(IMISC_FULLREJUV)); - GetPlrHandSeed(&player.HoldItem); - player.HoldItem._iStatFlag = true; - player.InvList[j] = player.HoldItem; - } - } - } - for (auto &item : player.SpdList) { + for (Item &item : InventoryAndBeltPlayerItemsRange { player }) { if (item._itype == ItemType::Misc) { if (item._iMiscId == IMISC_HEAL || item._iMiscId == IMISC_MANA) { @@ -3264,19 +3231,10 @@ bool OperateShrineGlimmering(int pnum) if (pnum != MyPlayerId) return false; - auto &player = Players[pnum]; - - for (auto &item : player.InvBody) { - if (item._iMagical != ITEM_QUALITY_NORMAL && !item._iIdentified) - item._iIdentified = true; - } - for (int j = 0; j < player._pNumInv; j++) { - if (player.InvList[j]._iMagical != ITEM_QUALITY_NORMAL && !player.InvList[j]._iIdentified) - player.InvList[j]._iIdentified = true; - } - for (auto &item : player.SpdList) { - if (item._iMagical != ITEM_QUALITY_NORMAL && !item._iIdentified) + for (Item &item : PlayerItemsRange { Players[pnum] }) { + if (item._iMagical != ITEM_QUALITY_NORMAL && !item._iIdentified) { item._iIdentified = true; // belt items can't be magical? + } } InitDiabloMsg(EMSG_SHRINE_GLIMMERING); diff --git a/Source/player.cpp b/Source/player.cpp index 91154b979..02628b7e0 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -3067,11 +3067,11 @@ void StripTopGold(Player &player) { Item tmpItem = player.HoldItem; - for (int i = 0; i < player._pNumInv; i++) { - if (player.InvList[i]._itype == ItemType::Gold) { - if (player.InvList[i]._ivalue > MaxGold) { - int val = player.InvList[i]._ivalue - MaxGold; - player.InvList[i]._ivalue = MaxGold; + for (Item &item : InventoryPlayerItemsRange { player }) { + if (item._itype == ItemType::Gold) { + if (item._ivalue > MaxGold) { + int val = item._ivalue - MaxGold; + item._ivalue = MaxGold; SetPlrHandItem(player.HoldItem, 0); SetGoldSeed(player, player.HoldItem); player.HoldItem._ivalue = val;