TableTower/core/describe_cards.gd
RobinFiredrake 1735ed09fd Card threads, Morgan's welcome, and typing/scroll/wheel papercuts
- Card threads: a port on each card's bottom edge. Plain runs after any
  result (once the result's own threads finish); Otherwise runs only when
  no thread of the result ran. Saved as card_threads (LOGIC.md 3g).
- Typing on long cards no longer rebuilds the inspector or the card per
  key (~175 ms -> ~7 ms). Timing tool: tests/perf_typing.gd.
- "+ Add result" scrolls to the new row and focuses it.
- Mouse wheel zooms; Shift+wheel pans sideways; scrolling lists keep it.
- "SPREAD" caption over the switcher; Morgan's first-run welcome.
- docs/MECHANICS.md: checklist of table mechanics for planning.

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

139 lines
5.5 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name DescribeCards
extends RefCounted
## Plain-language sentences for cards, options and pipes (see Describe).
## A pick card's dynamic options source: "your Skills, level 1 or more",
## "your Contacts", "your Skill values, where Chosen option is not Pilot".
## "" for a card without options_from; the pattern as written when it's bad.
static func options(table: RollTable, board: TowerBoard) -> String:
if table == null or table.options_from.strip_edges() == "":
return ""
var L := Describe._labels(board)
var p := RollTable.parse_options_from(table.options_from)
var s := ""
match str(p["kind"]):
"prefix":
s = "your " + Describe._label("@%s*" % p["name"], L)
"list":
s = "your " + Describe._label("@" + str(p["name"]), L)
_:
return table.options_from.strip_edges()
var w := _where(table.options_where, L)
if w != "":
s += ", " + w
return Describe._short(s, table.options_description())
## The options_where filter as a short tail: "level 1 or more" for one
## comparison on option_value, else "where <condition>"; "" when empty.
static func _where(where: String, L: Dictionary) -> String:
var src := where.strip_edges()
if src == "":
return ""
var m := Builder.parse_condition(src)
if m["ok"] and (m["clauses"] as Array).size() == 1:
var c: Dictionary = m["clauses"][0]
if str(c["var"]) == "option_value" and ["eq", "ge", "le", "gt", "lt"].has(str(c["cmp"])):
var tail := Describe._tail(str(c["cmp"]), str(c["value"]), L).trim_prefix("is ")
return "level " + tail
return "where " + Describe._condition(src, L)
## A pipe's label in plain words, parts joined with " · ": its inputs ("Tier
## 3"), "only if <condition>" or "otherwise", the count ("×2", "×Terms") and
## "saves <name>". "" for a plain pipe.
static func pipe(entry: TableEntry, target_id: String, board: TowerBoard) -> String:
if entry == null:
return ""
var L := Describe._labels(board)
var parts := PackedStringArray()
var w := entry.with_for(target_id)
for nm: String in w:
parts.append("%s %s" % [Describe._label(nm, L), Describe._expr(str(w[nm]), L)])
var cond := entry.when_for(target_id)
if cond != "":
parts.append("only if " + Describe._condition(cond, L))
elif entry.is_otherwise(target_id):
parts.append("if no thread ran" if entry.id == RollTable.OUTLET_ID else "otherwise")
var ce := entry.count_expr(target_id)
if ce != "":
parts.append("×" + Describe._expr(ce, L))
elif entry.count_for(target_id) != 1:
parts.append("×%d" % entry.count_for(target_id))
if entry.pipe_distinct.has(target_id) and (ce != "" or entry.count_for(target_id) != 1):
parts.append("no repeats")
if entry.dice_for(target_id) != "":
parts.append("with " + entry.dice_for(target_id))
var again := entry.reroll_for(target_id)
if not again.is_empty():
parts.append("again on " + reroll_names(again, board.get_table(target_id) if board else null))
var sv := entry.save_for(target_id)
if sv != "":
parts.append(("adds to " if _is_list(sv, board) else "saves ") + Describe._label(sv, L))
return " · ".join(parts)
## Whether `name` ("@features") is a value a list card declares, so saving
## into it adds an item (RollContext.save_result()).
static func _is_list(name: String, board: TowerBoard) -> bool:
if board == null or not name.strip_edges().begins_with("@"):
return false
var n := name.strip_edges().substr(1).to_lower()
for t in board.tables:
if t.kind == RollTable.KIND_VARIABLE and t.var_name == n:
return t.var_type == "list"
return false
## "Mixed, Binary": the names of the results a thread rolls again on (their
## numbers on a ranged card: "96–00"), in the card's order.
static func reroll_names(ids: Array, t: RollTable) -> String:
if t == null:
return ", ".join(ids)
var out := PackedStringArray()
for i in t.entries.size():
var e := t.entries[i]
if ids.has(e.id):
var label := Formatter.strip_tags(e.text).strip_edges()
out.append(t.num_label(e, i) if e.has_range or label == "" else label)
return ", ".join(out)
## A check card: "Roll 2d6 + Strength modifier, need 8 or more" ("need 5 or
## less" for a roll-under check). "" for other kinds.
static func check(table: RollTable, board: TowerBoard) -> String:
if table == null or table.kind != RollTable.KIND_CHECK:
return ""
var L := Describe._labels(board)
var roll := _with_mod(table.check_dice, table.check_mod, L)
var mod := table.check_mod.strip_edges()
var target := Describe._expr(table.check_target, L)
var num := Expr.is_number(table.check_target.strip_edges())
var need := ""
if table.check_cmp == "le":
need = ("%s or less" % target) if num else "at most " + target
else:
need = ("%s or more" % target) if num else "at least " + target
return Describe._short("Roll %s, need %s" % [roll, need], "Roll %s %s vs %s" % [table.check_dice, mod, table.check_target])
## A ranged roll table's roll: "Roll d20 + Luck", "Roll 2D − Fatigue",
## "Roll d100". "" for other cards.
static func roll(table: RollTable, board: TowerBoard) -> String:
if table == null or table.kind != RollTable.KIND_ROLL or not table.is_ranged():
return ""
return "Roll " + _with_mod(table.dice, table.dice_mod, Describe._labels(board))
## Dice plus a modifier in words: "2D + Strength modifier", "d20 − Fatigue".
static func _with_mod(dice_src: String, mod_src: String, L: Dictionary) -> String:
var out := Describe._expr(dice_src, L)
var mod := mod_src.strip_edges()
if mod != "":
var mt := Builder.tokens(mod)
if not mt.is_empty() and mt[0]["s"] == "-":
out += " %s %s" % [Describe.MINUS, Describe._expr(mod.substr(1), L)]
else:
out += " + " + Describe._expr(mod, L)
return out