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 .. "," .. startZ] = true
- local path = {}
- 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 .. "," .. nz] then
- visited[nx .. "," .. nz] = true
- table.insert(queue, {nx, nz})
- table.insert(path, {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
- local exitX, exitZ = generateValidMaze()
- for x = -radius - 1, radius + 1 do
- for z = -radius - 1, radius + 1 do
- if maze[x][z] then
- for layer = 0, 4 do
- local partPosition = originPosition + Vector3.new(x * partSize, 0, z * partSize) + Vector3.new(0, layer * partSize, 0)
- local newPart = PartService.createPart(ItemType.COBBLESTONE, partPosition)
- newPart:setSize(Vector3.new(partSize, partSize, partSize))
- newPart:setCollidable(true)
- newPart:setAnchored(true)
- end
- end
- end
- end
- for x = -radius - 1, radius + 1 do
- for z = -radius - 1, radius + 1 do
- if not maze[x][z] then
- local pathPosition = originPosition + Vector3.new(x * partSize, 0, z * partSize)
- local pathPart = PartService.createPart(ItemType.STONE, pathPosition)
- pathPart:setSize(Vector3.new(partSize, 0.2, partSize))
- pathPart:setCollidable(true)
- pathPart:setAnchored(true)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement