Advertisement
Inksaver

b.lua (back X blocks)

Apr 27th, 2020 (edited)
2,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.40 KB | None | 0 0
  1. version = 20230801.1200
  2. -- https://pastebin.com/g7DjxRbr
  3. -- pastebin get g7DjxRbr b.lua
  4. -- Last edited: see version YYYYMMDD.HHMM
  5. local usage = {}
  6. usage.backwards = [[
  7. b 10  :moves 10 blocks back unless
  8. obstructed. No blocks broken
  9.  
  10. b 10 d :moves 10 blocks back unless
  11. bedrock. Blocks are removed.
  12.  
  13. 'd' or any character / string is ok]]
  14. usage.down = [[
  15. d 10  :moves 10 blocks down unless
  16. obstructed. No blocks broken
  17.  
  18. d 10 d :moves 10 blocks down unless
  19. bedrock. Blocks are removed.
  20.  
  21. 'd' or any character / string is ok]]
  22. usage.forwards = [[
  23. f 10  :moves 10 blocks forward unless
  24. obstructed. No blocks broken
  25.  
  26. f 10 d :moves 10 blocks forward unless
  27. bedrock. Blocks are removed.
  28.  
  29. 'd' or any character / string is ok]]
  30. usage.up = [[
  31. u 10  :moves 10 blocks up unless
  32. obstructed. No blocks broken
  33.  
  34. u 10 d :moves 10 blocks up unless
  35. bedrock. Blocks are removed.
  36.  
  37. 'd' or any character / string is ok]]
  38. args = {...}
  39.  
  40. function checkArgs(direction)
  41.     local numBlocksRequested = 1
  42.     local doDig = false
  43.     if args[1] ~= nil then
  44.         if args[1]:lower() == "h" then
  45.             term.clear()
  46.             term.setCursorPos(1,1)
  47.             print(usage[direction])
  48.             print("Fuel Level: "..turtle.getFuelLevel())
  49.             error()
  50.         else
  51.             numBlocksRequested = tonumber(args[1])
  52.             if numBlocksRequested == nil then
  53.                 print("Use a number as argument, not "..args[1])
  54.                 error()
  55.             end
  56.         end
  57.         if args[2] ~= nil then -- any character here will work
  58.             doDig = true
  59.         end
  60.     end
  61.     return numBlocksRequested, doDig
  62. end
  63.  
  64. function turnRound()
  65.     turtle.turnRight()
  66.     turtle.turnRight()
  67. end
  68.  
  69. function doMoves(numBlocksRequested, doDig, direction)
  70.     local errorMsg = nil
  71.     local numBlocksMoved = 0
  72.     local Move, Dig, Detect
  73.     local turned = false
  74.    
  75.     -- re-assign turtle functions to new variables
  76.     if direction == "forwards" then
  77.         Move = turtle.forward
  78.         Dig = turtle.dig
  79.         Detect = turtle.detect
  80.     elseif direction == "backwards" then
  81.         Move = turtle.back
  82.         Dig = turtle.dig
  83.         Detect = turtle.detect
  84.     elseif direction == "up" then
  85.         Move = turtle.up
  86.         Dig = turtle.digUp
  87.         Detect = turtle.detectUp
  88.     else
  89.         Move = turtle.down
  90.         Dig = turtle.digDown
  91.         Detect = turtle.detectDown
  92.     end
  93.    
  94.     for i = 1, numBlocksRequested, 1 do
  95.         local moveOK, moveError = Move() -- try to move forward/up/down
  96.         if doDig then
  97.             if moveOK then
  98.                 numBlocksMoved = numBlocksMoved + 1
  99.             else -- did not move due to obstruction
  100.                 -- while moveOK == false do -- same effect if you prefer.
  101.                 if direction == "backwards" and not turned then
  102.                     turnRound()
  103.                     turned = true
  104.                     Move = turtle.forward
  105.                 end
  106.                 while not moveOK do -- did not move if obstruction
  107.                     local digOK, digError = Dig()
  108.                     if digOK then
  109.                         sleep(0.5) -- allow sand / gravel to drop if digging forward / up
  110.                     else -- unable to dig, or nothing to dig
  111.                         if digError == "Unbreakable block detected" then
  112.                             errorMsg = digError
  113.                             break
  114.                         end
  115.                     end
  116.                     moveOK, moveError = Move() -- try to move forward/up/down again
  117.                     if moveOK then
  118.                         numBlocksMoved = numBlocksMoved + 1
  119.                     end
  120.                 end
  121.             end
  122.         else
  123.             if moveOK then
  124.                 numBlocksMoved = numBlocksMoved + 1
  125.             else -- move did not succeed
  126.                 errorMsg = moveError
  127.                 break
  128.             end
  129.         end
  130.     end
  131.    
  132.     if turned then -- was "backwards" but obstuction rotated 180 so need to turn round again
  133.         turnRound()
  134.     end
  135.    
  136.     return numBlocksMoved, errorMsg
  137. end
  138.  
  139. function printLog(direction, numBlocksRequested, numBlocksMoved, errorMsg)
  140.     print("Moved "..direction.." "..numBlocksMoved.. " / ".. numBlocksRequested)
  141.     if errorMsg ~= nil then
  142.         print (errorMsg)
  143.     end
  144. end
  145.  
  146. function main()
  147.     local directions = {"backwards", "down", "forwards", "up"}
  148.     --***********************************************************************************************
  149.     --Change this to 1-4 to suit application (forwards, up, down, backwards) f.lua, u.lua, d.lua, b.lua
  150.     local directionIndex = 1 -- this is for b.lua
  151.     --***********************************************************************************************
  152.     local direction = directions[directionIndex] -- e.g backwards
  153.     local numBlocksRequested, doDig = checkArgs(direction)
  154.     if turtle.getFuelLevel() == 0 then
  155.         print("No fuel")
  156.     else
  157.         print("Fuel level: "..turtle.getFuelLevel())
  158.         local numBlocksMoved, errorMsg = doMoves(numBlocksRequested, doDig, direction)
  159.         printLog(direction, numBlocksRequested, numBlocksMoved, errorMsg)
  160.     end
  161. end
  162.  
  163. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement