class_name RangeWeights extends RefCounted ## How Build edits a ranged card (RollTable.range_edit): ## - Weights: each result says how many die faces it covers (TableEntry.weight); ## the ranges are laid out from it in row order (reflow). Dragging a row moves ## its share, and the ranges after it follow. ## - Ranges: each range is typed by hand, as printed books do. With auto_sort on, ## rows put themselves back in roll order after an edit or a drag. ## The ranges stay the stored truth either way, so rolling never looks at this. const WEIGHTS := "weights" const RANGES := "ranges" ## True when the card's ranges come from its weights. static func uses_weights(t: RollTable) -> bool: return t.is_ranged() and t.range_edit == WEIGHTS ## First value the weights lay out from: the dice's lowest roll, or, for a card ## whose totals clamp (a modifier or expression dice), where its first range starts. static func start_value(t: RollTable) -> int: if RollRange.clamps(t): for e in t.entries: if e.has_range: return e.range_lo return RollRange.natural(t).x ## Lays the ranges out from the weights, in row order. Weight 0 = no range. static func reflow(t: RollTable) -> void: var next := start_value(t) for e in t.entries: if e.weight <= 0: e.has_range = false continue e.has_range = true e.range_lo = next e.range_hi = next + e.weight - 1 next = e.range_hi + 1 ## Switches how the card is edited. Going to Weights reads each weight off its ## range and turns every gap between ranges into an empty result, so no roll ## changes. Going to Ranges keeps the ranges as they are. static func set_mode(t: RollTable, mode: String) -> void: if mode == t.range_edit: return t.range_edit = mode if mode == WEIGHTS: to_weights(t) elif t.auto_sort: sort_rows(t) static func to_weights(t: RollTable) -> void: sort_rows(t) var out: Array[TableEntry] = [] var next := start_value(t) for e in t.entries: if not e.has_range: e.weight = 0 out.append(e) continue if e.range_lo > next: var gap := TableEntry.new(TowerBoard.new_id("e"), "") gap.weight = e.range_lo - next out.append(gap) # Overlaps can't be kept as weights: the later row starts after the earlier one. e.weight = maxi(1, e.range_hi - maxi(e.range_lo, next) + 1) next = maxi(next, e.range_hi + 1) out.append(e) t.entries = out reflow(t) ## Puts rows in roll order (by range start); rows without a range keep their ## order at the end. Returns true when anything moved. static func sort_rows(t: RollTable) -> bool: var before := t.entries.map(func(e: TableEntry) -> String: return e.id) var ranged: Array[TableEntry] = [] var rest: Array[TableEntry] = [] for e in t.entries: (ranged if e.has_range else rest).append(e) ranged.sort_custom(func(a: TableEntry, b: TableEntry) -> bool: return a.range_lo < b.range_lo) var sorted: Array[TableEntry] = [] sorted.append_array(ranged) sorted.append_array(rest) t.entries = sorted return sorted.map(func(e: TableEntry) -> String: return e.id) != before ## After a weight, range or row-order edit: reflow (Weights) or auto-sort ## (Ranges). Returns true when auto-sort moved rows. static func after_edit(t: RollTable) -> bool: if not t.is_ranged(): return false if t.range_edit == WEIGHTS: reflow(t) return false return t.auto_sort and sort_rows(t) ## "Weights add up to 95 of 100: 5 short." when the weights don't cover the ## dice exactly; "" when they do, or when the card isn't edited by weight. static func total_problem(t: RollTable) -> String: if not uses_weights(t) or t.is_expr_dice(): return "" var total := 0 for e in t.entries: total += maxi(0, e.weight) var nat := RollRange.natural(t) var size := nat.y - nat.x + 1 if total == size or (total > size and RollRange.clamps(t)): return "" var off := "%d short" % (size - total) if total < size else "%d too many" % (total - size) return "Weights add up to %d of %d: %s." % [total, size, off]