Advertisement
Guest User

dig

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