Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local radius = math.floor(10)
- local originPosition = Vector3.new(342, 69, 342)
- local partSize = 3.1
- local maze = {}
- local directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }
- local function shuffle(tbl)
- for i = #tbl, 2, -1 do
- local j = math.random(i)
- tbl[i], tbl[j] = tbl[j], tbl[i]
- end
- end
- local function generateMaze(x, z)
- maze[x][z] = false
- shuffle(directions)
- for _, dir in ipairs(directions) do
- local nx, nz = x + dir[1] * 2, z + dir[2] * 2
- if maze[nx] and maze[nx][nz] then
- maze[x + dir[1]][z + dir[2]] = false
- generateMaze(nx, nz)
- end
- end
- end
- local function isValid(x, z)
- return maze[x] and maze[x][z] == false
- end
- local function findPath(startX, startZ, endX, endZ)
- local queue = {{startX, startZ}}
- local visited = {}
- visited[startX] = {}
- visited[startX][startZ] = true
- while #queue > 0 do
- local node = table.remove(queue, 1)
- local x, z = node[1], node[2]
- if x == endX and z == endZ then
- return true
- end
- for _, dir in ipairs(directions) do
- local nx, nz = x + dir[1], z + dir[2]
- if isValid(nx, nz) and (not (visited[nx] and visited[nx][nz])) then
- visited[nx] = visited[nx] or {}
- visited[nx][nz] = true
- table.insert(queue, {nx, nz})
- end
- end
- end
- return false
- end
- local function generateValidMaze()
- repeat
- for x = -radius - 1, radius + 1 do
- maze[x] = {}
- for z = -radius - 1, radius + 1 do
- maze[x][z] = true
- end
- end
- generateMaze(0, 0)
- local exitX, exitZ
- repeat
- local exitSide = math.random(4)
- if exitSide == 1 then exitX, exitZ = -radius-1, math.random(-radius, radius) end
- if exitSide == 2 then exitX, exitZ = radius+1, math.random(-radius, radius) end
- if exitSide == 3 then exitX, exitZ = math.random(-radius, radius), -radius-1 end
- if exitSide == 4 then exitX, exitZ = math.random(-radius, radius), radius+1 end
- until maze[exitX] and maze[exitX][exitZ]
- maze[exitX][exitZ] = false
- until findPath(0, 0, exitX, exitZ)
- return exitX, exitZ
- end
- for x = -radius - 1, radius + 1 do
- for z = -radius - 1, radius + 1 do
- for y = 0, 4 do
- local destroyPos = originPosition + Vector3.new(x * partSize, y * partSize, z * partSize)
- BlockService.destroyBlock(destroyPos)
- end
- end
- end
- local exitX, exitZ = generateValidMaze()
- for x = -radius - 1, radius + 1 do
- for z = -radius - 1, radius + 1 do
- local basePosition = originPosition + Vector3.new(x * partSize, 0, z * partSize)
- if maze[x][z] then
- for layer = 0, 4 do
- local blockPos = basePosition + Vector3.new(0, layer * partSize, 0)
- BlockService.placeBlock(ItemType.COBBLESTONE, blockPos)
- end
- else
- BlockService.placeBlock(ItemType.STONE, basePosition)
- end
- end
- end
Advertisement
Comments
-
- Maze code showcase : https://discord.com/channels/1132704840313749615/1341548622734688316/1386176575011028992
- 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