A pick option can cost an amount of a value (TableEntry.cost_value and cost_amount, JSON "cost"; the inspector's Costs row). The pop-up shows the cost, and greys the option out with the reason when there isn't enough; Random never picks it. Picking it takes the amount away as an ordinary change (RollCosts). A costly Reroll option that threads back to a card gives spend-a-point rerolls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
154 lines
6.1 KiB
GDScript
154 lines
6.1 KiB
GDScript
extends SceneTree
|
||
## 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: godot --headless --path . -s res://tests/test_roll_features.gd
|
||
|
||
var _failures := 0
|
||
|
||
|
||
func _init() -> void:
|
||
_test_combine()
|
||
await _test_choose()
|
||
await _test_pause()
|
||
await _test_costs()
|
||
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
||
quit(1 if _failures else 0)
|
||
|
||
|
||
## 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)
|
||
|
||
|
||
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 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")
|
||
|
||
|
||
func _check(ok: bool, what: String) -> void:
|
||
if not ok:
|
||
_failures += 1
|
||
print("FAIL ", what)
|