diff --git a/core/roll_choice.gd b/core/roll_choice.gd index 2184325..9793c4b 100644 --- a/core/roll_choice.gd +++ b/core/roll_choice.gd @@ -145,6 +145,8 @@ static func default_reply(req: Dictionary) -> Variant: return int(req.get("min", 0)) "text": return "" + "pause": + return true return -1 diff --git a/core/roll_tree.gd b/core/roll_tree.gd index d6cfa23..ba6eece 100644 --- a/core/roll_tree.gd +++ b/core/roll_tree.gd @@ -36,7 +36,7 @@ static func step_of(n: Dictionary, depth: int = 0) -> Dictionary: step["kind"] = n["kind"] if n.has("effects"): step["effects"] = (n["effects"] as Array).duplicate(true) - for k in ["answer", "passes", "pass", "after", "picked", "random", "limit", "cancelled", "deck_empty", "dropped", "kept", "combined", "case", "option", "check", "inputs"]: + for k in ["answer", "passes", "pass", "after", "picked", "random", "limit", "cancelled", "deck_empty", "dropped", "kept", "combined", "stopped_before", "case", "option", "check", "inputs"]: if n.has(k): step[k] = n[k] if n.has("why"): diff --git a/core/roller.gd b/core/roller.gd index 5d51ad4..575ca55 100644 --- a/core/roller.gd +++ b/core/roller.gd @@ -50,7 +50,9 @@ const CANCELLED := "Cancelled" const NO_CASE := "No case matched" const NO_OPTIONS := "No options available" -enum { OP_ROLL, OP_PIPE, OP_LOOP, OP_RESTORE, OP_COMBINE } +enum { OP_ROLL, OP_PIPE, OP_LOOP, OP_RESTORE, OP_COMBINE, OP_PAUSE } +## A paused thread the player chose not to continue. +const STOPPED_HERE := "Stopped here" ## A RollContext wired to this board: it resolves [[names]] against the board's @@ -198,6 +200,8 @@ static func _chain(board: TowerBoard, table_id: String, depth: int, ctx: RollCon var keep: Dictionary = await RollCombine.run(f, ctx) if not keep.is_empty(): _queue_pipes(keep, ctx, stack, int(f["depth"])) + OP_PAUSE: + await _do_pause(board, f, ctx, stack) # A halted roll leaves frames behind: put back any card inputs they hid. while not stack.is_empty(): var rest: Dictionary = stack.pop_back() @@ -352,6 +356,9 @@ static func _queue_pipes(node: Dictionary, ctx: RollContext, stack: Array, depth fr["no_follow"] = true fr["save"] = "" stack.append(fr) + if e.pipe_pause.has(p): + # Runs first: waits for the player before the thread above it. + stack.append({"op": OP_PAUSE, "node": node, "to": p, "depth": depth}) ## Appends why-trace facts to a node's "why". @@ -373,6 +380,22 @@ static func _log(node: Dictionary, lines: Array) -> void: (node["effects"] as Array).append_array(lines) +## A paused thread: asks the player (kind "pause") whether to go on. Stopping +## drops the thread waiting under this frame and marks the result. +static func _do_pause(board: TowerBoard, f: Dictionary, ctx: RollContext, stack: Array) -> void: + var t := board.get_table(str(f["to"])) + var node: Dictionary = f["node"] + var req := {"kind": "pause", "table_id": str(f["to"]), "title": t.name if t else str(f["to"]), + "prompt": "Continue to %s?" % Formatter.strip_tags(t.name if t else str(f["to"])), + "from": Formatter.strip_tags((node["table"] as RollTable).name)} + var reply: Variant = await RollChoice.ask(ctx, req) + if reply == true: + return + if not stack.is_empty() and int((stack.back() as Dictionary)["op"]) == OP_PIPE: + stack.pop_back() + node["stopped_before"] = str(f["to"]) + + static func _do_pipe(board: TowerBoard, f: Dictionary, ctx: RollContext, stack: Array) -> void: if int(f["left"]) <= 0: return diff --git a/core/table_entry.gd b/core/table_entry.gd index 8b64176..a103603 100644 --- a/core/table_entry.gd +++ b/core/table_entry.gd @@ -42,6 +42,9 @@ var page: String = "" ## target table id -> how repeated rolls combine: "sum", "max", "min" or ## "choose" (RollCombine). Missing = keep every roll. var pipe_combine: Dictionary = {} +## target table id -> true: the roll waits before following this thread until +## the player continues (Roller.OP_PAUSE). Missing = no pause. +var pipe_pause: Dictionary = {} ## Router cards: this case's condition. "" is the Otherwise case (always ## matches). Ignored on other kinds. var when: String = "" @@ -216,6 +219,7 @@ func remove_pipe(target: String) -> void: pipe_with.erase(target) pipe_color.erase(target) pipe_combine.erase(target) + pipe_pause.erase(target) func to_dict() -> Dictionary: @@ -241,7 +245,8 @@ func to_dict() -> Dictionary: var wv: Variant = pipe_with.get(p) var col := str(pipe_color.get(p, "")) var comb := combine_for(p) - if expr == "" and n == 1 and s == "" and w == "" and not other and wv == null and col == "" and comb == "all": + var paused := pipe_pause.has(p) + if expr == "" and n == 1 and s == "" and w == "" and not other and wv == null and col == "" and comb == "all" and not paused: list.append(p) else: var po := {"table": p} @@ -261,6 +266,8 @@ func to_dict() -> Dictionary: po["color"] = col if comb != "all": po["combine"] = comb + if paused: + po["pause"] = true list.append(po) d["pipes"] = list if has_range: @@ -292,6 +299,8 @@ static func from_dict(d: Dictionary) -> TableEntry: e.pipe_color[target] = str(p["color"]) if RollCombine.MODES.has(str(p.get("combine", "all"))) and str(p.get("combine", "all")) != "all": e.pipe_combine[target] = str(p["combine"]) + if bool(p.get("pause", false)): + e.pipe_pause[target] = true elif str(p) != "": e.add_pipe(str(p)) var r: Variant = d.get("range") diff --git a/tests/test_roll_features.gd b/tests/test_roll_features.gd index adf356b..b76cb49 100644 --- a/tests/test_roll_features.gd +++ b/tests/test_roll_features.gd @@ -10,6 +10,7 @@ var _failures := 0 func _init() -> void: _test_combine() await _test_choose() + await _test_pause() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) @@ -93,6 +94,24 @@ func _test_choose() -> void: _check(golds[1].get("kept") == "choose" and golds[0].get("dropped", false), "the picked roll is kept") +func _test_pause() -> void: + var b := _board("all") + b.get_table("start").entries[0].pipe_pause["gold"] = true + var rng := RandomNumberGenerator.new() + var tree := Roller.roll(b, "start", rng) + _check(_kids(tree, "gold").size() == 3, "headless rolls continue past a pause") + var ctx := Roller.new_context(b, rng) + var asked: Array = [] + ctx.prompt = func(req: Dictionary) -> Variant: + asked.append(req) + return false + tree = await Roller.roll_async(b, "start", ctx) + _check(asked.size() == 1 and asked[0]["kind"] == "pause", "a paused thread asks once") + _check(_kids(tree, "gold").is_empty() and tree.get("stopped_before") == "gold", "Stop here ends the roll before the thread") + var back := TableEntry.from_dict(b.get_table("start").entries[0].to_dict()) + _check(back.pipe_pause.has("gold"), "the pause is saved") + + func _check(ok: bool, what: String) -> void: if not ok: _failures += 1 diff --git a/tests/test_small_ui.gd b/tests/test_small_ui.gd index 61f9930..2721294 100644 --- a/tests/test_small_ui.gd +++ b/tests/test_small_ui.gd @@ -22,6 +22,7 @@ func _init() -> void: await _test_chip_and_export() await _test_deck_mode() await _test_batch() + await _test_pause_aside() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) @@ -125,6 +126,37 @@ func _test_batch() -> void: (dialogs[dialogs.size() - 1] as ModalCard).close() +## A paused roll waits in the tray; another roll sets it aside; Continue +## brings it back and finishes it. +func _test_pause_aside() -> void: + var road := main.data.get_table("road") + for e in road.entries: + for to in e.pipes: + e.pipe_pause[to] = true + var bandit := road.entries[0] + var tid := bandit.pipes[0] if not bandit.pipes.is_empty() else "" + var weights := [] + for e in road.entries: + weights.append(e.weight) + e.weight = 0 + bandit.weight = 1 + main.rolling.start_roll("road", true) + await _frames(3) + var pauses: RollPauses = main.rolling.pauses + _check(pauses.has_waiting() and main.tray._pause_box.get_child_count() > 0, "the roll waits with Continue in the tray") + main.rolling.start_roll("weather", true) + await _frames(3) + _check(pauses.has_waiting() and str(main.current.get("root", "")) == "weather", "another roll sets the paused one aside") + pauses.answer(0, true) + await _frames(4) + _check(not pauses.has_waiting() and str(main.current.get("root", "")) == "road", "Continue makes it current again") + var reached := (main.current.get("all", []) as Array).any(func(s: Dictionary) -> bool: return str(s.get("table_id", "")) == tid) + _check(tid == "" or reached, "and it goes on past the pause") + for i in road.entries.size(): + road.entries[i].weight = weights[i] + road.entries[i].pipe_pause.clear() + + func _frames(n: int = 2) -> void: for i in n: await process_frame diff --git a/ui/inspector.gd b/ui/inspector.gd index 0ad6627..31e4098 100644 --- a/ui/inspector.gd +++ b/ui/inspector.gd @@ -322,6 +322,17 @@ func set_pipe_count(to: String, s: String) -> void: wrote(selected_table()) +func set_pipe_pause(to: String, on: bool) -> void: + var e := _pipe_entry() + if e == null or e.pipe_pause.has(to) == on: + return + if on: + e.pipe_pause[to] = true + else: + e.pipe_pause.erase(to) + wrote(selected_table()) + + func set_pipe_combine(to: String, mode: String) -> void: var e := _pipe_entry() if e == null or e.combine_for(to) == mode: @@ -660,6 +671,11 @@ func _pipe_box(t: RollTable, e: TableEntry, to: String) -> Control: other.toggled.connect(func(on: bool) -> void: set_pipe_otherwise(to, on)) parts["otherwise"] = other v.add_child(other) + var pause := InspectorKit.check_box("Wait for me first", e.pipe_pause.has(to)) + pause.tooltip_text = "The roll stops here with Continue ▸ in the tray, so you can reveal the next part when you're ready" + pause.toggled.connect(func(on: bool) -> void: set_pipe_pause(to, on)) + parts["pause"] = pause + v.add_child(pause) # Count and saves as. var cr := InspectorKit.flow() diff --git a/ui/main_rolling.gd b/ui/main_rolling.gd index cbeb537..2dce18c 100644 --- a/ui/main_rolling.gd +++ b/ui/main_rolling.gd @@ -13,10 +13,13 @@ var m: Main var _token := 0 var _pulse_tween: Tween var _choice: ChoiceDialog +## Rolls waiting at a paused thread. +var pauses: RollPauses func _init(p_main: Main) -> void: m = p_main + pauses = RollPauses.new(p_main) ## Rolls a table and plays the result down its pipes. A fast roll (Shift held @@ -39,7 +42,9 @@ func start_roll(tid: String, fast: bool = false) -> void: fast = fast or Input.is_key_pressed(KEY_SHIFT) _archive_current() _token += 1 - var my := _token + # The roll's token lives in a holder, so a paused roll set aside can be + # made current again (RollPauses.answer). + var run := {"token": _token} # A question left open by the previous roll answers Cancel; that roll then # sees it has been replaced and stops quietly. cancel_choice() @@ -50,13 +55,14 @@ func start_roll(tid: String, fast: bool = false) -> void: (c as TableCard).set_rolling(false) (c as TableCard).clear_flick() var ctx := Roller.new_context(m.data, m.rng, m.persistent_vars, m.decks) - ctx.prompt = _prompt_for.bind(my) + ctx.prompt = _prompt_for.bind(run) m.rolling_ctx = ctx m.current = {"collection": m.data.id, "root": tid, "root_name": t.name, "time": Time.get_unix_time_from_system(), "steps": [], "all": [], "done": false, "pending": true} var tree: Dictionary = await Roller.roll_async(m.data, tid, ctx) if m.rolling_ctx == ctx: m.rolling_ctx = null + var my := int(run["token"]) if my != _token or not m.is_inside_tree(): # Another roll started while this one waited for an answer. m.local_state._store_decks() @@ -88,14 +94,18 @@ func start_roll(tid: String, fast: bool = false) -> void: ## The roll's prompt hook: lights the steps so far, then asks through ## prompt_hook (the pop-up, or a test's script). A reply for a roll that has ## been replaced in the meantime is turned into Cancel. -func _prompt_for(req: Dictionary, my: int) -> Variant: - if my != _token: +func _prompt_for(req: Dictionary, run: Dictionary) -> Variant: + if int(run["token"]) != _token: return null m.current["prompted"] = true m.current["asking"] = Formatter.strip_tags(str(req.get("title", ""))) show_progress() - var reply: Variant = await m.prompt_hook.call(req) - if my != _token: + var reply: Variant + if str(req.get("kind", "")) == "pause": + reply = await pauses.wait(req, run) + else: + reply = await m.prompt_hook.call(req) + if int(run["token"]) != _token: return null m.current.erase("asking") return reply diff --git a/ui/result_tray.gd b/ui/result_tray.gd index e9bfab4..bdb4ea4 100644 --- a/ui/result_tray.gd +++ b/ui/result_tray.gd @@ -37,6 +37,7 @@ var _earlier: EarlierRolls var side_panel: Control ## Phone layout: shorter labels. var compact := false +var _pause_box: VBoxContainer var _vars: VariablesPanel var _tab_earlier: Button var _tab_vars: Button @@ -105,6 +106,9 @@ func setup(p_main: Main) -> void: _halt.add_child(_halt_label) _halt.hide() head.add_child(_halt) + # Rolls waiting at a paused thread (RollPauses). + _pause_box = VBoxContainer.new() + now.add_child(_pause_box) # A chain that reached a sheet card leads with the rendered sheet. _headline = PanelContainer.new() @@ -233,6 +237,10 @@ func _make_copy_menu() -> MenuButton: func render() -> void: + for c in _pause_box.get_children(): + c.queue_free() + if main.rolling.pauses.has_waiting(): + _pause_box.add_child(main.rolling.pauses.make_strip()) var current := main.current var steps: Array = current.get("steps", []) var fresh_from := 0 diff --git a/ui/roll_pauses.gd b/ui/roll_pauses.gd new file mode 100644 index 0000000..1abc643 --- /dev/null +++ b/ui/roll_pauses.gd @@ -0,0 +1,73 @@ +class_name RollPauses +extends RefCounted +## Paused rolls (threads set to wait, Roller.OP_PAUSE). A roll that reaches +## one waits with "Continue ▸" and "Stop here" in the tray. Starting another +## roll sets the paused one aside, and its chip stays; Continue makes it the +## current roll again and carries on from where it stopped. + +class Waiter: + extends RefCounted + signal done(reply: Variant) + +var m: Main +## {run, waiter, label, current, ctx}, oldest first. +var waiting: Array[Dictionary] = [] + + +func _init(p_main: Main) -> void: + m = p_main + + +## The prompt hook's answer to a "pause" request for roll `run`. +func wait(req: Dictionary, run: Dictionary) -> Variant: + var w := Waiter.new() + var label := "%s: before %s" % [Formatter.strip_tags(str(m.current.get("root_name", ""))), Formatter.strip_tags(str(req.get("title", "")))] + waiting.append({"run": run, "waiter": w, "label": label, "current": m.current, "ctx": m.rolling_ctx}) + m.tray.render() + var reply: Variant = await w.done + return reply + + +## Continue (true) or Stop here (false) for the paused roll at `i`. +func answer(i: int, go_on: bool) -> void: + if i < 0 or i >= waiting.size(): + return + var it: Dictionary = waiting[i] + waiting.remove_at(i) + var run: Dictionary = it["run"] + if int(run["token"]) != m.rolling._token: + # A set-aside roll: it becomes the current roll again. + m.rolling._archive_current() + m.rolling._token += 1 + run["token"] = m.rolling._token + m.current = it["current"] + m.rolling_ctx = it["ctx"] + (it["waiter"] as Waiter).done.emit(go_on) + + +## True while roll `run` (or any, for {}) is waiting. +func has_waiting(run: Dictionary = {}) -> bool: + if run.is_empty(): + return not waiting.is_empty() + return waiting.any(func(it: Dictionary) -> bool: return is_same(it["run"], run)) + + +## The tray's strip of waiting rolls: one row per pause. +func make_strip() -> Control: + var v := VBoxContainer.new() + v.add_theme_constant_override("separation", 6) + for i in waiting.size(): + var row := HBoxContainer.new() + row.add_theme_constant_override("separation", 8) + var l := Style.make_label("⏸ " + str(waiting[i]["label"]), Style.body_bold(), 14, Style.ACCENT) + l.size_flags_vertical = Control.SIZE_SHRINK_CENTER + row.add_child(l) + var go := Style.bar_button("Continue ▸", true) + go.pressed.connect(answer.bind(i, true)) + row.add_child(go) + var stop := Style.bar_button("Stop here") + stop.tooltip_text = "End this roll here; what it rolled so far stays" + stop.pressed.connect(answer.bind(i, false)) + row.add_child(stop) + v.add_child(row) + return v diff --git a/ui/roll_pauses.gd.uid b/ui/roll_pauses.gd.uid new file mode 100644 index 0000000..5ce67c8 --- /dev/null +++ b/ui/roll_pauses.gd.uid @@ -0,0 +1 @@ +uid://d3ifk8kxg7y67 diff --git a/ui/tray_text.gd b/ui/tray_text.gd index 8983c0b..ea12687 100644 --- a/ui/tray_text.gd +++ b/ui/tray_text.gd @@ -77,6 +77,9 @@ static func caption_for(step: Dictionary, board: TowerBoard = null, plain: bool caption += " · kept, your choice" if step.get("dropped", false): caption += " · set aside" + if str(step.get("stopped_before", "")) != "" and board != null: + var nt := board.get_table(str(step["stopped_before"])) + caption += " · stopped before %s" % Formatter.strip_tags(nt.name if nt else str(step["stopped_before"])) if str(step.get("save_as", "")) != "": if plain: caption += " · saved as %s" % Describe.label(str(step["save_as"]), board)