Costs on choice options
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>
This commit is contained in:
parent
fe133d3a94
commit
25018ebdf3
7 changed files with 149 additions and 5 deletions
|
|
@ -28,8 +28,15 @@ static func request_for(t: RollTable, ctx: RollContext) -> Dictionary:
|
||||||
if not option_shown(e, ctx, log):
|
if not option_shown(e, ctx, log):
|
||||||
hidden += 1
|
hidden += 1
|
||||||
continue
|
continue
|
||||||
options.append({"index": k, "entry": k, "text": TextExpander.render(e.text, ctx), "details": e.details,
|
var o := {"index": k, "entry": k, "text": TextExpander.render(e.text, ctx), "details": e.details,
|
||||||
"weight": e.span() if ranged else maxi(0, e.weight)})
|
"weight": e.span() if ranged else maxi(0, e.weight)}
|
||||||
|
if RollCosts.has_cost(e):
|
||||||
|
o["cost"] = RollCosts.describe(e, ctx.board)
|
||||||
|
var why_not := RollCosts.blocked(e, ctx, ctx.board)
|
||||||
|
if why_not != "":
|
||||||
|
o["disabled"] = why_not
|
||||||
|
o["weight"] = 0
|
||||||
|
options.append(o)
|
||||||
if t.is_dynamic() and not t.entries.is_empty() and not option_shown(t.entries[0], ctx, log):
|
if t.is_dynamic() and not t.entries.is_empty() and not option_shown(t.entries[0], ctx, log):
|
||||||
chosen_hidden = true
|
chosen_hidden = true
|
||||||
elif t.is_dynamic() and not t.entries.is_empty():
|
elif t.is_dynamic() and not t.entries.is_empty():
|
||||||
|
|
@ -187,14 +194,14 @@ static func run(t: RollTable, f: Dictionary, ctx: RollContext, stack: Array) ->
|
||||||
var idx := int(reply) if (reply is int or reply is float) else -1
|
var idx := int(reply) if (reply is int or reply is float) else -1
|
||||||
var opt: Dictionary = {}
|
var opt: Dictionary = {}
|
||||||
for o: Dictionary in opts:
|
for o: Dictionary in opts:
|
||||||
if int(o["index"]) == idx:
|
if int(o["index"]) == idx and not o.has("disabled"):
|
||||||
opt = o
|
opt = o
|
||||||
break
|
break
|
||||||
var node: Dictionary
|
var node: Dictionary
|
||||||
if not opt.is_empty():
|
if not opt.is_empty():
|
||||||
node = _option_node(t, opt, ctx)
|
node = _option_node(t, opt, ctx)
|
||||||
node["picked"] = true
|
node["picked"] = true
|
||||||
elif not t.is_dynamic() and int(req["hidden"]) == 0:
|
elif not t.is_dynamic() and int(req["hidden"]) == 0 and not opts.any(func(o: Dictionary) -> bool: return o.has("cost")):
|
||||||
# A plain fixed list: Random rolls the card the usual way
|
# A plain fixed list: Random rolls the card the usual way
|
||||||
# (weights, ranges, card mode).
|
# (weights, ranges, card mode).
|
||||||
node = RollCards.pick_node(t, ctx)
|
node = RollCards.pick_node(t, ctx)
|
||||||
|
|
@ -208,6 +215,8 @@ static func run(t: RollTable, f: Dictionary, ctx: RollContext, stack: Array) ->
|
||||||
node = _option_node(t, opts[k], ctx)
|
node = _option_node(t, opts[k], ctx)
|
||||||
node["random"] = true
|
node["random"] = true
|
||||||
node["kind"] = RollTable.KIND_CHOICE
|
node["kind"] = RollTable.KIND_CHOICE
|
||||||
|
if node.has("entry"):
|
||||||
|
RollCosts.pay(node["entry"], ctx, t, node)
|
||||||
Roller._log(node, req["log"])
|
Roller._log(node, req["log"])
|
||||||
Roller._why(node, req["why"])
|
Roller._why(node, req["why"])
|
||||||
Roller._attach(f, node, ctx)
|
Roller._attach(f, node, ctx)
|
||||||
|
|
|
||||||
51
core/roll_costs.gd
Normal file
51
core/roll_costs.gd
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
class_name RollCosts
|
||||||
|
extends RefCounted
|
||||||
|
## Costs on choice options: an option can cost an amount of a value
|
||||||
|
## (TableEntry.cost_value, cost_amount). It's offered greyed out, with the
|
||||||
|
## reason, when the value is too low; picking it takes the amount away.
|
||||||
|
## A "Reroll?" option that pipes back to a card and costs a point makes a
|
||||||
|
## spend-a-point-to-reroll table.
|
||||||
|
|
||||||
|
|
||||||
|
## "" when the option can be afforded now, else why not (shown to the player).
|
||||||
|
static func blocked(e: TableEntry, ctx: RollContext, board: TowerBoard = null) -> String:
|
||||||
|
if not has_cost(e):
|
||||||
|
return ""
|
||||||
|
var have := _number(Expr.evaluate(var_ref(e), ctx))
|
||||||
|
var need := _number(Expr.evaluate(e.cost_amount, ctx))
|
||||||
|
if have >= need:
|
||||||
|
return ""
|
||||||
|
return "Needs %s %s (you have %s)" % [Expr.format_value(need), label(e, board), Expr.format_value(have)]
|
||||||
|
|
||||||
|
|
||||||
|
static func has_cost(e: TableEntry) -> bool:
|
||||||
|
return e.cost_value.strip_edges() != "" and e.cost_amount.strip_edges() != ""
|
||||||
|
|
||||||
|
|
||||||
|
## "Costs 1 Luck".
|
||||||
|
static func describe(e: TableEntry, board: TowerBoard = null) -> String:
|
||||||
|
return "Costs %s %s" % [e.cost_amount.strip_edges(), label(e, board)] if has_cost(e) else ""
|
||||||
|
|
||||||
|
|
||||||
|
## Takes the cost away (an ordinary change, so it shows in the result's
|
||||||
|
## changes and the Why trace).
|
||||||
|
static func pay(e: TableEntry, ctx: RollContext, t: RollTable, node: Dictionary) -> void:
|
||||||
|
if not has_cost(e):
|
||||||
|
return
|
||||||
|
var facts: Array = []
|
||||||
|
Roller._log(node, Effects.run("%s -= %s" % [var_ref(e), e.cost_amount.strip_edges()], ctx, t.id, facts))
|
||||||
|
Roller._why(node, facts)
|
||||||
|
|
||||||
|
|
||||||
|
static func var_ref(e: TableEntry) -> String:
|
||||||
|
var v := e.cost_value.strip_edges()
|
||||||
|
return v if v.begins_with("@") else "@" + v
|
||||||
|
|
||||||
|
|
||||||
|
static func label(e: TableEntry, board: TowerBoard) -> String:
|
||||||
|
return Describe.label(var_ref(e).trim_prefix("@"), board) if board else var_ref(e).trim_prefix("@")
|
||||||
|
|
||||||
|
|
||||||
|
static func _number(r: Dictionary) -> float:
|
||||||
|
var v: Variant = r.get("value")
|
||||||
|
return float(v) if r.get("error", "") == "" and (v is int or v is float) else 0.0
|
||||||
1
core/roll_costs.gd.uid
Normal file
1
core/roll_costs.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://drlg4yx1p2cs1
|
||||||
|
|
@ -45,6 +45,10 @@ var pipe_combine: Dictionary = {}
|
||||||
## target table id -> true: the roll waits before following this thread until
|
## target table id -> true: the roll waits before following this thread until
|
||||||
## the player continues (Roller.OP_PAUSE). Missing = no pause.
|
## the player continues (Roller.OP_PAUSE). Missing = no pause.
|
||||||
var pipe_pause: Dictionary = {}
|
var pipe_pause: Dictionary = {}
|
||||||
|
## Choice options: what picking this costs (RollCosts): a value name and an
|
||||||
|
## amount (an expression). "" = free.
|
||||||
|
var cost_value: String = ""
|
||||||
|
var cost_amount: String = ""
|
||||||
## Router cards: this case's condition. "" is the Otherwise case (always
|
## Router cards: this case's condition. "" is the Otherwise case (always
|
||||||
## matches). Ignored on other kinds.
|
## matches). Ignored on other kinds.
|
||||||
var when: String = ""
|
var when: String = ""
|
||||||
|
|
@ -274,6 +278,8 @@ func to_dict() -> Dictionary:
|
||||||
d["range"] = [range_lo, range_hi]
|
d["range"] = [range_lo, range_hi]
|
||||||
if page != "":
|
if page != "":
|
||||||
d["page"] = page
|
d["page"] = page
|
||||||
|
if cost_value.strip_edges() != "":
|
||||||
|
d["cost"] = {"value": cost_value.strip_edges(), "amount": cost_amount.strip_edges()}
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -283,6 +289,10 @@ static func from_dict(d: Dictionary) -> TableEntry:
|
||||||
e.weight = maxi(0, int(d.get("weight", 1)))
|
e.weight = maxi(0, int(d.get("weight", 1)))
|
||||||
e.effects = str(d.get("effects", ""))
|
e.effects = str(d.get("effects", ""))
|
||||||
e.page = str(d.get("page", ""))
|
e.page = str(d.get("page", ""))
|
||||||
|
var cost: Variant = d.get("cost")
|
||||||
|
if cost is Dictionary:
|
||||||
|
e.cost_value = str(cost.get("value", ""))
|
||||||
|
e.cost_amount = str(cost.get("amount", ""))
|
||||||
e.when = str(d.get("when", "")).strip_edges() if d.get("when") != null else ""
|
e.when = str(d.get("when", "")).strip_edges() if d.get("when") != null else ""
|
||||||
e.show_if = str(d.get("show_if", "")).strip_edges() if d.get("show_if") != null else ""
|
e.show_if = str(d.get("show_if", "")).strip_edges() if d.get("show_if") != null else ""
|
||||||
for p: Variant in d.get("pipes", []):
|
for p: Variant in d.get("pipes", []):
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ func _init() -> void:
|
||||||
_test_combine()
|
_test_combine()
|
||||||
await _test_choose()
|
await _test_choose()
|
||||||
await _test_pause()
|
await _test_pause()
|
||||||
|
await _test_costs()
|
||||||
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
||||||
quit(1 if _failures else 0)
|
quit(1 if _failures else 0)
|
||||||
|
|
||||||
|
|
@ -112,6 +113,41 @@ func _test_pause() -> void:
|
||||||
_check(back.pipe_pause.has("gold"), "the pause is saved")
|
_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:
|
func _check(ok: bool, what: String) -> void:
|
||||||
if not ok:
|
if not ok:
|
||||||
_failures += 1
|
_failures += 1
|
||||||
|
|
|
||||||
|
|
@ -182,8 +182,19 @@ func _option_row(k: int, o: Dictionary) -> Control:
|
||||||
var fit := func() -> void: b.custom_minimum_size.y = row.get_combined_minimum_size().y + 17.0
|
var fit := func() -> void: b.custom_minimum_size.y = row.get_combined_minimum_size().y + 17.0
|
||||||
row.minimum_size_changed.connect(fit)
|
row.minimum_size_changed.connect(fit)
|
||||||
fit.call_deferred()
|
fit.call_deferred()
|
||||||
|
if o.has("cost"):
|
||||||
|
# What it costs; greyed out with the reason when it can't be afforded.
|
||||||
|
var cost := Style.make_label(str(o.get("disabled", o["cost"])), Style.body_bold(), 13, Style.DANGER_INK if o.has("disabled") else Style.TRIM_DARK)
|
||||||
|
cost.mouse_filter = MOUSE_FILTER_IGNORE
|
||||||
|
col.add_child(cost)
|
||||||
|
if o.has("disabled"):
|
||||||
|
b.disabled = true
|
||||||
|
b.modulate.a = 0.55
|
||||||
|
b.tooltip_text = str(o["disabled"])
|
||||||
var index := int(o.get("index", k))
|
var index := int(o.get("index", k))
|
||||||
b.pressed.connect(func() -> void: answer(index))
|
b.pressed.connect(func() -> void:
|
||||||
|
if not b.disabled:
|
||||||
|
answer(index))
|
||||||
b.mouse_entered.connect(func() -> void: _set_hover(k))
|
b.mouse_entered.connect(func() -> void: _set_hover(k))
|
||||||
b.mouse_exited.connect(func() -> void:
|
b.mouse_exited.connect(func() -> void:
|
||||||
if _hover == k:
|
if _hover == k:
|
||||||
|
|
|
||||||
|
|
@ -517,6 +517,31 @@ func _book_line(t: RollTable, e: TableEntry) -> Control:
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
## "[1] of [Points ▾]": what picking this option costs (RollCosts).
|
||||||
|
func _cost_row(t: RollTable, e: TableEntry) -> Control:
|
||||||
|
var row := InspectorKit.flow()
|
||||||
|
var amount := InspectorKit.amount(e.cost_amount, "0")
|
||||||
|
amount.tooltip_text = "How much picking this takes away (0 or empty: free)"
|
||||||
|
row.add_child(amount)
|
||||||
|
row.add_child(InspectorKit.word("of"))
|
||||||
|
var picker := ValuePicker.new(main, e.cost_value.trim_prefix("@"))
|
||||||
|
row.add_child(picker)
|
||||||
|
var apply := func() -> void:
|
||||||
|
var a := amount.text.strip_edges()
|
||||||
|
e.cost_amount = "" if a == "" or a == "0" else a
|
||||||
|
if e.cost_amount == "":
|
||||||
|
e.cost_value = ""
|
||||||
|
wrote(t)
|
||||||
|
amount.text_changed.connect(func(_s: String) -> void: apply.call())
|
||||||
|
picker.value_chosen.connect(func(n: String) -> void:
|
||||||
|
e.cost_value = "@" + n.trim_prefix("@")
|
||||||
|
if amount.text.strip_edges() == "":
|
||||||
|
amount.text = "1"
|
||||||
|
apply.call())
|
||||||
|
row.add_child(InspectorKit.note("Greyed out in the pop-up when there isn't enough.", Style.MUTED, 12))
|
||||||
|
return row
|
||||||
|
|
||||||
|
|
||||||
func _card_link(t: RollTable) -> Button:
|
func _card_link(t: RollTable) -> Button:
|
||||||
var b := InspectorKit.link("On %s · edit the card" % _title_of(t.name, "this card"), "Show the card's settings")
|
var b := InspectorKit.link("On %s · edit the card" % _title_of(t.name, "this card"), "Show the card's settings")
|
||||||
b.add_theme_color_override("font_color", Style.MUTED)
|
b.add_theme_color_override("font_color", Style.MUTED)
|
||||||
|
|
@ -548,6 +573,7 @@ func _result_section(t: RollTable, e: TableEntry) -> void:
|
||||||
show_if_builder.setup(main, e.show_if, not plain, "res.show_if", {"empty_note": "Always offered."})
|
show_if_builder.setup(main, e.show_if, not plain, "res.show_if", {"empty_note": "Always offered."})
|
||||||
show_if_builder.changed.connect(set_entry_show_if)
|
show_if_builder.changed.connect(set_entry_show_if)
|
||||||
_content.add_child(InspectorKit.field("Offer only if", show_if_builder))
|
_content.add_child(InspectorKit.field("Offer only if", show_if_builder))
|
||||||
|
_content.add_child(InspectorKit.field("Costs", _cost_row(t, e)))
|
||||||
|
|
||||||
if t.kind == RollTable.KIND_ROUTER:
|
if t.kind == RollTable.KIND_ROUTER:
|
||||||
_content.add_child(InspectorCard.case_block(self, t, e))
|
_content.add_child(InspectorCard.case_block(self, t, e))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue