Advertisement
mauza

Untitled

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