class_name SpreadLinks extends RefCounted ## Rolling a card from another spread: [[Treasure::Loot]] in result text rolls ## the Loot card of the spread called Treasure and puts its result in the text ## (with its own threads and references, which resolve inside Treasure). The ## roll uses this spread's values, so the other spread's changes (@gold += 10) ## land here; its decks are not kept between rolls. ## ## A linked card's id is "::". The app sets `loader` (a ## spread's id or name -> TowerBoard or null); without it, links are missing. const SEP := "::" ## func(spread_id_or_name: String) -> TowerBoard (or null). Set by Main. static var loader: Callable = Callable() static func is_link(name_or_id: String) -> bool: return name_or_id.contains(SEP) ## "Treasure::Loot" -> the linked id "treasure::t12", or "" when the spread or ## the card can't be found. static func resolve(name_or_id: String) -> String: var parts := name_or_id.split(SEP, true, 1) if parts.size() != 2: return "" var other := load_board(parts[0]) if other == null: return "" var card_id := Roller.resolve(other, parts[1]) return "" if card_id == "" else "%s%s%s" % [other.id, SEP, card_id] static func load_board(spread: String) -> TowerBoard: if not loader.is_valid() or spread.strip_edges() == "": return null var found: Variant = loader.call(spread.strip_edges()) return found if found is TowerBoard else null ## The card a linked id names, or null. static func card(linked_id: String) -> RollTable: var parts := linked_id.split(SEP, true, 1) var other := load_board(parts[0]) if parts.size() == 2 else null return other.get_table(parts[1]) if other else null ## Rolls a linked card inside its own spread: while it runs, the context's ## spread (and its decks) are the other spread's; then they're put back. static func roll_async(linked_id: String, depth: int, ctx: RollContext) -> Dictionary: var visit := _enter(linked_id, ctx) if visit.is_empty(): return {} var node: Dictionary = await Roller._chain(visit["board"], visit["card"], depth, ctx) return _leave(visit, node, ctx) ## The same without waiting (headless rolls: tests, Test spread). static func roll_sync(linked_id: String, depth: int, ctx: RollContext) -> Dictionary: var visit := _enter(linked_id, ctx) if visit.is_empty(): return {} var node: Dictionary = Roller._chain_sync(visit["board"], visit["card"], depth, ctx) return _leave(visit, node, ctx) static func _enter(linked_id: String, ctx: RollContext) -> Dictionary: var parts := linked_id.split(SEP, true, 1) var other := load_board(parts[0]) if parts.size() == 2 else null if other == null or other.get_table(parts[1]) == null: return {} var visit := {"board": other, "card": parts[1], "home": ctx.board, "decks": ctx.get_meta("decks", {})} ctx.board = other ctx.set_meta("decks", {}) ctx.forget_declarations() return visit static func _leave(visit: Dictionary, node: Dictionary, ctx: RollContext) -> Dictionary: ctx.board = visit["home"] ctx.set_meta("decks", visit["decks"]) ctx.forget_declarations() # Every node from the other spread is marked, so this spread's cards with # the same ids aren't lit or animated for them. var stack: Array = [] if node.is_empty() else [node] while not stack.is_empty(): var each: Dictionary = stack.pop_back() each["spread"] = (visit["board"] as TowerBoard).name stack.append_array(each.get("children", [])) return node