Jabulba

turtleMove

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