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 presenter: Presenter var settings := GameSettings.DEFAULTS.duplicate() ## 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 := {} ## 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: pool = Cards.load_cards() table = GameTable.new() 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)) 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) 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"}) ## 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 _bot_wait = 0.0 # 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() 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 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() 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") 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() ## 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() # 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() presenter.run() 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), "show_left": presenter.time_left()}) ## 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" # 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 ## 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 _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 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, "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 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 != "": 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 := _hits_on(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) ## 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() 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.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"]: 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()