- 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>
65 lines
2.3 KiB
GDScript
65 lines
2.3 KiB
GDScript
class_name Scales
|
|
extends RefCounted
|
|
## Scale values (docs/LOGIC.md §5.4): a variable card with var_type "scale"
|
|
## lists ordered steps ("Supercold", "Cold", "Temperate", "Hot", "Superhot").
|
|
##
|
|
## - The value is stored as the step's name, so {@climate} shows "Cold".
|
|
## - In rules it reads as the step's number (1 = the first step), and a quoted
|
|
## step name next to it reads as that step's number too, so
|
|
## @atmosphere >= "Thin" and @climate = toward(@climate, "Temperate") work.
|
|
## - Saving a number picks that step, stopping at the ends; saving a name
|
|
## matches the steps without caring about capitals.
|
|
|
|
|
|
## The step whose position (1-based) is `n`, kept within the ends. "" when
|
|
## there are no steps.
|
|
static func label_at(steps: Array, n: int) -> String:
|
|
if steps.is_empty():
|
|
return ""
|
|
return str(steps[clampi(n, 1, steps.size()) - 1])
|
|
|
|
|
|
## The position (1-based) of a step name, or 0 when it isn't one. Numbers are
|
|
## taken as positions as they are.
|
|
static func position_of(steps: Array, v: Variant) -> int:
|
|
if v is int or v is float:
|
|
return int(v)
|
|
var s := str(v).strip_edges().to_lower()
|
|
for k in steps.size():
|
|
if str(steps[k]).to_lower() == s:
|
|
return k + 1
|
|
return 0
|
|
|
|
|
|
## What a scale variable stores for `v`: a number becomes the step at that
|
|
## position (kept within the ends), a step name its canonical spelling, and
|
|
## anything else is kept as it is.
|
|
static func store(steps: Array, v: Variant) -> Variant:
|
|
if steps.is_empty():
|
|
return v
|
|
if v is int or v is float or (v is String and (v as String).is_valid_float()):
|
|
return label_at(steps, roundi(float(v)))
|
|
var k := position_of(steps, v)
|
|
return str(steps[k - 1]) if k > 0 else v
|
|
|
|
|
|
## Moves position `a` one step toward position `b`.
|
|
static func toward(a: Variant, b: Variant) -> int:
|
|
var x := int(Expr.to_value(a)) if Expr.is_number(Expr.to_value(a)) else 0
|
|
var y := int(Expr.to_value(b)) if Expr.is_number(Expr.to_value(b)) else 0
|
|
return x + signi(y - x)
|
|
|
|
|
|
## Cleans a "steps" value from JSON or the UI: trimmed names, blanks dropped.
|
|
static func clean(v: Variant) -> Array[String]:
|
|
var out: Array[String] = []
|
|
var items: Array = []
|
|
if v is Array:
|
|
items = v
|
|
elif v is String:
|
|
items = Array((v as String).split("\n"))
|
|
for item: Variant in items:
|
|
var s := str(item).strip_edges()
|
|
if s != "":
|
|
out.append(s)
|
|
return out
|