From 8a93087fb7e8b5d8cdd8d0f384fc0e4840c0b208 Mon Sep 17 00:00:00 2001 From: Juliano Leal Goncalves Date: Sun, 29 Aug 2021 23:40:53 -0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20quest=20entrance?= =?UTF-8?q?=20boundary=20check=20to=20a=20member=20function=20on=20QuestSt?= =?UTF-8?q?ruct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/quests.cpp | 26 ++++++++++++++------------ Source/quests.h | 6 ++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Source/quests.cpp b/Source/quests.cpp index 3ce858793..34654c5d5 100644 --- a/Source/quests.cpp +++ b/Source/quests.cpp @@ -85,12 +85,6 @@ int topY; int lineSpacing; int act2finSpacing; -/** - * Specifies a displacement from the quest entrance for - * which the hover text of the cursor will be visible. - */ -Displacement questEntranceOffsets[7] = { { 0, 0 }, { -1, 0 }, { 0, -1 }, { -1, -1 }, { -2, -1 }, { -1, -2 }, { -2, -2 } }; - const char *const QuestTriggerNames[5] = { N_(/* TRANSLATORS: Quest Map*/ "King Leoric's Tomb"), N_(/* TRANSLATORS: Quest Map*/ "The Chamber of Bone"), @@ -451,12 +445,10 @@ bool ForceQuests() if (quest._qidx != Q_BETRAYER && currlevel == quest._qlevel && quest._qslvl != 0) { int ql = quest._qslvl - 1; - for (int j = 0; j < 7; j++) { - if (quest.position + questEntranceOffsets[j] == cursPosition) { - strcpy(infostr, fmt::format(_(/* TRANSLATORS: Used for Quest Portals. {:s} is a Map Name */ "To {:s}"), _(QuestTriggerNames[ql])).c_str()); - cursPosition = quest.position; - return true; - } + if (quest.EntranceBoundaryContains(cursPosition)) { + strcpy(infostr, fmt::format(_(/* TRANSLATORS: Used for Quest Portals. {:s} is a Map Name */ "To {:s}"), _(QuestTriggerNames[ql])).c_str()); + cursPosition = quest.position; + return true; } } } @@ -889,4 +881,14 @@ bool Quest::IsAvailable() return true; } +bool Quest::EntranceBoundaryContains(Point position) const +{ + constexpr Displacement questEntranceOffsets[7] = { { 0, 0 }, { -1, 0 }, { 0, -1 }, { -1, -1 }, { -2, -1 }, { -1, -2 }, { -2, -2 } }; + + return std::any_of( + std::begin(questEntranceOffsets), + std::end(questEntranceOffsets), + [&](auto offset) { return this->position + offset == position; }); +} + } // namespace devilution diff --git a/Source/quests.h b/Source/quests.h index acec3bf0f..90e0789f4 100644 --- a/Source/quests.h +++ b/Source/quests.h @@ -57,6 +57,12 @@ struct Quest { uint8_t _qvar2; bool IsAvailable(); + + /** + * @brief Gets a value indicating whether the provided position is in the entrance boundary of the quest. + * @param position The position to check against this quest's entrance boundary. + */ + bool EntranceBoundaryContains(Point position) const; }; struct QuestData {