Files
2026-08-16 19:10:31 +02:00

234 lines
6.2 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
import random
from data.constants import (
BOW_RANGE,
DEEP_GEAR_FLOOR,
FINAL_FLOOR,
FINAL_FLOOR_POTIONS,
FIREBALL_DAMAGE,
FIREBALL_RADIUS,
HEALING_POTION_HP,
INVISIBILITY_POTION_TURNS,
ITEMS_PER_FLOOR,
PLATE_EVASION_PENALTY,
STRENGTH_POTION_BONUS,
STRENGTH_POTION_TURNS,
WAR_HAMMER_CRIT_PENALTY,
)
from entities.item import Item
@dataclass(frozen=True, slots=True)
class ItemTemplate:
key: str
name: str
category: str
power: int = 0
armor_bonus: int = 0
evasion_mod: int = 0
crit_mod: float = 0.0
ranged_range: int = 0
enables_running: bool = False
heal_amount: int = 0
effect: str = ""
effect_power: int = 0
effect_turns: int = 0
aoe_damage: int = 0
aoe_radius: int = 0
min_floor: int = 0
max_floor: int = 0
guaranteed_only: bool = False
CATALOG: dict[str, ItemTemplate] = {
template.key: template
for template in (
ItemTemplate(
key="rusty_dagger",
name="Rusty Dagger",
category="weapon",
power=2,
min_floor=0,
max_floor=2,
),
ItemTemplate(
key="short_sword",
name="Short Sword",
category="weapon",
power=4,
min_floor=1,
max_floor=4,
),
ItemTemplate(
key="hunters_bow",
name="Hunter's Bow",
category="weapon",
power=4,
ranged_range=BOW_RANGE,
min_floor=2,
max_floor=8,
),
ItemTemplate(
key="battle_axe",
name="Battle Axe",
category="weapon",
power=7,
min_floor=4,
max_floor=8,
),
ItemTemplate(
key="war_hammer",
name="War Hammer",
category="weapon",
power=10,
crit_mod=WAR_HAMMER_CRIT_PENALTY,
min_floor=6,
max_floor=8,
),
ItemTemplate(
key="leather_armor",
name="Leather Armor",
category="armor",
armor_bonus=2,
min_floor=0,
max_floor=3,
),
ItemTemplate(
key="chain_mail",
name="Chain Mail",
category="armor",
armor_bonus=4,
min_floor=3,
max_floor=6,
),
ItemTemplate(
key="plate_armor",
name="Plate Armor",
category="armor",
armor_bonus=7,
evasion_mod=PLATE_EVASION_PENALTY,
min_floor=5,
max_floor=8,
),
ItemTemplate(
key="healing_potion",
name="Healing Potion",
category="potion",
effect="heal",
heal_amount=HEALING_POTION_HP,
min_floor=0,
max_floor=8,
),
ItemTemplate(
key="strength_potion",
name="Strength Potion",
category="potion",
effect="strength",
effect_power=STRENGTH_POTION_BONUS,
effect_turns=STRENGTH_POTION_TURNS,
min_floor=0,
max_floor=8,
),
ItemTemplate(
key="invisibility_potion",
name="Invisibility Potion",
category="potion",
effect="invisible",
effect_turns=INVISIBILITY_POTION_TURNS,
min_floor=0,
max_floor=8,
),
ItemTemplate(
key="fireball_scroll",
name="Scroll of Fireball",
category="scroll",
effect="fireball",
aoe_damage=FIREBALL_DAMAGE,
aoe_radius=FIREBALL_RADIUS,
min_floor=0,
max_floor=8,
),
ItemTemplate(
key="teleport_scroll",
name="Scroll of Teleport",
category="scroll",
effect="teleport",
min_floor=0,
max_floor=8,
),
ItemTemplate(
key="mapping_scroll",
name="Scroll of Mapping",
category="scroll",
effect="mapping",
min_floor=0,
max_floor=8,
),
ItemTemplate(
key="shoes_of_hermes",
name="Shoes of Hermes",
category="boots",
enables_running=True,
min_floor=1,
max_floor=1,
guaranteed_only=True,
),
)
}
def spawn_item(key: str, x: int, y: int) -> Item:
template = CATALOG[key]
return Item(
x=x,
y=y,
key=template.key,
name=template.name,
sprite_key=f"item_{template.key}",
category=template.category,
power=template.power,
armor_bonus=template.armor_bonus,
evasion_mod=template.evasion_mod,
crit_mod=template.crit_mod,
ranged_range=template.ranged_range,
enables_running=template.enables_running,
heal_amount=template.heal_amount,
effect=template.effect,
effect_power=template.effect_power,
effect_turns=template.effect_turns,
aoe_damage=template.aoe_damage,
aoe_radius=template.aoe_radius,
)
def _random_pool(floor_index: int) -> list[str]:
return [
template.key
for template in CATALOG.values()
if not template.guaranteed_only
and template.min_floor <= floor_index <= template.max_floor
and not (template.key == "hunters_bow" and floor_index <= 2)
]
def roll_floor_items(floor_index: int, rng: random.Random) -> list[str]:
"""Item keys for a floor; guaranteed items replace ordinary rolls."""
if floor_index == FINAL_FLOOR:
return ["healing_potion"] * FINAL_FLOOR_POTIONS
low, high = ITEMS_PER_FLOOR[floor_index]
keys: list[str] = []
if floor_index == 1:
keys.append("shoes_of_hermes")
elif floor_index == 2:
keys.append("hunters_bow")
elif floor_index == DEEP_GEAR_FLOOR:
keys.append("battle_axe") # guaranteed answer to the dragon's armor 8
pool = _random_pool(floor_index)
while len(keys) < rng.randint(low, high):
keys.append(rng.choice(pool))
return keys