Skip to content

Commit

Permalink
use ray-tracing to calculate the base lighting in the dungeon
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Sep 10, 2023
1 parent ac03e14 commit f6fe692
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
9 changes: 2 additions & 7 deletions Source/gendung.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,18 +1365,13 @@ static void DRLG_LightSubtiles()
#endif
memset(dLight, c, sizeof(dLight));

assert(LightList[MAXLIGHTS]._lxoff == 0);
assert(LightList[MAXLIGHTS]._lyoff == 0);
if (!nCollLightTable[0]) {
for (i = 0; i < MAXDUNX; i++) {
for (j = 0; j < MAXDUNY; j++) {
pn = dPiece[i][j];
c = nCollLightTable[pn] & PSF_LIGHT_RADIUS;
if (c != 0) {
LightList[MAXLIGHTS]._lradius = c;
LightList[MAXLIGHTS]._lx = i;
LightList[MAXLIGHTS]._ly = j;
DoLighting(MAXLIGHTS);
TraceLightSource(i, j, c);
}
}
}
Expand Down Expand Up @@ -1615,7 +1610,7 @@ void DRLG_ChangeMap(int x1, int y1, int x2, int y2/*, bool hasNewObjPiece*/)
y1 = 2 * y1 + DBORDERY;
x2 = 2 * x2 + DBORDERX + 1;
y2 = 2 * y2 + DBORDERY + 1;
// TODO: LoadPreLighting, DRLG_LightSubtiles?
// TODO: LoadPreLighting, DRLG_LightSubtiles? (see SyncPedestal)
ObjChangeMap(x1, y1, x2, y2 /*, bool hasNewObjPiece*/);
// activate monsters
MonChangeMap();
Expand Down
89 changes: 86 additions & 3 deletions Source/lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ BYTE visionactive[MAXVISION];
/* The list of visions/views in the game. */
LightListStruct VisionList[MAXVISION];
/* The list of the indices of the active light-sources. */
BYTE lightactive[MAXLIGHTS + 1];
BYTE lightactive[MAXLIGHTS];
/* The list of light-sources in the game + one for temporary use. */
LightListStruct LightList[MAXLIGHTS + 1];
LightListStruct LightList[MAXLIGHTS];
/* The number of visions/views in the game. */
int numvision;
/* The number of light-sources in the game. */
Expand Down Expand Up @@ -467,7 +467,7 @@ static void RotateRadius(int* ox, int* oy, int* dx, int* dy, int* bx, int* by)
*oy = ny;
}

void DoLighting(unsigned lnum)
static void DoLighting(unsigned lnum)
{
LightListStruct* lis = &LightList[lnum];
int x, y, xoff, yoff;
Expand Down Expand Up @@ -637,6 +637,89 @@ static void DoUnLight(LightListStruct* lis)
lis->_lunflag = false;
}

BYTE *srcDark;
static bool LightPos(int x1, int y1, int radius_block)
{
assert(IN_DUNGEON_AREA(x1, y1));

// int yoff = 0;
// int xoff = 0;
// BYTE (&dist0)[MAX_TILE_DIST][MAX_TILE_DIST] = distMatrix[yoff][xoff];
// BYTE radius_block = dist0[abs(nYPos - y1)][abs(nXPos - x1)];
// BYTE v = srcDark[radius_block];
BYTE v = srcDark[radius_block];
if (v < dLight[x1][y1])
dLight[x1][y1] = v;

return !nBlockTable[dPiece[x1][y1]];
}

void TraceLightSource(int nXPos, int nYPos, int nRadius)
{
const int8_t* cr;
int i, x1, y1, limit;
int d, dx, dy, xinc, yinc;

srcDark = darkTable[nRadius];
BYTE v = srcDark[0];
if (v < dLight[nXPos][nYPos])
dLight[nXPos][nYPos] = v;

nRadius = 2 * (nRadius + 1) * 8 * 16;
static_assert(INT_MAX / (2 * 8 * 16) > MAX_LIGHT_RAD, "Light tracing overflows in TraceLightSource.");
static_assert(MAX_OFFSET == 8, "Light tracing shift must be adjusted in TraceLightSource.");
cr = &CrawlTable[CrawlNum[15]];
for (i = (BYTE)*cr; i > 0; i--) {
x1 = nXPos;
y1 = nYPos;
limit = nRadius;
dx = *++cr;
dy = *++cr;

// find out step size and direction on the y coordinate
xinc = dx < 0 ? -1 : 1;
yinc = dy < 0 ? -1 : 1;

dy = abs(dy);
dx = abs(dx);
if (dx >= dy) {
assert(dx != 0);

// multiply by 2 so we round up
dy *= 2;
d = 0;
do {
d += dy;
if (d >= dx) {
d -= 2 * dx; // multiply by 2 to support rounding
y1 += yinc;
limit -= 1 * 109; // 1 * 7;
}
x1 += xinc;
limit -= 2 * 8 * 16;
if (limit <= 0)
break;
} while (LightPos(x1, y1, (nRadius - limit) >> (1 + 4))); // * MAX_OFFSET / (2 * 8 * 16)
} else {
// multiply by 2 so we round up
dx *= 2;
d = 0;
do {
d += dx;
if (d >= dy) {
d -= 2 * dy; // multiply by 2 to support rounding
x1 += xinc;
limit -= 1 * 109; // 1 * 7;
}
y1 += yinc;
limit -= 2 * 8 * 16;
if (limit <= 0)
break;
} while (LightPos(x1, y1, (nRadius - limit) >> (1 + 4))); // * MAX_OFFSET / (2 * 8 * 16)
}
}
}

void DoUnVision(int nXPos, int nYPos, int nRadius)
{
int i, j, x1, y1, x2, y2;
Expand Down
6 changes: 3 additions & 3 deletions Source/lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ extern "C" {

extern BYTE visionactive[MAXVISION];
extern LightListStruct VisionList[MAXVISION];
extern BYTE lightactive[MAXLIGHTS + 1];
extern LightListStruct LightList[MAXLIGHTS + 1];
extern BYTE lightactive[MAXLIGHTS];
extern LightListStruct LightList[MAXLIGHTS];
extern int numlights;
extern int numvision;

Expand All @@ -31,7 +31,7 @@ extern int numvision;
#define COLOR_TRN_UNIQ MAXDARKNESS + 4
extern BYTE ColorTrns[NUM_COLOR_TRNS][NUM_COLORS];

void DoLighting(unsigned lnum);
void TraceLightSource(int x, int y, int r);
void DoUnVision(int nXPos, int nYPos, int nRadius);
void DoVision(int nXPos, int nYPos, int nRadius, bool local);
void InitLighting();
Expand Down
7 changes: 1 addition & 6 deletions Source/objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,7 @@ static int SetupObject(int type, int ox, int oy)
} else
#endif
{
assert(LightList[MAXLIGHTS]._lxoff == 0);
assert(LightList[MAXLIGHTS]._lyoff == 0);
LightList[MAXLIGHTS]._lradius = ods->oLightRadius;
LightList[MAXLIGHTS]._lx = ox + ods->oLightOffX;
LightList[MAXLIGHTS]._ly = oy + ods->oLightOffY;
DoLighting(MAXLIGHTS);
TraceLightSource(ox + ods->oLightOffX, oy + ods->oLightOffY, ods->oLightRadius);
}
}
if (ods->oDoorFlag != ODT_NONE) {
Expand Down
2 changes: 1 addition & 1 deletion defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static_assert(DMAXY % 2 == 0, "DRLG_L4 constructs the dungeon by mirroring a qua
#define MAXITEMS 127
#define ITEM_NONE 0xFF
#define MAXBELTITEMS 8
#define MAXLIGHTS 31
#define MAXLIGHTS 32
#define MAXMISSILES 125
#define MIS_MULTI 0xFF
#define MAXMONSTERS 200
Expand Down

0 comments on commit f6fe692

Please sign in to comment.