class_name MazeGraph extends RefCounted # The maze as a graph, before any tiles: a grid of regions joined by tubes. # Each region is a labyrinth of corridors (MazeCarver); the only ways between # regions are tubes. # # 1. A random spanning tree over neighbouring regions (a recursive # backtracker, so it has long corridors and dead ends). Its tubes are two-way. # 2. Start is a random region, End the one furthest from it along the tree. # 3. With the tree rooted at End, some tubes on the Start-End route become # one-way towards End. # 4. Extra tubes join nearby regions: two-way loops, and one-way tubes in any # direction. # # Why this can't soft-lock: every tree tube can be used towards End (two-way, # or one-way towards End), so from every region the tree leads to End. Extra # tubes only add ways, so they can't take that away. And every region is # reachable from Start: the route's one-way tubes point away from Start, and # everything off the route hangs off it by two-way tubes. # (Inside a region every corridor can be crawled both ways; MazeCarver puts # blow zones only where they don't cut a region, so this still holds.) # With a single region, Start and End are both in it. # MazeValidator checks the finished tiles anyway. const AUTO = &"auto" const MANUAL = &"manual" const GOO = &"goo" const ONE_WAY = &"one_way" # Goes from .from to .to only. const EXTRA_TUBE_REACH = 2 # Extra tubes join regions up to this many grid steps apart (diagonals count as one). const EXTRA_TUBE_TRIES = 40 # Per extra tube, tries to find a pair of regions not joined yet. const GRID_STEPS: Array[Vector2i] = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT] var gridSize := Vector2i.ZERO # Regions across and down. # Per tube: {from: region, to: region, kind: AUTO/MANUAL/GOO/ONE_WAY, # isRequired: true for the tree (the maze breaks without it)}. var tubes: Array[Dictionary] = [] var start := 0 var end := 0 var _random: RandomNumberGenerator var _level: Dictionary var _treeNeighbors: Array[Array] = [] # Per region: regions joined to it in the tree. # screens: the maze's size in screens. level: see MazeSettings.LEVELS. func _init(screens: Vector2i, level: Dictionary, random: RandomNumberGenerator) -> void: _random = random _level = level gridSize = screens * Vector2i(level.regionsPerScreen) _treeNeighbors.resize(region_count()) for region in region_count(): _treeNeighbors[region] = [] _build_tree() _pick_start_and_end() _make_route_one_way() _add_extra_tubes(roundi(level.extraTubesPerScreen * screens.x * screens.y)) func region_count() -> int: return gridSize.x * gridSize.y func grid_cell(region: int) -> Vector2i: return Vector2i(region % gridSize.x, region / gridSize.x) func region_at(cell: Vector2i) -> int: return cell.y * gridSize.x + cell.x # ---- Building ---- func _build_tree() -> void: var visited := {} var stack: Array[int] = [_random.randi_range(0, region_count() - 1)] visited[stack[0]] = true while not stack.is_empty(): var region: int = stack.back() var unvisited: Array[int] = [] for neighbor in _grid_neighbors(region): if not visited.has(neighbor): unvisited.append(neighbor) if unvisited.is_empty(): stack.pop_back() continue var next: int = unvisited[_random.randi_range(0, unvisited.size() - 1)] visited[next] = true _treeNeighbors[region].append(next) _treeNeighbors[next].append(region) tubes.append({from = region, to = next, kind = _two_way_kind(), isRequired = true}) stack.append(next) func _grid_neighbors(region: int) -> Array[int]: var neighbors: Array[int] = [] var cell := grid_cell(region) for step in GRID_STEPS: var next := cell + step if next.x >= 0 and next.y >= 0 and next.x < gridSize.x and next.y < gridSize.y: neighbors.append(region_at(next)) return neighbors func _two_way_kind() -> StringName: return pick_two_way_kind(_random, _level.steeredShare) # Auto, or (with chance `steeredShare`) manual or goo. MazeCarver uses this too. static func pick_two_way_kind(random: RandomNumberGenerator, steeredShare: float) -> StringName: if random.randf() >= steeredShare: return AUTO return MANUAL if random.randf() < 0.5 else GOO func _pick_start_and_end() -> void: start = _random.randi_range(0, region_count() - 1) var distances := _tree_distances(start) end = start for region in region_count(): if distances[region] > distances[end]: end = region func _tree_distances(from: int) -> PackedInt32Array: var distances := PackedInt32Array() distances.resize(region_count()) distances.fill(-1) distances[from] = 0 var queue: Array[int] = [from] while not queue.is_empty(): var region: int = queue.pop_front() for next: int in _treeNeighbors[region]: if distances[next] == -1: distances[next] = distances[region] + 1 queue.append(next) return distances # Turns some tree tubes on the Start-End route one-way, pointing towards End. func _make_route_one_way() -> void: var route := _route() var routeTubes: Array[int] = [] for i in range(route.size() - 1): routeTubes.append(_tube_between(route[i], route[i + 1])) _shuffle(routeTubes) var oneWayCount := mini(_level.oneWayOnRoute, routeTubes.size()) for i in oneWayCount: var tube := tubes[routeTubes[i]] # Point it from the region nearer Start to the one nearer End. if route.find(tube.from) > route.find(tube.to): var swap: int = tube.from tube.from = tube.to tube.to = swap tube.kind = ONE_WAY # The regions from Start to End along the tree. func _route() -> Array[int]: var distances := _tree_distances(end) var route: Array[int] = [start] while route.back() != end: for next: int in _treeNeighbors[route.back()]: if distances[next] == distances[route.back()] - 1: route.append(next) break return route func _tube_between(regionA: int, regionB: int) -> int: for i in tubes.size(): var tube := tubes[i] if (tube.from == regionA and tube.to == regionB) or (tube.from == regionB and tube.to == regionA): return i return -1 func _add_extra_tubes(count: int) -> void: for i in count: for attempt in EXTRA_TUBE_TRIES: var from := _random.randi_range(0, region_count() - 1) var to := _random_nearby(from) if to == -1 or _tube_between(from, to) != -1: continue var kind := ONE_WAY if _random.randf() < 0.5 else _two_way_kind() tubes.append({from = from, to = to, kind = kind, isRequired = false}) break # A random other region within EXTRA_TUBE_REACH grid steps, or -1. func _random_nearby(region: int) -> int: var cell := grid_cell(region) var offset := Vector2i(_random.randi_range(-EXTRA_TUBE_REACH, EXTRA_TUBE_REACH), _random.randi_range(-EXTRA_TUBE_REACH, EXTRA_TUBE_REACH)) var other := cell + offset if offset == Vector2i.ZERO or other.x < 0 or other.y < 0 or other.x >= gridSize.x or other.y >= gridSize.y: return -1 return region_at(other) func _shuffle(items: Array[int]) -> void: for i in range(items.size() - 1, 0, -1): var j := _random.randi_range(0, i) var swap := items[i] items[i] = items[j] items[j] = swap