Advertisement
BruceWplays

Goto V6 Experimental

Sep 20th, 2022 (edited)
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function gotoapi(gx, gy, gz, enddirlet)
  2.     --local tArgs = {...}
  3.     face = 0
  4.     local r = 1
  5.     tru = 1
  6.     local tturned = 0
  7.     rednet.open("left")
  8.     local cx,cy,cz = gps.locate(2,false)
  9.     if (gx and gy and gz) == nil then
  10.       print("Incorrect usage of Function.")
  11.       --print("goto <x> <y> <z> (<Heading>)")
  12.     else
  13.     --local gx = tonumber(tArgs[1])
  14.     --local gy = tonumber(tArgs[2])
  15.     --local gz = tonumber(tArgs[3])
  16.     --local enddirlet = tArgs[4]
  17.     local x
  18.     local y
  19.     local z
  20.     if enddirlet ~= nil then
  21.         if enddirlet == "n" or enddirlet == "north" then
  22.             enddir = 4
  23.         end
  24.         if enddirlet == "s" or enddirlet == "south" then
  25.             enddir = 2
  26.         end
  27.         if enddirlet == "e" or enddirlet == "east" then
  28.             enddir = 1
  29.         end
  30.         if enddirlet == "w" or enddirlet == "west" then
  31.             enddir = 3
  32.         end
  33.     end
  34.     function tturnup()
  35.         tturned = tturned + 1
  36.         if tturned > 25 then
  37.             os.reboot()
  38.         end
  39.     end
  40.  
  41.     function c_face()
  42.         while face >= 5 do
  43.             face = face - 4
  44.         end
  45.         while face <= 0 do
  46.             face = face + 4
  47.         end
  48.     end
  49.  
  50.  
  51.  
  52.  
  53. function gps_face_math(cur_x,cur_z)
  54.   mnx,mny,mnz = gps.locate(3,false)
  55.   if mnx > cur_x then
  56.     face = 1
  57.   end
  58.   if mnx < cur_x then
  59.     face = 3
  60.   end
  61.   if mnz > cur_z then
  62.     face = 2
  63.   end
  64.   if mnz < cur_z then
  65.     face = 4
  66.   end
  67. end
  68. function forward_get_face()
  69.   tturned = 0
  70.   cux,cuy,cuz = gps.locate(3,false)
  71.   while turtle.forward() == false do
  72.     tturned = 0
  73.    if turtle.up() == false then
  74.         --turtle.digUp()
  75.    end
  76.   end
  77.   gps_face_math(cux,cuz)
  78.   return true
  79. end
  80.  
  81.  
  82.  
  83.     function get_Face()
  84.     while turtle.forward() == false do
  85.         tturned = 0
  86.        if turtle.up() == false then
  87.             --turtle.digUp()
  88.        end
  89.       end
  90.     nx,ny,nz = gps.locate(3,false)
  91.     gps_face_math(cx,cz)
  92.     end
  93.     function calc()
  94.     x = nx - gx
  95.     y = ny - gy
  96.     z = nz - gz
  97.     end
  98.     function setDir(dir)
  99.         noredoturndir = 0
  100.       c_face()
  101.       if dir == 4 then
  102.         if face == 1 then
  103.             gototurnleft = 1
  104.             --print("E-to-N")
  105.             noredoturndir = 1
  106.         end
  107.       end
  108.       if dir == 1 then
  109.         if face == 4 then
  110.             gototurnleft = 0
  111.             --print("N-to-E")
  112.             noredoturndir = 1
  113.         end
  114.       end
  115.    
  116.       if noredoturndir == 0 then
  117.          if face >= dir then
  118.             gototurnleft = 1
  119.          end
  120.          if face < dir then
  121.             gototurnleft = 0
  122.         end
  123.       end
  124.      
  125.       if dir == 4 then
  126.         if face == 1 then
  127.             gototurnleft = 1
  128.             --print("E-to-N")
  129.             noredoturndir = 1
  130.         end
  131.       end
  132.       if dir == 1 then
  133.         if face == 4 then
  134.             gototurnleft = 0
  135.             --print("N-to-E")
  136.             noredoturndir = 1
  137.         end
  138.       end
  139.       while dir ~= face do
  140.         c_face()
  141.         if gototurnleft == 1 then
  142.             turtle.turnLeft()
  143.             face = face - 1
  144.             tturnup()
  145.         end
  146.         if gototurnleft == 0 then
  147.             turtle.turnRight()
  148.             face = face + 1
  149.             tturnup()
  150.         end
  151.       c_face()
  152.       end
  153.       noredoturndir = 0
  154.     end
  155.     function f()
  156.         while not forward_get_face() do
  157.         tturned = 0
  158.         if not turtle.up()  then
  159.             --turtle.digUp()
  160.         end
  161.         y = y + 1
  162.         end
  163.     end
  164.     function go()
  165.         tturned = 0
  166.     if x < 0 then
  167.       setDir(1)
  168.       while x ~= 0 do
  169.        f()
  170.        x = x + 1
  171.       end
  172.     end
  173.     if x > 0 then
  174.       setDir(3)
  175.       while x ~= 0 do
  176.        f()
  177.        x = x - 1
  178.       end
  179.     end
  180.     if z < 0 then
  181.       setDir(2)
  182.       while z ~= 0 do
  183.        f()
  184.        z = z + 1
  185.       end
  186.     end
  187.     if z > 0 then
  188.       setDir(4)
  189.       while z ~= 0 do
  190.        f()
  191.        z = z - 1
  192.       end
  193.     end
  194.     while y < 0 do
  195.         tturned = 0
  196.       if turtle.up() ~= true then
  197.        --turtle.digUp()
  198.       end
  199.       y = y + 1
  200.     end
  201.     while y > 0 do
  202.         tturned = 0
  203.       if turtle.down() ~= true then
  204.        --turtle.digDown()
  205.       end
  206.       y = y - 1
  207.     end
  208.     end
  209.    
  210.    
  211.    
  212.     tonumber(gx)
  213.     tonumber(gy)
  214.     tonumber(gz)
  215.     get_Face()
  216.     calc()
  217.     go()
  218.     if enddir ~= nil then
  219.         setDir(enddir)
  220.     end
  221.    
  222.     end
  223.     end
  224.    
  225.    
  226.    
  227.     function goto(Workinggototable)  
  228.       gtloop = 0
  229.       gtloopsleep = 0.01
  230.       gtloopsleep2 = gtloopsleep ^ 2
  231.       gototable = {}                    
  232.       goit = 1
  233.       while true do
  234.         while goit < 5 do
  235.           if Workinggototable[goit] ~= nil then
  236.               table.insert(gototable, Workinggototable[goit])
  237.           elseif Workinggototable[goit] == nil then
  238.               table.insert(gototable, Workinggototable[goit])
  239.               break
  240.           end
  241.           goit = goit + 1
  242.         end
  243.         gotoapi(gototable[1], gototable[2], gototable[3], gototable[4])
  244.         local ecx,ecy,ecz = gps.locate(5,false)
  245.         if ecx == tonumber(gototable[1]) then
  246.           if ecy == tonumber(gototable[2]) then
  247.             if ecz == tonumber(gototable[3]) then
  248.                 if enddir ~= nil then
  249.                     if face == enddir then
  250.                         break
  251.                     end
  252.                 else
  253.                     break
  254.                 end
  255.             end
  256.           end
  257.         end
  258.         if gtloop < 100 then
  259.           gtloop = gtloop + 1
  260.         end
  261.         if gtloop > 5 then    
  262.           if gtloopsleep > 2.5 then
  263.             gtloopsleep = 2.5
  264.           end
  265.           if gtloopsleep2 > 2.5 then
  266.             gtloopsleep2 = 2.5
  267.           end
  268.           sleep(gtloopsleep2)
  269.           gtloopsleep = gtloopsleep * 1.1
  270.           gtloopsleep2 = gtloopsleep ^ 2
  271.         end
  272.       end
  273.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement