multimaze/maze3d.lua

160 lines
4.1 KiB
Lua
Raw Normal View History

-------------------------------------
2020-04-29 11:34:19 +00:00
-- 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)
start_time = os.clock()
print("Generating maze "..width.."x"..height.."x"..depth)
math.randomseed(seed) -- seed the random number generator with the system clock
width = width - 1
height = height - 1
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
if cz - 2 > 0 and world[cz - 2][cy][cx] == "#" then
directions = directions .. "-"
end
if cz + 2 < depth and world[cz + 2][cy][cx] == "#" then
directions = directions .. "+"
end
if cy - 2 > 0 and world[cz][cy - 2][cx] == "#" then
directions = directions .. "u"
end
if cy + 2 < height and world[cz][cy + 2][cx] == "#" then
directions = directions .. "d"
end
if cx - 2 > 0 and world[cz][cy][cx - 2] == "#" then
directions = directions .. "l"
end
if cx + 2 < width and world[cz][cy][cx + 2] == "#" then
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
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)
2020-04-30 00:59:23 +00:00
if curdir == "+" then
world[cz + 1][cy][cx] = " "
world[cz + 2][cy][cx] = " "
cz = cz + 2
elseif curdir == "-" then
world[cz - 1][cy][cx] = " "
world[cz - 2][cy][cx] = " "
cz = cz - 2
elseif curdir == "u" then
world[cz][cy - 1][cx] = " "
world[cz][cy - 2][cx] = " "
cy = cy - 2
elseif curdir == "d" then
world[cz][cy + 1][cx] = " "
world[cz][cy + 2][cx] = " "
cy = cy + 2
elseif curdir == "l" then
world[cz][cy][cx - 1] = " "
world[cz][cy][cx - 2] = " "
cx = cx - 2
elseif curdir == "r" then
world[cz][cy][cx + 1] = " "
world[cz][cy][cx + 2] = " "
cx = cx + 2
end
table.insert(nodes, { x = cx, y = cy, z = cz })
else
table.remove(nodes, curnode)
2020-04-30 00:59:23 +00:00
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
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.")