Add svg export for 2d mazes
This commit is contained in:
parent
816f871fa3
commit
7c86534a13
4 changed files with 57 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
*.png
|
*.png
|
||||||
|
*.svg
|
||||||
*.scad
|
*.scad
|
||||||
*.blend1
|
*.blend1
|
||||||
*.stl
|
*.stl
|
||||||
|
|
18
main.lua
18
main.lua
|
@ -2,6 +2,7 @@ local a = require("./utils/ansi")
|
||||||
local strings = require("./utils/strings")
|
local strings = require("./utils/strings")
|
||||||
local debug = require("./utils/print_r")
|
local debug = require("./utils/print_r")
|
||||||
local calculations = require("./utils/calculations")
|
local calculations = require("./utils/calculations")
|
||||||
|
|
||||||
local maze2d = require("./maze")
|
local maze2d = require("./maze")
|
||||||
local maze3d = require("./maze3d")
|
local maze3d = require("./maze3d")
|
||||||
local openscad = require("./openscad")
|
local openscad = require("./openscad")
|
||||||
|
@ -26,6 +27,8 @@ local settings = {
|
||||||
scale = 5,
|
scale = 5,
|
||||||
inverted = true,
|
inverted = true,
|
||||||
|
|
||||||
|
colour = "#6495ED", -- Cornflower blue :D
|
||||||
|
|
||||||
path_length = 2,
|
path_length = 2,
|
||||||
path_width = 1,
|
path_width = 1,
|
||||||
path_depth = 1
|
path_depth = 1
|
||||||
|
@ -47,6 +50,7 @@ local function help()
|
||||||
|
|
||||||
]]..a.item("help")..[[ Displays this message
|
]]..a.item("help")..[[ Displays this message
|
||||||
]]..a.item("maze")..[[ Generates a 2d maze
|
]]..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")..[[ Generates a 3d maze
|
||||||
]]..a.item("maze3d_openscad")..[[ Generates a 3d maze as an openscad file
|
]]..a.item("maze3d_openscad")..[[ Generates a 3d maze as an openscad file
|
||||||
|
|
||||||
|
@ -59,7 +63,7 @@ local function help()
|
||||||
]]..a.item("-s --seed")..a.value(" {string}")..[[ Sets the seed
|
]]..a.item("-s --seed")..a.value(" {string}")..[[ Sets the seed
|
||||||
]]..a.item("-f --filename")..a.value(" {string}")..[[ Sets the output filename
|
]]..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("--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("-m --scale")..a.value(" {int}")..[[ Sets the scale of the output [maze_svg, openscad output only]
|
||||||
]]..a.item("-l --path-length")..a.value(" {int}")..[[ Sets the path length
|
]]..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("-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.item("-z --path-depth")..a.value(" {int}")..[[ Sets the path depth [3d only]
|
||||||
|
@ -129,6 +133,17 @@ local function maze_text()
|
||||||
print("Generation completed in " .. time .. "ms.")
|
print("Generation completed in " .. time .. "ms.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function maze_svg()
|
||||||
|
local world, time = maze2d.generate_maze(
|
||||||
|
settings.seed,
|
||||||
|
settings.width,
|
||||||
|
settings.height,
|
||||||
|
settings.path_length,
|
||||||
|
settings.path_width
|
||||||
|
)
|
||||||
|
print(maze2d.make_svg(world, settings.width, settings.height, settings.scale, settings.colour))
|
||||||
|
end
|
||||||
|
|
||||||
local function maze3d_text()
|
local function maze3d_text()
|
||||||
local world, time = maze3d.generate_maze(
|
local world, time = maze3d.generate_maze(
|
||||||
settings.seed,
|
settings.seed,
|
||||||
|
@ -173,6 +188,7 @@ end
|
||||||
local mode_functions = {
|
local mode_functions = {
|
||||||
["help"] = help,
|
["help"] = help,
|
||||||
["maze"] = maze_text,
|
["maze"] = maze_text,
|
||||||
|
["maze_svg"] = maze_svg,
|
||||||
["maze3d"] = maze3d_text,
|
["maze3d"] = maze3d_text,
|
||||||
["maze3d_openscad"] = maze3d_openscad
|
["maze3d_openscad"] = maze3d_openscad
|
||||||
}
|
}
|
||||||
|
|
19
maze.lua
19
maze.lua
|
@ -1,3 +1,5 @@
|
||||||
|
local svg = require("../utils/svg_writer")
|
||||||
|
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
-- Maze generation script
|
-- Maze generation script
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
@ -31,6 +33,23 @@ function M.printspace(space, w, h)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.make_svg(space, width, height, scale, fill)
|
||||||
|
local result = { svg.start(width * scale, height * scale) }
|
||||||
|
for y = 0, height - 1, 1 do
|
||||||
|
for x = 0, width - 1, 1 do
|
||||||
|
if space[y][x] == "#" then
|
||||||
|
table.insert(result, svg.rect(
|
||||||
|
x * scale, y * scale,
|
||||||
|
scale, scale,
|
||||||
|
fill
|
||||||
|
))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(result, svg.finish())
|
||||||
|
return table.concat(result, "")
|
||||||
|
end
|
||||||
|
|
||||||
-- Initialise the world
|
-- Initialise the world
|
||||||
|
|
||||||
function M.generate_maze(seed, width, height, path_length, path_width)
|
function M.generate_maze(seed, width, height, path_length, path_width)
|
||||||
|
|
20
utils/svg_writer.lua
Normal file
20
utils/svg_writer.lua
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.start(width, height)
|
||||||
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
|
||||||
|
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\
|
||||||
|
<!-- Generated by svg_writer which was written by Starbeamrainbowlabs -->\
|
||||||
|
<!-- svg_writer is a port of SvgWriter, which was also implemented by Starbeamrainbowlabs in C# -->\
|
||||||
|
<svg version=\"1.1\" x=\"0\" y=\"0\" width=\""..width.."\" height=\""..height.."\" xmlns=\"http://www.w3.org/2000/svg\">"
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.finish()
|
||||||
|
return "</svg>"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.rect(x, y, width, height, fill)
|
||||||
|
return "<rect x=\""..x.."\" y=\""..y.."\" width=\""..width.."\" height=\""..height.."\" fill=\""..fill.."\" />"
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
Loading…
Reference in a new issue