2020-04-29 11:31:35 +00:00
|
|
|
-------------------------------------
|
|
|
|
-- Maze generation script
|
|
|
|
-------------------------------------
|
|
|
|
-- A test by @Starbeamrainbowlabs
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
local M = {}
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
-- Intelligent table printing function: http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +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
|
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)
|
2020-04-29 11:31:35 +00:00
|
|
|
for y = 0, h - 1, 1 do
|
|
|
|
local line = ""
|
|
|
|
for x = 0, w - 1, 1 do
|
|
|
|
line = line .. space[y][x]
|
|
|
|
end
|
|
|
|
print(line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Initialise the world
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
function M.generate_maze(seed, width, height)
|
|
|
|
start_time = os.clock()
|
2020-04-29 11:31:35 +00:00
|
|
|
-- minetest.log("action", "width: "..width..", height: "..height)
|
|
|
|
math.randomseed(seed) -- seed the random number generator with the system clock
|
|
|
|
|
|
|
|
width = width - 1
|
|
|
|
height = height - 1
|
|
|
|
|
|
|
|
local world = {}
|
|
|
|
for y = 0, height, 1 do
|
|
|
|
world[y] = {}
|
|
|
|
for x = 0, width, 1 do
|
|
|
|
world[y][x] = "#"
|
|
|
|
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 = 1, 1 -- our current position
|
|
|
|
table.insert(nodes, { x = cx, y = cy })
|
|
|
|
world[cy][cx] = " "
|
|
|
|
while #nodes > 0 do
|
|
|
|
-- io.write("Nodes left: " .. curnode .. "\r")
|
|
|
|
--print("Nodes left: " .. #nodes)
|
|
|
|
--print("Currently at (" .. cx .. ", " .. cy .. ")")
|
|
|
|
|
|
|
|
local directions = "" -- the different directions we can move
|
|
|
|
if cy - 2 > 0 and world[cy - 2][cx] == "#" then
|
|
|
|
directions = directions .. "u"
|
|
|
|
end
|
|
|
|
if cy + 2 < height and world[cy + 2][cx] == "#" then
|
|
|
|
directions = directions .. "d"
|
|
|
|
end
|
|
|
|
if cx - 2 > 0 and world[cy][cx - 2] == "#" then
|
|
|
|
directions = directions .. "l"
|
|
|
|
end
|
|
|
|
if cx + 2 < width and world[cy][cx + 2] == "#" then
|
|
|
|
directions = directions .. "r"
|
|
|
|
end
|
|
|
|
--print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
|
|
|
if #directions > 0 then
|
|
|
|
-- we still have somewhere that we can go
|
|
|
|
--print("This node is not a dead end yet.")
|
|
|
|
local curdirnum = math.random(1, #directions)
|
|
|
|
local curdir = string.sub(directions, curdirnum, curdirnum)
|
|
|
|
if curdir == "u" then
|
|
|
|
world[cy - 1][cx] = " "
|
|
|
|
world[cy - 2][cx] = " "
|
|
|
|
cy = cy - 2
|
|
|
|
elseif curdir == "d" then
|
|
|
|
world[cy + 1][cx] = " "
|
|
|
|
world[cy + 2][cx] = " "
|
|
|
|
cy = cy + 2
|
|
|
|
elseif curdir == "l" then
|
|
|
|
world[cy][cx - 1] = " "
|
|
|
|
world[cy][cx - 2] = " "
|
|
|
|
cx = cx - 2
|
|
|
|
elseif curdir == "r" then
|
|
|
|
world[cy][cx + 1] = " "
|
|
|
|
world[cy][cx + 2] = " "
|
|
|
|
cx = cx + 2
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(nodes, { x = cx, y = cy })
|
|
|
|
else
|
|
|
|
--print("The node at " .. curnode .. " is a dead end.")
|
|
|
|
table.remove(nodes, curnode)
|
|
|
|
if #nodes > 0 then
|
|
|
|
--print("performing teleport.");
|
|
|
|
curnode = math.random(1, #nodes)
|
|
|
|
--print("New node: " .. curnode)
|
|
|
|
-- print("Nodes table: ")
|
|
|
|
-- print_r(nodes)
|
|
|
|
cx = nodes[curnode]["x"]
|
|
|
|
cy = nodes[curnode]["y"]
|
|
|
|
else
|
|
|
|
--print("Maze generation complete, no teleportation necessary.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--printspace(world, width, height)
|
|
|
|
end
|
2020-04-29 18:57:18 +00:00
|
|
|
|
|
|
|
end_time = os.clock()
|
|
|
|
|
|
|
|
return world, (end_time - start_time) * 1000
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
-- local world, time = generate_maze(os.time(), width, height)
|
|
|
|
-- printspace(world, width, height)
|
|
|
|
-- print("Generation completed in " .. time .. "s.")
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
return M
|