multimaze/main.lua

188 lines
4.9 KiB
Lua
Raw Normal View History

local a = require("./utils/ansi")
local strings = require("./utils/strings")
local debug = require("./utils/print_r")
local calculations = require("./utils/calculations")
local maze2d = require("./maze")
local maze3d = require("./maze3d")
local openscad = require("./openscad")
-- 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
local settings = {
mode = "help",
extras = {},
width = 11,
height = 11,
depth = 7,
2020-05-03 01:00:19 +00:00
seed = os.time(),
2020-04-30 18:10:32 +00:00
filename = "maze.scad",
scale = 5,
inverted = true,
path_length = 2,
path_width = 1,
path_depth = 1
}
2020-09-06 01:09:47 +00:00
local function help()
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:")..[[
]]..a.item("help")..[[ Displays this message
]]..a.item("maze")..[[ Generates a 2d maze
]]..a.item("maze3d")..[[ Generates a 3d maze
]]..a.item("maze3d_openscad")..[[ Generates a 3d maze as an openscad file
]]..a.header("Options:")..[[
2020-09-06 01:10:14 +00:00
]]..a.item("--help")..[[ Shows this message
2020-09-06 01:09:47 +00:00
]]..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 [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("--no-invert")..[[ Don't invert the output [openscad output only]
]]..a.item("-m --scale")..a.value(" {int}")..[[ Sets the scale of the output [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 [3d only]
]]..a.header("Environment Variables:")..[[
]]..a.item("NO_COLOR")..[[ Disable ANSI escape sequences (aka coloured output)
]])
end
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()
elseif arg[i] == "--width" or arg[i] == "-w" then
i = i + 1
settings.width = tonumber(arg[i])
elseif arg[i] == "--height" or arg[i] == "-h" then
i = i + 1
settings.height = tonumber(arg[i])
elseif arg[i] == "--depth" or arg[i] == "-d" then
i = i + 1
settings.depth = tonumber(arg[i])
elseif arg[i] == "--seed" or arg[i] == "-s" then
i = i + 1
settings.seed = tonumber(arg[i])
elseif arg[i] == "--filename" or arg[i] == "-f" then
i = i + 1
settings.filename = 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])
elseif arg[i] == "--no-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
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
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
i = i + 1
settings.path_depth = tonumber(arg[i])
end
end
if #settings.extras > 0 then
settings.mode = table.remove(settings.extras, 1)
end
2020-09-06 01:09:47 +00:00
-------------------------------------------------------------------------------
local function maze_text()
local world, time = maze2d.generate_maze(
2020-05-03 01:00:19 +00:00
settings.seed,
settings.width,
settings.height,
settings.path_length,
settings.path_width
)
maze2d.printspace(world, settings.width, settings.height)
print("Generation completed in " .. time .. "ms.")
end
local function maze3d_text()
local world, time = maze3d.generate_maze(
2020-05-03 01:00:19 +00:00
settings.seed,
settings.width,
settings.height,
settings.depth,
settings.path_length,
settings.path_width,
settings.path_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(
2020-08-27 21:37:09 +00:00
settings.seed,
settings.width,
settings.height,
settings.depth,
settings.path_length,
settings.path_width,
settings.path_depth
)
2020-04-30 18:10:32 +00:00
local openscad_settings = {
scale = settings.scale,
inverted = settings.inverted
}
openscad.write3d(
world,
settings.width, settings.height, settings.depth,
2020-04-30 18:10:32 +00:00
settings.filename,
openscad_settings
)
print("Maze written to "..a.fgreen(a.hicol(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(a.fred(a.hicol("Error: Unrecognised mode '"..a.FBLE..settings.mode..a.FRED.."'")))
print()
selection = mode_functions["help"]
end
selection()