From 0b19c2fd4f59ab0bac87c04cd32456434ab8ad3a Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Fri, 21 Apr 2023 00:16:06 +0200 Subject: [PATCH] Assert correct use of LightFalloffs --- Source/lighting.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Source/lighting.cpp b/Source/lighting.cpp index e34a94320..522629d8c 100644 --- a/Source/lighting.cpp +++ b/Source/lighting.cpp @@ -62,8 +62,10 @@ const DisplacementOf VisionCrawlTable[23][15] = { // clang-format on }; -/** 16 falloff tables for the light cone */ -uint8_t LightFalloffs[16][128]; +/** @brief Number of supported light radiuses (first radius starts with 0) */ +constexpr size_t NumLightRadiuses = 16; +/** Falloff tables for the light cone */ +uint8_t LightFalloffs[NumLightRadiuses][128]; bool dovision; /** 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]; @@ -218,6 +220,9 @@ bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref= 0 && nRadius <= NumLightRadiuses); + assert(InDungeonBounds(position)); + int xoff = 0; int yoff = 0; int lightX = 0; @@ -259,12 +264,10 @@ void DoLighting(Point position, int nRadius, int lnum) maxY = MAXDUNY - position.y; } - if (InDungeonBounds(position)) { - if (IsNoneOf(leveltype, DTYPE_NEST, DTYPE_CRYPT)) { - SetLight(position, 0); - } else if (GetLight(position) > LightFalloffs[nRadius][0]) { - SetLight(position, LightFalloffs[nRadius][0]); - } + if (IsNoneOf(leveltype, DTYPE_NEST, DTYPE_CRYPT)) { + SetLight(position, 0); + } else if (GetLight(position) > LightFalloffs[nRadius][0]) { + SetLight(position, LightFalloffs[nRadius][0]); } for (int i = 0; i < 4; i++) { @@ -394,21 +397,21 @@ void MakeLightTable() // Generate light falloffs ranges if (IsAnyOf(leveltype, DTYPE_NEST, DTYPE_CRYPT)) { - for (int j = 0; j < 16; j++) { + for (int j = 0; j < NumLightRadiuses; j++) { double fa = (sqrt((double)(16 - j))) / 128; fa *= fa; for (int i = 0; i < 128; i++) { uint8_t color = 15 - static_cast(fa * (double)((128 - i) * (128 - i))); if (color > 15) color = 0; - color -= static_cast((15 - j) / 2); + color -= static_cast((NumLightRadiuses - j - 1) / 2); if (color > 15) color = 0; - LightFalloffs[15 - j][i] = color; + LightFalloffs[NumLightRadiuses - j - 1][i] = color; } } } else { - for (int j = 0; j < 16; j++) { + for (int j = 0; j < NumLightRadiuses; j++) { for (int i = 0; i < 128; i++) { if (i > (j + 1) * 8) { LightFalloffs[j][i] = 15;