Advertisement
mauza

Untitled

Jul 31st, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.36 KB | None | 0 0
  1.  
  2. -- ********************************************************************************** --
  3. -- Global Vars
  4. -- ********************************************************************************** --
  5. direction = { FORWARD=0, RIGHT=1, BACK=2, LEFT=3, UP=4, DOWN=5 }
  6. orientation = { positive_x=0, positive_z=1, negative_x=2, negative_z=3}
  7. local compare_slot = 1
  8. local replace_slot = 2
  9. local fuel_slot = 3
  10. local last_empty_slot = 16
  11. local tries = 11
  12.  
  13. local position = {x=0, y=0, z=0}
  14. local currOrient = orientation.positive_x
  15.  
  16. function Move(move_direction, times)
  17. local move = turtle.forward
  18. local detect = turtle.detect
  19. if move_direction == direction.UP then
  20. move = turtle.up
  21. detect = turtle.detectUp
  22. elseif move_direction == direction.DOWN then
  23. move = turtle.down
  24. detect = turtle.detectDown
  25. end
  26. times = times or 1
  27. for i = 1, times, 1 do
  28. while not move() do
  29. if detect() then
  30. if not Dig(move_direction) then
  31. return false
  32. end
  33. elseif Attack(move_direction) then
  34. while Attack(move_direction) do end
  35. elseif turtle.getFuelLevel() == 0 then
  36. add_fuel()
  37. end
  38. end
  39. calc_position(move_direction)
  40. end
  41. return true
  42. end
  43.  
  44. function calc_position(move_direction)
  45. if move_direction == direction.UP then
  46. position.y = position.y + 1
  47. file_write('y', position.y)
  48. elseif move_direction == direction.DOWN then
  49. position.y = position.y - 1
  50. file_write('y', position.y)
  51. elseif move_direction == direction.FORWARD then
  52. if currOrient == orientation.positive_x then
  53. position.x = position.x + 1
  54. file_write('x', position.x)
  55. elseif currOrient == orientation.negative_x then
  56. position.x = position.x - 1
  57. file_write('x', position.x)
  58. elseif currOrient == orientation.positive_z then
  59. position.z = position.z + 1
  60. file_write('z', position.z)
  61. elseif currOrient == orientation.negative_z then
  62. position.z = position.z - 1
  63. file_write('z', position.z)
  64. end
  65. end
  66. writeMessage(position.x .. ' - ' .. position.y .. ' - ' .. position.z)
  67. end
  68.  
  69. function turn_left(times)
  70. times = times or 1
  71. for i = 1, times, 1 do
  72. if not turtle.turnLeft() then
  73. add_fuel()
  74. turtle.turnLeft()
  75. end
  76. if currOrient == 0 then
  77. currOrient = 3
  78. else
  79. currOrient = currOrient - 1
  80. end
  81. file_write('orientation', currOrient)
  82. end
  83. end
  84.  
  85. function turn_right(times)
  86. times = times or 1
  87. for i = 1, times, 1 do
  88. if not turtle.turnRight() then
  89. add_fuel()
  90. turtle.turnRight()
  91. end
  92. if currOrient == 3 then
  93. currOrient = 0
  94. else
  95. currOrient = currOrient + 1
  96. end
  97. file_write('orientation', currOrient)
  98. end
  99. end
  100.  
  101. function set_orientation(desired_orient)
  102. orient_diff = currOrient - desired_orient
  103. if math.abs(orient_diff) == 2 then
  104. turn_right(2)
  105. elseif orient_diff == -3 or orient_diff == 1 then
  106. turn_left()
  107. elseif orient_diff == -1 or orient_diff == 3 then
  108. turn_right()
  109. end
  110. end
  111.  
  112. function ensure_inventory_space()
  113. writeMessage("ensuring inventory has room")
  114. if turtle.getItemCount(last_empty_slot) > 0 then
  115. unload()
  116. end
  117. end
  118.  
  119. function unload()
  120. writeMessage("unloading inventory")
  121. local last_x = position.x
  122. local last_y = position.y
  123. local last_z = position.z
  124. local last_orient = currOrient
  125. local last_selected_slot = turtle.getSelectedSlot()
  126. go_to_position(0, 0, 0, orientation.negative_x, {3,1,2})
  127. for i = 3, last_empty_slot do
  128. turtle.select(i)
  129. turtle.drop()
  130. end
  131. turtle.select(last_selected_slot)
  132. go_to_position(last_x, last_y, last_z, last_orient, {2,1,3})
  133. end
  134.  
  135. function Dig(side)
  136. writeMessage("digging")
  137. local result = false
  138. ensure_inventory_space()
  139. if side == direction.UP then
  140. result = turtle.digUp()
  141. elseif side == direction.DOWN then
  142. result = turtle.digDown()
  143. elseif side == direction.FORWARD then
  144. result = turtle.dig()
  145. end
  146. return result
  147. end
  148.  
  149. function Attack(side)
  150. writeMessage("attacking")
  151. local result = false
  152. if side == direction.UP then
  153. result = turtle.attackUp()
  154. elseif side == direction.DOWN then
  155. result = turtle.attackDown()
  156. elseif side == direction.FORWARD then
  157. result = turtle.attack()
  158. end
  159. return result
  160. end
  161.  
  162. function Place(side)
  163. writeMessage("placing")
  164. local result = false
  165. if side == direction.UP then
  166. result = turtle.placeUp()
  167. elseif side == direction.DOWN then
  168. result = turtle.placeDown()
  169. elseif side == direction.FORWARD then
  170. result = turtle.place()
  171. end
  172. return result
  173. end
  174.  
  175. function ensure_place(side)
  176. turtle.select(replace_slot)
  177. local attempt = 0
  178. while not Place(side) do
  179. Dig(side)
  180. Attack(side)
  181. attempt = attempt + 1
  182. if attempt >= tries then
  183. return false
  184. end
  185. end
  186. return true
  187. end
  188.  
  189. function compare_and_replace(side)
  190. local starting_orient = currOrient
  191. local ini_slot = turtle.getSelectedSlot()
  192. turtle.select(compare_slot)
  193. if side == direction.UP then
  194. compare_result = turtle.compareUp()
  195. elseif side == direction.DOWN then
  196. compare_result = turtle.compareDown()
  197. elseif side == direction.FORWARD then
  198. compare_result = turtle.compare()
  199. elseif side == direction.LEFT then
  200. turn_left()
  201. compare_result = turtle.compare()
  202. elseif side == direction.RIGHT then
  203. turn_right()
  204. compare_result = turtle.compare()
  205. elseif side == direction.BACK then
  206. turn_right(2)
  207. compare_result = turtle.compare()
  208. end
  209. if compare_result == false then
  210. ensure_place(side)
  211. end
  212. set_orientation(starting_orient)
  213. turtle.select(ini_slot)
  214. end
  215.  
  216. function add_fuel()
  217. local ini_slot = turtle.getSelectedSlot()
  218. turtle.select(fuel_slot)
  219. fuel_count = turtle.getItemCount()
  220. if fuel_count > 1 then
  221. turtle.refuel(fuel_count - 1)
  222. end
  223. turtle.select(ini_slot)
  224. end
  225.  
  226. function writeMessage(message)
  227. print(message)
  228. end
  229.  
  230. function file_write(file_name, value)
  231. local outputFile = io.open(file_name, 'w')
  232. outputFile:write(value)
  233. outputFile:close()
  234. end
  235.  
  236. function file_read(file_name)
  237. writeMessage("Reading " .. file_name)
  238. local outputFile = io.open(file_name, 'r')
  239. value = outputFile:read()
  240. outputFile:close()
  241. return value
  242. end
  243.  
  244. function dig_to_bedrock()
  245. while Move(direction.DOWN, 1) do end
  246. end
  247.  
  248.  
  249. function mine_one_level(width)
  250. for i = 1, width + 1 do
  251. if i ~= 1 then
  252. if position.x == 0 then
  253. turn_left()
  254. Move(direction.FORWARD, 1)
  255. compare_and_replace(direction.UP)
  256. compare_and_replace(direction.DOWN)
  257. turn_left()
  258. else
  259. turn_right()
  260. Move(direction.FORWARD, 1)
  261. compare_and_replace(direction.UP)
  262. compare_and_replace(direction.DOWN)
  263. turn_right()
  264. end
  265. end
  266. for i = 1, width do
  267. Move(direction.FORWARD, 1)
  268. compare_and_replace(direction.UP)
  269. compare_and_replace(direction.DOWN)
  270. end
  271. end
  272. end
  273.  
  274. function go_to_position(x, y, z, desired_orient, order)
  275. x_diff = position.x - x
  276. y_diff = position.y - y
  277. z_diff = position.z - z
  278. x_y_z = {x_diff, y_diff, z_diff}
  279. for i, index in ipairs(order) do
  280. current_diff = x_y_z[index]
  281. magnitude = math.abs(current_diff)
  282. if current_diff < 0 then
  283. if index == 1 then
  284. set_orientation(orientation.positive_x)
  285. Move(direction.FORWARD, magnitude)
  286. elseif index == 3 then
  287. set_orientation(orientation.positive_z)
  288. Move(direction.FORWARD, magnitude)
  289. elseif index == 2 then
  290. Move(direction.UP, magnitude)
  291. end
  292. elseif current_diff > 0 then
  293. if index == 1 then
  294. set_orientation(orientation.negative_x)
  295. Move(direction.FORWARD, magnitude)
  296. elseif index == 3 then
  297. set_orientation(orientation.negative_z)
  298. Move(direction.FORWARD, magnitude)
  299. elseif index == 2 then
  300. Move(direction.DOWN, magnitude)
  301. end
  302. end
  303. end
  304. set_orientation(desired_orient)
  305. end
  306.  
  307. function reset_files()
  308. file_write('x', '')
  309. file_write('y', '')
  310. file_write('z', '')
  311. file_write('orientation', '')
  312. file_write('mined_level', '')
  313. file_write('width', '')
  314. end
  315.  
  316. function quarry(width, mined_level)
  317. if not mined_level then
  318. dig_to_bedrock()
  319. file_write('mined_level', position.y)
  320. go_to_position(0, position.y + 3, 0, orientation.positive_x, {3,1,2})
  321. else
  322. go_to_position(0, mined_level + 3, 0, orientation.positive_x, {3,1,2})
  323. end
  324. while position.y <= -4 do
  325. mine_one_level(width)
  326. file_write('mined_level', position.y)
  327. go_to_position(0, position.y + 3, 0, orientation.positive_x, {3,1,2})
  328. end
  329. go_to_position(0, 0, 0, orientation.positive_x, {3,1,2})
  330. reset_files()
  331. end
  332.  
  333. -- ********************************************************************************** --
  334. -- Main Function
  335. -- ********************************************************************************** --
  336. local args = { ... }
  337.  
  338. if #args == 0 then
  339. position.x = tonumber(file_read('x'))
  340. position.y = tonumber(file_read('y'))
  341. position.z = tonumber(file_read('z'))
  342. currOrient = tonumber(file_read('orientation'))
  343. quarryWidth = tonumber(file_read('width'))
  344. mined_level = tonumber(file_read('mined_level'))
  345. quarry(quarryWidth, mined_level)
  346. elseif #args == 1 then
  347. quarryWidth = tonumber(args[1])
  348. file_write('width', quarryWidth)
  349. quarry(quarryWidth)
  350. else
  351. writeMessage("something is horribly wrong")
  352. end
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement