Advertisement
mauza

Untitled

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