From 4d7c08d5b4830fbd25c299c5d1396286f42efcc7 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Wed, 20 Apr 2022 17:42:58 +1000 Subject: [PATCH] Deduplicate code for setting stat requirements/flag --- Source/inv.cpp | 23 ++--------------- Source/items.cpp | 67 +++++++++++++----------------------------------- 2 files changed, 20 insertions(+), 70 deletions(-) diff --git a/Source/inv.cpp b/Source/inv.cpp index 4d9ecde5c..deb379581 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -853,23 +853,6 @@ void CheckInvCut(int pnum, Point cursorPosition, bool automaticMove, bool dropIt } } -void UpdateBookLevel(const Player &player, Item &book) -{ - if (book._iMiscId != IMISC_BOOK) - return; - - book._iMinMag = spelldata[book._iSpell].sMinInt; - int8_t spellLevel = player._pSplLvl[book._iSpell]; - while (spellLevel != 0) { - book._iMinMag += 20 * book._iMinMag / 100; - spellLevel--; - if (book._iMinMag + 20 * book._iMinMag / 100 > 255) { - book._iMinMag = -1; - spellLevel = 0; - } - } -} - void TryCombineNaKrulNotes(Player &player, Item ¬eItem) { int idx = noteItem.IDidx; @@ -1606,8 +1589,7 @@ void InvGetItem(Player &player, int ii) item._iCreateInfo &= ~CF_PREGEN; CheckQuestItem(player, item); - UpdateBookLevel(player, item); - item._iStatFlag = player.CanUseItem(item); + item.updateRequiredStatsCacheForPlayer(player); if (item._itype != ItemType::Gold || !GoldAutoPlace(player, item)) { // The item needs to go into the players hand @@ -1641,8 +1623,7 @@ void AutoGetItem(int pnum, Item *itemPointer, int ii) item._iCreateInfo &= ~CF_PREGEN; CheckQuestItem(player, item); - UpdateBookLevel(player, item); - item._iStatFlag = player.CanUseItem(item); + item.updateRequiredStatsCacheForPlayer(player); bool done; bool autoEquipped = false; diff --git a/Source/items.cpp b/Source/items.cpp index 3181a5e6c..338b4494f 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -514,49 +514,6 @@ void CalcSelfItems(Player &player) } while (changeflag); } -void CalcPlrItemMin(Player &player) -{ - for (Item &item : InventoryAndBeltPlayerItemsRange { player }) { - item._iStatFlag = player.CanUseItem(item); - } -} - -uint8_t CalculateMagicRequirementForBook(spell_id spell, int8_t spellLevel) -{ - int magicRequirement = spelldata[spell].sMinInt; - - while (spellLevel != 0) { - magicRequirement += 20 * magicRequirement / 100; - spellLevel--; - if (magicRequirement + 20 * magicRequirement / 100 > 255) { - magicRequirement = 255; - break; - } - } - - return magicRequirement; -} - -void CalcPlrBookVals(Player &player) -{ - auto processItem = [](Player &player, Item &item) { - if (item._itype == ItemType::Misc && item._iMiscId == IMISC_BOOK) { - spell_id spell = item._iSpell; - int8_t spellLevel = player._pSplLvl[spell]; - item._iMinMag = CalculateMagicRequirementForBook(spell, spellLevel); - item._iStatFlag = player.CanUseItem(item); - } - }; - for (Item &item : InventoryPlayerItemsRange { player }) { - processItem(player, item); - } - if (&player == MyPlayer) { - for (Item &item : Stash.stashList) { - processItem(player, item); - } - } -} - bool GetItemSpace(Point position, int8_t inum) { int xx = 0; @@ -2744,12 +2701,18 @@ void CalcPlrItemVals(Player &player, bool loadgfx) void CalcPlrInv(Player &player, bool loadgfx) { - CalcPlrItemMin(player); + // Determine the players current stats, this updates the statFlag on all equipped items that became unusable after + // a change in equipment. CalcSelfItems(player); + + // Determine the current item bonuses gained from usable equipped items CalcPlrItemVals(player, loadgfx); - CalcPlrItemMin(player); - if (&player == &Players[MyPlayerId]) { - CalcPlrBookVals(player); + + if (&player == MyPlayer) { + // Now that stat gains from equipped items have been calculated, mark unusable scrolls etc + for (Item &item : InventoryAndBeltPlayerItemsRange { player }) { + item.updateRequiredStatsCacheForPlayer(player); + } player.CalcScrolls(); CalcPlrStaff(player); if (IsStashOpen) { @@ -3982,8 +3945,14 @@ void UseItem(int pnum, item_misc_id mid, spell_id spl) player._pManaBase += spelldata[spl].sManaCost << 6; player._pManaBase = std::min(player._pManaBase, player._pMaxManaBase); } - if (pnum == MyPlayerId) - CalcPlrBookVals(player); + if (&player == MyPlayer) { + for (Item &item : InventoryPlayerItemsRange { player }) { + item.updateRequiredStatsCacheForPlayer(player); + } + if (IsStashOpen) { + Stash.RefreshItemStatFlags(); + } + } drawmanaflag = true; break; case IMISC_MAPOFDOOM: