diff --git a/src/commander_ai_lab/sim/rules.py b/src/commander_ai_lab/sim/rules.py index b26641b..8b57753 100644 --- a/src/commander_ai_lab/sim/rules.py +++ b/src/commander_ai_lab/sim/rules.py @@ -4,12 +4,15 @@ Commander AI Lab — Simulator Rules & Helpers AI weights, card enrichment heuristics, and scoring functions. Ported from mtg-commander-lan JavaScript (AI_DEFAULT_WEIGHTS, enrichCardForSim, headlessScoreCard, headlessGetPower/Toughness/HasKw). + +N-player ready: score_card() accepts num_opponents so keyword weights +scale with pod size (e.g. Propaganda is worth more vs 3 opponents). """ from __future__ import annotations +import hashlib import re -import random from typing import Optional from commander_ai_lab.sim.models import Card @@ -130,9 +133,12 @@ def enrich_card(card: Card) -> Card: _apply_oracle_flags(card) return card - # Default: treat as creature with random CMC + # Default: treat as creature with deterministic CMC based on + # card name hash (fixes #32 — no more random.randint jitter + # between games for the same card). if not card.type_line or len(card.type_line) < 3: - estimated_cmc = random.randint(2, 6) + name_hash = int(hashlib.md5(name_lower.encode()).hexdigest(), 16) + estimated_cmc = (name_hash % 5) + 2 # 2-6 inclusive estimated_pow = max(1, estimated_cmc - 1) card.type_line = "Creature" card.cmc = estimated_cmc @@ -181,15 +187,46 @@ def _apply_oracle_flags(card: Card) -> None: card.is_ramp = True -def score_card(card: Card, weights: Optional[dict] = None) -> float: +# ── Multi-opponent scaling factors ──────────────────────────── +# Keywords / effects that become more valuable in larger pods. +# Each entry maps a weight key → the extra bonus *per additional +# opponent* beyond 1. 1v1 uses base weights; 4-player FFA adds +# 3× the bonus for each matching keyword. +_MULTI_OPP_SCALING: dict[str, float] = { + # Group-slug / pillowfort effects punish each opponent + "kw_menace": 0.5, # harder to block with more players + "kw_flying": 0.3, # evasion matters more in FFA + "kw_hexproof": 0.5, # more removal flying around + "kw_indestructible": 0.5, + "kw_ward": 0.4, + # Multiplayer-relevant triggered abilities + "trig_etb": 0.3, + "trig_dies": 0.5, # more creatures dying in FFA + # Spells that scale with opponents + "spell_destroy": 0.5, # removal more precious + "spell_draw": 0.3, # card advantage matters more + "spell_counter": 0.5, # counters protect from more threats +} + + +def score_card( + card: Card, + weights: Optional[dict] = None, + num_opponents: int = 1, +) -> float: """ Score a card for AI play priority. - Ported from headlessScoreCard() in app.js. + + Ported from headlessScoreCard() in app.js. The optional + *num_opponents* parameter (default 1 for 1v1) scales keywords + that become more valuable in larger pods. """ w = weights or AI_DEFAULT_WEIGHTS + extra_opps = max(0, num_opponents - 1) # 0 for 1v1 cmc = card.cmc or 0 score = 10.0 - cmc * w.get("card_baseCmcPenalty", 1.0) + # ── Power / toughness bonus ── if card.pt: parts = card.pt.split("/") try: @@ -202,22 +239,83 @@ def score_card(card: Card, weights: Optional[dict] = None) -> float: t = 0 score += (p + t) * w.get("card_ptBonus", 1.0) + # ── Keyword / oracle-text scoring ── text = (card.oracle_text or "").lower() - if "flying" in text: - score += w.get("kw_flying", 2.5) - if "deathtouch" in text: - score += w.get("kw_deathtouch", 2.5) - if "haste" in text: - score += w.get("kw_haste", 2.5) - if "trample" in text: - score += w.get("kw_trample", 1.5) - if "lifelink" in text: - score += w.get("kw_lifelink", 1.5) + kw_list = [k.lower() for k in (card.keywords or [])] + def _kw_bonus(keyword: str, weight_key: str, default: float) -> float: + """Return base weight + per-extra-opponent scaling.""" + base = w.get(weight_key, default) + multi = _MULTI_OPP_SCALING.get(weight_key, 0.0) * extra_opps + return base + multi + + if "flying" in text or "flying" in kw_list: + score += _kw_bonus("flying", "kw_flying", 2.5) + if "deathtouch" in text or "deathtouch" in kw_list: + score += _kw_bonus("deathtouch", "kw_deathtouch", 2.5) + if "haste" in text or "haste" in kw_list: + score += _kw_bonus("haste", "kw_haste", 2.5) + if "trample" in text or "trample" in kw_list: + score += _kw_bonus("trample", "kw_trample", 1.5) + if "lifelink" in text or "lifelink" in kw_list: + score += _kw_bonus("lifelink", "kw_lifelink", 1.5) + if "double strike" in text or "double strike" in kw_list: + score += _kw_bonus("double strike", "kw_doubleStrike", 3.0) + if "first strike" in text or "first strike" in kw_list: + score += _kw_bonus("first strike", "kw_firstStrike", 1.5) + if "hexproof" in text or "hexproof" in kw_list: + score += _kw_bonus("hexproof", "kw_hexproof", 2.0) + if "indestructible" in text or "indestructible" in kw_list: + score += _kw_bonus("indestructible", "kw_indestructible", 3.0) + if "menace" in text or "menace" in kw_list: + score += _kw_bonus("menace", "kw_menace", 1.5) + if "vigilance" in text or "vigilance" in kw_list: + score += w.get("kw_vigilance", 1.0) + if "ward" in text or "ward" in kw_list: + score += _kw_bonus("ward", "kw_ward", 1.5) + if "infect" in text or "infect" in kw_list: + score += w.get("kw_infect", 3.0) + if "annihilator" in text: + score += w.get("kw_annihilator", 4.0) + if "cascade" in text or "cascade" in kw_list: + score += w.get("kw_cascade", 3.0) + if "defender" in text or "defender" in kw_list: + score += w.get("kw_defender", -2.0) + + # ── Triggered ability bonuses ── + if "when" in text and "enters" in text: + score += w.get("trig_etb", 2.0) + _MULTI_OPP_SCALING.get("trig_etb", 0.0) * extra_opps + if "whenever" in text and "attack" in text: + score += w.get("trig_attack", 1.5) + if "when" in text and "dies" in text: + score += w.get("trig_dies", 1.0) + _MULTI_OPP_SCALING.get("trig_dies", 0.0) * extra_opps + + # ── Multiplayer-specific oracle patterns ── + if extra_opps > 0: + # "each opponent" effects scale linearly with opponent count + if "each opponent" in text: + score += 2.0 * extra_opps + # Pillowfort (Propaganda, Ghostly Prison, etc.) + if "attack you" in text and ("pay" in text or "tax" in text or "can't" in text): + score += 1.5 * extra_opps + # "each player" draw/damage effects + if "each player" in text: + score += 1.0 * extra_opps + + # ── Spell-type bonuses ── if card.is_ramp: score += w.get("spell_ramp", 6.0) if card.is_removal: - score += w.get("spell_destroy", 5.0) + score += _kw_bonus("removal", "spell_destroy", 5.0) + if card.is_board_wipe: + # Board wipes get better in multiplayer — more creatures to hit + score += 1.0 * extra_opps + if "draw" in text and ("card" in text or "cards" in text): + score += w.get("spell_draw", 3.0) + _MULTI_OPP_SCALING.get("spell_draw", 0.0) * extra_opps + if "counter target" in text: + score += w.get("spell_counter", 4.0) + _MULTI_OPP_SCALING.get("spell_counter", 0.0) * extra_opps + if "search your library" in text: + score += w.get("spell_search", 2.0) return score