Browse Source

Remove duplicate code in DoLighting

pull/4424/head
Vladimir Olteanu 4 years ago committed by Anders Jenbo
parent
commit
25d412d1ac
  1. 12
      Source/engine/displacement.hpp
  2. 66
      Source/lighting.cpp

12
Source/engine/displacement.hpp

@ -125,6 +125,18 @@ struct Displacement {
return { (deltaY - deltaX) * 32, (deltaY + deltaX) * -16 };
}
constexpr Displacement Rotate(int quadrants)
{
constexpr int Sines[] = { 0, 1, 0, -1 };
quadrants = (quadrants % 4 + 4) % 4;
int sine = Sines[quadrants];
int cosine = Sines[(quadrants + 1) % 4];
return Displacement { deltaX * cosine - deltaY * sine, deltaX * sine + deltaY * cosine };
}
private:
static constexpr Displacement fromDirection(Direction direction)
{

66
Source/lighting.cpp

@ -571,60 +571,24 @@ void DoLighting(Point position, int nRadius, int lnum)
}
}
int mult = xoff + 8 * yoff;
for (int y = 0; y < minY; y++) {
for (int x = 1; x < maxX; x++) {
int radiusBlock = lightblock[mult][y][x];
if (radiusBlock < 128) {
Point temp = position + Displacement { x, y };
int8_t v = lightradius[nRadius][radiusBlock];
if (InDungeonBounds(temp))
if (v < GetLight(temp))
SetLight(temp, v);
}
}
}
RotateRadius(&xoff, &yoff, &distX, &distY, &lightX, &lightY, &blockX, &blockY);
mult = xoff + 8 * yoff;
for (int y = 0; y < maxY; y++) {
for (int x = 1; x < maxX; x++) {
int radiusBlock = lightblock[mult][y + blockY][x + blockX];
if (radiusBlock < 128) {
Point temp = position + Displacement { y, -x };
int8_t v = lightradius[nRadius][radiusBlock];
if (InDungeonBounds(temp))
if (v < GetLight(temp))
SetLight(temp, v);
}
}
}
RotateRadius(&xoff, &yoff, &distX, &distY, &lightX, &lightY, &blockX, &blockY);
mult = xoff + 8 * yoff;
for (int y = 0; y < maxY; y++) {
for (int x = 1; x < minX; x++) {
int radiusBlock = lightblock[mult][y + blockY][x + blockX];
if (radiusBlock < 128) {
Point temp = position - Displacement { x, y };
int8_t v = lightradius[nRadius][radiusBlock];
if (InDungeonBounds(temp))
if (v < GetLight(temp))
SetLight(temp, v);
}
}
}
RotateRadius(&xoff, &yoff, &distX, &distY, &lightX, &lightY, &blockX, &blockY);
mult = xoff + 8 * yoff;
for (int y = 0; y < minY; y++) {
for (int x = 1; x < minX; x++) {
int radiusBlock = lightblock[mult][y + blockY][x + blockX];
if (radiusBlock < 128) {
Point temp = position + Displacement { -y, x };
for (int i = 0; i < 4; i++) {
int mult = xoff + 8 * yoff;
int yBound = i > 0 && i < 3 ? maxY : minY;
int xBound = i < 2 ? maxX : minX;
for (int y = 0; y < yBound; y++) {
for (int x = 1; x < xBound; x++) {
int radiusBlock = lightblock[mult][y + blockY][x + blockX];
if (radiusBlock >= 128)
continue;
Point temp = position + (Displacement { x, y }).Rotate(-i);
int8_t v = lightradius[nRadius][radiusBlock];
if (InDungeonBounds(temp))
if (v < GetLight(temp))
SetLight(temp, v);
if (!InDungeonBounds(temp))
continue;
if (v < GetLight(temp))
SetLight(temp, v);
}
}
RotateRadius(&xoff, &yoff, &distX, &distY, &lightX, &lightY, &blockX, &blockY);
}
}

Loading…
Cancel
Save