2020-04-29 11:31:35 +00:00
|
|
|
-------------------------------------
|
2020-04-29 11:34:19 +00:00
|
|
|
-- 3D Maze generation script
|
2020-04-29 11:31:35 +00:00
|
|
|
-------------------------------------
|
|
|
|
-- A test by @Starbeamrainbowlabs
|
2020-04-29 18:57:18 +00:00
|
|
|
local M = {}
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-05-03 15:48:50 +00:00
|
|
|
-- 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
|
2020-04-29 11:31:35 +00:00
|
|
|
|
|
|
|
----------------------------------
|
|
|
|
-- function to print out the world
|
|
|
|
----------------------------------
|
2020-04-29 18:57:18 +00:00
|
|
|
function M.printspace(space, w, h, d)
|
2020-04-29 11:31:35 +00:00
|
|
|
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
|
|
|
|
|
2020-09-30 00:20:17 +00:00
|
|
|
function M.generate_maze(seed, width, height, depth, path_length, path_width, path_depth, branching_factor)
|
2020-08-27 21:37:09 +00:00
|
|
|
local start_time = os.clock()
|
2020-05-03 15:48:50 +00:00
|
|
|
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
|
2020-09-30 00:20:17 +00:00
|
|
|
if not branching_factor then branching_factor = 3 end
|
2020-05-03 15:48:50 +00:00
|
|
|
|
2020-08-27 21:37:09 +00:00
|
|
|
-- print("Generating maze "..width.."x"..height.."x"..depth.." | path: length "..path_length.." width "..path_width.." depth "..path_depth)
|
2020-04-29 11:31:35 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
local directions = "" -- the different directions we can move in
|
2020-05-03 15:48:50 +00:00
|
|
|
if cz - path_length > 0 and world[cz - path_length][cy][cx] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "-"
|
|
|
|
end
|
2020-05-03 15:48:50 +00:00
|
|
|
if cz + path_length < depth-path_depth and world[cz + path_length][cy][cx] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "+"
|
|
|
|
end
|
2020-05-03 15:48:50 +00:00
|
|
|
if cy - path_length > 0 and world[cz][cy - path_length][cx] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "u"
|
|
|
|
end
|
2020-05-03 15:48:50 +00:00
|
|
|
if cy + path_length < height-path_width and world[cz][cy + path_length][cx] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "d"
|
|
|
|
end
|
2020-05-03 15:48:50 +00:00
|
|
|
if cx - path_length > 0 and world[cz][cy][cx - path_length] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "l"
|
|
|
|
end
|
2020-05-03 15:48:50 +00:00
|
|
|
if cx + path_length < width-path_width and world[cz][cy][cx + path_length] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "r"
|
|
|
|
end
|
2020-04-30 00:59:23 +00:00
|
|
|
|
|
|
|
-- If this is 1 or less, then we will switch our attention to another candidate node after moving
|
2020-09-30 00:20:17 +00:00
|
|
|
local shift_attention = math.random(0, branching_factor)
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-08-27 21:37:09 +00:00
|
|
|
--print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
2020-04-29 11:31:35 +00:00
|
|
|
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)
|
2020-04-30 00:59:23 +00:00
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
|
|
|
|
if curdir == "+" then
|
2020-05-03 15:48:50 +00:00
|
|
|
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
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "-" then
|
2020-05-05 23:44:46 +00:00
|
|
|
for iz = cz-path_length,cz do
|
2020-05-03 15:48:50 +00:00
|
|
|
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
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "u" then
|
2020-05-03 15:48:50 +00:00
|
|
|
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
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "d" then
|
2020-05-03 15:48:50 +00:00
|
|
|
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] = " "
|
2020-08-27 21:37:09 +00:00
|
|
|
-- print("[tunnel/d] ("..ix..", "..iy..", "..iz..")")
|
2020-05-03 15:48:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- world[cz][cy + 1][cx] = " "
|
|
|
|
-- world[cz][cy + 2][cx] = " "
|
|
|
|
cy = cy + path_length
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "l" then
|
2020-05-03 15:48:50 +00:00
|
|
|
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
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "r" then
|
2020-05-03 15:48:50 +00:00
|
|
|
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
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
|
|
|
|
2020-08-27 21:37:09 +00:00
|
|
|
table.insert(nodes, { x = cx, y = cy, z = cz })
|
2020-04-29 11:31:35 +00:00
|
|
|
else
|
|
|
|
table.remove(nodes, curnode)
|
2020-04-30 00:59:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if #directions == 0 or shift_attention <= 1 then
|
2020-04-29 11:31:35 +00:00
|
|
|
if #nodes > 0 then
|
|
|
|
curnode = math.random(1, #nodes)
|
2020-04-29 18:57:18 +00:00
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
cx = nodes[curnode]["x"]
|
|
|
|
cy = nodes[curnode]["y"]
|
|
|
|
cz = nodes[curnode]["z"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-29 18:57:18 +00:00
|
|
|
|
2020-08-27 21:37:09 +00:00
|
|
|
-- WARNING: os.clock() isn't accurate, because it gives the total **CPU time**, not **real time**!
|
|
|
|
local end_time = os.clock()
|
2020-04-29 18:57:18 +00:00
|
|
|
return world, (end_time - start_time) * 1000
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
return M
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
-- 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.")
|