uepyon

goto.lua

Mar 23rd, 2024 (edited)
207
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function getFaceTo()
  2.   local pos0 = vector.new(gps.locate())
  3.   turtle.forward()
  4.   local pos1 = vector.new(gps.locate())
  5.   local dist = pos1 - pos0
  6.   turtle.back()
  7.   local faceto = ""
  8.   if dist.x > 0 then
  9.     faceto = "west"
  10.   elseif dist.x < 0 then
  11.     faceto = "east"
  12.   elseif dist.z > 0 then
  13.     faceto = "north"
  14.   else
  15.     faceto = "south"
  16.   end
  17.   return faceto
  18. end
  19.  
  20. local function forBack(dist)
  21.   d = dist
  22.   if d < 0 then
  23.     d = -d
  24.   end  
  25.   for i=1, d do
  26.     if dist > 0 then
  27.       turtle.forward()
  28.     else
  29.       turtle.back()
  30.     end
  31.   end
  32. end
  33.  
  34. local function upDown(dist)
  35.   d = dist
  36.   if d < 0 then
  37.     d = -d
  38.   end  
  39.   for i=1, d do
  40.     if dist > 0 then
  41.       turtle.up()
  42.     else
  43.       turtle.down()
  44.     end
  45.   end
  46. end
  47.  
  48. local function moveAxisX(faceto, curpos, target)
  49.   local dist = target - curpos
  50.   if faceto == "north" then
  51.     turtle.turnLeft()
  52.     forBack(dist.x)
  53.     turtle.turnRight()
  54.   elseif faceto == "south" then
  55.     turtle.turnRight()
  56.     forBack(dist.x)
  57.     turtle.turnLeft()
  58.   elseif faceto == "east" then
  59.     turtle.turnLeft()
  60.     turtle.turnLeft()
  61.     forBack(dist.x)
  62.     turtle.turnRight()
  63.     turtle.turnRight()
  64.   else
  65.     forBack(dist.x)
  66.   end
  67. end
  68.  
  69. local function moveAxisZ(faceto, curpos, target)
  70.   local dist = target - curpos
  71.   if faceto == "north" then
  72.     forBack(dist.z)
  73.   elseif faceto == "south" then
  74.     turtle.turnLeft()
  75.     turtle.turnLeft()
  76.     forBack(dist.z)
  77.     turtle.turnRight()
  78.     turtle.turnRight()
  79.   elseif faceto == "east" then
  80.     turtle.turnLeft()
  81.     forBack(dist.z)
  82.     turtle.turnRight()
  83.   else
  84.     turtle.turnRight()
  85.     forBack(dist.z)
  86.     turtle.turnLeft()
  87.   end
  88. end
  89.  
  90. local function moveAxisY(curpos, target)
  91.   local dist = target - curpos
  92.   upDown(dist.y)
  93. end
  94.  
  95. local function dispStatus(pos, target, faceto)
  96.   print("pos:"..tostring(pos))
  97.   print("faceTo:"..faceto)
  98.   print("dist:"..tostring(target - pos))
  99. end
  100.  
  101. args = {...}
  102. x = tonumber(args[1])
  103. y = tonumber(args[2])
  104. z = tonumber(args[3])
  105. target = vector.new(x, y, z)
  106.  
  107. rednet.open("left")
  108. curpos = vector.new(gps.locate())
  109.  
  110. faceto = getFaceTo()
  111. dispStatus(curpos, target, faceto)
  112.  
  113. moveAxisZ(faceto, curpos, target)
  114. moveAxisY(curpos, target)
  115. moveAxisX(faceto, curpos, target)
Advertisement
Comments
  • uepyon
    2 years (edited)
    Comment was deleted
  • uepyon
    2 years
    # text 0.08 KB | 0 0
    1. goto x y z
    2. Goto function for moving a turtle to desired coordinates with gps.
Add Comment
Please, Sign In to add comment