diff --git a/Source/items.cpp b/Source/items.cpp index 8f38ca3bd..e94a9c1a6 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -4623,6 +4623,23 @@ void Item::SetNewAnimation(bool showAnimation) } } +void Item::updateRequiredStatsCacheForPlayer(const Player &player) +{ + if (_itype == ItemType::Misc && _iMiscId == IMISC_BOOK) { + _iMinMag = spelldata[_iSpell].sMinInt; + int8_t spellLevel = player._pSplLvl[_iSpell]; + while (spellLevel != 0) { + _iMinMag += 20 * _iMinMag / 100; + spellLevel--; + if (_iMinMag + 20 * _iMinMag / 100 > 255) { + _iMinMag = 255; + spellLevel = 0; + } + } + } + _iStatFlag = player.CanUseItem(*this); +} + void initItemGetRecords() { memset(itemrecord, 0, sizeof(itemrecord)); diff --git a/Source/items.h b/Source/items.h index 08fa87a82..01c1e6a71 100644 --- a/Source/items.h +++ b/Source/items.h @@ -171,6 +171,9 @@ enum icreateinfo_flag2 { // All item animation frames have this width. constexpr int ItemAnimWidth = 96; +// Defined in player.h, forward declared here to allow for functions which operate in the context of a player. +struct Player; + struct Item { /** Randomly generated identifier */ int32_t _iSeed = 0; @@ -396,6 +399,12 @@ struct Item { * @param showAnimation Definies if the Animation (Flipping) is shown or if only the final Frame (item on the ground) is shown */ void SetNewAnimation(bool showAnimation); + + /** + * @brief If this item is a spell book, calculates the magic requirement to learn a new level, then for all items sets _iStatFlag + * @param player Player to compare stats against requirements + */ + void updateRequiredStatsCacheForPlayer(const Player &player); }; struct ItemGetRecordStruct { @@ -411,8 +420,6 @@ struct CornerStoneStruct { Item item; }; -struct Player; - /** Contains the items on ground in the current game. */ extern Item Items[MAXITEMS + 1]; extern uint8_t ActiveItems[MAXITEMS]; diff --git a/Source/qol/stash.cpp b/Source/qol/stash.cpp index eb9d95ddf..dfa1e55cc 100644 --- a/Source/qol/stash.cpp +++ b/Source/qol/stash.cpp @@ -563,7 +563,7 @@ void StashStruct::PreviousPage(unsigned offset) void StashStruct::RefreshItemStatFlags() { for (auto &item : Stash.stashList) { - item._iStatFlag = MyPlayer->CanUseItem(item); + item.updateRequiredStatsCacheForPlayer(*MyPlayer); } }