class_name RollCosts extends RefCounted ## 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: 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(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: 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: 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 ## changes and the Why trace). static func pay(e: TableEntry, ctx: RollContext, t: RollTable, node: Dictionary) -> void: if not has_cost(e): return var facts: Array = [] Roller._log(node, Effects.run("%s -= %s" % [var_ref(e), e.cost_amount.strip_edges()], ctx, t.id, facts)) Roller._why(node, facts) ## 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 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("@") static func _number(r: Dictionary) -> float: var v: Variant = r.get("value") return float(v) if r.get("error", "") == "" and (v is int or v is float) else 0.0