maze2d: allow varying the path width & length

This commit is contained in:
Starbeamrainbowlabs 2020-05-03 14:23:25 +01:00
parent 9fabc496fb
commit 0554e37fcc
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 40 additions and 23 deletions

View file

@ -16,32 +16,41 @@ local settings = {
filename = "maze.scad", filename = "maze.scad",
scale = 5, scale = 5,
inverted = true inverted = true,
path_length = 2,
path_width = 1
} }
for i=1,#arg do for i=1,#arg do
if not strings.starts_with(arg[i], "-") then if not strings.starts_with(arg[i], "-") then
table.insert(settings.extras, arg[i]) table.insert(settings.extras, arg[i])
elseif string.match(arg[i], "--width") or string.match(arg[i], "-w") then elseif arg[i] == "--width" or arg[i] == "-w" then
i = i + 1 i = i + 1
settings.width = tonumber(arg[i]) settings.width = tonumber(arg[i])
elseif string.match(arg[i], "--height") or string.match(arg[i], "-h") then elseif arg[i] == "--height" or arg[i] == "-h" then
i = i + 1 i = i + 1
settings.height = tonumber(arg[i]) settings.height = tonumber(arg[i])
elseif string.match(arg[i], "--depth") or string.match(arg[i], "-d") then elseif arg[i] == "--depth" or arg[i] == "-d" then
i = i + 1 i = i + 1
settings.depth = tonumber(arg[i]) settings.depth = tonumber(arg[i])
elseif string.match(arg[i], "--seed") or string.match(arg[i], "-s") then elseif arg[i] == "--seed" or arg[i] == "-s" then
i = i + 1 i = i + 1
settings.seed = tonumber(arg[i]) settings.seed = tonumber(arg[i])
elseif string.match(arg[i], "--filename") or string.match(arg[i], "-f") then elseif arg[i] == "--filename" or arg[i] == "-f" then
i = i + 1 i = i + 1
settings.filename = arg[i] settings.filename = arg[i]
elseif string.match(arg[i], "--scale") then elseif arg[i] == "--scale" then
i = i + 1 i = i + 1
settings.scale = tonumber(arg[i]) settings.scale = tonumber(arg[i])
elseif string.match(arg[i], "--no-invert") then elseif arg[i] == "--no-invert" then
settings.inverted = false settings.inverted = false
elseif arg[i] == "--path-length" then
i = i + 1
settings.path_length = tonumber(arg[i])
elseif arg[i] == "--path-width" then
i = i + 1
settings.path_width = tonumber(arg[i])
end end
end end
@ -71,6 +80,8 @@ Options:
-f --filename {string} Sets the output filename -f --filename {string} Sets the output filename
--no-invert Don't invert the output [openscad output only] --no-invert Don't invert the output [openscad output only]
--scale {int} Sets the scale of the output [openscad output only] --scale {int} Sets the scale of the output [openscad output only]
--path-length {int} Sets the path length
--path-width {int} Sets the path width
]]) ]])
end end
@ -78,7 +89,9 @@ local function maze_text()
local world, time = maze2d.generate_maze( local world, time = maze2d.generate_maze(
settings.seed, settings.seed,
settings.width, settings.width,
settings.height settings.height,
settings.path_length,
settings.path_width
) )
maze2d.printspace(world, settings.width, settings.height) maze2d.printspace(world, settings.width, settings.height)
print("Generation completed in " .. time .. "ms.") print("Generation completed in " .. time .. "ms.")

View file

@ -35,13 +35,10 @@ end
function M.generate_maze(seed, width, height, path_length, path_width) 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_length then path_length = 2 end
if not path_width then path_width = 1 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
@ -65,29 +62,33 @@ function M.generate_maze(seed, width, height, path_length, path_width)
while #nodes > 0 do while #nodes > 0 do
-- io.write("Nodes left: " .. curnode .. "\r") -- io.write("Nodes left: " .. curnode .. "\r")
--print("Nodes left: " .. #nodes) --print("Nodes left: " .. #nodes)
--print("Currently at (" .. cx .. ", " .. cy .. ")") -- print("Currently at (" .. cx .. ", " .. cy .. "), up: "..(cy-path_length)..", down: "..cy+path_length..", left: "..cx-path_length..", right: "..cx+path_length..", width: "..width..", height: "..height)
local directions = "" -- the different directions we can move local directions = "" -- the different directions we can move
if cy - path_length > 0 and world[cy - path_length][cx] == "#" then if cy - path_length > 0 and world[cy - path_length][cx] == "#" then
directions = directions .. "u" directions = directions .. "u"
-- print("up | cy: "..cy..", target: "..cy-path_length..", value: '"..world[cy - path_length][cx].."', path_length: "..path_length)
end end
if cy + path_length < height and world[cy + path_length][cx] == "#" then if cy + path_length < height and world[cy + path_length][cx] == "#" then
directions = directions .. "d" directions = directions .. "d"
-- print("down | cy: "..cy..", target: "..cy+path_length..", value: '"..world[cy + path_length][cx].."', path_length: "..path_length)
end end
if cx - path_length > 0 and world[cy][cx - path_length] == "#" then if cx - path_length > 0 and world[cy][cx - path_length] == "#" then
directions = directions .. "l" directions = directions .. "l"
-- print("left | cx: "..cx..", target: "..cx-path_length..", value: '"..world[cy][cx - path_length].."', path_length: "..path_length)
end end
if cx + path_length < width and world[cy][cx + path_length] == "#" then if cx + path_length < width and world[cy][cx + path_length] == "#" then
directions = directions .. "r" directions = directions .. "r"
-- print("right | cx: "..cx..", target: "..cx+path_length..", value: '"..world[cy][cx + path_length].."', path_length: "..path_length)
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
for ix = cx,cx+path_width-1 do for ix = cx,cx+(path_width-1) do
for iy = cy-path_length,cy do for iy = cy-path_length,cy do
world[iy][ix] = " " world[iy][ix] = " "
end end
@ -97,7 +98,7 @@ function M.generate_maze(seed, width, height, path_length, path_width)
cy = cy - path_length cy = cy - path_length
elseif curdir == "d" then elseif curdir == "d" then
for ix = cx,cx+path_width-1 do for ix = cx,cx+path_width-1 do
for iy = cy,cy+path_length do for iy = cy,cy+path_length+(path_width-1) do
world[iy][ix] = " " world[iy][ix] = " "
end end
end end
@ -114,8 +115,8 @@ function M.generate_maze(seed, width, height, path_length, path_width)
-- world[cy][cx - 2] = " " -- world[cy][cx - 2] = " "
cx = cx - path_length cx = cx - path_length
elseif curdir == "r" then elseif curdir == "r" then
for iy = cy,cy+path_width-1 do for iy = cy,cy+(path_width-1) do
for ix = cx,cx+path_length-1 do for ix = cx,cx+path_length+(path_width-1) do
world[iy][ix] = " " world[iy][ix] = " "
end end
end end
@ -123,6 +124,8 @@ function M.generate_maze(seed, width, height, path_length, path_width)
-- world[cy][cx + 2] = " " -- world[cy][cx + 2] = " "
cx = cx + path_length cx = cx + path_length
end end
-- print("Now at ("..cx..", "..cy..")")
table.insert(nodes, { x = cx, y = cy }) table.insert(nodes, { x = cx, y = cy })
else else
@ -140,8 +143,9 @@ function M.generate_maze(seed, width, height, path_length, path_width)
--print("Maze generation complete, no teleportation necessary.") --print("Maze generation complete, no teleportation necessary.")
end end
end end
M.printspace(world, width, height)
io.read("*l") -- M.printspace(world, width, height)
-- io.read("*l")
end end
end_time = os.clock() end_time = os.clock()