Advertisement
Satscape

Computercraft turtle smart tunneller

Dec 18th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. -- save this as "tunnel"
  2. -- place turtle at front-left and a chest behind him.
  3. -- put torches in last slot to light up your tunnel
  4. -- usage:   tunnel 20 10
  5. -- will dig a tunnel 20 long and 10x10
  6. -- when inventory is getting full, turtle will return to chest to empty
  7. -- turtle will use any fuel it mines or you can drop lava/coal etc in for him to use
  8.  
  9.  
  10. -- grab fuel from all slots
  11. function grabFuel()
  12.  for i=1,15 do
  13.   turtle.select(i)
  14.   turtle.refuel()
  15.  end
  16. end
  17.  
  18. function digAndMove()
  19.     while (turtle.detect()) do
  20.         turtle.dig(); os.sleep(0.5);
  21.     end
  22.  while (not turtle.forward()) do   
  23.   turtle.dig(); turtle.attack(); os.sleep(0.5);
  24.  end
  25. end
  26.  
  27. function digAndMoveUp()
  28.     while (turtle.detectUp()) do
  29.         turtle.digUp(); os.sleep(0.5);
  30.     end
  31.  while (not turtle.up()) do
  32.     turtle.digUp();
  33.  end
  34. end
  35.  
  36. function digAndMoveDown()
  37.     while (turtle.detectDown()) do
  38.         turtle.digDown(); os.sleep(0.5);
  39.     end
  40.     while (not turtle.down()) do
  41.     turtle.digDown(); os.sleep(0.5)
  42.  end
  43. end
  44.  
  45. function returnHome()
  46.   local done=false
  47.   if tonumber(distanceFromChest) >=tonumber(tunlen) then
  48.    done=true
  49.   end
  50.   turtle.turnLeft()
  51.   turtle.turnLeft()
  52.   for d=1,distanceFromChest do
  53.     digAndMove()
  54.   end
  55.   --make sure we are back home
  56.   while not turtle.detect() do
  57.     turtle.forward()
  58.     os.sleep(0.2)
  59.   end
  60.  
  61.   --unload
  62.   for slot=1, 15, 1 do
  63.     turtle.select(slot);
  64.     turtle.drop();
  65.   end
  66.   if done then
  67.    announce("All done")
  68.    error("done")
  69.   else
  70.    turtle.turnLeft()
  71.    turtle.turnLeft()
  72.    for d=1,distanceFromChest do
  73.     digAndMove()
  74.    end
  75.   end
  76. end
  77.  
  78. function announce(thing)
  79. print(thing)
  80. end
  81.  
  82. ----------------- START
  83. args={...}
  84. tunlen=args[1]
  85. tunsize=args[2]
  86.  
  87. if tonumber(tunsize) % 2 ~= 0 then
  88.  tunsize=tonumber(tunsize)+1
  89. end
  90. distanceFromChest=0;
  91. distanceWere=0;
  92.  
  93. announce("Starting to dig tunnel "..tunlen.." long...")
  94.  
  95. --find start of mine
  96. while not turtle.detect() do
  97.   turtle.forward()
  98.   os.sleep(0.2)
  99. end
  100.  
  101. while true do
  102.  if distanceFromChest > tonumber(tunlen) then
  103.   returnHome()
  104.  end
  105.     grabFuel();
  106.     announce("Fuel "..turtle.getFuelLevel().." at "..distanceFromChest.." of "..tunlen);
  107.    
  108.     --place torch?
  109.     if distanceFromChest % 5 == 1 then
  110.      turtle.turnLeft()
  111.      turtle.dig();
  112.      turtle.select(16);
  113.      turtle.place();
  114.      turtle.turnRight()
  115.     end
  116.    
  117.     grabFuel();
  118.    
  119.     --facing bottom left block
  120.      digAndMove()
  121.      distanceFromChest=distanceFromChest + 1;
  122.    
  123.     turtle.turnRight()
  124.    
  125.     for bottotop=1,tonumber(tunsize) do
  126.      for leftright=2,tonumber(tunsize) do
  127.       digAndMove()
  128.      end --leftright
  129.    
  130.      if (bottotop < tonumber(tunsize)) then
  131.        digAndMoveUp()
  132.        turtle.turnLeft()
  133.        turtle.turnLeft()
  134.      else
  135.        turtle.turnRight()
  136.       for down=2,tunsize do
  137.         digAndMoveDown()
  138.       end
  139.      end
  140.      end --botttotop
  141.  
  142.     local goHome=true
  143.     for inv=1,16 do
  144.       if turtle.getItemCount(inv)==0 then
  145.        goHome=false
  146.       end
  147.      end
  148.     if goHome then
  149.       announce("about to drop off load")
  150.       returnHome()
  151.     end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement