2020-09-27 00:37:10 +00:00
|
|
|
local svg = require("../utils/svg_writer")
|
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
-------------------------------------
|
|
|
|
-- Maze generation script
|
|
|
|
-------------------------------------
|
|
|
|
-- A test by @Starbeamrainbowlabs
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
local M = {}
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
-- Intelligent table printing function: http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
-- 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
|
2020-04-29 11:31:35 +00:00
|
|
|
|
|
|
|
----------------------------------
|
|
|
|
-- function to print out the world
|
|
|
|
----------------------------------
|
2020-04-29 18:57:18 +00:00
|
|
|
function M.printspace(space, w, h)
|
2020-05-03 01:01:40 +00:00
|
|
|
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
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
|
|
|
|
2020-09-27 23:53:52 +00:00
|
|
|
function M.make_svg(space, width, height, settings)
|
|
|
|
-- Settings: scale, colour, inverted
|
|
|
|
local result = { svg.start(width * settings.scale, height * settings.scale) }
|
2020-09-27 00:37:10 +00:00
|
|
|
for y = 0, height - 1, 1 do
|
|
|
|
for x = 0, width - 1, 1 do
|
2020-09-27 23:53:52 +00:00
|
|
|
local do_square = false
|
|
|
|
if settings.inverted then
|
|
|
|
if space[y][x] == "#" then do_square = true end
|
|
|
|
else
|
|
|
|
if space[y][x] == " " then do_square = true end
|
|
|
|
end
|
|
|
|
if do_square then
|
2020-09-27 00:37:10 +00:00
|
|
|
table.insert(result, svg.rect(
|
2020-09-27 23:53:52 +00:00
|
|
|
x * settings.scale, y * settings.scale,
|
|
|
|
settings.scale, settings.scale,
|
|
|
|
settings.colour
|
2020-09-27 00:37:10 +00:00
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(result, svg.finish())
|
|
|
|
return table.concat(result, "")
|
|
|
|
end
|
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
-- Initialise the world
|
|
|
|
|
2020-09-28 00:25:47 +00:00
|
|
|
function M.generate_maze(seed, width, height, path_length, path_width, branching_factor)
|
2020-05-03 01:01:40 +00:00
|
|
|
start_time = os.clock()
|
2020-05-03 13:23:25 +00:00
|
|
|
|
2020-05-03 01:01:40 +00:00
|
|
|
if not path_length then path_length = 2 end
|
|
|
|
if not path_width then path_width = 1 end
|
2020-09-28 00:25:47 +00:00
|
|
|
if not branching_factor then branching_factor = 6 end
|
2020-05-03 15:48:50 +00:00
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
math.randomseed(seed) -- seed the random number generator with the system clock
|
|
|
|
|
2020-05-03 15:49:21 +00:00
|
|
|
-- width = width - 1
|
|
|
|
-- height = height - 1
|
2020-04-29 11:31:35 +00:00
|
|
|
|
|
|
|
local world = {}
|
|
|
|
for y = 0, height, 1 do
|
|
|
|
world[y] = {}
|
|
|
|
for x = 0, width, 1 do
|
|
|
|
world[y][x] = "#"
|
|
|
|
end
|
|
|
|
end
|
2020-05-03 01:01:40 +00:00
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
-- 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)
|
2020-05-03 13:23:25 +00:00
|
|
|
-- print("Currently at (" .. cx .. ", " .. cy .. "), up: "..(cy-path_length)..", down: "..cy+path_length..", left: "..cx-path_length..", right: "..cx+path_length..", width: "..width..", height: "..height)
|
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
local directions = "" -- the different directions we can move
|
2020-05-03 01:01:40 +00:00
|
|
|
if cy - path_length > 0 and world[cy - path_length][cx] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "u"
|
2020-05-03 13:23:25 +00:00
|
|
|
-- print("up | cy: "..cy..", target: "..cy-path_length..", value: '"..world[cy - path_length][cx].."', path_length: "..path_length)
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
2020-05-03 14:15:42 +00:00
|
|
|
if cy + path_length < height-path_width and world[cy + path_length][cx] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "d"
|
2020-05-03 13:23:25 +00:00
|
|
|
-- print("down | cy: "..cy..", target: "..cy+path_length..", value: '"..world[cy + path_length][cx].."', path_length: "..path_length)
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
2020-05-03 01:01:40 +00:00
|
|
|
if cx - path_length > 0 and world[cy][cx - path_length] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "l"
|
2020-05-03 13:23:25 +00:00
|
|
|
-- print("left | cx: "..cx..", target: "..cx-path_length..", value: '"..world[cy][cx - path_length].."', path_length: "..path_length)
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
2020-05-03 14:15:42 +00:00
|
|
|
if cx + path_length < width-path_width and world[cy][cx + path_length] == "#" then
|
2020-04-29 11:31:35 +00:00
|
|
|
directions = directions .. "r"
|
2020-05-03 13:23:25 +00:00
|
|
|
-- print("right | cx: "..cx..", target: "..cx+path_length..", value: '"..world[cy][cx + path_length].."', path_length: "..path_length)
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
2020-05-03 13:23:25 +00:00
|
|
|
-- print("radar output: '" .. directions .. "' (length: " .. #directions .. "), curnode: " .. curnode)
|
2020-05-03 13:27:18 +00:00
|
|
|
|
2020-09-28 00:25:47 +00:00
|
|
|
local shift_attention = math.random(0, branching_factor)
|
2020-05-03 13:27:18 +00:00
|
|
|
|
2020-04-29 11:31:35 +00:00
|
|
|
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
|
2020-05-03 13:23:25 +00:00
|
|
|
for ix = cx,cx+(path_width-1) do
|
2020-05-03 01:01:40 +00:00
|
|
|
for iy = cy-path_length,cy do
|
|
|
|
world[iy][ix] = " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- world[cy - 1][cx] = " "
|
|
|
|
-- world[cy - 2][cx] = " "
|
|
|
|
cy = cy - path_length
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "d" then
|
2020-05-03 01:01:40 +00:00
|
|
|
for ix = cx,cx+path_width-1 do
|
2020-05-03 13:23:25 +00:00
|
|
|
for iy = cy,cy+path_length+(path_width-1) do
|
2020-05-03 01:01:40 +00:00
|
|
|
world[iy][ix] = " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- world[cy + 1][cx] = " "
|
|
|
|
-- world[cy + 2][cx] = " "
|
|
|
|
cy = cy + path_length
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "l" then
|
2020-05-03 01:01:40 +00:00
|
|
|
for iy = cy,cy+path_width-1 do
|
|
|
|
for ix = cx-path_length,cx do
|
|
|
|
world[iy][ix] = " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- world[cy][cx - 1] = " "
|
|
|
|
-- world[cy][cx - 2] = " "
|
|
|
|
cx = cx - path_length
|
2020-04-29 11:31:35 +00:00
|
|
|
elseif curdir == "r" then
|
2020-05-03 13:23:25 +00:00
|
|
|
for iy = cy,cy+(path_width-1) do
|
|
|
|
for ix = cx,cx+path_length+(path_width-1) do
|
2020-05-03 01:01:40 +00:00
|
|
|
world[iy][ix] = " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- world[cy][cx + 1] = " "
|
|
|
|
-- world[cy][cx + 2] = " "
|
|
|
|
cx = cx + path_length
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
2020-05-03 13:23:25 +00:00
|
|
|
|
|
|
|
-- print("Now at ("..cx..", "..cy..")")
|
2020-05-03 13:27:18 +00:00
|
|
|
|
|
|
|
-- If the number of directions we could travel in was 1, then we've just travelled in the last remaining direction possible
|
2020-09-28 00:25:47 +00:00
|
|
|
-- THIS ISN'T TRUE, because we're analysing the old position and then inserting the new one - so we have to insert regardless
|
|
|
|
table.insert(nodes, { x = cx, y = cy })
|
2020-04-29 11:31:35 +00:00
|
|
|
else
|
|
|
|
--print("The node at " .. curnode .. " is a dead end.")
|
|
|
|
table.remove(nodes, curnode)
|
2020-05-03 13:27:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if #directions == 0 or shift_attention <= 1 then
|
2020-04-29 11:31:35 +00:00
|
|
|
if #nodes > 0 then
|
|
|
|
--print("performing teleport.");
|
|
|
|
curnode = math.random(1, #nodes)
|
|
|
|
cx = nodes[curnode]["x"]
|
|
|
|
cy = nodes[curnode]["y"]
|
|
|
|
end
|
|
|
|
end
|
2020-05-03 13:23:25 +00:00
|
|
|
|
|
|
|
-- M.printspace(world, width, height)
|
|
|
|
-- io.read("*l")
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
2020-04-29 18:57:18 +00:00
|
|
|
|
2020-05-03 01:01:40 +00:00
|
|
|
end_time = os.clock()
|
2020-04-29 18:57:18 +00:00
|
|
|
|
|
|
|
return world, (end_time - start_time) * 1000
|
2020-04-29 11:31:35 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
-- local world, time = generate_maze(os.time(), width, height)
|
|
|
|
-- printspace(world, width, height)
|
|
|
|
-- print("Generation completed in " .. time .. "s.")
|
2020-04-29 11:31:35 +00:00
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
return M
|