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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
# Add project root to path
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
| 7 |
+
|
| 8 |
+
import engine_rust
|
| 9 |
+
|
| 10 |
+
from ai.utils.benchmark_decks import parse_deck
|
| 11 |
+
from engine.game.data_loader import CardDataLoader # Only for descriptions
|
| 12 |
+
from engine.game.desc_utils import get_action_desc
|
| 13 |
+
from engine.game.game_state import GameState # Only for descriptions
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def verify_abilities():
|
| 17 |
+
print("--- AI Ability Visibility Verification (Rust Engine) ---")
|
| 18 |
+
|
| 19 |
+
# 1. Load DB
|
| 20 |
+
db_path = "engine/data/cards_compiled.json"
|
| 21 |
+
with open(db_path, "r", encoding="utf-8") as f:
|
| 22 |
+
db_content = f.read()
|
| 23 |
+
db_json = json.loads(db_content)
|
| 24 |
+
|
| 25 |
+
# Initialize Python GameState for descriptions
|
| 26 |
+
loader = CardDataLoader(db_path)
|
| 27 |
+
members, lives, energy = loader.load()
|
| 28 |
+
GameState.initialize_class_db(members, lives)
|
| 29 |
+
py_gs = GameState()
|
| 30 |
+
|
| 31 |
+
# 2. Setup Rust Engine State
|
| 32 |
+
db = engine_rust.PyCardDatabase(db_content)
|
| 33 |
+
gs = engine_rust.PyGameState(db)
|
| 34 |
+
|
| 35 |
+
# Load a benchmark deck to get valid IDs
|
| 36 |
+
deck_file = "ai/decks/liella_cup.txt"
|
| 37 |
+
main_deck, lives_deck, energy_deck = parse_deck(
|
| 38 |
+
deck_file, db_json["member_db"], db_json["live_db"], db_json.get("energy_db", {})
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Standard sizes
|
| 42 |
+
test_lives = (lives_deck * 10)[:12]
|
| 43 |
+
test_deck = (main_deck * 10)[:48]
|
| 44 |
+
test_energy = (energy_deck * 10)[:12]
|
| 45 |
+
|
| 46 |
+
gs.initialize_game(test_deck, test_deck, test_energy, test_energy, test_lives, test_lives)
|
| 47 |
+
|
| 48 |
+
# Fast-forward to Main Phase
|
| 49 |
+
# Phase Flow: MULLIGAN_P1 -> MULLIGAN_P2 -> LIVE_SET -> MAIN
|
| 50 |
+
print(f"Skipping Mulligan P1 (Current: {gs.phase})...")
|
| 51 |
+
gs.step(0) # Confirm Mulligan P1
|
| 52 |
+
print(f"Skipping Mulligan P2 (Current: {gs.phase})...")
|
| 53 |
+
gs.step(0) # Confirm Mulligan P2
|
| 54 |
+
|
| 55 |
+
# In Live Set, we need to set a card.
|
| 56 |
+
# Actions 400-459 are Live Set.
|
| 57 |
+
# Player 1 (index 0) sets a card.
|
| 58 |
+
print(f"Setting Live Card (Current: {gs.phase})...")
|
| 59 |
+
gs.step(400) # Set first card in hand as live
|
| 60 |
+
# Player 2 (index 1) sets a card.
|
| 61 |
+
gs.step(400)
|
| 62 |
+
|
| 63 |
+
# Sync Python GS for descriptions
|
| 64 |
+
hand = list(gs.get_player(0).hand)
|
| 65 |
+
py_gs.players[0].hand = hand
|
| 66 |
+
py_gs.current_player = gs.current_player
|
| 67 |
+
py_gs.phase = gs.phase
|
| 68 |
+
|
| 69 |
+
# 3. Find or Inject Ability Card
|
| 70 |
+
ability_card_id = -1
|
| 71 |
+
hand_idx = -1
|
| 72 |
+
for i, cid in enumerate(hand):
|
| 73 |
+
if cid in members:
|
| 74 |
+
card = members[cid]
|
| 75 |
+
# Check for ACTIVATED trigger
|
| 76 |
+
if any(ab.trigger.name == "ACTIVATED" for ab in card.abilities):
|
| 77 |
+
ability_card_id = cid
|
| 78 |
+
hand_idx = i
|
| 79 |
+
break
|
| 80 |
+
|
| 81 |
+
# Sync Python GS for descriptions
|
| 82 |
+
hand = list(gs.get_player(0).hand)
|
| 83 |
+
py_gs.players[0].hand = hand
|
| 84 |
+
py_gs.current_player = gs.current_player
|
| 85 |
+
py_gs.phase = gs.phase
|
| 86 |
+
|
| 87 |
+
# 3. Inject and Play Ability Card (Keke ID 247)
|
| 88 |
+
target_cid = 247
|
| 89 |
+
print(f"Injecting Card ID {target_cid} into hand...")
|
| 90 |
+
# Update Rust State
|
| 91 |
+
gs.get_player(0).hand = [target_cid] + list(gs.get_player(0).hand)
|
| 92 |
+
# Update Python Stage for get_action_desc
|
| 93 |
+
py_gs.players[0].hand = [target_cid] + list(py_gs.players[0].hand)
|
| 94 |
+
|
| 95 |
+
# Check if card has an activated ability in py_gs
|
| 96 |
+
card = members[target_cid]
|
| 97 |
+
abs = [ab for ab in card.abilities if ab.trigger.name == "ACTIVATED"]
|
| 98 |
+
print(f"Card {card.name} has {len(abs)} activated abilities.")
|
| 99 |
+
for ab in abs:
|
| 100 |
+
print(f" - Ability: {ab.raw_text}")
|
| 101 |
+
|
| 102 |
+
print(f"Playing {get_action_desc(500, py_gs)} to Center...")
|
| 103 |
+
# Action ID 2 (Hand 0 to Center)
|
| 104 |
+
gs.step(2)
|
| 105 |
+
|
| 106 |
+
# Update Python Stage
|
| 107 |
+
py_gs.players[0].stage[1] = target_cid
|
| 108 |
+
py_gs.players[0].hand = list(gs.get_player(0).hand)
|
| 109 |
+
|
| 110 |
+
# Check legality
|
| 111 |
+
def check_legal(gs, py_gs):
|
| 112 |
+
legal_actions = list(gs.get_legal_action_ids())
|
| 113 |
+
print(f"Phase: {gs.phase} | Legal IDs: {len(legal_actions)}")
|
| 114 |
+
if 201 in legal_actions:
|
| 115 |
+
print("SUCCESS: Action 201 (Center Ability) is legal.")
|
| 116 |
+
return True
|
| 117 |
+
print("Action 201 is NOT legal.")
|
| 118 |
+
if len(legal_actions) < 20: # Show only if list is small
|
| 119 |
+
for a in legal_actions:
|
| 120 |
+
print(f" - Action {a}: {get_action_desc(a, py_gs)}")
|
| 121 |
+
return False
|
| 122 |
+
|
| 123 |
+
if not check_legal(gs, py_gs):
|
| 124 |
+
print("Skipping to next turn's Main Phase...")
|
| 125 |
+
# Finish Turn 1
|
| 126 |
+
gs.step(0) # End Main
|
| 127 |
+
gs.step(0) # End Live Start
|
| 128 |
+
gs.step(0) # End Live Set
|
| 129 |
+
gs.step(0) # End Performance
|
| 130 |
+
gs.step(0) # End Turn
|
| 131 |
+
|
| 132 |
+
# Turn 2
|
| 133 |
+
gs.step(0) # End Active/Standby
|
| 134 |
+
gs.step(0) # End Draw
|
| 135 |
+
|
| 136 |
+
# Now we should be in Turn 2 Main Phase
|
| 137 |
+
# Re-sync Python GS
|
| 138 |
+
py_gs.phase = gs.phase
|
| 139 |
+
py_gs.current_player = gs.current_player
|
| 140 |
+
|
| 141 |
+
check_legal(gs, py_gs)
|
| 142 |
+
|
| 143 |
+
# 4. Run MCTS
|
| 144 |
+
model_path = "ai/models/alphanet_best.onnx"
|
| 145 |
+
mcts = engine_rust.PyHybridMCTS(model_path, 0.0)
|
| 146 |
+
|
| 147 |
+
print("\nRunning MCTS (1600 sims)...")
|
| 148 |
+
suggestions = mcts.get_suggestions(gs, 1600)
|
| 149 |
+
|
| 150 |
+
print("\nTop MCTS Suggestions:")
|
| 151 |
+
for action_id, score, visits in sorted(suggestions, key=lambda x: x[2], reverse=True)[:10]:
|
| 152 |
+
desc = get_action_desc(int(action_id), py_gs)
|
| 153 |
+
print(f" Action {action_id:4}: {visits:5} visits | {score:.4f} score | {desc}")
|
| 154 |
+
|
| 155 |
+
# 5. Final confirmation
|
| 156 |
+
ability_actions = [a for (a, s, v) in suggestions if 200 <= a <= 202]
|
| 157 |
+
if ability_actions:
|
| 158 |
+
print(f"\nCONFIRMED: MCTS explored ability actions: {[int(a) for a in ability_actions]}")
|
| 159 |
+
for a in ability_actions:
|
| 160 |
+
print(f" - {get_action_desc(int(a), py_gs)}")
|
| 161 |
+
else:
|
| 162 |
+
print("\nISSUE: MCTS did NOT explore ability actions.")
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
if __name__ == "__main__":
|
| 166 |
+
verify_abilities()
|