Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.wrap("right")
- local w, h = monitor.getSize()
- local size = tonumber(read())
- local matrix = {}
- local focusedcell = nil
- local queue = {}
- local lengths = {}
- monitor.clear()
- monitor.setTextScale(0.5)
- term.redirect(monitor)
- function getNeighbors(mat, row, col, radius)
- local rows, cols = #mat, #mat[1]
- local out = {}
- for i = row - radius, row + radius do
- if mat[i] and mat[i][col]then
- if mat[i][col] ~= mat[row][col] then
- if not mat[i][col]["wall"] == true then
- if mat[i][col]["visited"] == false then
- table.insert(out, mat[i][col])
- end
- end
- end
- end
- end
- for j = col - radius, col + radius do
- if mat[row] and mat[row][j] then
- if mat[row][j] ~= mat[row][col] then
- if not mat[row][j]["wall"] == true then
- if mat[row][j]["visited"] == false then
- table.insert(out, mat[row][j])
- table.insert(out, mat[row][j])
- end
- end
- end
- end
- end
- return out
- end
- function mazeGen(focus)
- --monitor.setCursorPos(focus["x"], focus["y"])
- --monitor.write("e")
- local neighbors = getNeighbors(matrix, focus["x"], focus["y"], 2)
- focus["visited"] = true
- if #neighbors ~= 0 then
- local ranNeigh = neighbors[math.random(1,#neighbors)]
- local differencex, differencey = (ranNeigh["x"]-focus["x"]), (ranNeigh["y"]-focus["y"])
- --print(differencex, differencey)
- --[[monitor.setCursorPos(focus["x"]+(differencex/2), focus["y"]+(differencey/2))
- monitor.write(" ")]]
- paintutils.drawPixel(focus["x"]+(differencex/2), focus["y"]+(differencey/2), colors.black)
- matrix[focus["x"]+(differencex/2)][focus["y"]+(differencey/2)]["visited"] = true
- matrix[focus["x"]+(differencex/2)][focus["y"]+(differencey/2)]["wall"] = false
- table.insert(queue, focus)
- sleep(0.05)
- mazeGen(ranNeigh)
- else
- while #queue > 0 do
- local check = queue[#queue]
- local nei = getNeighbors(matrix, check["x"], check["y"], 2)
- --print(#nei)
- if #nei == 0 then
- table.insert(lengths, {["point"] = check, ["length"] = #queue})
- table.remove(queue, #queue)
- if #queue == 0 then
- break
- end
- else
- --print("Found! "..check["x"].." ".. check["y"])
- focus = check
- break
- end
- end
- if #queue == 0 then
- --print("Maze done!")
- return 0
- end
- mazeGen(focus)
- end
- end
- for x = 1, w*1 do
- matrix[x] = {}
- for y = 1, h*1 do
- if y % 2 == 0 or x % 2 == 0 then
- matrix[x][y] = {["x"] = x, ["y"] = y, ["visited"] = false, ["neighbors"] = {}, ["wall"] = true}
- paintutils.drawPixel(x, y, colors.white)
- else
- matrix[x][y] = {["x"] = x, ["y"] = y, ["visited"] = false, ["neighbors"] = {}, ["wall"] = false}
- end
- end
- end
- local focusedcell = matrix[1][2]
- local startingcell = matrix[1][2]
- --[[print("Caching Neighbors")
- for i,v in pairs(matrix) do
- for o,b in pairs(matrix[i]) do
- sleep()
- print(b["x"], b["y"])
- b["neighbors"] = getNeighbors(matrix, i, o, 2)
- end
- end]]
- while focusedcell["wall"] == true do
- focusedcell = matrix[math.random(1,w)][math.random(1,h)]
- startingcell = focusedcell
- end
- monitor.setCursorPos(startingcell["x"], startingcell["y"])
- monitor.write("s")
- --print("Found a non-wall cell to start from~!")
- --local neighbors = focusedcell["neighbors"]
- --[[for i,v in pairs(neighbors) do
- print(v["x"], v["y"])
- monitor.setCursorPos(v["x"], v["y"])
- monitor.write("e")
- end]]
- mazeGen(focusedcell)
- table.sort(lengths, function(a,b)
- local aNum = a["length"] -- Coin value of a
- local bNum = b["length"] -- Coin value of b
- return aNum > bNum -- Return their comparisons, < for ascending, > for descending
- end)
- --print("Point: "..lengths[1]["point"]["x"].." "..lengths[1]["point"]["y"].. " Highest Length: "..lengths[1]["length"])
- monitor.setCursorPos(lengths[1]["point"]["x"], lengths[1]["point"]["y"])
- monitor.write("e")
- monitor.setCursorPos(startingcell["x"], startingcell["y"])
- monitor.write("s")
Add Comment
Please, Sign In to add comment