public void GenerateMaze(int sizeX, int sizeY, int sizeZ, int seed, int smoothness) { iSmooth = smoothness; MSIZEX = sizeX; MSIZEY = sizeY; MSIZEZ = sizeZ; maze_base = new int[MSIZEX * MSIZEZ]; maze_data = new Byte[MSIZEX, MSIZEZ]; s_stack = new Stack(); rand = new Random(seed); MazeInit(rand); cMazeState state = new cMazeState(rand.Next() % MSIZEX, rand.Next() % MSIZEZ, 0); analyze_cell(state, rand); }
void analyze_cell(cMazeState s, Random r) { bool bEnd = false, found; int indexSrc, indexDest, tDir = 0, prevDir = 0; while (true) { if (s.dir == 15) { while (s.dir == 15) { s = (cMazeState)s_stack.pop(); if (s == null) { bEnd = true; break; } } if (bEnd == true) { break; } } else { do { prevDir = tDir; tDir = (int)System.Math.Pow(2, r.Next() % 4); if ((r.Next() % 32) < iSmooth) { if ((s.dir & prevDir) == 0) { tDir = prevDir; } } if ((s.dir & tDir) != 0) { found = true; } else { found = false; } } while (found == true && s.dir != 15); s.dir |= tDir; indexSrc = cell_index(s.x, s.y); // direction W if (tDir == 1 && s.x > 0) { indexDest = cell_index(s.x - 1, s.y); if (base_cell(indexSrc) != base_cell(indexDest)) { merge(indexSrc, indexDest); maze_data[s.x, s.y] |= (byte)Direction.W; s_stack.push(new cMazeState(s)); s.x -= 1; s.dir = 0; } } // direction E if (tDir == 2 && s.x < MSIZEX - 1) { indexDest = cell_index(s.x + 1, s.y); if (base_cell(indexSrc) != base_cell(indexDest)) { merge(indexSrc, indexDest); maze_data[s.x + 1, s.y] |= (byte)Direction.W; s_stack.push(new cMazeState(s)); s.x += 1; s.dir = 0; } } // direction N if (tDir == 4 && s.y > 0) { indexDest = cell_index(s.x, s.y - 1); if (base_cell(indexSrc) != base_cell(indexDest)) { merge(indexSrc, indexDest); maze_data[s.x, s.y] |= (byte)Direction.N; s_stack.push(new cMazeState(s)); s.y -= 1; s.dir = 0; } } // direction S if (tDir == 8 && s.y < MSIZEZ - 1) { indexDest = cell_index(s.x, s.y + 1); if (base_cell(indexSrc) != base_cell(indexDest)) { merge(indexSrc, indexDest); maze_data[s.x, s.y + 1] |= (byte)Direction.N; s_stack.push(new cMazeState(s)); s.y += 1; s.dir = 0; } } } // else } // while } // function
public cMazeState(cMazeState s) { x = s.x; y = s.y; dir = s.dir; }