Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local speed = .01
- function mazeGenRecursive(mapW, mapH)
- local maze = {}
- local solution = {}
- local firstForward = true
- local firstBack = true
- local firstSolve = true
- for y = 1, mapH do
- maze[y] = {}
- for x = 1, mapW do
- maze[y][x] = {}
- for i = 1, 4 do
- maze[y][x][i] = true
- end
- end
- end
- local current = {1, 1} --y, x
- local stack = {}
- local cellsRemaining = mapW * mapH
- while cellsRemaining > 1 or #stack > 0 do
- local fullCells = {}
- if current[1] > 1 and maze[current[1] - 1][current[2]][1] and maze[current[1] - 1][current[2]][2] and maze[current[1] - 1][current[2]][3] and maze[current[1] - 1][current[2]][4] then
- fullCells[#fullCells + 1] = 1
- end
- if current[1] < mapH and maze[current[1] + 1][current[2]][1] and maze[current[1] + 1][current[2]][2] and maze[current[1] + 1][current[2]][3] and maze[current[1] + 1][current[2]][4]then
- fullCells[#fullCells + 1] = 3
- end
- if current[2] > 1 and maze[current[1]][current[2] - 1][1] and maze[current[1]][current[2] - 1][2] and maze[current[1]][current[2] - 1][3] and maze[current[1]][current[2] - 1][4] then
- fullCells[#fullCells + 1] = 4
- end
- if current[2] < mapW and maze[current[1]][current[2] + 1][1] and maze[current[1]][current[2] + 1][2] and maze[current[1]][current[2] + 1][3] and maze[current[1]][current[2] + 1][4] then
- fullCells[#fullCells + 1] = 2
- end
- if #fullCells > 0 then
- firstBack = true
- term.setBackgroundColor(colors.white)
- if firstForward then
- term.setCursorPos(current[2]*2, current[1]*2)
- term.write(" ")
- sleep(speed)
- firstForward = false
- end
- cellsRemaining = cellsRemaining - 1
- stack[#stack + 1] = {current[1], current[2]}
- fullCells = fullCells[math.random(1, #fullCells)]
- maze[current[1]][current[2]][fullCells] = false
- if fullCells == 1 then
- maze[current[1] - 1][current[2]][3] = false
- term.setCursorPos(current[2]*2, current[1]*2 - 1)
- current = {current[1] - 1, current[2]}
- elseif fullCells == 2 then
- maze[current[1]][current[2] + 1][4] = false
- term.setCursorPos(current[2]*2 + 1, current[1]*2)
- current = {current[1], current[2] + 1}
- elseif fullCells == 3 then
- maze[current[1] + 1][current[2]][1] = false
- term.setCursorPos(current[2]*2, current[1]*2 + 1)
- current = {current[1] + 1, current[2]}
- elseif fullCells == 4 then
- maze[current[1]][current[2] - 1][2] = false
- term.setCursorPos(current[2]*2 - 1, current[1]*2)
- current = {current[1], current[2] - 1}
- end
- term.write(" ")
- sleep(speed)
- term.setCursorPos(current[2]*2, current[1]*2)
- term.write(" ")
- if current[1] == mapH and current[2] == mapW and #solution == 0 then
- for i, v in pairs(stack) do
- solution[i] = {v[1], v[2]}
- end
- solution[#solution + 1] = {current[1], current[2]}
- end
- else
- firstForward = true
- term.setBackgroundColor(colors.red)
- if firstBack then
- term.setCursorPos(current[2]*2, current[1]*2)
- term.write(" ")
- sleep(speed)
- firstBack = false
- end
- local temp = {current[1], current[2]}
- current = table.remove(stack)
- if current[1] > temp[1] then
- term.setCursorPos(current[2]*2, current[1]*2 - 1)
- term.write(" ")
- elseif current[1] < temp[1] then
- term.setCursorPos(current[2]*2, current[1]*2 + 1)
- term.write(" ")
- elseif current[2] > temp[2] then
- term.setCursorPos(current[2]*2 - 1, current[1]*2)
- term.write(" ")
- elseif current[2] < temp[2] then
- term.setCursorPos(current[2]*2 + 1, current[1]*2)
- term.write(" ")
- end
- sleep(speed)
- term.setCursorPos(current[2]*2, current[1]*2)
- term.write(" ")
- end
- sleep(speed)
- end
- term.setBackgroundColor(colors.blue)
- local prevCoord = nil
- for i, v in pairs(solution) do
- if firstSolve then
- term.setCursorPos(v[2]*2, v[1]*2)
- term.write(" ")
- prevCoord = {v[1], v[2]}
- firstSolve = false
- else
- if v[1] > prevCoord[1] then
- term.setCursorPos(v[2]*2, v[1]*2 - 1)
- term.write(" ")
- elseif v[1] < prevCoord[1] then
- term.setCursorPos(v[2]*2, v[1]*2 + 1)
- term.write(" ")
- elseif v[2] > prevCoord[2] then
- term.setCursorPos(v[2]*2 - 1, v[1]*2)
- term.write(" ")
- elseif v[2] < prevCoord[2] then
- term.setCursorPos(v[2]*2 + 1, v[1]*2)
- term.write(" ")
- end
- term.setCursorPos(v[2]*2, v[1]*2)
- term.write(" ")
- prevCoord = {v[1], v[2]}
- end
- end
- end
- function mazeGenBranching(mapW, mapH)
- local maze = {}
- for y = 1, mapH do
- maze[y] = {}
- for x = 1, mapW do
- maze[y][x] = {}
- for i = 1, 4 do
- maze[y][x][i] = true
- end
- end
- end
- local current = {1, 1}
- local list = {}
- list[1] = {math.random(1, mapH), math.random(1, mapW)} --y, x
- while #list > 0 do
- local choice = math.random(1, #list)
- current = {list[choice][1], list[choice][2]}
- term.setBackgroundColor(colors.yellow)
- term.setCursorPos(current[2]*2, current[1]*2)
- term.write(" ")
- sleep(speed)
- local openWalls = {}
- if current[1] > 1 and maze[current[1] - 1][current[2]][1] and maze[current[1] - 1][current[2]][2] and maze[current[1] - 1][current[2]][3] and maze[current[1] - 1][current[2]][4] then
- openWalls[#openWalls + 1] = 1
- end
- if current[1] < mapH and maze[current[1] + 1][current[2]][1] and maze[current[1] + 1][current[2]][2] and maze[current[1] + 1][current[2]][3] and maze[current[1] + 1][current[2]][4] then
- openWalls[#openWalls + 1] = 3
- end
- if current[2] > 1 and maze[current[1]][current[2] - 1][1] and maze[current[1]][current[2] - 1][2] and maze[current[1]][current[2] - 1][3] and maze[current[1]][current[2] - 1][4] then
- openWalls[#openWalls + 1] = 4
- end
- if current[2] < mapW and maze[current[1]][current[2] + 1][1] and maze[current[1]][current[2] + 1][2] and maze[current[1]][current[2] + 1][3] and maze[current[1]][current[2] + 1][4] then
- openWalls[#openWalls + 1] = 2
- end
- if #openWalls > 0 then
- openWalls = openWalls[math.random(1, #openWalls)]
- maze[current[1]][current[2]][openWalls] = false
- if openWalls == 1 then
- maze[current[1] - 1][current[2]][3] = false
- list[#list + 1] = {current[1] - 1, current[2]}
- term.setBackgroundColor(colors.purple)
- term.setCursorPos(current[2]*2, current[1]*2 - 1)
- term.write(" ")
- sleep(speed + .05)
- term.setBackgroundColor(colors.yellow)
- term.setCursorPos(current[2]*2, current[1]*2 - 1)
- term.write(" ")
- elseif openWalls == 2 then
- maze[current[1]][current[2] + 1][4] = false
- list[#list + 1] = {current[1], current[2] + 1}
- term.setBackgroundColor(colors.purple)
- term.setCursorPos(current[2]*2 + 1, current[1]*2)
- term.write(" ")
- sleep(speed + .05)
- term.setBackgroundColor(colors.yellow)
- term.setCursorPos(current[2]*2 + 1, current[1]*2)
- term.write(" ")
- elseif openWalls == 3 then
- maze[current[1] + 1][current[2]][1] = false
- list[#list + 1] = {current[1] + 1, current[2]}
- term.setBackgroundColor(colors.purple)
- term.setCursorPos(current[2]*2, current[1]*2 + 1)
- term.write(" ")
- sleep(speed + .05)
- term.setBackgroundColor(colors.yellow)
- term.setCursorPos(current[2]*2, current[1]*2 + 1)
- term.write(" ")
- elseif openWalls == 4 then
- maze[current[1]][current[2] - 1][2] = false
- list[#list + 1] = {current[1], current[2] - 1}
- term.setBackgroundColor(colors.purple)
- term.setCursorPos(current[2]*2 - 1, current[1]*2)
- term.write(" ")
- sleep(speed + .05)
- term.setBackgroundColor(colors.yellow)
- term.setCursorPos(current[2]*2 - 1, current[1]*2)
- term.write(" ")
- end
- sleep(speed)
- else
- table.remove(list, choice)
- end
- end
- local solution = solveMaze(maze)
- local gen = maze
- maze = {}
- for i = 1, mapH*2 + 1 do
- maze[i] = {}
- end
- for i = 1, mapW*2 + 1 do
- maze[1][i] = 1
- maze[mapH*2 + 1][i] = 1
- end
- for y = 2, mapH*2 do
- for x = 1, mapW*2 + 1 do
- if x == 1 or x == mapW*2 + 1 then
- maze[y][x] = 1
- elseif y%2 == 0 and x%2 == 0 then
- maze[y][x] = 2^15
- elseif y%2 == 1 and x%2 == 1 then
- maze[y][x] = 1
- else
- if y%2 == 0 then
- if gen[y/2][(x - 1)/2][2] then
- maze[y][x] = 1
- else
- maze[y][x] = 2^15
- end
- elseif y%2 == 1 then
- if gen[(y - 1)/2][x/2][3] then
- maze[y][x] = 1
- else
- maze[y][x] = 2^15
- end
- end
- end
- end
- end
- local firstSolve = true
- term.setBackgroundColor(colors.orange)
- local prevCoord = nil
- for i, v in pairs(solution) do
- if firstSolve then
- term.setCursorPos(v[2]*2, v[1]*2)
- term.write(" ")
- prevCoord = {v[1], v[2]}
- firstSolve = false
- else
- if v[1] > prevCoord[1] then
- term.setCursorPos(v[2]*2, v[1]*2 - 1)
- term.write(" ")
- elseif v[1] < prevCoord[1] then
- term.setCursorPos(v[2]*2, v[1]*2 + 1)
- term.write(" ")
- elseif v[2] > prevCoord[2] then
- term.setCursorPos(v[2]*2 - 1, v[1]*2)
- term.write(" ")
- elseif v[2] < prevCoord[2] then
- term.setCursorPos(v[2]*2 + 1, v[1]*2)
- term.write(" ")
- end
- term.setCursorPos(v[2]*2, v[1]*2)
- term.write(" ")
- prevCoord = {v[1], v[2]}
- end
- end
- end
- function solveMaze(maze)
- local stack = {}
- local visited = {}
- local mapW, mapH = #maze[1], #maze
- for i = 1, mapH do
- visited[i] = {}
- end
- local sLocation = {1, 1} --y, x
- visited[1][1] = true
- while true do
- local adjCells = {}
- if sLocation[1] > 1 and not maze[sLocation[1]][sLocation[2]][1] and not visited[sLocation[1] - 1][sLocation[2]] then
- adjCells[#adjCells + 1] = 1
- end
- if sLocation[2] < mapW and not maze[sLocation[1]][sLocation[2]][2] and not visited[sLocation[1]][sLocation[2] + 1] then
- adjCells[#adjCells + 1] = 2
- end
- if sLocation[1] < mapH and not maze[sLocation[1]][sLocation[2]][3] and not visited[sLocation[1] + 1][sLocation[2]] then
- adjCells[#adjCells + 1] = 3
- end
- if sLocation[2] > 1 and not maze[sLocation[1]][sLocation[2]][4] and not visited[sLocation[1]][sLocation[2] - 1] then
- adjCells[#adjCells + 1] = 4
- end
- if #adjCells > 0 then
- stack[#stack + 1] = {sLocation[1], sLocation[2]}
- adjCells = adjCells[math.random(1, #adjCells)]
- if adjCells == 1 then
- sLocation[1] = sLocation[1] - 1
- elseif adjCells == 2 then
- sLocation[2] = sLocation[2] + 1
- elseif adjCells == 3 then
- sLocation[1] = sLocation[1] + 1
- elseif adjCells == 4 then
- sLocation[2] = sLocation[2] - 1
- end
- visited[sLocation[1]][sLocation[2]] = true
- if sLocation[1] == mapH and sLocation[2] == mapW then
- stack[#stack + 1] = {sLocation[1], sLocation[2]}
- break
- end
- else
- sLocation = table.remove(stack)
- end
- end
- return stack
- end
- while true do
- local typeList = {"Recursive", "Branching"}
- local termW, termH = term.getSize()
- local mapW, mapH = math.floor(termW/2), math.floor(termH/2)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Choose maze type: ")
- local begun = false
- local type = ""
- while not begun do
- for i, v in pairs(typeList) do
- term.setCursorPos(termW/2 - #v/2 + 1, i*2 + 1)
- term.write(v)
- end
- local click = {os.pullEvent("mouse_click")}
- for i, v in pairs(typeList) do
- if click[4] == i*2 + 1 then
- term.setBackgroundColor(colors.blue)
- term.setCursorPos(1, i*2 + 1)
- term.clearLine()
- term.setCursorPos(termW/2 - #v/2 + 1, i*2 + 1)
- term.write(v)
- sleep(.2)
- begun = true
- type = v
- break
- end
- end
- end
- --while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- if type == "Recursive" then
- mazeGenRecursive(mapW, mapH)
- elseif type == "Branching" then
- mazeGenBranching(mapW, mapH)
- end
- os.pullEvent()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement