TableTower/tests/test_roller.gd
RobinFiredrake 2d3f5e9e3d Reroll this undoes the old result's values; partly drawn tint from a turned accent
- RollJournal: each roll logs the values it saves or erases; a reroll undoes
  the old branch's writes before rolling again (a number a later step changed
  keeps that later change).
- Style.turned_accent: the accent with its hue turned (OK HSL). Partly drawn
  deck rows use it turned 200 degrees, so they don't look like a lit row.
- Tests in test_roller and test_fast_roll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:13:14 -06:00

107 lines
4.4 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.

extends TestBase
## Headless checks for the data model and roller.
## Run: tools/run_tests.sh roller
func run() -> void:
var board := SampleBoard.create()
var rng := RandomNumberGenerator.new()
rng.seed = 12345
var trinkets := board.get_table("trinkets")
_check(trinkets.entries.size() == 100, "trinkets has 100 entries")
_check(trinkets.die_size() == 100, "trinkets rolls a d100")
var unique := {}
for e in trinkets.entries:
unique[e.text] = true
_check(unique.size() == 100, "trinket entries are unique")
_check(trinkets.num_label(trinkets.entries[0], 0) == "01", "first trinket is numbered 01")
_check(trinkets.num_label(trinkets.entries[99], 99) == "00", "last trinket is numbered 00")
var wild := board.get_table("wilderness")
_check(wild.is_ranged() and wild.die_size() == 100, "wilderness is a ranged d100")
_check(wild.range_problems() == "", "wilderness ranges have no gaps or overlaps")
_check(wild.num_label(wild.entries[5], 5) == "96–00", "last range reads 96–00")
for e in wild.entries:
_check(e.pipes.size() == 1, "range %s has exactly one thread" % e.text)
# Range weighting: 01–30 should come up about 30% of the time.
var counts := {}
for i in 4000:
var n := Roller.roll(board, "wilderness", rng)
var text: String = (n["entry"] as TableEntry).text
counts[text] = counts.get(text, 0) + 1
var share := float(counts.get("Travelers on the road", 0)) / 4000.0
_check(absf(share - 0.30) < 0.02, "01–30 rolls about 30%% of the time (got %.3f)" % share)
var rare := float(counts.get("An odd find", 0)) / 4000.0
_check(absf(rare - 0.05) < 0.01, "96–00 rolls about 5%% of the time (got %.3f)" % rare)
# Chains follow pipes.
var reached := false
for i in 500:
var n := Roller.roll(board, "wilderness", rng)
if Roller.reaches(n, "trinkets"):
reached = true
_check(Roller.flatten(n, []).size() == 2, "wilderness → trinkets chain has two steps")
break
_check(reached, "wilderness can reach trinkets")
# Gaps and overlaps are reported.
wild.entries[1].range_lo = 35
_check(wild.range_problems() == "No result for 31–34.", "gap reported: %s" % wild.range_problems())
wild.entries[1].range_lo = 25
_check(wild.range_problems() == "25–30 matches more than one result.", "overlap reported: %s" % wild.range_problems())
wild.entries[1].range_lo = 31
var n_gap := RollTable.new("g", "Gappy")
var only := TableEntry.new("e1", "Low")
only.has_range = true
only.range_lo = 1
only.range_hi = 50
n_gap.entries.append(only)
var stops := 0
for i in 200:
var b2 := TowerBoard.new()
b2.tables.append(n_gap)
if Roller.roll(b2, "g", rng).has("stop"):
stops += 1
_check(stops > 50 and stops < 150, "rolls into a gap stop with a message (%d/200)" % stops)
# JSON round trip keeps everything.
var copy := TowerBoard.new()
_check(copy.load_json(board.to_json()), "spread JSON loads back")
_check(copy.to_json() == board.to_json(), "spread JSON round-trips unchanged")
_check(copy.get_table("trinkets").collapsed, "collapsed state is saved")
# Removing a table removes pipes into it.
copy.remove_table("loot")
var dangling := false
for t in copy.tables:
for e in t.entries:
if e.pipes.has("loot"):
dangling = true
_check(not dangling and copy.get_table("loot") == null, "removing a table removes threads into it")
_test_journal_undo()
finish()
## RollJournal: undoing part of a roll puts values back. A number a later
## write changed again keeps that later change; a value first set by the undone
## part is erased.
func _test_journal_undo() -> void:
var persistent := {"luck": {"value": 5, "source": "a"}}
var ctx := RollContext.new(persistent)
ctx.save("@luck", 4) # 0: undone
ctx.save("@gold", 10) # 1: undone, new value
ctx.save("place", "cave") # 2: undone, chain value
ctx.save("@luck", 2) # 3: a later write (-2)
RollJournal.undo(ctx.journal, 0, 3, persistent, ctx.chain_vars)
_eq(int(persistent["luck"]["value"]), 3, "a later change stays; only the undone -1 comes off")
_check(not persistent.has("gold"), "a value the undone part made is gone")
_check(not ctx.chain_vars.has("place"), "a chain value the undone part made is gone")
var fresh := {"luck": {"value": 5, "source": "a"}}
var ctx2 := RollContext.new(fresh)
ctx2.save("@luck", 1)
ctx2.erase("@luck")
RollJournal.undo(ctx2.journal, 0, 2, fresh, ctx2.chain_vars)
_eq(int(fresh["luck"]["value"]), 5, "a value set then erased comes back as it was")