Advertisement
pferguson2212

ManicMiner2

Feb 1st, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. print("How deep?:")
  2. local depth = tonumber(read())
  3. print("How wide?:")
  4. local width = tonumber(read())
  5. print("How long?:")
  6. local length = tonumber(read())
  7.  
  8. local complete = 0
  9. local curX = 1
  10. local curY = 1
  11. local curZ = 1
  12. local dir = 1
  13.  
  14. local lastX = 1
  15. local lastY = 1
  16. local lastZ = 1
  17.  
  18. local check1 = true
  19. local check2 = true
  20. local check3 = true
  21. local check4 = true
  22.  
  23. local debug = true
  24.  
  25. function turnLeft()
  26.     turtle.turnLeft()
  27.     dir = dir - 1
  28.     if dir < 1 then dir = 4 end
  29.    
  30.     if debug then
  31.         if dir == 1 then print("Turned left, facing forward") end
  32.         if dir == 2 then print("Turned left, facing right") end
  33.         if dir == 3 then print("Turned left, facing backward") end
  34.         if dir == 4 then print("Turned left, facing left") end
  35.     end
  36. end
  37.  
  38. function turnRight()
  39.     turtle.turnRight()
  40.     dir = dir + 1
  41.     if dir > 4 then dir = 1 end
  42.    
  43.     if debug == 1 then
  44.         if dir == 1 then print("Turned right, facing forward") end
  45.         if dir == 2 then print("Turned right, facing right") end
  46.         if dir == 3 then print("Turned right, facing backward") end
  47.         if dir == 4 then print("Turned right, facing left") end
  48.     end
  49. end
  50.  
  51. function moveDown()
  52.     if debug then print("Moving down") end
  53.    
  54.     if curZ > depth then
  55.         if debug then print("Reached required depth.") end
  56.         return false
  57.     end
  58.     if not turtle.down() then
  59.         if debug then print("Failed to move down, blocked.") end
  60.         return false
  61.     end
  62.    
  63.     curZ = curZ + 1
  64.     if debug then print("Moved down, now at level "..curZ..".") end
  65.     return true
  66. end
  67.  
  68. function moveUp()
  69.     if debug then print("Moving up") end
  70.    
  71.     if curZ <= 1 then
  72.         if debug then print("Surfaced.") end
  73.         return false
  74.     end
  75.     if not turtle.up() then
  76.         if debug then print("Failed to move up, blocked.") end
  77.         return false
  78.     end
  79.    
  80.     curZ = curZ - 1
  81.     if debug then print("Moved up, now at level "..curZ..".") end
  82.     return true
  83. end
  84.  
  85. function moveForward()
  86.     if curX == 1 and dir == 3 then
  87.         if debug then print("Failed to move forward, mining limit reached.") end
  88.         return false
  89.     end
  90.     if curX == length and dir == 1 then
  91.         if debug then print("Failed to move forward, mining limit reached.") end
  92.         return false
  93.     end
  94.     if curY == 1 and dir == 4 then
  95.         if debug then print("Failed to move forward, mining limit reached.") end
  96.         return false
  97.     end
  98.     if curY == width and dir == 2 then
  99.         if debug then print("Failed to move forward, mining limit reached.") end
  100.         return false
  101.     end
  102.    
  103.     if not turtle.forward() then
  104.         if debug then print("Failed to move forward, blocked.") end
  105.         return false
  106.     end
  107.  
  108.     if dir == 1 then curX = curX + 1 end
  109.     if dir == 2 then curY = curY + 1 end
  110.     if dir == 3 then curX = curX - 1 end
  111.     if dir == 4 then curY = curY - 1 end
  112.  
  113.     if debug then print(curX..","..curY.."(X,Y)") end
  114.     return true
  115. end
  116.  
  117. function nextLevel()
  118.     if debug then print("Moving to next level") end
  119.     if curX ~= 1 or curY ~= 1 or dir ~= 1 then
  120.         -- Moving to 1,1 position
  121.         while dir ~= 3 do turnRight() end
  122.         while curX > 1 do moveForward() end
  123.         turnRight()
  124.         while curY > 1 do moveForward() end
  125.         turnRight()
  126.     end
  127.     for d = 1, 3 do
  128.         if turtle.detectDown() then
  129.             if debug then print("Digging down") end
  130.             if not turtle.digDown() then
  131.                 print("Failed to dig down, bedrock.")
  132.                 return false
  133.             end
  134.         end
  135.         if debug then print("Moving down") end
  136.         if not moveDown() then
  137.             print("Failed to move down, blocked.")
  138.             return false
  139.         end
  140.     end
  141.     return true
  142. end
  143.  
  144. function itemCheck()
  145.     if debug then print("Checking available exclusions") end
  146.     if turtle.getItemCount(1) == 0 then check1 = false end
  147.     if turtle.getitemCount(2) == 0 then check2 = false end
  148.     if turtle.getItemCount(3) == 0 then check3 = false end
  149.     if turtle.getItemCount(4) == 0 then check4 = false end
  150. end
  151.  
  152. function checkUp()
  153.     if check1 then
  154.         turtle.select(1)
  155.         if turtle.compareUp() then return false end
  156.     end
  157.     if check2 then
  158.         turtle.select(2)
  159.         if turtle.compareUp() then return false end
  160.     end
  161.     if check3 then
  162.         turtle.select(3)
  163.         if turtle.compareUp() then return false end
  164.     end
  165.     if check4 then
  166.         turtle.select(4)
  167.         if turtle.compareUp() then return false end
  168.     end
  169.     return true
  170. end
  171.  
  172. function checkDown()
  173.     if check1 then
  174.         turtle.select(1)
  175.         if turtle.compareDown() then return false end
  176.     end
  177.     if check2 then
  178.         turtle.select(2)
  179.         if turtle.compareDown() then return false end
  180.     end
  181.     if check3 then
  182.         turtle.select(3)
  183.         if turtle.compareDown() then return false end
  184.     end
  185.     if check4 then
  186.         turtle.select(4)
  187.         if turtle.compareDown() then return false end
  188.     end
  189.     return true
  190. end
  191.  
  192. function mine()
  193.     if checkUp() then turtle.digUp() end
  194.     if checkDown() then turtle.digDown() end
  195. end
  196.  
  197. while complete == 0 do
  198.     if not nextLevel() then
  199.         complete = 1
  200.     else
  201.         for y = 1, width do
  202.             for x = 1, length - 1 do
  203.                 mine()
  204.                 if turtle.detect() then turtle.dig() end
  205.                 moveForward()
  206.             end
  207.             mine()
  208.             if y~= width then
  209.                 if dir == 1 then
  210.                     turnRight()
  211.                     turtle.dig()
  212.                     moveForward()
  213.                     turnRight()
  214.                 else
  215.                     if dir == 3 then
  216.                         turnLeft()
  217.                         turtle.dig()
  218.                         moveForward()
  219.                         turnLeft()
  220.                     end
  221.                 end
  222.             end
  223.         end
  224.     end
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement