Skip to content

Commit d1e182c

Browse files
committed
Reject out-of-bounds field-of-view origins
1 parent d9cf9e8 commit d1e182c

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- `tcod.map.compute_fov` now raises `ValueError` instead of warning when `pov` is outside the transparency array, as previously announced by the warning.
12+
913
## [21.2.2] - 2026-10-22
1014

1115
### Changed

‎tcod/map.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def compute_fov(
180180
`pov` is the point-of-view origin point. Areas are visible if they can
181181
be seen from this position. `pov` should be a 2D index matching the axes
182182
of the `transparency` array, and must be within the bounds of the
183-
`transparency` array.
183+
`transparency` array. A `ValueError` is raised if `pov` is out of bounds.
184184
185185
`radius` is the maximum view distance from `pov`. If this is zero then
186186
the maximum distance is used.
@@ -240,13 +240,9 @@ def compute_fov(
240240
msg = "The tcod.map.compute_fov function has changed. The `x` and `y` parameters should now be given as a single tuple."
241241
raise TypeError(msg)
242242
if not (0 <= pov[0] < transparency.shape[0] and 0 <= pov[1] < transparency.shape[1]):
243-
warnings.warn(
244-
f"Given pov index {pov!r} is outside the array of shape {transparency.shape!r}."
245-
"\nThis will raise an error in future versions.",
246-
RuntimeWarning,
247-
stacklevel=2,
248-
)
249-
map_buffer: NDArray[np.bool_] = np.zeros(
243+
msg = f"Given pov index {pov!r} is outside the array of shape {transparency.shape!r}."
244+
raise ValueError(msg)
245+
map_buffer: NDArray[np.bool_] = np.empty(
250246
transparency.shape,
251247
dtype=[("transparent", bool), ("walkable", bool), ("fov", bool)],
252248
)

‎tests/test_tcod.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ def test_tcod_bsp() -> None:
6161

6262

6363
@pytest.mark.parametrize("pov", [(-1, 0), (0, -1), (3, 0), (0, 5)])
64-
def test_compute_fov_out_of_bounds(pov: tuple[int, int], monkeypatch: pytest.MonkeyPatch) -> None:
64+
def test_compute_fov_out_of_bounds(pov: tuple[int, int]) -> None:
6565
transparency = np.ones((3, 5), dtype=bool)
66-
# Model an uninitialized allocation containing stale visible cells.
67-
monkeypatch.setattr(np, "empty", np.ones)
68-
with pytest.warns(RuntimeWarning, match="outside the array"):
69-
visible = tcod.map.compute_fov(transparency, pov)
66+
with pytest.raises(ValueError, match="outside the array"):
67+
tcod.map.compute_fov(transparency, pov)
68+
69+
70+
@pytest.mark.parametrize("pov", [(0, 0), (0, 4), (2, 0), (2, 4)])
71+
def test_compute_fov_at_boundary(pov: tuple[int, int]) -> None:
72+
transparency = np.ones((3, 5), dtype=bool)
73+
visible = tcod.map.compute_fov(transparency, pov)
7074
assert visible.shape == transparency.shape
71-
assert not visible.any()
75+
assert visible.all()
7276

7377

7478
@pytest.mark.filterwarnings("ignore:Use map.+ to check for this")

0 commit comments

Comments
 (0)