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.
34 lines
872 B
Python
34 lines
872 B
Python
"""Loot table system for mob drops."""
|
|
|
|
import random
|
|
from dataclasses import dataclass
|
|
|
|
from mudlib.thing import Thing
|
|
|
|
|
|
@dataclass
|
|
class LootEntry:
|
|
"""A single loot table entry."""
|
|
|
|
name: str
|
|
chance: float # 0.0-1.0
|
|
min_count: int = 1
|
|
max_count: int = 1
|
|
description: str = ""
|
|
|
|
|
|
def roll_loot(entries: list[LootEntry]) -> list[Thing]:
|
|
"""Roll a loot table and return the resulting Things."""
|
|
items: list[Thing] = []
|
|
for entry in entries:
|
|
if random.random() < entry.chance:
|
|
count = random.randint(entry.min_count, entry.max_count)
|
|
for _ in range(count):
|
|
items.append(
|
|
Thing(
|
|
name=entry.name,
|
|
description=entry.description,
|
|
portable=True,
|
|
)
|
|
)
|
|
return items
|