print("Hello world! Build maze")
local mazeCells = {} -- empty table with rows and columns
local pathWidth = 8
local wallHt = 8
local wallHtPos = wallHt/2
local wallSize = 2
local wallLen = pathWidth+2*wallSize
local wallOffset = pathWidth/2+wallSize/2
local floorHeight = 1
local floorHeightPos= floorHeight/2
local floorTileDist = pathWidth +wallSize
local stack={}
table.insert(stack,{zVal=0,xVal=0,})
local cols = 10
local rows = 10
local rnd = Random.new()
-- offset of the mazeCells in positions x,z
local newX = -50
local newZ = 30
local function createPart(x,y,z,px,py,pz)
local part = Instance.new("Part",workspace)
part.Anchored=true
part.Size=Vector3.new(x,y,z)
part.Position=Vector3.new(px,py,pz)
part.TopSurface = Enum.SurfaceType.Smooth
return part
end
local function makeFloor()
for z=0, rows-1,1 do
mazeCells[z] = {}
for x=0,cols-1,1 do
local posX = x*floorTileDist+newX
local posZ = z*floorTileDist+newZ
local part = createPart(pathWidth,floorHeight,pathWidth,posX,floorHeightPos,posZ)
mazeCells[z][x] = {tile=part}
end
end
end
local function makeWalls()
for z=0, rows-1,1 do
for x=0,cols-1,1 do
--east walls
local posX = x*floorTileDist+wallOffset+newX
local posZ = z*floorTileDist+newZ
local part = createPart(wallSize,wallHt,wallLen,posX,wallHtPos,posZ)
mazeCells[z][x].eastWall = part
if mazeCells[z][x+1] then
mazeCells[z][x+1].westWall = part
end
--south walls with offset on z is check and used
local posX = x*floorTileDist+newX
local posZ = z*floorTileDist+wallOffset+newZ
local part = createPart(wallLen,wallHt,wallSize,posX,wallHtPos,posZ)
mazeCells[z][x].southWall = part
if mazeCells[z+1] then
mazeCells[z+1][x].northWall = part
end
--west like east edge walls and x = 0, without the part and without storing part local part =
if x == 0 then
---local posX = x*floorTileDist+wallOffset, because x = 0 then is this:
local posX = -wallOffset+newX
local posZ = z*floorTileDist+newZ
createPart(wallSize,wallHt,wallLen,posX,wallHtPos,posZ)
end
--north edge walls is similar with south walls
--z*floorTileDist is zero and will be negative build way
if z==0 and x~=0 then
local posX = x*floorTileDist+newX
local posZ = -wallOffset+newZ
createPart(wallLen,wallHt,wallSize,posX,wallHtPos,posZ)
end
end
end
end
local function removeWall(wall)
local s = wall.Size
local p = wall.Position
wall.Size = Vector3.new(s.X, floorHeight, s.Z)
wall.Position = Vector3.new(p.X,floorHeightPos, p.z)
wall.BrickColor = BrickColor.Red()
end
local function rebuildMazeCells()
for z=0, rows-1,1 do
for x=0,cols-1,1 do
local cell = mazeCells[z][x]
if cell.visited then
cell.tile.BrickColor = BrickColor.Red()
if cell.northPath then
removeWall(cell.northWall)
end
if cell.eastPath then
removeWall(cell.eastWall)
end
if cell.southPath then
removeWall(cell.southWall)
end
if cell.westPath then
removeWall(cell.westWall)
end
end
end
end
end
local function getUnvisitedNeighbor(z, x)
local neighbors= {} --north:0, east:1, south:2, west:3
-- north
if mazeCells[z-1] and not mazeCells[z-1][x].visited then
table.insert(neighbors,0)
end
-- east
if mazeCells[z][x+1] and not mazeCells[z][x+1].visited then
table.insert(neighbors,1)
end
-- south
if mazeCells[z+1] and not mazeCells[z+1][x].visited then
table.insert(neighbors,2)
end
-- west
if mazeCells[z][x-1] and not mazeCells[z][x-1].visited then
table.insert(neighbors,3)
end
return neighbors
end
local function searchPath()
if stack==nil or #stack==0 then
return false
end
local stackCell = stack[#stack]
local x = stackCell.xVal
local z = stackCell.zVal
mazeCells[z][x].tile.BrickColor = BrickColor.Green()
wait()
local neighbors = getUnvisitedNeighbor(z,x)
if #neighbors > 0 then
local idx = rnd:NextInteger(1,#neighbors)
local nextCellDir = neighbors[idx]
if nextCellDir == 0 then --nord
mazeCells[z][x].northPath = true
mazeCells[z-1][x].southPath = true
mazeCells[z-1][x].visited = true
table.insert(stack, {zVal = z-1, xVal = x})
elseif nextCellDir == 1 then --east
mazeCells[z][x].eastPath = true
mazeCells[z][x+1].westPath = true
mazeCells[z][x+1].visited = true
table.insert(stack, {zVal = z, xVal = x+1})
elseif nextCellDir == 2 then --south
mazeCells[z][x].southPath = true
mazeCells[z+1][x].northPath = true
mazeCells[z+1][x].visited = true
table.insert(stack, {zVal = z+1, xVal = x})
elseif nextCellDir == 3 then --west
mazeCells[z][x].westPath = true
mazeCells[z][x-1].eastPath = true
mazeCells[z][x-1].visited = true
table.insert(stack, {zVal = z, xVal = x-1})
end
else -- if no neighbors in the table remove the top element from stack
table.remove(stack, #stack)
end
return true -- return true when is finished
end
makeFloor()
makeWalls()
mazeCells[0][0].visited = true
wait(6)
while searchPath() do
rebuildMazeCells()
wait()
end
print('Finished!')