TableTower/core/roll_table.gd
RobinFiredrake 60c2d03147 Build the pipe-board app in Godot
Tables are cards on a felt board, joined by copper pipes that carry a
roll from one table to the next. Play mode rolls with a flicker and a
bead down each pipe; Build mode edits names, results, ranges and pipes.

- core/: data model (RollTable, TableEntry, TowerBoard), sample board
  with a d100 table of 100 trinkets and a weighted d100 range table,
  and a Roller with a depth limit
- ui/: board with pan and zoom, pipe and port layers, collapsible table
  cards, result tray with earlier rolls, undo toasts, and a Collapse all
  / Expand all button
- assets/fonts/: Rokkitt and Atkinson Hyperlegible (SIL OFL)
- tests/test_roller.gd: headless checks for the model and roller

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:05:41 -06:00

125 lines
3.2 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 RollTable
extends RefCounted
## A named list of entries placed on the board.
##
## Plain tables roll one die face per entry (6 entries = d6). If any entry has a
## range, the table becomes a ranged dice table (d100 by default) where the size
## of each range sets its chance.
var id: String = ""
var name: String = ""
var position: Vector2 = Vector2.ZERO
var collapsed: bool = false
## Die size for ranged tables. 0 means the default (100).
var die: int = 0
var entries: Array[TableEntry] = []
func _init(p_id: String = "", p_name: String = "") -> void:
id = p_id
name = p_name
func is_ranged() -> bool:
for e in entries:
if e.has_range:
return true
return false
func die_size() -> int:
if is_ranged():
return die if die > 0 else 100
return entries.size()
## Formats a die value the way printed tables do: d100 values are two digits
## and 100 is written "00".
func fmt(value: int) -> String:
if die_size() == 100:
return "00" if value == 100 else "%02d" % value
return str(value)
func num_label(e: TableEntry, index: int) -> String:
if not e.has_range:
return fmt(index + 1)
if e.range_lo == e.range_hi:
return fmt(e.range_lo)
return "%s–%s" % [fmt(e.range_lo), fmt(e.range_hi)]
func chance(e: TableEntry) -> float:
return float(e.span()) / float(maxi(1, die_size()))
func entry_index_for(value: int) -> int:
for i in entries.size():
var e := entries[i]
if e.has_range and value >= e.range_lo and value <= e.range_hi:
return i
return -1
func find_entry(eid: String) -> TableEntry:
for e in entries:
if e.id == eid:
return e
return null
## Describes gaps and overlaps in a ranged table, or "" when the ranges are clean.
func range_problems() -> String:
if not is_ranged():
return ""
var n := die_size()
var hits := PackedInt32Array()
hits.resize(n + 1)
for e in entries:
if e.has_range:
for v in range(maxi(1, e.range_lo), mini(n, e.range_hi) + 1):
hits[v] += 1
var gaps := _runs(hits, n, func(h: int) -> bool: return h == 0)
var overlaps := _runs(hits, n, 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)
return " ".join(parts)
func _runs(hits: PackedInt32Array, n: int, test: Callable) -> String:
var out := PackedStringArray()
var start := -1
for v in range(1, n + 2):
var on: bool = v <= n and test.call(hits[v])
if on and start < 0:
start = v
elif not on and start >= 0:
out.append(fmt(start) if start == v - 1 else "%s–%s" % [fmt(start), fmt(v - 1)])
start = -1
return ", ".join(out)
func to_dict() -> Dictionary:
var list: Array = []
for e in entries:
list.append(e.to_dict())
var d := {"id": id, "name": name, "x": roundi(position.x), "y": roundi(position.y), "entries": list}
if collapsed:
d["collapsed"] = true
if die > 0:
d["die"] = die
return d
static func from_dict(d: Dictionary) -> RollTable:
var t := RollTable.new(str(d.get("id", "")), str(d.get("name", "")))
t.position = Vector2(float(d.get("x", 0)), float(d.get("y", 0)))
t.collapsed = bool(d.get("collapsed", false))
t.die = int(d.get("die", 0))
for ed in d.get("entries", []):
if ed is Dictionary:
t.entries.append(TableEntry.from_dict(ed))
return t