Advertisement
Cat_in_the_hat

Maze genrater

Feb 19th, 2025
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. local radius = math.floor(10)
  2.  
  3. local originPosition = Vector3.new(342, 69, 342)
  4. local partSize = 3.1
  5. local maze = {}
  6. local directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }
  7.  
  8. local function shuffle(tbl)
  9.     for i = #tbl, 2, -1 do
  10.         local j = math.random(i)
  11.         tbl[i], tbl[j] = tbl[j], tbl[i]
  12.     end
  13. end
  14.  
  15. local function generateMaze(x, z)
  16.     maze[x][z] = false
  17.     shuffle(directions)
  18.    
  19.     for _, dir in ipairs(directions) do
  20.         local nx, nz = x + dir[1] * 2, z + dir[2] * 2
  21.         if maze[nx] and maze[nx][nz] then
  22.             maze[x + dir[1]][z + dir[2]] = false
  23.             generateMaze(nx, nz)
  24.         end
  25.     end
  26. end
  27.  
  28. local function isValid(x, z)
  29.     return maze[x] and maze[x][z] == false
  30. end
  31.  
  32. local function findPath(startX, startZ, endX, endZ)
  33.     local queue = {{startX, startZ}}
  34.     local visited = {}
  35.     visited[startX .. "," .. startZ] = true
  36.     local path = {}
  37.  
  38.     while #queue > 0 do
  39.         local node = table.remove(queue, 1)
  40.         local x, z = node[1], node[2]
  41.  
  42.         if x == endX and z == endZ then  
  43.             return true  
  44.         end  
  45.        
  46.         for _, dir in ipairs(directions) do  
  47.             local nx, nz = x + dir[1], z + dir[2]  
  48.             if isValid(nx, nz) and not visited[nx .. "," .. nz] then  
  49.                 visited[nx .. "," .. nz] = true  
  50.                 table.insert(queue, {nx, nz})  
  51.                 table.insert(path, {nx, nz})  
  52.             end  
  53.         end
  54.     end
  55.    
  56.     return false
  57. end
  58.  
  59. local function generateValidMaze()
  60.     repeat
  61.         for x = -radius - 1, radius + 1 do
  62.             maze[x] = {}
  63.             for z = -radius - 1, radius + 1 do
  64.                 maze[x][z] = true
  65.             end
  66.         end
  67.  
  68.         generateMaze(0, 0)
  69.  
  70.         local exitX, exitZ  
  71.         repeat  
  72.             local exitSide = math.random(4)  
  73.             if exitSide == 1 then exitX, exitZ = -radius-1, math.random(-radius, radius) end  
  74.             if exitSide == 2 then exitX, exitZ = radius+1, math.random(-radius, radius) end  
  75.             if exitSide == 3 then exitX, exitZ = math.random(-radius, radius), -radius-1 end  
  76.             if exitSide == 4 then exitX, exitZ = math.random(-radius, radius), radius+1 end  
  77.         until maze[exitX] and maze[exitX][exitZ]  
  78.        
  79.         maze[exitX][exitZ] = false
  80.     until findPath(0, 0, exitX, exitZ)
  81.  
  82.     return exitX, exitZ
  83. end
  84.  
  85. local exitX, exitZ = generateValidMaze()
  86.  
  87. for x = -radius - 1, radius + 1 do
  88.     for z = -radius - 1, radius + 1 do
  89.         if maze[x][z] then
  90.             for layer = 0, 4 do
  91.                 local partPosition = originPosition + Vector3.new(x * partSize, 0, z * partSize) + Vector3.new(0, layer * partSize, 0)
  92.                 local newPart = PartService.createPart(ItemType.COBBLESTONE, partPosition)
  93.                 newPart:setSize(Vector3.new(partSize, partSize, partSize))
  94.                 newPart:setCollidable(true)
  95.                 newPart:setAnchored(true)
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. for x = -radius - 1, radius + 1 do
  102.     for z = -radius - 1, radius + 1 do
  103.         if not maze[x][z] then
  104.             local pathPosition = originPosition + Vector3.new(x * partSize, 0, z * partSize)
  105.             local pathPart = PartService.createPart(ItemType.STONE, pathPosition)
  106.             pathPart:setSize(Vector3.new(partSize, 0.2, partSize))
  107.             pathPart:setCollidable(true)
  108.             pathPart:setAnchored(true)
  109.         end
  110.     end
  111. end
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement