Advertisement
dcx_

cutter test

Mar 31st, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. local w = 10
  2. local l = 15
  3. local h = 8
  4. local cur_x = 5
  5. local cur_y = 0
  6. local cur_z = 5
  7. local storage_x = 5
  8. local storage_y = 1
  9. local storage_z = 6
  10.  
  11. function Refuel()
  12.     if(turtle.getFuelLevel() <= 400) then
  13.         turtle.select(1)
  14.         local fuel = turtle.getItemCount(1) - 1
  15.         print("Refueling " .. fuel .. " units")
  16.         turtle.refuel(fuel)
  17.     end
  18. end
  19.  
  20. function Mine(blocks)
  21.     --print("Mining " .. blocks .. " blocks")
  22.     for y = 1,blocks,1 do
  23.         while(turtle.detect()) do
  24.             turtle.dig()
  25.             sleep(0.5)
  26.         end
  27.         turtle.forward()
  28.         cur_y = cur_y + 1
  29.         while(turtle.detectUp()) do
  30.             turtle.digUp()
  31.             sleep(0.5)
  32.         end
  33.         turtle.digDown()
  34.     end
  35. end
  36.  
  37. function checkLevelFinished()
  38.     if(cur_x == w) then
  39.         print("Finished level, heading home")
  40.         if(cur_x % 2 == 0) then
  41.             turtle.turnRight()
  42.             turtle.turnRight()
  43.             Dump()
  44.         else
  45.             turtle.turnRight()
  46.             turtle.turnRight()
  47.             for i = 1,l-1,1 do
  48.                 turtle.forward()
  49.                 cur_y = cur_y - 1
  50.             end
  51.             turtle.turnLeft()
  52.             turtle.turnLeft()
  53.             Dump()
  54.         end
  55.         return true
  56.     else
  57.         return false
  58.     end
  59. end
  60.  
  61. function TryDumping()
  62.     if(turtle.getItemCount(15) > 0) then
  63.         Dump()
  64.         GoTo(cur_x - storage_x, cur_z - storage_z)
  65.         return true
  66.     else
  67.         return false
  68.     end
  69. end
  70.  
  71. function GoTo(x, z)
  72.    
  73.     if(z > 0) then
  74.         while(z > 0) do
  75.             if(turtle.detectDown()) then
  76.                 turtle.digDown()
  77.             end
  78.             turtle.down()
  79.             z = z - 1
  80.         end
  81.     else
  82.         while(z < 0) do
  83.             if(turtle.detectUp()) then
  84.                 turtle.digUp()
  85.             end
  86.             turtle.up()
  87.             z = z + 1
  88.         end
  89.     end
  90.    
  91.     if(x > 0) then
  92.         turtle.turnRight()
  93.         while(x > 0) do
  94.             if(turtle.detect()) then
  95.                 turtle.dig()
  96.             end
  97.             turtle.forward()
  98.             x = x - 1
  99.         end
  100.     else
  101.         turtle.turnLeft()
  102.         while(x < 0) do
  103.             if(turtle.detect()) then
  104.                 turtle.dig()
  105.             end
  106.             turtle.forward()
  107.             x = x + 1
  108.         end
  109.     end
  110. end
  111.  
  112. function Dump()
  113.     print("Going home to dump items into storage")
  114.     local offset_x = cur_x - storage_x
  115.     if(offset_x < 0) then
  116.         offset_x = offset_x * (-1)
  117.         turtle.turnRight()
  118.     else
  119.         turtle.turnLeft()
  120.     end
  121.     for i = offset_x,2,-1 do
  122.         turtle.forward()
  123.     end
  124.     local offset_z = cur_z - storage_z
  125.     if(offset_z < 0) then
  126.         offset_z = offset_z * (-1)
  127.         for i = offset_z,1,-1 do
  128.             turtle.down()
  129.         end
  130.     else
  131.         for i = offset_z,0,-1 do
  132.             turtle.up()
  133.         end
  134.     end
  135.     if(cur_x - storage_x < 0) then
  136.         turtle.turnRight()
  137.     else
  138.         turtle.turnLeft()
  139.     end
  140.     print("Dumping items into storage")
  141.     local turned = false
  142.     local i = 16
  143.     while(i >= 2) do
  144.         turtle.select(i)
  145.         while(turtle.getItemCount(i) <= 0) do
  146.             i = i - 1
  147.             turtle.select(i)
  148.         end
  149.         local item = string.sub(turtle.getItemDetail(i).name, 11)
  150.         if(item == "coal") then
  151.             turtle.transferTo(1, turtle.getItemCount(i))
  152.         else
  153.             turtle.drop()
  154.         end
  155.         i = i - 1
  156.     end
  157.     turtle.turnRight()
  158.     turtle.turnRight()
  159. end
  160.  
  161. function RotateRight()
  162.     --print("Rotating right")
  163.     turtle.turnRight()
  164.     Mine(1)
  165.     turtle.turnRight()
  166. end
  167.  
  168. function RotateLeft()
  169.     --print("Rotating left")
  170.     turtle.turnLeft()
  171.     Mine(1)
  172.     turtle.turnLeft()
  173. end
  174.  
  175. function Run()
  176.  
  177.     while(cur_z > 2) do
  178.         if(turtle.detectUp()) then
  179.             turtle.digUp()
  180.         end
  181.         turtle.up()
  182.         cur_z = cur_z - 1
  183.     end
  184.     if(turtle.detect()) then
  185.         turtle.dig()
  186.     end
  187.     turtle.forward()
  188.     turtle.turnLeft()
  189.     while(cur_x > 1) do
  190.         if(turtle.detect()) then
  191.             turtle.dig()
  192.         end
  193.         turtle.forward()
  194.         cur_x = cur_x - 1
  195.     end
  196.     turtle.turnRight()
  197.            
  198.     while(cur_z <= h - 1) do
  199.         print("Starting to mine levels " .. cur_z - 1 .. ", " .. cur_z .. ", " .. cur_z + 1 .. " of " .. h)
  200.         while(cur_x <= w) do
  201.             Refuel()
  202.             Mine(l - 1)
  203.             if(checkLevelFinished()) then break end
  204.             RotateRight()
  205.             cur_x = cur_x + 1
  206.             Refuel()
  207.             Mine(l - 1)
  208.             if(checkLevelFinished()) then break end
  209.             RotateLeft()
  210.             TryDumping()
  211.         end
  212.        
  213.         if(cur_z == h - 1) then
  214.             print("Finished work, heading home")
  215.             Dump()
  216.             turtle.up()
  217.             turtle.forward()
  218.             turtle.turnRight(2)
  219.             break
  220.         end
  221.        
  222.         GoTo(1, cur_z + 2)
  223.        
  224.         if(h % 2 == 0) then
  225.             turtle.down()
  226.             turtle.digDown()
  227.             cur_z = cur_z + 1
  228.         end
  229.         cur_x = 1
  230.     end
  231.    
  232.     print("Finished mining a " .. w .. "x" .. l .. "x" .. h .. " area")
  233. end
  234.  
  235. Run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement