diff --git a/core/roll_costs.gd b/core/roll_costs.gd index 8b84d3f..9340a28 100644 --- a/core/roll_costs.gd +++ b/core/roll_costs.gd @@ -1,30 +1,31 @@ class_name RollCosts extends RefCounted -## Costs on choice options: an option can cost an amount of a value -## (TableEntry.cost_value, cost_amount). It's offered greyed out, with the -## reason, when the value is too low; picking it takes the amount away. -## A "Reroll?" option that pipes back to a card and costs a point makes a -## spend-a-point-to-reroll table. +## Costs: a choice option (TableEntry) or a whole card (RollTable) can cost an +## amount of a value (cost_value, cost_amount). An option is offered greyed +## out, with the reason, when the value is too low; picking it takes the +## amount away. A "Reroll?" option that pipes back to a card and costs a point +## makes a spend-a-point-to-reroll table. A card with a cost can't be rolled +## without it; a thread that reaches it asks first (card_gate). ## "" when the option can be afforded now, else why not (shown to the player). -static func blocked(e: TableEntry, ctx: RollContext, board: TowerBoard = null) -> String: +static func blocked(e: Object, ctx: RollContext, board: TowerBoard = null) -> String: if not has_cost(e): return "" var have := _number(Expr.evaluate(var_ref(e), ctx)) - var need := _number(Expr.evaluate(e.cost_amount, ctx)) + var need := _number(Expr.evaluate(str(e.get("cost_amount")), ctx)) if have >= need: return "" return "Needs %s %s (you have %s)" % [Expr.format_value(need), label(e, board), Expr.format_value(have)] -static func has_cost(e: TableEntry) -> bool: - return e.cost_value.strip_edges() != "" and e.cost_amount.strip_edges() != "" +static func has_cost(e: Object) -> bool: + return str(e.get("cost_value")).strip_edges() != "" and str(e.get("cost_amount")).strip_edges() != "" ## "Costs 1 Luck". -static func describe(e: TableEntry, board: TowerBoard = null) -> String: - return "Costs %s %s" % [e.cost_amount.strip_edges(), label(e, board)] if has_cost(e) else "" +static func describe(e: Object, board: TowerBoard = null) -> String: + return "Costs %s %s" % [str(e.get("cost_amount")).strip_edges(), label(e, board)] if has_cost(e) else "" ## Takes the cost away (an ordinary change, so it shows in the result's @@ -37,12 +38,47 @@ static func pay(e: TableEntry, ctx: RollContext, t: RollTable, node: Dictionary) Roller._why(node, facts) -static func var_ref(e: TableEntry) -> String: - var v := e.cost_value.strip_edges() - return v if v.begins_with("@") else "@" + v +## A card's cost before it rolls. A roll that starts on the card (its +## button) pays when it can, else stops with the reason. A thread that reaches +## it asks the player first (a "pause" question: Continue pays). Returns false +## when the card doesn't roll; then a stop node says why. Payment shows as a +## change on the card's result (frame "pre_why"). +static func card_gate(board: TowerBoard, t: RollTable, f: Dictionary, ctx: RollContext) -> bool: + if not has_cost(t): + return true + var reason := blocked(t, ctx, board) + var cost := describe(t, board).to_lower() + if not f.get("top", false): + if reason != "": + Roller._attach(f, Roller._stop(t, "Not rolled: %s" % reason.to_lower()), ctx) + return false + var request := {"kind": "pause", "table_id": t.id, "title": t.name, "cost": cost, + "prompt": "Pay %s to roll %s?" % [cost.trim_prefix("costs "), Formatter.strip_tags(t.name)], + "from": Formatter.strip_tags(t.name)} + var reply: Variant = await RollChoice.ask(ctx, request) + if reply != true: + Roller._attach(f, Roller._stop(t, "Not rolled: you didn't pay"), ctx) + return false + elif reason != "": + Roller._attach(f, Roller._stop(t, "Can't roll: %s" % reason.to_lower()), ctx) + return false + var facts: Array = [] + var lines := Effects.run("%s -= %s" % [var_ref(t), t.cost_amount.strip_edges()], ctx, t.id, facts) + var pre_why: Array = f.get("pre_why", []) + pre_why.append_array(facts) + f["pre_why"] = pre_why + var pre_log: Array = f.get("pre_log", []) + pre_log.append_array(lines) + f["pre_log"] = pre_log + return true -static func label(e: TableEntry, board: TowerBoard) -> String: +static func var_ref(e: Object) -> String: + var value_name := str(e.get("cost_value")).strip_edges() + return value_name if value_name.begins_with("@") else "@" + value_name + + +static func label(e: Object, board: TowerBoard) -> String: return Describe.label(var_ref(e).trim_prefix("@"), board) if board else var_ref(e).trim_prefix("@") diff --git a/core/roll_nodes.gd b/core/roll_nodes.gd index 846e7a8..9cdab0b 100644 --- a/core/roll_nodes.gd +++ b/core/roll_nodes.gd @@ -20,11 +20,13 @@ static func attach(frame: Dictionary, node: Dictionary, ctx: RollContext) -> voi node["save_as"] = frame["save"] if frame.has("inputs"): node["inputs"] = frame["inputs"] - var facts: Array = frame["pre_why"] + # Facts from before the pick (inputs, a paid cost) come first. + if frame.has("pre_why"): + var facts: Array = (frame["pre_why"] as Array).duplicate() if node.has("why"): facts.append_array(node["why"]) node["why"] = facts - add_log(node, frame["pre_log"]) + add_log(node, frame.get("pre_log", [])) (frame["parent"]["children"] as Array).append(node) if frame.get("top", false): ctx.root = node diff --git a/core/roll_table.gd b/core/roll_table.gd index e1a0d73..8e0de59 100644 --- a/core/roll_table.gd +++ b/core/roll_table.gd @@ -120,6 +120,10 @@ var var_type: String = "number" var initial: String = "" ## Scale variables: the ordered steps, lowest first. var steps: Array[String] = [] +## A cost to roll this card (RollCosts): the value ("@luck") and the amount +## taken away each time. Empty = free. +var cost_value: String = "" +var cost_amount: String = "" ## List cards: true when the list keeps repeats (an item added every time, in ## order); false adds an item only if it isn't there yet. var list_repeats := false diff --git a/core/roll_table_io.gd b/core/roll_table_io.gd index 06cf31e..92d4ece 100644 --- a/core/roll_table_io.gd +++ b/core/roll_table_io.gd @@ -19,6 +19,8 @@ static func to_dict(t: RollTable) -> Dictionary: d["tags"] = t.tags.duplicate() if t.color != "": d["color"] = t.color + if t.cost_value.strip_edges() != "" and t.cost_amount.strip_edges() != "": + d["cost"] = {"value": t.cost_value.strip_edges(), "amount": t.cost_amount.strip_edges()} if t.roll_floor.strip_edges() != "": d["roll_floor"] = t.roll_floor.strip_edges() if t.roll_ceiling.strip_edges() != "": @@ -141,6 +143,10 @@ static func from_dict(d: Dictionary) -> RollTable: t.steps = Scales.clean(d.get("steps", [])) t.members = LedgerMembers.clean(d.get("members", [])) t.list_repeats = bool(d.get("repeats", false)) + var cost: Variant = d.get("cost") + if cost is Dictionary: + t.cost_value = str(cost.get("value", "")) + t.cost_amount = str(cost.get("amount", "")) t.check_dice = RollTable.expr_text(d.get("check_dice", RollTable.CHECK_DEFAULT_DICE), RollTable.CHECK_DEFAULT_DICE).strip_edges() if t.check_dice == "": t.check_dice = RollTable.CHECK_DEFAULT_DICE diff --git a/core/roller.gd b/core/roller.gd index 1243887..7ec9f41 100644 --- a/core/roller.gd +++ b/core/roller.gd @@ -216,6 +216,8 @@ static func _do_roll(board: TowerBoard, f: Dictionary, ctx: RollContext, stack: if not _take_action(f, t, ctx): return RollPipes.bind_inputs(t, f, ctx, stack) + if not await RollCosts.card_gate(board, t, f, ctx): + return if t.entries.is_empty(): _attach(f, _stop(t, "This table has no results"), ctx) return diff --git a/help/topics.txt b/help/topics.txt index 7558340..d2a1436 100644 --- a/help/topics.txt +++ b/help/topics.txt @@ -233,11 +233,12 @@ The [b]Book[/b] holds [b]Pages[/b]: results you want to use on more than one tab [demo]repeats[/demo] # Pausing a roll -Tick [b]Wait for me first[/b] on a thread and the roll stops there, with [b]Continue ▸[/b] and [b]Stop here[/b] in the tray. Use it to reveal something a piece at a time. If you roll something else meanwhile, the paused roll waits in the tray until you continue it. +Tick [b]Wait for me first[/b] on a thread and the roll stops there. The roll so far plays out first, then a pop-up asks [b]Continue ▸[/b], [b]Stop here[/b] or [b]Later[/b] (the same buttons wait in the tray). After Continue the rest of the roll plays out too. Use it to reveal something a piece at a time. If you roll something else meanwhile, the paused roll waits in the tray until you continue it. [demo]pause[/demo] # Options that cost An option on a [b]Which option[/b] card can cost something: in its inspector, [b]Costs [1] of [Points][/b]. Picking it takes that away; when there isn't enough, the option is greyed out and says why. An option such as [b]Reroll[/b] that threads back to the loot card and costs a point gives players a spend-a-point reroll. +- A whole card can cost something too: [b]Costs to roll[/b] in the card's inspector. Its roll button is greyed out when there isn't enough, and pressing it pays. When a thread reaches the card, the roll asks first: [b]Pay ▸[/b] rolls it, [b]Don't pay[/b] skips it. [demo]costs[/demo] # One table adjusting another diff --git a/tests/test_roll_features.gd b/tests/test_roll_features.gd index 1297d71..3d71952 100644 --- a/tests/test_roll_features.gd +++ b/tests/test_roll_features.gd @@ -11,6 +11,7 @@ func _init() -> void: await _test_choose() await _test_pause() await _test_costs() + await _test_card_costs() _test_limits() finish() @@ -136,6 +137,34 @@ func _test_pause() -> void: _check(back.pipe_pause.has("gold"), "the pause is saved") +## A card that costs 1 point to roll: its own roll pays or stops; a thread +## that reaches it asks first (Continue pays, Stop skips it). +func _test_card_costs() -> void: + var b := _board("all") + var gold := b.get_table("gold") + gold.cost_value = "@points" + gold.cost_amount = "1" + var ctx := Roller.new_context(b, RandomNumberGenerator.new(), {"points": {"value": 2}}) + var tree: Dictionary = await Roller.roll_async(b, "gold", ctx) + _check(not tree.has("stop") and int(ctx.lookup("@points")) == 1, "rolling the card itself pays its cost") + _check((tree.get("why", []) as Array).any(func(f: Dictionary) -> bool: return f.get("type") == "effect"), "the payment shows in Why this?") + ctx = Roller.new_context(b, RandomNumberGenerator.new(), {"points": {"value": 0}}) + tree = await Roller.roll_async(b, "gold", ctx) + _check(str(tree.get("stop", "")).begins_with("Can't roll"), "with nothing to pay it doesn't roll: %s" % tree.get("stop", "")) + var asked: Array = [] + ctx = Roller.new_context(b, RandomNumberGenerator.new(), {"points": {"value": 5}}) + ctx.prompt = func(req: Dictionary) -> Variant: + asked.append(req) + return asked.size() == 1 + tree = await Roller.roll_async(b, "start", ctx) + _check(asked.size() == 3 and str(asked[0].get("cost", "")) != "", "a thread into it asks before each roll") + var golds := _kids(tree, "gold") + _check(golds.size() == 3 and not golds[0].has("stop") and golds[1].has("stop"), "Continue pays and rolls; not paying skips it") + _check(int(ctx.lookup("@points")) == 4, "only the paid roll took a point") + var back := RollTable.from_dict(gold.to_dict()) + _check(back.cost_value == "@points" and back.cost_amount == "1", "the card's cost is saved") + + ## A pick card whose "Reroll" option costs 1 point. func _test_costs() -> void: var b := TowerBoard.new() diff --git a/ui/inspector_card.gd b/ui/inspector_card.gd index f75242f..bb3bbd9 100644 --- a/ui/inspector_card.gd +++ b/ui/inspector_card.gd @@ -59,6 +59,8 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: insp.wrote(t)))) if t.kind != RollTable.KIND_VARIABLE: + box.add_child(InspectorKit.field("Costs to roll", InspectorResult.cost_row(insp, t, t, + "Its roll button is greyed out when there isn't enough; a thread that reaches it asks first."))) box.add_child(_inputs(insp, t)) InspectorThreads.build(insp, box, t) diff --git a/ui/inspector_result.gd b/ui/inspector_result.gd index 71a447e..3097d46 100644 --- a/ui/inspector_result.gd +++ b/ui/inspector_result.gd @@ -28,27 +28,29 @@ static func book_line(insp: Inspector, t: RollTable, e: TableEntry) -> Control: ## "[1] of [Points ▾]": what picking this option costs (RollCosts). -static func cost_row(insp: Inspector, t: RollTable, e: TableEntry) -> Control: +## "Costs [1] of [value]": for a choice option (`holder` a TableEntry) or a +## whole card (`holder` the RollTable). `note` sits under it. +static func cost_row(insp: Inspector, t: RollTable, holder: Object, note: String = "Greyed out in the pop-up when there isn't enough.") -> Control: var row := InspectorKit.flow() - var amount := InspectorKit.amount(e.cost_amount, "0") - amount.tooltip_text = "How much picking this takes away (0 or empty: free)" + var amount := InspectorKit.amount(str(holder.get("cost_amount")), "0") + amount.tooltip_text = "How much it takes away (0 or empty: free)" row.add_child(amount) row.add_child(InspectorKit.word("of")) - var picker := ValuePicker.new(insp.main, e.cost_value.trim_prefix("@")) + var picker := ValuePicker.new(insp.main, str(holder.get("cost_value")).trim_prefix("@")) row.add_child(picker) var apply := func() -> void: - var a := amount.text.strip_edges() - e.cost_amount = "" if a == "" or a == "0" else a - if e.cost_amount == "": - e.cost_value = "" + var typed := amount.text.strip_edges() + holder.set("cost_amount", "" if typed == "" or typed == "0" else typed) + if str(holder.get("cost_amount")) == "": + holder.set("cost_value", "") insp.wrote(t) amount.text_changed.connect(func(_s: String) -> void: apply.call()) picker.value_chosen.connect(func(n: String) -> void: - e.cost_value = "@" + n.trim_prefix("@") + holder.set("cost_value", "@" + n.trim_prefix("@")) if amount.text.strip_edges() == "": amount.text = "1" apply.call()) - row.add_child(InspectorKit.note("Greyed out in the pop-up when there isn't enough.", Style.MUTED, 12)) + row.add_child(InspectorKit.note(note, Style.MUTED, 12)) return row diff --git a/ui/main_rolling.gd b/ui/main_rolling.gd index a219f35..8881f96 100644 --- a/ui/main_rolling.gd +++ b/ui/main_rolling.gd @@ -10,6 +10,8 @@ var m: Main var _token := 0 var _pulse_tween: Tween +## True when _play_one lit nodes again (after a pause) without redrawing yet. +var _relit := false var _choice: ChoiceDialog ## Rolls waiting at a paused thread. var pauses: RollPauses @@ -51,7 +53,7 @@ func start_roll(tid: String, fast: bool = false) -> void: m.sound.play("roll") # 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} + var run := {"token": _token, "fast": fast} # A question left open by the previous roll answers Cancel; that roll then # sees it has been replaced and stops quietly. cancel_choice() @@ -76,7 +78,9 @@ func start_roll(tid: String, fast: bool = false) -> void: if ctx.persistent_changed: m.local_state._store_variables() return - var prompted: bool = m.current.get("prompted", false) + # Questions light the roll so far at once, so the rest shows at once too; + # a pause (or a cost) animates up to itself and the rest animates after. + var prompted: bool = run.get("other_prompt", false) m.lit_entries.clear() m.lit_pipes.clear() m.lit_texts.clear() @@ -111,11 +115,27 @@ func _prompt_for(req: Dictionary, run: Dictionary) -> Variant: return null m.current["prompted"] = true m.current["asking"] = Formatter.strip_tags(str(req.get("title", ""))) - show_progress() var reply: Variant if str(req.get("kind", "")) == "pause": + if run.get("fast", false) or m.rolling_ctx == null: + show_progress() + else: + # The roll so far animates (dice, lights, threads), then it waits. + # Steps shown before an earlier pause are listed again as they replay, + # and the previous roll's lights go out first. + m.current["steps"] = [] + m.lit_entries.clear() + m.lit_pipes.clear() + m.lit_texts.clear() + m.lit_columns.clear() + m._refresh_lit() + await _play_node(m.rolling_ctx.root, int(run["token"])) + if int(run["token"]) != _token: + return null reply = await pauses.wait(req, run) else: + run["other_prompt"] = true + show_progress() reply = await m.prompt_hook.call(req) if int(run["token"]) != _token: return null @@ -189,9 +209,16 @@ func _play_node(root_node: Dictionary, my: int) -> void: if my != _token or n.is_empty(): return var key: String = top[2] + var replay: bool = n.get("animated", false) + if not replay and _relit: + # Nodes shown before a pause were lit without redrawing; do it once. + _relit = false + m._refresh_lit() + m.tray.render() if key != "": m.lit_pipes[key] = true - await _pulse(key, my) + if not replay: + await _pulse(key, my) if my != _token: return await _play_one(n, my, int(top[1])) @@ -206,14 +233,21 @@ func _play_node(root_node: Dictionary, my: int) -> void: var c: Dictionary = kids[k] var ck := "" if (c.has("pass") or c.has("after")) else Roller.link_key(tid, eid, c) stack.append([c, int(top[1]) + 1, ck]) + if _relit: + _relit = false + m._refresh_lit() + m.tray.render() -## Animates one node: the card tumbles (plain rolls only), then the result lights. +## Animates one node: the card tumbles (plain rolls only), then the result +## lights. A node already animated (before a pause) only lights again, at once. func _play_one(n: Dictionary, my: int, depth: int) -> void: var t: RollTable = n["table"] var stopped := n.has("stop") var card := m.card_for(t.id) - var tumble: bool = not n.has("kind") or bool(n.get("random", false)) + var replay: bool = n.get("animated", false) + n["animated"] = true + var tumble: bool = (not n.has("kind") or bool(n.get("random", false))) and not replay # A table hidden in a collapsed group rolls on the group's card. var gcard: GroupCard = m.hidden_groups().get(t.id) if not stopped and gcard and tumble: @@ -248,12 +282,16 @@ func _play_one(n: Dictionary, my: int, depth: int) -> void: m.lit_texts[ek] = str(n.get("summary", e.text)) TableCardColumns.note_lit(m, ek, n) deck_reveal.release(t.id, e.id) - m._refresh_lit() - if card: + if replay: + _relit = true + else: + m._refresh_lit() + if card and not replay: card.reveal(e.id) card.sparkle(e.id) (m.current["steps"] as Array).append(Roller.step_of(n, depth)) - m.tray.render() + if not replay: + m.tray.render() ## Shows the whole current roll at once: every result and pipe on its path lit, diff --git a/ui/roll_pauses.gd b/ui/roll_pauses.gd index 1abc643..7f842f8 100644 --- a/ui/roll_pauses.gd +++ b/ui/roll_pauses.gd @@ -21,19 +21,43 @@ func _init(p_main: Main) -> void: ## The prompt hook's answer to a "pause" request for roll `run`. func wait(req: Dictionary, run: Dictionary) -> Variant: var w := Waiter.new() + var cost := str(req.get("cost", "")) 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}) + if cost != "": + label = str(req.get("prompt", label)) + var item := {"run": run, "waiter": w, "label": label, "current": m.current, "ctx": m.rolling_ctx, "cost": cost != ""} + waiting.append(item) m.tray.render() + item["dialog"] = _popup(item) var reply: Variant = await w.done return reply +## The pop-up for a waiting roll: Continue / Stop here (Pay / Don't pay for a +## cost), or Later, which leaves it waiting in the tray's strip. +func _popup(item: Dictionary) -> ModalCard: + var dialog := ModalCard.new("Pay to roll?" if item["cost"] else "Paused", 420.0) + dialog.body.add_child(ModalCard.note(str(item["label"]) if item["cost"] else "The roll is waiting %s." % str(item["label"]).get_slice(": ", 1), Style.INK)) + var reply := func(go_on: bool) -> void: + var at := waiting.find(item) + if at >= 0: + answer(at, go_on) + dialog.add_button("Later", "plain", dialog.close) + dialog.add_button("Don't pay" if item["cost"] else "Stop here", "plain", reply.bind(false)) + dialog.add_button("Pay ▸" if item["cost"] else "Continue ▸", "primary", reply.bind(true)) + m._add_modal(dialog) + return dialog + + ## 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 dialog: ModalCard = it.get("dialog") + if dialog and is_instance_valid(dialog) and dialog.is_inside_tree(): + dialog.close() var run: Dictionary = it["run"] if int(run["token"]) != m.rolling._token: # A set-aside roll: it becomes the current roll again. @@ -62,11 +86,12 @@ func make_strip() -> Control: 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) + var is_cost: bool = waiting[i].get("cost", false) + var go := Style.bar_button("Pay ▸" if is_cost else "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" + var stop := Style.bar_button("Don't pay" if is_cost else "Stop here") + stop.tooltip_text = "Skip this card; what the roll gave so far stays" if is_cost else "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) diff --git a/ui/table_card_header.gd b/ui/table_card_header.gd index a57301c..2f8b238 100644 --- a/ui/table_card_header.gd +++ b/ui/table_card_header.gd @@ -212,7 +212,29 @@ func _make_kind_header(_build: bool) -> Control: ## Re-renders the header's live labels: a dynamic pick's available count and a ## choice's question. +## Greys out the roll button while the card can't roll: a range runs +## backwards, or it costs more than the value has (the tooltip says why). +func sync_roll_button() -> void: + if not is_instance_valid(roll_button): + return + var table := card.table + var reason := "a range runs backwards" if RollRange.backwards(table) != "" else "" + if reason == "" and RollCosts.has_cost(table): + reason = RollCosts.blocked(table, Roller.live_context(card.main.data, card.main.persistent_vars), card.main.data) + roll_button.disabled = reason != "" + roll_button.modulate.a = 0.4 if reason != "" else 1.0 + if not roll_button.has_meta("base_tip"): + roll_button.set_meta("base_tip", roll_button.tooltip_text) + var tip := str(roll_button.get_meta("base_tip")) + if RollCosts.has_cost(table): + tip += "\n" + RollCosts.describe(table, card.main.data) + if reason != "": + tip += "\nCan't roll: " + reason + roll_button.tooltip_text = tip + + func refresh_live() -> void: + sync_roll_button() var table := card.table var main := card.main if is_instance_valid(options_label): diff --git a/ui/table_card_skin.gd b/ui/table_card_skin.gd index 932e7eb..bfd7150 100644 --- a/ui/table_card_skin.gd +++ b/ui/table_card_skin.gd @@ -67,10 +67,7 @@ func update_warning() -> void: problems = ("%s\n" % problems if problems != "" else "") + "\n".join(lines) card._warn_label.text = problems card._warn_panel.visible = problems != "" - if is_instance_valid(card.header_ui.roll_button): - var back := RollRange.backwards(card.table) != "" - card.header_ui.roll_button.disabled = back - card.header_ui.roll_button.modulate.a = 0.4 if back else 1.0 + card.header_ui.sync_roll_button() func skin_scrollbar(bar: VScrollBar) -> void: