mud/tests/test_loot.py
Jared Miller 0fbd63a1f7
Add loot table system with LootEntry and roll_loot
LootEntry defines probabilistic item drops with min/max counts.
roll_loot takes a loot table and returns Thing instances.
MobTemplate now has loot field, parsed from TOML [[loot]] sections.
2026-02-14 10:02:38 -05:00

102 lines
3 KiB
Python

"""Tests for loot table system."""
from mudlib.loot import LootEntry, roll_loot
from mudlib.mobs import MobTemplate, load_mob_template
from mudlib.thing import Thing
class TestLootEntry:
def test_loot_entry_fields(self):
entry = LootEntry(name="gold coin", chance=0.5, min_count=1, max_count=3)
assert entry.name == "gold coin"
assert entry.chance == 0.5
class TestRollLoot:
def test_empty_table_returns_empty(self):
assert roll_loot([]) == []
def test_guaranteed_drop(self):
"""chance=1.0 always drops."""
entry = LootEntry(name="gold coin", chance=1.0)
items = roll_loot([entry])
assert len(items) == 1
assert items[0].name == "gold coin"
def test_zero_chance_never_drops(self):
"""chance=0.0 never drops."""
entry = LootEntry(name="rare gem", chance=0.0)
items = roll_loot([entry])
assert len(items) == 0
def test_count_range(self):
"""min_count/max_count controls number of items."""
entry = LootEntry(name="coin", chance=1.0, min_count=3, max_count=3)
items = roll_loot([entry])
assert len(items) == 3
assert all(item.name == "coin" for item in items)
def test_items_are_portable_things(self):
entry = LootEntry(name="sword", chance=1.0, description="a rusty sword")
items = roll_loot([entry])
assert isinstance(items[0], Thing)
assert items[0].portable is True
assert items[0].description == "a rusty sword"
def test_multiple_entries(self):
entries = [
LootEntry(name="coin", chance=1.0, min_count=2, max_count=2),
LootEntry(name="gem", chance=1.0),
]
items = roll_loot(entries)
assert len(items) == 3 # 2 coins + 1 gem
class TestMobTemplateLoot:
def test_template_default_empty_loot(self):
t = MobTemplate(
name="rat", description="a rat", pl=10, stamina=10, max_stamina=10
)
assert t.loot == []
def test_load_template_with_loot(self, tmp_path):
toml_content = """
name = "goblin"
description = "a goblin"
pl = 50.0
stamina = 40.0
max_stamina = 40.0
moves = ["punch right"]
[[loot]]
name = "crude club"
chance = 0.8
description = "a crude wooden club"
[[loot]]
name = "gold coin"
chance = 0.5
min_count = 1
max_count = 3
"""
f = tmp_path / "goblin.toml"
f.write_text(toml_content)
template = load_mob_template(f)
assert len(template.loot) == 2
assert template.loot[0].name == "crude club"
assert template.loot[0].chance == 0.8
assert template.loot[1].min_count == 1
assert template.loot[1].max_count == 3
def test_load_template_without_loot(self, tmp_path):
toml_content = """
name = "rat"
description = "a rat"
pl = 10.0
stamina = 10.0
max_stamina = 10.0
"""
f = tmp_path / "rat.toml"
f.write_text(toml_content)
template = load_mob_template(f)
assert template.loot == []