class_name Roller extends RefCounted ## Rolls a table and follows its pipes, producing a result tree. ## ## Each node is a Dictionary: ## table: RollTable, value: int (die value), index: int, entry: TableEntry, ## children: Array of nodes, and "stop": String when the chain couldn't continue. const MAX_DEPTH := 12 ## Rolls `table_id` and everything its pipes lead to. `decks` maps table id to ## DeckState for card-mode tables; missing decks are created in it on demand. ## Extra node keys: "save_as" (on nodes reached through a pipe that saves), ## "reshuffled" (card deck was refilled for this draw). static func roll(board: TowerBoard, table_id: String, rng: RandomNumberGenerator, depth: int = 0, decks: Dictionary = {}) -> Dictionary: var t := board.get_table(table_id) if t == null: return {} if depth > MAX_DEPTH: return {"table": t, "stop": "Chain stopped after %d tables" % MAX_DEPTH, "children": []} if t.entries.is_empty(): return {"table": t, "stop": "This table has no results", "children": []} var index: int var value: int var reshuffled := false if t.card_mode: if not decks.has(t.id): decks[t.id] = DeckState.new() var deck: DeckState = decks[t.id] if deck.cards_left(t).y <= 0: return {"table": t, "stop": "This table has no results", "children": []} if deck.cards_left(t).x <= 0: if t.deck_empty == "stop": return {"table": t, "stop": "Deck empty", "children": []} deck.reshuffle() reshuffled = true var card := deck.draw(t, rng) index = int(card["index"]) value = int(card["value"]) elif t.is_ranged(): value = t.dice_obj().roll(rng) index = t.entry_index_for(value) if index < 0: return {"table": t, "value": value, "stop": "No result for %s" % t.fmt(value), "children": []} else: index = _pick_weighted(t, rng) if index < 0: return {"table": t, "stop": "No result can come up", "children": []} value = index + 1 var e := t.entries[index] var children: Array = [] for p in e.pipes: var save := e.save_for(p) for k in e.count_for(p): var child := roll(board, p, rng, depth + 1, decks) if child.is_empty(): break if save != "": child["save_as"] = save children.append(child) var node := {"table": t, "index": index, "value": value, "entry": e, "children": children} if reshuffled: node["reshuffled"] = true return node ## Index of a weighted random entry, or -1 if every weight is 0. static func _pick_weighted(t: RollTable, rng: RandomNumberGenerator) -> int: var total := t.total_weight() if total <= 0: return -1 var pick := rng.randi_range(0, total - 1) for i in t.entries.size(): pick -= maxi(0, t.entries[i].weight) if pick < 0: return i return -1 ## Ids of tables that can never finish rolling: every result they can give ## leads, through pipes, back into such a table. A table that loops on only ## some of its entries is fine. static func endless_cycles(board: TowerBoard) -> Array[String]: var done := {} var changed := true while changed: changed = false for t in board.tables: if done.has(t.id): continue if _can_finish(t, board, done): done[t.id] = true changed = true var out: Array[String] = [] for t in board.tables: if not done.has(t.id): out.append(t.id) return out static func _can_finish(t: RollTable, board: TowerBoard, done: Dictionary) -> bool: if t.entries.is_empty(): return true for e in t.entries: if t.is_ranged(): if not e.has_range or t.chance(e) <= 0.0: continue elif e.weight <= 0: continue var ok := true for p in e.pipes: if board.get_table(p) != null and not done.has(p): ok = false break if ok: return true # No entry can come up at all: the roll just stops. for e in t.entries: if (t.is_ranged() and e.has_range and t.chance(e) > 0.0) or (not t.is_ranged() and e.weight > 0): return false return true ## One line of the result tray for a single node. static func step_of(n: Dictionary) -> Dictionary: var t: RollTable = n["table"] var text: String = n["stop"] if n.has("stop") else (n["entry"] as TableEntry).text return { "table": t.name, "num": t.fmt(n["value"]) if n.has("value") else "", "die": t.die_size(), "dice": t.dice_label(), "text": text, } ## The whole chain in the order it was rolled. static func flatten(n: Dictionary, out: Array = []) -> Array: if n.is_empty(): return out out.append(step_of(n)) for c in n["children"]: flatten(c, out) return out static func reaches(n: Dictionary, table_id: String) -> bool: if n.is_empty(): return false if (n["table"] as RollTable).id == table_id: return true for c in n["children"]: if reaches(c, table_id): return true return false ## Marks every entry and pipe along the chain as lit. static func collect_lit(n: Dictionary, entries: Dictionary, pipes: Dictionary) -> void: if n.is_empty() or n.has("stop"): return var t: RollTable = n["table"] var e: TableEntry = n["entry"] entries[entry_key(t.id, e.id)] = true for c in n["children"]: pipes[pipe_key(t.id, e.id, (c["table"] as RollTable).id)] = true collect_lit(c, entries, pipes) static func entry_key(table_id: String, entry_id: String) -> String: return table_id + ":" + entry_id static func pipe_key(table_id: String, entry_id: String, to_id: String) -> String: return "%s:%s>%s" % [table_id, entry_id, to_id]