From ca48efc185e04f9a77678bf3b69846e92a0a02fb Mon Sep 17 00:00:00 2001 From: ephphatha Date: Wed, 20 Apr 2022 17:19:57 +1000 Subject: [PATCH] Update spell book requirements when refreshing stash items --- Source/items.cpp | 17 +++++++++++++++++ Source/items.h | 11 +++++++++-- Source/qol/stash.cpp | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Source/items.cpp b/Source/items.cpp index 4b7516cab..3181a5e6c 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -4626,6 +4626,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 ebd326198..0b3e06dd6 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; @@ -414,6 +417,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 { @@ -429,8 +438,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 b9cc7ca74..2af7add2b 100644 --- a/Source/qol/stash.cpp +++ b/Source/qol/stash.cpp @@ -561,7 +561,7 @@ void StashStruct::PreviousPage(unsigned offset) void StashStruct::RefreshItemStatFlags() { for (auto &item : Stash.stashList) { - item._iStatFlag = MyPlayer->CanUseItem(item); + item.updateRequiredStatsCacheForPlayer(*MyPlayer); } }