From 0441ffc2f41f60a8c9b0e4945ad464db3516abf3 Mon Sep 17 00:00:00 2001 From: Juliano Leal Goncalves Date: Sun, 27 Jun 2021 23:57:59 -0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20'fromDirection'=20f?= =?UTF-8?q?rom=20'Point'=20type=20to=20'Displacement'=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/control.cpp | 2 +- Source/controls/plrctrls.cpp | 2 +- Source/engine/displacement.hpp | 24 ++++++++++++++++++++ Source/engine/point.hpp | 40 +++++++++++++--------------------- Source/monster.cpp | 2 +- Source/player.cpp | 8 +++---- 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index f36c0f4f9..cc4c80ca5 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -323,7 +323,7 @@ static void DrawSpell(const CelOutputBuffer &out) static void PrintSBookHotkey(const CelOutputBuffer &out, Point position, const std::string &text) { // Align the hot key text with the top-right corner of the spell icon - position += { SPLICONLENGTH - (GetLineWidth(text.c_str()) + 5), 17 - SPLICONLENGTH }; + position += Displacement { SPLICONLENGTH - (GetLineWidth(text.c_str()) + 5), 17 - SPLICONLENGTH }; // Draw a drop shadow below and to the left of the text DrawString(out, text.c_str(), position + Point { -1, 1 }, UIS_BLACK); diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 793936b73..d7d4a6753 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -1417,7 +1417,7 @@ void UpdateSpellTarget() if (plr[myplr]._pRSpell == SPL_TELEPORT) range = 4; - auto cursm = plr[myplr].position.future + Point::fromDirection(plr[myplr]._pdir) * range; + auto cursm = plr[myplr].position.future + Displacement::fromDirection(plr[myplr]._pdir) * range; cursmx = cursm.x; cursmy = cursm.y; } diff --git a/Source/engine/displacement.hpp b/Source/engine/displacement.hpp index c84534208..46d346937 100644 --- a/Source/engine/displacement.hpp +++ b/Source/engine/displacement.hpp @@ -82,6 +82,30 @@ struct Displacement { { return { abs(a.deltaX), abs(a.deltaY) }; } + + static constexpr Displacement fromDirection(Direction direction) + { + switch (direction) { + case DIR_S: + return { 1, 1 }; + case DIR_SW: + return { 0, 1 }; + case DIR_W: + return { -1, 1 }; + case DIR_NW: + return { -1, 0 }; + case DIR_N: + return { -1, -1 }; + case DIR_NE: + return { 0, -1 }; + case DIR_E: + return { 1, -1 }; + case DIR_SE: + return { 1, 0 }; + default: + return { 0, 0 }; + } + }; }; } // namespace devilution diff --git a/Source/engine/point.hpp b/Source/engine/point.hpp index a25f1175e..9759b6322 100644 --- a/Source/engine/point.hpp +++ b/Source/engine/point.hpp @@ -3,6 +3,7 @@ #include #include "engine/direction.hpp" +#include "engine/displacement.hpp" #include "utils/stdcompat/abs.hpp" #include "utils/stdcompat/algorithm.hpp" @@ -12,30 +13,6 @@ struct Point { int x; int y; - static constexpr Point fromDirection(Direction direction) - { - switch (direction) { - case DIR_S: - return { 1, 1 }; - case DIR_SW: - return { 0, 1 }; - case DIR_W: - return { -1, 1 }; - case DIR_NW: - return { -1, 0 }; - case DIR_N: - return { -1, -1 }; - case DIR_NE: - return { 0, -1 }; - case DIR_E: - return { 1, -1 }; - case DIR_SE: - return { 1, 0 }; - default: - return { 0, 0 }; - } - }; - constexpr bool operator==(const Point &other) const { return x == other.x && y == other.y; @@ -53,9 +30,16 @@ struct Point { return *this; } + constexpr Point &operator+=(const Displacement &displacement) + { + x += displacement.deltaX; + y += displacement.deltaY; + return *this; + } + constexpr Point &operator+=(Direction direction) { - return (*this) += Point::fromDirection(direction); + return (*this) += Displacement::fromDirection(direction); } constexpr Point &operator-=(const Point &other) @@ -85,6 +69,12 @@ struct Point { return a; } + constexpr friend Point operator+(Point a, Displacement displacement) + { + a += displacement; + return a; + } + constexpr friend Point operator+(Point a, Direction direction) { a += direction; diff --git a/Source/monster.cpp b/Source/monster.cpp index 7f728262d..8a0c13e3f 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -1037,7 +1037,7 @@ void PlaceGroup(int mtype, int num, int leaderf, int leader) } j = 0; - for (try2 = 0; j < num && try2 < 100; xp += Point::fromDirection(static_cast(GenerateRnd(8))).x, yp += Point::fromDirection(static_cast(GenerateRnd(8))).x) { /// BUGFIX: `yp += Point.y` + for (try2 = 0; j < num && try2 < 100; xp += Displacement::fromDirection(static_cast(GenerateRnd(8))).deltaX, yp += Displacement::fromDirection(static_cast(GenerateRnd(8))).deltaX) { /// BUGFIX: `yp += Point.y` if (!MonstPlace(xp, yp) || (dTransVal[xp][yp] != dTransVal[x1][y1]) || ((leaderf & 2) != 0 && (abs(xp - x1) >= 4 || abs(yp - y1) >= 4))) { diff --git a/Source/player.cpp b/Source/player.cpp index 43186616c..c3b201379 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -1660,13 +1660,13 @@ static void RespawnDeadItem(ItemStruct *itm, Point target) itm->_itype = ITYPE_NONE; } -static void PlrDeadItem(PlayerStruct &player, ItemStruct *itm, Point direction) +static void PlrDeadItem(PlayerStruct &player, ItemStruct *itm, Displacement direction) { if (itm->isEmpty()) return; - Point target = direction + player.position.tile; - if (direction != Point { 0, 0 } && ItemSpaceOk(target)) { + Point target = player.position.tile + direction; + if (direction != Displacement { 0, 0 } && ItemSpaceOk(target)) { RespawnDeadItem(itm, target); player.HoldItem = *itm; NetSendCmdPItem(false, CMD_RESPAWNITEM, target); @@ -1778,7 +1778,7 @@ StartPlayerKill(int pnum, int earflag) Direction pdd = player._pdir; for (auto &item : player.InvBody) { pdd = left[pdd]; - PlrDeadItem(player, &item, Point::fromDirection(pdd)); + PlrDeadItem(player, &item, Displacement::fromDirection(pdd)); } CalcPlrInv(pnum, false);