Advertisement
Tander

computercraft mining

Dec 31st, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. --[[
  2. 1 coal enough for a length of 19 to 96 "meters"
  3. (it depends on the amount of liquid on the way)
  4.  
  5. Slots:
  6. 1  - diamond ore or another block that turtle must avoid
  7. 2  - filler block for making tonnels through liquids
  8. 3  - torches
  9. 4  - chests for unload mined resources
  10. 16 - coal or another fuel
  11.  
  12. Flags:
  13. nochest, nc - program will be terminated if turtle's inventory is full.
  14. passliquid, pl - turle will be digging in spite of liquids around.
  15. fastmode, fm - disable a few difficult checks
  16. --]]
  17.  
  18. -- config --
  19. length = 40 --length of tonnel by default
  20. faillimit = 10
  21.  
  22. -- initialisation --
  23. local args = { ... }
  24. arg1 = tonumber(args[1])
  25.  
  26. if args[1] ~= nil then
  27.     if arg1 ~= nil then
  28.         length = arg1
  29.     elseif (args[1] == 'help') or (args[1] == '?') or (args[1] == '/?') then
  30.         print('Usage: mine <(length_of_tonnel)> [flags]')
  31.         print('type "mine flags" for more about flags')
  32.         print('Slots:')
  33.         print('1  - block that turtle shouldn\'t destroy (e.g. diamond ore)')
  34.         print('2  - block for making tonnels through liquids (e.g. glass)')
  35.         print('3  - torches')
  36.         print('4  - chests for unload mined resources')
  37.         print('16 - coal or another fuel [OPTIONAL]')
  38.         do return end
  39.     elseif  (args[1] == 'flags') or (args[1] == 'f') then
  40.         print('nochest, nc - program will be terminated if turtle\'s inventory is full.')
  41.         print('passliquid, pl - turle will be digging in spite of liquids around.')
  42.         print('fastmode, fm - disable a few difficult checks')
  43.         do return end
  44.     else
  45.         print('Usage: mine [(length_of_tonnel)] [flags]')
  46.         print('type "mine help" for full help')
  47.         do return end
  48.     end
  49. end
  50.  
  51. for a,content in ipairs(args) do
  52.   if (content == 'nochest') or (content == 'nc') then nochest = true end
  53.   if (content == 'passliquid') or (content == 'pl') then passliquid = true end
  54.   if (content == 'fastmode') or (content == 'fm') then fastmode = true else fastmode = false end
  55. end
  56.  
  57. turtle.select(16)
  58. turtle.refuel()
  59. print("fuel level: ", turtle.getFuelLevel())
  60. turtle.select(1)
  61. i = 0
  62. failCount = 0
  63. height = 0
  64. torch = 0
  65.  
  66. -- code --
  67. function placeLeft()
  68.     turtle.turnLeft()
  69.     local flag = turtle.place()
  70.     turtle.turnRight()
  71.     return flag
  72. end
  73.  
  74. function placeRight()
  75.     turtle.turnRight()
  76.     local flag = turtle.place()
  77.     turtle.turnLeft()
  78.     return flag
  79. end
  80.  
  81. function blockLiquid()
  82.     if passliquid then
  83.         print('Liquid detected! Ingoring due to "passliqid" flag...')
  84.     else
  85.     print('Liquid detected! Trying to fix...')
  86.     if turtle.getItemCount(2) < 8 then
  87.         print('Filler block running out! Refill slot 2 and restart program.')
  88.         do return 'blockingfail' end
  89.     end
  90.    
  91.     turtle.select(2)
  92.     if turtle.up() then height = height + 1 end
  93.     turtle.placeUp()
  94.     placeLeft()
  95.     placeRight()
  96.     if turtle.down() then height = height - 1 end
  97.     placeLeft()
  98.     placeRight()
  99.     if turtle.down() then height = height - 1 end
  100.     placeLeft()
  101.     placeRight()
  102.     turtle.placeDown()
  103.     if turtle.up() then height = height + 1 end    
  104.     turtle.select(1)
  105.    
  106.     if height ~= 0 then
  107.         print('Wrong height!')
  108.         do return end
  109.     end
  110.     end
  111. end
  112.  
  113. while i < length do
  114.    
  115.     if not turtle.compareDown() then
  116.         turtle.digDown()
  117.     end
  118.    
  119.     if not turtle.compareUp() then
  120.         if not turtle.detectUp() and turtle.digUp() then --blocking liquid
  121.             if blockLiquid() == 'blockingfail' then
  122.                     do return end
  123.             end
  124.         else -- or just digging
  125.             turtle.digUp()
  126.             if not fastmode then sleep(0.4) end
  127.             for fail = 0,faillimit do          
  128.             if not turtle.detectUp() and turtle.digUp() then --blocking liquid
  129.                 if blockLiquid() == 'blockingfail' then
  130.                     do return end
  131.                 end
  132.                 break
  133.             end
  134.            
  135.             if not turtle.detectUp() and not turtle.digUp() then
  136.                 break
  137.             end
  138.            
  139.             turtle.digUp()
  140.            
  141.             if fail == faillimit then
  142.                 print('Something strange above. Help me, master!')
  143.                 do return end
  144.             end
  145.             end
  146.         end
  147.     end
  148.    
  149.     if torch < 8 then --torch placing
  150.         torch = torch + 1
  151.     else
  152.         turtle.turnRight()
  153.         turtle.turnRight()
  154.         turtle.select(3)
  155.         turtle.place()
  156.         turtle.select(1)
  157.         turtle.turnRight()
  158.         turtle.turnRight()
  159.         torch = 0
  160.     end
  161.    
  162.     if turtle.getItemCount(16)>0 then --unloading mined resources
  163.         print('Invenory is full! Slot 4 must contain a chest. Trying to unloading items...')
  164.         if nochest then
  165.             print('Chests was denied by "nochest" flag! Clear inventory and restart program.')
  166.             do return end
  167.         end
  168.         turtle.select(4)
  169.         turtle.placeDown()
  170.         for j = 5,16 do
  171.             turtle.select(j)
  172.             turtle.dropDown()
  173.         end
  174.         turtle.select(1)
  175.     end
  176.    
  177.     if not turtle.compare() then
  178.         for fail = 0,faillimit do --because it can be sand or gravel
  179.             turtle.dig()
  180.             if turtle.forward() then break end
  181.             if fail == faillimit then
  182.                 print('Something strange in the front. Help me, master!')
  183.                 do return end
  184.             end
  185.         end
  186.     else
  187.         print('Block like in slot 1 detected, mine it and restart program.')
  188.         do return end
  189.     end
  190.    
  191.     i = i + 1
  192.     if length == i then print(length,' blocks successfully pass!') end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement