Advertisement
PhamKun

Locate Turtle

Aug 13th, 2022 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.28 KB | None | 0 0
  1. local setX = tonumber(arg[1])
  2. local setY = tonumber(arg[2])
  3. local setZ = tonumber(arg[3])
  4.  
  5. --util function
  6. local function checkF()
  7.     if turtle.getFuelLevel() == 0 then
  8.         turtle.select(16)
  9.         turtle.refuel(1)
  10.     end
  11. end
  12.  
  13. --chop tree function
  14. local function chopUp()
  15.     local high = 1
  16.     while turtle.up() == false do
  17.         turtle.dig()
  18.         turtle.digUp()
  19.         checkF()
  20.         turtle.up()
  21.         high = high + 1
  22.     end
  23.     return high
  24. end
  25.  
  26. local function chopDown(a)
  27.     for i = 1, a, 1 do
  28.         turtle.dig()
  29.         turtle.digDown()
  30.         checkF()
  31.         turtle.down()
  32.     end
  33.     turtle.dig()
  34. end
  35.  
  36. --plant sapling
  37.     --at the end of this function, the turtle should return to original position a.k.a setX, setY, setZ
  38.     --spaling slot will be the 15th slot
  39. local function placeSapling()
  40.     turtle.select(15)
  41.     turtle.place()
  42.     turtle.turnLeft()
  43.     checkF()
  44.     turtle.back()
  45.     turtle.place()
  46.     turtle.turnRight()
  47.     turtle.place()
  48.     checkF()
  49.     turtle.back()
  50.     turtle.place()
  51. end
  52.  
  53.  
  54. --functions use for init()
  55. --clean up older log if there is any
  56. local function chopUpInit()
  57.     local high = 1
  58.     while high <= 32 do
  59.         turtle.dig()
  60.         turtle.digUp()
  61.         checkF()
  62.         turtle.up()
  63.         high = high + 1
  64.     end
  65.     return high
  66. end
  67.  
  68. --go to init position
  69. local function initLococaltion()
  70.     --make sure the turtle is always facing south at the end of this function
  71.     local x1, y1, z1 = gps.locate()
  72.     if turtle.forward() == false then
  73.         turtle.refuel()
  74.         if not turtle.forward() then
  75.             turtle.dig()
  76.         end
  77.         turtle.forward()
  78.     end
  79.    
  80.     local x2, y2, z2 = gps.locate()
  81.     if x1 - x2 == 0 then
  82.         turtle.turnRight()
  83.         if z2 - z1 > 0 then
  84.             turtle.turnRight()
  85.             turtle.turnRight()
  86.         end
  87.     end
  88.  
  89.     --go to y coordinate
  90.     local x, y, z = gps.locate()
  91.     while not (y == setY) do
  92.         if setY - y > 0 then
  93.             if turtle.up() == false then
  94.                 turtle.refuel()
  95.                 if turtle.detectUp() then
  96.                     turtle.digUp()
  97.                 end
  98.                 turtle.up()
  99.             end
  100.         elseif setY - y < 0 then
  101.             if turtle.down() == false then
  102.                 turtle.refuel()
  103.                 if turtle.detectDown() then
  104.                     turtle.digDown()
  105.                     turtle.down()
  106.                 end
  107.             end
  108.         end
  109.         x, y, z = gps.locate()
  110.     end
  111.  
  112.     --go to x coordinate
  113.     while not (x == setX) do
  114.         if setX - x > 0 then
  115.             if turtle.forward() == false then
  116.                 turtle.refuel()
  117.                 if turtle.detect() then
  118.                     turtle.dig()
  119.                 end
  120.                 turtle.forward()
  121.             end
  122.         elseif setX - x < 0 then
  123.             if turtle.back() == false then
  124.                 turtle.refuel()
  125.                 if not turtle.back() then
  126.                     turtle.turnRight()
  127.                     turtle.turnRight()
  128.                     turtle.dig()
  129.                     turtle.turnRight()
  130.                     turtle.turnRight()
  131.                 end
  132.                 turtle.back()
  133.             end
  134.         end
  135.         x, y, z = gps.locate()
  136.     end
  137.  
  138.     --go to z coordinate
  139.     turtle.turnRight()
  140.     while not (z == setZ) do
  141.         if setZ - z > 0 then
  142.             if turtle.forward() == false then
  143.                 turtle.refuel()
  144.                 if turtle.detect() then
  145.                     turtle.dig()
  146.                 end
  147.                 turtle.forward()
  148.             end
  149.         elseif setZ - z < 0 then
  150.             if turtle.back() == false then
  151.                 turtle.refuel()
  152.                 if not turtle.back() then
  153.                     turtle.turnRight()
  154.                     turtle.turnRight()
  155.                     turtle.dig()
  156.                     turtle.turnRight()
  157.                     turtle.turnRight()
  158.                 end
  159.                 turtle.back()
  160.             end
  161.         end
  162.         x, y, z = gps.locate()
  163.     end
  164. end
  165.  
  166.  
  167.  
  168. --main
  169. initLococaltion()
  170. checkF()
  171. turtle.dig()
  172. turtle.forward()
  173. local a = chopUpInit()
  174. turtle.turnLeft()
  175. checkF()
  176. turtle.forward()
  177. turtle.turnLeft()
  178. chopDown(a)
  179. placeSapling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement