Compare commits
3 Commits
26ac4a72c8
...
4048f03a8d
Author | SHA1 | Date |
---|---|---|
Starbeamrainbowlabs | 4048f03a8d | |
Starbeamrainbowlabs | c3064a8469 | |
Starbeamrainbowlabs | 5e0d97fac6 |
72
README.md
72
README.md
|
@ -2,14 +2,82 @@
|
|||
|
||||
> A collection of maze generation programs
|
||||
|
||||
## `main.lua`
|
||||
Ported back in 2015 (and sine adapted) from an algorithm of my own design that originally implemented in Python.
|
||||
Ported back in 2015 (and since adapted) from an algorithm of my own design that was originally implemented in Python.
|
||||
|
||||
Now has a 3d mode!
|
||||
|
||||
Current blog posts about this project:
|
||||
|
||||
- [Language Review: Lua](https://starbeamrainbowlabs.com/blog/article.php?article=posts/070-Language-Review-Lua.html) (2D maze generator)
|
||||
- [3D mazes with Lua, OpenSCAD, and Blender](#coming-soon) (coming soon!)
|
||||
|
||||
## Installation
|
||||
|
||||
### System Requirements
|
||||
- **Required**
|
||||
- [Lua](https://lua.org/)
|
||||
- **Optional**
|
||||
- [OpenSCAD](http://www.openscad.org/): for creating STL files and viewing generated .scad files
|
||||
- [Blender](https://www.blender.org/): a great tool for importing generated STL files and having some fun rendering them
|
||||
|
||||
### Setup
|
||||
Clone this repository:
|
||||
|
||||
```bash
|
||||
git clone https://git.starbeamrainbowlabs.com/sbrl/multimaze.git
|
||||
cd multimaze.lua
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
See the usage information for the program by calling `main.lua` like so:
|
||||
|
||||
```
|
||||
lua main.lua help
|
||||
```
|
||||
|
||||
Operating modes:
|
||||
|
||||
Mode | Description
|
||||
--------------------|----------------------
|
||||
`help` | Displays the help text
|
||||
`maze` | Generates 2D mazes and writes them to stdout
|
||||
`maze3d` | Generates 3D mazes and writes them in layers to stdout
|
||||
`maze3d_openscad` | Generates 3D mazes and writes an OpenSCAD file to the specified location
|
||||
|
||||
To render a 2D maze as ASCII, do this:
|
||||
|
||||
```bash
|
||||
lua main.lua maze --width 25 --height 25
|
||||
# Alternatively, in shorthand:
|
||||
lua main.lua maze -w 25 -h 25
|
||||
```
|
||||
|
||||
To render a 3D maze as a .STL file, do this:
|
||||
|
||||
```bash
|
||||
# Generate the OpenSCAD file
|
||||
lua main.lua maze3d_openscad -w 91 -h 91 -d 71 -f 91x91x71.scad
|
||||
# Convert the OpenSCAD file to an STL
|
||||
openscad -o 91x91x71.stl 91x91x71.scad
|
||||
```
|
||||
|
||||
|
||||
## Contributing
|
||||
Contributions are very welcome - both issues and patches! Please mention in your pull request that you release your work under the MPL-2.0 (see below).
|
||||
|
||||
This guide looks good for how to create patch in git: [How To Create and Apply Git Patch Files](https://devconnected.com/how-to-create-and-apply-git-patch-files/)
|
||||
|
||||
If you find a bug, there are a number of different ways you can get in touch.
|
||||
|
||||
1. By commenting on any of the blog posts (see links above)
|
||||
2. By getting in touch through any of the methods mentioned on [my main website](https://starbeamrainbowlabs.com/)
|
||||
3. If you know me IRL, that works too
|
||||
|
||||
If this becomes a thing that people want to contribute to regularly, I will move it to GitHub to make that an easier process.
|
||||
|
||||
If you're feeling that way inclined, I have a [Liberapay profile](https://liberapay.com/sbrl) if you'd like to donate to say an extra thank you :-)
|
||||
|
||||
|
||||
## License
|
||||
The contents of this repository is released under the Mozilla Public License 2.0. The full license text is included in the `LICENSE` file in this repository. Tldr legal have a [great summary](https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)) of the license if you're interested.
|
||||
|
|
Binary file not shown.
2
main.lua
2
main.lua
|
@ -121,7 +121,7 @@ end
|
|||
|
||||
local function maze3d_openscad()
|
||||
local world, time = maze3d.generate_maze(
|
||||
setting.seed,
|
||||
settings.seed,
|
||||
settings.width,
|
||||
settings.height,
|
||||
settings.depth,
|
||||
|
|
30
maze3d.lua
30
maze3d.lua
|
@ -44,13 +44,12 @@ end
|
|||
-- Initialise the world
|
||||
|
||||
function M.generate_maze(seed, width, height, depth, path_length, path_width, path_depth)
|
||||
start_time = os.clock()
|
||||
|
||||
local start_time = os.clock()
|
||||
if not path_length then path_length = 2 end
|
||||
if not path_width then path_width = 1 end
|
||||
if not path_depth then path_depth = 1 end
|
||||
|
||||
print("Generating maze "..width.."x"..height.."x"..depth)
|
||||
-- print("Generating maze "..width.."x"..height.."x"..depth.." | path: length "..path_length.." width "..path_width.." depth "..path_depth)
|
||||
math.randomseed(seed) -- seed the random number generator with the system clock
|
||||
|
||||
local world = {}
|
||||
|
@ -73,48 +72,38 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
|||
while #nodes > 0 do
|
||||
-- io.write("Nodes left: " .. curnode .. "\r")
|
||||
--print("Nodes left: " .. #nodes)
|
||||
-- print("Currently at ("..cx..", "..cy..", "..cz.."), up: "..(cy-path_length)..", down: "..cy+path_length..", left: "..cx-path_length..", right: "..cx+path_length..", d++: "..cz+path_length..", d--: "..cz-path_length..", width: "..width..", height: "..height..", depth: "..depth)
|
||||
|
||||
local directions = "" -- the different directions we can move in
|
||||
if cz - path_length > 0 and world[cz - path_length][cy][cx] == "#" then
|
||||
directions = directions .. "-"
|
||||
directions = directions .. "u"
|
||||
-- print("d-- | cz: "..cz..", target: "..cz-path_length..", value: '"..world[cz - path_length][cy][cx].."', path_length: "..path_length)
|
||||
end
|
||||
if cz + path_length < depth-path_depth and world[cz + path_length][cy][cx] == "#" then
|
||||
directions = directions .. "+"
|
||||
-- print("d++ | cz: "..cz..", target: "..cz+path_length..", value: '"..world[cz + path_length][cy][cx].."', path_length: "..path_length)
|
||||
end
|
||||
if cy - path_length > 0 and world[cz][cy - path_length][cx] == "#" then
|
||||
directions = directions .. "u"
|
||||
-- print("up | cy: "..cy..", target: "..cy-path_length..", value: '"..world[cz][cy - path_length][cx].."', path_length: "..path_length)
|
||||
end
|
||||
if cy + path_length < height-path_width and world[cz][cy + path_length][cx] == "#" then
|
||||
directions = directions .. "d"
|
||||
-- print("down | cy: "..cy..", target: "..cy+path_length..", value: '"..world[cz][cy + path_length][cx].."', path_length: "..path_length)
|
||||
end
|
||||
if cx - path_length > 0 and world[cz][cy][cx - path_length] == "#" then
|
||||
directions = directions .. "l"
|
||||
-- print("left | cx: "..cx..", target: "..cx-path_length..", value: '"..world[cz][cy][cx - path_length].."', path_length: "..path_length)
|
||||
end
|
||||
if cx + path_length < width-path_width and world[cz][cy][cx + path_length] == "#" then
|
||||
directions = directions .. "r"
|
||||
-- print("right | cx: "..cx..", target: "..cx+path_length..", value: '"..world[cz][cy][cx + path_length].."', path_length: "..path_length)
|
||||
end
|
||||
|
||||
-- If this is 1 or less, then we will switch our attention to another candidate node after moving
|
||||
local shift_attention = math.random(0, 3)
|
||||
|
||||
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
|
||||
local curdirnum = math.random(1, #directions)
|
||||
local curdir = string.sub(directions, curdirnum, curdirnum)
|
||||
|
||||
print("chose '"..curdir.."'")
|
||||
|
||||
if curdir == "+" then
|
||||
|
||||
for iz = cz,cz+path_length+(path_depth-1) do
|
||||
for ix = cx,cx+(path_width-1) do
|
||||
for iy = cy,cy+(path_width-1) do
|
||||
|
@ -152,6 +141,7 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
|||
for ix = cx,cx+(path_width-1) do
|
||||
for iy = cy,cy+path_length+(path_width-1) do
|
||||
world[iz][iy][ix] = " "
|
||||
-- print("[tunnel/d] ("..ix..", "..iy..", "..iz..")")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -182,9 +172,7 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
|||
cx = cx + path_length
|
||||
end
|
||||
|
||||
if #directions > 1 then
|
||||
table.insert(nodes, { x = cx, y = cy, z = cz })
|
||||
end
|
||||
table.insert(nodes, { x = cx, y = cy, z = cz })
|
||||
else
|
||||
table.remove(nodes, curnode)
|
||||
end
|
||||
|
@ -198,14 +186,12 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
|||
cz = nodes[curnode]["z"]
|
||||
end
|
||||
end
|
||||
|
||||
M.printspace(world, width, height, depth)
|
||||
io.read("*l")
|
||||
end
|
||||
|
||||
end_time = os.clock()
|
||||
|
||||
-- WARNING: os.clock() isn't accurate, because it gives the total **CPU time**, not **real time**!
|
||||
local end_time = os.clock()
|
||||
return world, (end_time - start_time) * 1000
|
||||
return world
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Reference in New Issue