extends SceneTree # Headless check for Maze mode: # godot --headless --audio-driver Dummy --script res://tests/maze_check.gd # 1. Builds many mazes (every size and complexity, several seeds) and runs the # soft-lock check on each. # 2. Negative tests: the validator must reject # - a one-way trap (a region whose only way in is a one-way tube), # - Start inside a blow zone, # - a hand-made blow-zone trap and a suction trap. In both, Start and End are # joined by air, so a check that only looks at which areas join (the old # chamber model) would pass them. # 3. Junctions: one hand-made map with a cross tile, as manual tubes (you steer # at the junction: End is reachable, must pass) and as auto tubes (they go # straight across: End is out of reach, must fail). const SEEDS_PER_SETTING = 4 const TILE_SET = preload("res://Assets/level_tiles.tres") # Hand-made maps: "#" rock, "." air, "S" Start, "E" End, letters are tube tiles # (see TUBE_KEY): [openings, kind, entry side bit]. # "|", "-" and "+" (cross) take the kind the map is checked with. const TUBE_KEY := { "|": [5, &"", 0], "-": [10, &"", 0], "+": [15, &"", 0], "v": [5, MazeGraph.ONE_WAY, 0], "^": [5, MazeGraph.ONE_WAY, 1], } # A down blower over the crossing (3, 2): walking from S to the left blows the # slime down the shaft, which it can't climb back up past the blow zone. const WIND_TRAP: Array[String] = [ "#######", "###v###", "#E...S#", "###.###", "###.###", "###.###", "#######", ] # Walking from S to E crosses the tile over a one-way entrance (4, 2): it # sucks the slime into a pocket whose only way out leads back to the start # side. End can't be reached at all. # S's only tube crosses another at (3, 3). Steering (manual) turns up to E; # an auto tube goes straight on to the dead end P (and back). const JUNCTION: Array[String] = [ "#######", "###E###", "###|###", "#S-+-.#", "###|###", "###.###", "#######", ] const SUCTION_TRAP: Array[String] = [ "#########", "#########", "#S.....E#", "###|^####", "###|v####", "##.....##", "##.....##", "#########", ] func _initialize() -> void: var layer := TileMapLayer.new() layer.tile_set = TILE_SET root.add_child(layer) var mazeCount := 0 var firstTryCount := 0 var validCount := 0 var startTime := Time.get_ticks_msec() for complexity in range(MazeSettings.MIN_COMPLEXITY, MazeSettings.MAX_COMPLEXITY + 1): var levelStart := Time.get_ticks_msec() for width in range(MazeSettings.MIN_WIDTH, MazeSettings.MAX_WIDTH + 1): for height in range(MazeSettings.MIN_HEIGHT, MazeSettings.MAX_HEIGHT + 1): for seedIndex in SEEDS_PER_SETTING: var firstSeed := 1000 * seedIndex + 37 * complexity + 7 * width + height var builder := MazeBuilder.new(layer, Vector2i(width, height), complexity, firstSeed) mazeCount += 1 if builder.isValid: validCount += 1 if builder.attempts == 1: firstTryCount += 1 for problem in builder.problems: print(" c%d %dx%d: %s" % [complexity, width, height, problem]) print(" complexity %d: %d ms" % [complexity, Time.get_ticks_msec() - levelStart]) print("Mazes: %d, valid: %d, valid on the first try: %d (%d ms)" % [mazeCount, validCount, firstTryCount, Time.get_ticks_msec() - startTime]) var results := { "one-way trap": _check_one_way_trap(layer), "Start in a blow zone": _check_start_in_blow_zone(layer), "blow-zone trap (hand-made)": not _check_map(layer, WIND_TRAP, MazeGraph.AUTO), "suction trap (hand-made)": not _check_map(layer, SUCTION_TRAP, MazeGraph.AUTO), "auto crossing going straight (hand-made)": not _check_map(layer, JUNCTION, MazeGraph.AUTO), } var junctionPasses := _check_map(layer, JUNCTION, MazeGraph.MANUAL) print("Validator passes a manual junction you steer through: %s" % ("yes" if junctionPasses else "NO")) var allCaught := true for test: String in results: print("Validator catches %s: %s" % [test, "yes" if results[test] else "NO"]) allCaught = allCaught and results[test] quit(0 if validCount == mazeCount and allCaught and junctionPasses else 1) # In a good maze, take a one-way tube's exit region X (not End's) and block # every other mouth into X with rock. The tube is then X's only way in, and it # has no way out. Tries mazes until Start can still reach End, so it's a real # soft-lock and not just an unsolvable maze. func _check_one_way_trap(layer: TileMapLayer) -> bool: var tiles := MazeTiles.new(layer.tile_set) for trapSeed in range(4242, 4302): var builder := MazeBuilder.new(layer, Vector2i(2, 2), 3, trapSeed) if not builder.isValid: continue var carver := builder.carver for exit in OneWayTube.find_exits(layer): var region := _region_with(carver, exit.cell + exit.outward) if region == -1 or region == _region_with(carver, carver.endCell): continue for cell in layer.get_used_cells(): for outward in SlimeMoves.SIDES: var isMouth := TubeSteer.get_openings(layer, cell) & TubeSteer.side_bit(outward) != 0 if isMouth and cell != exit.cell and carver.regionAir[region].has(cell + outward): tiles.set_tile(layer, cell, 0, MazeTiles.ROCK) var validator := MazeValidator.new(layer, carver.bounds, carver.startCell, carver.endCell) if validator.problem.begins_with("Soft-lock"): print(" one-way trap (seed %d): %s" % [trapSeed, validator.problem]) return true if validator.isValid: return false # Missed a real trap. break # The map is changed now: try the next maze. return false # A good maze, checked again as if Start stood in a one-way exit's blow zone. func _check_start_in_blow_zone(layer: TileMapLayer) -> bool: for blowSeed in range(4242, 4302): var builder := MazeBuilder.new(layer, Vector2i(2, 2), 4, blowSeed) var exits := OneWayTube.find_exits(layer) if not builder.isValid or exits.is_empty(): continue var zone := OneWayTube.blow_zone(layer, exits[0].cell, exits[0].outward) var validator := MazeValidator.new(layer, builder.carver.bounds, zone[0], builder.carver.endCell) print(" Start in a blow zone (seed %d): %s" % [blowSeed, validator.problem]) return not validator.isValid return false # Builds a hand-made map with `kind` for the generic tube symbols, and returns # whether the validator passes it. func _check_map(layer: TileMapLayer, map: Array[String], kind: StringName) -> bool: layer.clear() var tiles := MazeTiles.new(layer.tile_set) var start := Vector2i.ZERO var end := Vector2i.ZERO for y in map.size(): for x in map[y].length(): var symbol := map[y][x] var cell := Vector2i(x, y) if symbol == "#": tiles.set_tile(layer, cell, 0, MazeTiles.ROCK) elif TUBE_KEY.has(symbol): var symbolKind: StringName = TUBE_KEY[symbol][1] if TUBE_KEY[symbol][1] != &"" else kind tiles.set_tile(layer, cell, TUBE_KEY[symbol][0], symbolKind, TUBE_KEY[symbol][2]) elif symbol == "S": start = cell elif symbol == "E": end = cell var bounds := Rect2i(0, 0, map[0].length(), map.size()) var validator := MazeValidator.new(layer, bounds, start, end) print(" hand-made map (%s): %s" % [kind, validator.problem if not validator.isValid else "valid"]) return validator.isValid func _region_with(carver: MazeCarver, cell: Vector2i) -> int: for region in carver.regionAir.size(): if carver.regionAir[region].has(cell): return region return -1