multimaze/main.lua

135 lines
3.2 KiB
Lua

local strings = require("./utils/strings")
local debug = require("./utils/print_r")
local maze2d = require("./maze")
local maze3d = require("./maze3d")
local openscad = require("./openscad")
local settings = {
mode = "help",
extras = {},
width = 11,
height = 11,
depth = 7,
seed = os.time(),
filename = "maze.scad",
scale = 5,
inverted = true
}
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
i = i + 1
settings.width = tonumber(arg[i])
elseif string.match(arg[i], "--height") or string.match(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
i = i + 1
settings.depth = tonumber(arg[i])
elseif string.match(arg[i], "--seed") or string.match(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
i = i + 1
settings.filename = arg[i]
elseif string.match(arg[i], "--scale") then
i = i + 1
settings.scale = tonumber(arg[i])
elseif string.match(arg[i], "--no-invert") then
settings.inverted = false
end
end
if #settings.extras > 0 then
settings.mode = table.remove(settings.extras, 1)
end
local function help()
print([[
multimaze: Multidimensional maze generator
By Starbeamrainbowlabs
Usage:
lua main.lua {mode} [{options}]
Modes:
help Displays this message
maze Generates a 2d maze
maze3d Generates a 3d maze
maze3d_openscad Generates a 3d maze as an openscad file
Options:
-w --width {int} Sets the width of the maze
-h --height {int} Sets the height of the maze
-d --depth {int} Sets the depth of the maze [3d only]
-s --seed {string} Sets the seed
-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]
]])
end
local function maze_text()
local world, time = maze2d.generate_maze(
settings.seed,
settings.width,
settings.height
)
maze2d.printspace(world, settings.width, settings.height)
print("Generation completed in " .. time .. "ms.")
end
local function maze3d_text()
local world, time = maze3d.generate_maze(
settings.seed,
settings.width,
settings.height,
settings.depth
)
maze3d.printspace(world, settings.width, settings.height, settings.depth)
print("Generation completed in " .. time .. "ms.")
end
local function maze3d_openscad()
local world, time = maze3d.generate_maze(
setting.seed,
settings.width,
settings.height,
settings.depth
)
local openscad_settings = {
scale = settings.scale,
inverted = settings.inverted
}
openscad.write3d(
world,
settings.width, settings.height, settings.depth,
settings.filename,
openscad_settings
)
print("Maze written to "..settings.filename)
end
local mode_functions = {
["help"] = help,
["maze"] = maze_text,
["maze3d"] = maze3d_text,
["maze3d_openscad"] = maze3d_openscad
}
local selection = mode_functions[settings.mode]
if not selection then
print("Error: Unrecognised mode '"..settings.mode.."'")
selection = mode_functions["help"]
end
selection()