Aadvert

Untitled

Mar 7th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. -- true: passage; false: wall; nil: unexplored
  2. local map, deadEnd, locationOfIntrest = {}, {}, {}
  3. local tX, tY = 0, 0
  4. local tFace = 0 -- tface % 4 = facing direction
  5.  
  6. local getFaceXY, getFace, getXY, resolveFace;
  7. do -- Various positional helper functions
  8.     function getFace()
  9.         return tFace % 4
  10.     end
  11.     local faces = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
  12.     function getFaceXY(nFace)
  13.         nFace = nFace or tFace
  14.         nFace = (nFace %4)+1 -- Lua, you...
  15.         local fX, fY = faces[nFace][1], faces[nFace][2]
  16.         return fX, fY
  17.     end
  18.     function getXY()
  19.         return tX, tY
  20.     end
  21.     function resolveFace(nX, nY, nFace)
  22.         nX, nY = nX or tX, nY or tY
  23.         nFace = nFace or tFace
  24.         local fX, fY = getFaceXY(nFace)
  25.         return nX + fX, nY + fY
  26.     end
  27. end
  28.  
  29. local safeMap, safeDeadEnd, safeLocationOfIntrest, safeLOI;
  30. do --Safe* functions
  31.     function safeMap(nX, nY, bState) -- Check/set the map safely
  32.         --print("SafeMap: ", nX, ", ", nY, ", ", bState)
  33.         if not map[nY] then -- Use Y, X because of terminals
  34.             map[nY] = {}
  35.         end
  36.         if bState == nil then -- We're looking up
  37.             return map[nY][nX]
  38.         end
  39.         if map[nY][nX] then
  40.             return false, map[nY][nX]
  41.             --error("Attempting to modify map.")
  42.         end
  43.         map[nY][nX] = bState
  44.         return true, bState
  45.     end
  46.  
  47.     function safeDeadEnd(nX, nY, bState)
  48.         if not deadEnd[nY] then
  49.             deadEnd[nY] = {}
  50.         end
  51.         if bState == nil then -- We're looking up
  52.             return deadEnd[nY][nX]
  53.         end
  54.         if deadEnd[nY][nX] then
  55.             return false, deadEnd[nY][nX]
  56.         end
  57.         deadEnd[nY][nX] = bState
  58.         return true, bState
  59.     end
  60.    
  61.     function safeLocationOfIntrest(nX, nY, sValue)
  62.         if not locationOfIntrest[nY] then
  63.             locationOfIntrest[nY] = {}
  64.         end
  65.         if sValue == nil then -- We're looking up
  66.             return locationOfIntrest[nY][nX]
  67.         end
  68.         local oLOI = locationOfIntrest[nY][nX]
  69.         locationOfIntrest[nY][nX] = sValue
  70.         return true, oLOI, sValue
  71.     end
  72.     safeLOI = safeLocationOfIntrest; -- shortcut for lazies
  73. end
  74.  
  75.  
  76. local render, getDrawChar;
  77. do -- Graphical functions
  78.     local rX, rY = term.getSize()
  79.     local nCenterY = math.floor(rY / 2)
  80.     local nCenterX = math.floor(rX / 2)
  81.     tLOI = {["Start"] = "S", ["End"] = "E", ["Destination"] = "D"}
  82.  
  83.     function getDrawChar(x, y)
  84.         -- D = destination, T = turtle, # = wall, * = unexplored
  85.         -- S = Start, E = end, - = closed path
  86.         local sLOI = safeLocationOfIntrest(x, y)
  87.         sLOI = tLOI[sLOI]
  88.         local sMap = safeMap(x, y)
  89.         local sTurt = (x == tX and y == tY) and "T"
  90.         if sMap then
  91.             sMap = " "
  92.         else
  93.             sMap = (sMap == false and "#") or "*"
  94.         end
  95.         return sTurt or sLOI or sMap
  96.     end
  97.  
  98.     function render() -- 36x12 default
  99.         term.clear()
  100.         for y = 1, rY do
  101.             term.setCursorPos(1, y)
  102.             local dY = (y - nCenterY) + tY  -- draw Y
  103.             for x = 1, rX do
  104.                 local dX = (x - nCenterX) + tX  -- draw X
  105.                 term.write(getDrawChar(dX, dY))
  106.             end
  107.         end
  108.     end
  109. end
  110. -- Major functions: Pathfinding, mostly
  111.  
  112. local function checkDeadEnd(nX, nY, nXOrgin, nYOrgin)
  113.     local checked = {}
  114.     local function isChecked(nX, nY, bAdd)
  115.         if not checked[nY] then
  116.             checked[nY] = {}
  117.         end
  118.         if bAdd and not checked[nY][nX] then
  119.             checked[nY][nX] = true
  120.             return false
  121.         end
  122.         return checked[nY][nX]
  123.     end
  124.     local function isDeadEnd(nX, nY, nXOrgin, nYOrgin)
  125.         local mres = safeMap(nX, nY)
  126.         if mres == nil or mres == true then -- Not a dead end!
  127.             return true
  128.         end
  129.        
  130.         -- Check faces Do one by one because asdf.
  131.         if not (nX+1 == nXOrgin and nY == nYOrgin) then
  132.             if not isChecked(nX+1, nY, true) and isDeadEnd(nX+1, nY, nX, nY) then
  133.                 return true
  134.             end
  135.         end
  136.         if not (nX-1 == nXOrgin and nY == nYOrgin) then
  137.             if not isChecked(nX-1, nY, true) and isDeadEnd(nX-1, nY, nX, nY) then
  138.                 return true
  139.             end
  140.         end
  141.         if not (nX == nXOrgin and nY+1 == nYOrgin) then
  142.             if not isChecked(nX, nY+1, true) and isDeadEnd(nX, nY+1, nX, nY) then
  143.                 return true
  144.             end
  145.         end
  146.         if not (nX == nXOrgin and nY-1 == nYOrgin) then
  147.             if not isChecked(nX, nY-1, true) and isDeadEnd(nX, nY-1, nX, nY) then
  148.                 return true
  149.             end
  150.         end
  151.         safeDeadEnd(nX, nY)
  152.         return false -- Dead end
  153.     end
  154.     return isDeadEnd(nX, nY, nXOrgin, nYOrgin)
  155. end
  156.  
  157. local function checkRedundantPaths() --TODO: Marks Loops as blocked.
  158. end
  159.  
  160. local turnLeft, turnRight, moveForward, moveBackward;
  161. do -- Movement functions
  162.     function turnLeft()
  163.         tFace = tFace + 1
  164.         turtle.turnLeft()
  165.     end
  166.    
  167.     function turnRight()
  168.         tFace = tFace - 1
  169.         turtle.turnRight()
  170.     end
  171.    
  172.     function moveForward()
  173.         local dX, dY = resolveFace()
  174.         if turtle.forward() then
  175.             tX, tY = dX, dY
  176.             safeMap(dX, dY, true)
  177.             return true
  178.         end
  179.         safeMap(dX, dY, false)
  180.         return false       
  181.     end
  182.    
  183.     function moveBackward()
  184.         local fX, fY = getFaceXY()     
  185.         if turtle.back() then
  186.             tX, tY = tX - fX, tY - fY
  187.             safeMap(tX, tY, true)
  188.             return true
  189.         end
  190.         safeMap(tX - fX, tY - fY, false)
  191.         return false
  192.     end
  193. end
  194.  
  195.    
  196. local function findMove() -- Find a suitable move
  197.    
  198. end
  199.  
  200. local function move_Block()
  201.     if turtle.down() then -- End of maze indicator
  202.         return true
  203.     end
  204.     render()
  205.     -- Turn left once, to set position so we can go forward once, turn left, follow wall, etc.
  206.     turnLeft()
  207.    
  208.     for i = 1, 3 do
  209.         if moveForward() then
  210.             if move_Block() then
  211.                 return true
  212.             end
  213.         end
  214.         turnRight()
  215.     end
  216.     -- Dead end, walk back in shame.
  217.     turnLeft()
  218.     moveBackward()
  219.     return false
  220. end
  221.  
  222.  
  223. local function main()
  224.     safeMap(tX, tY, true)
  225.     safeLOI(tX, tY, "Start")
  226.     while true do
  227.         --local path = findMove()
  228.         --displayPath(path)
  229.         move_Block()
  230.     end
  231.     print("Maze complete.")
  232. end
  233.  
  234. main()
Advertisement
Add Comment
Please, Sign In to add comment