From 95daecc1bb9b28980467bf4851be32c5a4c3914b Mon Sep 17 00:00:00 2001 From: obligaron Date: Sun, 1 Aug 2021 09:32:54 +0200 Subject: [PATCH] Introduce GetSpellListSelection and remove selection game logic from DrawSpellList --- Source/control.cpp | 74 +++++++++++++++++++++++++++++++++++++++---- Source/control.h | 2 -- test/control_test.cpp | 14 -------- 3 files changed, 68 insertions(+), 22 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index 159dfcdb1..982be0fbb 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -47,11 +47,9 @@ int dropGoldValue; bool drawmanaflag; bool chrbtnactive; int pnumlines; -spell_id pSpell; UiFlags InfoColor; char tempstr[256]; int sbooktab; -spell_type pSplType; int8_t initialDropGoldIndex; bool talkflag; bool sbookflag; @@ -581,6 +579,64 @@ void RemoveGold(int pnum, int goldIndex) dropGoldValue = 0; } +bool GetSpellListSelection(spell_id &pSpell, spell_type &pSplType) +{ + pSpell = spell_id::SPL_INVALID; + pSplType = spell_type::RSPLTYPE_INVALID; + + uint64_t mask; + + int x = PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS; + int y = PANEL_Y - 17; + + for (int i = RSPLTYPE_SKILL; i < RSPLTYPE_INVALID; i++) { + auto &myPlayer = Players[MyPlayerId]; + switch ((spell_type)i) { + case RSPLTYPE_SKILL: + mask = myPlayer._pAblSpells; + break; + case RSPLTYPE_SPELL: + mask = myPlayer._pMemSpells; + break; + case RSPLTYPE_SCROLL: + mask = myPlayer._pScrlSpells; + break; + case RSPLTYPE_CHARGES: + mask = myPlayer._pISpells; + break; + case RSPLTYPE_INVALID: + break; + } + int8_t j = SPL_FIREBOLT; + for (uint64_t spl = 1; j < MAX_SPELLS; spl <<= 1, j++) { + if ((mask & spl) == 0) + continue; + int lx = x; + int ly = y - SPLICONLENGTH; + if (MousePosition.x >= lx && MousePosition.x < lx + SPLICONLENGTH && MousePosition.y >= ly && MousePosition.y < ly + SPLICONLENGTH) { + pSpell = (spell_id) j; + pSplType = (spell_type)i; + if (myPlayer._pClass == HeroClass::Monk && j == SPL_SEARCH) + pSplType = RSPLTYPE_SKILL; + return true; + } + x -= SPLICONLENGTH; + if (x == PANEL_X + 12 - SPLICONLENGTH) { + x = PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS; + y -= SPLICONLENGTH; + } + } + if (mask != 0 && x != PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS) + x -= SPLICONLENGTH; + if (x == PANEL_X + 12 - SPLICONLENGTH) { + x = PANEL_X + 12 + SPLICONLENGTH * SPLROWICONLS; + y -= SPLICONLENGTH; + } + } + + return false; +} + } // namespace void DrawSpell(const Surface &out) @@ -611,7 +667,7 @@ void DrawSpellList(const Surface &out) int s; uint64_t mask; - pSpell = SPL_INVALID; + spell_id pSpell = SPL_INVALID; infostr[0] = '\0'; Point location; int &x = location.x; @@ -663,7 +719,7 @@ void DrawSpellList(const Surface &out) int ly = y - SPLICONLENGTH; if (MousePosition.x >= lx && MousePosition.x < lx + SPLICONLENGTH && MousePosition.y >= ly && MousePosition.y < ly + SPLICONLENGTH) { pSpell = (spell_id)j; - pSplType = (spell_type)i; + spell_type pSplType = (spell_type)i; if (myPlayer._pClass == HeroClass::Monk && j == SPL_SEARCH) pSplType = RSPLTYPE_SKILL; DrawSpellCel(out, location, *pSpellCels, c); @@ -738,8 +794,11 @@ void DrawSpellList(const Surface &out) void SetSpell() { + spell_id pSpell; + spell_type pSplType; + spselflag = false; - if (pSpell == SPL_INVALID) { + if (!GetSpellListSelection(pSpell, pSplType)) { return; } @@ -754,7 +813,10 @@ void SetSpell() void SetSpeedSpell(int slot) { - if (pSpell == SPL_INVALID) { + spell_id pSpell; + spell_type pSplType; + + if (!GetSpellListSelection(pSpell, pSplType)) { return; } auto &myPlayer = Players[MyPlayerId]; diff --git a/Source/control.h b/Source/control.h index 0f5eb1d42..e59a28315 100644 --- a/Source/control.h +++ b/Source/control.h @@ -36,11 +36,9 @@ extern int dropGoldValue; extern bool drawmanaflag; extern bool chrbtnactive; extern int pnumlines; -extern spell_id pSpell; extern UiFlags InfoColor; extern char tempstr[256]; extern int sbooktab; -extern spell_type pSplType; extern int8_t initialDropGoldIndex; extern bool talkflag; extern bool sbookflag; diff --git a/test/control_test.cpp b/test/control_test.cpp index 7871c94d7..4cba8b1dd 100644 --- a/test/control_test.cpp +++ b/test/control_test.cpp @@ -4,20 +4,6 @@ using namespace devilution; -TEST(Control, SetSpell) -{ - pnumlines = 1; - pSpell = SPL_FIREBOLT; - pSplType = RSPLTYPE_CHARGES; - auto &myPlayer = Players[MyPlayerId]; - SetSpell(); - EXPECT_EQ(spselflag, false); - EXPECT_EQ(myPlayer._pRSpell, SPL_FIREBOLT); - EXPECT_EQ(myPlayer._pRSplType, RSPLTYPE_CHARGES); - EXPECT_EQ(pnumlines, 0); - EXPECT_EQ(force_redraw, 255); -} - TEST(Control, ClearPanel) { pnumlines = 1;