diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f14484..7384559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Relation components were incorrectly loaded from pickled data as pointing in the wrong direction. + ## [5.5.0] - 2026-01-14 ### Changed diff --git a/tcod/ecs/registry.py b/tcod/ecs/registry.py index a2927f1..3891318 100644 --- a/tcod/ecs/registry.py +++ b/tcod/ecs/registry.py @@ -71,7 +71,7 @@ def _relations_lookup_from( for component_key, target_components in components.items(): for target in target_components: relations_lookup[(component_key, ...)].add(origin) - relations_lookup[(component_key, origin)].add(origin) + relations_lookup[(component_key, target)].add(origin) relations_lookup[(origin, component_key, None)].add(target) relations_lookup[(..., component_key, None)].add(target) diff --git a/tests/test_ecs.py b/tests/test_ecs.py index 95c334c..4887fdd 100644 --- a/tests/test_ecs.py +++ b/tests/test_ecs.py @@ -16,6 +16,28 @@ # ruff: noqa: D103 +@pytest.mark.parametrize("component_key", [int, ("distance", int)]) +@pytest.mark.parametrize("protocol", [2, 4, 5]) +def test_pickle_component_relation_targets(component_key: type[int] | tuple[str, type[int]], protocol: int) -> None: + world = tcod.ecs.Registry() + moon, planet = world["moon"], world["planet"] + distance = 10 + moon.relation_components[component_key][planet] = distance + moon.relation_tag["LandedOn"] = planet + assert set(world.Q.all_of(relations=[(component_key, planet)])) == {moon} + + restored: tcod.ecs.Registry = pickle.loads(pickle.dumps(world, protocol=protocol)) # noqa: S301 + moon, planet = restored["moon"], restored["planet"] + + assert moon.relation_components[component_key][planet] == distance + assert set(restored.Q.all_of(relations=[(component_key, planet)])) == {moon} + assert set(restored.Q.all_of(relations=[(component_key, moon)])) == set() + assert set(restored.Q.all_of(relations=[(component_key, ...)])) == {moon} + assert set(restored.Q.all_of(relations=[(moon, component_key, None)])) == {planet} + assert set(restored.Q.all_of(relations=[(..., component_key, None)])) == {planet} + assert set(restored.Q.all_of(relations=[("LandedOn", planet)])) == {moon} + + def test_world() -> None: world = tcod.ecs.Registry() with pytest.warns():