Browse Source

♻️ Move 'fromDirection' from 'Point' type to 'Displacement' type

pull/2262/head^2
Juliano Leal Goncalves 5 years ago committed by Anders Jenbo
parent
commit
0441ffc2f4
  1. 2
      Source/control.cpp
  2. 2
      Source/controls/plrctrls.cpp
  3. 24
      Source/engine/displacement.hpp
  4. 40
      Source/engine/point.hpp
  5. 2
      Source/monster.cpp
  6. 8
      Source/player.cpp

2
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);

2
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;
}

24
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

40
Source/engine/point.hpp

@ -3,6 +3,7 @@
#include <cmath>
#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;

2
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<Direction>(GenerateRnd(8))).x, yp += Point::fromDirection(static_cast<Direction>(GenerateRnd(8))).x) { /// BUGFIX: `yp += Point.y`
for (try2 = 0; j < num && try2 < 100; xp += Displacement::fromDirection(static_cast<Direction>(GenerateRnd(8))).deltaX, yp += Displacement::fromDirection(static_cast<Direction>(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))) {

8
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);

Loading…
Cancel
Save