From 828a52dc6df9ec9d8ce655cb695f4388550e9ef1 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 19 Apr 2023 01:37:16 +0200 Subject: [PATCH] Clean up ProcessLightsList() --- Source/levels/drlg_l3.cpp | 8 ++-- Source/lighting.cpp | 93 ++++++++++++++------------------------- Source/lighting.h | 2 +- Source/objects.cpp | 2 +- 4 files changed, 40 insertions(+), 65 deletions(-) diff --git a/Source/levels/drlg_l3.cpp b/Source/levels/drlg_l3.cpp index 34f4148b7..009a5fbb0 100644 --- a/Source/levels/drlg_l3.cpp +++ b/Source/levels/drlg_l3.cpp @@ -2150,11 +2150,11 @@ void PlaceCaveLights() for (int j = 0; j < MAXDUNY; j++) { for (int i = 0; i < MAXDUNX; i++) { if (dPiece[i][j] >= 55 && dPiece[i][j] <= 146) { - DoLighting({ i, j }, 7, -1); + DoLighting({ i, j }, 7, {}); } else if (dPiece[i][j] >= 153 && dPiece[i][j] <= 160) { - DoLighting({ i, j }, 7, -1); + DoLighting({ i, j }, 7, {}); } else if (IsAnyOf(dPiece[i][j], 149, 151)) { - DoLighting({ i, j }, 7, -1); + DoLighting({ i, j }, 7, {}); } } } @@ -2166,7 +2166,7 @@ void PlaceHiveLights() for (int j = 0; j < MAXDUNY; j++) { for (int i = 0; i < MAXDUNX; i++) { if (dPiece[i][j] >= 381 && dPiece[i][j] <= 456) { - DoLighting({ i, j }, 9, -1); + DoLighting({ i, j }, 9, {}); } } } diff --git a/Source/lighting.cpp b/Source/lighting.cpp index 566a90dcf..796b445e6 100644 --- a/Source/lighting.cpp +++ b/Source/lighting.cpp @@ -75,28 +75,21 @@ 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(int *x, int *y, int *dx, int *dy, int *lx, int *ly, int *bx, int *by) +void RotateRadius(DisplacementOf &offset, DisplacementOf &dist, DisplacementOf &light, DisplacementOf &block) { - *bx = 0; - *by = 0; - - int swap = *dx; - *dx = 7 - *dy; - *dy = swap; - swap = *lx; - *lx = 7 - *ly; - *ly = swap; - - *x = *dx - *lx; - *y = *dy - *ly; - - if (*x < 0) { - *x += 8; - *bx = 1; + dist = { static_cast(7 - dist.deltaY), dist.deltaX }; + light = { static_cast(7 - light.deltaY), light.deltaX }; + offset = { static_cast(dist.deltaX - light.deltaX), static_cast(dist.deltaY - light.deltaY) }; + + block.deltaX = 0; + if (offset.deltaX < 0) { + offset.deltaX += 8; + block.deltaX = 1; } - if (*y < 0) { - *y += 8; - *by = 1; + block.deltaY = 0; + if (offset.deltaY < 0) { + offset.deltaY += 8; + block.deltaY = 1; } } @@ -210,34 +203,24 @@ bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref offset) { assert(radius >= 0 && radius <= NumLightRadiuses); assert(InDungeonBounds(position)); - int xoff = 0; - int yoff = 0; - int lightX = 0; - int lightY = 0; - int blockX = 0; - int blockY = 0; - - if (Lnum >= 0) { - Light &light = Lights[Lnum]; - xoff = light.position.offset.deltaX; - yoff = light.position.offset.deltaY; - if (xoff < 0) { - xoff += 8; - position -= { 1, 0 }; - } - if (yoff < 0) { - yoff += 8; - position -= { 0, 1 }; - } + DisplacementOf light = {}; + DisplacementOf block = {}; + + if (offset.deltaX < 0) { + offset.deltaX += 8; + position -= { 1, 0 }; + } + if (offset.deltaY < 0) { + offset.deltaY += 8; + position -= { 0, 1 }; } - int distX = xoff; - int distY = yoff; + DisplacementOf dist = offset; int minX = 15; if (position.x - 15 < 0) { @@ -267,7 +250,7 @@ void DoLighting(Point position, uint8_t radius, int Lnum) int xBound = i < 2 ? maxX : minX; for (int y = 0; y < yBound; y++) { for (int x = 1; x < xBound; x++) { - int linearDistance = LightConeInterpolations[xoff][yoff][x + blockX][y + blockY]; + int linearDistance = LightConeInterpolations[offset.deltaX][offset.deltaY][x + block.deltaX][y + block.deltaY]; if (linearDistance >= 128) continue; Point temp = position + (Displacement { x, y }).Rotate(-i); @@ -278,7 +261,7 @@ void DoLighting(Point position, uint8_t radius, int Lnum) SetLight(temp, v); } } - RotateRadius(&xoff, &yoff, &distX, &distY, &lightX, &lightY, &blockX, &blockY); + RotateRadius(offset, dist, light, block); } } @@ -439,7 +422,7 @@ void ToggleLighting() memcpy(dLight, dPreLight, sizeof(dLight)); for (const Player &player : Players) { if (player.plractive && player.isOnActiveLevel()) { - DoLighting(player.position.tile, player._pLightRad, -1); + DoLighting(player.position.tile, player._pLightRad, {}); } } } @@ -587,20 +570,14 @@ void ProcessLightList() } } for (int i = 0; i < ActiveLightCount; i++) { - int j = ActiveLights[i]; - Light &light = Lights[j]; - if (!light.isInvalid) { - DoLighting(light.position.tile, light.radius, j); - } - } - int i = 0; - while (i < ActiveLightCount) { - if (Lights[ActiveLights[i]].isInvalid) { + const Light &light = Lights[ActiveLights[i]]; + if (light.isInvalid) { ActiveLightCount--; std::swap(ActiveLights[ActiveLightCount], ActiveLights[i]); - } else { - i++; + i--; + continue; } + DoLighting(light.position.tile, light.radius, light.position.offset); } UpdateLighting = false; @@ -648,9 +625,7 @@ void ProcessVisionList() if (!UpdateVision) return; - for (int i = 0; i < TransVal; i++) { - TransList[i] = false; - } + TransList = {}; for (const Player &player : Players) { int id = player.getId(); diff --git a/Source/lighting.h b/Source/lighting.h index 95e0f8407..ee0424f02 100644 --- a/Source/lighting.h +++ b/Source/lighting.h @@ -56,7 +56,7 @@ extern bool DisableLighting; #endif extern bool UpdateLighting; -void DoLighting(Point position, uint8_t radius, int Lnum); +void DoLighting(Point position, uint8_t radius, DisplacementOf offset); void DoUnVision(Point position, uint8_t radius); void DoVision(Point position, uint8_t radius, MapExplorationType doAutomap, bool visible); void MakeLightTable(); diff --git a/Source/objects.cpp b/Source/objects.cpp index f84b48bae..7cb4acd29 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -1348,7 +1348,7 @@ void AddTrap(Object &trap) void AddObjectLight(Object &object, int r) { if (ApplyObjectLighting) { - DoLighting(object.position, r, -1); + DoLighting(object.position, r, {}); object._oVar1 = -1; } else { object._oVar1 = 0;