Advertisement
mauza

Untitled

Jul 29th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 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 = 16
  10. local tries = 11
  11. local last_empty_slot = 15
  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. elseif move_direction == direction.DOWN then
  48. position.y = position.y -1
  49. elseif move_direction == direction.FORWARD then
  50. if currOrient == orientation.positive_x then
  51. position.x = position.x + 1
  52. elseif currOrient == orientation.negative_x then
  53. position.x = position.x - 1
  54. elseif currOrient == orientation.positive_z then
  55. position.z = position.z + 1
  56. elseif currOrient == orientation.negative_z then
  57. position.z = position.z - 1
  58. end
  59. end
  60. writeMessage(position.x .. ' - ' .. position.y .. ' - ' .. position.z)
  61. end
  62.  
  63. function turn_left(times)
  64. times = times or 1
  65. for i = 1, times, 1 do
  66. if not turtle.turnLeft() then
  67. add_fuel()
  68. turtle.turnLeft()
  69. end
  70. if currOrient == 0 then
  71. currOrient = 3
  72. else
  73. currOrient = currOrient - 1
  74. end
  75. end
  76. end
  77.  
  78. function turn_right(times)
  79. times = times or 1
  80. for i = 1, times, 1 do
  81. if not turtle.turnRight() then
  82. add_fuel()
  83. turtle.turnRight()
  84. end
  85. if currOrient == 3 then
  86. currOrient = 0
  87. else
  88. currOrient = currOrient + 1
  89. end
  90. end
  91. end
  92.  
  93. function set_orientation(desired_orient)
  94. orient_diff = currOrient - desired_orient
  95. if math.abs(orient_diff) == 2 then
  96. turn_right(2)
  97. elseif orient_diff == -3 or orient_diff == 1 then
  98. turn_left()
  99. elseif orient_diff == -1 or orient_diff == 3 then
  100. turn_right()
  101. end
  102. end
  103.  
  104. function ensure_inventory_space()
  105. if turtle.getItemCount(last_empty_slot) > 0 then
  106. unload()
  107. end
  108. end
  109.  
  110. function unload()
  111. local last_x = position.x
  112. local last_y = position.y
  113. local last_z = position.z
  114. local last_orient = currOrient
  115. local last_selected_slot = turtle.getSelectedSlot()
  116. go_to_position(0, 0, 0, orientation.negative_x)
  117. for i = 3, last_empty_slot do
  118. turtle.select(i)
  119. turtle.drop()
  120. end
  121. turtle.select(last_selected_slot)
  122. go_to_position(last_x, last_y, last_z, last_orient)
  123. end
  124.  
  125. function Dig(side)
  126. result = false
  127. ensure_inventory_space()
  128. if side == direction.UP then
  129. result = turtle.digUp()
  130. elseif side == direction.Down then
  131. result = turtle.digDown()
  132. elseif side == direction.FORWARD then
  133. result = turtle.dig()
  134. end
  135. return result
  136. end
  137.  
  138. function Attack(side)
  139. result = false
  140. if side == direction.UP then
  141. result = turtle.attackUp()
  142. elseif side == direction.Down then
  143. result = turtle.attackDown()
  144. elseif side == direction.FORWARD then
  145. result = turtle.attack()
  146. end
  147. return result
  148. end
  149.  
  150. function Place(side)
  151. result = false
  152. if side == direction.UP then
  153. result = turtle.placeUp()
  154. elseif side == direction.Down then
  155. result = turtle.placeDown()
  156. elseif side == direction.FORWARD then
  157. result = turtle.place()
  158. end
  159. return result
  160. end
  161.  
  162. function ensure_place(side)
  163. turtle.select(replace_slot)
  164. local attempt = 0
  165. while Place(side) == false do
  166. Dig(side)
  167. Attack(side)
  168. attempt = attempt + 1
  169. if attempt >= tries then
  170. return false
  171. end
  172. end
  173. return true
  174. end
  175.  
  176. function compare_and_replace(side)
  177. local starting_orient = currOrient
  178. local ini_slot = turtle.getSelectedSlot()
  179. turtle.select(compare_slot)
  180. if side == direction.UP then
  181. compare_result = turtle.compareUp()
  182. elseif side == direction.DOWN then
  183. compare_result = turtle.compareDown()
  184. elseif side == direction.FORWARD then
  185. compare_result = turtle.compare()
  186. elseif side == direction.LEFT then
  187. turn_left()
  188. compare_result = turtle.compare()
  189. elseif side == direction.RIGHT then
  190. turn_right()
  191. compare_result = turtle.compare()
  192. elseif side == direction.BACK then
  193. turn_right(2)
  194. compare_result = turtle.compare()
  195. end
  196. if compare_result == false then
  197. ensure_place(side)
  198. end
  199. set_orientation(starting_orient)
  200. turtle.select(ini_slot)
  201. end
  202.  
  203. function add_fuel()
  204. local ini_slot = turtle.getSelectedSlot()
  205. turtle.select(fuel_slot)
  206. fuel_count = turtle.getItemCount()
  207. if fuel_count > 1 then
  208. turtle.refuel(fuel_count - 1)
  209. end
  210. turtle.select(ini_slot)
  211. end
  212.  
  213. function writeMessage(message)
  214. print(message)
  215. end
  216.  
  217. function file_write(file_name, value)
  218. local outputFile = io.open(file_name, 'w')
  219. outputFile:write(value)
  220. outputFile:close()
  221. end
  222.  
  223. function file_read(file_name)
  224. local outputFile = fs.open(file_name, 'r')
  225. return outputFile.readLine()
  226. end
  227.  
  228. function dig_to_bedrock()
  229. while Move(direction.DOWN, 1) do end
  230. end
  231.  
  232. function move_up_to_next_level()
  233. for i = 1, 3 do
  234. Move(direction.UP, 1)
  235. end
  236. end
  237.  
  238. function mine_one_level(width)
  239. for i = 1, width + 1 do
  240. if i ~= 1 then
  241. if position.x == 0 then
  242. turn_left()
  243. Move(direction.FORWARD, 1)
  244. compare_and_replace(direction.UP)
  245. compare_and_replace(direction.DOWN)
  246. turn_left()
  247. else
  248. turn_right()
  249. Move(direction.FORWARD, 1)
  250. compare_and_replace(direction.UP)
  251. compare_and_replace(direction.DOWN)
  252. turn_right()
  253. end
  254. end
  255. for i = 1, width do
  256. Move(direction.FORWARD, 1)
  257. compare_and_replace(direction.UP)
  258. compare_and_replace(direction.DOWN)
  259. end
  260. end
  261. end
  262.  
  263. function go_to_position(x, y, z, desired_orient)
  264. x_diff = x - position.x
  265. y_diff = y - position.y
  266. z_diff = z - position.z
  267. if x_diff < 0 then
  268. set_orientation(orientation.positive_x)
  269. Move(direction.FORWARD, math.abs(x_diff))
  270. elseif x_diff > 0 then
  271. set_orientation(orientation.negative_x)
  272. Move(direction.FORWARD, math.abs(x_diff))
  273. end
  274. if z_diff < 0 then
  275. set_orientation(orientation.positive_z)
  276. Move(direction.FORWARD, math.abs(z_diff))
  277. elseif z_diff > 0 then
  278. set_orientation(orientation.negative_z)
  279. Move(direction.FORWARD, math.abs(z_diff))
  280. end
  281. if y_diff < 0 then
  282. Move(direction.DOWN, math.abs(y_diff))
  283. elseif y_diff > 0 then
  284. Move(direction.UP, math.abs(y_diff))
  285. end
  286. set_orientation(desired_orient)
  287. end
  288.  
  289. function quarry(width)
  290. writeMessage("Starting to mine")
  291. dig_to_bedrock()
  292. while position.y <= -4 do
  293. move_up_to_next_level()
  294. mine_one_level(width)
  295. go_to_position(0, position.y, 0, orientation.positive_x)
  296. end
  297. go_to_position(0, 0, 0, orientation.positive_x)
  298. end
  299.  
  300. -- ********************************************************************************** --
  301. -- Main Function
  302. -- ********************************************************************************** --
  303. local args = { ... }
  304.  
  305. quarryWidth = tonumber(args[1])
  306.  
  307. -- quarry(quarryWidth)
  308. Move(direction.DOWN, 1)
  309.  
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement