Advertisement
mrkirby153

[TURTLE] Tunnel

Mar 7th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. -- tunnel <l> <w> <h> <left/right>
  2. local tArgs = { ... }
  3.  
  4. local length = 0
  5. local height = 0
  6. local width = 0
  7.  
  8. local slotWithEnderChest = 16
  9.  
  10. -- 0: left 1: right
  11. local direction = 0
  12.  
  13. if #tArgs ~= 4 then
  14.     print("Usage: <l> <w> <h> <left/right>")
  15.     return
  16. end
  17.  
  18. if tArgs[3] == "right" then
  19.     direction = 1
  20. end
  21.  
  22. length = tArgs[1]
  23. width = tArgs[2]
  24. height = tArgs[3] -1
  25.  
  26. function checkEmpty()
  27.     emptySlots = 0
  28.     for i=1,16 do
  29.         if i == slotWithEnderChest then
  30.             break
  31.         end
  32.         if turtle.getItemCount(i) == 0 then
  33.             emptySlots = emptySlots + 1
  34.         end
  35.     end
  36.     if emptySlots < 1 then
  37.         return false
  38.     else
  39.         return true
  40.     end
  41. end
  42. function empty()
  43.     -- Empty the turtle's inventory
  44.    
  45.     currentSlot = turtle.getSelectedSlot()
  46.     -- Check if the ender chest can be placed
  47.         if turtle.detectUp() then
  48.             turtle.digUp()
  49.         end
  50.     -- Place the ender chest
  51.     turtle.select(slotWithEnderChest)
  52.     turtle.placeUp()
  53.    
  54.     chest = peripheral.wrap("up")
  55.     for i=1,16 do
  56.         if i == slotWithEnderChest then
  57.             break
  58.         end
  59.         turtle.select(i)
  60.         turtle.dropUp()
  61.     end
  62.     turtle.select(slotWithEnderChest)
  63.     turtle.digUp()
  64.     turtle.select(currentSlot)
  65. end
  66.  
  67. function doEmpty()
  68.     if not checkEmpty() then
  69.         empty()
  70.     end
  71. end
  72. function checkRefuel()
  73.     currentSlot = turtle.getSelectedSlot()
  74.     if turtle.getFuelLevel() < 1000 then
  75.         print("Refuelling")
  76.         if turtle.detectUp() then
  77.             turtle.digUp()
  78.         end
  79.         -- Place the ender chest
  80.         turtle.select(slotWithEnderChest)
  81.         turtle.placeUp()
  82.         turtle.select(currentSlot)
  83.         chest = peripheral.wrap("top")
  84.        
  85.         inventorySize = chest.getInventorySize()
  86.         for i=1,inventorySize do
  87.             currentItem = chest.getStackInSlot(i)
  88.             if currentItem == nil then
  89.                 break
  90.             end
  91.             if currentItem["name"] == "coal" then
  92.                 -- Find the first inventory slot in the turtle
  93.                 freeSlot = -1
  94.                 for j=1,16 do
  95.                     print("Checking slot "..j..":"..turtle.getItemCount(j))
  96.                     if turtle.getItemCount(j) == 0 then
  97.                         freeSlot = j
  98.                         print("Found free slot "..j)
  99.                         break
  100.                     end
  101.                 end
  102.                 chest.pushItemIntoSlot("down", i, 64, freeSlot)
  103.                 turtle.select(i)
  104.                 turtle.refuel()
  105.             end
  106.         end
  107.         turtle.select(slotWithEnderChest)
  108.         turtle.digUp()
  109.         turtle.select(currentSlot)
  110.     else
  111.         print(turtle.getFuelLevel())
  112.     end
  113. end
  114.  
  115.  
  116. function digTunnel()
  117.     for w=1, width do
  118.         for l=1, length do
  119.             checkRefuel()
  120.             checkEmpty()
  121.             turtle.dig()
  122.             turtle.forward()
  123.             for h=1, height do
  124.                 while turtle.detectUp() do
  125.                     turtle.digUp()
  126.                 end
  127.                 turtle.up()
  128.             end
  129.             for h=1, height do
  130.                 turtle.down()
  131.             end
  132.         end
  133.         for l=1, length do
  134.             turtle.back()
  135.         end
  136.         -- Turn
  137.         if direction == 0 then
  138.             turtle.turnRight()
  139.             turtle.dig()
  140.             turtle.forward()
  141.             turtle.turnLeft()
  142.         else
  143.             turtle.turnLeft()
  144.             turtle.dig()
  145.             turtle.forward()
  146.             turtle.turnRight()
  147.         end
  148.     end
  149.     empty()
  150. end
  151. digTunnel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement