From 25d412d1ac1c96e94e5bd80b224e2ce3b6d80ae3 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Thu, 7 Apr 2022 05:04:34 +0300 Subject: [PATCH] Remove duplicate code in DoLighting --- Source/engine/displacement.hpp | 12 +++++++ Source/lighting.cpp | 66 ++++++++-------------------------- 2 files changed, 27 insertions(+), 51 deletions(-) diff --git a/Source/engine/displacement.hpp b/Source/engine/displacement.hpp index 8c47a3afd..0a0c29da5 100644 --- a/Source/engine/displacement.hpp +++ b/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) { diff --git a/Source/lighting.cpp b/Source/lighting.cpp index 664e130d4..84c495ac3 100644 --- a/Source/lighting.cpp +++ b/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); } }