Advertisement
Wassaa

goto

Oct 26th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.91 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.    
  257. elseif #args == 5 and args[1] == "special" and args[2] == "offset" and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) then
  258.     newx, newy, newz = tonumber(args[3]) + x, tonumber(args[4]) + y, tonumber(args[5]) + z
  259.     print("Going to x: "..newx + x..", y: "..newy + y..", z: "..newz + z.."...")
  260.    
  261. --elseif #args == 4 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) then
  262.    -- newx, newy, newz, newf = tonumber(args[1]), tonumber(args[2]), tonumber(args[3]), tonumber(args[4])
  263.     --print("Going to x: "..newx..", y: "..newy..", z: "..newz..", f: "..newf.."...")
  264. 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
  265.     newx, newy, newz, newf = tonumber(args[2]), tonumber(args[3]), tonumber(args[4]), tonumber(args[5])
  266.     x, y, z, f = tonumber(args[6]), tonumber(args[7]), tonumber(args[8]), tonumber(args[9])
  267. elseif #args == 5 and args[1] == "add" and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) then
  268.     local location, xname, yname, zname = args[2], args[3], args[4], args[5]
  269.     if not fs.exists("locations/"..location) then
  270.         local fLocation = fs.open("locations/"..location,"w")
  271.         fLocation.writeLine(xname)
  272.         fLocation.writeLine(yname)
  273.         fLocation.writeLine(zname)
  274.         fLocation.close()
  275.         print("Location \""..location.."\" added.")
  276.     else
  277.         print("This location already exists. Would you like to replace it?\n\(y/n\)")
  278.         while true do
  279.             event, character = os.pullEvent()
  280.             if event == "char" and character == "y" then
  281.                 local fLocation = fs.open("locations/"..location,"w")
  282.                 fLocation.writeLine(xname)
  283.                 fLocation.writeLine(yname)
  284.                 fLocation.writeLine(zname)
  285.                 fLocation.close()
  286.                 print("Location changed.")
  287.                 break
  288.             elseif event == "char" and character == "n" then
  289.                 print("Location not changed.")
  290.                 break
  291.             end
  292.         end
  293.     end
  294.     error()
  295. else
  296.     print("To goto coords, use: \"goto <x> <z>\" or \"goto <x> <y> <z>\"")
  297.     print("To goto a set location, use: \"goto <name>\"")
  298.     print("To set a new location, use: \"goto add <name> <x> <y> <z>\"")
  299.     error()
  300. end
  301.  
  302. if args[1] ~= "special" then
  303.     x, y, z = gps.locate(5,false)
  304. end
  305. if not x or not y or not z then
  306.     error("Out of GPS range")
  307. end
  308. local distance = findDistance(x,y,z,newx,newy,newz)
  309. local fuelLevel = turtle.getFuelLevel()
  310. if type(fuelLevel) == "string" then
  311.     fuelLevel = 9001e9001
  312. end
  313. if distance > fuelLevel then
  314.     error("Not enough fuel to travel so far!")
  315. end
  316.  
  317.  
  318. if args[1] ~= "special" then
  319.     turtle.up()
  320.     turtle.up()
  321.     turtle.up()
  322.     f = getDirection()
  323. end
  324.  
  325. if newy > y then
  326.     gotoy(y,newy)
  327. end
  328. if newx ~= x then
  329.     gotox(x,newx,f)
  330. end
  331. if newz ~= z then
  332.     gotoz(z,newz,f)
  333. end
  334. if newy < y then
  335.     gotoy(y,newy)
  336. end
  337. if newf ~= f then
  338.     if f == 1 then
  339.         turtle.turnLeft()
  340.     elseif f == 2 then
  341.         turtle.turnLeft()
  342.         turtle.turnLeft()
  343.     elseif f == 3 then
  344.         turtle.turnRight()
  345.     end
  346.     if newf == 1 then
  347.         turtle.turnRight()
  348.     elseif newf == 2 then
  349.         turtle.turnLeft()
  350.         turtle.turnLeft()
  351.     elseif newf == 3 then
  352.         turtle.turnLeft()
  353.     end
  354. end
  355.  
  356. if args[1] ~= "special" then
  357.     turtle.down()
  358.     turtle.down()
  359.     turtle.down()
  360.     print("Done traveling!")
  361. end
  362.  
  363. if args[1] ~= "special" then
  364.     rednet.close("left")
  365. end
  366. 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