From 4ff83431d223e4bbad1e889156e714c9faacb91a Mon Sep 17 00:00:00 2001 From: Andrew James Date: Sun, 20 Mar 2022 00:32:54 +1100 Subject: [PATCH] Remove the use of HoldItem in AutoGetItem (#4178) --- Source/control.cpp | 8 ++-- Source/control.h | 2 +- Source/inv.cpp | 104 +++++++++++++++++++------------------------ Source/inv.h | 1 - Source/qol/stash.cpp | 4 +- 5 files changed, 52 insertions(+), 67 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index eaf93017e..917befd96 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -372,7 +372,7 @@ void RemoveGold(Player &player, int goldIndex) SetGoldSeed(player, player.HoldItem); player.HoldItem._ivalue = dropGoldValue; player.HoldItem._iStatFlag = true; - ControlSetGoldCurs(player); + ControlSetGoldCurs(player.HoldItem); player._pGold = CalculateGold(player); dropGoldValue = 0; } @@ -397,10 +397,10 @@ bool IsLevelUpButtonVisible() } // namespace -void ControlSetGoldCurs(Player &player) +void ControlSetGoldCurs(Item &goldItem) { - SetPlrHandGoldCurs(player.HoldItem); - NewCursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); + SetPlrHandGoldCurs(goldItem); + NewCursor(goldItem._iCurs + CURSOR_FIRSTITEM); } void CalculatePanelAreas() diff --git a/Source/control.h b/Source/control.h index e7821651e..b2e28958f 100644 --- a/Source/control.h +++ b/Source/control.h @@ -68,7 +68,7 @@ inline bool CanPanelsCoverView() { return GetScreenWidth() <= PANEL_WIDTH && GetScreenHeight() <= SPANEL_HEIGHT + PANEL_HEIGHT; } -void ControlSetGoldCurs(Player &player); +void ControlSetGoldCurs(Item &goldItem); void DrawSpellList(const Surface &out); void SetSpell(); diff --git a/Source/inv.cpp b/Source/inv.cpp index e42bd255f..5712d0cf7 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -867,7 +867,7 @@ void CheckInvCut(int pnum, Point cursorPosition, bool automaticMove, bool dropIt } CalcPlrInv(player, true); - CheckItemStats(player); + holdItem._iStatFlag = player.CanUseItem(holdItem); if (pnum == MyPlayerId) { if (automaticallyEquipped) { @@ -910,26 +910,26 @@ void CheckInvCut(int pnum, Point cursorPosition, bool automaticMove, bool dropIt } } -void CheckBookLevel(Player &player) +void UpdateBookLevel(Player &player, Item &book) { - if (player.HoldItem._iMiscId != IMISC_BOOK) + if (book._iMiscId != IMISC_BOOK) return; - player.HoldItem._iMinMag = spelldata[player.HoldItem._iSpell].sMinInt; - int8_t spellLevel = player._pSplLvl[player.HoldItem._iSpell]; + book._iMinMag = spelldata[book._iSpell].sMinInt; + int8_t spellLevel = player._pSplLvl[book._iSpell]; while (spellLevel != 0) { - player.HoldItem._iMinMag += 20 * player.HoldItem._iMinMag / 100; + book._iMinMag += 20 * book._iMinMag / 100; spellLevel--; - if (player.HoldItem._iMinMag + 20 * player.HoldItem._iMinMag / 100 > 255) { - player.HoldItem._iMinMag = -1; + if (book._iMinMag + 20 * book._iMinMag / 100 > 255) { + book._iMinMag = -1; spellLevel = 0; } } } -void CheckNaKrulNotes(Player &player) +void TryCombineNaKrulNotes(Player &player, Item ¬eItem) { - int idx = player.HoldItem.IDidx; + int idx = noteItem.IDidx; _item_indexes notes[] = { IDI_NOTE1, IDI_NOTE2, IDI_NOTE3 }; if (IsNoneOf(idx, IDI_NOTE1, IDI_NOTE2, IDI_NOTE3)) { @@ -950,24 +950,24 @@ void CheckNaKrulNotes(Player &player) } } - player.HoldItem = {}; - GetItemAttrs(player.HoldItem, IDI_FULLNOTE, 16); - SetupItem(player.HoldItem); + noteItem = {}; + GetItemAttrs(noteItem, IDI_FULLNOTE, 16); + SetupItem(noteItem); } -void CheckQuestItem(Player &player) +void CheckQuestItem(Player &player, Item &questItem) { auto &myPlayer = Players[MyPlayerId]; - if (player.HoldItem.IDidx == IDI_OPTAMULET && Quests[Q_BLIND]._qactive == QUEST_ACTIVE) + if (questItem.IDidx == IDI_OPTAMULET && Quests[Q_BLIND]._qactive == QUEST_ACTIVE) Quests[Q_BLIND]._qactive = QUEST_DONE; - if (player.HoldItem.IDidx == IDI_MUSHROOM && Quests[Q_MUSHROOM]._qactive == QUEST_ACTIVE && Quests[Q_MUSHROOM]._qvar1 == QS_MUSHSPAWNED) { + if (questItem.IDidx == IDI_MUSHROOM && Quests[Q_MUSHROOM]._qactive == QUEST_ACTIVE && Quests[Q_MUSHROOM]._qvar1 == QS_MUSHSPAWNED) { player.Say(HeroSpeech::NowThatsOneBigMushroom, 10); // BUGFIX: Voice for this quest might be wrong in MP Quests[Q_MUSHROOM]._qvar1 = QS_MUSHPICKED; } - if (player.HoldItem.IDidx == IDI_ANVIL && Quests[Q_ANVIL]._qactive != QUEST_NOTAVAIL) { + if (questItem.IDidx == IDI_ANVIL && Quests[Q_ANVIL]._qactive != QUEST_NOTAVAIL) { if (Quests[Q_ANVIL]._qactive == QUEST_INIT) { Quests[Q_ANVIL]._qactive = QUEST_ACTIVE; } @@ -976,11 +976,11 @@ void CheckQuestItem(Player &player) } } - if (player.HoldItem.IDidx == IDI_GLDNELIX && Quests[Q_VEIL]._qactive != QUEST_NOTAVAIL) { + if (questItem.IDidx == IDI_GLDNELIX && Quests[Q_VEIL]._qactive != QUEST_NOTAVAIL) { myPlayer.Say(HeroSpeech::INeedToGetThisToLachdanan, 30); } - if (player.HoldItem.IDidx == IDI_ROCK && Quests[Q_ROCK]._qactive != QUEST_NOTAVAIL) { + if (questItem.IDidx == IDI_ROCK && Quests[Q_ROCK]._qactive != QUEST_NOTAVAIL) { if (Quests[Q_ROCK]._qactive == QUEST_INIT) { Quests[Q_ROCK]._qactive = QUEST_ACTIVE; } @@ -989,12 +989,12 @@ void CheckQuestItem(Player &player) } } - if (player.HoldItem.IDidx == IDI_ARMOFVAL && Quests[Q_BLOOD]._qactive == QUEST_ACTIVE) { + if (questItem.IDidx == IDI_ARMOFVAL && Quests[Q_BLOOD]._qactive == QUEST_ACTIVE) { Quests[Q_BLOOD]._qactive = QUEST_DONE; myPlayer.Say(HeroSpeech::MayTheSpiritOfArkaineProtectMe, 20); } - if (player.HoldItem.IDidx == IDI_MAPOFDOOM) { + if (questItem.IDidx == IDI_MAPOFDOOM) { Quests[Q_GRAVE]._qlog = false; Quests[Q_GRAVE]._qactive = QUEST_ACTIVE; if (Quests[Q_GRAVE]._qvar1 != 1) { @@ -1003,7 +1003,7 @@ void CheckQuestItem(Player &player) } } - CheckNaKrulNotes(player); + TryCombineNaKrulNotes(player, questItem); } void OpenHive() @@ -1151,7 +1151,6 @@ bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex, Item &goldStack goldStack._ivalue = 0; player._pGold = CalculateGold(player); - NewCursor(CURSOR_HAND); return true; } @@ -1603,13 +1602,6 @@ void CheckInvScrn(bool isShiftHeld, bool isCtrlHeld) } } -void CheckItemStats(Player &player) -{ - Item &item = player.HoldItem; - - item._iStatFlag = player.CanUseItem(item); -} - void InvGetItem(int pnum, int ii) { auto &item = Items[ii]; @@ -1628,9 +1620,9 @@ void InvGetItem(int pnum, int ii) item._iCreateInfo &= ~CF_PREGEN; player.HoldItem = item; - CheckQuestItem(player); - CheckBookLevel(player); - CheckItemStats(player); + CheckQuestItem(player, player.HoldItem); + UpdateBookLevel(player, player.HoldItem); + player.HoldItem._iStatFlag = player.CanUseItem(player.HoldItem); bool cursorUpdated = false; if (player.HoldItem._itype == ItemType::Gold && GoldAutoPlace(player, player.HoldItem)) cursorUpdated = true; @@ -1640,48 +1632,44 @@ void InvGetItem(int pnum, int ii) NewCursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); } -void AutoGetItem(int pnum, Item *item, int ii) +void AutoGetItem(int pnum, Item *itemPointer, int ii) { - bool done; - bool autoEquipped = false; - - if (pcurs != CURSOR_HAND) { - return; - } + Item &item = *itemPointer; + auto &player = Players[pnum]; if (dropGoldFlag) { CloseGoldDrop(); dropGoldValue = 0; } - if (dItem[item->position.x][item->position.y] == 0) + if (dItem[item.position.x][item.position.y] == 0) return; - auto &player = Players[pnum]; + item._iCreateInfo &= ~CF_PREGEN; + CheckQuestItem(player, item); + UpdateBookLevel(player, item); + item._iStatFlag = player.CanUseItem(item); + SetICursor(item._iCurs + CURSOR_FIRSTITEM); - item->_iCreateInfo &= ~CF_PREGEN; - player.HoldItem = *item; /// BUGFIX: overwrites cursor item, allowing for belt dupe bug - CheckQuestItem(player); - CheckBookLevel(player); - CheckItemStats(player); - SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); - if (player.HoldItem._itype == ItemType::Gold) { - done = GoldAutoPlace(player, player.HoldItem); + bool done; + bool autoEquipped = false; + + if (item._itype == ItemType::Gold) { + done = GoldAutoPlace(player, item); if (!done) { - item->_ivalue = player.HoldItem._ivalue; - SetPlrHandGoldCurs(*item); + SetPlrHandGoldCurs(item); } } else { - done = AutoEquipEnabled(player, player.HoldItem) && AutoEquip(pnum, player.HoldItem); + done = AutoEquipEnabled(player, item) && AutoEquip(pnum, item); if (done) { autoEquipped = true; } if (!done) { - done = AutoPlaceItemInBelt(player, player.HoldItem, true); + done = AutoPlaceItemInBelt(player, item, true); } if (!done) { - done = AutoPlaceItemInInventory(player, player.HoldItem, true); + done = AutoPlaceItemInInventory(player, item, true); } } @@ -1697,10 +1685,8 @@ void AutoGetItem(int pnum, Item *item, int ii) if (pnum == MyPlayerId) { player.Say(HeroSpeech::ICantCarryAnymore); } - player.HoldItem = *item; - RespawnItem(*item, true); - NetSendCmdPItem(true, CMD_RESPAWNITEM, item->position, player.HoldItem); - player.HoldItem._itype = ItemType::None; + RespawnItem(item, true); + NetSendCmdPItem(true, CMD_RESPAWNITEM, item.position, item); } int FindGetItem(int32_t iseed, _item_indexes idx, uint16_t createInfo) diff --git a/Source/inv.h b/Source/inv.h index f4078b93b..d2e78b339 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -168,7 +168,6 @@ void CheckInvItem(bool isShiftHeld = false, bool isCtrlHeld = false); * Check for interactions with belt */ void CheckInvScrn(bool isShiftHeld, bool isCtrlHeld); -void CheckItemStats(Player &player); void InvGetItem(int pnum, int ii); void AutoGetItem(int pnum, Item *item, int ii); diff --git a/Source/qol/stash.cpp b/Source/qol/stash.cpp index 0b6927f72..ca9f2e409 100644 --- a/Source/qol/stash.cpp +++ b/Source/qol/stash.cpp @@ -202,7 +202,7 @@ void CheckStashCut(Point cursorPosition, bool automaticMove, bool dropItem) if (!holdItem.isEmpty()) { CalcPlrInv(player, true); - CheckItemStats(player); + holdItem._iStatFlag = player.CanUseItem(holdItem); if (automaticallyEquipped) { PlaySFX(ItemInvSnds[ItemCAnimTbl[holdItem._iCurs]]); } else if (!automaticMove || automaticallyMoved) { @@ -266,7 +266,7 @@ void WithdrawGold(Player &player, int amount) SetGoldSeed(player, player.HoldItem); player.HoldItem._ivalue = amount; player.HoldItem._iStatFlag = true; - ControlSetGoldCurs(player); + ControlSetGoldCurs(player.HoldItem); Stash.gold -= amount; Stash.dirty = true; }