so i made a very primitive random maze generator for my game and it works fine, but it has one major problem, its really slow and i dont really have any idea what i could do.
here is my current script i just made and a video:
local mapSizeX = 10
local mapSizeY = 10
local segmentStart = workspace.Map.Segments.SegmentStart
for length = 1,mapSizeX do
for height = 1,mapSizeY do
if length == 1 and height == 1 then
print("segment start")
else
local segmentClone = workspace.Map.Segments.SegmentStart:Clone()
segmentClone.Name = "Segment"
segmentClone.Parent = workspace.Map.Segments
segmentClone:SetPrimaryPartCFrame(CFrame.new(segmentClone.PrimaryPart.Position+Vector3.new((length-1)*51,(height-1)*51,0)))
segmentClone.Height.Value = height
segmentClone.Length.Value = length
wait()
end
end
end
function getSegment(length,height)
local segment
for i,v in pairs(workspace.Map.Segments:GetChildren()) do
if v.Length.Value == length and v.Height.Value == height then
segment = v
end
end
if segment ~= nil then
return segment
else
return "N/A"
end
end
local segmentsVisited = {}
function getNeighbours(segment)
local neighbours = {}
table.insert(neighbours,getSegment(segment.Length.Value + 1, segment.Height.Value))
table.insert(neighbours,getSegment(segment.Length.Value - 1, segment.Height.Value))
table.insert(neighbours,getSegment(segment.Length.Value, segment.Height.Value - 1))
table.insert(neighbours,getSegment(segment.Length.Value, segment.Height.Value + 1))
local neighboursFiltered = {}
for i,v in pairs(neighbours) do
if v ~= "N/A" then
table.insert(neighboursFiltered,v)
end
end
return neighboursFiltered
end
function checkSegment(segment)
print("workin")
local neighbours = getNeighbours(segment)
local chosenNeighbour = neighbours[math.random(1,#neighbours)]
if not table.find(segmentsVisited,chosenNeighbour) then
if chosenNeighbour.Height.Value == segment.Height.Value then
if chosenNeighbour.Length.Value < segment.Length.Value then
if segment:FindFirstChild("WallLeft") then
segment.WallLeft:Destroy()
end
if chosenNeighbour:FindFirstChild("WallRight") then
chosenNeighbour.WallRight:Destroy()
end
else
if segment:FindFirstChild("WallRight") then
segment.WallRight:Destroy()
end
if chosenNeighbour:FindFirstChild("WallLeft") then
chosenNeighbour.WallLeft:Destroy()
end
end
else
if chosenNeighbour.Height.Value < segment.Height.Value then
if segment:FindFirstChild("WallDown") then
segment.WallDown:Destroy()
end
if chosenNeighbour:FindFirstChild("WallUp") then
chosenNeighbour.WallUp:Destroy()
end
else
if segment:FindFirstChild("WallUp") then
segment.WallUp:Destroy()
end
if chosenNeighbour:FindFirstChild("WallDown") then
chosenNeighbour.WallDown:Destroy()
end
end
end
table.insert(segmentsVisited,chosenNeighbour)
chosenNeighbour.Back.Color = Color3.fromRGB(255, 0, 0)
end
wait()
if #segmentsVisited ~= mapSizeX*mapSizeY then
checkSegment(chosenNeighbour)
else
for i,v in pairs(segmentsVisited) do
v.Back.Color = Color3.fromRGB(0,255,0)
wait()
end
for i,v in pairs(segmentsVisited) do
v.Back.Color = Color3.fromRGB(99, 95, 98)
wait()
end
workspace.Map.TemporaryWalls:Destroy()
end
end
checkSegment(segmentStart)