Jabulba

forwardDigger

Aug 15th, 2015
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.15 KB | None | 0 0
  1. -- forward digger program by Caio "Jabulba" Jabulka 2015
  2. -- Turtle type: Mining Turtle
  3. -- Purpose: Turtle moves the specified amount of times forward breaking blocks above and below.
  4. -- Licensed MIT
  5.  
  6. local enderChestId = "EnderStorage:enderChest"
  7. local inventoryEmptiedCount = 0
  8. local arg = ...
  9. arg = tonumber(arg) or 1
  10.  
  11. assert(type(arg) == "number", "Expected at most one argument as number.")
  12.  
  13. local function turtleMove(direction)
  14.     direction = direction or "forward"
  15.    
  16.     if direction == "f" or direction == "forward" then
  17.         return turtle.forward()
  18.        
  19.     elseif direction == "b" or direction == "backward" then
  20.         return turtle.back()
  21.        
  22.     elseif direction == "d" or direction == "down" then
  23.         return turtle.down()
  24.        
  25.     elseif direction == "u" or direction == "up" then
  26.         return turtle.up()
  27.     end
  28. end
  29.  
  30. local function emptyInventory()
  31.     clearCobblestone = clearCobblestone or false
  32.    
  33.     print("Inventory full! Clearing it now...")
  34.     inventoryEmptiedCount = inventoryEmptiedCount + 1
  35.     turtle.select(1)
  36.     while turtle.detect() do
  37.         turtle.dig()
  38.         os.sleep(0.2)
  39.     end
  40.     turtle.place()
  41.     for i = 2, 16, 1 do
  42.         turtle.select(i)
  43.         turtle.drop()
  44.     end
  45.     turtle.select(1)
  46.     turtle.dig()
  47.     print("Inventory cleared for the " .. tostring(inventoryEmptiedCount) .. " time.")
  48. end
  49.  
  50. local function checkInventory()
  51.     local fullSlots = 0
  52.     for i = 1, 16, 1 do
  53.         if turtle.getItemCount(i) > 0 then
  54.             fullSlots = fullSlots + 1
  55.         end
  56.     end
  57.    
  58.     local data = turtle.getItemDetail(1)
  59.     if fullSlots == 0 or not data or not data.name == enderChestId then
  60.         print("WHERE IS THE ENDER CHEST!?")
  61.         while not data or not data.name == enderChestId do
  62.             os.sleep(0.5)
  63.             data = turtle.getItemDetail(1)
  64.         end
  65.     end
  66.     if fullSlots > 14 then
  67.         emptyInventory()
  68.     end
  69. end
  70.  
  71. local function checkFuel()
  72.     local fuelLevel = turtle.getFuelLevel()
  73.     if fuelLevel < 5 then
  74.         print("CRITICAL: FUEL LEVEL CRITICAL.")
  75.         print("EMERGENCY REFUELING INITIATED.")
  76.         for slot = 1, 16, 1 do
  77.             if turtle.refuel() then
  78.                 break
  79.             end
  80.         end
  81.     elseif fuelLevel < 25 then
  82.         print("CRITICAL: FUEL LEVEL EXTREMELY LOW.")
  83.     elseif fuelLevel < 500 then
  84.         print("WARNING: FUEL LEVEL LOW.")
  85.     end
  86. end
  87.  
  88. local function digMove(direction, attempts)
  89.     direction = direction or "forward"
  90.     attempts = attempts or 1
  91.    
  92.     checkFuel()
  93.     checkInventory()
  94.    
  95.     if attempts > 30 then
  96.         return false
  97.     end
  98.  
  99.     turtle.dig()
  100.        
  101.     if direction == "f" or direction == "forward" then
  102.         turtle.dig()
  103.        
  104.         if not turtleMove("f") then
  105.             turtle.attack()
  106.             digMove("f", attempts)
  107.         end
  108.     elseif direction == "d" or direction == "down" then
  109.         turtle.digDown()
  110.        
  111.         if not turtleMove("d") then
  112.             turtle.attackDown()
  113.             digMove("d", attempts)
  114.         end
  115.     elseif direction == "u" or direction == "up" then
  116.         turtle.digUp()
  117.        
  118.         if not turtleMove("u") then
  119.             turtle.attackUp()
  120.             digMove("u", attempts)
  121.         end
  122.     end
  123.    
  124.     return true
  125. end
  126.  
  127. for runs = 1, arg, 1 do
  128.     digMove("forward")
  129.     turtle.digUp()
  130.     turtle.digDown()
  131. end
  132.  
  133. -- Clear the inventory
  134.  turtleMove("backward")
  135. emptyInventory()
Advertisement
Add Comment
Please, Sign In to add comment