Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo Vision #6432

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
116 changes: 59 additions & 57 deletions Source/lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "player.h"
#include "utils/attributes.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

namespace devilution {

std::array<bool, MAXVISION> VisionActive;
Expand All @@ -34,36 +38,42 @@

namespace {

/*
* X- Y-coordinate offsets of lighting visions.
* The last entry-pair is only for alignment.
*/
const DisplacementOf<int8_t> VisionCrawlTable[23][15] = {
// clang-format off
{ { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 }, { 5, 0 }, { 6, 0 }, { 7, 0 }, { 8, 0 }, { 9, 0 }, { 10, 0 }, { 11, 0 }, { 12, 0 }, { 13, 0 }, { 14, 0 }, { 15, 0 } },
{ { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 }, { 5, 0 }, { 6, 0 }, { 7, 0 }, { 8, 1 }, { 9, 1 }, { 10, 1 }, { 11, 1 }, { 12, 1 }, { 13, 1 }, { 14, 1 }, { 15, 1 } },
{ { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 1 }, { 5, 1 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 10, 1 }, { 11, 1 }, { 12, 2 }, { 13, 2 }, { 14, 2 }, { 15, 2 } },
{ { 1, 0 }, { 2, 0 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, { 6, 1 }, { 7, 1 }, { 8, 2 }, { 9, 2 }, { 10, 2 }, { 11, 2 }, { 12, 2 }, { 13, 3 }, { 14, 3 }, { 15, 3 } },
{ { 1, 0 }, { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, { 6, 2 }, { 7, 2 }, { 8, 2 }, { 9, 3 }, { 10, 3 }, { 11, 3 }, { 12, 3 }, { 13, 4 }, { 14, 4 }, { 0, 0 } },
{ { 1, 0 }, { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 2 }, { 6, 2 }, { 7, 3 }, { 8, 3 }, { 9, 3 }, { 10, 4 }, { 11, 4 }, { 12, 4 }, { 13, 5 }, { 14, 5 }, { 0, 0 } },
{ { 1, 0 }, { 2, 1 }, { 3, 1 }, { 4, 2 }, { 5, 2 }, { 6, 3 }, { 7, 3 }, { 8, 3 }, { 9, 4 }, { 10, 4 }, { 11, 5 }, { 12, 5 }, { 13, 6 }, { 14, 6 }, { 0, 0 } },
{ { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 2 }, { 5, 3 }, { 6, 3 }, { 7, 4 }, { 8, 4 }, { 9, 5 }, { 10, 5 }, { 11, 6 }, { 12, 6 }, { 13, 7 }, { 0, 0 }, { 0, 0 } },
{ { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 2 }, { 5, 3 }, { 6, 4 }, { 7, 4 }, { 8, 5 }, { 9, 6 }, { 10, 6 }, { 11, 7 }, { 12, 7 }, { 12, 8 }, { 13, 8 }, { 0, 0 } },
{ { 1, 1 }, { 2, 2 }, { 3, 2 }, { 4, 3 }, { 5, 4 }, { 6, 5 }, { 7, 5 }, { 8, 6 }, { 9, 7 }, { 10, 7 }, { 10, 8 }, { 11, 8 }, { 12, 9 }, { 0, 0 }, { 0, 0 } },
{ { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 5 }, { 7, 6 }, { 8, 7 }, { 9, 8 }, { 10, 9 }, { 11, 9 }, { 11, 10 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
{ { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
{ { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 5, 6 }, { 6, 7 }, { 7, 8 }, { 8, 9 }, { 9, 10 }, { 9, 11 }, { 10, 11 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
{ { 1, 1 }, { 2, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }, { 5, 6 }, { 5, 7 }, { 6, 8 }, { 7, 9 }, { 7, 10 }, { 8, 10 }, { 8, 11 }, { 9, 12 }, { 0, 0 }, { 0, 0 } },
{ { 1, 1 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 5 }, { 4, 6 }, { 4, 7 }, { 5, 8 }, { 6, 9 }, { 6, 10 }, { 7, 11 }, { 7, 12 }, { 8, 12 }, { 8, 13 }, { 0, 0 } },
{ { 1, 1 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 5 }, { 3, 6 }, { 4, 7 }, { 4, 8 }, { 5, 9 }, { 5, 10 }, { 6, 11 }, { 6, 12 }, { 7, 13 }, { 0, 0 }, { 0, 0 } },
{ { 0, 1 }, { 1, 2 }, { 1, 3 }, { 2, 4 }, { 2, 5 }, { 3, 6 }, { 3, 7 }, { 3, 8 }, { 4, 9 }, { 4, 10 }, { 5, 11 }, { 5, 12 }, { 6, 13 }, { 6, 14 }, { 0, 0 } },
{ { 0, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 2, 5 }, { 2, 6 }, { 3, 7 }, { 3, 8 }, { 3, 9 }, { 4, 10 }, { 4, 11 }, { 4, 12 }, { 5, 13 }, { 5, 14 }, { 0, 0 } },
{ { 0, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 }, { 2, 6 }, { 2, 7 }, { 2, 8 }, { 3, 9 }, { 3, 10 }, { 3, 11 }, { 3, 12 }, { 4, 13 }, { 4, 14 }, { 0, 0 } },
{ { 0, 1 }, { 0, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 2, 8 }, { 2, 9 }, { 2, 10 }, { 2, 11 }, { 2, 12 }, { 3, 13 }, { 3, 14 }, { 3, 15 } },
{ { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 1, 8 }, { 1, 9 }, { 1, 10 }, { 1, 11 }, { 2, 12 }, { 2, 13 }, { 2, 14 }, { 2, 15 } },
{ { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 1, 8 }, { 1, 9 }, { 1, 10 }, { 1, 11 }, { 1, 12 }, { 1, 13 }, { 1, 14 }, { 1, 15 } },
{ { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 }, { 0, 10 }, { 0, 11 }, { 0, 12 }, { 0, 13 }, { 0, 14 }, { 0, 15 } },
// clang-format on
constexpr int NumVisionAngles = 72;

// Precalculated cosine used for the vision cone, moving in steps of 5 degrees
// std::cos((i * (360.0f / 72) * PI / 180.0f);
const float VisionDeltaX[NumVisionAngles] = {
glebm marked this conversation as resolved.
Show resolved Hide resolved
1.0, 0.9961946980917455, 0.984807753012208, 0.9659258262890683, 0.9396926207859084, 0.9063077870366499,

Check warning on line 46 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 46 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 46 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 46 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 46 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'
0.8660254037844387, 0.8191520442889918, 0.766044443118978, 0.7071067811865476, 0.6427876096865394,

Check warning on line 47 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 47 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 47 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 47 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'

Check warning on line 47 in Source/lighting.cpp

View workflow job for this annotation

GitHub Actions / build

'initializing': truncation from 'double' to 'const float'
0.5735764363510462, 0.5, 0.42261826174069944, 0.3420201433256688, 0.25881904510252074, 0.17364817766693041,
0.08715574274765814, 0.0, -0.08715574274765824, -0.1736481776669303, -0.25881904510252085, -0.3420201433256687,
-0.42261826174069933, -0.5, -0.5735764363510462, -0.6427876096865394, -0.7071067811865475, -0.7660444431189779,
-0.8191520442889919, -0.8660254037844387, -0.9063077870366499, -0.9396926207859083, -0.9659258262890682,
-0.984807753012208, -0.9961946980917455, -1.0, -0.9961946980917455, -0.984807753012208, -0.9659258262890683,
-0.9396926207859084, -0.90630778703665, -0.8660254037844386, -0.8191520442889918, -0.766044443118978,
-0.7071067811865477, -0.6427876096865395, -0.5735764363510464, -0.5, -0.42261826174069916, -0.34202014332566855,
-0.25881904510252063, -0.17364817766693033, -0.08715574274765825, -1.8369701987210297e-16, 0.08715574274765789,
0.17364817766692997, 0.2588190451025203, 0.342020143325669, 0.4226182617406996, 0.5, 0.573576436351046,
0.6427876096865393, 0.7071067811865474, 0.7660444431189778, 0.8191520442889916, 0.8660254037844384, 0.90630778703665,
0.9396926207859084, 0.9659258262890683, 0.984807753012208, 0.9961946980917455
};

// Precalculated sine used for the vision cone, moving in steps of 5 degrees
// std::sin((i * (360.0f / 72) * PI / 180.0f);
const float VisionDeltaY[NumVisionAngles] = {
0.0, 0.08715574274765817, 0.17364817766693033, 0.25881904510252074, 0.3420201433256687, 0.42261826174069944,
0.5, 0.573576436351046, 0.6427876096865393, 0.7071067811865475, 0.766044443118978, 0.8191520442889918,
0.8660254037844386, 0.9063077870366499, 0.9396926207859083, 0.9659258262890683, 0.984807753012208,
0.9961946980917455, 1.0, 0.9961946980917455, 0.984807753012208, 0.9659258262890683, 0.9396926207859084,
0.90630778703665, 0.8660254037844387, 0.8191520442889917, 0.766044443118978, 0.7071067811865476, 0.6427876096865395,
0.5735764363510459, 0.5, 0.4226182617406994, 0.3420201433256688, 0.25881904510252074, 0.17364817766693041,
0.08715574274765814, 0.0, -0.08715574274765824, -0.1736481776669303, -0.25881904510252085, -0.3420201433256687,
-0.42261826174069933, -0.5, -0.5735764363510462, -0.6427876096865394, -0.7071067811865475, -0.7660444431189779,
-0.8191520442889919, -0.8660254037844387, -0.9063077870366499, -0.9396926207859083, -0.9659258262890682,
-0.984807753012208, -0.9961946980917455, -1.0, -0.9961946980917455, -0.984807753012208, -0.9659258262890683,
-0.9396926207859084, -0.90630778703665, -0.8660254037844386, -0.8191520442889918, -0.766044443118978,
-0.7071067811865477, -0.6427876096865395, -0.5735764363510464, -0.5, -0.42261826174069916, -0.34202014332566855,
-0.25881904510252063, -0.17364817766693033, -0.08715574274765825
};

/** @brief Number of supported light radiuses (first radius starts with 0) */
Expand All @@ -74,9 +84,6 @@
/** interpolations of a 32x32 (16x16 mirrored) light circle moving between tiles in steps of 1/8 of a tile */
uint8_t LightConeInterpolations[8][8][16][16];

/** RadiusAdj maps from VisionCrawlTable index to lighting vision radius adjustment. */
const uint8_t RadiusAdj[23] = { 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 4, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0 };

void RotateRadius(DisplacementOf<int8_t> &offset, DisplacementOf<int8_t> &dist, DisplacementOf<int8_t> &light, DisplacementOf<int8_t> &block)
{
dist = { static_cast<int8_t>(7 - dist.deltaY), dist.deltaX };
Expand Down Expand Up @@ -286,34 +293,29 @@
{
DoVisionFlags(position, doAutomap, visible);

static const Displacement factors[] = { { 1, 1 }, { -1, 1 }, { 1, -1 }, { -1, -1 } };
for (auto factor : factors) {
for (int j = 0; j < 23; j++) {
int lineLen = radius - RadiusAdj[j];
for (int k = 0; k < lineLen; k++) {
Point crawl = position + VisionCrawlTable[j][k] * factor;
if (!InDungeonBounds(crawl))
break;
bool blockerFlag = TileHasAny(dPiece[crawl.x][crawl.y], TileProperties::BlockLight);
bool tileOK = !blockerFlag;

if (VisionCrawlTable[j][k].deltaX > 0 && VisionCrawlTable[j][k].deltaY > 0) {
tileOK = tileOK || TileAllowsLight(crawl + Displacement { -factor.deltaX, 0 });
tileOK = tileOK || TileAllowsLight(crawl + Displacement { 0, -factor.deltaY });
}
for (int i = 0; i < NumVisionAngles; i++) {
float dx = VisionDeltaX[i];
float dy = VisionDeltaY[i];

if (!tileOK)
break;
for (int distance = 1; distance <= radius; distance++) {
float x = (position.x + 0.5f) * TILE_WIDTH + dx * distance * TILE_WIDTH;
float y = (position.y + 0.5f) * TILE_HEIGHT + dy * distance * TILE_HEIGHT;

DoVisionFlags(crawl, doAutomap, visible);
int tileX = static_cast<int>(x / TILE_WIDTH);
int tileY = static_cast<int>(y / TILE_HEIGHT);

if (blockerFlag)
break;
if (!InDungeonBounds({ tileX, tileY }))
break;

int8_t trans = dTransVal[crawl.x][crawl.y];
if (trans != 0)
TransList[trans] = true;
}
Point target { tileX, tileY };
DoVisionFlags(target, doAutomap, visible);

if (TileHasAny(dPiece[tileX][tileY], TileProperties::BlockLight))
break;

int8_t trans = dTransVal[tileX][tileY];
if (trans != 0)
TransList[trans] = true;
}
}
}
Expand Down
Loading