Advertisement
mauza

Untitled

Jul 31st, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 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. local outputFile = fs.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 quarry(width, mined_level)
  308. if not mined_level then
  309. dig_to_bedrock()
  310. else
  311. go_to_position(0, mined_level + 3, 0, orientation.positive_x, {3,1,2})
  312. end
  313. while position.y <= -4 do
  314. mine_one_level(width)
  315. file_write('mined_level', position.y)
  316. go_to_position(0, position.y + 3, 0, orientation.positive_x, {3,1,2})
  317. end
  318. go_to_position(0, 0, 0, orientation.positive_x, {3,1,2})
  319. end
  320.  
  321. -- ********************************************************************************** --
  322. -- Main Function
  323. -- ********************************************************************************** --
  324. local args = { ... }
  325.  
  326. if #args == 0 then
  327. position.x = tonumber(file_read('x'))
  328. position.y = tonumber(file_read('y'))
  329. position.z = tonumber(file_read('z'))
  330. currOrient = tonumber(file_read('orientation'))
  331. quarryWidth = tonumber(file_read('width'))
  332. mined_level = tonumber(file_read('mined_level'))
  333. quarry(quarryWidth, mined_level)
  334. elseif #args == 1 then
  335. quarryWidth = tonumber(args[1])
  336. file_write('width', quarryWidth)
  337. quarry(quarryWidth)
  338. else
  339. writeMessage("something is horribly wrong")
  340. end
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement