files-game/match/moves.gd
2026-10-01 17:22:18 -04:00

140 lines
5.2 KiB
GDScript

class_name Moves
## Turns the legal actions into moves the table can show (app/src/moves.ts): a label, a cost, a
## group for the move list, and for click-to-act a source (what you pick first) and a target (what
## you click to play it). The table never builds an action; it only filters and labels the
## engine's list.
##
## Keys for things on the table:
## - `hand:<card name>` a card in your hand
## - `anomaly:<player>:<slot>` an Anomaly (on the board, or in its slot when it isn't in play)
## - `loc:<id>` a location
## - `gate:<line id>` a gateway between two locations
## - `none` no target: the move is listed on its source's menu
## - `bar` the action bar (End, Pass, choices)
const GROUP_ORDER: Array[String] = ["Turn", "Place and move", "Advance", "Attack", "Hardware", "Program", "Execution", "Ability", "Recruit", "Anomalies", "Firewalls", "Salvage"]
static var _ref_re := RegEx.create_from_string("^anomaly:(\\d):(\\d)$")
static func anomaly_key(r: Dictionary) -> String:
return "anomaly:%d:%d" % [r.player, r.slot]
static func loc_key(loc: String) -> String:
return "loc:" + loc
static func gate_key(line: String) -> String:
return "gate:" + line
static func hand_key(card_name: String) -> String:
return "hand:" + card_name
## An Anomaly ref from an `anomaly:` key, or null.
static func ref_of_key(key: String) -> Variant:
var m := _ref_re.search(key)
return State.ref(int(m.get_string(1)), int(m.get_string(2))) if m != null else null
## Where a card on the board is: its location, or its wearer.
static func _card_spot(s: Dictionary, id: int) -> String:
var loc = State.program_location(s, id)
if loc != null:
return loc_key(loc)
var r = State.wearer_of(s, id)
if r != null:
return anomaly_key(r)
return "none"
static func _source_and_target(s: Dictionary, a: Dictionary, me: int) -> Array:
match a.type:
"end", "pass", "choose":
return ["bar", "none", "Turn"]
"place", "advance", "attack":
var group := "Attack" if a.type == "attack" else "Advance" if a.type == "advance" else "Place and move"
return [anomaly_key(State.ref(me, a.slot)), loc_key(a.to), group]
"recall":
return [anomaly_key(State.ref(me, a.slot)), "none", "Place and move"]
"clearFirewall", "firewall":
return [gate_key(a.line), "none", "Firewalls"]
"salvage":
return [hand_key(State.name_of(s, a.card)), "none", "Salvage"]
"installHardware":
return [hand_key(State.name_of(s, a.card)), anomaly_key(State.ref(me, a.slot)), "Hardware: " + State.name_of(s, a.card)]
"recruit":
return [hand_key(State.name_of(s, a.card)), anomaly_key(State.ref(me, a.slot)), "Recruit: " + State.name_of(s, a.card)]
"buyAnomaly":
return [anomaly_key(State.ref(me, a.slot)), "none", "Anomalies"]
"installProgram":
return [hand_key(State.name_of(s, a.card)), loc_key(a.at), "Program: " + State.name_of(s, a.card)]
"play":
# Executions target a Server; with no target, the Anomaly or card they name.
var target := "none"
if a.target:
target = loc_key(a.target)
elif a.args.get("ref") != null:
target = anomaly_key(a.args.ref)
elif a.args.get("card") != null:
target = _card_spot(s, a.args.card)
return [hand_key(State.name_of(s, a.card)), target, "Execution: " + State.name_of(s, a.card)]
"activate":
var src = RulesEngine.ability_source(s, a.card, a.name)
var source := "none"
if src != null and src.get("ref") != null:
source = anomaly_key(src.ref)
elif src != null and src.get("at"):
source = loc_key(src.at)
var target := "none"
if a.args.get("to"):
target = loc_key(a.args.to)
elif a.args.get("ref") != null:
target = anomaly_key(a.args.ref)
elif a.args.get("card") != null:
target = _card_spot(s, a.args.card)
var d := State.def_of(s, a.card)
var ability_name: String = d.ability.name if src != null and src.kind == "anomaly" and d.has("ability") else a.name
# A target that is the source itself (a card on the same Anomaly) is listed on the source's menu.
return [source, "none" if target == source else target, "Ability: " + ability_name]
return ["none", "none", "Other"]
## The legal actions as moves, with duplicate labels (interchangeable copies of a card) dropped.
## A move is {action, key, label, cost, group, source, target}.
static func moves_of(s: Dictionary, legal: Array, me: int) -> Array:
var seen := {}
var out := []
for a in legal:
var label := Describe.describe_action(s, a)
if seen.has(label):
continue
seen[label] = true
var st := _source_and_target(s, a, me)
out.append({"action": a, "key": RulesEngine.action_key(a), "label": label, "cost": RulesEngine.action_cost(s, a),
"source": st[0], "target": st[1], "group": st[2]})
return out
static func _group_rank(g: String) -> int:
var i := GROUP_ORDER.find(g.split(":")[0])
return GROUP_ORDER.size() if i < 0 else i
## Moves grouped for the move list, in a steady order: an Array of [group, moves].
static func group_moves(moves: Array) -> Array:
var groups := {}
for m in moves:
if not groups.has(m.group):
groups[m.group] = []
groups[m.group].append(m)
var out := []
for g in groups:
out.append([g, groups[g]])
out.sort_custom(func(x, y):
var rx := _group_rank(x[0])
var ry := _group_rank(y[0])
return rx < ry or (rx == ry and x[0] < y[0]))
return out