This commit is contained in:
Forrest 2026-10-01 23:01:56 -04:00
parent 0cba7f5ee0
commit 32ed347b58
172 changed files with 5464 additions and 582 deletions

View file

@ -11,7 +11,8 @@ var pool: Dictionary
var match_: LocalMatch
var table: GameTable
var hud: Hud
var settings := {"stops": "combat", "autoSkip": true, "checkInvariants": false, "botDelay": 0.45}
var presenter: Presenter
var settings := GameSettings.DEFAULTS.duplicate()
## The picked source key, or "".
var selected := ""
@ -22,9 +23,10 @@ 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
## What the mouse is over in the hand-focus row, which wins over the table's hover while open.
var _hand_hover := {}
## Engine events already acted out by the presenter.
var _events_seen := 0
func _ready() -> void:
@ -33,6 +35,10 @@ func _ready() -> void:
add_child(table)
hud = Hud.new()
add_child(hud)
presenter = Presenter.new()
presenter.table = table
presenter.hud = hud
add_child(presenter)
hud.set_settings(settings)
hud.move_chosen.connect(_play)
hud.source_picked.connect(func(key): _select(key))
@ -41,14 +47,29 @@ func _ready() -> void:
hud.view_toggled.connect(func(): table.toggle_view())
hud.new_game_requested.connect(start_game)
hud.settings_changed.connect(_on_settings)
hud.menu_requested.connect(_to_menu)
hud.hand_hovered.connect(func(info): _hand_hover = info)
hud.hand_picked.connect(_pick)
hud.hand_focus_changed.connect(func(open):
if not open:
_hand_hover = {}
table.set_hand_tucked(open))
# From the menu, the router brings the game and saved settings. Opened directly (the editor,
# tests), it deals a game from --seed or a random seed with the default settings.
var routed := SceneRouter.take()
if routed.has("settings"):
settings = routed.settings.duplicate()
hud.set_settings(settings)
Motion.speed_scale = GameSettings.anim_scale(settings)
if routed.has("game"):
start_game(routed.game)
return
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}.
@ -65,9 +86,10 @@ func start_game(opts: Dictionary) -> void:
match_.changed.connect(_on_changed)
selected = ""
_moves_version = -1
_combat_seen = 0
_bot_wait = 0.0
_log_seen = match_.log_lines().size()
# From 0, so the opening deal plays out.
_events_seen = 0
presenter.skip()
hud.reset_for_new_game()
table.set_seat(match_.human, true)
_on_changed()
@ -75,10 +97,17 @@ func start_game(opts: Dictionary) -> void:
func _on_settings(s: Dictionary) -> void:
settings = s
Motion.speed_scale = GameSettings.anim_scale(s)
GameSettings.save(s)
if match_ != null and _bot_task < 0:
match_.set_options({"stops": s.stops, "autoSkip": s.autoSkip, "checkInvariants": s.checkInvariants})
func _to_menu(screen: String) -> void:
_join_bot()
SceneRouter.to_menu(screen)
# ---------------------------------------------------------------------------------------------
# The bot
@ -91,13 +120,14 @@ func _process(delta: float) -> void:
_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():
elif not _paused and not match_.is_over() and match_.bot_to_act() and not hud.modal_open() and not presenter.busy():
_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()
var hover: Dictionary = _hand_hover if hud.hand_focus_open() else table.hovered
if hover != _hover_seen:
_hover_seen = hover.duplicate()
_inspect(_hover_seen)
_refresh()
@ -124,57 +154,14 @@ func _on_changed() -> void:
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()
# The show for this change: the presenter plans it before the table syncs, then cues what
# needs the new positions.
# A new game starts from nothing, not from the last game's table.
var before := table.shown_view() if _events_seen > 0 else {}
presenter.stage(match_.events_since(_events_seen), before, match_.view(), match_.human)
_events_seen = match_.event_count()
_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)]
presenter.run()
func _refresh() -> void:
@ -182,7 +169,7 @@ func _refresh() -> void:
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)})
"thinking": _thinking(), "paused": _paused, "preview": _preview(v), "show_left": presenter.time_left()})
## How each key glows: the combat, your sources, and the selected source's targets.
@ -204,6 +191,11 @@ func _marks(v: Dictionary) -> Dictionary:
for mv in moves:
if mv.source == selected and mv.target != "none":
marks[mv.target] = "attack" if mv.action.type == "attack" else "target"
# Its defenders glow too: clicking one attacks where it stands (see _hits_on).
if mv.action.type == "attack":
for r in State.anomalies_at(v, mv.action.to):
if not marks.has(Moves.anomaly_key(r)):
marks[Moves.anomaly_key(r)] = "attack"
return marks
@ -211,8 +203,8 @@ func _marks(v: Dictionary) -> Dictionary:
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":
for mv in _hits_on(_hover_seen.key):
if 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
@ -260,14 +252,15 @@ func _inspect(h: Dictionary) -> void:
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})
hud.inspect({"art": card, "text": text, "key": key})
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]
var type := " · %s" % Style.CLASS_NAMES.get(d.type, d.type) if d.type != "" else ""
var out := "[b]%s[/b]\n[color=%s]%s%s · %d Gb[/color]" % [d.name, Style.hex(Style.card_accent(d)), d.kind.capitalize(), type, d.bandwidth]
if d.kind == "anomaly":
out += "\n%d Cores · %d Health · %d Expansion Slots" % [d.cores, d.health, d.slots]
if d.text != "":
@ -342,7 +335,7 @@ func _pick(key: String, at: Vector2) -> void:
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)
var hits := _hits_on(key)
if hits.size() == 1:
_play(hits[0])
return
@ -355,11 +348,35 @@ func _pick(key: String, at: Vector2) -> void:
_select(key)
## The selected source's moves onto `key`. An Anomaly on the board that isn't one of your sources
## also stands for its location: its card covers most of the tile, so clicking the defender you
## want to attack attacks its location.
func _hits_on(key: String) -> Array:
var hits := moves.filter(func(mv): return mv.source == selected and mv.target == key)
if not hits.is_empty() or moves.any(func(mv): return mv.source == key):
return hits
var r = Moves.ref_of_key(key)
var v := match_.view()
if r == null or r.slot > v.players[r.player].anomalies.size():
return hits
var at = State.anomaly(v, r).at
if at == null:
return hits
return moves.filter(func(mv): return mv.source == selected and mv.target == Moves.loc_key(at))
func _unhandled_input(e: InputEvent) -> void:
if match_ == null:
return
if e is InputEventMouseButton and e.pressed:
if e.button_index == MOUSE_BUTTON_LEFT:
# A click while the game acts itself out skips to the end of the show. On your
# decision it still picks what's under the mouse: the show is of moves already made,
# and a click on your attacker or its target shouldn't be lost to it.
if presenter.busy():
presenter.skip()
if not match_.human_to_act():
return
_pick(table.hovered.get("key", "none"), e.position)
elif e.button_index == MOUSE_BUTTON_RIGHT:
_cancel()
@ -377,10 +394,22 @@ func _unhandled_input(e: InputEvent) -> void:
return
match e.keycode:
KEY_ESCAPE:
if hud.modal_open() and match_.view().choice == null and not match_.is_over():
if hud.hand_focus_open():
hud.set_hand_focus(false)
elif hud.drawer_open() and not hud.modal_open():
hud.set_drawer(false)
elif hud.modal_open() and match_.view().choice == null and not match_.is_over():
hud.close_modal()
_cancel()
KEY_TAB:
if not hud.modal_open():
hud.toggle_hand_focus()
KEY_QUOTELEFT:
hud.toggle_drawer()
KEY_SPACE:
if presenter.busy():
presenter.skip()
return
var v := match_.view()
if v.choice == null and not hud.modal_open():
for t in ["pass", "end"]: