Advertisement
ColdIV

SugarCaneLane

Jun 9th, 2021 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. if turtle.getItemDetail(1) == nil then
  2.     print ("Put fuel in the first slot of the turtle and run again!\n")
  3. end
  4.  
  5. local data = turtle.getItemDetail(1) -- fuel slot 1
  6. local fuelType = data.name
  7. local args = {...}
  8.  
  9. --local maxDistance = tonumber(args[1]) or 30 -- straight line
  10. local distance = 0
  11. local path = {}
  12. local target = "minecraft:sugar_cane"
  13. local skipped = 0
  14. local sleepTime = 60 -- in seconds
  15.  
  16. -- resting and current Position
  17. local rPosition = { x = tonumber(args[1]), y = tonumber(args[2]), z = tonumber(args[3]) }
  18. local cPosition = {}
  19. cPosition.x, cPosition.y, cPosition.z = gps.locate()
  20.  
  21. -- back to start?
  22. if rPosition.x ~= cPosition.x or
  23.    rPosition.y ~= cPosition.y or
  24.    rPosition.z ~= cPosition.z then
  25.     -- go to resting Position first
  26.     -- move turtle above sugar canes
  27.     turtle.up()
  28.     turtle.up()
  29.    
  30.     cPosition.x, cPosition.y, cPosition.z = gps.locate()
  31.     print ("I am at:\nx: " .. cPosition.x .. "\ny: " .. cPosition.y + 2 .. "\nz: " .. cPosition.z .. "\n")
  32.     print ("I want to be at:\nx: " .. rPosition.x .. "\ny: " .. rPosition.y .. "\nz: " .. rPosition.z .. "\n")
  33.    
  34.     turtle.forward()
  35.    
  36.     local tmp = {}
  37.     tmp.x, tmp.y, tmp.z = gps.locate()
  38.     --[[
  39.     print (
  40.         math.abs(math.floor(tmp.x) - math.floor(rPosition.x)) < math.abs(math.floor(cPosition.x) - math.floor(rPosition.x)),
  41.         math.abs(math.floor(tmp.y) - math.floor(rPosition.y)) < math.abs(math.floor(cPosition.y) - math.floor(rPosition.y)),
  42.         math.abs(math.floor(tmp.z) - math.floor(rPosition.z)) < math.abs(math.floor(cPosition.z) - math.floor(rPosition.z))
  43.     )
  44.     ]]
  45.     while not (math.abs(math.floor(tmp.z) - math.floor(rPosition.z)) < math.abs(math.floor(cPosition.z) - math.floor(rPosition.z)))     and rPosition.z ~= tmp.z do
  46.         turtle.back()
  47.         turtle.turnRight()
  48.         turtle.forward()
  49.         tmp.x, tmp.y, tmp.z = gps.locate()
  50.     end
  51.        
  52.     while math.abs(math.floor(tmp.z) - math.floor(rPosition.z)) < math.abs(math.floor(cPosition.z) - math.floor(rPosition.z))
  53.     and rPosition.z ~= tmp.z do
  54.         turtle.forward()
  55.         tmp.x, tmp.y, tmp.z = gps.locate()
  56.     end
  57.     print ("Reached z position\n")
  58.    
  59.     turtle.forward()
  60.    
  61.     while not (math.abs(math.floor(tmp.x) - math.floor(rPosition.x)) < math.abs(math.floor(cPosition.x) - math.floor(rPosition.x)))     and rPosition.x ~= tmp.x do
  62.         turtle.back()
  63.         turtle.turnRight()
  64.         turtle.forward()
  65.         tmp.x, tmp.y, tmp.z = gps.locate()
  66.     end
  67.        
  68.     while math.abs(math.floor(tmp.x) - math.floor(rPosition.x)) < math.abs(math.floor(cPosition.x) - math.floor(rPosition.x))
  69.     and rPosition.x ~= tmp.x do
  70.         turtle.forward()
  71.         tmp.x, tmp.y, tmp.z = gps.locate()
  72.     end
  73.     print ("Reached x position\n")
  74.        
  75.     while tmp.y ~= rPosition.y do
  76.         turtle.down()
  77.         tmp.x, tmp.y, tmp.z = gps.locate()
  78.     end
  79.     print ("Reached y position\n")
  80.    
  81.     turtle.turnRight()
  82.     turtle.turnRight()
  83. end
  84.  
  85. local _, tmpI = turtle.inspect()
  86. while tmpI.name ~= target do
  87.     print ("Turning to " .. target .. "\n")
  88.     turtle.turnRight()
  89.     _, tmpI = turtle.inspect()
  90. end
  91.  
  92.  
  93. function harvest ()
  94.     local _, data = turtle.inspect()
  95.     if data.name == target then
  96.         turtle.dig()
  97.         turtle.suckUp()
  98.         turtle.suck()
  99.         turtle.suckDown()
  100.         skipped = 0
  101.         --print ("harvest true\n")
  102.         return true
  103.     end
  104.     skipped = skipped + 1
  105.     --print ("harvest false\n")
  106.     return false
  107. end
  108.  
  109. function checkFuel ()
  110.     if turtle.getFuelLevel() <= #path + 10 then
  111.         print ("no fuel\n")
  112.         for i = 1, 16, 1 do
  113.             if turtle.getItemDetail(i).name == fuelType then
  114.                 turtle.select(i)
  115.                 turtle.refuel()
  116.                 print ("refuel\n")
  117.                 return true
  118.             end
  119.         end
  120.  
  121.         print ("out of fuel\n")
  122.         return false
  123.     end
  124.    
  125.     return true
  126. end
  127.    
  128.  
  129. function run ()
  130.     if not checkFuel() then return end
  131.     local cond = false
  132.     turtle.up()
  133.     turtle.up()
  134.     local _, data = turtle.inspect()
  135.     if data.name == target then cond = true end
  136.     turtle.down()
  137.     while cond and skipped < 6 do -- ignore max distance for now
  138.         print ("skipped: " .. skipped .. "\ndistance: " .. distance .. "\n")
  139.        
  140.         -- refuel
  141.         if not checkFuel() then return end
  142.        
  143.         if harvest() then
  144.             if turtle.forward() then
  145.                 distance = distance + 1
  146.                 table.insert(path, "forward")
  147.             end
  148.         else
  149.             --print ("check left\n")
  150.             turtle.turnLeft()
  151.  
  152.             if harvest() then
  153.                 if turtle.forward() then
  154.                     table.insert(path, "left")
  155.                 end
  156.             else
  157.                 --print ("check right\n")
  158.                 turtle.turnRight()
  159.                 turtle.turnRight()
  160.  
  161.                 if harvest() then
  162.                     if turtle.forward() then
  163.                         table.insert(path, "right")
  164.                     end
  165.                 else
  166.                     turtle.turnLeft()
  167.                     if turtle.forward() then
  168.                         distance = distance + 1
  169.                         table.insert(path, "forward")
  170.                     end
  171.                 end
  172.             end
  173.         end
  174.        
  175.        
  176.     end
  177.    
  178.     print ("Moving back!\nDistance: " .. distance .. "\nPath Length: " .. #path .. "\n")
  179.     turtle.turnRight()
  180.     turtle.turnRight()
  181.     for i = #path, 1, -1 do
  182.         while not turtle.forward() do harvest() end
  183.         if path[i] == "forward" then
  184.             -- just forward
  185.         elseif path[i] == "left" then
  186.             turtle.turnRight()
  187.         elseif path[i] == "right" then
  188.             turtle.turnLeft()
  189.         end
  190.     end
  191.     turtle.down()
  192.     turtle.turnRight()
  193.     turtle.turnRight()
  194.    
  195.     print ("wait " .. sleepTime .. "s\n")
  196.     sleep(sleepTime)
  197.     path = {}
  198.     skipped = 0
  199.     distance = 0
  200.     print ("restart\n")
  201.     run()
  202. end
  203.  
  204. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement