diff --git a/core/describe.gd b/core/describe.gd index 114c75a..2c48cb4 100644 --- a/core/describe.gd +++ b/core/describe.gd @@ -583,7 +583,9 @@ static func _why_roll(f: Dictionary, L: Dictionary, plain: bool) -> String: if not plain: var t := "roll %s: %s" % [f.get("dice", ""), faces if faces != "" else total] if f.has("mod") and _nonzero(f["mod"]): - t += " %s = %s" % [_signed(f["mod"]).replace(MINUS, "-"), total] + t += " %s = %s" % [_signed(f["mod"]).replace(MINUS, "-"), Expr.format_value(f.get("raw", f.get("total")))] + if f.has("limited"): + t += " (%s %s: %s)" % ["min" if f["limited"] == "floor" else "max", f.get("limit_expr", ""), total] if past: t += " (past the end: %s)" % ends return t @@ -594,7 +596,10 @@ static func _why_roll(f: Dictionary, L: Dictionary, plain: bool) -> String: var why_mod := "" if str(f.get("mod_expr", "")) != "": why_mod = " for " + _expr(str(f["mod_expr"]).trim_prefix("-"), L) - s += ", %s%s = %s" % [_signed(f["mod"]), why_mod, total] + s += ", %s%s = %s" % [_signed(f["mod"]), why_mod, Expr.format_value(f.get("raw", f.get("total")))] + if f.has("limited"): + var up: bool = f["limited"] == "floor" + s += ", %s to %s (never %s %s)" % ["raised" if up else "lowered", total, "below" if up else "above", _expr(str(f.get("limit_expr", "")), L)] if past: var top: bool = float(Expr.to_value(f.get("counts_as"))) < float(Expr.to_value(f.get("total"))) s += ", past the %s of the table, so it counts as %s" % ["top" if top else "bottom", ends] diff --git a/core/roll_cards.gd b/core/roll_cards.gd index 5e1ff53..06fa7a8 100644 --- a/core/roll_cards.gd +++ b/core/roll_cards.gd @@ -42,6 +42,10 @@ static func pick_node(t: RollTable, ctx: RollContext) -> Dictionary: return Roller._stop(t, "Can't roll %s: %s" % [t.roll_text(), rv["error"]]) value = int(rv["value"]) fact = {"type": "roll", "pick": "dice", "dice": t.roll_text(), "faces": faces, "total": value} + if rv.has("limited"): + fact["limited"] = rv["limited"] + fact["limit_expr"] = rv["limit_expr"] + fact["raw"] = rv["raw"] # The Why line names the modifier ("+3 for Luck") when it's all of the change. if t.has_mod() and not t.is_expr_dice() and t.dice_obj().bonus == 0: fact["mod_expr"] = t.dice_mod.strip_edges() @@ -50,7 +54,7 @@ static func pick_node(t: RollTable, ctx: RollContext) -> Dictionary: for x: int in faces: nat += x fact["nat"] = nat - fact["mod"] = value - nat + fact["mod"] = int(fact.get("raw", value)) - nat index = t.entry_index_clamped(value) if t.clamps(): var span := RollRange.span(t) diff --git a/core/roll_range.gd b/core/roll_range.gd index 92035c6..bc356e4 100644 --- a/core/roll_range.gd +++ b/core/roll_range.gd @@ -214,7 +214,29 @@ static func roll_value(t: RollTable, ctx: RollContext) -> Dictionary: var m := modifier(t.dice_mod, ctx) if m["error"] != "": return {"value": 0, "error": "the modifier %s %s" % [t.dice_mod.strip_edges(), m["error"]]} - return {"value": total + int(m["value"]), "error": ""} + return limit(t, total + int(m["value"]), ctx) + + +## Applies the card's floor and ceiling (RollTable.roll_floor, roll_ceiling: +## "Never below", "Never above"; numbers or values set by earlier results). +## {value, error, limited: "floor"|"ceiling" when it moved the total}. +static func limit(t: RollTable, v: int, ctx: RollContext) -> Dictionary: + var out := {"value": v, "error": "", "raw": v} + for side in ["floor", "ceiling"]: + var src := (t.roll_floor if side == "floor" else t.roll_ceiling).strip_edges() + if src == "": + continue + var r := Expr.evaluate(src, ctx) + if r["error"] != "": + return {"value": v, "error": "Never %s %s: %s" % ["below" if side == "floor" else "above", src, r["error"]]} + if r["value"] == null: + continue + var lim := Expr.as_int(r, v) + if (side == "floor" and out["value"] < lim) or (side == "ceiling" and out["value"] > lim): + out["value"] = lim + out["limited"] = side + out["limit_expr"] = src + return out ## The result index for a rolled total: past the ends counts as the end diff --git a/core/roll_table.gd b/core/roll_table.gd index 717ff8b..9239f34 100644 --- a/core/roll_table.gd +++ b/core/roll_table.gd @@ -55,6 +55,10 @@ var color: String = "" ## little), and the height of its results list, which then scrolls. 0 = automatic. var width: float = 0.0 var list_height: float = 0.0 +## Ranged tables: the total never goes below / above these (numbers or +## expressions such as @atmo_min, often set by an earlier result). "" = none. +var roll_floor: String = "" +var roll_ceiling: String = "" ## Dice expression for ranged tables, e.g. "1d100", "2d6", "1d8+2", or any ## expression such as "2d6 + dm(@end)". var dice: String = DEFAULT_DICE @@ -436,6 +440,10 @@ func to_dict() -> Dictionary: d["tags"] = tags.duplicate() if color != "": d["color"] = color + if roll_floor.strip_edges() != "": + d["roll_floor"] = roll_floor.strip_edges() + if roll_ceiling.strip_edges() != "": + d["roll_ceiling"] = roll_ceiling.strip_edges() if width > 0.0: d["w"] = roundi(width) if list_height > 0.0: @@ -507,6 +515,8 @@ static func from_dict(d: Dictionary) -> RollTable: for tag: Variant in d.get("tags", []): t.tags.append(str(tag)) t.color = str(d.get("color", "")) + t.roll_floor = str(d.get("roll_floor", "")) + t.roll_ceiling = str(d.get("roll_ceiling", "")) t.width = maxf(0.0, float(d.get("w", 0))) t.list_height = maxf(0.0, float(d.get("list_h", 0))) if d.has("dice"): diff --git a/tests/test_roll_features.gd b/tests/test_roll_features.gd index f96b57b..ebb02ce 100644 --- a/tests/test_roll_features.gd +++ b/tests/test_roll_features.gd @@ -12,6 +12,7 @@ func _init() -> void: await _test_choose() await _test_pause() await _test_costs() + _test_limits() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) @@ -148,6 +149,45 @@ func _test_costs() -> void: _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") + + func _check(ok: bool, what: String) -> void: if not ok: _failures += 1 diff --git a/ui/inspector_roll.gd b/ui/inspector_roll.gd index b74c792..1ae3af1 100644 --- a/ui/inspector_roll.gd +++ b/ui/inspector_roll.gd @@ -46,6 +46,7 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool mbox.add_child(mod) mbox.add_child(merr) box.add_child(InspectorKit.field("Modifier", mbox)) + box.add_child(InspectorKit.field("Limits", limits(insp, t))) # A live sentence, what the modifier does, and any range problems. var sentence := InspectorKit.note("", Style.INK, 14) @@ -67,6 +68,42 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool refresh.call() +## "Never below [ ]" and "Never above [ ]": a number, or a value an earlier +## result sets (so one table can adjust a later one), via RollTable.roll_floor +## and roll_ceiling. +static func limits(insp: Inspector, t: RollTable) -> Control: + var v := InspectorKit.vbox(4) + for floor_side in [true, false]: + var row := InspectorKit.flow() + row.add_child(InspectorKit.word("Never below" if floor_side else "Never above")) + var le := InspectorKit.line(t.roll_floor if floor_side else t.roll_ceiling, "no limit", 110.0) + le.tooltip_text = "A number, or a value such as @atmo_min that an earlier result sets" + var err := InspectorKit.error_label() + var apply := func(s: String) -> void: + var msg := Expr.check(s) if s.strip_edges() != "" else "" + InspectorKit.show_error(err, msg) + InspectorKit.mark(le, msg == "") + if msg != "": + return + if floor_side: + t.roll_floor = s.strip_edges() + else: + t.roll_ceiling = s.strip_edges() + insp.wrote(t) + le.text_changed.connect(apply) + row.add_child(le) + var pick := ValuePicker.new(insp.main, "") + pick.tooltip_text = "Use a value" + pick.value_chosen.connect(func(n: String) -> void: + le.text = "@" + n.trim_prefix("@") + apply.call(le.text)) + row.add_child(pick) + v.add_child(row) + v.add_child(err) + v.add_child(InspectorKit.note("To have one table adjust a later one, give the earlier result a change such as “Set Atmo min to 7”, then use that value here.", Style.MUTED, 12)) + return v + + ## Deck mode: draw results without repeats, and what an empty deck does. static func deck(insp: Inspector, box: VBoxContainer, t: RollTable) -> void: var v := InspectorKit.vbox(6)