Advertisement
Cat_in_the_hat

Maze genrater (updated )

Feb 19th, 2025 (edited)
208
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. local radius = math.floor(10)
  2. local originPosition = Vector3.new(342, 69, 342)
  3. local partSize = 3.1
  4. local maze = {}
  5. local directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }
  6.  
  7. local function shuffle(tbl)
  8.     for i = #tbl, 2, -1 do
  9.         local j = math.random(i)
  10.         tbl[i], tbl[j] = tbl[j], tbl[i]
  11.     end
  12. end
  13.  
  14. local function generateMaze(x, z)
  15.     maze[x][z] = false
  16.     shuffle(directions)
  17.     for _, dir in ipairs(directions) do
  18.         local nx, nz = x + dir[1] * 2, z + dir[2] * 2
  19.         if maze[nx] and maze[nx][nz] then
  20.             maze[x + dir[1]][z + dir[2]] = false
  21.             generateMaze(nx, nz)
  22.         end
  23.     end
  24. end
  25.  
  26. local function isValid(x, z)
  27.     return maze[x] and maze[x][z] == false
  28. end
  29.  
  30. local function findPath(startX, startZ, endX, endZ)
  31.     local queue = {{startX, startZ}}
  32.     local visited = {}
  33.     visited[startX] = {}
  34.     visited[startX][startZ] = true
  35.     while #queue > 0 do
  36.         local node = table.remove(queue, 1)
  37.         local x, z = node[1], node[2]
  38.         if x == endX and z == endZ then  
  39.             return true  
  40.         end  
  41.         for _, dir in ipairs(directions) do  
  42.             local nx, nz = x + dir[1], z + dir[2]  
  43.             if isValid(nx, nz) and (not (visited[nx] and visited[nx][nz])) then  
  44.                 visited[nx] = visited[nx] or {}
  45.                 visited[nx][nz] = true  
  46.                 table.insert(queue, {nx, nz})  
  47.             end  
  48.         end
  49.     end
  50.     return false
  51. end
  52.  
  53. local function generateValidMaze()
  54.     repeat
  55.         for x = -radius - 1, radius + 1 do
  56.             maze[x] = {}
  57.             for z = -radius - 1, radius + 1 do
  58.                 maze[x][z] = true
  59.             end
  60.         end
  61.         generateMaze(0, 0)
  62.         local exitX, exitZ  
  63.         repeat  
  64.             local exitSide = math.random(4)  
  65.             if exitSide == 1 then exitX, exitZ = -radius-1, math.random(-radius, radius) end  
  66.             if exitSide == 2 then exitX, exitZ = radius+1, math.random(-radius, radius) end  
  67.             if exitSide == 3 then exitX, exitZ = math.random(-radius, radius), -radius-1 end  
  68.             if exitSide == 4 then exitX, exitZ = math.random(-radius, radius), radius+1 end  
  69.         until maze[exitX] and maze[exitX][exitZ]  
  70.         maze[exitX][exitZ] = false
  71.     until findPath(0, 0, exitX, exitZ)
  72.     return exitX, exitZ
  73. end
  74.  
  75. for x = -radius - 1, radius + 1 do
  76.     for z = -radius - 1, radius + 1 do
  77.         for y = 0, 4 do
  78.             local destroyPos = originPosition + Vector3.new(x * partSize, y * partSize, z * partSize)
  79.             BlockService.destroyBlock(destroyPos)
  80.         end
  81.     end
  82. end
  83.  
  84. local exitX, exitZ = generateValidMaze()
  85.  
  86. for x = -radius - 1, radius + 1 do
  87.     for z = -radius - 1, radius + 1 do
  88.         local basePosition = originPosition + Vector3.new(x * partSize, 0, z * partSize)
  89.         if maze[x][z] then
  90.             for layer = 0, 4 do
  91.                 local blockPos = basePosition + Vector3.new(0, layer * partSize, 0)
  92.                 BlockService.placeBlock(ItemType.COBBLESTONE, blockPos)
  93.             end
  94.         else
  95.             BlockService.placeBlock(ItemType.STONE, basePosition)
  96.         end
  97.     end
  98. end
Advertisement
Comments
  • Cat_in_the_hat
    12 hours
    # Java 0.19 KB | 0 0
    1. Maze code showcase : https://discord.com/channels/1132704840313749615/1341548622734688316/1386176575011028992
    2. It's a code that generates  a maxe using blocks in bedwars so you don't need to
Add Comment
Please, Sign In to add comment
Advertisement