You can see my video tutorial about how this source script works on the official youtube channel:
This is the source code used in this video tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 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!') |