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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version

### Fixed

- Copying or pickling Perlin and Simplex noise no longer advances a shared random generator while preparing an unused wavelet tile.
- `tcod.image.Image.rotate90` updates the width and height of rectangular images, keeping NumPy array conversions consistent with the rotated pixels.
- Adding a root to an already resolved `tcod.path.Pathfinder` resets its predecessor only when its value improves the current distance. This prevents cyclic paths while preserving routes to cheaper roots.
- Lazy pathfinder queries reconsider previously visited goals after adding a root or lowering a root's distance.
Expand Down
13 changes: 11 additions & 2 deletions tcod/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,18 @@ def __getstate__(self) -> dict[str, Any]:
if self.dimensions < 4 and self.noise_c.waveletTileData == ffi.NULL: # noqa: PLR2004
# Trigger a side effect of wavelet, so that copies will be synced.
saved_algo = self.algorithm
saved_random = self.noise_c.rand
wavelet_random = saved_random
if saved_algo != tcod.constants.NOISE_WAVELET:
# Preparing an unused algorithm must not advance a shared RNG.
wavelet_random = ffi.gc(lib.TCOD_random_save(saved_random), lib.TCOD_random_delete)
Comment on lines +361 to +364

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do need this part explained to me, why this branch conditional? I would've expected the RNG to be copied for all permutations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition preserves the existing behavior when wavelet is the selected algorithm. Its first sample initializes the tile and advances the shared RNG; serialization currently performs that same lazy initialization. Copying the RNG there would also change the subsequent random sequence for existing wavelet users.

For Perlin/Simplex, sampling the existing field does not advance the RNG. The serialization workaround initializes a wavelet tile that those algorithms do not use, so I copy the generator only for that case. The original and restored objects still share the prepared tile if they later switch to wavelet.

I checked first sampling and pickling against native RNG draws for both generator algorithms. Wavelet retains the same advanced sequence before and after; Perlin/Simplex pickling now retains the sequence of ordinary sampling. The branch is intended to keep this backward-compatibility boundary, and could be removed if changing direct-wavelet serialization is preferred.

self.noise_c.rand = wavelet_random
self.algorithm = tcod.constants.NOISE_WAVELET
self.get_point()
self.algorithm = saved_algo
try:
self.get_point()
finally:
self.algorithm = saved_algo
self.noise_c.rand = saved_random

waveletTileData = None # noqa: N806
if self.noise_c.waveletTileData != ffi.NULL:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import pickle
from collections.abc import Callable

import numpy as np
import pytest
Expand Down Expand Up @@ -103,3 +104,20 @@ def test_noise_copy() -> None:

def test_noise_grid() -> None:
tcod.noise.grid((2, 2), scale=2) # Check int scale


@pytest.mark.parametrize("algorithm", [tcod.noise.Algorithm.PERLIN, tcod.noise.Algorithm.SIMPLEX])
@pytest.mark.parametrize("rng_algorithm", [tcod.random.MERSENNE_TWISTER, tcod.random.MULTIPLY_WITH_CARRY])
@pytest.mark.parametrize("snapshot", [copy.copy, copy.deepcopy, lambda n: pickle.loads(pickle.dumps(n))])
def test_noise_snapshot_preserves_shared_random(
algorithm: int, rng_algorithm: int, snapshot: Callable[[tcod.noise.Noise], tcod.noise.Noise]
) -> None:
rng = tcod.random.Random(rng_algorithm, seed=42)
noise = tcod.noise.Noise(2, algorithm=algorithm, seed=rng)
expected = copy.deepcopy(rng)
noise_copy = snapshot(noise)
assert [rng.randint(0, 100) for _ in range(8)] == [expected.randint(0, 100) for _ in range(8)]
assert noise.algorithm == algorithm
# The lazy wavelet tile remains synchronized if this algorithm is selected later.
noise.algorithm = noise_copy.algorithm = tcod.noise.Algorithm.WAVELET
np.testing.assert_array_equal(noise.sample_ogrid(np.ogrid[:3, :4]), noise_copy.sample_ogrid(np.ogrid[:3, :4]))
Loading