Advertisement
mrjoecool

move

Aug 11th, 2022 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. --check for right arguments
  2. local args  = {...}
  3. if #args ~= 3 then
  4.     error("please enter x,y,z of the target position")
  5. end
  6. local x,y,z = tonumber(args[1]),tonumber(args[2]),tonumber(args[3])
  7.  
  8.  
  9. --open Rednet
  10. if not rednet.isOpen() then
  11.     peripheral.find("modem", rednet.open)
  12. end
  13.  
  14.  
  15. --check for enderchests
  16. local slot15 = turtle.getItemDetail(15)
  17. local slot16 = turtle.getItemDetail(16)
  18. if slot15 and slot16 then
  19.     if not(slot15.name == "enderstorage:ender_chest" and slot15.name == "enderstorage:ender_chest") then
  20.         print("enderchest missing")
  21.         error()
  22.     end
  23. else
  24.     print("enderchest missing")
  25.     error()
  26. end
  27.  
  28.  
  29. --place enderchest, take lava bucket, refuel and put enderchest back in inventory
  30. function refuel()
  31.     if turtle.getFuelLevel() < 1000 then
  32.         print("fuel = "..turtle.getFuelLevel())
  33.         turtle.select(16)
  34.         turtle.digUp()
  35.         turtle.up()
  36.         turtle.digUp()
  37.         turtle.down()
  38.         local ok,err = turtle.placeUp()
  39.         if not ok then
  40.             print("could not place enderchest")
  41.             print(err)
  42.             error()
  43.         end
  44.         turtle.suckUp()
  45.         turtle.refuel()
  46.         turtle.dropUp()
  47.         turtle.digUp()
  48.     end
  49. end
  50.  
  51.  
  52. -- -x = 1, -z = 2, +x = 3, +z = 4
  53. function getDirection()
  54.     local loc1 = vector.new(gps.locate(2, false))
  55.     if not turtle.forward() then
  56.         for j=1,6 do
  57.             if not turtle.forward() then
  58.                 turtle.dig()
  59.             else
  60.                 break
  61.             end
  62.         end
  63.     end
  64.     loc2 = vector.new(gps.locate(2, false))
  65.     heading = loc2 - loc1
  66.     turtle.back()
  67.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  68. end
  69.  
  70.  
  71. -- -x = 1, -z = 2, +x = 3, +z = 4
  72. function turn(cfacing, tfacing)
  73.     print(cfacing, tfacing)
  74.     while cfacing ~= tfacing do
  75.         local facingDiff = tfacing - cfacing
  76.         if math.abs(facingDiff) ~= 3 then
  77.             if facingDiff > 0 then
  78.                 turtle.turnRight()
  79.                 cfacing = cfacing + 1
  80.             else
  81.                 turtle.turnLeft()
  82.                 cfacing = cfacing - 1
  83.             end
  84.         else
  85.             if facingDiff > 0 then
  86.                 turtle.turnLeft()
  87.                 cfacing = cfacing + 3
  88.             else
  89.                 turtle.turnRight()
  90.                 cfacing = cfacing - 3
  91.             end
  92.         end
  93.     end
  94. end
  95.  
  96.  
  97. function moveToPos(tx,ty,tz)
  98.     local cfacing = getDirection()
  99.     local cx,cy,cz = gps.locate()
  100.     refuel()
  101.     os.sleep(2)
  102.     print(cy, ty)
  103.     print(gps.locate())
  104.     if cy < ty then
  105.         while cy < ty do
  106.             if not turtle.up() then
  107.                 turtle.digUp()
  108.             else
  109.                 cy = cy + 1
  110.             end
  111.         end
  112.     end
  113.     refuel()
  114.     if cx ~= tx then
  115.         if cx < tx then --move to +x
  116.             turn(cfacing,3)
  117.             cfacing = 3
  118.             while cx < tx do
  119.                 turtle.forward()
  120.                 cx = cx + 1
  121.             end
  122.         else --move to -x
  123.             turn(cfacing,1)
  124.             cfacing = 1
  125.             while cx > tx do
  126.                 turtle.forward()
  127.                 cx = cx - 1
  128.             end
  129.         end
  130.     end
  131.     refuel()
  132.     if cz ~= tz then
  133.         if cz < tz then --move to +x
  134.             turn(cfacing,4)
  135.             cfacing = 4
  136.             while cz < tz do
  137.                 turtle.forward()
  138.                 cz = cz + 1
  139.             end
  140.         else --move to -x
  141.             turn(cfacing,2)
  142.             cfacing = 2
  143.             while cz > tz do
  144.                 turtle.forward()
  145.                 cz = cz - 1
  146.             end
  147.         end
  148.     end
  149.     refuel()
  150.     if cy > ty then
  151.         while cy > ty do
  152.             if not turtle.down() then
  153.                 turtle.digDown()
  154.             else
  155.                 cy = cy - 1
  156.             end
  157.         end
  158.     end
  159. end
  160.  
  161. moveToPos(x,y,z)
  162. rednet.broadcast(string.format("arrived at: x: %s, y: %s, z: %s",x,y,z))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement