LookStickNow

coord_test

May 22nd, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1. function get_home_coords()
  2. local home = vector.new(gps.locate(5))
  3. if not home then
  4. print("Failed to get my home location.")
  5. else
  6. print("My home is at (" .. home.x .. ", " .. home.y .. ", " .. home.z ..")")
  7. end
  8. return home
  9. end
  10.  
  11. function get_curr_coords()
  12. local new_pos = vector.new(gps.locate(5))
  13. if not new_pos then
  14. print("Failed to get current location.")
  15. else
  16. print("I'm currently at (" .. new_pos.x .. ", " .. new_pos.y .. ", " .. new_pos.z .. ")")
  17. end
  18. return new_pos
  19. end
  20.  
  21. function get_curr_disp(home, current)
  22. return current - home
  23. end
  24.  
  25. function slideLeft()
  26. turtle.turnLeft()
  27. turtle.forward()
  28. turtle.turnRight()
  29. end
  30.  
  31. function slidesLeft(amount)
  32. turtle.turnLeft()
  33. for i=0,(amount-1),1 do
  34. turtle.forward()
  35. end
  36. turtle.turnRight()
  37. end
  38.  
  39. function slideRight()
  40. turtle.turnRight()
  41. turtle.forward()
  42. turtle.turnLeft()
  43. end
  44.  
  45. function slidesRight(amount)
  46. turtle.turnRight()
  47. for i=0,(amount-1),1 do
  48. turtle.forward()
  49. end
  50. turtle.turnLeft()
  51. end
  52.  
  53. function goForwards(num)
  54. for i=1,num do
  55. turtle.forward()
  56. end
  57. end
  58.  
  59. function get_orientation()
  60. location_1 = vector.new(gps.locate(5, false))
  61. if not turtle.forward() then
  62. for j=1,6 do
  63. if not turtle.forward() then
  64. turtle.dig()
  65. else
  66. break
  67. end
  68. end
  69. end
  70. location_2 = vector.new(gps.locate(5, false))
  71. heading = location_2 - location_1
  72. turtle.back()
  73. calculation = ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  74. print("I AM ORIENTED TOWARDS ", calculation)
  75. return calculation
  76. end
  77.  
  78. function go_to_location(x, y, z, direction_to_mine)
  79. hurdle_blocks = {"computercraft:turtle_advanced"}
  80. go_left = nil
  81. go_right = nil
  82. direction_curr = get_orientation()
  83. direction_start_move = nil
  84. heading = vector.new(x, y, z)
  85. current_pos = get_curr_coords()
  86. displacement = get_curr_disp(current_pos, heading)
  87.  
  88. if displacement.x < 0 then
  89. direction_start_move = 1
  90. elseif displacement.x > 0 then
  91. direction_start_move = 3
  92. elseif displacement.z < 0 then
  93. direction_start_move = 2
  94. elseif displacement.z > 0 then
  95. direction_start_move = 4
  96. end
  97.  
  98. while direction_curr ~= direction_start_move do
  99. turtle.turnRight()
  100. direction_curr = get_orientation()
  101. end
  102.  
  103. success, down_block = turtle.inspectDown()
  104. while down_block.name == hurdle_blocks[1] do
  105. print(down_block.name, hurdle_blocks[1])
  106. os.sleep(1)
  107. success, down_block = turtle.inspectDown()
  108. end
  109.  
  110. turtle.forward()
  111. for i=1,math.abs(displacement.y) do
  112. turtle.down()
  113. end
  114.  
  115. if direction_curr == 1 or direction_curr == 3 then
  116. first = math.abs(displacement.x) - 1
  117. second = displacement.z
  118. else
  119. first = math.abs(displacement.z) - 1
  120. second = displacement.x
  121. end
  122.  
  123. if direction_curr == 3 or direction_curr == 2 then
  124. if second < 0 then
  125. go_left = true
  126. elseif second > 0 then
  127. go_right = true
  128. end
  129. elseif direction_curr == 1 or direction_curr == 4 then
  130. if second < 0 then
  131. go_right = true
  132. elseif second > 0 then
  133. go_left = true
  134. end
  135. end
  136.  
  137. second = math.abs(second)
  138.  
  139. for i=1,first do
  140. turtle.forward()
  141. success, infront_block = turtle.inspect()
  142. success, down_block = turtle.inspectDown()
  143. print(infront_block.name, down_block.name)
  144. if infront_block.name == hurdle_blocks[1] then
  145. print("INFRONT")
  146. turtle.up()
  147. go_down = false
  148. end
  149. if go_down then
  150. print("GOING DOWN")
  151. turtle.down()
  152. end
  153. if down_block.name == hurdle_blocks[1] then
  154. print("BELOW")
  155. go_down = true
  156. end
  157. end
  158.  
  159. if go_right then
  160. turtle.turnRight()
  161. elseif go_left then
  162. turtle.turnLeft()
  163. else
  164. print("I dont know which way to turn...")
  165. end
  166.  
  167. for i=1,second do
  168. turtle.forward()
  169. success, infront_block = turtle.inspect()
  170. success, down_block = turtle.inspectDown()
  171. print(infront_block.name, down_block.name)
  172. if infront_block.name == hurdle_blocks[1] then
  173. print("INFRONT")
  174. turtle.up()
  175. go_down = false
  176. end
  177. if go_down then
  178. print("GOING DOWN")
  179. turtle.down()
  180. end
  181. if down_block.name == hurdle_blocks[1] then
  182. print("BELOW")
  183. go_down = true
  184. end
  185. end
  186.  
  187. direction_curr = get_orientation()
  188. while direction_curr ~= direction_to_mine do
  189. turtle.turnRight()
  190. direction_curr = get_orientation()
  191. end
  192. end
  193.  
  194. function go_home(home, direction_to_mine)
  195. hurdle_blocks = {"quark:jungle_chest"}
  196. go_left = nil
  197. go_right = nil
  198. direction_curr = get_orientation()
  199. direction_start_move = nil
  200. heading = vector.new(home.x, home.y, home.z)
  201. current_pos = get_curr_coords()
  202. displacement = get_curr_disp(current_pos, heading)
  203.  
  204. while direction_curr ~= direction_to_mine do
  205. turtle.turnRight()
  206. direction_curr = get_orientation()
  207. end
  208. turtle.turnRight()
  209. turtle.turnRight()
  210.  
  211. if direction_curr == 1 or direction_curr == 3 then
  212. first = math.abs(displacement.x) - 1
  213. second = displacement.z
  214. else
  215. first = math.abs(displacement.z) - 1
  216. second = displacement.x
  217. end
  218.  
  219. if direction_curr == 3 or direction_curr == 2 then
  220. if second < 0 then
  221. go_right = true
  222. elseif second > 0 then
  223. go_left = true
  224. end
  225. elseif direction_curr == 1 or direction_curr == 4 then
  226. if second < 0 then
  227. go_left = true
  228. elseif second > 0 then
  229. go_right = true
  230. end
  231. end
  232.  
  233. second = math.abs(second)
  234.  
  235. for i=1,first+1 do
  236. turtle.forward()
  237. end
  238.  
  239. if go_right then
  240. turtle.turnRight()
  241. elseif go_left then
  242. turtle.turnLeft()
  243. else
  244. print("I dont know which way to turn...")
  245. end
  246.  
  247. for i=1,second do
  248. success, block_front = turtle.inspect()
  249. for i=1,#hurdle_blocks do
  250. if block_front.name == hurdle_blocks[i] then
  251. turtle.up()
  252. end
  253. end
  254. turtle.forward()
  255. end
  256.  
  257. direction_curr = get_orientation()
  258. while direction_curr ~= direction_to_mine do
  259. turtle.turnRight()
  260. direction_curr = get_orientation()
  261. end
  262.  
  263. end
  264.  
  265. function mine_selected_area(home, direction_to_mine, width, height, depth)
  266. print("MINING SELECTED AREA OF (" .. width .. ", " .. height .. ", " .. depth .. ")")
  267. go_right = true
  268. last_x, last_y, last_z = nil, nil, nil
  269. for i=1,depth do
  270. for k=1,height-1 do
  271. for j=1,width-1 do
  272. if go_right and inventory_check() then
  273. current_position = get_curr_coords()
  274. last_x, last_y, last_z = current_position.x, current_position.y, current_position.z
  275. try_refuel()
  276. go_home(home, direction_to_mine)
  277. dump_items()
  278. go_to_location(last_x, last_y, last_z, direction_to_mine)
  279. end
  280. turtle.dig()
  281. if go_right then
  282. slideRight()
  283. else
  284. slideLeft()
  285. end
  286. end
  287. turtle.dig()
  288. go_right = not go_right
  289. if k ~= height-1 then
  290. turtle.up()
  291. end
  292. end
  293. turtle.down()
  294. if i ~= depth then
  295. turtle.forward()
  296. end
  297. end
  298. for i=1,depth-1 do
  299. turtle.back()
  300. end
  301. end
  302.  
  303. function dump_items()
  304. chests = {"quark:jungle_chest"}
  305. success, block_down = turtle.inspectDown()
  306. for i=1,#chests do
  307. if block_down.name == chests[i] then
  308. for k=1,16 do
  309. turtle.select(k)
  310. turtle.dropDown()
  311. end
  312. end
  313. end
  314. turtle.select(1)
  315. end
  316.  
  317. function try_refuel()
  318. for i=1,16 do
  319. turtle.select(i)
  320. if turtle.refuel() then
  321. print("REFUELING")
  322. turtle.refuel()
  323. end
  324. end
  325. turtle.select(1)
  326. end
  327.  
  328. function inventory_check()
  329. sum = 0
  330. for i=1,16 do
  331. turtle.select(i)
  332. amount_of_items = turtle.getItemCount()
  333. if amount_of_items > 32 then
  334. sum = sum + 1
  335. end
  336. end
  337. if sum >= 6 then
  338. return true
  339. else
  340. return false
  341. end
  342. end
  343.  
  344. function run(x, y, z, mining_direction, width, height, depth)
  345. --local tArgs = {...}
  346. home = get_home_coords()
  347. --x, y, z, = tArgs[1], tArgs[2], tArgs[3]
  348. --mining_direction = tArgs[4]
  349. --width, height, depth = tArgs[5], tArgs[6], tArgs[7]
  350. go_to_location(x, y, z, mining_direction)
  351. mine_selected_area(home, mining_direction, width, height, depth)
  352. go_home(home, mining_direction)
  353. end
  354.  
  355. run()
  356.  
Add Comment
Please, Sign In to add comment