Advertisement
Guest User

goto

a guest
Apr 6th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.59 KB | None | 0 0
  1. args = {...}
  2.  
  3. if not fs.isDir("locations") then
  4.     fs.makeDir("locations")
  5. end
  6.  
  7. local function getDirection()
  8.     local f = 0
  9.     local x, y, z = gps.locate(5,false)
  10.     y = nil
  11.     if args[1] == "special" then
  12.         if turtle.detect() then
  13.             if not turtle.dig() then
  14.                 error("Tried to dig bedrock.")
  15.             end
  16.         end
  17.         while not turtle.forward() do
  18.             turtle.attack()
  19.         end
  20.     else
  21.         if not turtle.forward() then
  22.             error("I am against a wall.")
  23.         end
  24.     end
  25.     local newx, newy, newz = gps.locate(5,false)
  26.     newy = nil
  27.     turtle.back()
  28.     if newz > z then
  29.         f = 0
  30.     elseif newx < x then
  31.         f = 1
  32.     elseif newz < z then
  33.         f = 2
  34.     elseif newx > x then
  35.         f = 3
  36.     end
  37.     return f
  38. end
  39. local function gotox(x,newx,f)
  40.     if f == 0 then
  41.         turtle.turnRight()
  42.     elseif f == 2 then
  43.         turtle.turnLeft()
  44.     elseif f == 3 then
  45.         turtle.turnRight()
  46.         turtle.turnRight()
  47.     end
  48.     if newx < x then
  49.         local j = x - newx
  50.         for i=1,j do
  51.             if args[1] == "special" then
  52.                 local i = 0
  53.                 while not turtle.forward() do
  54.                     if not turtle.dig() then
  55.                         i = i + 1
  56.                         turtle.attack()
  57.                         if i == 30 then
  58.                             error("Path blocked.")
  59.                         end
  60.                     end
  61.                 end
  62.             else
  63.                 if not turtle.forward() then
  64.                     error("Path blocked.")
  65.                 end
  66.             end
  67.         end
  68.     elseif newx > x then
  69.         local j = newx - x
  70.         turtle.turnRight()
  71.         turtle.turnRight()
  72.         for i=1,j do
  73.             if args[1] == "special" then
  74.                 local i = 0
  75.                 while not turtle.forward() do
  76.                     if not turtle.dig() then
  77.                         i = i + 1
  78.                         turtle.attack()
  79.                         if i == 30 then
  80.                             error("Path blocked.")
  81.                         end
  82.                     end
  83.                 end
  84.             else
  85.                 if not turtle.forward() then
  86.                     error("Path blocked.")
  87.                 end
  88.             end
  89.         end
  90.         turtle.turnLeft()
  91.         turtle.turnLeft()
  92.     end
  93.         if f == 0 then
  94.         turtle.turnLeft()
  95.     elseif f == 2 then
  96.         turtle.turnRight()
  97.     elseif f == 3 then
  98.         turtle.turnLeft()
  99.         turtle.turnLeft()
  100.     end
  101. end
  102. local function gotoy(y,newy)
  103.     if newy > y then
  104.         local j = newy - y
  105.         for i=1,j do
  106.             if args[1] == "special" then
  107.                 local i = 0
  108.                 while not turtle.up() do
  109.                     if not turtle.digUp() then
  110.                         i = i + 1
  111.                         turtle.attackUp()
  112.                         if i == 30 then
  113.                             error("Path blocked.")
  114.                         end
  115.                     end
  116.                 end
  117.             else
  118.                 if not turtle.up() then
  119.                     error("Path blocked.")
  120.                 end
  121.             end
  122.         end
  123.     elseif newy < y then
  124.         local j = y - newy
  125.         for i=1,j do
  126.             if args[1] == "special" then
  127.                 local i = 0
  128.                 while not turtle.down() do
  129.                     if not turtle.digDown() then
  130.                         i = i + 1
  131.                         turtle.attackDown()
  132.                         if i == 30 then
  133.                             error("Path blocked.")
  134.                         end
  135.                     end
  136.                 end
  137.             else
  138.                 if not turtle.down() then
  139.                     error("Path blocked.")
  140.                 end
  141.             end
  142.         end
  143.     end
  144. end
  145. local function gotoz(z,newz,f)
  146.     if f == 1 then
  147.         turtle.turnLeft()
  148.     elseif f == 2 then
  149.         turtle.turnLeft()
  150.         turtle.turnLeft()
  151.     elseif f == 3 then
  152.         turtle.turnRight()
  153.     end
  154.     if newz > z then
  155.         local j = newz - z
  156.         for i=1,j do
  157.             if args[1] == "special" then
  158.                 local i = 0
  159.                 while not turtle.forward() do
  160.                     if not turtle.dig() then
  161.                         i = i + 1
  162.                         turtle.attack()
  163.                         if i == 30 then
  164.                             error("Path blocked.")
  165.                         end
  166.                     end
  167.                 end
  168.             else
  169.                 if not turtle.forward() then
  170.                     error("Path blocked.")
  171.                 end
  172.             end
  173.         end
  174.     elseif newz < z then
  175.         turtle.turnRight()
  176.         turtle.turnRight()
  177.         local j = z - newz
  178.         for i=1,j do
  179.             if args[1] == "special" then
  180.                 local i = 0
  181.                 while not turtle.forward() do
  182.                     if not turtle.dig() then
  183.                         i = i + 1
  184.                         turtle.attack()
  185.                         if i == 30 then
  186.                             error("Path blocked.")
  187.                         end
  188.                     end
  189.                 end
  190.             else
  191.                 if not turtle.forward() then
  192.                     error("Path blocked.")
  193.                 end
  194.             end
  195.         end
  196.         turtle.turnLeft()
  197.         turtle.turnLeft()
  198.     end
  199.     if f == 1 then
  200.         turtle.turnRight()
  201.     elseif f == 2 then
  202.         turtle.turnRight()
  203.         turtle.turnRight()
  204.     elseif f == 3 then
  205.         turtle.turnLeft()
  206.     end
  207. end
  208. local function findDistance(x,y,z,newx,newy,newz)
  209.     local distance = 0
  210.     local xDist = 0
  211.     local yDist = 0
  212.     local zDist = 0
  213.     if x > newx then
  214.         xDist = x - newx
  215.     elseif x < newx then
  216.         xDist = newx - x
  217.     end
  218.     if y > newy then
  219.         yDist = y - newy
  220.     elseif x < newx then
  221.         yDist = newy - y
  222.     end
  223.     if z > newz then
  224.         zDist = z - newz
  225.     elseif z < newz then
  226.         zDist = newz - z
  227.     end
  228.     distance = xDist + yDist + zDist
  229.     return distance
  230. end
  231.  
  232. if args[1] ~= "special" then
  233.     rednet.open("left")
  234. end
  235.  
  236. x, y, z, f, newx, newy, newz, newf = 0, 0, 0, 0, 0, 0, 0, 0
  237.  
  238. if #args == 1 then
  239.     local location = args[1]
  240.     if fs.exists("locations/"..location) then
  241.         local fLocation = fs.open("locations/"..location,"r")
  242.         newx = tonumber(fLocation.readLine())
  243.         newy = tonumber(fLocation.readLine())
  244.         newz = tonumber(fLocation.readLine())
  245.         fLocation.close()
  246.         print("Going to "..location.."...")
  247.     else
  248.         error("Unknown location.")
  249.     end
  250. elseif #args == 2 and tonumber(args[1]) and tonumber(args[2]) then
  251.     newx, newz = tonumber(args[1]), tonumber(args[2])
  252.     print("Going to x: "..newx..", z: "..newz.."...")
  253. elseif #args == 3 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3])then
  254.     newx, newy, newz = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
  255.     print("Going to x: "..newx..", y: "..newy..", z: "..newz.."...")
  256. elseif #args == 4 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) then
  257.     newx, newy, newz, newf = tonumber(args[1]), tonumber(args[2]), tonumber(args[3]), tonumber(args[4])
  258.     print("Going to x: "..newx..", y: "..newy..", z: "..newz..", f: "..newf.."...")
  259. elseif #args == 9 and args[1] == "special" and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) and tonumber(args[6]) and tonumber(args[6]) and tonumber(args[7]) and tonumber(args[8]) and tonumber(args[9]) then
  260.     newx, newy, newz, newf = tonumber(args[2]), tonumber(args[3]), tonumber(args[4]), tonumber(args[5])
  261.     x, y, z, f = tonumber(args[6]), tonumber(args[7]), tonumber(args[8]), tonumber(args[9])
  262. elseif #args == 5 and args[1] == "add" and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) then
  263.     local location, xname, yname, zname = args[2], args[3], args[4], args[5]
  264.     if not fs.exists("locations/"..location) then
  265.         local fLocation = fs.open("locations/"..location,"w")
  266.         fLocation.writeLine(xname)
  267.         fLocation.writeLine(yname)
  268.         fLocation.writeLine(zname)
  269.         fLocation.close()
  270.         print("Location \""..location.."\" added.")
  271.     else
  272.         print("This location already exists. Would you like to replace it?\n\(y/n\)")
  273.         while true do
  274.             event, character = os.pullEvent()
  275.             if event == "char" and character == "y" then
  276.                 local fLocation = fs.open("locations/"..location,"w")
  277.                 fLocation.writeLine(xname)
  278.                 fLocation.writeLine(yname)
  279.                 fLocation.writeLine(zname)
  280.                 fLocation.close()
  281.                 print("Location changed.")
  282.                 break
  283.             elseif event == "char" and character == "n" then
  284.                 print("Location not changed.")
  285.                 break
  286.             end
  287.         end
  288.     end
  289.     error()
  290. else
  291.     print("To goto coords, use: \"goto <x> <z>\" or \"goto <x> <y> <z>\"")
  292.     print("To goto a set location, use: \"goto <name>\"")
  293.     print("To set a new location, use: \"goto add <name> <x> <y> <z>\"")
  294.     error()
  295. end
  296.  
  297. if args[1] ~= "special" then
  298.     x, y, z = gps.locate(5,false)
  299. end
  300. if not x or not y or not z then
  301.     error("Out of GPS range")
  302. end
  303. local distance = findDistance(x,y,z,newx,newy,newz)
  304. local fuelLevel = turtle.getFuelLevel()
  305. if type(fuelLevel) == "string" then
  306.     fuelLevel = 9001e9001
  307. end
  308. if distance > fuelLevel then
  309.     error("Not enough fuel to travel so far!")
  310. end
  311.  
  312.  
  313. if args[1] ~= "special" then
  314.     turtle.up()
  315.     turtle.up()
  316.     turtle.up()
  317.     f = getDirection()
  318. end
  319.  
  320. if newy > y then
  321.     gotoy(y,newy)
  322. end
  323. if newx ~= x then
  324.     gotox(x,newx,f)
  325. end
  326. if newz ~= z then
  327.     gotoz(z,newz,f)
  328. end
  329. if newy < y then
  330.     gotoy(y,newy)
  331. end
  332. if newf ~= f then
  333.     if f == 1 then
  334.         turtle.turnLeft()
  335.     elseif f == 2 then
  336.         turtle.turnLeft()
  337.         turtle.turnLeft()
  338.     elseif f == 3 then
  339.         turtle.turnRight()
  340.     end
  341.     if newf == 1 then
  342.         turtle.turnRight()
  343.     elseif newf == 2 then
  344.         turtle.turnLeft()
  345.         turtle.turnLeft()
  346.     elseif newf == 3 then
  347.         turtle.turnLeft()
  348.     end
  349. end
  350.  
  351. if args[1] ~= "special" then
  352.     turtle.down()
  353.     turtle.down()
  354.     turtle.down()
  355.     print("Done traveling!")
  356. end
  357.  
  358. if args[1] ~= "special" then
  359.     rednet.close("left")
  360. end
  361. args, x, y, z, f, newx, newy, newz, newf = nil, nil, nil, nil, nil, nil, nil, nil, nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement