class_name RollContext extends RefCounted ## State for one top-level roll: chain variables, the collection's persistent ## variables, the RNG, the current depth and the hooks back into the roller. ## ## Create one per top-level roll and share it with every table rolled in that ## chain (inline references and pipes alike). ## ## [b]Hooks the roller must set:[/b] ## - [member resolve_table]: [code]func(name_or_id: String) -> String[/code]. Returns ## the table id, or "" when there is no such table. It should match the id ## exactly or the name case-insensitively, ignoring BBCode in names; ## [method normalize_name] does the name cleanup. ## - [member roll_table]: [code]func(table_id: String, depth: int) -> Dictionary[/code]. ## Rolls the table at the given depth and returns a result node. ## ## [b]Node contract for roll_table:[/b] ## - Returns [code]{}[/code] (empty) if the table doesn't exist; it is then shown ## with the missing-table marker. ## - Otherwise the node must have [code]"summary": String[/code], the BBCode text ## that replaces the reference inline (normally the node's expanded entry text). ## It may contain internal [url] markers from deeper expansions. ## - Every other key (table, entry, value, children, stop, ...) belongs to the ## roller; TextExpander only collects the nodes in its "children" output. ## - When the roller itself stops (depth limit, empty table), it still returns a ## node whose summary explains why, for example [method TextExpander.deep_marker]. ## - roll_table may change [member depth] while it runs; TextExpander restores it. const DEFAULT_MAX_DEPTH := 32 ## Chain variables: name (no "@") -> String value. Fresh for every top-level roll. var chain_vars: Dictionary = {} ## The collection's persistent variables, owned and saved elsewhere: ## name (without the "@") -> {"value": String, "source": table_id}. ## This is the caller's Dictionary itself, not a copy, so saves show up there. var persistent_vars: Dictionary = {} ## True once save() has written a persistent variable in this roll, so the caller ## knows local state needs saving. var persistent_changed: bool = false ## Depth of the text currently being expanded. References in that text are ## rolled at this depth. The roller sets it (depth + 1 for an entry's text). var depth: int = 0 ## References at a depth greater than this show the "chain too deep" marker. var max_depth: int = DEFAULT_MAX_DEPTH var rng: RandomNumberGenerator = null ## func(name_or_id: String) -> String (table id or ""). var resolve_table: Callable = Callable() ## func(table_id: String, depth: int) -> Dictionary (a result node). var roll_table: Callable = Callable() func _init(p_persistent: Dictionary = {}, p_rng: RandomNumberGenerator = null) -> void: persistent_vars = p_persistent rng = p_rng if p_rng != null else RandomNumberGenerator.new() ## True for names that start with "@" (persistent variables). static func is_persistent(name: String) -> bool: return name.strip_edges().begins_with("@") ## Saves a value. Names starting with "@" go to the persistent variables ## (stored without the "@", with the source table id); others to the chain. func save(name: String, value: String, source_id: String = "") -> void: var n: String = name.strip_edges() if n.begins_with("@"): n = n.substr(1) if n == "": return persistent_vars[n] = {"value": value, "source": source_id} persistent_changed = true elif n != "": chain_vars[n] = value ## Returns the saved String value, or null when the variable isn't set. ## "@name" reads a persistent variable, "name" a chain variable. func lookup(name: String) -> Variant: var n: String = name.strip_edges() if n.begins_with("@"): var entry: Variant = persistent_vars.get(n.substr(1)) if entry is Dictionary: return str((entry as Dictionary).get("value", "")) if entry != null: return str(entry) return null if chain_vars.has(n): return str(chain_vars[n]) return null ## Calls resolve_table, returning "" if the hook isn't set. func resolve(name_or_id: String) -> String: if not resolve_table.is_valid(): return "" return str(resolve_table.call(name_or_id)) ## Calls roll_table at the current depth and restores depth afterwards. ## Returns {} if the hook isn't set or the roller returned something that isn't ## a Dictionary. func roll(table_id: String) -> Dictionary: if not roll_table.is_valid(): return {} var d: int = depth var result: Variant = roll_table.call(table_id, d) depth = d return result as Dictionary if result is Dictionary else {} ## Lowercase, trimmed, BBCode-free form of a table name, for matching ## [[references]] against names: normalize_name("[b]Gems[/b] ") == "gems". static func normalize_name(name: String) -> String: return Formatter.strip_tags(name).strip_edges().to_lower()