Advertisement
CaptainSpaceCat

2D Branching Maze Generator

Jun 9th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.71 KB | None | 0 0
  1. local termW, termH = term.getSize()
  2. local debug = false
  3.  
  4. function drawRelevantImage(img, xPos, yPos)
  5.         for i, v in pairs(img) do
  6.                 for k, e in pairs(v) do
  7.                         if i + yPos - 1 >= 1 and i + yPos - 1 <= termH and k + xPos - 1 >= 1 and k + xPos - 1 <= termW then
  8.                                 term.setCursorPos(k + xPos - 1, i + yPos - 1)
  9.                                 term.setBackgroundColor(e)
  10.                                 term.write(" ")
  11.                         end
  12.                 end
  13.         end
  14. end
  15.  
  16. function win()
  17.         local termW, termH = term.getSize()
  18.         for i = 1, 7 do
  19.                 term.setCursorPos(termW/2 - 11, 1)
  20.                 term.setBackgroundColor(colors.black)
  21.                 term.setTextColor(colors.yellow)
  22.                 term.clearLine()
  23.                 term.write("You won! Congratulations!")
  24.                 sleep(.1)
  25.                 term.setCursorPos(termW/2 - 11, 1)
  26.                 term.setBackgroundColor(colors.black)
  27.                 term.setTextColor(colors.blue)
  28.                 term.clearLine()
  29.                 term.write("You won! Congratulations!")
  30.                 sleep(.1)
  31.         end
  32.         term.setBackgroundColor(colors.black)
  33.         term.clear()
  34.         term.setCursorPos(1, 1)
  35. end
  36.  
  37. function gameGen(mapW, mapH)
  38.         pLocation = math.random(1, 4)
  39.         if pLocation == 1 then
  40.                 pLocation = {2, 2}
  41.                 gLocation = {mapW*2, mapH*2}
  42.         elseif pLocation == 2 then
  43.                 pLocation = {mapW*2, 2}
  44.                 gLocation = {2, mapH*2}
  45.         elseif pLocation == 3 then
  46.                 pLocation = {2, mapH*2}
  47.                 gLocation = {mapW*2, 2}
  48.         elseif pLocation == 4 then
  49.                 pLocation = {mapW*2, mapH*2}
  50.                 gLocation = {2, 2}
  51.         end
  52.         return pLocation, gLocation
  53. end
  54.  
  55. function mazeGenBranching(mapW, mapH)
  56.     local maze = {}
  57.         for y = 1, mapH do
  58.                 maze[y] = {}
  59.                 for x = 1, mapW do
  60.                         maze[y][x] = {}
  61.                         for i = 1, 4 do
  62.                 maze[y][x][i] = true
  63.             end
  64.                 end
  65.         end
  66.         local current = {1, 1}
  67.     local list = {}
  68.     list[1] = {1, 1}   --y, x
  69.     local cellsRemaining = mapW * mapH
  70.     while cellsRemaining > 1 do
  71.         local choice = math.random(1, #list)
  72.         current = {list[choice][1], list[choice][2]}
  73.         local openWalls = {}
  74.         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
  75.             openWalls[#openWalls + 1] = 1
  76.         end
  77.         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
  78.             openWalls[#openWalls + 1] = 3
  79.         end
  80.         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
  81.             openWalls[#openWalls + 1] = 4
  82.         end
  83.         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
  84.             openWalls[#openWalls + 1] = 2
  85.         end
  86.         if #openWalls > 0 then
  87.             cellsRemaining = cellsRemaining - 1
  88.             openWalls = openWalls[math.random(1, #openWalls)]
  89.             maze[current[1]][current[2]][openWalls] = false
  90.             if openWalls == 1 then
  91.                 maze[current[1] - 1][current[2]][3] = false
  92.                 list[#list + 1] = {current[1] - 1, current[2]}
  93.             elseif openWalls == 2 then
  94.                 maze[current[1]][current[2] + 1][4] = false
  95.                 list[#list + 1] = {current[1], current[2] + 1}
  96.             elseif openWalls == 3 then
  97.                 maze[current[1] + 1][current[2]][1] = false
  98.                 list[#list + 1] = {current[1] + 1, current[2]}
  99.             elseif openWalls == 4 then
  100.                 maze[current[1]][current[2] - 1][2] = false
  101.                 list[#list + 1] = {current[1], current[2] - 1}
  102.             end
  103.         else
  104.             table.remove(list, choice)
  105.         end
  106.     end
  107.  
  108.     local gen = maze
  109.         maze = {}
  110.         for i = 1, mapH*2 + 1 do
  111.                 maze[i] = {}
  112.         end
  113.         for i = 1, mapW*2 + 1 do
  114.                 maze[1][i] = 1
  115.                 maze[mapH*2 + 1][i] = 1
  116.         end
  117.         for y = 2, mapH*2 do
  118.                 for x = 1, mapW*2 + 1 do
  119.                         if x == 1 or x == mapW*2 + 1 then
  120.                                 maze[y][x] = 1
  121.                         elseif y%2 == 0 and x%2 == 0 then
  122.                                 maze[y][x] = 2^15
  123.                         elseif y%2 == 1 and x%2 == 1 then
  124.                                 maze[y][x] = 1
  125.                         else
  126.                                 if y%2 == 0 then
  127.                                         if gen[y/2][(x - 1)/2][2] then
  128.                                                 maze[y][x] = 1
  129.                                         else
  130.                                                 maze[y][x] = 2^15
  131.                                         end
  132.                                 elseif y%2 == 1 then
  133.                                         if gen[(y - 1)/2][x/2][3] then
  134.                                                 maze[y][x] = 1
  135.                                         else
  136.                                                 maze[y][x] = 2^15
  137.                                         end
  138.                                 end
  139.                         end
  140.                 end
  141.         end
  142.         return maze
  143. end
  144.  
  145. term.setBackgroundColor(colors.black)
  146. term.clear()
  147. --local logo = mazeGenRecursive(5, 5)
  148. term.setCursorPos(termW/2 - 4, 3)
  149. term.write("Maze Game")
  150. --logo[2][2] = colors.lightBlue
  151. --logo[10][10] = colors.orange
  152. --drawRelevantImage(logo, termW/2 - 5, 6)
  153. term.setCursorPos(termW/2 - 7, termH - 2)
  154. term.setBackgroundColor(colors.black)
  155. term.setTextColor(colors.white)
  156. term.write("Press any key...")
  157. os.pullEvent("key")
  158. term.setBackgroundColor(colors.black)
  159. term.clear()
  160. term.setCursorPos(1, 1)
  161. term.write("Enter maze width: ")
  162. local mapW = read()
  163. while not tonumber(mapW) do
  164.         term.setCursorPos(1, 1)
  165.         term.clearLine()
  166.         term.write("Enter maze width: ")
  167.         mapW = read()
  168. end
  169. term.setCursorPos(1, 2)
  170. term.write("Enter maze height: ")
  171. local mapH = read()
  172. while not tonumber(mapH) do
  173.         term.setCursorPos(1, 2)
  174.         term.clearLine()
  175.         term.write("Enter maze height: ")
  176.         mapH = read()
  177. end
  178.  
  179. mapW, mapH = tonumber(mapW), tonumber(mapH)
  180. local maze = mazeGenBranching(mapW, mapH)
  181. local pLocation, gLocation = gameGen(mapW, mapH, maze)
  182. maze[gLocation[2]][gLocation[1]] = 2
  183. if debug then
  184.         paintutils.drawImage(maze, 1, 1)
  185.         term.setCursorPos(pLocation[1], pLocation[2])
  186.         term.setBackgroundColor(colors.lightBlue)
  187.         term.write(" ")
  188. end
  189.  
  190. term.setBackgroundColor(colors.black)
  191. term.setTextColor(colors.white)
  192. term.clear()
  193. term.setCursorPos(1, 1)
  194. print("You are the blue square...")
  195. term.setCursorPos(termW/2 + 1, termH/2 + 1)
  196. term.setBackgroundColor(colors.lightBlue)
  197. term.write(" ")
  198. term.setCursorPos(termW/2 - 7, termH - 1)
  199. term.setBackgroundColor(colors.black)
  200. term.setTextColor(colors.white)
  201. term.write("Press any key...")
  202. os.pullEvent("key")
  203.  
  204. term.setBackgroundColor(colors.black)
  205. term.setTextColor(colors.white)
  206. term.clear()
  207. term.setCursorPos(1, 1)
  208. print("Get to the orange square...")
  209. term.setCursorPos(termW/2 + 1, termH/2 + 1)
  210. term.setBackgroundColor(colors.orange)
  211. term.write(" ")
  212. term.setCursorPos(termW/2 - 7, termH - 1)
  213. term.setBackgroundColor(colors.black)
  214. term.setTextColor(colors.white)
  215. term.write("Press any key...")
  216. os.pullEvent("key")
  217. while true do
  218.   term.setBackgroundColor(colors.black)
  219.   term.clear()
  220.   --term.setCursorPos(10, 1)
  221.   --write(pLocation[1], " ", tostring(pLocation[2]))
  222.   drawRelevantImage(maze, termW/2 - pLocation[1] + 2, termH/2 - pLocation[2] + 2)
  223.   term.setBackgroundColor(colors.lightBlue)
  224.   term.setCursorPos(termW/2 + 1, termH/2 + 1)
  225.   term.write(" ")
  226.   if pLocation[1] == gLocation[1] and pLocation[2] == gLocation[2] then
  227.     win()
  228.     break
  229.   end
  230.   local events = {os.pullEvent()}
  231.   if events[1] == "key" then
  232.     if events[2] == 200 then
  233.       if maze[pLocation[2] - 1][pLocation[1]] ~= 1 then
  234.         pLocation[2] = pLocation[2] - 1
  235.       end
  236.     elseif events[2] == 203 then
  237.       if maze[pLocation[2]][pLocation[1] - 1] ~= 1 then
  238.         pLocation[1] = pLocation[1] - 1
  239.       end
  240.     elseif events[2] == 208 then
  241.       if maze[pLocation[2] + 1][pLocation[1]] ~= 1 then
  242.         pLocation[2] = pLocation[2] + 1
  243.       end
  244.     elseif events[2] == 205 then
  245.       if maze[pLocation[2]][pLocation[1] + 1] ~= 1 then
  246.         pLocation[1] = pLocation[1] + 1
  247.       end
  248.     end
  249.   end
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement