fix(combat): block on projected fight damage, and three lethal-search defects
Mining the 37 run files showed 81% of runs (30/37) die in Act 1, and 43% to an
Act 1 boss. THE_KIN_BOSS alone killed 9. Every one of those fights ran 5-10
turns and cost 44-80 HP -- about 10-13 a turn, with block cards in hand the
whole time. Four defects, fixed here together because they were found and
verified as one combat-correctness pass.
1. NO DEFENCE POLICY (the big one, found by mining the data)
facts classes a hit of <=15% of max HP as THREAT_CHIP. At 80 max HP that is
12, exactly what the boss deals, and _fallback_combat only blocked for HEAVY
or worse while HP was HEALTHY (>60%). So at 74/80 HP the bot attacked through
the boss's main attack and only started blocking below 48 HP.
_jev_combat also asked a should_defend Noul on every combat turn and never
read it -- grep -rn should_defend returned one line, the one creating it.
Defence therefore fell to choice("Which single play best advances winning
this fight?"), which is damage-biased: on the real Kin state Jev answered
Bash at 0.42 confidence, below the 0.45 gate, so it fell through to the
fallback, which also chose damage. Both paths agreed on the wrong answer.
Blocking is arithmetic, so it is now decided in code before Jev is asked,
using turns_to_kill, projected_incoming, affordable_loss and must_block /
block_urgent. Measured against all 600 real combat captures, the rule changes
8 of 68 in-play turns (11.8%) and stays silent on short fights and when
nothing is incoming.
2. ENEMY BLOCK COUNTED TWICE IN THE LETHAL SEARCH
`total - max(0, enemy.block) >= enemy.effective_hp` subtracts block a second
time, because effective_hp is already hp + block. A 10 hp / 5 block enemy
against 18 raw damage read as "not lethal" and real kills were discarded.
3. THE LETHAL EXECUTOR IGNORED PLAYER STATUSES
`_lethal_line(f.playable, f.energy, [], enemy)` passed an empty status list,
so facts reported lethal_available: true while the code meant to execute the
kill found nothing. CombatFacts.player_status is now passed through.
4. RELIC_SELECT ASKED good_relicN AND READ relicN
Every answer missed, so best_by_noul returned (None, 0.0) for every state and
the path could only ever take the rarest relic.
Tests: 50 in test_facts.py and 131 in test_brain.py, with a regression case for
each -- a blocked enemy, a Strength-carrying player, a relic offer whose
highest-rated relic is deliberately the common one so the rarity fallback cannot
pass by accident, and the Kin turn itself.
This commit is contained in:
parent
f59aa87fa3
commit
471c77b353
4 changed files with 474 additions and 35 deletions
189
facts.py
189
facts.py
|
|
@ -156,6 +156,7 @@ class EnemyFact:
|
|||
intent_kinds: list[str] = field(default_factory=list)
|
||||
intent_text: list[str] = field(default_factory=list)
|
||||
statuses: list[str] = field(default_factory=list)
|
||||
status_amounts: list[dict] = field(default_factory=list)
|
||||
is_minion: bool = False
|
||||
|
||||
@property
|
||||
|
|
@ -173,7 +174,10 @@ class EnemyFact:
|
|||
"hp_state": _hp_bucket(self.hp_pct, self.hp),
|
||||
"incoming": self.incoming_damage,
|
||||
"intends": ", ".join(self.intent_text) or "unknown",
|
||||
"statuses": self.statuses or "none",
|
||||
# Names AND amounts: an enemy at Strength 6 and Strength 1 used to
|
||||
# look identical to Jev. Amounts are context, not a comparison, so
|
||||
# no arithmetic crosses the boundary.
|
||||
"statuses": self.status_amounts or "none",
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -221,6 +225,11 @@ def enemy_facts(enemy: dict) -> EnemyFact:
|
|||
intents = enemy.get("intents") or []
|
||||
|
||||
names = [str(s.get("name")) for s in statuses if isinstance(s, dict) and s.get("name")]
|
||||
amounts = [
|
||||
{"name": str(s.get("name")), "amount": _as_int(s.get("amount"))}
|
||||
for s in statuses
|
||||
if isinstance(s, dict) and s.get("name")
|
||||
]
|
||||
|
||||
return EnemyFact(
|
||||
entity_id=str(enemy.get("entity_id") or "?"),
|
||||
|
|
@ -236,6 +245,7 @@ def enemy_facts(enemy: dict) -> EnemyFact:
|
|||
if isinstance(i, dict)
|
||||
],
|
||||
statuses=names,
|
||||
status_amounts=amounts,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -262,9 +272,11 @@ class CombatFacts:
|
|||
hand: list[dict]
|
||||
playable: list[dict]
|
||||
potions: list[dict]
|
||||
player_status: list
|
||||
lethal_available: bool
|
||||
killable: list[str]
|
||||
deck_counts: dict
|
||||
deck_summary: dict
|
||||
draw_pile_count: int
|
||||
discard_pile_count: int
|
||||
exhaust_pile_count: int
|
||||
|
|
@ -321,6 +333,90 @@ class CombatFacts:
|
|||
"""
|
||||
return self.block + self.max_block_available >= self.incoming_damage
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Fight-length projection.
|
||||
#
|
||||
# The old model asked "is THIS turn's hit big?" (see `threat`). That is
|
||||
# myopic and it lost 9 runs to THE_KIN_BOSS: at 80 max HP a hit of <=12 is
|
||||
# classed CHIP, so the bot attacked through the boss's main attack, took
|
||||
# ~10-13 a turn, and died in 5-10 turns having dealt 44-80 damage to
|
||||
# itself. The quantity that decides whether to block is the TOTAL damage
|
||||
# the rest of the fight will cost, not one turn of it.
|
||||
# ---------------------------------------------------------------------
|
||||
|
||||
@property
|
||||
def attack_power(self) -> int:
|
||||
"""Best single-turn damage the hand can produce, ignoring targets."""
|
||||
energy = self.energy
|
||||
total = 0
|
||||
for card in sorted(self.playable, key=lambda x: -parse_card_damage(x).raw_total):
|
||||
dmg = parse_card_damage(card).raw_total
|
||||
if dmg <= 0:
|
||||
continue
|
||||
cost = _as_int(card.get("cost"))
|
||||
if cost <= energy:
|
||||
total += dmg
|
||||
energy -= cost
|
||||
return total
|
||||
|
||||
@property
|
||||
def turns_to_kill(self) -> int:
|
||||
"""
|
||||
Turns this fight still needs, from this turn's reachable damage.
|
||||
|
||||
Deliberately pessimistic: it assumes every later turn looks like this
|
||||
one. If the hand is all block, the estimate is huge, which correctly
|
||||
pushes toward "this fight will grind me down".
|
||||
"""
|
||||
power = self.attack_power
|
||||
if power <= 0:
|
||||
return 20
|
||||
return max(1, -(-self.total_enemy_hp // power))
|
||||
|
||||
@property
|
||||
def projected_incoming(self) -> int:
|
||||
"""Total damage the rest of this fight is likely to deal us."""
|
||||
return self.turns_to_kill * self.incoming_damage
|
||||
|
||||
@property
|
||||
def affordable_loss(self) -> int:
|
||||
"""
|
||||
HP we can spend on this fight and still enter the next one healthy.
|
||||
|
||||
HP is a resource (it carries across the act), so we do not want to
|
||||
leave a fight at 1 HP. Keep a floor of 30% of max HP.
|
||||
"""
|
||||
return max(0, self.hp - int(self.max_hp * 0.30))
|
||||
|
||||
@property
|
||||
def must_block(self) -> bool:
|
||||
"""
|
||||
True when the rest of this fight will cost more HP than we can spare.
|
||||
|
||||
This is the decision the bot was missing. It fires on the Kin turn
|
||||
(74/80 HP, 12 incoming, ~12 turns to kill -> 144 projected vs 50
|
||||
affordable) and stays silent on a trivial fight (15 HP enemy, 6
|
||||
incoming, 2 turns -> 12 projected vs 50 affordable).
|
||||
"""
|
||||
if self.incoming_damage <= 0:
|
||||
return False
|
||||
return self.projected_incoming > self.affordable_loss
|
||||
|
||||
@property
|
||||
def block_urgent(self) -> bool:
|
||||
"""
|
||||
Block now even at the cost of tempo: this turn alone is dangerous.
|
||||
Separate from `must_block` because it also covers the case where the
|
||||
current hit is lethal-ish regardless of how long the fight lasts.
|
||||
"""
|
||||
if self.incoming_damage <= 0:
|
||||
return False
|
||||
return (
|
||||
self.threat in (THREAT_SEVERE, THREAT_LETHAL)
|
||||
or self.hp_bucket in (HP_WOUNDED, HP_CRITICAL)
|
||||
or self.must_block
|
||||
)
|
||||
|
||||
def to_state(self) -> dict:
|
||||
"""
|
||||
The compact, semantic state handed to Jev.
|
||||
|
|
@ -334,7 +430,18 @@ class CombatFacts:
|
|||
"your_turn": self.in_play_phase,
|
||||
"energy": self.energy,
|
||||
"your_health": self.hp_bucket,
|
||||
"your_statuses": [
|
||||
{"name": str(s.get("name")), "amount": _as_int(s.get("amount"))}
|
||||
for s in self.player_status
|
||||
if isinstance(s, dict) and s.get("name")
|
||||
] or "none",
|
||||
"incoming_threat": self.threat,
|
||||
# The fight-length view. `threat` alone is myopic: at 80 max HP
|
||||
# a 12-damage hit reads as "chip" even when the fight will run
|
||||
# 10 more turns and kill us. These expose the accumulated view
|
||||
# without handing Jev any arithmetic to do.
|
||||
"fight_is_grinding": self.must_block,
|
||||
"this_turn_is_dangerous": self.block_urgent,
|
||||
"lethal_available": self.lethal_available,
|
||||
"can_survive_with_cards": self.survives_with_cards,
|
||||
"enemies_you_can_kill_now": self.killable or "none",
|
||||
|
|
@ -435,9 +542,11 @@ def combat_facts(observation: dict) -> CombatFacts:
|
|||
hand=hand,
|
||||
playable=affordable,
|
||||
potions=[p for p in (player.get("potions") or []) if isinstance(p, dict)],
|
||||
player_status=player_status,
|
||||
lethal_available=lethal,
|
||||
killable=killable,
|
||||
deck_counts=deck_counts,
|
||||
deck_summary=deck_summary(player),
|
||||
draw_pile_count=_as_int(player.get("draw_pile_count")),
|
||||
discard_pile_count=_as_int(player.get("discard_pile_count")),
|
||||
exhaust_pile_count=_as_int(player.get("exhaust_pile_count")),
|
||||
|
|
@ -445,7 +554,14 @@ def combat_facts(observation: dict) -> CombatFacts:
|
|||
|
||||
|
||||
def _subset_damage(playable: list[dict], energy: int, attacker_status: list, enemy: EnemyFact) -> int:
|
||||
"""Exact max damage over subsets, honouring energy and enemy block."""
|
||||
"""
|
||||
Exact max RAW damage over subsets, honouring energy and attacker statuses.
|
||||
|
||||
Raw means before enemy block: compare the result against `enemy.effective_hp`
|
||||
(hp + block) so the block is subtracted exactly once. Subtracting it here and
|
||||
comparing against `effective_hp` required hp + 2*block, which silently
|
||||
discarded real lethal lines whenever an enemy played Defend.
|
||||
"""
|
||||
best = 0
|
||||
n = min(len(playable), 12)
|
||||
for r in range(1, n + 1):
|
||||
|
|
@ -468,7 +584,7 @@ def _subset_damage(playable: list[dict], energy: int, attacker_status: list, ene
|
|||
total += card_total
|
||||
if total > best:
|
||||
best = total
|
||||
return max(0, best - max(0, enemy.block))
|
||||
return best
|
||||
|
||||
|
||||
def enemy_status_names(enemy: EnemyFact) -> list:
|
||||
|
|
@ -476,6 +592,73 @@ def enemy_status_names(enemy: EnemyFact) -> list:
|
|||
return [{"name": n, "amount": 1} for n in enemy.statuses]
|
||||
|
||||
|
||||
def deck_summary(player: dict) -> dict:
|
||||
"""
|
||||
Stable aggregates over ALL FOUR PILES, for macro decisions.
|
||||
|
||||
The reward, shop and map states do NOT expose the deck, so this travels with
|
||||
the composition snapshot taken during combat. It is deliberately a
|
||||
*supplement* to the name->count map, never a replacement: card identities
|
||||
are the signal for synergy, redundancy and upgrades, and only counts can
|
||||
carry them. These aggregates are the part that is arithmetic, so it is
|
||||
computed here and never asked of the model.
|
||||
|
||||
Every field is computed over the whole deck and is stable across snapshots
|
||||
of the same deck. There is deliberately no cost aggregate: the state does
|
||||
not expose a card's BASE cost, and a temporary in-combat cost modifier
|
||||
applies to the copy in hand. Measured over 600 captures, one Strike read
|
||||
`cost: "0"` in hand while the same Strike read `cost: "1"` in the draw pile,
|
||||
so an average cost would differ between two snapshots of an identical deck.
|
||||
"""
|
||||
names: list[str] = []
|
||||
attacks = blocks = other = upgraded = 0
|
||||
for pile in ("hand", "draw_pile", "discard_pile", "exhaust_pile"):
|
||||
for card in player.get(pile) or []:
|
||||
if not isinstance(card, dict):
|
||||
continue
|
||||
name = str(card.get("name") or "?")
|
||||
names.append(name)
|
||||
if name.endswith("+"):
|
||||
upgraded += 1
|
||||
text = card.get("description") or ""
|
||||
if DAMAGE_RE.search(text):
|
||||
attacks += 1
|
||||
elif BLOCK_RE.search(text):
|
||||
blocks += 1
|
||||
else:
|
||||
other += 1
|
||||
|
||||
if not names:
|
||||
return {}
|
||||
|
||||
return {
|
||||
"size": len(names),
|
||||
"attacks": attacks,
|
||||
"block_cards": blocks,
|
||||
"other_cards": other,
|
||||
"upgraded": upgraded,
|
||||
}
|
||||
|
||||
|
||||
def deck_context(deck: Any) -> Any:
|
||||
"""
|
||||
What a macro question receives as deck context: the full name->count map
|
||||
(card identities, which carry the synergy and redundancy signal) plus the
|
||||
stable aggregates. Tolerates a legacy flat name->count snapshot.
|
||||
"""
|
||||
if not deck:
|
||||
return "unknown"
|
||||
if isinstance(deck, dict) and "counts" in deck:
|
||||
counts = deck.get("counts") or {}
|
||||
if not counts:
|
||||
return "unknown"
|
||||
return {"cards": counts, **({"summary": deck["summary"]}
|
||||
if deck.get("summary") else {})}
|
||||
if isinstance(deck, dict):
|
||||
return {"cards": deck}
|
||||
return deck
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# CLI
|
||||
# --------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue