Implements load_zone() and load_zones() functions to parse zone definitions from TOML files. Wires zone loading into server startup to register all zones from content/zones/ directory. Updates player zone lookup to use the registry instead of hardcoded overworld check. Includes tavern.toml as first hand-built interior zone (8x6 bounded).
146 lines
3.4 KiB
Python
146 lines
3.4 KiB
Python
"""Tests for zone loading from TOML files."""
|
|
|
|
import pathlib
|
|
import tempfile
|
|
|
|
from mudlib.zones import load_zone, load_zones
|
|
|
|
|
|
def test_load_zone():
|
|
"""Load a zone from TOML file."""
|
|
# Create a temporary TOML file
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
|
|
f.write("""
|
|
name = "test_zone"
|
|
description = "a test zone"
|
|
width = 4
|
|
height = 3
|
|
toroidal = false
|
|
|
|
[terrain]
|
|
rows = [
|
|
"####",
|
|
"#..#",
|
|
"####",
|
|
]
|
|
|
|
[terrain.impassable]
|
|
tiles = ["#"]
|
|
""")
|
|
temp_path = pathlib.Path(f.name)
|
|
|
|
try:
|
|
zone = load_zone(temp_path)
|
|
|
|
assert zone.name == "test_zone"
|
|
assert zone.width == 4
|
|
assert zone.height == 3
|
|
assert zone.toroidal is False
|
|
assert len(zone.terrain) == 3
|
|
assert zone.terrain[0] == ["#", "#", "#", "#"]
|
|
assert zone.terrain[1] == ["#", ".", ".", "#"]
|
|
assert zone.terrain[2] == ["#", "#", "#", "#"]
|
|
assert zone.impassable == {"#"}
|
|
finally:
|
|
temp_path.unlink()
|
|
|
|
|
|
def test_load_zone_toroidal():
|
|
"""Load a toroidal zone."""
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
|
|
f.write("""
|
|
name = "toroidal_zone"
|
|
description = "a toroidal test zone"
|
|
width = 3
|
|
height = 2
|
|
toroidal = true
|
|
|
|
[terrain]
|
|
rows = [
|
|
"...",
|
|
"...",
|
|
]
|
|
""")
|
|
temp_path = pathlib.Path(f.name)
|
|
|
|
try:
|
|
zone = load_zone(temp_path)
|
|
|
|
assert zone.toroidal is True
|
|
# Default impassable set from Zone class
|
|
assert zone.impassable == {"^", "~"}
|
|
finally:
|
|
temp_path.unlink()
|
|
|
|
|
|
def test_load_zones_from_directory():
|
|
"""Load all zones from a directory."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
tmpdir_path = pathlib.Path(tmpdir)
|
|
|
|
# Create two zone files
|
|
zone1_path = tmpdir_path / "zone1.toml"
|
|
zone1_path.write_text("""
|
|
name = "zone1"
|
|
description = "first zone"
|
|
width = 2
|
|
height = 2
|
|
toroidal = false
|
|
|
|
[terrain]
|
|
rows = [
|
|
"..",
|
|
"..",
|
|
]
|
|
""")
|
|
|
|
zone2_path = tmpdir_path / "zone2.toml"
|
|
zone2_path.write_text("""
|
|
name = "zone2"
|
|
description = "second zone"
|
|
width = 3
|
|
height = 3
|
|
toroidal = true
|
|
|
|
[terrain]
|
|
rows = [
|
|
"###",
|
|
"#.#",
|
|
"###",
|
|
]
|
|
""")
|
|
|
|
# Create a non-TOML file that should be ignored
|
|
(tmpdir_path / "readme.txt").write_text("not a zone file")
|
|
|
|
zones = load_zones(tmpdir_path)
|
|
|
|
assert len(zones) == 2
|
|
assert "zone1" in zones
|
|
assert "zone2" in zones
|
|
assert zones["zone1"].name == "zone1"
|
|
assert zones["zone2"].name == "zone2"
|
|
|
|
|
|
def test_load_tavern_zone():
|
|
"""Load the actual tavern zone file."""
|
|
# This tests the real tavern.toml file in content/zones/
|
|
project_root = pathlib.Path(__file__).resolve().parents[1]
|
|
tavern_path = project_root / "content" / "zones" / "tavern.toml"
|
|
|
|
zone = load_zone(tavern_path)
|
|
|
|
assert zone.name == "tavern"
|
|
assert zone.width == 8
|
|
assert zone.height == 6
|
|
assert zone.toroidal is False
|
|
assert len(zone.terrain) == 6
|
|
assert zone.terrain[0] == ["#", "#", "#", "#", "#", "#", "#", "#"]
|
|
assert zone.terrain[5] == ["#", "#", "#", "#", ".", "#", "#", "#"]
|
|
assert zone.impassable == {"#"}
|
|
# Check that interior is passable
|
|
assert zone.is_passable(1, 1)
|
|
assert zone.is_passable(4, 3)
|
|
# Check that walls are impassable
|
|
assert not zone.is_passable(0, 0)
|
|
assert not zone.is_passable(7, 0)
|