extends TestBase ## Book-table features in the engine: scale values, cards with columns, and ## threads that roll with other dice or roll again on some results. ## Run: godot --headless --path . -s res://tests/test_book_tables.gd -- --sample func _init() -> void: _test_scales() _test_scale_effects() await _test_columns_row() await _test_columns_each() _test_dice_override() _test_reroll() _test_save_load() _test_lint() finish() const CLIMATE := ["Supercold", "Cold", "Temperate", "Hot", "Superhot"] func _board() -> TowerBoard: var b := TowerBoard.new() var v := RollTable.create("variable", "v_climate", "Climate", "scale") v.var_name = "climate" v.steps.assign(CLIMATE) b.tables.append(v) var d := RollTable.create("variable", "v_atmo", "Atmosphere", "scale") d.var_name = "atmo" d.steps.assign(["None", "Thin", "Medium", "Dense"]) b.tables.append(d) return b func _ctx(b: TowerBoard, seed_value: int = 1) -> RollContext: var rng := RandomNumberGenerator.new() rng.seed = seed_value return Roller.new_context(b, rng, {}, {}) func _ev(src: String, ctx: RollContext) -> Variant: return Expr.evaluate(src, ctx)["value"] func _test_scales() -> void: var b := _board() var ctx := _ctx(b) ctx.save("@climate", 2) _eq(ctx.get_value("@climate"), "Cold", "a number saves as the step at that position") ctx.save("@climate", "temperate") _eq(ctx.get_value("@climate"), "Temperate", "a step name saves in its own spelling") ctx.save("@climate", 9) _eq(ctx.get_value("@climate"), "Superhot", "past the top stops at the last step") ctx.save("@climate", -3) _eq(ctx.get_value("@climate"), "Supercold", "below the bottom stops at the first step") ctx.save("@climate", "Cold") _eq(_ev("@climate", ctx), 2, "a scale reads as its step's number") _eq(_ev("@climate < \"Temperate\"", ctx), true, "a step name beside a scale reads as its number") _eq(_ev("@climate >= \"Hot\"", ctx), false, "comparing steps") _eq(_ev("toward(@climate, \"Temperate\")", ctx), 3, "toward moves one step") _eq(_ev("@atmo >= \"Thin\"", ctx), false, "an unset scale reads as 0, below every step") ctx.save("@atmo", "Medium") _eq(_ev("@climate = \"Cold\" and @atmo >= \"Thin\"", ctx), true, "each step name goes with the scale beside it") _eq(_ev("\"Cold\" + \"!\"", ctx), "Cold!", "text with no scale beside it stays text") var text := TextExpander.render("It is {@climate}.", ctx) _check(text.contains("Cold"), "{@climate} shows the step's name") func _test_scale_effects() -> void: var b := _board() var ctx := _ctx(b) Effects.run("@climate = \"Hot\"", ctx) Effects.run("@climate += 1", ctx) _eq(ctx.get_value("@climate"), "Superhot", "+= moves up a step") Effects.run("@climate += 1", ctx) _eq(ctx.get_value("@climate"), "Superhot", "and stops at the top") var log := Effects.run("@climate -= 2", ctx) _eq(ctx.get_value("@climate"), "Temperate", "-= moves down") _check(str(log[0]["text"]).contains("Temperate") and str(log[0]["text"]).contains("steps"), "the log names the steps: %s" % log[0]["text"]) Effects.run("@climate = 1", ctx) Effects.run("@climate = toward(@climate, \"Temperate\")", ctx) _eq(ctx.get_value("@climate"), "Cold", "toward the middle from the bottom") Effects.run("@atmo = \"Thin\"; @atmo = max(@atmo, \"Medium\")", ctx) _eq(ctx.get_value("@atmo"), "Medium", "raise to at least Medium") var bad := Effects.run("@climate *= 2", ctx) _check(bad[0].get("error", false), "a scale can't be multiplied") ## World type card: 1d10, 3 results, columns Resources | Water | Life. func _type_card(rolls: String) -> RollTable: var t := RollTable.new("type", "Oceanic") t.dice = "1d10" t.column_rolls = rolls t.columns = [{"name": "Resources", "save_as": "@res"}, {"name": "Water", "save_as": "@water"}, {"name": "Life", "save_as": "@life", "when": "@water != \"None\""}] var rows := [[1, 3, "Poor", "None", "None"], [4, 9, "Rich", "Plentiful", "Simple"], [10, 10, "Ultra-Rich", "Maximal", "Complex"]] for r: Array in rows: var e := TableEntry.new("e%d" % r[0], r[2]) e.has_range = true e.range_lo = r[0] e.range_hi = r[1] e.cells.assign([r[3], r[4]]) t.entries.append(e) return t func _test_columns_row() -> void: var b := TowerBoard.new() b.tables.append(_type_card(RollTable.COLUMNS_ROW)) for s in 20: var ctx := _ctx(b, s) var node := await Roller.roll_async(b, "type", ctx) var e: TableEntry = node["entry"] _eq(ctx.get_value("@res"), Formatter.strip_tags(e.text), "row: column 0 saves the result's text") if e.cells[0] == "None": _eq(ctx.get_value("@life"), null, "a skipped column saves nothing") _check(bool((node["columns"] as Array)[2].get("skipped", false)), "and is marked skipped") else: _eq(ctx.get_value("@life"), e.cells[1], "row: every column reads the same result") _check(str(node["summary"]).begins_with("Resources: "), "the summary names the columns: %s" % node["summary"]) func _test_columns_each() -> void: var b := TowerBoard.new() b.tables.append(_type_card(RollTable.COLUMNS_EACH)) var mixed := false for s in 60: var ctx := _ctx(b, s) var node := await Roller.roll_async(b, "type", ctx) var cols: Array = node["columns"] if int(cols[0]["index"]) != int(cols[1]["index"]): mixed = true var columns_rolled := (node["why"] as Array).filter(func(f: Dictionary) -> bool: return str(f.get("column", "")) != "") _check(columns_rolled.size() >= 1, "each: the other columns are rolled with why facts") _check(mixed, "each: columns can land on different rows") ## Feature: 1d100 card, the thread rolls it with 3d10+70. func _feature_board() -> TowerBoard: var b := TowerBoard.new() var start := RollTable.new("start", "Start") var e := TableEntry.new("s1", "Go") e.add_pipe("feat") e.set_pipe_dice("feat", "3d10+70") start.entries.append(e) var feat := RollTable.new("feat", "Feature") for r: Array in [[1, 70, "Nothing"], [71, 95, "Something"], [96, 100, "Multiple"]]: var x := TableEntry.new("f%d" % r[0], r[2]) x.has_range = true x.range_lo = r[0] x.range_hi = r[1] feat.entries.append(x) b.tables.append(start) b.tables.append(feat) return b func _test_dice_override() -> void: var b := _feature_board() var low := 100 for s in 80: var tree := Roller.roll(b, "start", _ctx(b, s).rng) var kid: Dictionary = tree["children"][0] low = mini(low, int(kid["value"])) _check(str((kid["why"] as Array)[0]["dice"]) == "3d10+70", "the why fact names the thread's dice") _check(low >= 73, "3d10+70 never rolls below 73 (lowest %d)" % low) _eq(DescribeCards.pipe(b.get_table("start").entries[0], "feat", b), "with 3d10+70", "the thread label says the dice") func _test_reroll() -> void: var b := _feature_board() var e := b.get_table("start").entries[0] e.set_pipe_dice("feat", "") e.set_pipe_reroll("feat", ["f96"]) var skipped := false for s in 200: var tree := Roller.roll(b, "start", _ctx(b, s).rng) var kid: Dictionary = tree["children"][0] _check((kid["entry"] as TableEntry).id != "f96", "a result the thread rolls again on never stays") for f: Dictionary in kid.get("why", []): if f["type"] == "reroll": skipped = true _check(skipped, "a reroll why fact is recorded") _eq(DescribeCards.pipe(e, "feat", b), "again on 96–00", "the label lists the numbers") e.set_pipe_reroll("feat", ["f1", "f71", "f96"]) var tree2 := Roller.roll(b, "start", _ctx(b).rng) _eq(str(tree2["children"][0]["stop"]), RollCards.ALL_REROLLED, "rolling again on everything stops") func _test_save_load() -> void: var b := _board() b.tables.append(_type_card(RollTable.COLUMNS_EACH)) for t in _feature_board().tables: b.tables.append(t) b.get_table("start").entries[0].set_pipe_reroll("feat", ["f96"]) var text := JSON.stringify(b.to_dict()) var back := TowerBoard.new() back.load_dict(JSON.parse_string(text)) _eq(back.get_table("v_climate").steps, CLIMATE, "scale steps round-trip") var t := back.get_table("type") _eq(t.column_rolls, RollTable.COLUMNS_EACH, "column rolls round-trip") _eq(t.column_names(), ["Resources", "Water", "Life"], "columns round-trip") _eq(str(t.columns[2].get("when", "")), "@water != \"None\"", "a column's condition round-trips") _eq(t.entries[1].cells, ["Plentiful", "Simple"], "cells round-trip") var se := back.get_table("start").entries[0] _eq(se.dice_for("feat"), "3d10+70", "thread dice round-trip") _eq(se.reroll_for("feat"), ["f96"], "thread rerolls round-trip") var canon := JsonIO.canonical(JSON.parse_string(text)) var again := TowerBoard.new() again.load_dict(canon) _eq(again.get_table("type").entries[1].cells, ["Plentiful", "Simple"], "import keeps cells") _eq(again.get_table("start").entries[0].reroll_for("feat"), ["f96"], "import keeps rerolls") _eq(again.get_table("v_climate").steps, CLIMATE, "import keeps steps") func _test_lint() -> void: var b := _feature_board() var e := b.get_table("start").entries[0] e.set_pipe_reroll("feat", ["f1", "f71", "f96"]) var msgs := Roller.lint_pipes(b).map(func(i: Dictionary) -> String: return str(i["message"])) _check(msgs.any(func(m: String) -> bool: return m.contains("every result")), "lint: rerolling everything") var s := RollTable.create("variable", "v", "Odd", "scale") s.steps.assign(["Only"]) b.tables.append(s) msgs = Roller.lint_pipes(b).map(func(i: Dictionary) -> String: return str(i["message"])) _check(msgs.any(func(m: String) -> bool: return m.contains("two steps")), "lint: a one-step scale")