class_name FreePlay extends RefCounted ## Deterministic free-play variants of the verified levels.json boards. ## ## Only structure-preserving maps are used: ## * a dihedral symmetry of the square (rotation / mirror) applied to every ## cell, to `solution` (row -> column) and to the `clues` coordinates; ## * a one-to-one relabeling of region ids applied to every cell of `regions`. ## ## Both maps are automorphisms of the puzzle. They keep one rat per row, column ## and region, the "no two rats touching (even diagonally)" rule, and the exact ## number of solutions: a D4 symmetry is a bijection of cells that preserves ## adjacency and region membership, and a region relabeling preserves the ## equality relation between cells. Consequently every solution of the source ## (including any alternate one) maps to exactly one solution of the variant ## and back, so the verified uniqueness is preserved rather than re-guessed. ## The source solution is transported through the same map; no new solution is ## invented. ## ## Board.gd derives each region's guest portrait from the region id and the ## level id, so relabeling region ids relabels the associated guest ids too; ## the level dictionaries carry no separate guest-id array to transform. ## ## `id` is deliberately kept from the source so that applying the inverse map ## recovers the source entry byte-for-byte at the value level. const LEVELS_PATH := "res://data/levels.json" const N := 8 const SYMMETRY_COUNT := 8 ## Loads and parses the verified level pack. Returns only dictionary entries. static func all_source_levels() -> Array: if not FileAccess.file_exists(LEVELS_PATH): push_warning("FreePlay: no level file at %s" % LEVELS_PATH) return [] var file := FileAccess.open(LEVELS_PATH, FileAccess.READ) if file == null: push_warning("FreePlay: could not open %s" % LEVELS_PATH) return [] var parsed = JSON.parse_string(file.get_as_text()) file.close() if typeof(parsed) != TYPE_ARRAY: push_warning("FreePlay: %s is not a JSON array" % LEVELS_PATH) return [] var levels: Array = [] for entry in parsed: if typeof(entry) == TYPE_DICTIONARY: levels.append(entry) return levels ## Deterministic parameters shared by variant() and the QA inverse check. ## source_index picks a verified level; symmetry is a D4 index; region_map is a ## permutation of 0..N-1 applied to region ids, with its inverse provided too. static func variant_params(seed: int, index: int) -> Dictionary: var levels := all_source_levels() var count := levels.size() if count == 0: return {} var source_index := _mod(seed + index, count) # An explicit seed keeps results identical across runs and platforms. var rng := RandomNumberGenerator.new() rng.seed = seed ^ (index * 2654435761) var symmetry := rng.randi_range(0, SYMMETRY_COUNT - 1) var region_map: Array = [] for i in N: region_map.append(i) for i in range(N - 1, 0, -1): var j := rng.randi_range(0, i) var tmp = region_map[i] region_map[i] = region_map[j] region_map[j] = tmp var inverse_map: Array = [] inverse_map.resize(N) for i in N: inverse_map[int(region_map[i])] = i return { "source_index": source_index, "symmetry": symmetry, "region_map": region_map, "inverse_map": inverse_map, } ## Maps one cell coordinate through D4 symmetry `symmetry` on an n x n board: ## 0 identity; 1 rot90 cw; 2 rot180; 3 rot270 cw; ## 4 mirror left/right; 5 mirror top/bottom; ## 6 main-diagonal flip; 7 anti-diagonal flip. static func transform_cell(r: int, c: int, symmetry: int, n: int = N) -> Vector2i: var last := n - 1 match symmetry: 0: return Vector2i(r, c) 1: return Vector2i(c, last - r) 2: return Vector2i(last - r, last - c) 3: return Vector2i(last - c, r) 4: return Vector2i(r, last - c) 5: return Vector2i(last - r, c) 6: return Vector2i(c, r) 7: return Vector2i(last - c, last - r) return Vector2i(r, c) ## The inverse of each D4 element. Four of them are self-inverse. static func inverse_symmetry(symmetry: int) -> int: match symmetry: 1: return 3 3: return 1 _: return symmetry ## Returns a variant level in exactly the same shape as a levels.json entry: ## { clues, id, n, regions, solution }, with the same float value types. static func variant(seed: int, index: int) -> Dictionary: var params := variant_params(seed, index) if params.is_empty(): return {} var levels := all_source_levels() var source: Dictionary = levels[int(params["source_index"])] var n := int(source.get("n", N)) var symmetry := int(params["symmetry"]) var region_map: Array = params["region_map"] var src_regions: Array = source.get("regions", []) var src_solution: Array = source.get("solution", []) var src_clues: Array = source.get("clues", []) var regions: Array = [] for r in n: var row: Array = [] for c in n: row.append(0.0) regions.append(row) # Move every cell (and its relabeled region id) to its mirrored position. for r in n: for c in n: var dest := transform_cell(r, c, symmetry, n) var old_id := int(src_regions[r][c]) var new_id: int = old_id if old_id >= 0 and old_id < region_map.size(): new_id = int(region_map[old_id]) regions[dest.x][dest.y] = float(new_id) # solution[row] is the rat's column; a symmetry permutes rows, so write # each transported rat into its new row. var solution: Array = [] solution.resize(n) for r in n: var c := int(src_solution[r]) var dest := transform_cell(r, c, symmetry, n) solution[dest.x] = float(dest.y) # Clues are [row, column] coordinates of fixed rats. var clues: Array = [] for clue in src_clues: var r := int(clue[0]) var c := int(clue[1]) var dest := transform_cell(r, c, symmetry, n) clues.append([float(dest.x), float(dest.y)]) return { "clues": clues, "id": str(source.get("id", "")), "n": float(n), "regions": regions, "solution": solution, } static func _mod(value: int, modulus: int) -> int: var result := value % modulus if result < 0: result += modulus return result