Advertisement
Wassaa

newgoto

Oct 27th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 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 turtle.detect() then
  12.         if not turtle.dig() then
  13.             error("Tried to dig bedrock.")
  14.         end
  15.     end
  16.     while not turtle.forward() do
  17.         turtle.attack()
  18.     end
  19.     local newx, newy, newz = gps.locate(5,false)
  20.     newy = nil
  21.     turtle.back()
  22.     if newz > z then
  23.         f = 0
  24.     elseif newx < x then
  25.         f = 1
  26.     elseif newz < z then
  27.         f = 2
  28.     elseif newx > x then
  29.         f = 3
  30.     end
  31.     print(f)
  32.     return f
  33.  
  34. end
  35.  
  36. --getDirection()
  37.  
  38. local function moveTurtle(j)
  39.     for i=1,j do
  40.         local i = 0
  41.         while not turtle.forward() do
  42.             if not turtle.dig() then
  43.                 i = i + 1
  44.                 turtle.attack()
  45.                 if i == 30 then
  46.                     error("Path blocked.")
  47.                 end
  48.             end
  49.         end
  50.     end
  51. end
  52.  
  53. local function gotox(x,newx,f)
  54.     if f == 0 then
  55.         turtle.turnRight()
  56.     elseif f == 2 then
  57.         turtle.turnLeft()
  58.     elseif f == 3 then
  59.         turtle.turnRight()
  60.         turtle.turnRight()
  61.     end
  62.    
  63.     if newx < x then
  64.         local j = x - newx
  65.         moveTurtle(j)
  66.     elseif newx > x then
  67.         local j = newx - x
  68.         turtle.turnRight()
  69.         turtle.turnRight()
  70.         moveTurtle(j)
  71.         turtle.turnLeft()
  72.         turtle.turnLeft()
  73.     end
  74.     if f == 0 then
  75.         turtle.turnLeft()
  76.     elseif f == 2 then
  77.         turtle.turnRight()
  78.     elseif f == 3 then
  79.         turtle.turnLeft()
  80.         turtle.turnLeft()
  81.     end
  82. end
  83.  
  84. local function gotoz(z,newz,f)
  85.     if f == 1 then
  86.         turtle.turnLeft()
  87.     elseif f == 2 then
  88.         turtle.turnLeft()
  89.         turtle.turnLeft()
  90.     elseif f == 3 then
  91.         turtle.turnRight()
  92.     end
  93.     if newz > z then
  94.         local j = z - newz
  95.         moveTurtle(j)
  96.     elseif newz < z then
  97.         local j = newz - z
  98.         turtle.turnRight()
  99.         turtle.turnRight()
  100.         moveTurtle(j)
  101.         turtle.turnLeft()
  102.         turtle.turnLeft()
  103.     end
  104.     if f == 1 then
  105.         turtle.turnRight()
  106.     elseif f == 2 then
  107.         turtle.turnRight()
  108.         turtle.turnRight()
  109.     elseif f == 3 then
  110.         turtle.turnLeft()
  111.     end
  112. end
  113.  
  114. local function gotoy(y,newy)
  115.     if newy > y then
  116.         local j = newy - y
  117.         for i=1,j do
  118.             local i = 0
  119.             while not turtle.up() do
  120.                 if not turtle.digUp() then
  121.                     i = i + 1
  122.                     turtle.attackUp()
  123.                     if i == 30 then
  124.                         error("Path blocked.")
  125.                     end
  126.                 end
  127.             end
  128.         end
  129.     elseif newy < y then
  130.         local j = y - newy
  131.         for i=1,j do
  132.             local i = 0
  133.             while not turtle.down() do
  134.                 if not turtle.digDown() then
  135.                     i = i + 1
  136.                     turtle.attackDown()
  137.                     if i == 30 then
  138.                         error("Path blocked.")
  139.                     end
  140.                 end
  141.             end
  142.         end
  143.     end
  144. end
  145.  
  146. local function findDistance(x,y,z,newx,newy,newz)
  147.     local distance = 0
  148.     local xDist = 0
  149.     local yDist = 0
  150.     local zDist = 0
  151.     if x > newx then
  152.         xDist = x - newx
  153.     elseif x < newx then
  154.         xDist = newx - x
  155.     end
  156.     if y > newy then
  157.         yDist = y - newy
  158.     elseif x < newx then
  159.         yDist = newy - y
  160.     end
  161.     if z > newz then
  162.         zDist = z - newz
  163.     elseif z < newz then
  164.         zDist = newz - z
  165.     end
  166.     distance = xDist + yDist + zDist
  167.     return distance
  168. end
  169.  
  170. ----------------- functions done
  171.  
  172. rednet.open("left")
  173. x, y, z, f, newx, newy, newz, newf = 0, 0, 0, 0, 0, 0, 0, 0
  174. x, y, z = gps.locate(5,false)
  175. ---------- args -----------
  176.  
  177. if #args == 1 then
  178.     local location = args[1]
  179.     if fs.exists("locations/"..location) then
  180.         local fLocation = fs.open("locations/"..location,"r")
  181.         newx = tonumber(fLocation.readLine())
  182.         newy = tonumber(fLocation.readLine())
  183.         newz = tonumber(fLocation.readLine())
  184.         fLocation.close()
  185.         print("Going to "..location.."...")
  186.     else
  187.         error("Unknown location.")
  188.     end
  189. elseif #args == 2 and tonumber(args[1]) and tonumber(args[2]) then
  190.     newx, newz = tonumber(args[1]), tonumber(args[2])
  191.     print("Going to x: "..newx..", z: "..newz.."...")
  192. elseif #args == 3 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3])then
  193.     newx, newy, newz = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
  194.     print("Going to x: "..newx..", y: "..newy..", z: "..newz.."...")
  195. elseif #args == 4 and args[1] == "offset" and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) then
  196.     newx, newy, newz = (tonumber(args[2]) + x), (tonumber(args[3]) + y), (tonumber(args[4]) + z)
  197.     print("Going to x: "..newx..", y: "..newy..", z: "..newz.."...")
  198. elseif #args == 5 and args[1] == "add" and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) then
  199.     local location, xname, yname, zname = args[2], args[3], args[4], args[5]
  200.     if not fs.exists("locations/"..location) then
  201.         local fLocation = fs.open("locations/"..location,"w")
  202.         fLocation.writeLine(xname)
  203.         fLocation.writeLine(yname)
  204.         fLocation.writeLine(zname)
  205.         fLocation.close()
  206.         print("Location \""..location.."\" added.")
  207.     else
  208.         print("This location already exists. Would you like to replace it?\n\(y/n\)")
  209.         while true do
  210.             event, character = os.pullEvent()
  211.             if event == "char" and character == "y" then
  212.                 local fLocation = fs.open("locations/"..location,"w")
  213.                 fLocation.writeLine(xname)
  214.                 fLocation.writeLine(yname)
  215.                 fLocation.writeLine(zname)
  216.                 fLocation.close()
  217.                 print("Location changed.")
  218.                 break
  219.             elseif event == "char" and character == "n" then
  220.                 print("Location not changed.")
  221.                 break
  222.             end
  223.         end
  224.     end
  225.     error()
  226. else
  227.     print("To goto coords, use: \"goto <x> <z>\" or \"goto <x> <y> <z>\"")
  228.     print("To goto a set location, use: \"goto <name>\"")
  229.     print("To set a new location, use: \"goto add <name> <x> <y> <z>\"")
  230.     print("To goto coords based on current loc, use: \"goto offset <x> <y> <z>\"")
  231.     error()
  232. end
  233.  
  234. ----------------- args settings done -----------------
  235.  
  236.  
  237. local distance = findDistance(x,y,z,newx,newy,newz)
  238. local fuelLevel = turtle.getFuelLevel()
  239.  
  240. if distance > fuelLevel then
  241.     error("Not enough fuel to travel so far!")
  242. end
  243. f = getDirection()
  244.  
  245.  
  246. if newx ~= x then
  247.     gotox(x,newx,f)
  248. end
  249. if newz ~= z then
  250.     gotoz(z,newz,f)
  251. end
  252. if newy ~= y then
  253.     gotoy(y,newy)
  254. end
  255. if newf ~= f then
  256.     if f == 1 then
  257.         turtle.turnLeft()
  258.     elseif f == 2 then
  259.         turtle.turnLeft()
  260.         turtle.turnLeft()
  261.     elseif f == 3 then
  262.         turtle.turnRight()
  263.     end
  264.     if newf == 1 then
  265.         turtle.turnRight()
  266.     elseif newf == 2 then
  267.         turtle.turnLeft()
  268.         turtle.turnLeft()
  269.     elseif newf == 3 then
  270.         turtle.turnLeft()
  271.     end
  272. end
  273.  
  274. print("Done traveling!")
  275.  
  276. 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