Skip to content

Commit efc46f3

Browse files
roumingAJenbo
authored andcommitted
lighting: make DoVision() more generic
This change does not have any functional impact. Instead, it separates dungeon-dependent data from the vision algorithm, making the `DoVision()` function suitable to be called from the test case, which comes next. Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
1 parent 63ee653 commit efc46f3

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

Source/lighting.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,13 @@ void DoUnVision(Point position, uint8_t radius)
230230
}
231231
}
232232

233-
void DoVision(Point position, uint8_t radius, MapExplorationType doAutomap, bool visible)
233+
void DoVision(Point position, uint8_t radius,
234+
tl::function_ref<void(Point)> markVisibleFn,
235+
tl::function_ref<void(Point)> markTransparentFn,
236+
tl::function_ref<bool(Point)> passesLightFn,
237+
tl::function_ref<bool(Point)> inBoundsFn)
234238
{
235-
DoVisionFlags(position, doAutomap, visible);
239+
markVisibleFn(position);
236240

237241
// Adjustment to a ray length to ensure all rays lie on an
238242
// accurate circle
@@ -251,7 +255,7 @@ void DoVision(Point position, uint8_t radius, MapExplorationType doAutomap, bool
251255
const auto &relRayPoint = VisionRays[j][k];
252256
// Calculate the next point on a ray in the quadrant
253257
Point rayPoint = position + relRayPoint * quadrant;
254-
if (!InDungeonBounds(rayPoint))
258+
if (!inBoundsFn(rayPoint))
255259
break;
256260

257261
// We've cast an approximated ray on an integer 2D
@@ -280,28 +284,46 @@ void DoVision(Point position, uint8_t radius, MapExplorationType doAutomap, bool
280284
Displacement adjacent1 = { -quadrant.deltaX, 0 };
281285
Displacement adjacent2 = { 0, -quadrant.deltaY };
282286

283-
bool passesLight = (TileAllowsLight(rayPoint + adjacent1) || TileAllowsLight(rayPoint + adjacent2));
287+
bool passesLight = (passesLightFn(rayPoint + adjacent1) || passesLightFn(rayPoint + adjacent2));
284288
if (!passesLight)
285289
// Diagonally adjacent tiles do not pass the
286290
// light further, we are done with this ray
287291
break;
288292
}
289-
DoVisionFlags(rayPoint, doAutomap, visible);
293+
markVisibleFn(rayPoint);
290294

291-
bool passesLight = TileAllowsLight(rayPoint);
295+
bool passesLight = passesLightFn(rayPoint);
292296
if (!passesLight)
293297
// Tile does not pass the light further, we are
294298
// done with this ray
295299
break;
296300

297-
int8_t trans = dTransVal[rayPoint.x][rayPoint.y];
298-
if (trans != 0)
299-
TransList[trans] = true;
301+
markTransparentFn(rayPoint);
300302
}
301303
}
302304
}
303305
}
304306

307+
void DoVision(Point position, uint8_t radius, MapExplorationType doAutomap, bool visible)
308+
{
309+
auto markVisibleFn = [doAutomap, visible](Point rayPoint) {
310+
DoVisionFlags(rayPoint, doAutomap, visible);
311+
};
312+
auto markTransparentFn = [](Point rayPoint) {
313+
int8_t trans = dTransVal[rayPoint.x][rayPoint.y];
314+
if (trans != 0)
315+
TransList[trans] = true;
316+
};
317+
auto passesLightFn = [](Point rayPoint) {
318+
return TileAllowsLight(rayPoint);
319+
};
320+
auto inBoundsFn = [](Point rayPoint) {
321+
return InDungeonBounds(rayPoint);
322+
};
323+
324+
DoVision(position, radius, markVisibleFn, markTransparentFn, passesLightFn, inBoundsFn);
325+
}
326+
305327
tl::expected<void, std::string> LoadTrns()
306328
{
307329
RETURN_IF_ERROR(LoadFileInMemWithStatus("plrgfx\\infra.trn", InfravisionTable));

Source/lighting.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdint>
1010

1111
#include <expected.hpp>
12+
#include <function_ref.hpp>
1213

1314
#include "automap.h"
1415
#include "engine/displacement.hpp"
@@ -62,6 +63,11 @@ extern bool UpdateLighting;
6263
void DoUnLight(Point position, uint8_t radius);
6364
void DoLighting(Point position, uint8_t radius, DisplacementOf<int8_t> offset);
6465
void DoUnVision(Point position, uint8_t radius);
66+
void DoVision(Point position, uint8_t radius,
67+
tl::function_ref<void(Point)> markVisibleFn,
68+
tl::function_ref<void(Point)> markTransparentFn,
69+
tl::function_ref<bool(Point)> passesLightFn,
70+
tl::function_ref<bool(Point)> inBoundsFn);
6571
void DoVision(Point position, uint8_t radius, MapExplorationType doAutomap, bool visible);
6672
tl::expected<void, std::string> LoadTrns();
6773
void MakeLightTable();

0 commit comments

Comments
 (0)