TableTower/core/describe_dice.gd
RobinFiredrake b604553364 Size audit 2: split table_entry and describe
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:29:00 -06:00

90 lines
3.1 KiB
GDScript
Raw 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 DescribeDice
extends RefCounted
## Turns a dice or count expression into plain words for Describe:
## "2d6 + dm(@str)" -> "2d6 + Strength modifier", "1d6 + tier" -> "d6 + Tier".
## It reads the expression as tokens (small pieces such as "2d6", "+", "@str"),
## swaps each piece for its words, then joins them with tidy spacing.
## Describe.dice() and Describe._expr() delegate here.
## Maths signs shown as their print forms ("−" is Describe.MINUS).
const OPS := {"*": "×", "/": "÷", "-": "−", "==": "=", "!=": "≠", ">=": "≥", "<=": "≤"}
## The expression in words, using a label table from DescribeLabels.build().
## Text that can't be read comes back as it is.
static func words(expr: String, labels: Dictionary) -> String:
var source := expr.strip_edges()
if source == "":
return ""
var tokens := Builder.tokens(source)
if tokens.is_empty():
return source
return Describe._short(_join(_pieces(tokens, labels)), source)
## Each token as a [kind, text] piece; kinds: val fn op neg ( ) ,
static func _pieces(tokens: Array, labels: Dictionary) -> Array:
var pieces: Array = []
var index := 0
while index < tokens.size():
var token: Dictionary = tokens[index]
var token_type := str(token["t"])
var token_text := str(token["s"])
var prev_kind := str(pieces.back()[0]) if not pieces.is_empty() else ""
# dm(x) with one value inside: "X modifier".
if token_type == "name" and token_text == "dm" and index + 3 < tokens.size() \
and tokens[index + 1]["s"] == "(" and tokens[index + 3]["s"] == ")" \
and ["var", "name", "num"].has(str(tokens[index + 2]["t"])):
pieces.append(["val", Describe._value(str(tokens[index + 2]["s"]), labels) + " modifier"])
index += 4
continue
index += 1
match token_type:
"dice":
var dice_parts := Builder.dice_parts(token_text)
if int(dice_parts[0]) == 1:
pieces.append(["val", "d%d" % int(dice_parts[1])])
else:
pieces.append(["val", "%dd%d" % [int(dice_parts[0]), int(dice_parts[1])]])
"var", "prefix":
pieces.append(["val", DescribeLabels.lookup(token_text, labels)])
"name":
if index < tokens.size() and tokens[index]["s"] == "(":
pieces.append(["fn", token_text])
else:
pieces.append(["val", Describe._value(token_text, labels)])
"num":
pieces.append(["val", token_text])
"str":
pieces.append(["val", Builder.unquote(token_text)])
"kw":
pieces.append(["op", token_text])
_:
if token_text == "(" or token_text == ")" or token_text == ",":
pieces.append([token_text, token_text])
elif token_text == "-" and prev_kind in ["", "op", "(", ",", "neg"]:
pieces.append(["neg", "−"])
else:
pieces.append(["op", OPS.get(token_text, token_text)])
return pieces
## Joins the pieces with a space where a reader expects one.
static func _join(pieces: Array) -> String:
var out := ""
var prev := ""
for piece: Array in pieces:
var kind := str(piece[0])
var space := false
match kind:
"op":
space = out != ""
"val", "fn":
space = prev in ["op", ",", "val", ")"]
"(", "neg":
space = prev in ["op", ","]
if space:
out += " "
out += str(piece[1])
prev = kind
return out