Makes sense to commit these to a git repository!
This commit is contained in:
commit
faa2f1a36f
3 changed files with 428 additions and 0 deletions
63
.gitignore
vendored
Normal file
63
.gitignore
vendored
Normal file
|
@ -0,0 +1,63 @@
|
|||
|
||||
# Created by https://www.gitignore.io/api/lua,git
|
||||
# Edit at https://www.gitignore.io/?templates=lua,git
|
||||
|
||||
### Git ###
|
||||
# Created by git for backups. To disable backups in Git:
|
||||
# $ git config --global mergetool.keepBackup false
|
||||
*.orig
|
||||
|
||||
# Created by git when using merge tools for conflicts
|
||||
*.BACKUP.*
|
||||
*.BASE.*
|
||||
*.LOCAL.*
|
||||
*.REMOTE.*
|
||||
*_BACKUP_*.txt
|
||||
*_BASE_*.txt
|
||||
*_LOCAL_*.txt
|
||||
*_REMOTE_*.txt
|
||||
|
||||
### Lua ###
|
||||
# Compiled Lua sources
|
||||
luac.out
|
||||
|
||||
# luarocks build files
|
||||
*.src.rock
|
||||
*.zip
|
||||
*.tar.gz
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.os
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
*.def
|
||||
*.exp
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
|
||||
# End of https://www.gitignore.io/api/lua,git
|
161
maze.lua
Normal file
161
maze.lua
Normal file
|
@ -0,0 +1,161 @@
|
|||
-------------------------------------
|
||||
-- Maze generation script
|
||||
-------------------------------------
|
||||
-- A test by @Starbeamrainbowlabs
|
||||
|
||||
---------------------------------------------
|
||||
-- Intelligent table printing function
|
||||
---------------------------------------------
|
||||
-- From http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/
|
||||
|
||||
function print_r ( t )
|
||||
local print_r_cache={}
|
||||
local function sub_print_r(t,indent)
|
||||
if (print_r_cache[tostring(t)]) then
|
||||
print(indent.."*"..tostring(t))
|
||||
else
|
||||
print_r_cache[tostring(t)]=true
|
||||
if (type(t)=="table") then
|
||||
for pos,val in pairs(t) do
|
||||
if (type(val)=="table") then
|
||||
print(indent.."["..pos.."] => "..tostring(t).." {")
|
||||
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
|
||||
print(indent..string.rep(" ",string.len(pos)+6).."}")
|
||||
elseif (type(val)=="string") then
|
||||
print(indent.."["..pos..'] => "'..val..'"')
|
||||
else
|
||||
print(indent.."["..pos.."] => "..tostring(val))
|
||||
end
|
||||
end
|
||||
else
|
||||
print(indent..tostring(t))
|
||||
end
|
||||
end
|
||||
end
|
||||
if (type(t)=="table") then
|
||||
print(tostring(t).." {")
|
||||
sub_print_r(t," ")
|
||||
print("}")
|
||||
else
|
||||
sub_print_r(t," ")
|
||||
end
|
||||
print()
|
||||
end
|
||||
|
||||
if arg[1] ~= nil then
|
||||
width = tonumber(arg[1])
|
||||
else
|
||||
width = 35
|
||||
end
|
||||
if arg[2] ~= nil then
|
||||
height = tonumber(arg[2])
|
||||
else
|
||||
height = 15
|
||||
end
|
||||
|
||||
----------------------------------
|
||||
-- function to print out the world
|
||||
----------------------------------
|
||||
function printspace(space, w, h)
|
||||
for y = 0, h - 1, 1 do
|
||||
local line = ""
|
||||
for x = 0, w - 1, 1 do
|
||||
line = line .. space[y][x]
|
||||
end
|
||||
print(line)
|
||||
end
|
||||
end
|
||||
|
||||
-- Initialise the world
|
||||
start_time = os.clock()
|
||||
|
||||
function generate_maze(seed, width, height)
|
||||
-- minetest.log("action", "width: "..width..", height: "..height)
|
||||
math.randomseed(seed) -- seed the random number generator with the system clock
|
||||
|
||||
width = width - 1
|
||||
height = height - 1
|
||||
|
||||
local world = {}
|
||||
for y = 0, height, 1 do
|
||||
world[y] = {}
|
||||
for x = 0, width, 1 do
|
||||
world[y][x] = "#"
|
||||
end
|
||||
end
|
||||
|
||||
-- do a random walk to create pathways
|
||||
local nodes = {} -- the nodes left that we haven't investigated
|
||||
local curnode = 1 -- the node we are currently operating on
|
||||
local cx, cy = 1, 1 -- our current position
|
||||
table.insert(nodes, { x = cx, y = cy })
|
||||
world[cy][cx] = " "
|
||||
while #nodes > 0 do
|
||||
-- io.write("Nodes left: " .. curnode .. "\r")
|
||||
--print("Nodes left: " .. #nodes)
|
||||
--print("Currently at (" .. cx .. ", " .. cy .. ")")
|
||||
|
||||
local directions = "" -- the different directions we can move
|
||||
if cy - 2 > 0 and world[cy - 2][cx] == "#" then
|
||||
directions = directions .. "u"
|
||||
end
|
||||
if cy + 2 < height and world[cy + 2][cx] == "#" then
|
||||
directions = directions .. "d"
|
||||
end
|
||||
if cx - 2 > 0 and world[cy][cx - 2] == "#" then
|
||||
directions = directions .. "l"
|
||||
end
|
||||
if cx + 2 < width and world[cy][cx + 2] == "#" then
|
||||
directions = directions .. "r"
|
||||
end
|
||||
--print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
||||
if #directions > 0 then
|
||||
-- we still have somewhere that we can go
|
||||
--print("This node is not a dead end yet.")
|
||||
local curdirnum = math.random(1, #directions)
|
||||
local curdir = string.sub(directions, curdirnum, curdirnum)
|
||||
if curdir == "u" then
|
||||
world[cy - 1][cx] = " "
|
||||
world[cy - 2][cx] = " "
|
||||
cy = cy - 2
|
||||
elseif curdir == "d" then
|
||||
world[cy + 1][cx] = " "
|
||||
world[cy + 2][cx] = " "
|
||||
cy = cy + 2
|
||||
elseif curdir == "l" then
|
||||
world[cy][cx - 1] = " "
|
||||
world[cy][cx - 2] = " "
|
||||
cx = cx - 2
|
||||
elseif curdir == "r" then
|
||||
world[cy][cx + 1] = " "
|
||||
world[cy][cx + 2] = " "
|
||||
cx = cx + 2
|
||||
end
|
||||
|
||||
table.insert(nodes, { x = cx, y = cy })
|
||||
else
|
||||
--print("The node at " .. curnode .. " is a dead end.")
|
||||
table.remove(nodes, curnode)
|
||||
if #nodes > 0 then
|
||||
--print("performing teleport.");
|
||||
curnode = math.random(1, #nodes)
|
||||
--print("New node: " .. curnode)
|
||||
-- print("Nodes table: ")
|
||||
-- print_r(nodes)
|
||||
cx = nodes[curnode]["x"]
|
||||
cy = nodes[curnode]["y"]
|
||||
else
|
||||
--print("Maze generation complete, no teleportation necessary.")
|
||||
end
|
||||
end
|
||||
--printspace(world, width, height)
|
||||
end
|
||||
|
||||
return world
|
||||
end
|
||||
|
||||
local world = generate_maze(os.time(), width, height)
|
||||
|
||||
printspace(world, width, height)
|
||||
end_time = os.clock()
|
||||
print("Generation completed in " .. (end_time - start_time) .. "s.")
|
204
maze3d.lua
Normal file
204
maze3d.lua
Normal file
|
@ -0,0 +1,204 @@
|
|||
-------------------------------------
|
||||
-- Maze generation script
|
||||
-------------------------------------
|
||||
-- A test by @Starbeamrainbowlabs
|
||||
|
||||
---------------------------------------------
|
||||
-- Intelligent table printing function
|
||||
---------------------------------------------
|
||||
-- From http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/
|
||||
|
||||
function print_r ( t )
|
||||
local print_r_cache = {}
|
||||
local function sub_print_r(t, indent)
|
||||
if (print_r_cache[tostring(t)]) then
|
||||
print(indent.."*"..tostring(t))
|
||||
else
|
||||
print_r_cache[tostring(t)] = true
|
||||
if (type(t) == "table") then
|
||||
for pos, val in pairs(t) do
|
||||
if (type(val) == "table") then
|
||||
print(indent.."["..pos.."] => "..tostring(t).." {")
|
||||
sub_print_r(val, indent..string.rep(" ", string.len(pos) + 8))
|
||||
print(indent..string.rep(" ", string.len(pos) + 6).."}")
|
||||
elseif (type(val) == "string") then
|
||||
print(indent.."["..pos..'] => "'..val..'"')
|
||||
else
|
||||
print(indent.."["..pos.."] => "..tostring(val))
|
||||
end
|
||||
end
|
||||
else
|
||||
print(indent..tostring(t))
|
||||
end
|
||||
end
|
||||
end
|
||||
if (type(t) == "table") then
|
||||
print(tostring(t).." {")
|
||||
sub_print_r(t, " ")
|
||||
print("}")
|
||||
else
|
||||
sub_print_r(t, " ")
|
||||
end
|
||||
print()
|
||||
end
|
||||
|
||||
if arg[1] ~= nil then
|
||||
width = tonumber(arg[1])
|
||||
else
|
||||
width = 35
|
||||
end
|
||||
if arg[2] ~= nil then
|
||||
height = tonumber(arg[2])
|
||||
else
|
||||
height = 15
|
||||
end
|
||||
if arg[3] ~= nil then
|
||||
depth = tonumber(arg[3])
|
||||
else
|
||||
depth = 7
|
||||
end
|
||||
if arg[4] ~= nil then
|
||||
seed = tonumber(arg[4])
|
||||
else
|
||||
seed = os.time()
|
||||
end
|
||||
|
||||
----------------------------------
|
||||
-- function to print out the world
|
||||
----------------------------------
|
||||
function printspace(space, w, h, d)
|
||||
for z = 0, d - 1, 1 do
|
||||
for y = 0, h - 1, 1 do
|
||||
local line = ""
|
||||
for x = 0, w - 1, 1 do
|
||||
line = line .. space[z][y][x]
|
||||
end
|
||||
print(line)
|
||||
end
|
||||
print("")
|
||||
end
|
||||
end
|
||||
|
||||
-- Initialise the world
|
||||
start_time = os.clock()
|
||||
|
||||
function generate_maze3d(seed, width, height, depth)
|
||||
print("Generating maze "..width.."x"..height.."x"..depth)
|
||||
math.randomseed(seed) -- seed the random number generator with the system clock
|
||||
|
||||
width = width - 1
|
||||
height = height - 1
|
||||
|
||||
local world = {}
|
||||
for z = 0, depth, 1 do
|
||||
world[z] = {}
|
||||
for y = 0, height, 1 do
|
||||
world[z][y] = {}
|
||||
for x = 0, width, 1 do
|
||||
world[z][y][x] = "#"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- do a random walk to create pathways
|
||||
local nodes = {} -- the nodes left that we haven't investigated
|
||||
local curnode = 1 -- the node we are currently operating on
|
||||
local cx, cy, cz = 1, 1, 1 -- our current position
|
||||
table.insert(nodes, { x = cx, y = cy, z = cz })
|
||||
world[cz][cy][cx] = " "
|
||||
while #nodes > 0 do
|
||||
-- io.write("Nodes left: " .. curnode .. "\r")
|
||||
--print("Nodes left: " .. #nodes)
|
||||
|
||||
local directions = "" -- the different directions we can move in
|
||||
if cz - 2 > 0 and world[cz - 2][cy][cx] == "#" then
|
||||
directions = directions .. "-"
|
||||
-- print("cz: "..cz..", cz - 2: "..(cz-2)..", here: '"..world[cz][cy][cx].."', there: '"..world[cz - 2][cy][cx].."'")
|
||||
end
|
||||
if cz + 2 < depth and world[cz + 2][cy][cx] == "#" then
|
||||
directions = directions .. "+"
|
||||
end
|
||||
if cy - 2 > 0 and world[cz][cy - 2][cx] == "#" then
|
||||
directions = directions .. "u"
|
||||
end
|
||||
if cy + 2 < height and world[cz][cy + 2][cx] == "#" then
|
||||
directions = directions .. "d"
|
||||
end
|
||||
if cx - 2 > 0 and world[cz][cy][cx - 2] == "#" then
|
||||
directions = directions .. "l"
|
||||
end
|
||||
if cx + 2 < width and world[cz][cy][cx + 2] == "#" then
|
||||
directions = directions .. "r"
|
||||
end
|
||||
|
||||
print("Currently at ("..cx..", "..cy..", "..cz..") - directions: "..directions)
|
||||
|
||||
--print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
||||
if #directions > 0 then
|
||||
-- we still have somewhere that we can go
|
||||
--print("This node is not a dead end yet.")
|
||||
local curdirnum = math.random(1, #directions)
|
||||
local curdir = string.sub(directions, curdirnum, curdirnum)
|
||||
|
||||
print("Picked direction '"..curdir.."' (index "..curdirnum..")")
|
||||
|
||||
if curdir == "+" then
|
||||
world[cz + 1][cy][cx] = " "
|
||||
world[cz + 2][cy][cx] = " "
|
||||
cz = cz + 2
|
||||
elseif curdir == "-" then
|
||||
world[cz - 1][cy][cx] = " "
|
||||
world[cz - 2][cy][cx] = " "
|
||||
cz = cz - 2
|
||||
elseif curdir == "u" then
|
||||
world[cz][cy - 1][cx] = " "
|
||||
world[cz][cy - 2][cx] = " "
|
||||
cy = cy - 2
|
||||
elseif curdir == "d" then
|
||||
world[cz][cy + 1][cx] = " "
|
||||
world[cz][cy + 2][cx] = " "
|
||||
cy = cy + 2
|
||||
elseif curdir == "l" then
|
||||
world[cz][cy][cx - 1] = " "
|
||||
world[cz][cy][cx - 2] = " "
|
||||
cx = cx - 2
|
||||
elseif curdir == "r" then
|
||||
world[cz][cy][cx + 1] = " "
|
||||
world[cz][cy][cx + 2] = " "
|
||||
cx = cx + 2
|
||||
end
|
||||
|
||||
|
||||
print("Now at ("..cx..", "..cy..", "..cz..") ")
|
||||
|
||||
table.insert(nodes, { x = cx, y = cy, z = cz })
|
||||
else
|
||||
-- print("The node at " .. curnode .. " is a dead end.")
|
||||
table.remove(nodes, curnode)
|
||||
if #nodes > 0 then
|
||||
--print("performing teleport.");
|
||||
curnode = math.random(1, #nodes)
|
||||
--print("New node: " .. curnode)
|
||||
-- print("Nodes table: ")
|
||||
-- print_r(nodes)
|
||||
cx = nodes[curnode]["x"]
|
||||
cy = nodes[curnode]["y"]
|
||||
cz = nodes[curnode]["z"]
|
||||
else
|
||||
--print("Maze generation complete, no teleportation necessary.")
|
||||
end
|
||||
end
|
||||
-- io.read("*l")
|
||||
-- print("\n\n\n\n\n\n\n\n\n")
|
||||
-- printspace(world, width + 1, height + 1, depth + 1)
|
||||
|
||||
end
|
||||
|
||||
return world
|
||||
end
|
||||
|
||||
local world = generate_maze3d(seed, width, height, depth)
|
||||
|
||||
printspace(world, width, height, depth)
|
||||
end_time = os.clock()
|
||||
print("Generation completed in " .. (end_time - start_time) .. "s.")
|
Loading…
Reference in a new issue