TableTower/core/describe_cards.gd

107 lines
4.1 KiB
GDScript3
Raw Normal View History

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))
var sv := entry.save_for(target_id)
if sv != "":
parts.append("saves " + Describe._label(sv, L))
return " · ".join(parts)
## 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