Jabulba

turtleTurn

Aug 15th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 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] == "l" or args[2] == "r" or args[2] == "left" or args[2] == "right") then
  12.     args[1] = 0
  13.     error("Provided direction " .. args[2] .. " is invalid. Try 'l' or 'r'")
  14. end
  15.  
  16. local function checkFuel()
  17.     local fuelLevel = turtle.getFuelLevel()
  18.     if fuelLevel < 5 then
  19.         print("CRITICAL: FUEL LEVEL CRITICAL.")
  20.         print("EMERGENCY REFUELING INITIATED.")
  21.         for slot = 1, 16, 1 do
  22.             if turtle.refuel() then
  23.                 break
  24.             end
  25.         end
  26.     elseif fuelLevel < 25 then
  27.         print("CRITICAL: FUEL LEVEL EXTREMELY LOW.")
  28.     elseif fuelLevel < 500 then
  29.         print("WARNING: FUEL LEVEL LOW.")
  30.     end
  31. end
  32.  
  33. local function turtleTurn(direction, attempts)
  34.     direction = direction or "left"
  35.     attempts = attempts or 1
  36.    
  37.     checkFuel()
  38.    
  39.     if attempts > 30 then
  40.         return false
  41.     end
  42.        
  43.     if direction == "l" or direction == "left" then
  44.         if not turtle.turnLeft() then
  45.             turtleTurn("f", attempts)
  46.         end
  47.     elseif direction == "r" or direction == "right" then
  48.         if not turtle.turnRight() then
  49.             turtle.attackDown()
  50.             turtleTurn("d", attempts)
  51.         end
  52.     end
  53.    
  54.     return true
  55. end
  56.  
  57. for runs = 1, args[1], 1 do
  58.     turtleTurn(args[2])
  59. end
Advertisement
Add Comment
Please, Sign In to add comment