Fix some bugs in the generator
This commit is contained in:
parent
5e0d97fac6
commit
c3064a8469
2 changed files with 9 additions and 23 deletions
2
main.lua
2
main.lua
|
@ -121,7 +121,7 @@ end
|
||||||
|
|
||||||
local function maze3d_openscad()
|
local function maze3d_openscad()
|
||||||
local world, time = maze3d.generate_maze(
|
local world, time = maze3d.generate_maze(
|
||||||
setting.seed,
|
settings.seed,
|
||||||
settings.width,
|
settings.width,
|
||||||
settings.height,
|
settings.height,
|
||||||
settings.depth,
|
settings.depth,
|
||||||
|
|
28
maze3d.lua
28
maze3d.lua
|
@ -44,13 +44,12 @@ end
|
||||||
-- Initialise the world
|
-- Initialise the world
|
||||||
|
|
||||||
function M.generate_maze(seed, width, height, depth, path_length, path_width, path_depth)
|
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_length then path_length = 2 end
|
||||||
if not path_width then path_width = 1 end
|
if not path_width then path_width = 1 end
|
||||||
if not path_depth then path_depth = 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
|
math.randomseed(seed) -- seed the random number generator with the system clock
|
||||||
|
|
||||||
local world = {}
|
local world = {}
|
||||||
|
@ -73,48 +72,38 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
||||||
while #nodes > 0 do
|
while #nodes > 0 do
|
||||||
-- io.write("Nodes left: " .. curnode .. "\r")
|
-- io.write("Nodes left: " .. curnode .. "\r")
|
||||||
--print("Nodes left: " .. #nodes)
|
--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
|
local directions = "" -- the different directions we can move in
|
||||||
if cz - path_length > 0 and world[cz - path_length][cy][cx] == "#" then
|
if cz - path_length > 0 and world[cz - path_length][cy][cx] == "#" then
|
||||||
directions = directions .. "-"
|
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
|
end
|
||||||
if cz + path_length < depth-path_depth and world[cz + path_length][cy][cx] == "#" then
|
if cz + path_length < depth-path_depth and world[cz + path_length][cy][cx] == "#" then
|
||||||
directions = directions .. "+"
|
directions = directions .. "+"
|
||||||
-- print("d++ | cz: "..cz..", target: "..cz+path_length..", value: '"..world[cz + path_length][cy][cx].."', path_length: "..path_length)
|
|
||||||
end
|
end
|
||||||
if cy - path_length > 0 and world[cz][cy - path_length][cx] == "#" then
|
if cy - path_length > 0 and world[cz][cy - path_length][cx] == "#" then
|
||||||
directions = directions .. "u"
|
directions = directions .. "u"
|
||||||
-- print("up | cy: "..cy..", target: "..cy-path_length..", value: '"..world[cz][cy - path_length][cx].."', path_length: "..path_length)
|
|
||||||
end
|
end
|
||||||
if cy + path_length < height-path_width and world[cz][cy + path_length][cx] == "#" then
|
if cy + path_length < height-path_width and world[cz][cy + path_length][cx] == "#" then
|
||||||
directions = directions .. "d"
|
directions = directions .. "d"
|
||||||
-- print("down | cy: "..cy..", target: "..cy+path_length..", value: '"..world[cz][cy + path_length][cx].."', path_length: "..path_length)
|
|
||||||
end
|
end
|
||||||
if cx - path_length > 0 and world[cz][cy][cx - path_length] == "#" then
|
if cx - path_length > 0 and world[cz][cy][cx - path_length] == "#" then
|
||||||
directions = directions .. "l"
|
directions = directions .. "l"
|
||||||
-- print("left | cx: "..cx..", target: "..cx-path_length..", value: '"..world[cz][cy][cx - path_length].."', path_length: "..path_length)
|
|
||||||
end
|
end
|
||||||
if cx + path_length < width-path_width and world[cz][cy][cx + path_length] == "#" then
|
if cx + path_length < width-path_width and world[cz][cy][cx + path_length] == "#" then
|
||||||
directions = directions .. "r"
|
directions = directions .. "r"
|
||||||
-- print("right | cx: "..cx..", target: "..cx+path_length..", value: '"..world[cz][cy][cx + path_length].."', path_length: "..path_length)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If this is 1 or less, then we will switch our attention to another candidate node after moving
|
-- 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)
|
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
|
if #directions > 0 then
|
||||||
-- we still have somewhere that we can go
|
-- we still have somewhere that we can go
|
||||||
local curdirnum = math.random(1, #directions)
|
local curdirnum = math.random(1, #directions)
|
||||||
local curdir = string.sub(directions, curdirnum, curdirnum)
|
local curdir = string.sub(directions, curdirnum, curdirnum)
|
||||||
|
|
||||||
print("chose '"..curdir.."'")
|
|
||||||
|
|
||||||
if curdir == "+" then
|
if curdir == "+" then
|
||||||
|
|
||||||
for iz = cz,cz+path_length+(path_depth-1) do
|
for iz = cz,cz+path_length+(path_depth-1) do
|
||||||
for ix = cx,cx+(path_width-1) do
|
for ix = cx,cx+(path_width-1) do
|
||||||
for iy = cy,cy+(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 ix = cx,cx+(path_width-1) do
|
||||||
for iy = cy,cy+path_length+(path_width-1) do
|
for iy = cy,cy+path_length+(path_width-1) do
|
||||||
world[iz][iy][ix] = " "
|
world[iz][iy][ix] = " "
|
||||||
|
-- print("[tunnel/d] ("..ix..", "..iy..", "..iz..")")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -182,9 +172,7 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
||||||
cx = cx + path_length
|
cx = cx + path_length
|
||||||
end
|
end
|
||||||
|
|
||||||
if #directions > 1 then
|
|
||||||
table.insert(nodes, { x = cx, y = cy, z = cz })
|
table.insert(nodes, { x = cx, y = cy, z = cz })
|
||||||
end
|
|
||||||
else
|
else
|
||||||
table.remove(nodes, curnode)
|
table.remove(nodes, curnode)
|
||||||
end
|
end
|
||||||
|
@ -198,14 +186,12 @@ function M.generate_maze(seed, width, height, depth, path_length, path_width, pa
|
||||||
cz = nodes[curnode]["z"]
|
cz = nodes[curnode]["z"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.printspace(world, width, height, depth)
|
|
||||||
io.read("*l")
|
|
||||||
end
|
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_time - start_time) * 1000
|
||||||
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in a new issue