- Scales: var_type scale with steps; stored as step names, read as step numbers in rules (a quoted step name beside a scale reads as its number); += / -= move steps and stop at the ends; toward(). - Columns: RollTable.columns / column_rolls (row or each), TableEntry.cells; RollColumns fills the other columns, saves each, honours a column's when. - Threads: dice (roll with other dice instead) and reroll (result ids). - RollTable save/load split into RollTableIO; Expr's base into ExprBase. - RollLintBook checks; JSON canon keeps the new keys; tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
4.9 KiB
GDScript
125 lines
4.9 KiB
GDScript
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("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.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("saves " + Describe._label(sv, L))
|
||
return " · ".join(parts)
|
||
|
||
|
||
## "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
|