class_name RollCombine extends RefCounted ## Repeated rolls, combined. A thread that rolls its card more than once can ## keep every roll ("all", the default), add them together ("sum"), keep the ## highest or lowest ("max", "min"), or let the player choose one ("choose"). ## When combining, the repeated rolls don't follow their own threads; the kept ## one does, once the combine step has run (Roller.OP_COMBINE). A roll's number ## is its result text when that is a number, else the die total. const MODES := ["all", "sum", "max", "min", "choose"] const LABELS := ["Keep every roll", "Add them together", "Keep the highest", "Keep the lowest", "Let me choose"] static func value_of(n: Dictionary) -> float: var t := Formatter.strip_tags(str(n.get("summary", ""))).strip_edges() if t.is_valid_float(): return t.to_float() return float(n.get("value", 0)) ## Runs the combine frame `f` {node, to, mode, start, save}: returns the node ## whose threads should now run, or {} for none (a total, or nothing rolled). static func run(f: Dictionary, ctx: RollContext) -> Dictionary: var node: Dictionary = f["node"] var kids: Array = [] var all_kids: Array = node["children"] for i in range(int(f["start"]), all_kids.size()): var k: Dictionary = all_kids[i] var t: RollTable = k.get("table") if t and t.id == str(f["to"]) and not k.get("inline", false) and not k.has("stop"): kids.append(k) if kids.is_empty(): return {} var mode := str(f["mode"]) var keep: Dictionary = {} match mode: "sum": var total := 0.0 var parts := PackedStringArray() for k: Dictionary in kids: total += value_of(k) parts.append(_fmt(value_of(k))) k["dropped"] = true var t: RollTable = kids[0]["table"] var sum := {"table": t, "children": [], "summary": _fmt(total), "combined": "sum", "why": [{"type": "combine", "mode": "sum", "parts": parts, "total": total}]} all_kids.append(sum) _save(f, ctx, total, t) return {} "max", "min": keep = kids[0] for k: Dictionary in kids: if (mode == "max" and value_of(k) > value_of(keep)) or (mode == "min" and value_of(k) < value_of(keep)): keep = k "choose": keep = await _ask(kids, ctx) if keep.is_empty(): ctx.halted = Roller.CANCELLED return {} _: return {} for k: Dictionary in kids: if not is_same(k, keep): k["dropped"] = true keep["kept"] = mode _save(f, ctx, Formatter.to_export_bbcode(str(keep.get("summary", ""))), keep["table"]) return keep ## Asks which roll to keep, with the same pop-up as a choice card. static func _ask(kids: Array, ctx: RollContext) -> Dictionary: var t: RollTable = kids[0]["table"] var options: Array = [] for i in kids.size(): options.append({"index": i, "entry": int(kids[i].get("index", 0)), "text": str(kids[i].get("summary", "")), "details": "", "weight": 1}) var req := {"table_id": t.id, "kind": "pick", "title": t.name, "prompt": "Keep which roll?", "options": options, "min": 0, "max": 0, "default": -1, "log": [], "hidden": 0, "why": [], "combine": true} var reply: Variant = await RollChoice.ask(ctx, req) if reply == null: return {} var i := int(reply) if i < 0 or i >= kids.size(): i = ctx.rng.randi_range(0, kids.size() - 1) return kids[i] static func _save(f: Dictionary, ctx: RollContext, v: Variant, t: RollTable) -> void: var save := str(f.get("save", "")) if save != "": ctx.save(save, v, t.id) static func _fmt(x: float) -> String: return str(int(x)) if is_equal_approx(x, roundf(x)) else str(snappedf(x, 0.01))