Advertisement
Guest User

gpsapi.lua

a guest
Sep 18th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1.  
  2. local awal = vector.new(gps.locate())
  3. local sideawal ="east"
  4. local side = "east"
  5. local array = {"east","south","west","north"} --turn right
  6.  
  7. ------------------------------------ FUNCTION GPS ------------------------------
  8. function changeArray()
  9.     if side == "south" then
  10.         array = {"south","west","north","east"}
  11.     elseif side == "north" then
  12.         array = {"north","east","south","west"}
  13.     elseif side == "east" then
  14.         array = {"east","south","west","north"}
  15.     elseif side == "west" then
  16.         array = {"west","north","east","south"}
  17.     end
  18. end
  19.  
  20. function setAwal(vector)
  21.     awal = vector
  22. end
  23.  
  24. function getSide()
  25.     return side
  26. end
  27.  
  28. function checkside()
  29.     local a = vector.new(gps.locate())
  30.     turtle.dig()
  31.     turtle.forward()
  32.     local b = vector.new(gps.locate())
  33.     local c = a-b
  34.     if c.z == -1 then
  35.         side = "south"
  36.     elseif c.z == 1 then
  37.         side = "north"
  38.     elseif c.x == -1 then
  39.         side ="east"
  40.     elseif c.x == 1 then
  41.         side ="west"
  42.     end
  43.     changeArray()
  44.     a = b
  45.     turtle.back()
  46. end
  47.  
  48. function changeside(wside)
  49.     local i = 1
  50.     while side ~= wside do
  51.         i = i + 1
  52.         turtle.turnRight()
  53.         side = array[i]
  54.     end
  55.     changeArray()
  56. end
  57.  
  58. function goTo(destination,sideaw)
  59.     turtle.dig()
  60.     while not turtle.forward() do
  61.         turtle.turnRight()
  62.         turtle.dig()
  63.     end
  64.     checkside()
  65.     local posnow = vector.new(gps.locate())
  66.     local distance = destination-posnow
  67.    
  68.     if distance.y >0 then
  69.         for i = distance.y, 1, -1 do
  70.             turtle.digUp()
  71.             turtle.up()
  72.         end
  73.     elseif distance.y <0 then
  74.         for i = distance.y, -1 do
  75.             turtle.digDown()
  76.             turtle.down()
  77.         end
  78.     end
  79.    
  80.     local wside=nil
  81.     -- change side to x
  82.     if distance.x >0 then
  83.         wside = "east"
  84.         changeside(wside)
  85.     elseif distance.x <0 then
  86.         wside = "west"
  87.         changeside(wside)
  88.     end
  89.    
  90.    
  91.     --move to x
  92.     if distance.x >0 then
  93.         for i = distance.x, 1, -1 do
  94.             turtle.dig()
  95.             turtle.forward()
  96.         end
  97.     elseif distance.x <0 then
  98.         for i = distance.x, -1 do
  99.             turtle.dig()
  100.             turtle.forward()
  101.         end
  102.     end
  103.    
  104.     -- change side to z
  105.     if distance.z >0 then
  106.         wside = "south"
  107.         changeside(wside)
  108.     elseif distance.z <0 then
  109.         wside = "north"
  110.         changeside(wside)
  111.     end
  112.    
  113.     --move to z
  114.     if distance.z >0 then
  115.         for i = distance.z, 1, -1 do
  116.             turtle.dig()
  117.             turtle.forward()
  118.         end
  119.     elseif distance.z <0 then
  120.         for i = distance.z , -1 do
  121.             turtle.dig()
  122.             turtle.forward()
  123.         end
  124.     end
  125.     changeside(sideaw)
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement