maze.lua: add support for varying the path width and length, but we're enountering issues.

We keep getting loops, which should be impossible - and when the path 
width is more than 1 it misses out a bit in the bottom right of a corner 
from horizontal to up.
This commit is contained in:
Starbeamrainbowlabs 2020-05-03 02:01:40 +01:00
parent 0cf62aadd7
commit 9fabc496fb
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -22,19 +22,26 @@ local M = {}
-- function to print out the world
----------------------------------
function M.printspace(space, w, h)
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
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
function M.generate_maze(seed, width, height)
start_time = os.clock()
function M.generate_maze(seed, width, height, path_length, path_width)
start_time = os.clock()
if not path_length then path_length = 2 end
if not path_width then path_width = 1 end
path_length = 3
path_width = 1
-- minetest.log("action", "width: "..width..", height: "..height)
math.randomseed(seed) -- seed the random number generator with the system clock
@ -48,7 +55,7 @@ function M.generate_maze(seed, width, height)
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
@ -61,40 +68,60 @@ function M.generate_maze(seed, width, height)
--print("Currently at (" .. cx .. ", " .. cy .. ")")
local directions = "" -- the different directions we can move
if cy - 2 > 0 and world[cy - 2][cx] == "#" then
if cy - path_length > 0 and world[cy - path_length][cx] == "#" then
directions = directions .. "u"
end
if cy + 2 < height and world[cy + 2][cx] == "#" then
if cy + path_length < height and world[cy + path_length][cx] == "#" then
directions = directions .. "d"
end
if cx - 2 > 0 and world[cy][cx - 2] == "#" then
if cx - path_length > 0 and world[cy][cx - path_length] == "#" then
directions = directions .. "l"
end
if cx + 2 < width and world[cy][cx + 2] == "#" then
if cx + path_length < width and world[cy][cx + path_length] == "#" then
directions = directions .. "r"
end
--print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
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
for ix = cx,cx+path_width-1 do
for iy = cy-path_length,cy do
world[iy][ix] = " "
end
end
-- world[cy - 1][cx] = " "
-- world[cy - 2][cx] = " "
cy = cy - path_length
elseif curdir == "d" then
world[cy + 1][cx] = " "
world[cy + 2][cx] = " "
cy = cy + 2
for ix = cx,cx+path_width-1 do
for iy = cy,cy+path_length do
world[iy][ix] = " "
end
end
-- world[cy + 1][cx] = " "
-- world[cy + 2][cx] = " "
cy = cy + path_length
elseif curdir == "l" then
world[cy][cx - 1] = " "
world[cy][cx - 2] = " "
cx = cx - 2
for iy = cy,cy+path_width-1 do
for ix = cx-path_length,cx do
world[iy][ix] = " "
end
end
-- world[cy][cx - 1] = " "
-- world[cy][cx - 2] = " "
cx = cx - path_length
elseif curdir == "r" then
world[cy][cx + 1] = " "
world[cy][cx + 2] = " "
cx = cx + 2
for iy = cy,cy+path_width-1 do
for ix = cx,cx+path_length-1 do
world[iy][ix] = " "
end
end
-- world[cy][cx + 1] = " "
-- world[cy][cx + 2] = " "
cx = cx + path_length
end
table.insert(nodes, { x = cx, y = cy })
@ -113,10 +140,11 @@ function M.generate_maze(seed, width, height)
--print("Maze generation complete, no teleportation necessary.")
end
end
--printspace(world, width, height)
M.printspace(world, width, height)
io.read("*l")
end
end_time = os.clock()
end_time = os.clock()
return world, (end_time - start_time) * 1000
end