------------------------------------- -- 3D Maze generation script ------------------------------------- -- A test by @Starbeamrainbowlabs local M = {} -- if arg[1] ~= nil then -- width = tonumber(arg[1]) -- else -- width = 35 -- end -- if arg[2] ~= nil then -- height = tonumber(arg[2]) -- else -- height = 15 -- end -- if arg[3] ~= nil then -- depth = tonumber(arg[3]) -- else -- depth = 7 -- end -- if arg[4] ~= nil then -- seed = tonumber(arg[4]) -- else -- seed = os.time() -- end ---------------------------------- -- function to print out the world ---------------------------------- function M.printspace(space, w, h, d) for z = 0, d - 1, 1 do for y = 0, h - 1, 1 do local line = "" for x = 0, w - 1, 1 do line = line .. space[z][y][x] end print(line) end print("") end end -- Initialise the world function M.generate_maze(seed, width, height, depth, path_length, path_width, path_depth) start_time = os.clock() if not path_length then path_length = 2 end if not path_width then path_width = 1 end if not path_depth then path_depth = 1 end print("Generating maze "..width.."x"..height.."x"..depth) math.randomseed(seed) -- seed the random number generator with the system clock local world = {} for z = 0, depth, 1 do world[z] = {} for y = 0, height, 1 do world[z][y] = {} for x = 0, width, 1 do world[z][y][x] = "#" end end end -- do a random walk to create pathways local nodes = {} -- the nodes left that we haven't investigated local curnode = 1 -- the node we are currently operating on local cx, cy, cz = 1, 1, 1 -- our current position table.insert(nodes, { x = cx, y = cy, z = cz }) world[cz][cy][cx] = " " while #nodes > 0 do -- io.write("Nodes left: " .. curnode .. "\r") --print("Nodes left: " .. #nodes) -- print("Currently at ("..cx..", "..cy..", "..cz.."), up: "..(cy-path_length)..", down: "..cy+path_length..", left: "..cx-path_length..", right: "..cx+path_length..", d++: "..cz+path_length..", d--: "..cz-path_length..", width: "..width..", height: "..height..", depth: "..depth) local directions = "" -- the different directions we can move in if cz - path_length > 0 and world[cz - path_length][cy][cx] == "#" then directions = directions .. "-" directions = directions .. "u" -- print("d-- | cz: "..cz..", target: "..cz-path_length..", value: '"..world[cz - path_length][cy][cx].."', path_length: "..path_length) end if cz + path_length < depth-path_depth and world[cz + path_length][cy][cx] == "#" then directions = directions .. "+" -- print("d++ | cz: "..cz..", target: "..cz+path_length..", value: '"..world[cz + path_length][cy][cx].."', path_length: "..path_length) end if cy - path_length > 0 and world[cz][cy - path_length][cx] == "#" then directions = directions .. "u" -- print("up | cy: "..cy..", target: "..cy-path_length..", value: '"..world[cz][cy - path_length][cx].."', path_length: "..path_length) end if cy + path_length < height-path_width and world[cz][cy + path_length][cx] == "#" then directions = directions .. "d" -- print("down | cy: "..cy..", target: "..cy+path_length..", value: '"..world[cz][cy + path_length][cx].."', path_length: "..path_length) end if cx - path_length > 0 and world[cz][cy][cx - path_length] == "#" then directions = directions .. "l" -- print("left | cx: "..cx..", target: "..cx-path_length..", value: '"..world[cz][cy][cx - path_length].."', path_length: "..path_length) end if cx + path_length < width-path_width and world[cz][cy][cx + path_length] == "#" then directions = directions .. "r" -- print("right | cx: "..cx..", target: "..cx+path_length..", value: '"..world[cz][cy][cx + path_length].."', path_length: "..path_length) end -- If this is 1 or less, then we will switch our attention to another candidate node after moving local shift_attention = math.random(0, 3) print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode) if #directions > 0 then -- we still have somewhere that we can go local curdirnum = math.random(1, #directions) local curdir = string.sub(directions, curdirnum, curdirnum) print("chose '"..curdir.."'") if curdir == "+" then for iz = cz,cz+path_length+(path_depth-1) do for ix = cx,cx+(path_width-1) do for iy = cy,cy+(path_width-1) do world[iz][iy][ix] = " " end end end -- world[cz + 1][cy][cx] = " " -- world[cz + 2][cy][cx] = " " cz = cz + path_length elseif curdir == "-" then for iz = cz-path_length,cz do for ix = cx,cx+(path_width-1) do for iy = cy,cy+(path_width-1) do world[iz][iy][ix] = " " end end end -- world[cz - 1][cy][cx] = " " -- world[cz - 2][cy][cx] = " " cz = cz - path_length elseif curdir == "u" then for iz = cz,cz+(path_depth-1) do for ix = cx,cx+(path_width-1) do for iy = cy-path_length,cy do world[iz][iy][ix] = " " end end end -- world[cz][cy - 1][cx] = " " -- world[cz][cy - 2][cx] = " " cy = cy - path_length elseif curdir == "d" then for iz = cz,cz+(path_depth-1) do for ix = cx,cx+(path_width-1) do for iy = cy,cy+path_length+(path_width-1) do world[iz][iy][ix] = " " end end end -- world[cz][cy + 1][cx] = " " -- world[cz][cy + 2][cx] = " " cy = cy + path_length elseif curdir == "l" then for iz = cz,cz+(path_depth-1) do for iy = cy,cy+(path_width-1) do for ix = cx-path_length,cx do world[iz][iy][ix] = " " end end end -- world[cz][cy][cx - 1] = " " -- world[cz][cy][cx - 2] = " " cx = cx - path_length elseif curdir == "r" then for iz = cz,cz+(path_depth-1) do for iy = cy,cy+(path_width-1) do for ix = cx,cx+path_length+(path_width-1) do world[iz][iy][ix] = " " end end end -- world[cz][cy][cx + 1] = " " -- world[cz][cy][cx + 2] = " " cx = cx + path_length end if #directions > 1 then table.insert(nodes, { x = cx, y = cy, z = cz }) end else table.remove(nodes, curnode) end if #directions == 0 or shift_attention <= 1 then if #nodes > 0 then curnode = math.random(1, #nodes) cx = nodes[curnode]["x"] cy = nodes[curnode]["y"] cz = nodes[curnode]["z"] end end M.printspace(world, width, height, depth) io.read("*l") end end_time = os.clock() return world, (end_time - start_time) * 1000 end return M -- local world = M.generate_maze3d(seed, width, height, depth) -- -- M.printspace(world, width, height, depth) -- end_time = os.clock() -- print("Generation completed in " .. (end_time - start_time) .. "s.")