TableTower/tests/test_roll_features.gd
2026-12-01 14:15:25 -06:00

320 lines
14 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
## Roll features in the engine: combining a thread's repeated rolls (add,
## highest, lowest, choose), saving the combined value, and only the kept roll
## following its threads.
## Run: tools/run_tests.sh roll_features
func run() -> void:
_test_combine()
_test_no_repeats()
await _test_choose()
await _test_pause()
await _test_costs()
await _test_card_costs()
_test_limits()
_test_deck_dice_rules()
_test_deck_rerolls()
finish()
## Start → Gold ×3; Gold's results are numbers, and each pipes on to Note.
func _board(mode: String) -> TowerBoard:
var b := TowerBoard.new()
var start := RollTable.new("start", "Start")
var e := TableEntry.new("e1", "Treasure")
e.add_pipe("gold", 3, "loot")
if mode != "all":
e.pipe_combine["gold"] = mode
start.entries.append(e)
var gold := RollTable.new("gold", "Gold")
for i in 6:
var g := TableEntry.new("g%d" % i, str((i + 1) * 10))
g.add_pipe("note")
gold.entries.append(g)
var note := RollTable.new("note", "Note")
note.entries.append(TableEntry.new("n1", "Noted"))
for t in [start, gold, note]:
b.tables.append(t)
return b
func _kids(tree: Dictionary, tid: String) -> Array:
return (tree["children"] as Array).filter(func(k: Dictionary) -> bool: return (k["table"] as RollTable).id == tid)
## "No repeats": three rolls of a six-result card all differ, and with as
## many rolls as results every result comes up once; saved and loaded.
func _test_no_repeats() -> void:
for seed_value in 30:
var b := _board("all")
var e: TableEntry = b.get_table("start").entries[0]
e.pipe_distinct["gold"] = true
var rng := RandomNumberGenerator.new()
rng.seed = seed_value
var ids := {}
for k: Dictionary in _kids(Roller.roll(b, "start", rng), "gold"):
ids[(k["entry"] as TableEntry).id] = true
_check(ids.size() == 3, "no repeats: three different results (seed %d)" % seed_value)
var six := _board("all")
var six_e: TableEntry = six.get_table("start").entries[0]
six_e.pipe_distinct["gold"] = true
six_e.add_pipe("gold", 6, "loot")
var all_ids := {}
for k: Dictionary in _kids(Roller.roll(six, "start", RandomNumberGenerator.new()), "gold"):
all_ids[(k["entry"] as TableEntry).id] = true
_eq(all_ids.size(), 6, "six rolls of six results give each one once")
_check(TableEntry.from_dict(six_e.to_dict()).pipe_distinct.has("gold"), "No repeats is saved with the thread")
func _test_combine() -> void:
var rng := RandomNumberGenerator.new()
rng.seed = 7
var tree := Roller.roll(_board("all"), "start", rng)
_check(_kids(tree, "gold").size() == 3, "keep every roll: three Gold results")
_check((_kids(tree, "gold")[0]["children"] as Array).size() == 1, "and each follows its own thread")
var b := _board("sum")
var ctx := Roller.new_context(b, rng)
tree = Roller.roll(b, "start", rng, 0, {}, ctx)
var golds := _kids(tree, "gold")
var total := 0
for k: Dictionary in golds:
if not k.has("combined"):
total += int(str(k["summary"]))
_check(k.get("dropped", false) and (k["children"] as Array).is_empty(), "added rolls don't follow their threads")
var sum: Dictionary = golds[golds.size() - 1]
_check(sum.get("combined") == "sum" and int(str(sum["summary"])) == total, "add together: a total of %d" % total)
_check(int(ctx.lookup("loot")) == total, "save as keeps the total")
b = _board("max")
tree = Roller.roll(b, "start", rng)
golds = _kids(tree, "gold")
var best: Dictionary = {}
for k: Dictionary in golds:
if not k.get("dropped", false):
best = k
var top := 0
for k: Dictionary in golds:
top = maxi(top, int(str(k["summary"])))
_check(best.get("kept") == "max" and int(str(best["summary"])) == top, "keep the highest keeps %d" % top)
_check((best["children"] as Array).size() == 1, "the kept roll follows its thread")
var flat := Roller.flatten(tree, [])
_check(flat.any(func(s: Dictionary) -> bool: return s.get("dropped", false)), "set-aside rolls are marked in the steps")
var back := TableEntry.from_dict(b.get_table("start").entries[0].to_dict())
_check(back.combine_for("gold") == "max", "the combine mode is saved")
func _test_choose() -> void:
var b := _board("choose")
var rng := RandomNumberGenerator.new()
rng.seed = 3
var ctx := Roller.new_context(b, rng)
var asked: Array = []
ctx.prompt = func(req: Dictionary) -> Variant:
asked.append(req)
return 1
var tree: Dictionary = await Roller.roll_async(b, "start", ctx)
var golds := _kids(tree, "gold")
_check(asked.size() == 1 and (asked[0]["options"] as Array).size() == 3, "let me choose asks once, with the three rolls")
_check(golds[1].get("kept") == "choose" and golds[0].get("dropped", false), "the picked roll is kept")
func _test_pause() -> void:
var b := _board("all")
b.get_table("start").entries[0].pipe_pause["gold"] = true
var rng := RandomNumberGenerator.new()
var tree := Roller.roll(b, "start", rng)
_check(_kids(tree, "gold").size() == 3, "headless rolls continue past a pause")
var ctx := Roller.new_context(b, rng)
var asked: Array = []
ctx.prompt = func(req: Dictionary) -> Variant:
asked.append(req)
return false
tree = await Roller.roll_async(b, "start", ctx)
_check(asked.size() == 1 and asked[0]["kind"] == "pause", "a paused thread asks once")
_check(_kids(tree, "gold").is_empty() and tree.get("stopped_before") == "gold", "Stop here ends the roll before the thread")
var back := TableEntry.from_dict(b.get_table("start").entries[0].to_dict())
_check(back.pipe_pause.has("gold"), "the pause is saved")
## A card that costs 1 point to roll: its own roll pays or stops; a thread
## that reaches it asks first (Continue pays, Stop skips it).
func _test_card_costs() -> void:
var b := _board("all")
var gold := b.get_table("gold")
gold.cost_value = "@points"
gold.cost_amount = "1"
var ctx := Roller.new_context(b, RandomNumberGenerator.new(), {"points": {"value": 2}})
var tree: Dictionary = await Roller.roll_async(b, "gold", ctx)
_check(not tree.has("stop") and int(ctx.lookup("@points")) == 1, "rolling the card itself pays its cost")
_check((tree.get("why", []) as Array).any(func(f: Dictionary) -> bool: return f.get("type") == "effect"), "the payment shows in Why this?")
ctx = Roller.new_context(b, RandomNumberGenerator.new(), {"points": {"value": 0}})
tree = await Roller.roll_async(b, "gold", ctx)
_check(str(tree.get("stop", "")).begins_with("Can't roll"), "with nothing to pay it doesn't roll: %s" % tree.get("stop", ""))
var asked: Array = []
ctx = Roller.new_context(b, RandomNumberGenerator.new(), {"points": {"value": 5}})
ctx.prompt = func(req: Dictionary) -> Variant:
asked.append(req)
return asked.size() == 1
tree = await Roller.roll_async(b, "start", ctx)
_check(asked.size() == 3 and str(asked[0].get("cost", "")) != "", "a thread into it asks before each roll")
var golds := _kids(tree, "gold")
_check(golds.size() == 3 and not golds[0].has("stop") and golds[1].has("stop"), "Continue pays and rolls; not paying skips it")
_check(int(ctx.lookup("@points")) == 4, "only the paid roll took a point")
var back := RollTable.from_dict(gold.to_dict())
_check(back.cost_value == "@points" and back.cost_amount == "1", "the card's cost is saved")
## A pick card whose "Reroll" option costs 1 point.
func _test_costs() -> void:
var b := TowerBoard.new()
var pick := RollTable.create(RollTable.KIND_CHOICE, "pick", "Spend?", "pick")
pick.entries.clear()
var keep := TableEntry.new("k", "Keep it")
var again := TableEntry.new("r", "Reroll")
again.cost_value = "@points"
again.cost_amount = "1"
pick.entries.append(keep)
pick.entries.append(again)
b.tables.append(pick)
var rng := RandomNumberGenerator.new()
var ctx := Roller.new_context(b, rng, {"points": 1})
var req := RollChoice.request_for(pick, ctx)
var o: Dictionary = (req["options"] as Array)[1]
_check(o.get("cost") == "Costs 1 points" or str(o.get("cost", "")).begins_with("Costs 1"), "the option says what it costs: %s" % str(o.get("cost")))
_check(not o.has("disabled"), "with a point it can be picked")
ctx.prompt = func(_r: Dictionary) -> Variant: return 1
await Roller.roll_async(b, "pick", ctx)
_check(int(ctx.lookup("@points")) == 0, "picking it pays the point")
req = RollChoice.request_for(pick, ctx)
o = (req["options"] as Array)[1]
_check(str(o.get("disabled", "")).contains("you have 0"), "with none left it's greyed out: %s" % str(o.get("disabled", "")))
var tree: Dictionary = await Roller.roll_async(b, "pick", ctx)
_check((tree["entry"] as TableEntry).id == "k", "a greyed-out option can't be picked; the reply falls back")
for i in 20:
var t2 := Roller.roll(b, "pick", rng, 0, {}, ctx)
if (t2["entry"] as TableEntry).id == "r":
_check(false, "Random never picks an option you can't afford")
break
var back := TableEntry.from_dict(again.to_dict())
_check(back.cost_value == "@points" and back.cost_amount == "1", "the cost is saved")
## An earlier result sets @atmo_min; the later Atmosphere card never rolls below it.
func _test_limits() -> void:
var b := TowerBoard.new()
var feature := RollTable.new("feature", "Feature")
var e := TableEntry.new("f", "Thick haze")
e.effects = "@atmo_min = 7"
e.add_pipe("atmo")
feature.entries.append(e)
var atmo := RollTable.new("atmo", "Atmosphere")
atmo.dice = "2d6"
for i in range(2, 13):
var a := TableEntry.new("a%d" % i, "Atmo %d" % i)
a.has_range = true
a.range_lo = i
a.range_hi = i
atmo.entries.append(a)
atmo.roll_floor = "@atmo_min"
b.tables.append(feature)
b.tables.append(atmo)
var rng := RandomNumberGenerator.new()
var low := 99
for i in 60:
var ctx := Roller.new_context(b, rng)
var tree := Roller.roll(b, "feature", rng, 0, {}, ctx)
var child: Dictionary = tree["children"][0]
low = mini(low, int(child["value"]))
_check(low == 7, "the earlier result's floor holds: lowest roll %d" % low)
var ctx2 := Roller.new_context(b, rng)
var r := RollRange.limit(atmo, 3, ctx2)
_check(r["value"] == 3 and not r.has("limited"), "an unset value doesn't limit the roll")
Effects.run("@atmo_min = 7", ctx2, "feature")
r = RollRange.limit(atmo, 3, ctx2)
_check(r["value"] == 7 and r["limited"] == "floor", "below the floor is raised to it")
var why := Describe.why({"type": "roll", "pick": "dice", "dice": "2d6", "faces": [1, 2], "total": 7, "raw": 3, "limited": "floor", "limit_expr": "@atmo_min"}, b, true)
_check(why.contains("raised to 7"), "Why says it was raised: " + why)
var back := RollTable.from_dict(atmo.to_dict())
_check(back.roll_floor == "@atmo_min", "the floor is saved")
## A card in Deck mode on a board; `ranged` gives it six single-number results
## on a d6, else six plain results. Returns [board, table].
func _deck(ranged: bool) -> Array:
var b := TowerBoard.new()
var t := RollTable.new("deck", "Deck")
t.card_mode = true
t.dice = "d6"
for i in 6:
var e := TableEntry.new("c%d" % (i + 1), "Card %d" % (i + 1))
if ranged:
e.has_range = true
e.range_lo = i + 1
e.range_hi = i + 1
t.entries.append(e)
b.tables.append(t)
return [b, t]
func _draw(b: TowerBoard, t: RollTable, rng: RandomNumberGenerator, decks: Dictionary, f: Dictionary = {}) -> Dictionary:
return RollCards.pick_for(t, Roller.new_context(b, rng, {}, decks), f)
## A deck rolls like the same card without Deck mode: the table's modifier,
## "Never below" and a thread's own dice all apply, and a result that is
## already drawn is rolled again.
## Spec lines (deck-mode): "ALWAYS a deck rolls exactly like the same card without Deck mode, except that drawn results can't come up".
func _test_deck_dice_rules() -> void:
var rng := RandomNumberGenerator.new()
for seed_value in 20:
rng.seed = seed_value
var plain := _deck(false)
plain[1].dice_mod = "2"
var first := _draw(plain[0], plain[1], rng, {})
_check(int(first["index"]) >= 2, "a plain deck's modifier moves the pick (seed %d)" % seed_value)
var ranged := _deck(true)
var board: TowerBoard = ranged[0]
var table: RollTable = ranged[1]
table.roll_floor = "5"
var decks := {}
rng.seed = 3
var got := []
for k in 2:
got.append((_draw(board, table, rng, decks)["entry"] as TableEntry).id)
got.sort()
_eq(got, ["c5", "c6"], "a deck's \"Never below\" keeps low results out")
_check(_draw(board, table, rng, decks).has("stop"), "once the results the dice can reach are drawn, the deck stops")
var thread := _deck(true)
var thread_decks := {}
var one := _draw(thread[0], thread[1], rng, thread_decks, {"dice": "1d1"})
_eq((one["entry"] as TableEntry).id, "c1", "a thread's own dice pick the deck's result")
_check(_draw(thread[0], thread[1], rng, thread_decks, {"dice": "1d1"}).has("stop"), "an already drawn result is rolled again, not drawn twice")
var modded := _deck(true)
modded[1].dice_mod = "1"
var up := _draw(modded[0], modded[1], rng, {})
_check(not up.has("stop") and int(up["value"]) >= 2, "a ranged deck adds its modifier to the roll")
## Only the result that is kept leaves the deck; results rolled again go back,
## and the "can anything come up" check counts only what is left.
## Spec lines (deck-mode): "IF a drawn result is rolled again → it goes back; only the kept result leaves the deck".
func _test_deck_rerolls() -> void:
var rng := RandomNumberGenerator.new()
for seed_value in 20:
rng.seed = seed_value
var deck := _deck(false)
var decks := {}
var node := _draw(deck[0], deck[1], rng, decks, {"reroll": ["c1", "c2", "c3"]})
var state: DeckState = decks["deck"]
_eq(state.cards_left(deck[1]), Vector2i(5, 6), "one card leaves the deck, not the re-rolled ones (seed %d)" % seed_value)
_check(int(node["index"]) >= 3 and state.remaining(deck[1], node["entry"]) == 0, "only the kept result is drawn (seed %d)" % seed_value)
var left := _deck(false)
var left_decks := {}
for id in ["c2", "c3", "c4", "c5", "c6"]:
(left_decks.get_or_add("deck", DeckState.new()) as DeckState).take(left[1].find_entry(id))
var stop := _draw(left[0], left[1], rng, left_decks, {"reroll": ["c1"]})
_eq(stop.get("stop", ""), RollCards.ALL_REROLLED, "a thread that skips the last card left in the deck stops the roll")
_eq((left_decks["deck"] as DeckState).cards_left(left[1]), Vector2i(1, 6), "and nothing is drawn or reshuffled")