2020-09-06 00:47:35 +00:00
|
|
|
local a = require("./utils/ansi")
|
2020-04-29 18:57:18 +00:00
|
|
|
local strings = require("./utils/strings")
|
|
|
|
local debug = require("./utils/print_r")
|
2020-05-03 13:49:20 +00:00
|
|
|
local calculations = require("./utils/calculations")
|
2020-09-27 00:37:10 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
local maze2d = require("./maze")
|
|
|
|
local maze3d = require("./maze3d")
|
|
|
|
local openscad = require("./openscad")
|
|
|
|
|
2020-09-06 00:47:35 +00:00
|
|
|
-- ANSI escape sequence helpers
|
|
|
|
function a.header(str) return a.fblue(a.hicol(str)) end
|
|
|
|
function a.item(str) return a.fyellow(str) end
|
|
|
|
function a.value(str) return a.fgreen(str) end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
local settings = {
|
|
|
|
mode = "help",
|
|
|
|
extras = {},
|
|
|
|
|
|
|
|
width = 11,
|
|
|
|
height = 11,
|
|
|
|
depth = 7,
|
|
|
|
|
2020-09-30 00:20:17 +00:00
|
|
|
branching_factor = nil, -- Depends on the mode - nil will cause the engine to pick the default mode instead
|
|
|
|
|
2020-05-03 01:00:19 +00:00
|
|
|
seed = os.time(),
|
2020-09-27 01:15:05 +00:00
|
|
|
-- TODO: Add branching factor setting here & associated CLI argument
|
2020-09-27 23:53:52 +00:00
|
|
|
filename = nil,
|
2020-04-30 18:10:32 +00:00
|
|
|
|
|
|
|
scale = 5,
|
2020-05-03 13:23:25 +00:00
|
|
|
inverted = true,
|
|
|
|
|
2020-09-30 00:20:17 +00:00
|
|
|
colour = "#6495ED", -- Cornflower blue :D (if you get the reference, get in touch for a virtual cookie :D)
|
2020-09-27 00:37:10 +00:00
|
|
|
|
2020-05-03 13:23:25 +00:00
|
|
|
path_length = 2,
|
2020-05-03 15:48:50 +00:00
|
|
|
path_width = 1,
|
|
|
|
path_depth = 1
|
2020-04-29 18:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 01:09:47 +00:00
|
|
|
local function help()
|
2020-09-30 00:20:17 +00:00
|
|
|
--lua main.lua ]]..a.item("maze")..[[ [--width <int:11>] [--height <int:11>] [--seed <string>] [--path-length <int:2>] [--path-width <int:1>] [--branching-factor <int:6>]
|
|
|
|
--lua main.lua ]]..a.item("maze3d")..[[ [--width <int:11>] [--height <int:11>] [--depth <int:7>] [--seed <string>] [--path-length <int:2>] [--path-width <int:1>] [--path-depth <int:1>] [--branching-factor <int:6>]
|
2020-09-06 01:09:47 +00:00
|
|
|
print([[
|
|
|
|
]]..a.fgreen(a.hicol("multimaze: "))..a.fyellow(a.hicol("Multidimensional maze generator"))..[[
|
|
|
|
|
|
|
|
]]..a.locol("By Starbeamrainbowlabs")..[[
|
|
|
|
|
|
|
|
|
|
|
|
]]..a.header("Usage:")..[[
|
|
|
|
|
|
|
|
lua main.lua ]]..a.item("{mode} [{options}]")..[[
|
|
|
|
|
|
|
|
|
|
|
|
]]..a.header("Modes:")..[[
|
|
|
|
|
2020-09-30 00:20:17 +00:00
|
|
|
]]..a.item("help")..[[ Displays this message
|
|
|
|
]]..a.item("maze")..[[ Generates a 2d maze
|
|
|
|
]]..a.item("maze_svg")..[[ Generates a 2d maze as an SVG file
|
|
|
|
]]..a.item("maze3d")..[[ Generates a 3d maze
|
|
|
|
]]..a.item("maze3d_openscad")..[[ Generates a 3d maze as an openscad file
|
2020-09-06 01:09:47 +00:00
|
|
|
|
|
|
|
]]..a.header("Options:")..[[
|
|
|
|
|
2020-09-30 00:20:17 +00:00
|
|
|
]]..a.item("--help")..[[ Shows this message
|
|
|
|
]]..a.item("-w --width")..a.value(" {int}")..[[ Sets the width of the maze
|
|
|
|
]]..a.item("-h --height")..a.value(" {int}")..[[ Sets the height of the maze
|
|
|
|
]]..a.item("-d --depth")..a.value(" {int}")..[[ Sets the depth of the maze ]]..a.locol("[3d only]")..[[
|
|
|
|
|
|
|
|
]]..a.item("-s --seed")..a.value(" {string}")..[[ Sets the seed
|
|
|
|
]]..a.item("-f --filename")..a.value(" {string}")..[[ Sets the output filename
|
|
|
|
]]..a.item("--toggle-invert")..[[ Toggle inversion of the output ]]..a.locol("[maze_svg, maze3d_openscad only]")..[[
|
|
|
|
|
|
|
|
]]..a.item("-m --scale")..a.value(" {int}")..[[ Sets the scale of the output ]]..a.locol("[maze_svg, openscad output only]")..[[
|
|
|
|
|
|
|
|
]]..a.item("-l --path-length")..a.value(" {int}")..[[ Sets the path length
|
|
|
|
]]..a.item("-y --path-width")..a.value(" {int}")..[[ Sets the path width
|
|
|
|
]]..a.item("-z --path-depth")..a.value(" {int}")..[[ Sets the path depth ]]..a.locol("[3d only]")..[[
|
|
|
|
|
|
|
|
]]..a.item("-b --branching-factor")..a.value(" {int}")..[[Sets the path branching factor (higher = fewer branching paths, default: 6 for 2D, 3 for 3D)
|
|
|
|
]]..a.item("--colour")..a.value(" {string}")..[[ Sets the colour ]]..a.locol("[maze_svg only]")..[[
|
|
|
|
|
2020-09-06 01:09:47 +00:00
|
|
|
|
|
|
|
]]..a.header("Environment Variables:")..[[
|
|
|
|
|
|
|
|
]]..a.item("NO_COLOR")..[[ Disable ANSI escape sequences (aka coloured output)
|
|
|
|
|
|
|
|
]])
|
|
|
|
end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
for i=1,#arg do
|
|
|
|
if not strings.starts_with(arg[i], "-") then
|
2020-09-06 01:09:47 +00:00
|
|
|
table.insert(settings.extras, arg[i])
|
|
|
|
elseif arg[i] == "--help" then
|
|
|
|
help()
|
|
|
|
os.exit()
|
2020-05-03 13:23:25 +00:00
|
|
|
elseif arg[i] == "--width" or arg[i] == "-w" then
|
2020-04-29 18:57:18 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.width = tonumber(arg[i])
|
2020-05-03 13:23:25 +00:00
|
|
|
elseif arg[i] == "--height" or arg[i] == "-h" then
|
2020-04-29 18:57:18 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.height = tonumber(arg[i])
|
2020-05-03 13:23:25 +00:00
|
|
|
elseif arg[i] == "--depth" or arg[i] == "-d" then
|
2020-04-29 18:57:18 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.depth = tonumber(arg[i])
|
2020-09-30 00:20:17 +00:00
|
|
|
elseif arg[i] == "--branching-factor" or arg[i] == "-b" then
|
|
|
|
i = i + 1
|
|
|
|
settings.branching_factor = tonumber(arg[i])
|
2020-05-03 13:23:25 +00:00
|
|
|
elseif arg[i] == "--seed" or arg[i] == "-s" then
|
2020-04-29 18:57:18 +00:00
|
|
|
i = i + 1
|
2020-09-28 00:25:47 +00:00
|
|
|
settings.seed = strings.makeseed(arg[i])
|
2020-05-03 13:23:25 +00:00
|
|
|
elseif arg[i] == "--filename" or arg[i] == "-f" then
|
2020-04-29 18:57:18 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.filename = arg[i]
|
2020-09-27 01:15:05 +00:00
|
|
|
elseif arg[i] == "--colour" then
|
|
|
|
i = i + 1
|
|
|
|
settings.colour = arg[i]
|
2020-09-06 01:09:47 +00:00
|
|
|
elseif arg[i] == "--scale" or arg[i] == "-m" then
|
2020-04-30 18:10:32 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.scale = tonumber(arg[i])
|
2020-09-27 23:53:52 +00:00
|
|
|
elseif arg[i] == "--toggle-invert" then
|
2020-04-30 18:10:32 +00:00
|
|
|
settings.inverted = false
|
2020-09-06 01:09:47 +00:00
|
|
|
elseif arg[i] == "--path-length" or arg[i] == "-l" then
|
2020-05-03 13:23:25 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.path_length = tonumber(arg[i])
|
2020-09-06 01:09:47 +00:00
|
|
|
elseif arg[i] == "--path-width" or arg[i] == "-y" then
|
2020-05-03 13:23:25 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.path_width = tonumber(arg[i])
|
2020-09-06 01:09:47 +00:00
|
|
|
elseif arg[i] == "--path-depth" or arg[i] == "-z" then
|
2020-05-03 15:48:50 +00:00
|
|
|
i = i + 1
|
|
|
|
settings.path_depth = tonumber(arg[i])
|
2020-04-29 18:57:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #settings.extras > 0 then
|
|
|
|
settings.mode = table.remove(settings.extras, 1)
|
|
|
|
end
|
|
|
|
|
2020-09-06 00:47:35 +00:00
|
|
|
|
2020-09-06 01:09:47 +00:00
|
|
|
-------------------------------------------------------------------------------
|
2020-09-06 00:47:35 +00:00
|
|
|
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
local function maze_text()
|
|
|
|
local world, time = maze2d.generate_maze(
|
2020-05-03 01:00:19 +00:00
|
|
|
settings.seed,
|
2020-04-29 18:57:18 +00:00
|
|
|
settings.width,
|
2020-05-03 13:23:25 +00:00
|
|
|
settings.height,
|
|
|
|
settings.path_length,
|
2020-09-30 00:20:17 +00:00
|
|
|
settings.path_width,
|
|
|
|
settings.branching_factor
|
2020-04-29 18:57:18 +00:00
|
|
|
)
|
|
|
|
maze2d.printspace(world, settings.width, settings.height)
|
2020-09-28 00:25:47 +00:00
|
|
|
print("Generation completed in " .. time .. "ms with seed "..settings.seed..".")
|
2020-04-29 18:57:18 +00:00
|
|
|
end
|
|
|
|
|
2020-09-27 00:37:10 +00:00
|
|
|
local function maze_svg()
|
|
|
|
local world, time = maze2d.generate_maze(
|
|
|
|
settings.seed,
|
|
|
|
settings.width,
|
|
|
|
settings.height,
|
|
|
|
settings.path_length,
|
2020-09-30 00:20:17 +00:00
|
|
|
settings.path_width,
|
|
|
|
settings.branching_factor
|
2020-09-27 00:37:10 +00:00
|
|
|
)
|
2020-09-27 23:53:52 +00:00
|
|
|
|
|
|
|
local svg_settings = {
|
|
|
|
scale = settings.scale,
|
|
|
|
colour = settings.colour,
|
|
|
|
inverted = settings.inverted
|
|
|
|
}
|
|
|
|
|
|
|
|
local svg = maze2d.make_svg(world, settings.width, settings.height, svg_settings)
|
|
|
|
if not settings.filename then
|
|
|
|
print(svg)
|
|
|
|
else
|
|
|
|
local out = io.open(settings.filename, "w")
|
|
|
|
out:write(svg)
|
|
|
|
out:close()
|
2020-09-28 00:25:47 +00:00
|
|
|
print("Generation completed in "..time.."ms with seed "..settings.seed..".")
|
2020-09-27 23:53:52 +00:00
|
|
|
end
|
2020-09-27 00:37:10 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
local function maze3d_text()
|
|
|
|
local world, time = maze3d.generate_maze(
|
2020-05-03 01:00:19 +00:00
|
|
|
settings.seed,
|
2020-04-29 18:57:18 +00:00
|
|
|
settings.width,
|
|
|
|
settings.height,
|
2020-05-03 15:48:50 +00:00
|
|
|
settings.depth,
|
|
|
|
settings.path_length,
|
|
|
|
settings.path_width,
|
2020-09-30 00:20:17 +00:00
|
|
|
settings.path_depth,
|
|
|
|
settings.branching_factor
|
2020-04-29 18:57:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
maze3d.printspace(world, settings.width, settings.height, settings.depth)
|
|
|
|
|
2020-09-28 00:25:47 +00:00
|
|
|
print("Generation completed in " .. time .. "ms with seed "..settings.seed..".")
|
2020-04-29 18:57:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function maze3d_openscad()
|
|
|
|
local world, time = maze3d.generate_maze(
|
2020-08-27 21:37:09 +00:00
|
|
|
settings.seed,
|
2020-04-29 18:57:18 +00:00
|
|
|
settings.width,
|
|
|
|
settings.height,
|
2020-05-03 15:48:50 +00:00
|
|
|
settings.depth,
|
|
|
|
settings.path_length,
|
|
|
|
settings.path_width,
|
2020-09-30 00:20:17 +00:00
|
|
|
settings.path_depth,
|
|
|
|
settings.branching_factor
|
2020-04-29 18:57:18 +00:00
|
|
|
)
|
|
|
|
|
2020-09-27 23:53:52 +00:00
|
|
|
if not settings.filename then
|
2020-09-28 00:25:47 +00:00
|
|
|
print(a.fred(a.hicol("Error: No output filename specified.")))
|
2020-09-27 23:53:52 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-04-30 18:10:32 +00:00
|
|
|
local openscad_settings = {
|
|
|
|
scale = settings.scale,
|
|
|
|
inverted = settings.inverted
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
openscad.write3d(
|
|
|
|
world,
|
|
|
|
settings.width, settings.height, settings.depth,
|
2020-04-30 18:10:32 +00:00
|
|
|
settings.filename,
|
|
|
|
openscad_settings
|
2020-04-29 18:57:18 +00:00
|
|
|
)
|
2020-09-06 00:47:35 +00:00
|
|
|
print("Maze written to "..a.fgreen(a.hicol(settings.filename)))
|
2020-04-29 18:57:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local mode_functions = {
|
|
|
|
["help"] = help,
|
|
|
|
["maze"] = maze_text,
|
2020-09-27 00:37:10 +00:00
|
|
|
["maze_svg"] = maze_svg,
|
2020-04-29 18:57:18 +00:00
|
|
|
["maze3d"] = maze3d_text,
|
|
|
|
["maze3d_openscad"] = maze3d_openscad
|
|
|
|
}
|
|
|
|
|
|
|
|
local selection = mode_functions[settings.mode]
|
|
|
|
if not selection then
|
2020-09-06 00:47:35 +00:00
|
|
|
print(a.fred(a.hicol("Error: Unrecognised mode '"..a.FBLE..settings.mode..a.FRED.."'")))
|
|
|
|
print()
|
2020-04-29 18:57:18 +00:00
|
|
|
selection = mode_functions["help"]
|
|
|
|
end
|
|
|
|
|
|
|
|
selection()
|