extends TestBase ## Card threads (CardThreads, LOGIC.md §3g): threads off a card's bottom edge ## that run after the card's result and that result's own threads, whatever ## the result was ("Always"), or only when no result thread ran ("Otherwise"). ## Run: godot --headless --path . -s res://tests/test_card_threads.gd func _init() -> void: _test_always_runs_after() _test_otherwise() _test_card_condition() _test_kinds() _test_io_and_lit() finish() ## Start (results a, b; a → Side) then card thread → Next. func _board() -> TowerBoard: var b := TowerBoard.new() var start := RollTable.new("start", "Start") var a := TableEntry.new("a", "A") a.add_pipe("side") start.entries.append(a) start.entries.append(TableEntry.new("b", "B")) start.outlet.add_pipe("next") b.tables.append(start) for id in ["side", "next"]: var t := RollTable.new(id, id.capitalize()) t.entries.append(TableEntry.new(id + "1", id + " result")) b.tables.append(t) return b func _roll(b: TowerBoard, seed_value: int, id: String = "start") -> Dictionary: var rng := RandomNumberGenerator.new() rng.seed = seed_value return Roller.roll(b, id, rng) func _order(tree: Dictionary) -> Array: var out: Array = [] for step: Dictionary in Roller.flatten(tree): out.append(str(step.get("table_id", ""))) return out func _test_always_runs_after() -> void: var seen := {} for seed_value in 20: var b := _board() var tree := _roll(b, seed_value) var eid := (tree["entry"] as TableEntry).id seen[eid] = true var order := _order(tree) _check(order.has("next"), "the card thread runs after result %s" % eid) if eid == "a": _check(order.find("side") < order.find("next"), "after the result's own thread: %s" % str(order)) _check(seen.size() == 2, "both results came up") func _test_otherwise() -> void: for seed_value in 20: var b := _board() b.get_table("start").outlet.set_pipe_otherwise("next", true) var tree := _roll(b, seed_value) var eid := (tree["entry"] as TableEntry).id var order := _order(tree) _eq(order.has("next"), eid == "b", "otherwise card thread runs only when no result thread ran (got %s)" % eid) ## Only if on a card thread is read after the result's chain ran. func _test_card_condition() -> void: var b := _board() var start := b.get_table("start") start.entries[0].effects = "@hit = 1" start.entries[1].effects = "@hit = 0" start.outlet.set_pipe_when("next", "@hit = 1") for seed_value in 10: var tree := _roll(b, seed_value) _eq(_order(tree).has("next"), (tree["entry"] as TableEntry).id == "a", "Only if on a card thread") ## Every rolled kind follows its card threads; a loop only once, at the end. func _test_kinds() -> void: var b := _board() var loop := RollTable.create(RollTable.KIND_LOOP, "lp", "Loop") loop.loop_expr = "3" loop.outlet.add_pipe("next") b.tables.append(loop) var order := _order(_roll(b, 1, "lp")) _eq(order.count("next"), 1, "a loop's card thread runs once") _check(order.back() == "next", "after the loop's last row: %s" % str(order)) var check := RollTable.create(RollTable.KIND_CHECK, "ck", "Check") check.outlet.add_pipe("next") b.tables.append(check) _check(_order(_roll(b, 2, "ck")).has("next"), "a check follows its card thread") func _test_io_and_lit() -> void: var b := _board() var start := b.get_table("start") start.outlet.set_pipe_otherwise("next", true) var d := start.to_dict() _check(d.has("card_threads"), "card threads are saved") var back := RollTable.from_dict(d) _check(back.outlet.pipes == start.outlet.pipes and back.outlet.is_otherwise("next"), "and loaded with their options") _check(not RollTable.from_dict(b.get_table("side").to_dict()).to_dict().has("card_threads"), "no key without card threads") _check(start.find_entry(RollTable.OUTLET_ID) == start.outlet and start.thread_sources().has(start.outlet), "the outlet is a thread source") start.outlet.set_pipe_otherwise("next", false) var entries := {} var pipes := {} Roller.collect_lit(_roll(b, 3), entries, pipes) _check(pipes.has(Roller.pipe_key("start", RollTable.OUTLET_ID, "next")), "the card thread lights: %s" % str(pipes.keys())) _check(CardThreads.is_card_key(Roller.pipe_key("start", RollTable.OUTLET_ID, "next")), "card keys are recognised")