From 1d2707eb23d852dffd17618c830e004be2283b82 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Fri, 28 May 2021 21:50:53 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8FRemove=20redundant=20code=20f?= =?UTF-8?q?rom=20LineClear?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/monster.cpp | 88 ++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 57 deletions(-) diff --git a/Source/monster.cpp b/Source/monster.cpp index 78fbd4baf..975ea5f42 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -4758,69 +4758,43 @@ bool LineClearMissile(Point startPoint, Point endPoint) bool LineClear(bool (*Clear)(int, Point), int entity, Point startPoint, Point endPoint) { - int d; - int xincD, yincD, dincD, dincH; - bool done = false; - Point position = startPoint; - int dx = endPoint.x - position.x; - int dy = endPoint.y - position.y; - if (abs(dx) > abs(dy)) { - if (dx < 0) { - std::swap(position, endPoint); - dx = -dx; - dy = -dy; - } - if (dy > 0) { - d = 2 * dy - dx; - dincD = 2 * dy; - dincH = 2 * (dy - dx); - yincD = 1; + int d1 = endPoint.x - position.x; + int d2 = endPoint.y - position.y; + + bool horizontal = abs(d1) > abs(d2); + + if (!horizontal) + std::swap(d1, d2); + + if (d1 < 0) { + std::swap(position, endPoint); + d1 = -d1; + d2 = -d2; + } + + int incD = (d2 > 0) ? 1 : -1; + int d = 2 * d2 - (d1 * incD); + int dincD = 2 * d2; + int dincH = 2 * (d2 - (d1 * incD)); + + while (position != endPoint && (position == startPoint || Clear(entity, position))) { + if ((d <= 0) ^ (incD < 0)) { + d += dincD; } else { - d = 2 * dy + dx; - dincD = 2 * dy; - dincH = 2 * (dx + dy); - yincD = -1; - } - while (position != endPoint) { - if ((d <= 0) ^ (yincD < 0)) { - d += dincD; - } else { - d += dincH; - position.y += yincD; - } - position.x++; - done = position != startPoint && !Clear(entity, position); + d += dincH; + if (!horizontal) + position.x += incD; + else + position.y += incD; } - } else { - if (dy < 0) { - std::swap(position, endPoint); - dy = -dy; - dx = -dx; - } - if (dx > 0) { - d = 2 * dx - dy; - dincD = 2 * dx; - dincH = 2 * (dx - dy); - xincD = 1; - } else { - d = 2 * dx + dy; - dincD = 2 * dx; - dincH = 2 * (dy + dx); - xincD = -1; - } - while (!done && position != endPoint) { - if ((d <= 0) ^ (xincD < 0)) { - d += dincD; - } else { - d += dincH; - position.x += xincD; - } + if (horizontal) + position.x++; + else position.y++; - done = position != startPoint && !Clear(entity, position); - } } + return position == endPoint; }