Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed
- Symmetric shadowcasting rounds negative sector edges correctly, preserving visibility when a dungeon is rotated.
5 changes: 2 additions & 3 deletions src/libtcod-fov/fov_symmetric_shadowcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

Based on: https://www.albertford.com/shadowcasting/
*/
#include <float.h>
#include <math.h>
#include <stdbool.h>

Expand Down Expand Up @@ -83,11 +82,11 @@ static float slope(int row_depth, int column) { return (2.0f * column - 1.0f) /
/**
Round half numbers towards infinity.
*/
static int round_half_up(float n) { return (int)roundf(n * (1 + FLT_EPSILON)); }
static int round_half_up(float n) { return (int)floorf(n + 0.5f); }
/**
Round half numbers towards negative infinity.
*/
static int round_half_down(float n) { return (int)roundf(n * (1 - FLT_EPSILON)); }
static int round_half_down(float n) { return (int)ceilf(n - 0.5f); }
/**
Scan a row and recursively scan all of its children.

Expand Down
50 changes: 50 additions & 0 deletions tests/test_fov_symmetric.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <libtcod-fov/fov.h>

#include <array>
#include <catch2/catch_test_macros.hpp>
#include <string_view>
#include <utility>

namespace {
std::pair<int, int> rotate(int x, int y, int turns) {
for (int i = 0; i < turns; ++i) {
const int old_x = x;
x = y;
y = 6 - old_x;
}
return {x, y};
}
} // namespace

TEST_CASE("symmetric shadowcasting rounds negative sector edges", "[fov][symmetric]") {
const std::array<std::string_view, 7> dungeon = {
"#######", "###...#", "###.#.#", "#.....#", "#.#.#.#", "#.#..##", "#######"};
// Exact rational-slope reference, with the origin at (3, 1).
const std::array<std::string_view, 7> visible = {
"0011111", "0011111", "0011111", "0011100", "0011100", "0011000", "0011100"};
for (int turns = 0; turns < 4; ++turns) {
for (bool light_walls : {false, true}) {
CAPTURE(turns, light_walls);
TCODFOV_Map* map = TCODFOV_map_new(7, 7);
REQUIRE(map != nullptr);
for (int y = 0; y < 7; ++y) {
for (int x = 0; x < 7; ++x) {
const auto [rx, ry] = rotate(x, y, turns);
const bool floor = dungeon[y][x] == '.';
TCODFOV_map_set_properties(map, rx, ry, floor, floor);
}
}
const auto [pov_x, pov_y] = rotate(3, 1, turns);
REQUIRE(TCODFOV_map_compute_fov(map, pov_x, pov_y, 0, light_walls, TCODFOV_SYMMETRIC_SHADOWCAST) == TCODFOV_E_OK);
for (int y = 0; y < 7; ++y) {
for (int x = 0; x < 7; ++x) {
CAPTURE(x, y);
const auto [rx, ry] = rotate(x, y, turns);
const bool expected = visible[y][x] == '1' && (light_walls || dungeon[y][x] == '.');
CHECK(TCODFOV_map_is_in_fov(map, rx, ry) == expected);
}
}
TCODFOV_map_delete(map);
}
}
}
Loading