This commit is contained in:
Forrest 2026-10-01 17:22:18 -04:00
commit 0cba7f5ee0
385 changed files with 17912 additions and 0 deletions

398
scenes/game.gd Normal file
View file

@ -0,0 +1,398 @@
extends Node
## One game against the bot: the LocalMatch, the 3D table and the HUD, and the click-to-act logic
## between them (app/src/ui/game.tsx). You pick a source (a card in hand, an Anomaly, a gateway, a
## location) and then one of its glowing targets; the Moves tab lists everything else.
##
## The bot thinks on a WorkerThreadPool thread. While it does, nothing changes the match, the
## table only reads it, and nothing here calls Odds, whose caches the bot fills without locks
## (see _thinking()).
var pool: Dictionary
var match_: LocalMatch
var table: GameTable
var hud: Hud
var settings := {"stops": "combat", "autoSkip": true, "checkInvariants": false, "botDelay": 0.45}
## The picked source key, or "".
var selected := ""
var moves: Array = []
var _moves_version := -1
var _bot_task := -1
var _bot_thought := {}
var _bot_wait := 0.0
var _paused := false
var _hover_seen := {}
var _combat_seen := 0
## Log lines already turned into table pop-ups.
var _log_seen := 0
func _ready() -> void:
pool = Cards.load_cards()
table = GameTable.new()
add_child(table)
hud = Hud.new()
add_child(hud)
hud.set_settings(settings)
hud.move_chosen.connect(_play)
hud.source_picked.connect(func(key): _select(key))
hud.cancel_pressed.connect(_cancel)
hud.end_command_pressed.connect(_end_command)
hud.view_toggled.connect(func(): table.toggle_view())
hud.new_game_requested.connect(start_game)
hud.settings_changed.connect(_on_settings)
var args := OS.get_cmdline_user_args()
var seed := randi_range(1, 999999)
var i := args.find("--seed")
if i >= 0 and i + 1 < args.size():
seed = int(args[i + 1])
start_game({"seed": seed, "human": 0, "style": "greedy"})
if args.find("--seed") < 0:
hud.show_start({"seed": seed, "cancel": true})
## Starts a game from {seed, human, style}.
func start_game(opts: Dictionary) -> void:
_join_bot()
var rules := LocalMatch.rules_from([])
var style: String = opts.get("style", "greedy")
var params: Dictionary = GreedyBot.STYLES[style].params
var setup := {
"seed": opts.seed, "human": opts.get("human", 0), "decks": LocalMatch.random_decks(pool, opts.seed, rules),
"rules": [], "bot": params, "style": style,
}
match_ = LocalMatch.new(pool, setup, GreedyBot.new(params), {"stops": settings.stops, "autoSkip": settings.autoSkip, "checkInvariants": settings.checkInvariants})
match_.changed.connect(_on_changed)
selected = ""
_moves_version = -1
_combat_seen = 0
_bot_wait = 0.0
_log_seen = match_.log_lines().size()
hud.reset_for_new_game()
table.set_seat(match_.human, true)
_on_changed()
func _on_settings(s: Dictionary) -> void:
settings = s
if match_ != null and _bot_task < 0:
match_.set_options({"stops": s.stops, "autoSkip": s.autoSkip, "checkInvariants": s.checkInvariants})
# ---------------------------------------------------------------------------------------------
# The bot
func _process(delta: float) -> void:
if match_ == null:
return
if _bot_task >= 0:
if WorkerThreadPool.is_task_completed(_bot_task):
WorkerThreadPool.wait_for_task_completion(_bot_task)
_bot_task = -1
_bot_wait = 0.0
match_.bot_apply(_bot_thought)
elif not _paused and not match_.is_over() and match_.bot_to_act() and not hud.modal_open():
_bot_wait += delta
if _bot_wait >= settings.botDelay:
var m := match_
_bot_task = WorkerThreadPool.add_task(func(): _bot_thought = m.bot_think(), false, "bot")
if table.hovered != _hover_seen:
_hover_seen = table.hovered.duplicate()
_inspect(_hover_seen)
_refresh()
## Waits for a bot move in progress and drops it (for a new game).
func _join_bot() -> void:
if _bot_task >= 0:
WorkerThreadPool.wait_for_task_completion(_bot_task)
_bot_task = -1
func _thinking() -> bool:
return _bot_task >= 0
# ---------------------------------------------------------------------------------------------
# Drawing
func _on_changed() -> void:
if _moves_version != match_.version:
_moves_version = match_.version
moves = Moves.moves_of(match_.view(), match_.legal(), match_.human)
# Drop a selection that no longer has moves.
if selected != "" and not moves.any(func(mv): return mv.source == selected):
selected = ""
hud.hide_popover()
var c = match_.combat()
if c != null and c.id != _combat_seen:
_combat_seen = c.id
hud.show_combat_tab()
_refresh()
_pop_new_lines()
## Shows what the newest log lines did on the table: damage, roll results, captures. Several from
## one move come one after another; after a long run of moves at once, only the last few show.
func _pop_new_lines() -> void:
var lines := match_.log_lines()
var pops := []
for i in range(_log_seen, lines.size()):
var l: Dictionary = lines[i]
var loc = l.get("loc")
if loc == null or not BoardLayout.at.has(loc):
continue
var at: Vector3 = BoardLayout.at[loc]
var p = l.get("player")
var color: Color = GameTable.OWNER_COLORS[p].lightened(0.3) if p != null else Color("#d7dee6")
if l.get("ref") != null and l.get("amount") != null:
pops.append(func(delay): table.pop_damage(l.ref, l.amount, l.text.contains("destroyed"), loc, delay))
elif l.get("tag") == "capture" and l.text.contains(" captures "):
pops.append(func(delay):
table.flash_capture(loc, p)
table.pop_text(at, "%s takes %s" % [State.player_name(p), loc], color, delay))
elif l.get("tag") == "combat" and l.text.contains(" wins the roll"):
var t := _roll_score(l.text)
pops.append(func(delay): table.pop_text(at, "%s wins %s" % [State.player_name(p), t], color, delay))
elif l.get("tag") == "combat" and l.text.begins_with("Tie,"):
pops.append(func(delay): table.pop_text(at, "Stalemate", color, delay))
elif l.get("tag") == "combat" and l.text.contains(" ends: "):
pops.append(func(delay): table.pop_text(at, "Combat ends", Color("#ffb454"), delay))
_log_seen = lines.size()
pops = pops.slice(maxi(0, pops.size() - 5))
for i in pops.size():
pops[i].call(i * 0.45)
static var _roll_re := RegEx.create_from_string("wins the roll, (\\d+) to (\\d+)")
## "14–7" from "North wins the roll, 7 to 14: 7 damage.", the winner's total first.
static func _roll_score(text: String) -> String:
var m := _roll_re.search(text)
if m == null:
return ""
var a := int(m.get_string(1))
var b := int(m.get_string(2))
return "%d–%d" % [maxi(a, b), mini(a, b)]
func _refresh() -> void:
var v := match_.view()
var marks := _marks(v)
table.sync(v, marks)
hud.refresh({"match": match_, "view": v, "moves": moves, "selected": selected, "marks": marks,
"thinking": _thinking(), "paused": _paused, "preview": _preview(v)})
## How each key glows: the combat, your sources, and the selected source's targets.
func _marks(v: Dictionary) -> Dictionary:
var marks := {}
var c = match_.combat()
if c != null and not c.done:
marks[Moves.loc_key(c.loc)] = "fight"
var my_choice: bool = match_.human_to_act() and v.choice != null
if my_choice:
return marks
var in_window: bool = match_.human_to_act() and RulesEngine.open_window(v) != null
# Salvage is always there, so only other moves make a card glow.
for mv in moves:
if mv.source != "bar" and mv.source != "none" and mv.action.type != "salvage":
marks[mv.source] = "respond" if in_window else "source"
if selected != "":
marks[selected] = "selected"
for mv in moves:
if mv.source == selected and mv.target != "none":
marks[mv.target] = "attack" if mv.action.type == "attack" else "target"
return marks
## The odds of the attack you'd make by clicking the hovered location.
func _preview(v: Dictionary) -> String:
if selected == "" or _thinking() or _hover_seen.is_empty():
return ""
for mv in moves:
if mv.source == selected and mv.target == _hover_seen.key and mv.action.type == "attack":
var me := match_.human
var from = State.anomaly(v, State.ref(me, mv.action.slot)).at
var to: String = mv.action.to
var atk := RulesEngine.anomaly_cores(v, me, mv.action.slot, "attack", from, to)
var def := 0
for r in State.anomalies_at(v, to):
def += RulesEngine.anomaly_cores(v, r.player, r.slot, "defend", to, to)
var o := Odds.roll_odds(atk, def)
return "%d vs %d Cores: win %d%%, tie %d%%, lose %d%%. Average damage %.1f dealt, %.1f taken." % [
atk, def, roundi(o.win * 100), roundi(o.tie * 100), roundi(o.lose * 100), o.damageDealt, o.damageTaken]
return ""
func _inspect(h: Dictionary) -> void:
if h.is_empty():
return
var v := match_.view()
var key: String = h.key
var card: String = h.card
var text := ""
var r = Moves.ref_of_key(key)
if r != null:
var av := State.anomaly(v, r)
if card == "" and av.card >= 0 and State.anomaly(v, State.original_of(r)).faceUp:
card = State.name_of(v, av.card)
# The card pointed at (the Anomaly, or Hardware it wears), then the Anomaly's state.
text = _anomaly_info(v, r)
if card != "":
text = _card_info(v, card) + "\n\n" + text
elif key.begins_with("loc:"):
var loc := key.substr(4)
if card == "":
var server = v.locations[loc].server
card = State.name_of(v, server) if server != null else ""
text = (_card_info(v, card) + "\n\n" if card != "" else "") + _location_info(v, loc)
elif key.begins_with("gate:"):
var line := key.substr(5)
var level: int = v.firewalls.get(line, 0)
text = "[b]Gateway %s[/b]\n" % Describe.line_name(line)
text += "A Lv %d Firewall blocks this line. Clearing it costs %d Gb." % [level, State.firewall_cost(v, level)] if level else "Open: no Firewall."
elif card != "":
text = _card_info(v, card)
elif key.begins_with("pile:"):
var parts := key.split(":")
var pl: Dictionary = v.players[int(parts[1])]
text = "[b]%s's %s[/b]\n%d cards in hand, %d in the deck, %d Servers in the pile, %d in the discard pile." % [
State.player_name(int(parts[1])), parts[2], pl.hand.size(), pl.deck.size(), pl.serverPile.size(), pl.discard.size()]
hud.inspect({"art": card, "text": text})
static func _card_info(v: Dictionary, card: String) -> String:
var d: Dictionary = v.defs.get(card, {})
if d.is_empty():
return "[b]%s[/b]" % card
var out := "[b]%s[/b]\n[color=#7d8a97]%s%s · %d Gb[/color]" % [d.name, d.kind.capitalize(), " (%s)" % d.type if d.type != "" else "", d.bandwidth]
if d.kind == "anomaly":
out += "\n%d Cores · %d Health · %d Expansion Slots" % [d.cores, d.health, d.slots]
if d.text != "":
out += "\n" + d.text
if d.has("ability"):
out += "\n[b]%s[/b]%s: %s" % [d.ability.name, " (%d Gb)" % d.ability.cost if d.ability.cost else "", d.ability.text]
return out
static func _anomaly_info(v: Dictionary, r: Dictionary) -> String:
var av := State.anomaly(v, r)
var out := "[b]%s[/b]" % Describe.ref_name(v, r, null)
out += "\n%s" % ("on %s" % av.at if av.at != null else "in its slot")
var badge := GameTable.badge_text(v, r)
if badge != "":
out += " · " + badge
if not av.hardware.is_empty():
out += "\nHardware: " + ", ".join(av.hardware.map(func(h): return State.name_of(v, h)))
return out
static func _location_info(v: Dictionary, loc: String) -> String:
var L: Dictionary = v.locations[loc]
var out := "[b]%s[/b]" % loc
if L.owner == null:
out += " · open"
else:
out += " · held by %s" % State.player_name(L.owner)
if not GameMap.is_start(loc):
out += " · Lv %d for South, Lv %d for North" % [GameMap.level_for(0, loc), GameMap.level_for(1, loc)]
if not L.programs.is_empty():
out += "\nPrograms: " + ", ".join(L.programs.map(func(id): return State.name_of(v, id)))
var here := State.anomalies_at(v, loc)
if not here.is_empty():
out += "\nAnomalies: " + ", ".join(here.map(func(x): return Describe.ref_name(v, x, null)))
return out
# ---------------------------------------------------------------------------------------------
# Acting
func _play(mv: Dictionary) -> void:
selected = ""
hud.hide_popover()
if match_.human_to_act() and match_.legal().has(mv.action):
match_.act(mv.action)
else:
_refresh()
func _select(key: String) -> void:
selected = key if moves.any(func(mv): return mv.source == key) else ""
_refresh()
func _cancel() -> void:
selected = ""
hud.hide_popover()
_refresh()
func _end_command() -> void:
selected = ""
match_.end_command()
func _pick(key: String, at: Vector2) -> void:
if hud.popover_open():
hud.hide_popover()
return
var v := match_.view()
if match_.human_to_act() and v.choice != null:
return
if selected != "":
var hits := moves.filter(func(mv): return mv.source == selected and mv.target == key)
if hits.size() == 1:
_play(hits[0])
return
if hits.size() > 1:
hud.show_popover(hits, at)
return
if key == selected:
_cancel()
return
_select(key)
func _unhandled_input(e: InputEvent) -> void:
if match_ == null:
return
if e is InputEventMouseButton and e.pressed:
if e.button_index == MOUSE_BUTTON_LEFT:
_pick(table.hovered.get("key", "none"), e.position)
elif e.button_index == MOUSE_BUTTON_RIGHT:
_cancel()
elif e.button_index == MOUSE_BUTTON_WHEEL_UP:
table.zoom(0.92)
elif e.button_index == MOUSE_BUTTON_WHEEL_DOWN:
table.zoom(1.08)
elif e is InputEventKey:
if e.keycode == KEY_SHIFT:
match_.hold_stop = e.pressed
if not e.pressed and _bot_task < 0:
match_.poke()
return
if not e.pressed or e.echo:
return
match e.keycode:
KEY_ESCAPE:
if hud.modal_open() and match_.view().choice == null and not match_.is_over():
hud.close_modal()
_cancel()
KEY_SPACE:
var v := match_.view()
if v.choice == null and not hud.modal_open():
for t in ["pass", "end"]:
var hit := moves.filter(func(mv): return mv.action.type == t)
if not hit.is_empty():
_play(hit[0])
break
KEY_ENTER, KEY_KP_ENTER:
if not hud.modal_open():
_end_command()
KEY_T:
table.toggle_view()
KEY_P:
_paused = not _paused
_refresh()

1
scenes/game.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://cfv7h7j58adlm

6
scenes/game.tscn Normal file
View file

@ -0,0 +1,6 @@
[gd_scene format=3]
[ext_resource type="Script" path="res://scenes/game.gd" id="1_game"]
[node name="Game" type="Node"]
script = ExtResource("1_game")