Browse Source

♻️Remove redundant code from LineClear

LineClear
Anders Jenbo 5 years ago
parent
commit
1d2707eb23
  1. 88
      Source/monster.cpp

88
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) 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; Point position = startPoint;
int dx = endPoint.x - position.x; int d1 = endPoint.x - position.x;
int dy = endPoint.y - position.y; int d2 = endPoint.y - position.y;
if (abs(dx) > abs(dy)) {
if (dx < 0) { bool horizontal = abs(d1) > abs(d2);
std::swap(position, endPoint);
dx = -dx; if (!horizontal)
dy = -dy; std::swap(d1, d2);
}
if (dy > 0) { if (d1 < 0) {
d = 2 * dy - dx; std::swap(position, endPoint);
dincD = 2 * dy; d1 = -d1;
dincH = 2 * (dy - dx); d2 = -d2;
yincD = 1; }
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 { } else {
d = 2 * dy + dx; d += dincH;
dincD = 2 * dy; if (!horizontal)
dincH = 2 * (dx + dy); position.x += incD;
yincD = -1; else
} position.y += incD;
while (position != endPoint) {
if ((d <= 0) ^ (yincD < 0)) {
d += dincD;
} else {
d += dincH;
position.y += yincD;
}
position.x++;
done = position != startPoint && !Clear(entity, position);
} }
} else { if (horizontal)
if (dy < 0) { position.x++;
std::swap(position, endPoint); else
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;
}
position.y++; position.y++;
done = position != startPoint && !Clear(entity, position);
}
} }
return position == endPoint; return position == endPoint;
} }

Loading…
Cancel
Save