Created
August 18, 2026 23:56
-
-
Save SomeGuyWhoLovesCoding/d6ab8decdd3a4398d2440e69e00933d1 to your computer and use it in GitHub Desktop.
Semmi's maze generation made with Google AI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import haxe.ds.Vector; | |
| class Test { | |
| static function main() { | |
| var maze = new KruskalCellMergingMazeTest(50, 25); // how CRAZY \o/ | |
| maze.generate(80); | |
| maze.printGrid(); | |
| } | |
| } | |
| enum TileType { | |
| Wall; | |
| Floor; | |
| } | |
| class KruskalCellMergingMazeTest { | |
| private var width:Int; | |
| private var height:Int; | |
| private var grid:Vector<Vector<TileType>>; | |
| private var parent:Vector<Int>; | |
| public function new(width:Int, height:Int) { | |
| // Bitwise check for odd/even is faster than modulo | |
| this.width = (width & 1) == 0 ? width + 1 : width; | |
| this.height = (height & 1) == 0 ? height + 1 : height; | |
| initGrid(); | |
| } | |
| private function initGrid() { | |
| grid = new Vector(this.width); | |
| for (x in 0...this.width) { | |
| var col = new Vector<TileType>(this.height); | |
| for (y in 0...this.height) { | |
| col[y] = TileType.Wall; | |
| } | |
| grid[x] = col; | |
| } | |
| parent = new Vector(this.width * this.height); | |
| for (i in 0...parent.length) { | |
| parent[i] = i; | |
| } | |
| } | |
| private function find(i:Int):Int { | |
| // Recursive path compression is highly efficient and JIT-friendly | |
| if (parent[i] == i) return i; | |
| return parent[i] = find(parent[i]); | |
| } | |
| private inline function union(i:Int, j:Int):Bool { | |
| var rootI = find(i); | |
| var rootJ = find(j); | |
| if (rootI != rootJ) { | |
| parent[rootI] = rootJ; | |
| return true; | |
| } | |
| return false; | |
| } | |
| public function generate(roomCount:Int):Vector<Vector<TileType>> { | |
| var numCellsX = (this.width - 1) >> 1; | |
| var numCellsY = (this.height - 1) >> 1; | |
| // 1. Alle Standard-Basis-Zellen aktivieren | |
| var x = 1; | |
| while (x < this.width - 1) { | |
| var gridX = grid[x]; // Cache vector reference | |
| var y = 1; | |
| while (y < this.height - 1) { | |
| gridX[y] = TileType.Floor; | |
| y += 2; | |
| } | |
| x += 2; | |
| } | |
| // 2. Cell-Merging: Zufällige größere Räume/Gänge vorab verschmelzen | |
| for (_ in 0...roomCount) { | |
| // Directly calculate random odd coordinates without array lookup | |
| var cx = Std.int(Math.random() * numCellsX); | |
| var cy = Std.int(Math.random() * numCellsY); | |
| var startX = (cx << 1) + 1; | |
| var startY = (cy << 1) + 1; | |
| // Bitwise equivalent of (Math.floor(Math.random() * 2) + 1) * 2 | |
| var roomW = (Std.int(Math.random() * 2) + 1) << 1; | |
| var roomH = (Std.int(Math.random() * 2) + 1) << 1; | |
| if (startX + roomW < this.width - 1 && startY + roomH < this.height - 1) { | |
| var baseId = startX * this.height + startY; | |
| var endX = startX + roomW; | |
| var endY = startY + roomH; | |
| // Alle Wände und Böden innerhalb dieses Bereichs einreißen | |
| for (tx in startX...endX + 1) { | |
| var gridTx = grid[tx]; | |
| for (ty in startY...endY + 1) { | |
| gridTx[ty] = TileType.Floor; | |
| } | |
| } | |
| // Logische Zellen mit der Basis-Zelle verschmelzen (nur ungerade Koordinaten) | |
| for (tx in startX...endX + 1) { | |
| if ((tx & 1) == 1) { | |
| var txBase = tx * this.height; | |
| for (ty in startY...endY + 1) { | |
| if ((ty & 1) == 1) { | |
| union(baseId, txBase + ty); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // 3. Alle potenziellen Trennwände zwischen logischen Zellen sammeln | |
| // OPTIMIZATION: Pack coordinates into a single 32-bit Int (8 bits per coordinate) | |
| var edges = new Array<Int>(); | |
| var ex = 1; | |
| while (ex < this.width - 1) { | |
| var ey = 1; | |
| while (ey < this.height - 1) { | |
| if (ex + 2 < this.width - 1) { | |
| edges.push(ex | (ey << 8) | ((ex + 2) << 16) | (ey << 24)); | |
| } | |
| if (ey + 2 < this.height - 1) { | |
| edges.push(ex | (ey << 8) | (ex << 16) | ((ey + 2) << 24)); | |
| } | |
| ey += 2; | |
| } | |
| ex += 2; | |
| } | |
| // Wände zufällig mischen: Fisher-Yates Shuffle (O(N) instead of O(N log N) sort) | |
| var i = edges.length; | |
| while (i > 1) { | |
| var j = Std.int(Math.random() * i); | |
| i--; | |
| var temp = edges[i]; | |
| edges[i] = edges[j]; | |
| edges[j] = temp; | |
| } | |
| // 4. Kruskal-Logik: Wände entfernen, wenn die Zellen nicht im selben Set sind | |
| for (edge in edges) { | |
| var x1 = edge & 0xFF; | |
| var y1 = (edge >> 8) & 0xFF; | |
| var x2 = (edge >> 16) & 0xFF; | |
| var y2 = (edge >> 24) & 0xFF; // & 0xFF ensures safety even if sign-extended | |
| var id1 = x1 * this.height + y1; | |
| var id2 = x2 * this.height + y2; | |
| var root1 = find(id1); | |
| var root2 = find(id2); | |
| if (root1 != root2) { | |
| parent[root1] = root2; | |
| var wallX = (x1 + x2) >> 1; | |
| var wallY = (y1 + y2) >> 1; | |
| grid[wallX][wallY] = TileType.Floor; | |
| } | |
| } | |
| return grid; | |
| } | |
| // Konsolen-Vorschau zum Testen | |
| public function printGrid() { | |
| var rows = new Array<StringBuf>(); | |
| for (y in 0...this.height) { | |
| rows.push(new StringBuf()); | |
| } | |
| var wallChar = "⛆"; | |
| var floorChar = "⛚"; | |
| // CACHE-FRIENDLY: Iterate 'x' on the outside to match Vector<Vector> memory layout | |
| for (x in 0...this.width) { | |
| var gridX = this.grid[x]; | |
| for (y in 0...this.height) { | |
| rows[y].add(gridX[y] == TileType.Wall ? wallChar : floorChar); | |
| } | |
| } | |
| for (y in 0...this.height) { | |
| trace(rows[y].toString()); | |
| } | |
| } | |
| } | |
| // Semmi asked me to make a diff so here's this gist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment