kovakovi2000

CC: GPS goto

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