class_name RulesView extends RefCounted ## The display side of docs/PLAIN.md §5: whether rules show in plain words ## (Describe) or as their stored text, and short display helpers that pick ## between the two. ## ## The switch itself lives in Main (the Build inspector work): when Main has ## rules_plain() that decides, else its StateStore's "rules_view" setting, else ## an in-memory default (plain, or text with the --rules=text screenshot flag). ## Displays refresh on Main's `rules_view_changed` signal when it has one ## (watch()). ## Used when Main has neither rules_plain() nor a StateStore (--sample, tests). static var fallback_plain := true static var _args_read := false ## Longest pipe label drawn on the board before it's cut with "…". const PIPE_CHARS := 46 ## True when rules should read in plain words. static func plain(main: Object) -> bool: if main != null: if main.has_method("rules_plain"): return bool(main.call("rules_plain")) var st: Variant = main.get("state") if st is StateStore: return (st as StateStore).rules_plain() if not _args_read: _args_read = true if OS.get_cmdline_user_args().has("--rules=text"): fallback_plain = false return fallback_plain ## Calls `callable` whenever Main's rules view changes (when Main has the ## signal). Safe to call more than once with the same callable. static func watch(main: Object, callable: Callable) -> void: if main == null or not main.has_signal("rules_view_changed"): return if not main.is_connected("rules_view_changed", callable): main.connect("rules_view_changed", callable) ## An effects field as one short line: "Luck −1 · Gold + 2D" (plain) or ## "@luck -= 1 · @gold += 2d6" (text). At most `most` statements, then "+N". static func effects_line(text: String, board: TowerBoard, table: RollTable, is_plain: bool, most: int = 2) -> String: var list: Array = Describe.effects(text, board, table) if is_plain else Array(Effects.split(text)) var parts := PackedStringArray() for k in mini(most, list.size()): parts.append(str(list[k])) if list.size() > most: parts.append("+%d" % (list.size() - most)) return " · ".join(parts) ## A condition in plain words or as written. static func condition(expr: String, board: TowerBoard, is_plain: bool) -> String: return Describe.condition(expr, board) if is_plain else expr.strip_edges() ## A router case: "if Strength is 10 or more" / "if @str >= 10", or ## "Otherwise". static func case_label(e: TableEntry, board: TowerBoard, is_plain: bool) -> String: var w := e.when.strip_edges() if w == "": return "Otherwise" return "if " + condition(w, board, is_plain) ## A loop's rule: "repeat while Health is more than 0", "repeat until Done is ## set", "× Terms" (plain); "while @hp > 0", "× @terms" (text). static func loop_rule(t: RollTable, board: TowerBoard, is_plain: bool) -> String: if not is_plain: return TableCard.loop_rule(t) var e := t.loop_expr.strip_edges() if e == "": return "× ?" if t.loop_mode == "times" else "repeat %s ?" % t.loop_mode match t.loop_mode: "while": return "repeat while " + Describe.condition(e, board) "until": return "repeat until " + Describe.condition(e, board) return "× " + Describe.dice(e, board) ## A pick card's option source: "Options: your Skills, level 1 or more" or ## "Options from @skill_* · where option_value >= 1". static func options_line(t: RollTable, board: TowerBoard, is_plain: bool) -> String: if not is_plain: return TableCard.options_line(t) var d := Describe.options(t, board) return "Options: " + (d if d != "" else t.options_from.strip_edges()) ## A pick option's show_if: "only if Hit points is at most 6". static func show_if_line(e: TableEntry, board: TowerBoard, is_plain: bool) -> String: return "only if " + condition(e.show_if, board, is_plain) ## A dice expression: "2D + Luck" or "2d6 + @luck". static func dice(expr: String, board: TowerBoard, is_plain: bool) -> String: return Describe.dice(expr, board) if is_plain else expr.strip_edges() ## A ranged roll table's roll: "Roll d20 + Luck" or "1d20 + @luck". "" when ## it isn't ranged. static func roll_line(t: RollTable, board: TowerBoard, is_plain: bool) -> String: if t.kind != RollTable.KIND_ROLL or not t.is_ranged(): return "" return Describe.roll(t, board) if is_plain else t.roll_text() ## A check card's rule: "Roll 2D + Strength modifier, need 8 or more" or ## "2d6 + dm(@str) vs 8+". static func check_line(t: RollTable, board: TowerBoard, is_plain: bool) -> String: if is_plain: return Describe.check(t, board) var roll := RollRange.join(t.check_dice, t.check_mod) return "%s vs %s%s" % [roll, t.check_target.strip_edges(), "-" if t.check_cmp == "le" else "+"] ## The two outlet labels of a check card ([success, failure]): "8+" and "≤7" ## for a number target, else "✓" and "✗". static func check_outlets(t: RollTable) -> Array[String]: var tg := t.check_target.strip_edges() if not tg.is_valid_int(): return ["✓", "✗"] var n := tg.to_int() if t.check_cmp == "le": return ["≤%d" % n, "%d+" % (n + 1)] return ["%d+" % n, "≤%d" % (n - 1)] ## A pipe's label: Describe.pipe (plain) or PipeLayer's text tag, cut to ## PIPE_CHARS. static func pipe_label(e: TableEntry, to: String, board: TowerBoard, is_plain: bool) -> String: var s := Describe.pipe(e, to, board) if is_plain else PipeLayer._pipe_tag(e, to) return cut(s, PIPE_CHARS) ## A value's name: "Strength" or "@str". static func value_name(name: String, board: TowerBoard, is_plain: bool) -> String: return Describe.label(name, board) if is_plain else name ## `s`, or its first `most` characters and "…". static func cut(s: String, most: int) -> String: if s.length() <= most: return s return s.substr(0, most - 1).strip_edges() + "…"