From bf86114ec3b25cc96a0f443e4b9b4bf0e228a930 Mon Sep 17 00:00:00 2001 From: obligaron Date: Sun, 23 Apr 2023 08:37:53 +0200 Subject: [PATCH] Sync learned/changed spells --- Source/debug.cpp | 12 ++---------- Source/items.cpp | 10 +++++----- Source/msg.cpp | 18 +++++++++++------- Source/msg.h | 8 +++++--- Source/objects.cpp | 43 ++++++++++++++++++++++--------------------- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/Source/debug.cpp b/Source/debug.cpp index 9ee4b1f28..b8242c571 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -92,14 +92,6 @@ uint32_t glMid2Seed[NUMLEVELS]; uint32_t glMid3Seed[NUMLEVELS]; uint32_t glEndSeed[NUMLEVELS]; -void SetSpellLevelCheat(SpellID spl, uint8_t spllvl) -{ - Player &myPlayer = *MyPlayer; - - myPlayer._pMemSpells |= GetSpellBitmask(spl); - myPlayer._pSplLvl[static_cast(spl)] = spllvl; -} - void PrintDebugMonster(const Monster &monster) { EventPlrMsg(StrCat( @@ -530,9 +522,9 @@ std::string DebugCmdMinStats(const string_view parameter) std::string DebugCmdSetSpellsLevel(const string_view parameter) { uint8_t level = static_cast(std::max(0, atoi(parameter.data()))); - for (int i = static_cast(SpellID::Firebolt); i < MAX_SPELLS; i++) { + for (uint8_t i = static_cast(SpellID::Firebolt); i < MAX_SPELLS; i++) { if (GetSpellBookLevel(static_cast(i)) != -1) { - SetSpellLevelCheat(static_cast(i), level); + NetSendCmdParam2(true, CMD_CHANGE_SPELL_LEVEL, i, level); } } if (level == 0) diff --git a/Source/items.cpp b/Source/items.cpp index a63dae103..bd67a9a80 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -4052,10 +4052,10 @@ void UseItem(size_t pnum, item_misc_id mid, SpellID spellID, int spellFrom) NetSendCmdLocParam4(true, CMD_SPELLXY, cursPosition, static_cast(spellID), static_cast(SpellType::Scroll), spellLevel, static_cast(spellFrom)); } break; - case IMISC_BOOK: - player._pMemSpells |= GetSpellBitmask(spellID); - if (player._pSplLvl[static_cast(spellID)] < MaxSpellLevel) - player._pSplLvl[static_cast(spellID)]++; + case IMISC_BOOK: { + uint8_t newSpellLevel = player._pSplLvl[static_cast(spellID)] + 1; + if (newSpellLevel <= MaxSpellLevel) + NetSendCmdParam2(true, CMD_CHANGE_SPELL_LEVEL, static_cast(spellID), newSpellLevel); if (HasNoneOf(player._pIFlags, ItemSpecialEffect::NoMana)) { player._pMana += GetSpellData(spellID).sManaCost << 6; player._pMana = std::min(player._pMana, player._pMaxMana); @@ -4071,7 +4071,7 @@ void UseItem(size_t pnum, item_misc_id mid, SpellID spellID, int spellFrom) } } RedrawComponent(PanelDrawComponent::Mana); - break; + } break; case IMISC_MAPOFDOOM: doom_init(); break; diff --git a/Source/msg.cpp b/Source/msg.cpp index 52a55d1be..fc3202116 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -2304,17 +2304,21 @@ size_t OnCheatExperience(const TCmd *pCmd, size_t pnum) // NOLINT(misc-unused-pa return sizeof(*pCmd); } -size_t OnCheatSpellLevel(const TCmd *pCmd, size_t pnum) // NOLINT(misc-unused-parameters) +size_t OnChangeSpellLevel(const TCmd *pCmd, size_t pnum) // NOLINT(misc-unused-parameters) { -#ifdef _DEBUG + const auto &message = *reinterpret_cast(pCmd); + const SpellID spellID = static_cast(SDL_SwapLE16(message.wParam1)); + const uint8_t spellLevel = std::min(static_cast(SDL_SwapLE16(message.wParam2)), MaxSpellLevel); + if (gbBufferMsgs == 1) { SendPacket(pnum, pCmd, sizeof(*pCmd)); } else { Player &player = Players[pnum]; - player._pSplLvl[static_cast(player._pRSpell)]++; + player._pMemSpells |= GetSpellBitmask(spellID); + player._pSplLvl[static_cast(spellID)] = spellLevel; } -#endif - return sizeof(*pCmd); + + return sizeof(message); } size_t OnDebug(const TCmd *pCmd) @@ -3267,8 +3271,8 @@ size_t ParseCmd(size_t pnum, const TCmd *pCmd) return OnSyncQuest(pCmd, pnum); case CMD_CHEAT_EXPERIENCE: return OnCheatExperience(pCmd, pnum); - case CMD_CHEAT_SPELL_LEVEL: - return OnCheatSpellLevel(pCmd, pnum); + case CMD_CHANGE_SPELL_LEVEL: + return OnChangeSpellLevel(pCmd, pnum); case CMD_SETSHIELD: return OnSetShield(pCmd, player); case CMD_REMSHIELD: diff --git a/Source/msg.h b/Source/msg.h index 1cb6dcb2e..3fe6d290a 100644 --- a/Source/msg.h +++ b/Source/msg.h @@ -178,10 +178,12 @@ enum _cmd_id : uint8_t { // // body (TCmd) CMD_CHEAT_EXPERIENCE, - // Cheat: increase active spell level of player. + // Change spell level of player. // - // body (TCmd) - CMD_CHEAT_SPELL_LEVEL, + // body (TCmdParam2) + // int16_t spellID + // int16_t spellLevel + CMD_CHANGE_SPELL_LEVEL, // Debug command (nop). // // body (TCmd) diff --git a/Source/objects.cpp b/Source/objects.cpp index 4baff5ef6..36e8e5a12 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -1979,10 +1979,11 @@ void OperateBook(Player &player, Object &book, bool sendmsg) } if (setlvlnum == SL_BONECHAMB) { - player._pMemSpells |= GetSpellBitmask(SpellID::Guardian); - if (player._pSplLvl[static_cast(SpellID::Guardian)] < MaxSpellLevel) - player._pSplLvl[static_cast(SpellID::Guardian)]++; if (sendmsg) { + uint8_t newSpellLevel = player._pSplLvl[static_cast(SpellID::Guardian)] + 1; + if (newSpellLevel <= MaxSpellLevel) + NetSendCmdParam2(true, CMD_CHANGE_SPELL_LEVEL, static_cast(SpellID::Guardian), newSpellLevel); + Quests[Q_SCHAMB]._qactive = QUEST_DONE; NetSendCmdQuest(true, Quests[Q_SCHAMB]); } @@ -2526,30 +2527,29 @@ void OperateShrineEnchanted(Player &player) int cnt = 0; uint64_t spell = 1; - int maxSpells = gbIsHellfire ? MAX_SPELLS : 37; + uint8_t maxSpells = gbIsHellfire ? MAX_SPELLS : 37; uint64_t spells = player._pMemSpells; - for (int j = 0; j < maxSpells; j++) { + for (uint16_t j = 0; j < maxSpells; j++) { if ((spell & spells) != 0) cnt++; spell *= 2; } if (cnt > 1) { + int spellToReduce; + do { + spellToReduce = GenerateRnd(maxSpells) + 1; + } while ((player._pMemSpells & GetSpellBitmask(static_cast(spellToReduce))) == 0); + spell = 1; - for (int j = static_cast(SpellID::Firebolt); j < maxSpells; j++) { // BUGFIX: < MAX_SPELLS, there is no spell with MAX_SPELLS index (fixed) - if ((player._pMemSpells & spell) != 0) { - if (player._pSplLvl[j] < MaxSpellLevel) - player._pSplLvl[j]++; + for (uint8_t j = static_cast(SpellID::Firebolt); j < maxSpells; j++) { // BUGFIX: < MAX_SPELLS, there is no spell with MAX_SPELLS index (fixed) + if ((player._pMemSpells & spell) != 0 && player._pSplLvl[j] < MaxSpellLevel && j != spellToReduce) { + NetSendCmdParam2(true, CMD_CHANGE_SPELL_LEVEL, j, static_cast(player._pSplLvl[j] + 1)); } spell *= 2; } - int r; - do { - r = GenerateRnd(maxSpells); - } while ((player._pMemSpells & GetSpellBitmask(static_cast(r + 1))) == 0); - if (player._pSplLvl[r + 1] >= 2) - player._pSplLvl[r + 1] -= 2; - else - player._pSplLvl[r + 1] = 0; + + if (player._pSplLvl[spellToReduce] > 0) + NetSendCmdParam2(true, CMD_CHANGE_SPELL_LEVEL, spellToReduce, player._pSplLvl[spellToReduce] - 1); } InitDiabloMsg(EMSG_SHRINE_ENCHANTED); @@ -2579,10 +2579,11 @@ void OperateShrineCostOfWisdom(Player &player, SpellID spellId, diablo_message m player._pMemSpells |= GetSpellBitmask(spellId); - if (player._pSplLvl[static_cast(spellId)] < MaxSpellLevel) - player._pSplLvl[static_cast(spellId)]++; - if (player._pSplLvl[static_cast(spellId)] < MaxSpellLevel) - player._pSplLvl[static_cast(spellId)]++; + uint8_t curSpellLevel = player._pSplLvl[static_cast(spellId)]; + if (curSpellLevel < MaxSpellLevel) { + uint8_t newSpellLevel = std::min(static_cast(curSpellLevel + 2), MaxSpellLevel); + NetSendCmdParam2(true, CMD_CHANGE_SPELL_LEVEL, static_cast(spellId), newSpellLevel); + } uint32_t t = player._pMaxManaBase / 10; int v1 = player._pMana - player._pManaBase;