init
This commit is contained in:
commit
0cba7f5ee0
385 changed files with 17912 additions and 0 deletions
339
match/local_match.gd
Normal file
339
match/local_match.gd
Normal file
|
|
@ -0,0 +1,339 @@
|
|||
class_name LocalMatch
|
||||
extends RefCounted
|
||||
## The game controller for one person against the bot, in-process (app/src/match.ts). It holds the
|
||||
## real state and runs the bot. The table only ever sees view() and picks moves from legal().
|
||||
## Everything here is plain GDScript with no nodes, so tests can drive it headlessly.
|
||||
##
|
||||
## The bot's move is split in two so the table can run the thinking on a worker thread:
|
||||
## bot_think() only reads the state, bot_apply() changes it. bot_step() does both.
|
||||
|
||||
signal changed
|
||||
|
||||
## When the game waits for you to respond to the bot: "combat" (in combat only), "all" or "never".
|
||||
const STOPS: Array[String] = ["combat", "all", "never"]
|
||||
const DEFAULT_OPTIONS := {"autoSkip": true, "stops": "combat", "checkInvariants": false}
|
||||
|
||||
var pool: Dictionary
|
||||
## {seed, human, decks: [deck, deck], rules: ["K=V", ...], bot: GreedyParams-shaped Dictionary}
|
||||
var setup: Dictionary
|
||||
var rules: Dictionary
|
||||
var human: int
|
||||
var bot_side: int
|
||||
var s: Dictionary
|
||||
## Every action applied so far, as action keys (yours, the bot's and the automatic ones).
|
||||
var actions: Array[String] = []
|
||||
var options: Dictionary
|
||||
## Invariant errors found, when checking is on.
|
||||
var errors: Array[String] = []
|
||||
## Set while a key is held: stop at the next response window whatever the stops setting says.
|
||||
var hold_stop := false
|
||||
## Every combat so far, the current or latest last. See _snap_combat for the shape.
|
||||
var combats: Array = []
|
||||
## Bumped on every change, so the table knows when to redraw.
|
||||
var version := 0
|
||||
|
||||
var bot: Object
|
||||
## End command: the game turn being ended, or -1.
|
||||
var _ending := -1
|
||||
var _view_cache := {"version": -1, "view": {}}
|
||||
var _legal_cache := {"version": -1, "legal": []}
|
||||
var _log_cache := {"version": -1, "lines": []}
|
||||
|
||||
|
||||
## `bot` picks the bot's moves (a GreedyBot, RandomBot or anything with choose(view, actions, me)).
|
||||
func _init(card_pool: Dictionary, match_setup: Dictionary, the_bot: Object, opts := {}) -> void:
|
||||
pool = card_pool
|
||||
setup = match_setup
|
||||
rules = rules_from(setup.get("rules", []))
|
||||
human = setup.human
|
||||
bot_side = State.other(human)
|
||||
options = DEFAULT_OPTIONS.duplicate()
|
||||
options.merge(opts, true)
|
||||
bot = the_bot
|
||||
s = RulesEngine.create_game({"pool": pool, "decks": setup.decks, "seed": setup.seed, "rules": rules, "log": true})
|
||||
_settle()
|
||||
|
||||
|
||||
## The rules from `K=V` overrides on the defaults.
|
||||
static func rules_from(specs: Array) -> Dictionary:
|
||||
var r := Rules.defaults()
|
||||
for spec in specs:
|
||||
if spec.strip_edges() != "":
|
||||
r = Rules.with_rule(r, spec.strip_edges())
|
||||
return r
|
||||
|
||||
|
||||
## Random legal decks for both players, seeded like the sim's playGame.
|
||||
static func random_decks(card_pool: Dictionary, seed: int, match_rules: Dictionary) -> Array:
|
||||
return Play.deal(card_pool, seed, match_rules)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
# Reading
|
||||
|
||||
func is_over() -> bool:
|
||||
return s.over
|
||||
|
||||
|
||||
func decider() -> Variant:
|
||||
return RulesEngine.decider(s)
|
||||
|
||||
|
||||
func human_to_act() -> bool:
|
||||
return RulesEngine.decider(s) == human
|
||||
|
||||
|
||||
func bot_to_act() -> bool:
|
||||
return RulesEngine.decider(s) == bot_side
|
||||
|
||||
|
||||
## What the human may see. Read-only.
|
||||
func view() -> Dictionary:
|
||||
if _view_cache.version != version:
|
||||
_view_cache = {"version": version, "view": RulesEngine.view_for(s, human)}
|
||||
return _view_cache.view
|
||||
|
||||
|
||||
## Your legal moves, or none while the bot decides.
|
||||
func legal() -> Array:
|
||||
if not human_to_act():
|
||||
return []
|
||||
if _legal_cache.version != version:
|
||||
_legal_cache = {"version": version, "legal": RulesEngine.legal_actions(s)}
|
||||
return _legal_cache.legal
|
||||
|
||||
|
||||
## The play-by-play as you may read it.
|
||||
func log_text() -> Array:
|
||||
return State.log_for(s, human)
|
||||
|
||||
|
||||
## Log lines with their tags, with private text swapped for what you may read. Read-only.
|
||||
func log_lines() -> Array:
|
||||
if _log_cache.version == version:
|
||||
return _log_cache.lines
|
||||
var text := log_text()
|
||||
var out := []
|
||||
var lines: Array = s.log if s.log != null else []
|
||||
for i in lines.size():
|
||||
var l: Dictionary = lines[i].duplicate()
|
||||
l.text = text[i]
|
||||
l.erase("to")
|
||||
l.erase("public")
|
||||
out.append(l)
|
||||
_log_cache = {"version": version, "lines": out}
|
||||
return out
|
||||
|
||||
|
||||
## The current or latest combat, or null.
|
||||
func combat() -> Variant:
|
||||
return combats.back() if not combats.is_empty() else null
|
||||
|
||||
|
||||
func record() -> Dictionary:
|
||||
return {"version": 1, "setup": setup, "actions": actions.duplicate()}
|
||||
|
||||
|
||||
func ending_command() -> bool:
|
||||
return _ending >= 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
# Acting
|
||||
|
||||
## Your move. It must be one of legal().
|
||||
func act(a: Dictionary) -> void:
|
||||
if not human_to_act():
|
||||
Util.fail("It isn't your decision")
|
||||
return
|
||||
_ending = -1
|
||||
_apply(a, legal())
|
||||
_settle()
|
||||
_changed()
|
||||
|
||||
|
||||
## Ends steps until your command is over. Stops if a prompt or response window comes up for you.
|
||||
func end_command() -> void:
|
||||
if not human_to_act() or s.choice != null or RulesEngine.open_window(s) != null:
|
||||
return
|
||||
_ending = s.turn
|
||||
_settle()
|
||||
_changed()
|
||||
|
||||
|
||||
## The bot's next move, without making it. Only reads the state, so it can run on a thread while
|
||||
## nothing else changes the match.
|
||||
func bot_think() -> Dictionary:
|
||||
var lgl := RulesEngine.legal_actions(s)
|
||||
var a: Dictionary = bot.choose(RulesEngine.view_for(s, bot_side), lgl, bot_side)
|
||||
return {"action": a, "legal": lgl}
|
||||
|
||||
|
||||
## Makes a move from bot_think(), if it's still the bot's decision.
|
||||
func bot_apply(thought: Dictionary) -> bool:
|
||||
if not bot_to_act():
|
||||
return false
|
||||
_apply(thought.action, thought.legal)
|
||||
_settle()
|
||||
_changed()
|
||||
return true
|
||||
|
||||
|
||||
## One bot move, if it's the bot's decision. Returns whether it moved.
|
||||
func bot_step() -> bool:
|
||||
if not bot_to_act():
|
||||
return false
|
||||
return bot_apply(bot_think())
|
||||
|
||||
|
||||
## Runs the bot until you must decide or the game ends. For tests and instant bot speed.
|
||||
func run_bot(limit := 100000) -> void:
|
||||
var n := 0
|
||||
while bot_to_act():
|
||||
n += 1
|
||||
if n > limit:
|
||||
Util.fail("The bot made %d moves in a row" % limit)
|
||||
return
|
||||
bot_step()
|
||||
|
||||
|
||||
func set_options(o: Dictionary) -> void:
|
||||
options.merge(o, true)
|
||||
_settle()
|
||||
_changed()
|
||||
|
||||
|
||||
## Re-checks automatic moves, e.g. after a held key is released.
|
||||
func poke() -> void:
|
||||
var before := actions.size()
|
||||
_settle()
|
||||
if actions.size() != before:
|
||||
_changed()
|
||||
|
||||
|
||||
func _changed() -> void:
|
||||
version += 1
|
||||
changed.emit()
|
||||
|
||||
|
||||
func _apply(a: Dictionary, lgl: Array) -> void:
|
||||
Flow.combat_watch = _watch
|
||||
RulesEngine.apply_action(s, a, lgl)
|
||||
Flow.combat_watch = Callable()
|
||||
actions.append(RulesEngine.action_key(a))
|
||||
version += 1
|
||||
if options.checkInvariants:
|
||||
for e in Invariants.check(s):
|
||||
errors.append("Move %d (%s), turn %d: %s" % [actions.size(), RulesEngine.action_key(a), s.turn, e])
|
||||
var last = combat()
|
||||
if last != null and not last.done:
|
||||
# A capture in combat can win the game before the combat's end stage.
|
||||
if s.over:
|
||||
last.stage = "end"
|
||||
last.done = true
|
||||
elif s.combat != null and s.combat.id == last.id:
|
||||
combats[-1] = _snap_combat(last.stage)
|
||||
|
||||
|
||||
## Makes the moves the settings make for you: skipping empty steps, passing, End command.
|
||||
func _settle() -> void:
|
||||
var guard := 0
|
||||
while human_to_act():
|
||||
guard += 1
|
||||
if guard > 10000:
|
||||
Util.fail("The automatic moves are stuck in a loop")
|
||||
return
|
||||
var auto = _automatic()
|
||||
if auto == null:
|
||||
break
|
||||
_apply(auto, legal())
|
||||
if _ending >= 0 and (s.over or s.turn != _ending):
|
||||
_ending = -1
|
||||
|
||||
|
||||
## The move the settings make for you now, if any.
|
||||
func _automatic() -> Variant:
|
||||
if s.choice != null:
|
||||
_ending = -1
|
||||
return null
|
||||
var lgl := legal()
|
||||
var w = RulesEngine.open_window(s)
|
||||
if w != null:
|
||||
if _passes_for(w.kind):
|
||||
return _find(lgl, "pass")
|
||||
_ending = -1
|
||||
return null
|
||||
if _ending >= 0:
|
||||
if s.turn == _ending:
|
||||
return _find(lgl, "end")
|
||||
_ending = -1
|
||||
if options.autoSkip and lgl.all(func(a): return a.type == "end" or a.type == "salvage"):
|
||||
return _find(lgl, "end")
|
||||
return null
|
||||
|
||||
|
||||
static func _find(lgl: Array, type: String) -> Variant:
|
||||
for a in lgl:
|
||||
if a.type == type:
|
||||
return a
|
||||
return null
|
||||
|
||||
|
||||
## Whether the stops setting passes this response window for you.
|
||||
func _passes_for(kind: String) -> bool:
|
||||
if hold_stop:
|
||||
return false
|
||||
match options.stops:
|
||||
"all":
|
||||
return false
|
||||
"never":
|
||||
return true
|
||||
# In combat only: the bot's Executions, Abilities, installs and declarations outside combat pass.
|
||||
return kind == "response" and not (s.combat != null and not s.combat.ended)
|
||||
|
||||
|
||||
func _watch(_s: Dictionary, stage: String) -> void:
|
||||
var c: Dictionary = s.combat
|
||||
var last = combat()
|
||||
if stage == "start" or last == null or last.id != c.id:
|
||||
var info := _snap_combat(stage)
|
||||
info.logFrom = maxi(0, (s.log.size() if s.log != null else 1) - 1)
|
||||
combats.append(info)
|
||||
return
|
||||
# After the damage, keep the Cores and dice from the result: the fighters may be gone by now.
|
||||
if stage == "end":
|
||||
last.ended = c.ended
|
||||
last.stage = stage
|
||||
last.done = true
|
||||
else:
|
||||
combats[-1] = _snap_combat(stage)
|
||||
|
||||
|
||||
## The current combat as the readout shows it: {id, loc, kind, attacker, defenders, rolled, ended,
|
||||
## stage, done, logFrom}, where a fighter is {ref, cores, parts, dice, health, healthLeft}. Call only
|
||||
## while there is a combat.
|
||||
func _snap_combat(stage: String) -> Dictionary:
|
||||
var c: Dictionary = s.combat
|
||||
var last = combat()
|
||||
var prev = last if last != null and last.id == c.id else null
|
||||
var info := func(r: Dictionary) -> Dictionary:
|
||||
var parts := []
|
||||
var cores := Query.cores_of(s, Query.fighter_of(s, r), Callable(), parts)
|
||||
var in_play := State.anomaly(s, r).at != null
|
||||
return {
|
||||
"ref": r, "cores": cores, "parts": parts, "dice": c.dice.get(State.ref_key(r), []).duplicate(),
|
||||
"health": Query.health_of(s, r) if in_play else 0, "healthLeft": Query.health_left(s, r) if in_play else 0,
|
||||
}
|
||||
var defenders := []
|
||||
for d in c.defenders:
|
||||
defenders.append(info.call(d))
|
||||
# Defenders who left keep their last numbers, so the readout still shows who was there.
|
||||
if prev != null:
|
||||
for d in prev.defenders:
|
||||
if not c.defenders.any(func(x): return State.same_ref(x, d.ref)):
|
||||
defenders.append(d)
|
||||
return {
|
||||
"id": c.id, "loc": c.loc, "kind": c.kind, "attacker": info.call(c.attacker), "defenders": defenders,
|
||||
"rolled": c.rolled, "ended": c.ended, "stage": stage, "done": false,
|
||||
"logFrom": prev.logFrom if prev != null else maxi(0, (s.log.size() if s.log != null else 1) - 1),
|
||||
}
|
||||
1
match/local_match.gd.uid
Normal file
1
match/local_match.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dqd0wfs2yhvau
|
||||
140
match/moves.gd
Normal file
140
match/moves.gd
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
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
|
||||
1
match/moves.gd.uid
Normal file
1
match/moves.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://drttefx21d4c
|
||||
Loading…
Add table
Add a link
Reference in a new issue