Advertisement
eea

aimapgen

eea
Apr 30th, 2023 (edited)
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. -- Constants
  2. local MAZE_SIZE = 100 -- Size of the maze (10x10)
  3. local WALL_HEIGHT = 10
  4. local TILE_SIZE = 4
  5. -- Create a function to generate the maze
  6. local function generateMaze()
  7.     -- Create a maze grid
  8.     local maze = {}
  9.     for i = 1, MAZE_SIZE do
  10.         maze[i] = {}
  11.         for j = 1, MAZE_SIZE do
  12.             maze[i][j] = true -- true represents a wall
  13.         end
  14.     end
  15.  
  16.     -- Create a helper function for recursive backtracking
  17.     local function recursiveBacktracking(row, col)
  18.         -- Set the current cell as visited (false represents an open path)
  19.         maze[row][col] = false
  20.  
  21.         -- Create a random list of directions
  22.         local directions = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}}
  23.         for i = 1, 4 do
  24.             local randomIndex = math.random(i, 4)
  25.             directions[i], directions[randomIndex] = directions[randomIndex], directions[i]
  26.         end
  27.  
  28.         -- Loop through the random directions
  29.         for i = 1, 4 do
  30.             local newRow = row + directions[i][1]
  31.             local newCol = col + directions[i][2]
  32.  
  33.             -- Check if the new cell is within the maze boundaries
  34.             if newRow >= 1 and newRow < MAZE_SIZE and newCol >= 1 and newCol < MAZE_SIZE then
  35.                 -- Check if the new cell is a wall
  36.                 if maze[newRow][newCol] then
  37.                     -- Create a path between the current cell and the new cell
  38.                     local pathRow = row + (directions[i][1] / 2)
  39.                     local pathCol = col + (directions[i][2] / 2)
  40.                     maze[pathRow][pathCol] = false
  41.  
  42.                     -- Recursively call the function on the new cell
  43.                     recursiveBacktracking(newRow, newCol)
  44.                 end
  45.             end
  46.         end
  47.     end
  48.  
  49.     -- Start the recursive backtracking algorithm at a random cell
  50.     local startRow = math.random(1, MAZE_SIZE)
  51.     local startCol = math.random(1, MAZE_SIZE)
  52.     recursiveBacktracking(startRow, startCol)
  53.  
  54.     -- Return the generated maze
  55.     return maze
  56. end
  57.  
  58. -- Function to build the maze in Roblox (same as before)
  59. local function buildMaze()
  60.     -- Generate the maze
  61.     local maze = generateMaze()
  62.  
  63.     -- Create a base part to represent a wall
  64.     local wallPart = Instance.new("Part")
  65.     wallPart.Size = Vector3.new(TILE_SIZE, WALL_HEIGHT, TILE_SIZE)
  66.     wallPart.Anchored = true
  67.     wallPart.BrickColor = BrickColor.new("Medium stone grey")
  68.     wallPart.Material = Enum.Material.Concrete
  69.  
  70.     -- Loop through the maze and create walls where needed
  71.     for i = 1, MAZE_SIZE do
  72.         for j = 1, MAZE_SIZE do
  73.             if maze[i][j] then
  74.                 local wall = wallPart:Clone()
  75.                 wall.Position = Vector3.new((i - 1) * TILE_SIZE, WALL_HEIGHT / 2, (j - 1) * TILE_SIZE)
  76.                 wall.Parent = script
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. -- Call the function to build the maze
  83. buildMaze()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement