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:
parent
0cf62aadd7
commit
9fabc496fb
1 changed files with 57 additions and 29 deletions
84
maze.lua
84
maze.lua
|
@ -22,19 +22,26 @@ local M = {}
|
||||||
-- function to print out the world
|
-- function to print out the world
|
||||||
----------------------------------
|
----------------------------------
|
||||||
function M.printspace(space, w, h)
|
function M.printspace(space, w, h)
|
||||||
for y = 0, h - 1, 1 do
|
for y = 0, h - 1, 1 do
|
||||||
local line = ""
|
local line = ""
|
||||||
for x = 0, w - 1, 1 do
|
for x = 0, w - 1, 1 do
|
||||||
line = line .. space[y][x]
|
line = line .. space[y][x]
|
||||||
end
|
end
|
||||||
print(line)
|
print(line)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Initialise the world
|
-- Initialise the world
|
||||||
|
|
||||||
function M.generate_maze(seed, width, height)
|
function M.generate_maze(seed, width, height, path_length, path_width)
|
||||||
start_time = os.clock()
|
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)
|
-- minetest.log("action", "width: "..width..", height: "..height)
|
||||||
math.randomseed(seed) -- seed the random number generator with the system clock
|
math.randomseed(seed) -- seed the random number generator with the system clock
|
||||||
|
|
||||||
|
@ -61,40 +68,60 @@ function M.generate_maze(seed, width, height)
|
||||||
--print("Currently at (" .. cx .. ", " .. cy .. ")")
|
--print("Currently at (" .. cx .. ", " .. cy .. ")")
|
||||||
|
|
||||||
local directions = "" -- the different directions we can move
|
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"
|
directions = directions .. "u"
|
||||||
end
|
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"
|
directions = directions .. "d"
|
||||||
end
|
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"
|
directions = directions .. "l"
|
||||||
end
|
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"
|
directions = directions .. "r"
|
||||||
end
|
end
|
||||||
--print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
||||||
if #directions > 0 then
|
if #directions > 0 then
|
||||||
-- we still have somewhere that we can go
|
-- we still have somewhere that we can go
|
||||||
--print("This node is not a dead end yet.")
|
--print("This node is not a dead end yet.")
|
||||||
local curdirnum = math.random(1, #directions)
|
local curdirnum = math.random(1, #directions)
|
||||||
local curdir = string.sub(directions, curdirnum, curdirnum)
|
local curdir = string.sub(directions, curdirnum, curdirnum)
|
||||||
if curdir == "u" then
|
if curdir == "u" then
|
||||||
world[cy - 1][cx] = " "
|
for ix = cx,cx+path_width-1 do
|
||||||
world[cy - 2][cx] = " "
|
for iy = cy-path_length,cy do
|
||||||
cy = cy - 2
|
world[iy][ix] = " "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- world[cy - 1][cx] = " "
|
||||||
|
-- world[cy - 2][cx] = " "
|
||||||
|
cy = cy - path_length
|
||||||
elseif curdir == "d" then
|
elseif curdir == "d" then
|
||||||
world[cy + 1][cx] = " "
|
for ix = cx,cx+path_width-1 do
|
||||||
world[cy + 2][cx] = " "
|
for iy = cy,cy+path_length do
|
||||||
cy = cy + 2
|
world[iy][ix] = " "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- world[cy + 1][cx] = " "
|
||||||
|
-- world[cy + 2][cx] = " "
|
||||||
|
cy = cy + path_length
|
||||||
elseif curdir == "l" then
|
elseif curdir == "l" then
|
||||||
world[cy][cx - 1] = " "
|
for iy = cy,cy+path_width-1 do
|
||||||
world[cy][cx - 2] = " "
|
for ix = cx-path_length,cx do
|
||||||
cx = cx - 2
|
world[iy][ix] = " "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- world[cy][cx - 1] = " "
|
||||||
|
-- world[cy][cx - 2] = " "
|
||||||
|
cx = cx - path_length
|
||||||
elseif curdir == "r" then
|
elseif curdir == "r" then
|
||||||
world[cy][cx + 1] = " "
|
for iy = cy,cy+path_width-1 do
|
||||||
world[cy][cx + 2] = " "
|
for ix = cx,cx+path_length-1 do
|
||||||
cx = cx + 2
|
world[iy][ix] = " "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- world[cy][cx + 1] = " "
|
||||||
|
-- world[cy][cx + 2] = " "
|
||||||
|
cx = cx + path_length
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(nodes, { x = cx, y = cy })
|
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.")
|
--print("Maze generation complete, no teleportation necessary.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--printspace(world, width, height)
|
M.printspace(world, width, height)
|
||||||
|
io.read("*l")
|
||||||
end
|
end
|
||||||
|
|
||||||
end_time = os.clock()
|
end_time = os.clock()
|
||||||
|
|
||||||
return world, (end_time - start_time) * 1000
|
return world, (end_time - start_time) * 1000
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue