(Underdepths) Pass 5

This commit is contained in:
AlexKucera committed 2026-08-14 09:46:04 +02:00
1 parent 05e340dec2
commit b6d2a058e3
53 files changed
+5020

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
"""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))