class_name SheetValues extends RefCounted ## What a sheet's boxes are worth as values: for formulas, dice buttons, live ## {@name} text and (later) tables. A box counts once it has a name for ## tables (SheetBox.var_name). Each kind reads as one plain value: a number ## its number, a bar its current value, a checkbox 1 or 0, a list how many ## entries, currency its total, a formula its result. ## Formulas that use formulas stop this deep (a loop reads as 0). const MAX_DEPTH := 8 ## Every named box's value: {var_name: value}. static func vars(doc: SheetDoc, depth: int = 0) -> Dictionary: var out := {} for tab in doc.tabs: for box: SheetBox in tab["boxes"]: if box.var_name != "": out[box.var_name] = value_of(doc, box, depth) return out static func value_of(doc: SheetDoc, box: SheetBox, depth: int = 0) -> Variant: var v: Variant = box.value match box.kind: SheetKinds.NUMBER, SheetKinds.TICKS, SheetKinds.CLOCK, SheetKinds.DOTS, SheetKinds.LADDER, SheetKinds.XP: return number(v) SheetKinds.CHECKBOX: return 1 if bool(v) else 0 SheetKinds.RESOURCE: return number((v as Dictionary).get("cur", 0)) if v is Dictionary else 0 SheetKinds.LIST, SheetKinds.SLOTS, SheetKinds.CONDITIONS: return (v as Array).size() if v is Array else 0 SheetKinds.SPELL_SLOTS: return slots_left(box) SheetKinds.CURRENCY: return currency_total(box) SheetKinds.FORMULA: return formula(doc, box, depth)["value"] SheetKinds.HEADING, SheetKinds.DIVIDER, SheetKinds.DICE: return null return str(v) if v != null else "" ## A formula box's result: {"value", "error"}. A formula that reads itself, ## directly or through other formulas, is an error. static func formula(doc: SheetDoc, box: SheetBox, depth: int = 0) -> Dictionary: if depth == 0 and _loops(doc, box, []): return {"value": null, "error": "This formula uses itself"} return evaluate(doc, str(box.config.get("expr", "")), depth) ## True when following the @names a formula reads leads back to a box on `path`. static func _loops(doc: SheetDoc, box: SheetBox, path: Array) -> bool: if path.has(box.id): return true var next := path + [box.id] for name: String in Expr.names_in(str(box.config.get("expr", ""))): for tab in doc.tabs: for other: SheetBox in tab["boxes"]: if other.kind == SheetKinds.FORMULA and other.var_name != "" and other.var_name == name.trim_prefix("@"): if _loops(doc, other, next): return true return false ## Works out `src` (the rule language, docs/LOGIC.md §4) with the sheet's ## values as @variables. Dice in it roll. {"value", "error"}. static func evaluate(doc: SheetDoc, src: String, depth: int = 0, rng: RandomNumberGenerator = null) -> Dictionary: if src.strip_edges() == "": return {"value": null, "error": ""} if depth >= MAX_DEPTH: return {"value": 0, "error": "A formula uses itself"} return Expr.evaluate(src, RollContext.new(vars(doc, depth + 1), rng)) static func number(v: Variant) -> float: if v is int or v is float: return float(v) if v is bool: return 1.0 if v else 0.0 if v is String and (v as String).strip_edges().is_valid_float(): return (v as String).to_float() return 0.0 ## "14" for whole numbers, "2.5" otherwise. static func show(v: Variant) -> String: if v == null: return "—" if v is int or v is float: var n := float(v) return str(int(n)) if n == floorf(n) else str(snappedf(n, 0.01)) return str(v) static func currency_total(box: SheetBox) -> float: var total := 0.0 var held: Dictionary = box.value if box.value is Dictionary else {} for coin: Dictionary in box.config.get("coins", []): total += number(held.get(coin.get("name", ""), 0)) * number(coin.get("rate", 1)) return total static func slots_left(box: SheetBox) -> int: var used: Array = box.value if box.value is Array else [] var left := 0 var levels: Array = box.config.get("levels", []) for i in levels.size(): left += int(number((levels[i] as Dictionary).get("max", 0))) - (int(number(used[i])) if i < used.size() else 0) return left ## A list's point pool: {"total", "spent", "left"} (total 0 = no pool). static func pool(box: SheetBox) -> Dictionary: var total := number(box.config.get("pool", 0)) var column := str(box.config.get("cost_column", "Cost")) var spent := 0.0 for entry: Variant in (box.value if box.value is Array else []): if entry is Dictionary: spent += number((entry as Dictionary).get(column, 0)) return {"total": total, "spent": spent, "left": total - spent} ## The XP box's level (1 = first) and the next threshold (-1 at the top). static func xp_level(box: SheetBox) -> Dictionary: var xp := number(box.value) var levels: Array = box.config.get("levels", [0]) var level := 1 for i in levels.size(): if xp >= number(levels[i]): level = i + 1 var next := number(levels[level]) if level < levels.size() else -1.0 var prev := number(levels[level - 1]) if level - 1 < levels.size() else 0.0 return {"level": level, "next": next, "prev": prev}