Plain tables can have a modifier that moves the result up or down rows

The pick is made by weight, then moved by the modifier's value; the
first and last rows catch anything past them. Why this? names the move.
Switching between plain and ranged clears the modifier, since its
meaning changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
RobinFiredrake 2026-11-14 14:05:09 -06:00
parent 8592c9c059
commit 22d3613364
7 changed files with 76 additions and 13 deletions

View file

@ -132,6 +132,12 @@ static func _why_roll(f: Dictionary, L: Dictionary, plain: bool) -> String:
var total := Expr.format_value(f.get("total"))
match str(f.get("pick", "dice")):
"weighted":
if f.has("from") and _nonzero(f.get("mod", 0)):
var by := " for " + Describe._expr(str(f.get("mod_expr", "")).trim_prefix("-"), L) if str(f.get("mod_expr", "")) != "" else ""
if not plain:
return "roll %s: %s, moved %s rows = %s" % [f.get("dice", ""), Expr.format_value(f["from"]), _signed(f["mod"]).replace(Describe.MINUS, "-"), total]
return "Rolled %s on %s, moved %s %s%s, to row %s" % [Expr.format_value(f["from"]), f.get("dice", ""),
"down" if float(f["mod"]) > 0 else "up", _rows(absi(int(f["mod"]))), by, total]
return "Rolled %s on %s" % [total, f.get("dice", "")] if plain else "roll %s: %s" % [f.get("dice", ""), total]
"card":
return "Drew card %s" % total if plain else "draw: card %s" % total
@ -164,6 +170,10 @@ static func _why_roll(f: Dictionary, L: Dictionary, plain: bool) -> String:
return s
static func _rows(count: int) -> String:
return "1 row" if count == 1 else "%d rows" % count
static func _why_check(f: Dictionary, L: Dictionary, plain: bool) -> String:
var faces := _faces(f.get("faces"))
var nat := Expr.format_value(f.get("nat"))

View file

@ -108,6 +108,19 @@ static func pick_node(t: RollTable, ctx: RollContext, dice: String = "") -> Dict
return Roller._stop(t, "No result can come up")
value = index + 1
fact = {"type": "roll", "pick": "weighted", "dice": t.dice_label(), "total": value}
# A plain table's modifier moves the pick down (+) or up (−) the list by
# that many rows; the first and last rows catch anything past them.
if t.has_mod():
var shift := RollRange.modifier(t.dice_mod, ctx)
if shift["error"] != "":
return Roller._stop(t, "Can't work out the modifier %s: %s" % [t.dice_mod.strip_edges(), shift["error"]])
var moved := clampi(index + int(shift["value"]), 0, t.entries.size() - 1)
fact["mod"] = int(shift["value"])
fact["mod_expr"] = t.dice_mod.strip_edges()
fact["from"] = value
index = moved
value = index + 1
fact["total"] = value
var node := {"table": t, "index": index, "value": value, "entry": t.entries[index], "children": [],
"summary": "", "why": [fact]}
if reshuffled:

View file

@ -29,8 +29,8 @@ static func to_dict(t: RollTable) -> Dictionary:
d["list_h"] = roundi(t.list_height)
if t.is_ranged():
d["dice"] = t.dice
if t.has_mod():
d["dice_mod"] = t.dice_mod.strip_edges()
if t.has_mod():
d["dice_mod"] = t.dice_mod.strip_edges()
if t.card_mode:
d["card_mode"] = true
if t.deck_empty != "reshuffle":

View file

@ -95,6 +95,7 @@ A ranged table can add a [b]modifier[/b] to its dice, for rules like "roll 1d20
- [b]Ranges can go past the dice.[/b] A 1d6 table can have results for 0 or less and for 7 and up. Only a modifier reaches them, and their number's tooltip says so.
- Totals past either end count as the end result, so the top result also catches every higher total.
- The chances on the card follow the modifier's value as it is now.
- A [b]plain[/b] table (no ranges) can have a modifier too. It moves the result down the list (+) or up (−) by that many rows, and the first and last results catch anything past them. Why this? says how far it moved.
## In the sample
[b]Search the Ruins[/b] rolls 1d6 + Luck. With no Luck you can't reach "A tunnel caves in" or "A hidden vault". Pray at the shrine in [b]Make Camp[/b] to raise your Luck, gamble with the guards to lower it, then search again and watch the chances move. Why this? on the step names the modifier: "+1 for Luck".

View file

@ -14,6 +14,7 @@ func _init() -> void:
_test_words()
_test_problems()
_test_editor_forms()
_test_plain_shift()
finish()
@ -177,3 +178,27 @@ func _test_editor_forms() -> void:
_eq(ModifierEditor.compose(W.SUBTRACT, "@fatigue"), "-@fatigue", "compose subtract")
_eq(ModifierEditor.compose(W.DM, "@str"), "dm(@str)", "compose dm")
_eq(ModifierEditor.compose(W.ADD, ""), "", "no value yet, no text")
## A plain (unranged) table's modifier moves the pick by rows; the ends catch
## anything past them; the Why line says so; it's saved; a switch clears it.
func _test_plain_shift() -> void:
var t := RollTable.new("finds", "Finds")
for item in ["Nothing", "Coins", "Gem", "Relic"]:
t.entries.append(TableEntry.new("f%d" % t.entries.size(), item))
t.entries[0].weight = 1
for k in [1, 2, 3]:
t.entries[k].weight = 0
t.dice_mod = "@luck"
var b := TowerBoard.new()
b.tables.append(t)
var ctx := Roller.new_context(b, RandomNumberGenerator.new(), {"luck": {"value": 2}}, {})
var node := RollCards.pick_node(t, ctx)
_eq((node["entry"] as TableEntry).text, "Gem", "+2 moves Nothing down two rows to Gem")
var fact: Dictionary = node["why"][0]
_eq(DescribeWhy._why_roll(fact, Describe._labels(b), true), "Rolled 1 on d4, moved down 2 rows for Luck, to row 3", "the Why line explains the move")
var far := Roller.new_context(b, RandomNumberGenerator.new(), {"luck": {"value": 9}}, {})
_eq((RollCards.pick_node(t, far)["entry"] as TableEntry).text, "Relic", "past the last row counts as the last row")
_eq(RollTable.from_dict(t.to_dict()).dice_mod, "@luck", "a plain table's modifier is saved")
TableSettingsRules.apply_settings(t, {"ranged": true, "dice": "1d4"})
_eq(t.dice_mod, "", "switching to ranged clears it, since it would mean something else")

View file

@ -6,6 +6,20 @@ extends RefCounted
## the settings dialog's rules apply.
## The Modifier field (ModifierEditor), with an optional note under it.
static func _modifier_field(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool, note: String = "") -> void:
var mod := ModifierEditor.new(insp.main, t.dice_mod, plain, "card.dice_mod")
insp.fields["dice_mod"] = mod
var merr := InspectorKit.error_label()
mod.changed.connect(func(s: String) -> void: InspectorKit.show_error(merr, insp.apply_settings({"dice_mod": s})))
var mbox := InspectorKit.vbox(5)
mbox.add_child(mod)
if note != "":
mbox.add_child(InspectorKit.note(note, Style.MUTED, 12))
mbox.add_child(merr)
box.add_child(InspectorKit.field("Modifier", mbox))
static func build(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool) -> void:
var v := InspectorKit.vbox(6)
var ranged := t.is_ranged()
@ -19,9 +33,10 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool
)
v.add_child(seg)
if not ranged:
v.add_child(InspectorKit.note("Each result is equally likely, unless you give it a weight on the card. Switch to Ranged to add a modifier to the roll.", Style.MUTED, 13))
v.add_child(InspectorKit.note("Each result is equally likely, unless you give it a weight on the card.", Style.MUTED, 13))
v.add_child(err)
box.add_child(InspectorKit.field("Rolls", v))
_modifier_field(insp, box, t, plain, "Moves the result down the list (+) or up (−) by that many rows. The first and last results catch anything past them.")
return
var row := InspectorKit.hbox(6)
row.add_child(InspectorKit.word("Roll"))
@ -38,14 +53,7 @@ static func build(insp: Inspector, box: VBoxContainer, t: RollTable, plain: bool
v.add_child(err)
box.add_child(InspectorKit.field("Rolls", v))
var mod := ModifierEditor.new(insp.main, t.dice_mod, plain, "card.dice_mod")
insp.fields["dice_mod"] = mod
var merr := InspectorKit.error_label()
mod.changed.connect(func(s: String) -> void: InspectorKit.show_error(merr, insp.apply_settings({"dice_mod": s})))
var mbox := InspectorKit.vbox(5)
mbox.add_child(mod)
mbox.add_child(merr)
box.add_child(InspectorKit.field("Modifier", mbox))
_modifier_field(insp, box, t, plain)
box.add_child(InspectorKit.field("Limits", limits(insp, t)))
# A live sentence, what the modifier does, and any range problems.

View file

@ -270,7 +270,7 @@ static func apply_settings(t: RollTable, s: Dictionary) -> Dictionary:
return {"ok": false, "error": "“%s” is not a valid dice expression." % dice, "deck_reset": false}
var mod := str(s.get("dice_mod", t.dice_mod)).strip_edges()
var mod_err := mod_error(mod)
if ranged and mod_err != "":
if mod_err != "" and (ranged or s.has("dice_mod")):
return {"ok": false, "error": mod_err, "deck_reset": false}
var was_ranged := t.is_ranged()
var old_dice := t.dice
@ -282,9 +282,15 @@ static func apply_settings(t: RollTable, s: Dictionary) -> Dictionary:
t.tags = parse_tags(str(s.get("tags", "")))
t.card_mode = bool(s.get("card_mode", t.card_mode)) and rolls
t.deck_empty = "stop" if str(s.get("deck_empty", t.deck_empty)) == "stop" else "reshuffle"
# Plain tables keep a modifier too: it moves the pick up or down the list.
if s.has("dice_mod") or ranged:
t.dice_mod = mod
# Switching between plain and ranged changes what a modifier means (rows
# vs. points on the dice), so it starts empty.
if was_ranged != ranged:
t.dice_mod = ""
if ranged:
t.dice = dice
t.dice_mod = mod
if not was_ranged:
assign_ranges(t)
elif was_ranged: