TableTower/core/roll_live.gd
RobinFiredrake edb49fce27 Ledger entries are saved on the card and editable on the spread
The entries were only saved values of the spread, so copies, exports and
imports lost them. Now a Ledger card lists its entries (name and starting
value) in the spread file; current values stay saved values. Build mode
edits them on the card too. Older spreads take in the entries made on the
card when they open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:36:03 -06:00

84 lines
4 KiB
GDScript

class_name RollLive
extends RefCounted
## Live values for cards on the board: variable cards' starting values, the
## value a variable card shows right now, and a sheet card's text filled in
## with current values. Roller's and RollTable's matching functions hand
## their work to here.
## A variable card that says "starts at …" gives its variable that value
## the first time a roll needs it: before a top-level roll, each declared
## variable that isn't set yet and has a starting value is set to it (and
## saved like any other change). Variables with no starting value stay unset.
## Ledger entries that aren't set get their starting value the same way.
static func apply_starting_values(board: TowerBoard, ctx: RollContext) -> void:
if board == null:
return
for table in board.tables:
if table.is_collection() and table.var_name != "":
for member: Dictionary in table.members:
var member_value := "@" + LedgerMembers.value_name(table, str(member["name"]))
if ctx.get_value(member_value) == null:
ctx.save(member_value, LedgerMembers.start_value(str(member["start"])), table.id)
continue
if table.kind != RollTable.KIND_VARIABLE or table.var_name == "":
continue
if table.initial.strip_edges() == "" or ctx.get_value("@" + table.var_name) != null:
continue
var start := table.initial_value(ctx)
if str(start.get("error", "")) == "":
ctx.save("@" + table.var_name, start["value"], table.id)
## A context for showing values live (variable and sheet cards, previews):
## nothing is rolled or saved through it by the caller's intent, but dice in
## expressions still roll. `persistent` is the collection's variables.
static func live_context(board: TowerBoard, persistent: Dictionary) -> RollContext:
return Roller.new_context(board, RandomNumberGenerator.new(), persistent, {})
## Live display of a variable card: its current value as text, or its
## starting value (marked) when it isn't set yet. A collection shows its set
## members ("Gun Combat 1, Pilot 0"; set is false when there are none).
## Returns {"text": String, "set": bool, "error": String}.
static func variable_display(board: TowerBoard, persistent: Dictionary, table: RollTable) -> Dictionary:
var ctx := live_context(board, persistent)
if table.is_collection():
var listed := ctx.list_text(table.var_name + "_")
return {"text": listed, "set": listed != "", "error": ""}
var value: Variant = ctx.get_value("@" + table.var_name)
if value != null:
return {"text": Expr.format_value(value), "set": true, "error": ""}
var start := table.initial_value(ctx)
return {"text": Expr.format_value(start["value"]), "set": false, "error": str(start["error"])}
## A variable card's starting value: {"value", "error"}. An empty `initial`
## gives 0 for numbers and "" otherwise. For text and list variables, plain
## words that aren't an expression ("Aslan") are taken as they are.
static func initial_value(table: RollTable, ctx: RollContext) -> Dictionary:
var src := table.initial.strip_edges()
if src == "":
return {"value": 0 if table.var_type == "number" else "", "error": ""}
if table.var_type != "number" and looks_literal(src):
return {"value": src, "error": ""}
var result := Expr.evaluate(src, ctx)
if result["error"] != "":
return {"value": 0 if table.var_type == "number" else "", "error": result["error"]}
return result
## True for plain words that aren't an expression ("Aslan"): no quotes, no
## "@", no brackets, no operators, and not a number.
static func looks_literal(src: String) -> bool:
if src.begins_with("\"") or src.begins_with("'") or src.contains("@") or src.contains("("):
return false
return RegEx.create_from_string("^[^+\\-*/%=<>!&|]*$").search(src) != null and not src.is_valid_float()
## Live rendering of a sheet card's template with current values (BBCode with
## internal markers; [[references]] read as table names, nothing is rolled).
static func render_sheet(board: TowerBoard, persistent: Dictionary, table: RollTable) -> String:
if table.entries.is_empty():
return ""
return TextExpander.render(table.entries[0].text, live_context(board, persistent))