surazal

DaveMinerBeta

May 2nd, 2014
2,674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. xpos = 0
  2. ypos = 0
  3. zpos = 0
  4. direction = 0
  5.  
  6. origXpos = xpos
  7. origYpos = ypos
  8. origZpos = zpos
  9. origDirection = direction
  10.  
  11. print("How many slots do you want to reserve")
  12. print("for blocks you want the turtle to")
  13. print("ingore? (NOTE: Now's a good time to")
  14. print("check if you put your 'ingore blocks'")
  15. print("into the turtle). Enter 'q' to abort")
  16. write("Enter> ")
  17. numIgnoreSlots = tonumber(io.read())
  18.  
  19. if type(numIgnoreSlots) ~= "number" then
  20.     error("Invalid number of slots specified")
  21. elseif numIgnoreSlots > 8 or numIgnoreSlots < 0 then
  22.     error("Please enter a number between 0 and 8")
  23. end
  24.  
  25. print("How large an area do you want to mine?")
  26. write("Enter> ")
  27. gridSize = tonumber(io.read())
  28.  
  29. if type(gridSize) ~= "number" then
  30.     gridSize = 10
  31. elseif gridSize < 1 then
  32.     error("Grid size must be a positive integer greater than 1")
  33. end
  34.  
  35. print("Setting grid size to "..gridSize.." by "..gridSize.." blocks")
  36.  
  37. function turnLeft()
  38.     direction = (direction - 1) % 4
  39.     turtle.turnLeft()
  40. end
  41.  
  42. function turnRight()
  43.     direction = (direction + 1) % 4
  44.     turtle.turnRight()
  45. end
  46.  
  47. function forward()
  48.     local isNotMoved = true
  49.  
  50.     while isNotMoved do
  51.         if turtle.forward() then
  52.             isNotMoved = false
  53.         else
  54.             sleep(1)
  55.         end
  56.     end
  57.  
  58.     if direction == 0 then
  59.         xpos = xpos + 1
  60.     elseif direction == 1 then
  61.         zpos = zpos + 1
  62.     elseif direction == 2 then
  63.         xpos = xpos - 1
  64.     else
  65.         zpos = zpos - 1
  66.     end
  67. end
  68.  
  69. function down()
  70.     local isNotMoved = true
  71.  
  72.     while isNotMoved do
  73.         if turtle.down() then
  74.             isNotMoved = false
  75.         else
  76.             sleep(1)
  77.         end
  78.     end
  79.  
  80.     ypos = ypos - 1
  81. end
  82.  
  83. function up()
  84.     local isNotMoved = true
  85.  
  86.     while isNotMoved do
  87.         if turtle.up() then
  88.             isNotMoved = false
  89.         else
  90.             sleep(1)
  91.         end
  92.     end
  93.  
  94.     ypos = ypos + 1
  95. end
  96.  
  97. function isInventoryFull()
  98.     local retval = true
  99.     local inventorySlot = 0
  100.  
  101.     for inventorySlot = numIgnoreSlots + 1, 16 do
  102.         if turtle.getItemCount(inventorySlot) == 0 then
  103.             retval = false
  104.         end
  105.     end
  106.  
  107.     return retval
  108. end
  109.  
  110. function dropoff(doReturn)
  111.     -- Remember where we were
  112.     local returnXpos = xpos
  113.     local returnYpos = ypos
  114.     local returnZpos = zpos
  115.     local returnDirection = direction
  116.  
  117.     -- This returns us to the chest
  118.     for i = ypos, origYpos - 1 do
  119.         if turtle.detectUp() then
  120.             turtle.digUp()
  121.         end
  122.  
  123.         up()
  124.     end
  125.  
  126.     while direction ~= 3 do
  127.         turnRight()
  128.     end
  129.  
  130.     for i = origZpos, zpos - 1 do
  131.         if turtle.detect() then
  132.             turtle.dig()
  133.         end
  134.  
  135.         forward()
  136.     end
  137.  
  138.     turnLeft()
  139.  
  140.     for i = origXpos, xpos - 1 do
  141.         if turtle.detect() then
  142.             turtle.dig()
  143.         end
  144.  
  145.         forward()
  146.     end
  147.  
  148.     -- Clean out the first 8 slots of all except one item
  149.     for i = 1, numIgnoreSlots do
  150.         turtle.select(i)
  151.         turtle.drop(turtle.getItemCount(i) - 1)
  152.     end
  153.  
  154.     -- Drop off everything in slots 9-16 into the chest
  155.     for i = numIgnoreSlots + 1, 16 do
  156.         turtle.select(i)
  157.         turtle.drop()
  158.     end
  159.  
  160.     turtle.select(1)
  161.  
  162.     -- Head back to where we were before
  163.     turnRight()
  164.     turnRight()
  165.  
  166.     if doReturn then
  167.         for i = xpos, returnXpos - 1 do
  168.             if turtle.detect() then
  169.                 turtle.dig()
  170.             end
  171.  
  172.             forward()
  173.         end
  174.  
  175.         turnRight()
  176.  
  177.         for i = zpos, returnZpos - 1 do
  178.             if turtle.detect() then
  179.                 turtle.dig()
  180.             end
  181.  
  182.             forward()
  183.         end
  184.  
  185.         for i = returnYpos, ypos - 1 do
  186.             if turtle.detectDown() then
  187.                 turtle.digDown()
  188.             end
  189.  
  190.             down()
  191.         end
  192.  
  193.         while direction ~= returnDirection do
  194.             turnRight()
  195.         end
  196.     end
  197. end
  198.  
  199. function dropoffIfInventoryFull()
  200.     if isInventoryFull() then
  201.         dropoff(true)
  202.     end
  203. end
  204.  
  205. function digDown()
  206.     local isNotImpenetrable = true
  207.  
  208.     if turtle.detectDown() and not turtle.digDown() then
  209.         isNotImpenetrable = false
  210.     end
  211.  
  212.     dropoffIfInventoryFull()
  213.  
  214.     return isNotImpenetrable
  215. end
  216.  
  217. function digUp()
  218.     local isNotImpenetrable = true
  219.  
  220.     if turtle.detectUp() and not turtle.digUp() then
  221.         isNotImpenetrable = false
  222.     end
  223.  
  224.     dropoffIfInventoryFull()
  225.  
  226.     return isNotImpenetrable
  227. end
  228.  
  229. function digForward()
  230.     local isNotImpenetrable = true
  231.  
  232.     if turtle.detect() and not turtle.dig() then
  233.         isNotImpenetrable = false
  234.     end
  235.  
  236.     dropoffIfInventoryFull()
  237.  
  238.     return isNotImpenetrable
  239. end
  240.  
  241. function digForwardConditionally()
  242.     local isDiggable = true
  243.     local inventorySlot = 0
  244.  
  245.     for inventorySlot = 1, numIgnoreSlots do
  246.         turtle.select(inventorySlot)
  247.  
  248.         if turtle.compare() then
  249.             isDiggable = false
  250.         end
  251.     end
  252.  
  253.     turtle.select(1)
  254.  
  255.     if isDiggable then 
  256.         turtle.dig()
  257.     end
  258.  
  259.     dropoffIfInventoryFull()
  260. end
  261.  
  262. function doProcessCoord()
  263.     local retval = false
  264.  
  265.     if (xpos + 2 * zpos - 1) % 5 == 0 then
  266.         retval = true
  267.     end
  268.  
  269.     return retval
  270. end
  271.  
  272. function mineCoord()
  273.     while digDown() do
  274.         down()
  275.  
  276.         for i = 1, 4 do
  277.             turnRight()
  278.             digForwardConditionally()
  279.         end
  280.     end
  281.  
  282.     for i = ypos, origYpos - 1 do
  283.         if turtle.detectUp() then
  284.             turtle.digUp()
  285.         end
  286.  
  287.         up()
  288.     end
  289. end
  290.  
  291. function chompForward()
  292.     digForward()
  293.     forward()
  294.     digUp()
  295. end
  296.  
  297. function testerFunc()
  298.     turtle.placeUp()
  299. end
  300.  
  301. function mainLoop()
  302.     local isNotCompleted = true
  303.  
  304.     while isNotCompleted do
  305.         if doProcessCoord() then
  306.             mineCoord()
  307.             -- testerFunc()
  308.             dropoff(true)
  309.         end
  310.  
  311.         -- Now we process our move to the next block and start again
  312.             if zpos % 2 == 0 then
  313.                     if xpos == gridSize - 1 then
  314.                     if zpos == gridSize - 1 then
  315.                             isNotCompleted = false
  316.                 else
  317.                     turnRight()
  318.                     chompForward()
  319.                     turnRight()
  320.                     end
  321.             else
  322.                 chompForward()
  323.                     end
  324.             else
  325.                     if xpos == 0 then
  326.                     if zpos == gridSize - 1 then
  327.                             isNotCompleted = false
  328.                 else
  329.                     turnLeft()
  330.                     chompForward()
  331.                     turnLeft()
  332.                     end
  333.             else
  334.                 chompForward()
  335.                     end
  336.             end
  337.     end
  338.  
  339.     dropoff(false)
  340. end
  341.  
  342. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment