RollTable.roll_floor and roll_ceiling (numbers or values) limit a ranged roll's total after its modifier (RollRange.limit). An earlier result's change (e.g. @atmo_min = 7) can thereby raise or cap a later card's roll. The inspector's Limits rows take a number or a value; the Why trace says 'raised to 7 (never below Atmo min)'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
316 lines
10 KiB
GDScript
316 lines
10 KiB
GDScript
class_name RollRange
|
||
extends RefCounted
|
||
## The numbers side of a ranged roll table (RollTable delegates here): its
|
||
## span, number labels, chances, range problems and the roll modifier.
|
||
##
|
||
## A ranged table rolls `dice` and then adds its modifier (`dice_mod`, an
|
||
## expression such as "@luck", "-@fatigue" or "dm(@str)"). The dice alone
|
||
## give the natural range (1–20 for 1d20). Results may sit past it (-3, 24):
|
||
## only a modifier reaches them. A table with a modifier, or with a whole
|
||
## expression as its dice, uses the nearest result for totals past its ends:
|
||
## a total below the lowest range or above the highest one counts as that end
|
||
## result ("clamps").
|
||
|
||
|
||
## True when totals past the ends count as the end results: a ranged table
|
||
## with a modifier or with an expression for its dice.
|
||
static func clamps(t: RollTable) -> bool:
|
||
return t.is_ranged() and (t.is_expr_dice() or t.has_mod())
|
||
|
||
|
||
## Vector2i(lowest, highest) range values over the ranged results.
|
||
static func span(t: RollTable) -> Vector2i:
|
||
var lo := 0
|
||
var hi := 0
|
||
var first := true
|
||
for e in t.entries:
|
||
if not e.has_range:
|
||
continue
|
||
if first:
|
||
lo = e.range_lo
|
||
hi = e.range_hi
|
||
first = false
|
||
else:
|
||
lo = mini(lo, e.range_lo)
|
||
hi = maxi(hi, e.range_hi)
|
||
return Vector2i(lo, hi)
|
||
|
||
|
||
## Vector2i(lowest, highest) the dice alone can roll. Expression dice have
|
||
## no known natural range, so it's the ranges' span.
|
||
static func natural(t: RollTable) -> Vector2i:
|
||
if t.is_expr_dice():
|
||
return span(t)
|
||
var d := t.dice_obj()
|
||
return Vector2i(d.min_value(), d.max_value())
|
||
|
||
|
||
## Lowest value the card shows or rolls: 1 on plain tables, the ranges' span
|
||
## for expression dice, the dice and the ranges together with a modifier.
|
||
static func min_value(t: RollTable) -> int:
|
||
if not t.is_ranged():
|
||
return 1
|
||
if t.is_expr_dice():
|
||
return span(t).x
|
||
if t.has_mod():
|
||
return mini(span(t).x, natural(t).x)
|
||
return t.dice_obj().min_value()
|
||
|
||
|
||
## Highest value (see min_value). Plain tables: the result count.
|
||
static func max_value(t: RollTable) -> int:
|
||
if not t.is_ranged():
|
||
return t.entries.size()
|
||
if t.is_expr_dice():
|
||
return span(t).y
|
||
if t.has_mod():
|
||
return maxi(span(t).y, natural(t).y)
|
||
return t.dice_obj().max_value()
|
||
|
||
|
||
## True when the dice alone (no modifier) can land on the result.
|
||
static func natural_reach(t: RollTable, e: TableEntry) -> bool:
|
||
if not e.has_range:
|
||
return false
|
||
return t.is_expr_dice() or chance(t, e, 0) > 0.0
|
||
|
||
|
||
# ---------- Labels ----------
|
||
|
||
## Formats a value the way printed tables do: plain d100 values are two
|
||
## digits and 100 is written "00".
|
||
static func fmt(t: RollTable, value: int) -> String:
|
||
var two_digit := (t.dice_obj().is_d100() and not clamps(t)) if t.is_ranged() else t.entries.size() == 100
|
||
if two_digit:
|
||
return "00" if value == 100 else "%02d" % value
|
||
return str(value)
|
||
|
||
|
||
## The number column: "3", "01–30", and "≤6" / "24+" on the end results of
|
||
## a table that uses the nearest result past its ends.
|
||
static func num_label(t: RollTable, e: TableEntry, index: int) -> String:
|
||
if not e.has_range:
|
||
return fmt(t, index + 1)
|
||
if clamps(t):
|
||
var s := span(t)
|
||
var ranged := 0
|
||
for x in t.entries:
|
||
if x.has_range:
|
||
ranged += 1
|
||
if ranged > 1:
|
||
if e.range_lo == s.x:
|
||
return "≤%s" % fmt(t, e.range_hi)
|
||
if e.range_hi == s.y:
|
||
return "%s+" % fmt(t, e.range_lo)
|
||
if e.range_lo == e.range_hi:
|
||
return fmt(t, e.range_lo)
|
||
return "%s–%s" % [fmt(t, e.range_lo), fmt(t, e.range_hi)]
|
||
|
||
|
||
# ---------- Chances ----------
|
||
|
||
## Chance (0–1) that a roll lands on the result, with the modifier at `mod`
|
||
## (ignored when the table has none). 0 for expression dice, whose chances
|
||
## can't be known.
|
||
static func chance(t: RollTable, e: TableEntry, mod: int = 0) -> float:
|
||
if not t.is_ranged():
|
||
var total := t.total_weight()
|
||
return float(maxi(0, e.weight)) / float(total) if total > 0 else 0.0
|
||
if not e.has_range or t.is_expr_dice():
|
||
return 0.0
|
||
var d := t.dice_obj()
|
||
if not t.has_mod():
|
||
return d.probability_between(e.range_lo, e.range_hi)
|
||
var s := span(t)
|
||
var p := 0.0
|
||
var dist := d.distribution()
|
||
for v: int in dist:
|
||
var x := clampi(v + mod, s.x, s.y)
|
||
if x >= e.range_lo and x <= e.range_hi:
|
||
p += float(dist[v])
|
||
return p
|
||
|
||
|
||
## True when the result can come up at all (with some modifier, on a table
|
||
## that has one).
|
||
static func can_come_up(t: RollTable, e: TableEntry) -> bool:
|
||
if t.is_ranged():
|
||
return e.has_range and (clamps(t) or chance(t, e) > 0.0)
|
||
return e.weight > 0
|
||
|
||
|
||
# ---------- The modifier ----------
|
||
|
||
## "1d20 + @luck", "2d6 - 1", or the dice alone when there's no modifier.
|
||
static func join(dice: String, mod: String) -> String:
|
||
var d := dice.strip_edges()
|
||
var m := mod.strip_edges()
|
||
if m == "":
|
||
return d
|
||
if m.begins_with("-"):
|
||
return "%s - %s" % [d, m.substr(1).strip_edges()]
|
||
return "%s + %s" % [d, m]
|
||
|
||
|
||
## Works out a modifier expression: {"value": int, "error": String}. ""
|
||
## gives 0. Shared by roll tables and check cards.
|
||
static func modifier(src: String, ctx: RollContext) -> Dictionary:
|
||
if src.strip_edges() == "":
|
||
return {"value": 0, "error": ""}
|
||
var m := Expr.evaluate(src, ctx)
|
||
if m["error"] != "":
|
||
return {"value": 0, "error": m["error"]}
|
||
if not Expr.is_number(m["value"]):
|
||
return {"value": 0, "error": "gave \"%s\", not a number" % Expr.format_value(m["value"])}
|
||
return {"value": Expr.as_int(m, 0), "error": ""}
|
||
|
||
|
||
## The modifier's value right now, for showing chances outside a roll:
|
||
## {"known": bool, "value": int}. Card inputs read as their defaults. It's
|
||
## unknown when it rolls dice, reads a roll value that isn't set, or fails.
|
||
static func live_mod(t: RollTable, ctx: RollContext) -> Dictionary:
|
||
var src := t.dice_mod.strip_edges()
|
||
if src == "":
|
||
return {"known": true, "value": 0}
|
||
for inp: Dictionary in t.inputs:
|
||
var nm := str(inp.get("name", ""))
|
||
if nm != "" and ctx.get_value(nm) == null:
|
||
var dv := modifier(str(inp.get("default", "")), ctx)
|
||
ctx.save(nm, dv["value"])
|
||
for nm: String in Expr.names_in(src):
|
||
if not nm.begins_with("@") and ctx.get_value(nm) == null:
|
||
return {"known": false, "value": 0}
|
||
var was: Variant = ctx.faces
|
||
ctx.faces = []
|
||
var r := modifier(src, ctx)
|
||
var rolled := not (ctx.faces as Array).is_empty()
|
||
ctx.faces = was
|
||
if r["error"] != "" or rolled:
|
||
return {"known": false, "value": 0}
|
||
return {"known": true, "value": r["value"]}
|
||
|
||
|
||
## Rolls the dice and adds the modifier. Returns {"value": int, "error":
|
||
## String}; dice faces go to ctx.faces when it's collecting them.
|
||
static func roll_value(t: RollTable, ctx: RollContext) -> Dictionary:
|
||
var total := 0
|
||
if not t.is_expr_dice():
|
||
var d := t.dice_obj()
|
||
if ctx.faces != null:
|
||
total = d.bonus
|
||
for k in d.count:
|
||
var face := ctx.rng.randi_range(1, d.sides)
|
||
(ctx.faces as Array).append(face)
|
||
total += face
|
||
if not d.valid:
|
||
total = 0
|
||
else:
|
||
total = d.roll(ctx.rng)
|
||
else:
|
||
var r := Expr.evaluate(t.dice, ctx)
|
||
if r["error"] != "":
|
||
return {"value": 0, "error": r["error"]}
|
||
total = Expr.as_int(r, 0)
|
||
var m := modifier(t.dice_mod, ctx)
|
||
if m["error"] != "":
|
||
return {"value": 0, "error": "the modifier %s %s" % [t.dice_mod.strip_edges(), m["error"]]}
|
||
return limit(t, total + int(m["value"]), ctx)
|
||
|
||
|
||
## Applies the card's floor and ceiling (RollTable.roll_floor, roll_ceiling:
|
||
## "Never below", "Never above"; numbers or values set by earlier results).
|
||
## {value, error, limited: "floor"|"ceiling" when it moved the total}.
|
||
static func limit(t: RollTable, v: int, ctx: RollContext) -> Dictionary:
|
||
var out := {"value": v, "error": "", "raw": v}
|
||
for side in ["floor", "ceiling"]:
|
||
var src := (t.roll_floor if side == "floor" else t.roll_ceiling).strip_edges()
|
||
if src == "":
|
||
continue
|
||
var r := Expr.evaluate(src, ctx)
|
||
if r["error"] != "":
|
||
return {"value": v, "error": "Never %s %s: %s" % ["below" if side == "floor" else "above", src, r["error"]]}
|
||
if r["value"] == null:
|
||
continue
|
||
var lim := Expr.as_int(r, v)
|
||
if (side == "floor" and out["value"] < lim) or (side == "ceiling" and out["value"] > lim):
|
||
out["value"] = lim
|
||
out["limited"] = side
|
||
out["limit_expr"] = src
|
||
return out
|
||
|
||
|
||
## The result index for a rolled total: past the ends counts as the end
|
||
## result on a table that clamps; otherwise -1 when no range holds it.
|
||
static func index_for(t: RollTable, value: int) -> int:
|
||
if clamps(t):
|
||
var s := span(t)
|
||
value = clampi(value, s.x, s.y)
|
||
for i in t.entries.size():
|
||
var e := t.entries[i]
|
||
if e.has_range and value >= e.range_lo and value <= e.range_hi:
|
||
return i
|
||
return -1
|
||
|
||
|
||
# ---------- Problems ----------
|
||
|
||
## Gaps, overlaps and (on a table that doesn't clamp) results the dice can't
|
||
## reach, or "" when clean.
|
||
## The first range whose start is above its end ("9–4"), or "". Such a card
|
||
## can't be rolled until it's fixed (the editor no longer swaps the ends).
|
||
static func backwards(t: RollTable) -> String:
|
||
if not t.is_ranged():
|
||
return ""
|
||
for e in t.entries:
|
||
if e.has_range and e.range_lo > e.range_hi:
|
||
return "%s–%s" % [fmt(t, e.range_lo), fmt(t, e.range_hi)]
|
||
return ""
|
||
|
||
|
||
static func problems(t: RollTable) -> String:
|
||
if not t.is_ranged():
|
||
return ""
|
||
var back := backwards(t)
|
||
if back != "":
|
||
return "The range %s runs backwards: make its start no bigger than its end. The card can't be rolled until then." % back
|
||
var lo := min_value(t)
|
||
var hi := max_value(t)
|
||
if clamps(t):
|
||
var s := span(t)
|
||
lo = s.x
|
||
hi = s.y
|
||
if hi - lo > 100000:
|
||
return "The ranges span too many values to check."
|
||
var hits := PackedInt32Array()
|
||
hits.resize(maxi(0, hi - lo + 1))
|
||
var outside := PackedStringArray()
|
||
for e in t.entries:
|
||
if not e.has_range:
|
||
continue
|
||
for v in range(maxi(lo, e.range_lo), mini(hi, e.range_hi) + 1):
|
||
hits[v - lo] += 1
|
||
if e.range_lo < lo or e.range_hi > hi:
|
||
outside.append("%s–%s" % [fmt(t, e.range_lo), fmt(t, e.range_hi)])
|
||
var gaps := _runs(t, hits, lo, hi, func(h: int) -> bool: return h == 0)
|
||
var overlaps := _runs(t, hits, lo, hi, func(h: int) -> bool: return h > 1)
|
||
var parts := PackedStringArray()
|
||
if gaps != "":
|
||
parts.append("No result for %s." % gaps)
|
||
if overlaps != "":
|
||
parts.append("%s matches more than one result." % overlaps)
|
||
if not outside.is_empty():
|
||
parts.append("%s is outside %s–%s. Give the roll a modifier to reach it." % [", ".join(outside), fmt(t, lo), fmt(t, hi)])
|
||
return " ".join(parts)
|
||
|
||
|
||
static func _runs(t: RollTable, hits: PackedInt32Array, lo: int, n: int, test: Callable) -> String:
|
||
var out := PackedStringArray()
|
||
var start := -1
|
||
for v in range(lo, n + 2):
|
||
var on: bool = v <= n and test.call(hits[v - lo])
|
||
if on and start < 0:
|
||
start = v
|
||
elif not on and start >= 0:
|
||
out.append(fmt(t, start) if start == v - 1 else "%s–%s" % [fmt(t, start), fmt(t, v - 1)])
|
||
start = -1
|
||
return ", ".join(out)
|