TableTower/core/roll_costs.gd
RobinFiredrake 25018ebdf3 Costs on choice options
A pick option can cost an amount of a value (TableEntry.cost_value and
cost_amount, JSON "cost"; the inspector's Costs row). The pop-up shows
the cost, and greys the option out with the reason when there isn't
enough; Random never picks it. Picking it takes the amount away as an
ordinary change (RollCosts). A costly Reroll option that threads back to a
card gives spend-a-point rerolls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:12:06 -06:00

51 lines
1.9 KiB
GDScript

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.
## "" 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:
if not has_cost(e):
return ""
var have := _number(Expr.evaluate(var_ref(e), ctx))
var need := _number(Expr.evaluate(e.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() != ""
## "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 ""
## 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)
static func var_ref(e: TableEntry) -> String:
var v := e.cost_value.strip_edges()
return v if v.begins_with("@") else "@" + v
static func label(e: TableEntry, 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