"""D3 Q1 cont / Q3 / Q4: the sub-predicates of the eight action flags, and the
menu-side literal enumeration.

HYPOTHESIS A: FUN_1801a8900 (the extra player-only term inside FUN_1801a7260)
and FUN_1801a8850 (the early-out inside FUN_1800e2a40, which if TRUE leaves all
eight flag bytes UNINITIALISED) are additional conditions nobody has enumerated.

HYPOTHESIS B (Q2): there is a separate menu/action surface. Enumerate every
.rdata literal that looks like a per-card menu action so the enabled entries
("Send to Club", "Quick Sell", "Store all remaining") can be contrasted against
the disabled ones.

CONTROL for the literal scan: "TO_TRADE_PILE" is a known-present literal
published by FUN_18003e370, so the scan MUST return it; if it does not, the scan
is broken and every absence in the same run is worthless.
"""
import traceback

try:
    for label, a in [
        ("FUN_1801a8900  extra player term in TO_TRADE_PILE", 0x1801a8900),
        ("FUN_1801a8850  early-out guard in FUN_1800e2a40", 0x1801a8850),
        ("FUN_1801a8110  family getter", 0x1801a8110),
        ("FUN_1801a71c0  DISCARD predicate", 0x1801a71c0),
        ("FUN_1801a7210  MODIFY predicate", 0x1801a7210),
        ("FUN_1801a7250  TO_ACTIVE_SQUAD sub", 0x1801a7250),
        ("FUN_1801a7180  TO_STICKER_BOOK", 0x1801a7180),
        ("FUN_1801a7320  QUICK_SEARCH", 0x1801a7320),
        ("FUN_1801a71e0  DREAM_REPLACE", 0x1801a71e0),
    ]:
        src = dec(a)
        print("=" * 78)
        print("### %s  @ %#x   len(src)=%d" % (label, a, len(src)))
        print("=" * 78)
        print(src)
        print()

    print("#" * 78)
    print("### LITERAL SCAN over .rdata for menu-action-shaped names")
    print("#" * 78)
    NEEDLES = [b"TO_TRADE_PILE", b"TRADE_PILE", b"TRANSFER", b"LIST_ITEM",
               b"SEND_TO_CLUB", b"TO_CLUB", b"QUICK_SELL", b"QUICKSELL",
               b"DISCARD", b"STORE_ALL", b"MOVE_TO", b"CONTEXT", b"MENU",
               b"ACTION", b"AUCTION", b"WATCH", b"BID", b"BUY_NOW",
               b"tradePile", b"watchList", b"transfermarket"]
    for n in NEEDLES:
        hits = find_all(n, blocks=(".rdata", ".data"))
        print("--- %-16s %d hits" % (n.decode(), len(hits)))
        seen = set()
        for h in hits[:80]:
            # walk back to the start of the C string
            p = h
            for _ in range(120):
                try:
                    if mem.getByte(addr(p - 1)) & 0xFF == 0:
                        break
                except Exception:
                    break
                p -= 1
            s = rd_str(p, 160)
            if s in seen:
                continue
            seen.add(s)
            print("      %#x  %r" % (p, s))
except Exception:
    traceback.print_exc()
