Advertisement
Guest User

dig

a guest
Jul 6th, 2013
3,506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. ----------------------------------------------------------
  2. --                 Stripmining turtle                   --
  3. ----------------------------------------------------------
  4. --                   Version 1.3.2                      --
  5. ----------------------------------------------------------
  6. -- Makes the turtle mine straight down to the bedrock.  --
  7. -- Usege: dig <max Y> <length>                          --
  8. -- <max Y> - maximum height for mining                  --
  9. -- <length> - number of blocks to mine forward          --
  10. -- (return) - go back to the starting point.            --
  11. ----------------------------------------------------------
  12. -- Based on a script pastebin.com/JbFMHaNg by Eonsv     --
  13. -- Tested with CC 1.5                                   --
  14. -- Don't forget to use chunkloaders!                    --
  15. ----------------------------------------------------------
  16.  
  17. --Tests wether a file with the filename name exists.
  18. --Returns true if it does and false if not
  19. function fileExists(name)
  20.    local f = io.open(name,'r')
  21.    if (f ~= nil) then
  22.         f:close()
  23.         return true
  24.     else
  25.         return false
  26.     end
  27. end
  28.  
  29. local arg = {...}
  30.  
  31. --Saves current position to file
  32. function save()
  33.     local str = ''
  34.     str = str..y..'\n'
  35.     str = str..x..'\n'
  36.     str = str..distance..'\n'
  37.     local file = io.open('save', 'w')
  38.     file:write(str)
  39.     file:close()
  40. end
  41.  
  42. --Consumes a stack of fuel from the 2nd enderchest
  43. function refuel(fuel)
  44.     if(turtle.getFuelLevel() >= fuel) then
  45.         return true
  46.     end
  47.     print('Refueling')
  48.     turtle.select(2)
  49.     while(not turtle.placeUp()) do
  50.         turtle.digUp()
  51.         turtle.attackUp()
  52.     end
  53.     turtle.select(16)
  54.     while(not turtle.suckUp()) do
  55.         sleep('No fuel found. Sleeping.', 10)
  56.     end
  57.     turtle.refuel(64)
  58.     turtle.select(2)
  59.     turtle.digUp()
  60.     if(turtle.getFuelLevel() <= fuel) then
  61.         sleep('Refueling failed!', 10)
  62.         dump()
  63.         refuel(200)
  64.     end
  65. end
  66.  
  67. --Dumps all items into the 1st enderchest
  68. function dump()
  69.     turtle.select(1)
  70.     while(not turtle.placeUp()) do
  71.         turtle.digUp()
  72.         turtle.attackUp()
  73.     end
  74.     for i = 3, 16 do
  75.         if(turtle.getItemCount(i) > 0) then
  76.             turtle.select(i)
  77.             while(not turtle.dropUp()) do
  78.                 sleep('Dropoff chest is full. Sleeping.', 10)
  79.             end
  80.         end
  81.     end
  82.     turtle.select(1)
  83.     turtle.digUp()
  84.     print('Dropoff successful')
  85. end
  86.  
  87. function dropoff()
  88.     local empty = 0
  89.     --Calculates number of empty slots
  90.     for i = 2, 16 do
  91.         if(turtle.getItemCount(i) == 0) then
  92.             empty = empty + 1
  93.         end
  94.     end
  95.     --Dumps items if the inventory is full
  96.     if(empty == 0) then
  97.         dump()
  98.     end
  99. end
  100.  
  101. --Clears the screen
  102. function clear()
  103.     term.clear()
  104.     term.setCursorPos(1, 1)
  105. end
  106.  
  107. --Sleeps for %time% seconds, while displaying %text% and counting down
  108. function sleep(text, time)
  109.     for i = 0, time - 1 do
  110.         clear()
  111.         print(text)
  112.         print('Countdown: '..time - i..'s.')
  113.         os.sleep(1)
  114.     end
  115. end
  116.    
  117. --Returns true if successful, and false if the block is bedrock
  118. local function bedrockTest()
  119.     local test
  120.     test = turtle.down()
  121.     return test
  122. end
  123.  
  124. --Moves up until reaching initial height
  125. function moveUp()
  126.     for i = 1, y do
  127.         refuel(200)
  128.         while(not turtle.up()) do
  129.             turtle.attackUp()
  130.             turtle.digUp()
  131.         end
  132.     end
  133. end
  134.  
  135. --Digging down until bedrock
  136. function digDown()
  137.     local fail = 0
  138.     while(true) do
  139.         dropoff()
  140.         refuel(500)
  141.         turtle.digDown()
  142.         if(not bedrockTest()) then
  143.                 while(turtle.detectDown() == false) do
  144.                     turtle.attackDown()
  145.                     turtle.down()
  146.                 end
  147.             os.sleep(0.2)
  148.             fail = fail + 1
  149.             if(fail == 6) then
  150.                 break
  151.             end
  152.         end
  153.     end
  154. end
  155.  
  156. --Mining loop
  157. function loop()
  158.     while(x > 0) do
  159.         clear()
  160.         print('Blocks left to mine: '..x)
  161.         digDown()
  162.         print('Hit bedrock, moving up '..y..' blocks')
  163.         moveUp()
  164.         while(not turtle.forward()) do
  165.             turtle.attack()
  166.             turtle.dig()
  167.         end
  168.         dump()
  169.         x = x - 1
  170.         save()
  171.     end
  172. end
  173.  
  174. --Init sequence
  175. function init()
  176.     clear()
  177.     y = tonumber(arg[1])
  178.     y = y - 1
  179.     x = tonumber(arg[2])
  180.     if(arg[3] ~= nil) then
  181.         distance = x
  182.         print('Return mode on, distance: '..distance..' blocks')
  183.     else distance = 0
  184.     end
  185.     while(turtle.getItemCount(1) ~= 1) do
  186.         sleep('Wrong number of items in slot 1. Place one dropoff ender chest.', 5)
  187.     end
  188.     while(turtle.getItemCount(2) ~= 1) do
  189.         sleep('Wrong number of items in slot 2. Place one fuel ender chest.', 5)
  190.     end
  191.     save()
  192.     -- Creating startup file to continue mining after server restarts
  193.     if(not fileExists('startup')) then
  194.         local file = io.open('startup', 'w')
  195.         file:write("shell.run(\'dig\')")
  196.         file:close()
  197.     end
  198.     loop()
  199. end
  200.  
  201. --------
  202. --MAIN--
  203. --------
  204. if(not fileExists('startup')) then
  205.     for i = 1, 2 do
  206.         if(arg[i] == nil) then
  207.             clear()
  208.             print('Usage: dig <max Y> <length> return')
  209.             print()
  210.             print('<max Y> - maximum height for mining (e.g. your Y coordinate).')
  211.             print('<length> - number of blocks to mine forward.')
  212.             print('return - optional command to make the turtle go back to the starting point.')
  213.             do return end
  214.         end
  215.     end
  216.     init()
  217. else
  218.     clear()
  219.     --Reading save file
  220.     local file = io.open('save', 'r')
  221.     y = tonumber(file:read('*l'))
  222.     x = tonumber(file:read('*l'))
  223.     distance = tonumber(file:read('*l'))
  224.     file:close()
  225.     --If rebooted while dumping items
  226.     if(turtle.getItemCount(1) == 0) then
  227.         turtle.select(1)
  228.         turtle.digUp()
  229.         dump()
  230.         while(turtle.getItemCount(1) == 0) do
  231.             sleep('Missing chest in slot 1.', 10)
  232.         end
  233.     end
  234.     --If rebooted while refueling
  235.     if(turtle.getItemCount(2) == 0) then
  236.         turtle.select(2)
  237.         turtle.digUp()
  238.         refuel(500)
  239.         while(turtle.getItemCount(1) == 0) do
  240.             sleep('Missing chest in slot 2.', 10)
  241.         end
  242.     end
  243.     loop()
  244. end
  245.  
  246. --Finishing
  247. shell.run('delete', 'save')
  248. shell.run('delete', 'startup')
  249. print('Done!')
  250.  
  251. --Going to the starting point
  252. if distance > 0 then
  253.     turtle.turnRight()
  254.     turtle.turnRight()
  255.         while(distance > 0) do
  256.         clear()
  257.         print('Going back '..distance..' blocks')
  258.         while(not turtle.forward()) do
  259.             turtle.attack()
  260.             turtle.dig()
  261.         end
  262.         distance = distance - 1
  263.     end
  264. print('Done!')
  265. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement