Advertisement
CaptainSpaceCat

MazeScreen

Jun 9th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.27 KB | None | 0 0
  1. local speed = .01
  2.  
  3. function mazeGenRecursive(mapW, mapH)
  4.         local maze = {}
  5.     local solution = {}
  6.     local firstForward = true
  7.     local firstBack = true
  8.     local firstSolve = true
  9.         for y = 1, mapH do
  10.                 maze[y] = {}
  11.                 for x = 1, mapW do
  12.                         maze[y][x] = {}
  13.                         for i = 1, 4 do
  14.                                 maze[y][x][i] = true
  15.                         end
  16.                 end
  17.         end
  18.        
  19.         local current = {1, 1}   --y, x
  20.         local stack = {}
  21.         local cellsRemaining = mapW * mapH
  22.         while cellsRemaining > 1 or #stack > 0 do
  23.                 local fullCells = {}
  24.                 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
  25.                         fullCells[#fullCells + 1] = 1
  26.                 end
  27.                 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
  28.                         fullCells[#fullCells + 1] = 3
  29.                 end
  30.                 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
  31.                         fullCells[#fullCells + 1] = 4
  32.                 end
  33.                 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
  34.                         fullCells[#fullCells + 1] = 2
  35.                 end
  36.                 if #fullCells > 0 then
  37.             firstBack = true
  38.             term.setBackgroundColor(colors.white)
  39.             if firstForward then
  40.                 term.setCursorPos(current[2]*2, current[1]*2)
  41.                 term.write(" ")
  42.                 sleep(speed)
  43.                 firstForward = false
  44.             end
  45.                         cellsRemaining = cellsRemaining - 1
  46.                         stack[#stack + 1] = {current[1], current[2]}
  47.                         fullCells = fullCells[math.random(1, #fullCells)]
  48.                         maze[current[1]][current[2]][fullCells] = false
  49.                         if fullCells == 1 then
  50.                                 maze[current[1] - 1][current[2]][3] = false
  51.                 term.setCursorPos(current[2]*2, current[1]*2 - 1)
  52.                                 current = {current[1] - 1, current[2]}
  53.                         elseif fullCells == 2 then
  54.                                 maze[current[1]][current[2] + 1][4] = false
  55.                 term.setCursorPos(current[2]*2 + 1, current[1]*2)
  56.                                 current = {current[1], current[2] + 1}
  57.                         elseif fullCells == 3 then
  58.                                 maze[current[1] + 1][current[2]][1] = false
  59.                 term.setCursorPos(current[2]*2, current[1]*2 + 1)
  60.                                 current = {current[1] + 1, current[2]}
  61.                         elseif fullCells == 4 then
  62.                                 maze[current[1]][current[2] - 1][2] = false
  63.                 term.setCursorPos(current[2]*2 - 1, current[1]*2)
  64.                                 current = {current[1], current[2] - 1}
  65.                         end
  66.             term.write(" ")
  67.             sleep(speed)
  68.             term.setCursorPos(current[2]*2, current[1]*2)
  69.             term.write(" ")
  70.             if current[1] == mapH and current[2] == mapW and #solution == 0 then
  71.                 for i, v in pairs(stack) do
  72.                     solution[i] = {v[1], v[2]}
  73.                 end
  74.                 solution[#solution + 1] = {current[1], current[2]}
  75.             end
  76.                 else
  77.             firstForward = true
  78.             term.setBackgroundColor(colors.red)
  79.             if firstBack then
  80.                 term.setCursorPos(current[2]*2, current[1]*2)
  81.                 term.write(" ")
  82.                 sleep(speed)
  83.                 firstBack = false
  84.             end
  85.             local temp = {current[1], current[2]}
  86.                         current = table.remove(stack)
  87.             if current[1] > temp[1] then
  88.                 term.setCursorPos(current[2]*2, current[1]*2 - 1)
  89.                 term.write(" ")
  90.             elseif current[1] < temp[1] then
  91.                 term.setCursorPos(current[2]*2, current[1]*2 + 1)
  92.                 term.write(" ")
  93.             elseif current[2] > temp[2] then
  94.                 term.setCursorPos(current[2]*2 - 1, current[1]*2)
  95.                 term.write(" ")
  96.             elseif current[2] < temp[2] then
  97.                 term.setCursorPos(current[2]*2 + 1, current[1]*2)
  98.                 term.write(" ")
  99.             end
  100.             sleep(speed)
  101.             term.setCursorPos(current[2]*2, current[1]*2)
  102.             term.write(" ")
  103.                 end
  104.         sleep(speed)
  105.         end
  106.     term.setBackgroundColor(colors.blue)
  107.     local prevCoord = nil
  108.     for i, v in pairs(solution) do
  109.         if firstSolve then
  110.             term.setCursorPos(v[2]*2, v[1]*2)
  111.             term.write(" ")
  112.             prevCoord = {v[1], v[2]}
  113.             firstSolve = false
  114.         else
  115.             if v[1] > prevCoord[1] then
  116.                 term.setCursorPos(v[2]*2, v[1]*2 - 1)
  117.                 term.write(" ")
  118.             elseif v[1] < prevCoord[1] then
  119.                 term.setCursorPos(v[2]*2, v[1]*2 + 1)
  120.                 term.write(" ")
  121.             elseif v[2] > prevCoord[2] then
  122.                 term.setCursorPos(v[2]*2 - 1, v[1]*2)
  123.                 term.write(" ")
  124.             elseif v[2] < prevCoord[2] then
  125.                 term.setCursorPos(v[2]*2 + 1, v[1]*2)
  126.                 term.write(" ")
  127.             end
  128.             term.setCursorPos(v[2]*2, v[1]*2)
  129.             term.write(" ")
  130.             prevCoord = {v[1], v[2]}
  131.         end
  132.     end
  133. end
  134.  
  135. function mazeGenBranching(mapW, mapH)
  136.         local maze = {}
  137.         for y = 1, mapH do
  138.                 maze[y] = {}
  139.                 for x = 1, mapW do
  140.                         maze[y][x] = {}
  141.                         for i = 1, 4 do
  142.                                 maze[y][x][i] = true
  143.                         end
  144.                 end
  145.         end
  146.         local current = {1, 1}
  147.         local list = {}
  148.         list[1] = {math.random(1, mapH), math.random(1, mapW)}   --y, x
  149.         while #list > 0 do
  150.                 local choice = math.random(1, #list)
  151.                 current = {list[choice][1], list[choice][2]}
  152.         term.setBackgroundColor(colors.yellow)
  153.         term.setCursorPos(current[2]*2, current[1]*2)
  154.         term.write(" ")
  155.         sleep(speed)
  156.                 local openWalls = {}
  157.                 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
  158.                         openWalls[#openWalls + 1] = 1
  159.                 end
  160.                 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
  161.                         openWalls[#openWalls + 1] = 3
  162.                 end
  163.                 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
  164.                         openWalls[#openWalls + 1] = 4
  165.                 end
  166.                 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
  167.                         openWalls[#openWalls + 1] = 2
  168.                 end
  169.                 if #openWalls > 0 then
  170.                         openWalls = openWalls[math.random(1, #openWalls)]
  171.                         maze[current[1]][current[2]][openWalls] = false
  172.                         if openWalls == 1 then
  173.                                 maze[current[1] - 1][current[2]][3] = false
  174.                                 list[#list + 1] = {current[1] - 1, current[2]}
  175.                 term.setBackgroundColor(colors.purple)
  176.                 term.setCursorPos(current[2]*2, current[1]*2 - 1)
  177.                 term.write(" ")
  178.                 sleep(speed + .05)
  179.                 term.setBackgroundColor(colors.yellow)
  180.                 term.setCursorPos(current[2]*2, current[1]*2 - 1)
  181.                 term.write(" ")
  182.                         elseif openWalls == 2 then
  183.                                 maze[current[1]][current[2] + 1][4] = false
  184.                                 list[#list + 1] = {current[1], current[2] + 1}
  185.                 term.setBackgroundColor(colors.purple)
  186.                 term.setCursorPos(current[2]*2 + 1, current[1]*2)
  187.                 term.write(" ")
  188.                 sleep(speed + .05)
  189.                 term.setBackgroundColor(colors.yellow)
  190.                 term.setCursorPos(current[2]*2 + 1, current[1]*2)
  191.                 term.write(" ")
  192.                         elseif openWalls == 3 then
  193.                                 maze[current[1] + 1][current[2]][1] = false
  194.                                 list[#list + 1] = {current[1] + 1, current[2]}
  195.                 term.setBackgroundColor(colors.purple)
  196.                 term.setCursorPos(current[2]*2, current[1]*2 + 1)
  197.                 term.write(" ")
  198.                 sleep(speed + .05)
  199.                 term.setBackgroundColor(colors.yellow)
  200.                 term.setCursorPos(current[2]*2, current[1]*2 + 1)
  201.                 term.write(" ")
  202.                         elseif openWalls == 4 then
  203.                                 maze[current[1]][current[2] - 1][2] = false
  204.                                 list[#list + 1] = {current[1], current[2] - 1}
  205.                 term.setBackgroundColor(colors.purple)
  206.                 term.setCursorPos(current[2]*2 - 1, current[1]*2)
  207.                 term.write(" ")
  208.                 sleep(speed + .05)
  209.                 term.setBackgroundColor(colors.yellow)
  210.                 term.setCursorPos(current[2]*2 - 1, current[1]*2)
  211.                 term.write(" ")
  212.                         end
  213.             sleep(speed)
  214.                 else
  215.                         table.remove(list, choice)
  216.                 end
  217.         end
  218.         local solution = solveMaze(maze)
  219.         local gen = maze
  220.         maze = {}
  221.         for i = 1, mapH*2 + 1 do
  222.                 maze[i] = {}
  223.         end
  224.         for i = 1, mapW*2 + 1 do
  225.                 maze[1][i] = 1
  226.                 maze[mapH*2 + 1][i] = 1
  227.         end
  228.         for y = 2, mapH*2 do
  229.                 for x = 1, mapW*2 + 1 do
  230.                         if x == 1 or x == mapW*2 + 1 then
  231.                                 maze[y][x] = 1
  232.                         elseif y%2 == 0 and x%2 == 0 then
  233.                                 maze[y][x] = 2^15
  234.                         elseif y%2 == 1 and x%2 == 1 then
  235.                                 maze[y][x] = 1
  236.                         else
  237.                                 if y%2 == 0 then
  238.                                         if gen[y/2][(x - 1)/2][2] then
  239.                                                 maze[y][x] = 1
  240.                                         else
  241.                                                 maze[y][x] = 2^15
  242.                                         end
  243.                                 elseif y%2 == 1 then
  244.                                         if gen[(y - 1)/2][x/2][3] then
  245.                                                 maze[y][x] = 1
  246.                                         else
  247.                                                 maze[y][x] = 2^15
  248.                                         end
  249.                                 end
  250.                         end
  251.                 end
  252.         end
  253.     local firstSolve = true
  254.     term.setBackgroundColor(colors.orange)
  255.     local prevCoord = nil
  256.     for i, v in pairs(solution) do
  257.         if firstSolve then
  258.             term.setCursorPos(v[2]*2, v[1]*2)
  259.             term.write(" ")
  260.             prevCoord = {v[1], v[2]}
  261.             firstSolve = false
  262.         else
  263.             if v[1] > prevCoord[1] then
  264.                 term.setCursorPos(v[2]*2, v[1]*2 - 1)
  265.                 term.write(" ")
  266.             elseif v[1] < prevCoord[1] then
  267.                 term.setCursorPos(v[2]*2, v[1]*2 + 1)
  268.                 term.write(" ")
  269.             elseif v[2] > prevCoord[2] then
  270.                 term.setCursorPos(v[2]*2 - 1, v[1]*2)
  271.                 term.write(" ")
  272.             elseif v[2] < prevCoord[2] then
  273.                 term.setCursorPos(v[2]*2 + 1, v[1]*2)
  274.                 term.write(" ")
  275.             end
  276.             term.setCursorPos(v[2]*2, v[1]*2)
  277.             term.write(" ")
  278.             prevCoord = {v[1], v[2]}
  279.         end
  280.     end
  281. end
  282.  
  283. function solveMaze(maze)
  284.     local stack = {}
  285.     local visited = {}
  286.     local mapW, mapH = #maze[1], #maze
  287.     for i = 1, mapH do
  288.         visited[i] = {}
  289.     end
  290.     local sLocation = {1, 1}   --y, x
  291.     visited[1][1] = true
  292.     while true do
  293.         local adjCells = {}
  294.         if sLocation[1] > 1 and not maze[sLocation[1]][sLocation[2]][1] and not visited[sLocation[1] - 1][sLocation[2]] then
  295.             adjCells[#adjCells + 1] = 1
  296.         end
  297.         if sLocation[2] < mapW and not maze[sLocation[1]][sLocation[2]][2] and not visited[sLocation[1]][sLocation[2] + 1] then
  298.             adjCells[#adjCells + 1] = 2
  299.         end
  300.         if sLocation[1] < mapH and not maze[sLocation[1]][sLocation[2]][3] and not visited[sLocation[1] + 1][sLocation[2]] then
  301.             adjCells[#adjCells + 1] = 3
  302.         end
  303.         if sLocation[2] > 1 and not maze[sLocation[1]][sLocation[2]][4] and not visited[sLocation[1]][sLocation[2] - 1] then
  304.             adjCells[#adjCells + 1] = 4
  305.         end
  306.         if #adjCells > 0 then
  307.             stack[#stack + 1] = {sLocation[1], sLocation[2]}
  308.             adjCells = adjCells[math.random(1, #adjCells)]
  309.             if adjCells == 1 then
  310.                 sLocation[1] = sLocation[1] - 1
  311.             elseif adjCells == 2 then
  312.                 sLocation[2] = sLocation[2] + 1
  313.             elseif adjCells == 3 then
  314.                 sLocation[1] = sLocation[1] + 1
  315.             elseif adjCells == 4 then
  316.                 sLocation[2] = sLocation[2] - 1
  317.             end
  318.             visited[sLocation[1]][sLocation[2]] = true
  319.             if sLocation[1] == mapH and sLocation[2] == mapW then
  320.                 stack[#stack + 1] = {sLocation[1], sLocation[2]}
  321.                 break
  322.             end
  323.         else
  324.             sLocation = table.remove(stack)
  325.         end
  326.     end
  327.     return stack
  328. end
  329.  
  330. while true do
  331. local typeList = {"Recursive", "Branching"}
  332. local termW, termH = term.getSize()
  333. local mapW, mapH = math.floor(termW/2), math.floor(termH/2)
  334. term.setBackgroundColor(colors.black)
  335. term.clear()
  336. term.setCursorPos(1, 1)
  337. term.write("Choose maze type: ")
  338. local begun = false
  339. local type = ""
  340. while not begun do
  341.         for i, v in pairs(typeList) do
  342.                 term.setCursorPos(termW/2 - #v/2 + 1, i*2 + 1)
  343.                 term.write(v)
  344.         end
  345.         local click = {os.pullEvent("mouse_click")}
  346.         for i, v in pairs(typeList) do
  347.                 if click[4] == i*2 + 1 then
  348.                         term.setBackgroundColor(colors.blue)
  349.                         term.setCursorPos(1, i*2 + 1)
  350.             term.clearLine()
  351.                         term.setCursorPos(termW/2 - #v/2 + 1, i*2 + 1)
  352.                         term.write(v)
  353.                         sleep(.2)
  354.                         begun = true
  355.                         type = v
  356.                         break
  357.                 end
  358.         end
  359. end
  360. --while true do
  361.     term.setBackgroundColor(colors.black)
  362.     term.clear()
  363.     term.setCursorPos(1, 1)
  364.     if type == "Recursive" then
  365.         mazeGenRecursive(mapW, mapH)
  366.     elseif type == "Branching" then
  367.         mazeGenBranching(mapW, mapH)
  368.     end
  369.     os.pullEvent()
  370. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement