Advertisement
Guest User

dig

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