TableTower/core/describe_scales.gd

43 lines
1.6 KiB
GDScript3
Raw Normal View History

class_name DescribeScales
extends RefCounted
## Scale values in plain words (Describe, Scales): "Climate 1 step up",
## "Climate 2 steps down", "Climate = Hot", "Climate 1 step toward
## Temperate", "Atmosphere at least Medium", "Atmosphere at most Thin". A
## step's number in a rule reads as its name ("@climate = 2" -> "Climate =
## Cold").
## The phrase for a scale model (BuilderScales.refine'd), or "" when the op
## isn't a scale one. `steps` are the value's steps; `expr` turns an amount
## expression into words (Describe._expr).
static func effect(m: Dictionary, subj: String, steps: Array, expr: Callable) -> String:
var amount := str(m["amount"])
match str(m["op"]):
"step_up", "step_down":
var up: bool = m["op"] == "step_up"
if amount.is_valid_int():
var n := int(amount)
if n < 0:
up = not up
n = -n
return "%s %d %s %s" % [subj, n, "step" if n == 1 else "steps", "up" if up else "down"]
return "%s %s steps %s" % [subj, expr.call(amount), "up" if up else "down"]
"set":
return "%s = %s" % [subj, step_word(amount, steps, expr)]
"toward":
return "%s 1 step toward %s" % [subj, amount]
"at_least":
return "%s at least %s" % [subj, amount]
"at_most":
return "%s at most %s" % [subj, amount]
return ""
## A value token beside a scale in words: a whole number reads as the step at
## that position ("2" -> "Cold"), anything else through `expr`.
static func step_word(tok: String, steps: Array, expr: Callable) -> String:
var s := tok.strip_edges()
if s.is_valid_int() and not steps.is_empty():
return Scales.label_at(steps, int(s))
return str(expr.call(s))