Advertisement
mauza

Untitled

Jul 29th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. -- ********************************************************************************** --
  2. -- Global Vars
  3. -- ********************************************************************************** --
  4. direction = { FORWARD=0, RIGHT=1, BACK=2, LEFT=3, UP=4, DOWN=5 }
  5. orientation = { positive_x=0, positive_z=1, negative_x=2, negative_z=3}
  6. local compare_slot = 1
  7. local replace_slot = 2
  8. local fuel_slot = 16
  9. local tries = 11
  10. local last_empty_slot = 15
  11.  
  12. local position = {x=0, y=0, z=0}
  13. local currOrient = orientation.positive_x
  14.  
  15. function Move(move_direction, times)
  16. writeMessage("moving " .. move_direction)
  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. writeMessage("ensuring inventory has room")
  106. if turtle.getItemCount(last_empty_slot) > 0 then
  107. unload()
  108. end
  109. end
  110.  
  111. function unload()
  112. writeMessage("unloading inventory")
  113. local last_x = position.x
  114. local last_y = position.y
  115. local last_z = position.z
  116. local last_orient = currOrient
  117. local last_selected_slot = turtle.getSelectedSlot()
  118. go_to_position(0, 0, 0, orientation.negative_x)
  119. for i = 3, last_empty_slot do
  120. turtle.select(i)
  121. turtle.drop()
  122. end
  123. turtle.select(last_selected_slot)
  124. go_to_position(last_x, last_y, last_z, last_orient)
  125. end
  126.  
  127. function Dig(side)
  128. writeMessage("digging")
  129. local result = false
  130. ensure_inventory_space()
  131. if side == direction.UP then
  132. result = turtle.digUp()
  133. elseif side == direction.DOWN then
  134. result = turtle.digDown()
  135. elseif side == direction.FORWARD then
  136. result = turtle.dig()
  137. end
  138. return result
  139. end
  140.  
  141. function Attack(side)
  142. writeMessage("attacking")
  143. local result = false
  144. if side == direction.UP then
  145. result = turtle.attackUp()
  146. elseif side == direction.DOWN then
  147. result = turtle.attackDown()
  148. elseif side == direction.FORWARD then
  149. result = turtle.attack()
  150. end
  151. return result
  152. end
  153.  
  154. function Place(side)
  155. writeMessage("placing")
  156. local result = false
  157. if side == direction.UP then
  158. result = turtle.placeUp()
  159. elseif side == direction.DOWN then
  160. result = turtle.placeDown()
  161. elseif side == direction.FORWARD then
  162. result = turtle.place()
  163. end
  164. return result
  165. end
  166.  
  167. function ensure_place(side)
  168. turtle.select(replace_slot)
  169. local attempt = 0
  170. while not Place(side) do
  171. Dig(side)
  172. Attack(side)
  173. attempt = attempt + 1
  174. if attempt >= tries then
  175. return false
  176. end
  177. end
  178. return true
  179. end
  180.  
  181. function compare_and_replace(side)
  182. local starting_orient = currOrient
  183. local ini_slot = turtle.getSelectedSlot()
  184. turtle.select(compare_slot)
  185. if side == direction.UP then
  186. compare_result = turtle.compareUp()
  187. elseif side == direction.DOWN then
  188. compare_result = turtle.compareDown()
  189. elseif side == direction.FORWARD then
  190. compare_result = turtle.compare()
  191. elseif side == direction.LEFT then
  192. turn_left()
  193. compare_result = turtle.compare()
  194. elseif side == direction.RIGHT then
  195. turn_right()
  196. compare_result = turtle.compare()
  197. elseif side == direction.BACK then
  198. turn_right(2)
  199. compare_result = turtle.compare()
  200. end
  201. if compare_result == false then
  202. ensure_place(side)
  203. end
  204. set_orientation(starting_orient)
  205. turtle.select(ini_slot)
  206. end
  207.  
  208. function add_fuel()
  209. local ini_slot = turtle.getSelectedSlot()
  210. turtle.select(fuel_slot)
  211. fuel_count = turtle.getItemCount()
  212. if fuel_count > 1 then
  213. turtle.refuel(fuel_count - 1)
  214. end
  215. turtle.select(ini_slot)
  216. end
  217.  
  218. function writeMessage(message)
  219. print(message)
  220. end
  221.  
  222. function file_write(file_name, value)
  223. local outputFile = io.open(file_name, 'w')
  224. outputFile:write(value)
  225. outputFile:close()
  226. end
  227.  
  228. function file_read(file_name)
  229. local outputFile = fs.open(file_name, 'r')
  230. return outputFile.readLine()
  231. end
  232.  
  233. function dig_to_bedrock()
  234. while Move(direction.DOWN, 1) do end
  235. end
  236.  
  237. function move_up_to_next_level()
  238. for i = 1, 3 do
  239. Move(direction.UP, 1)
  240. end
  241. end
  242.  
  243. function mine_one_level(width)
  244. for i = 1, width + 1 do
  245. if i ~= 1 then
  246. if position.x == 0 then
  247. turn_left()
  248. Move(direction.FORWARD, 1)
  249. compare_and_replace(direction.UP)
  250. compare_and_replace(direction.DOWN)
  251. turn_left()
  252. else
  253. turn_right()
  254. Move(direction.FORWARD, 1)
  255. compare_and_replace(direction.UP)
  256. compare_and_replace(direction.DOWN)
  257. turn_right()
  258. end
  259. end
  260. for i = 1, width do
  261. Move(direction.FORWARD, 1)
  262. compare_and_replace(direction.UP)
  263. compare_and_replace(direction.DOWN)
  264. end
  265. end
  266. end
  267.  
  268. function go_to_position(x, y, z, desired_orient)
  269. x_diff = x - position.x
  270. y_diff = y - position.y
  271. z_diff = z - position.z
  272. if x_diff < 0 then
  273. set_orientation(orientation.positive_x)
  274. Move(direction.FORWARD, math.abs(x_diff))
  275. elseif x_diff > 0 then
  276. set_orientation(orientation.negative_x)
  277. Move(direction.FORWARD, math.abs(x_diff))
  278. end
  279. if z_diff < 0 then
  280. set_orientation(orientation.positive_z)
  281. Move(direction.FORWARD, math.abs(z_diff))
  282. elseif z_diff > 0 then
  283. set_orientation(orientation.negative_z)
  284. Move(direction.FORWARD, math.abs(z_diff))
  285. end
  286. if y_diff < 0 then
  287. Move(direction.DOWN, math.abs(y_diff))
  288. elseif y_diff > 0 then
  289. Move(direction.UP, math.abs(y_diff))
  290. end
  291. set_orientation(desired_orient)
  292. end
  293.  
  294. function quarry(width)
  295. writeMessage("Starting to mine")
  296. dig_to_bedrock()
  297. while position.y <= -4 do
  298. move_up_to_next_level()
  299. mine_one_level(width)
  300. go_to_position(0, position.y, 0, orientation.positive_x)
  301. end
  302. go_to_position(0, 0, 0, orientation.positive_x)
  303. end
  304.  
  305. -- ********************************************************************************** --
  306. -- Main Function
  307. -- ********************************************************************************** --
  308. local args = { ... }
  309.  
  310. quarryWidth = tonumber(args[1])
  311.  
  312. -- quarry(quarryWidth)
  313. -- mine_one_level(quarryWidth)
  314. Move(direction.FORWARD, 3)
  315. turn_right()
  316. Move(direction.FORWARD, 3)
  317. go_to_position(0, position.y, 0, orientation.positive_x)
  318. set_orientation(orientation.positive_z)
  319. set_orientation(orientation.negative_z)
  320. set_orientation(orientation.positive_x)
  321. set_orientation(orientation.negative_x)
  322.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement