Advertisement
1amw31rd

ComputerCraft - Turtle DigBorder

Apr 20th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. local blocksMovedSinceLastChestplace = 0
  2. local chestSlot = 1 -- make sure to put this before fuelSlot, otherwise chests will be used for fuel
  3. local fuelSlot = 2 -- fuel will not be checked for on any slot before this
  4. local distanceBetweenChests = 100
  5.  
  6. -- Functions
  7.  
  8. function decidePlaceChest()
  9.     if blocksMovedSinceLastChestplace >= distanceBetweenChests then
  10.         local slotsFull = 0
  11.    
  12.         for i=1, 16, 1 do
  13.             if i ~= torchSlot and i ~= chestSlot and i ~= fuelSlot then
  14.                 if turtle.getItemCount(i) > 0 then
  15.                     slotsFull = slotsFull + 1
  16.                 end
  17.             end
  18.         end
  19.        
  20.         if slotsFull >= 7 then
  21.             placeChest()
  22.         end
  23.        
  24.         blocksMovedSinceLastChestplace = 0
  25.     end
  26. end
  27.  
  28. function placeChest()
  29.     turtle.turnLeft()
  30.     turtle.turnLeft()
  31.     turtle.select(chestSlot)
  32.    
  33.     if turtle.getItemCount(chestSlot) < 5 then
  34.         setRed()
  35.         print("Low on chests!")
  36.     end
  37.    
  38.     turtle.select(chestSlot)
  39.     while turtle.place() == false do
  40.         turtle.dig()
  41.         sleep(0.5)
  42.     end
  43.    
  44.     for i=1, 16, 1 do
  45.         if i ~= torchSlot and i ~= chestSlot and i ~= fuelSlot then
  46.             turtle.select(i)
  47.             turtle.drop()
  48.         end
  49.     end
  50.    
  51.     turtle.turnRight()
  52.     turtle.turnRight()
  53. end
  54.  
  55. function fuel()
  56.     if turtle.getFuelLevel() < 5 then
  57.         local alreadyNotified = false
  58.  
  59.         while true do
  60.             local found = false
  61.  
  62.             for i=fuelSlot, 16, 1 do
  63.                 turtle.select(i)
  64.                 if turtle.refuel(1) == true then
  65.                     found = true
  66.                     break
  67.                 end
  68.             end
  69.  
  70.             if found == true then
  71.                 break
  72.             else
  73.                 if alreadyNotified == false then
  74.                     print("Need fuel!")
  75.                     alreadyNotified = true
  76.                 end
  77.             end
  78.         end
  79.         turtle.select(1)
  80.     end
  81. end
  82.  
  83. function tryForward()
  84.     local alreadyNotified = false
  85.     fuel()
  86.     while turtle.forward() == false do
  87.         if alreadyNotified == false then
  88.             print("Something is in my way!")
  89.             alreadyNotified = true
  90.         end
  91.         turtle.dig()
  92.         sleep(0.5)
  93.     end
  94.     blocksMovedSinceLastChestplace = blocksMovedSinceLastChestplace + 1
  95. end
  96. function tryUp()
  97.     local alreadyNotified = false
  98.     fuel()
  99.     while turtle.up() == false do
  100.         if alreadyNotified == false then
  101.             print("Something is in my way!")
  102.             alreadyNotified = true
  103.         end
  104.         turtle.digUp()
  105.         sleep(0.5)
  106.     end
  107.     blocksMovedSinceLastChestplace = blocksMovedSinceLastChestplace + 1
  108. end
  109. function tryDown()
  110.     local alreadyNotified = false
  111.     fuel()
  112.     while turtle.down() == false do
  113.         if alreadyNotified == false then
  114.             print("Something is in my way!")
  115.             alreadyNotified = true
  116.         end
  117.         turtle.digDown()
  118.         sleep(0.5)
  119.     end
  120.     blocksMovedSinceLastChestplace = blocksMovedSinceLastChestplace + 1
  121. end
  122.  
  123. function digDistance(distance)
  124.     for i=1, distance, 1 do
  125.         digSingle()
  126.         tryForward()
  127.     end
  128. end
  129.  
  130. function digSingle()
  131.     while turtle.detect() == true do
  132.         turtle.dig()
  133.     end
  134.     decidePlaceChest()
  135. end
  136. function digSingleUp()
  137.     while turtle.detectUp() == true do
  138.         turtle.digUp()
  139.     end
  140.     decidePlaceChest()
  141. end
  142.  
  143. -- Scripts
  144.  
  145. local args = { ... }
  146. -- or
  147. if table.getn(args) ~= 3 then
  148.     print("incorrect number of args")
  149.     print("cleararea <length> <width> <height>")
  150.     return
  151. end
  152. if args[1] == nil or args[2] == nil or args[3] == nil then
  153.     print("nil arg")
  154.     print("cleararea <length> <width> <height>")
  155.     return
  156. end
  157. if tonumber(args[1]) == nil or tonumber(args[2]) == nil or tonumber(args[3]) == nil then
  158.     print("NAN")
  159.     print("cleararea <length> <width> <height>")
  160.     return
  161. end
  162.  
  163. local length = tonumber(args[1])
  164. local width = tonumber(args[2])
  165. local height = tonumber(args[3])
  166. local newLength = length
  167.  
  168. for i=1, height, 1 do
  169.     digDistance(newLength)
  170.     newLength = length - 1
  171.     turtle.turnRight()
  172.     digDistance(width-1)
  173.     turtle.turnRight()
  174.     digDistance(length-1)
  175.     turtle.turnRight()
  176.     digDistance(width-1)
  177.     turtle.turnRight()
  178.  
  179.     if i ~= height then
  180.         tryDown()
  181.     end
  182. end
  183.  
  184. for i=1, height-1, 1 do
  185.     tryUp()
  186. end
  187.  
  188. turtle.back()
  189.  
  190. print("Complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement