From 0554e37fcc918b943308a75cf4c69d974e71a560 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 3 May 2020 14:23:25 +0100 Subject: [PATCH] maze2d: allow varying the path width & length --- main.lua | 33 +++++++++++++++++++++++---------- maze.lua | 30 +++++++++++++++++------------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/main.lua b/main.lua index 8ad043f..c089926 100644 --- a/main.lua +++ b/main.lua @@ -16,32 +16,41 @@ local settings = { filename = "maze.scad", scale = 5, - inverted = true + inverted = true, + + path_length = 2, + path_width = 1 } for i=1,#arg do if not strings.starts_with(arg[i], "-") then - table.insert(settings.extras, arg[i]) - elseif string.match(arg[i], "--width") or string.match(arg[i], "-w") then + table.insert(settings.extras, arg[i]) + elseif arg[i] == "--width" or arg[i] == "-w" then i = i + 1 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 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 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 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 settings.filename = arg[i] - elseif string.match(arg[i], "--scale") then + elseif arg[i] == "--scale" then i = i + 1 settings.scale = tonumber(arg[i]) - elseif string.match(arg[i], "--no-invert") then + elseif arg[i] == "--no-invert" then 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 @@ -71,6 +80,8 @@ Options: -f --filename {string} Sets the output filename --no-invert Don't invert 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 @@ -78,7 +89,9 @@ local function maze_text() local world, time = maze2d.generate_maze( settings.seed, settings.width, - settings.height + settings.height, + settings.path_length, + settings.path_width ) maze2d.printspace(world, settings.width, settings.height) print("Generation completed in " .. time .. "ms.") diff --git a/maze.lua b/maze.lua index 64400a1..4d726a7 100644 --- a/maze.lua +++ b/maze.lua @@ -35,13 +35,10 @@ end 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 @@ -65,29 +62,33 @@ function M.generate_maze(seed, width, height, path_length, path_width) while #nodes > 0 do -- io.write("Nodes left: " .. curnode .. "\r") --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 if cy - path_length > 0 and world[cy - path_length][cx] == "#" then directions = directions .. "u" + -- print("up | cy: "..cy..", target: "..cy-path_length..", value: '"..world[cy - path_length][cx].."', path_length: "..path_length) end if cy + path_length < height and world[cy + path_length][cx] == "#" then directions = directions .. "d" + -- print("down | cy: "..cy..", target: "..cy+path_length..", value: '"..world[cy + path_length][cx].."', path_length: "..path_length) end if cx - path_length > 0 and world[cy][cx - path_length] == "#" then directions = directions .. "l" + -- print("left | cx: "..cx..", target: "..cx-path_length..", value: '"..world[cy][cx - path_length].."', path_length: "..path_length) end if cx + path_length < width and world[cy][cx + path_length] == "#" then directions = directions .. "r" + -- print("right | cx: "..cx..", target: "..cx+path_length..", value: '"..world[cy][cx + path_length].."', path_length: "..path_length) 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 - for ix = cx,cx+path_width-1 do + for ix = cx,cx+(path_width-1) do for iy = cy-path_length,cy do world[iy][ix] = " " end @@ -97,7 +98,7 @@ function M.generate_maze(seed, width, height, path_length, path_width) cy = cy - path_length elseif curdir == "d" then 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] = " " end end @@ -114,8 +115,8 @@ function M.generate_maze(seed, width, height, path_length, path_width) -- world[cy][cx - 2] = " " cx = cx - path_length elseif curdir == "r" then - for iy = cy,cy+path_width-1 do - for ix = cx,cx+path_length-1 do + for iy = cy,cy+(path_width-1) do + for ix = cx,cx+path_length+(path_width-1) do world[iy][ix] = " " end end @@ -123,6 +124,8 @@ function M.generate_maze(seed, width, height, path_length, path_width) -- world[cy][cx + 2] = " " cx = cx + path_length end + + -- print("Now at ("..cx..", "..cy..")") table.insert(nodes, { x = cx, y = cy }) else @@ -140,8 +143,9 @@ function M.generate_maze(seed, width, height, path_length, path_width) --print("Maze generation complete, no teleportation necessary.") end end - M.printspace(world, width, height) - io.read("*l") + + -- M.printspace(world, width, height) + -- io.read("*l") end end_time = os.clock()