BruceWplays

Goto V5 old

Sep 20th, 2022
78
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.     local 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 > 10 then
  37.             os.reboot()
  38.         end
  39.     end
  40.    
  41.     function PL()
  42.       --print("Going to:")
  43.       --write(gx) write(" ") write(gy) write(" ") print(gz)
  44.       end
  45.     function c_face()
  46.     if face >= 5 then
  47.       face = 1
  48.     elseif face <= 0 then
  49.       face = 4
  50.     end
  51.     end
  52.     function get_Face()
  53.     while turtle.forward() == false do
  54.         tturned = 0
  55.        if turtle.up() == false then
  56.             --turtle.digUp()
  57.        end
  58.       end
  59.    
  60.     nx,ny,nz = gps.locate(3,false)
  61.     if nx > cx then
  62.       face = 1
  63.     end
  64.     if nx < cx then
  65.       face = 3
  66.     end
  67.     if nz > cz then
  68.       face = 2
  69.     end
  70.     if nz < cz then
  71.       face = 4
  72.     end
  73.     end
  74.     function calc()
  75.     x = nx - gx
  76.     y = ny - gy
  77.     z = nz - gz
  78.     end
  79.     function setDir(dir)
  80.         noredoturndir = 0
  81.       c_face()
  82.       if dir == 4 then
  83.         if face == 1 then
  84.             gototurnleft = 1
  85.             --print("E-to-N")
  86.             noredoturndir = 1
  87.         end
  88.       end
  89.       if dir == 1 then
  90.         if face == 4 then
  91.             gototurnleft = 0
  92.             --print("N-to-E")
  93.             noredoturndir = 1
  94.         end
  95.       end
  96.    
  97.       if noredoturndir == 0 then
  98.          if face >= dir then
  99.             gototurnleft = 1
  100.          end
  101.          if face < dir then
  102.             gototurnleft = 0
  103.         end
  104.       end
  105.      
  106.       if dir == 4 then
  107.         if face == 1 then
  108.             gototurnleft = 1
  109.             --print("E-to-N")
  110.             noredoturndir = 1
  111.         end
  112.       end
  113.       if dir == 1 then
  114.         if face == 4 then
  115.             gototurnleft = 0
  116.             --print("N-to-E")
  117.             noredoturndir = 1
  118.         end
  119.       end
  120.       while dir ~= face do
  121.         c_face()
  122.         if gototurnleft == 1 then
  123.             turtle.turnLeft()
  124.             face = face - 1
  125.             tturnup()
  126.         end
  127.         if gototurnleft == 0 then
  128.             turtle.turnRight()
  129.             face = face + 1
  130.             tturnup()
  131.         end
  132.       c_face()
  133.       end
  134.       noredoturndir = 0
  135.     end
  136.     function f()
  137.     while not turtle.forward() do
  138.         tturned = 0
  139.        if not turtle.up()  then
  140.             --turtle.digUp()
  141.        end
  142.        y = y + 1
  143.       end
  144.     end
  145.     function go()
  146.         tturned = 0
  147.     if x < 0 then
  148.       setDir(1)
  149.       while x ~= 0 do
  150.        f()
  151.        x = x + 1
  152.       end
  153.     end
  154.     if x > 0 then
  155.       setDir(3)
  156.       while x ~= 0 do
  157.        f()
  158.        x = x - 1
  159.       end
  160.     end
  161.     if z < 0 then
  162.       setDir(2)
  163.       while z ~= 0 do
  164.        f()
  165.        z = z + 1
  166.       end
  167.     end
  168.     if z > 0 then
  169.       setDir(4)
  170.       while z ~= 0 do
  171.        f()
  172.        z = z - 1
  173.       end
  174.     end
  175.     while y < 0 do
  176.         tturned = 0
  177.       if turtle.up() ~= true then
  178.        --turtle.digUp()
  179.       end
  180.       y = y + 1
  181.     end
  182.     while y > 0 do
  183.         tturned = 0
  184.       if turtle.down() ~= true then
  185.        --turtle.digDown()
  186.       end
  187.       y = y - 1
  188.     end
  189.     end
  190.    
  191.    
  192.    
  193.     tonumber(gx)
  194.     tonumber(gy)
  195.     tonumber(gz)
  196.     PL()
  197.     get_Face()
  198.     calc()
  199.     go()
  200.     if enddir ~= nil then
  201.         setDir(enddir)
  202.     end
  203.    
  204.     end
  205.     end
  206.    
  207.    
  208.    
  209.     function goto(Workinggototable)  
  210.       gtloop = 0
  211.       gtloopsleep = 0.0001
  212.       gototable = {}                    
  213.       goit = 1
  214.       while true do
  215.         while goit < 5 do
  216.           if Workinggototable[goit] ~= nil then
  217.               table.insert(gototable, Workinggototable[goit])
  218.           elseif Workinggototable[goit] == nil then
  219.               table.insert(gototable, Workinggototable[goit])
  220.               break
  221.           end
  222.           goit = goit + 1
  223.         end
  224.         gotoapi(gototable[1], gototable[2], gototable[3], gototable[4])
  225.         local ecx,ecy,ecz = gps.locate(5,false)
  226.         if ecx == tonumber(gototable[1]) then
  227.           if ecy == tonumber(gototable[2]) then
  228.             if ecz == tonumber(gototable[3]) then
  229.               break
  230.             end
  231.           end
  232.         end
  233.         if gtloop < 100 then
  234.           gtloop = gtloop + 1
  235.         end
  236.         if gtloop > 5 then    
  237.           if gtloopsleep > 2.5 then
  238.             gtloopsleep = 2.5
  239.           end
  240.           sleep(gtloopsleep)
  241.           gtloopsleep = gtloopsleep * 2
  242.         end
  243.       end
  244.     end
Add Comment
Please, Sign In to add comment