Files
agentic-labs-challenge/underdepths/engine/combat.py
T
2026-08-14 09:46:04 +02:00

28 lines
1016 B
Python

"""Combat resolution: hit chance, armor reduction, critical hits."""
from __future__ import annotations
import random
from dataclasses import dataclass
from typing import Any
@dataclass
class AttackRoll:
hit: bool
crit: bool
damage: int
def roll_attack(attacker: Any, defender: Any, rng: random.Random = random) -> AttackRoll:
hit_chance = 0.7 + (attacker.accuracy - defender.evasion) * 0.05
hit_chance = max(0.1, min(0.95, hit_chance))
base = attacker.power + getattr(attacker, "weapon_power", 0)
# Player exposes effective armor via total_armor(); plain objects/enemies use .armor.
total_armor = getattr(defender, "total_armor", None)
armor = total_armor() if callable(total_armor) else getattr(defender, "armor", 0)
reduced = max(0, base - armor)
if rng.random() >= hit_chance:
return AttackRoll(hit=False, crit=False, damage=0)
crit = rng.random() < attacker.crit_chance
return AttackRoll(hit=True, crit=crit, damage=reduced * (2 if crit else 1))