Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. # Miner
  2.  
  3. local SLOT_FUEL_CHEST = 1
  4. local SLOT_DEPOSIT_CHEST = 2
  5. local SLOTS_EMPTY = 14
  6.  
  7. local MINESHAFT_WIDTH = 10
  8.  
  9. --------- BEGIN NAVIGATION ---------
  10. local rel_x, rel_y, rel_z = 0, 0, 0
  11. local rel_bearing = 0
  12. local MAX_REL_Z = 100
  13.  
  14. function forward()
  15. if not turtle.forward() then
  16. return false
  17. end
  18. if rel_bearing == 0 then
  19. rel_y = rel_y + 1
  20. elseif rel_bearing == 1 then
  21. rel_x = rel_x + 1
  22. elseif rel_bearing == 2 then
  23. rel_y = rel_y - 1
  24. elseif rel_bearing == 3 then
  25. rel_x = rel_x - 1
  26. end
  27. return true
  28. end
  29.  
  30. function back()
  31. if not turtle.back() then
  32. return false
  33. end
  34. if rel_bearing == 0 then
  35. rel_y = rel_y - 1
  36. elseif rel_bearing == 1 then
  37. rel_x = rel_x - 1
  38. elseif rel_bearing == 2 then
  39. rel_y = rel_y + 1
  40. elseif rel_bearing == 3 then
  41. rel_x = rel_x + 1
  42. end
  43. return true
  44. end
  45.  
  46. function turnLeft()
  47. if not turtle.turnLeft() then
  48. return false
  49. end
  50. rel_bearing = (rel_bearing - 1) % 4
  51. return true
  52. end
  53.  
  54. function turnRight()
  55. if not turtle.turnRight() then
  56. return false
  57. end
  58. rel_bearing = (rel_bearing + 1) % 4
  59. return true
  60. end
  61.  
  62. function up()
  63. assert(rel_z + 1 <= MAX_REL_Z, "Attempted to move up past MAX_REL_Z")
  64. turtle.up()
  65. rel_z = rel_z + 1
  66. end
  67.  
  68. function down()
  69. turtle.down()
  70. rel_z = rel_z - 1
  71. end
  72.  
  73. --------- END NAVIGATION ---------
  74. --------- BEGIN FUEL ---------
  75. function get_place_dig_suck_drop_in_empty_space()
  76. local success, data = turtle.inspect()
  77. if not success then
  78. return turtle.place, turtle.dig, turtle.suck, turtle.drop
  79. end
  80.  
  81. success, data = turtle.inspectUp()
  82. if not success then
  83. return turtle.placeUp, turtle.digUp, turtle.suckUp, turtle.dropUp
  84. end
  85.  
  86. success, data = turtle.inspectDown()
  87. if not success then
  88. return turtle.placeDown, turtle.digDown, turtle.suckDown, turtle.dropDown
  89. end
  90.  
  91. error("No empty space in front, above, or below the turtle")
  92. end
  93.  
  94. function try_with_rotation(func)
  95. for i=1,4 do
  96. vals = {pcall(func)}
  97. if vals[1] then
  98. for j = 1,(i-1) do
  99. turnRight()
  100. end
  101. table.remove(vals, 1)
  102. return unpack(vals)
  103. end
  104. turnLeft()
  105. end
  106.  
  107. error("Could not execute function in any orientation")
  108. end
  109.  
  110. function refuel_from_chest(slot)
  111. print("Refueling from chest in slot " .. tostring(slot) .. ". Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
  112. local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
  113. local prev_slot = turtle.getSelectedSlot()
  114. turtle.select(slot)
  115. local success = place()
  116. assert(success, "Could not place chest")
  117. while turtle.getFuelLevel() ~= turtle.getFuelLimit() do
  118. success = suck()
  119. assert(success, "Could not obtain fuel from chest")
  120. success = turtle.refuel()
  121. assert(success, "Could not use item obtained from chest as fuel")
  122. end
  123. success = dig()
  124. assert(success, "Could not remove chest")
  125. print("Finished refueling. Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
  126. turtle.select(prev_slot)
  127. end
  128.  
  129. function handle_fuel()
  130. if turtle.getFuelLevel() / turtle.getFuelLimit() < 0.1 then
  131. try_with_rotation(function()
  132. refuel_from_chest(SLOT_FUEL_CHEST)
  133. end)
  134. return true
  135. end
  136. return false
  137. end
  138.  
  139. --------- END FUEL ---------
  140. --------- BEGIN MINE ---------
  141. function inspect_vertical()
  142. local _, up_data = turtle.inspectUp()
  143. local _, front_data = turtle.inspect()
  144. local _, down_data = turtle.inspectDown()
  145. return {down_data, front_data, up_data}
  146. end
  147.  
  148. function inspect_vertical_simple()
  149. local up_data = turtle.inspectUp()
  150. local front_data = turtle.inspect()
  151. local down_data = turtle.inspectDown()
  152. return {down_data, front_data, up_data}
  153. end
  154.  
  155. function mine_down_until(condition)
  156. local retval = false
  157. while not condition() do
  158. local block_exists = turtle.inspectDown()
  159. if block_exists then
  160. assert(turtle.digDown(), "Could not dig down")
  161. end
  162. assert(turtle.down(), "Could not move down")
  163. retval = true
  164. end
  165. return retval
  166. end
  167.  
  168. function mine_up_until(condition)
  169. local retval = false
  170. while not condition() do
  171. local block_exists = turtle.inspectUp()
  172. if block_exists then
  173. assert(turtle.digUp(), "Could not dig down")
  174. end
  175. assert(turtle.up(), "Could not move down")
  176. retval = true
  177. end
  178. return retval
  179. end
  180.  
  181. function mine_into_ground()
  182. no_err, action_taken = pcall(function()
  183. mine_down_until(function()
  184. local v_state = inspect_vertical_simple()
  185. return v_state[1] == true and v_state[2] == true
  186. end)
  187. end)
  188.  
  189. if not no_err then
  190. return false -- allow miner to work on bedrock
  191. end
  192. return action_taken -- could also be interpreted as condition resolved
  193. end
  194.  
  195. function mine_into_shaft()
  196. local v_state = inspect_vertical_simple()
  197. if v_state[1] == true and v_state[2] == true and v_state[3] == false then
  198. assert(turtle.digDown(), "Could not dig down")
  199. assert(turtle.down(), "Could not move down")
  200. turtle.dig()
  201. assert(forward(), "Could not move forward")
  202. return true
  203. end
  204. return false
  205. end
  206.  
  207. function mine_forward_shaft()
  208. turtle.digUp() -- allowed to fail
  209. turtle.digDown() -- allowed to fail
  210.  
  211. local success = turtle.dig()
  212. --assert(success, "Unable to dig forward") allow to move without digging
  213.  
  214. success = forward()
  215. assert(success, "Unable to move forward after mining forward")
  216. return true
  217. end
  218.  
  219. function maintain_bounds()
  220. if rel_y == MINESHAFT_WIDTH or rel_y == 0 then
  221. local at_bedrock = not turtle.digDown() and turtle.inspectDown()
  222. if at_bedrock then
  223. assert(turnRight(), "Could not face towards next mineshaft")
  224.  
  225. local shaft_height = 0
  226. local initial_x = rel_x
  227. local MAX_X_SEARCH = 256
  228.  
  229. while shaft_height < 3 do
  230. shaft_height = 0
  231. while not turtle.inspect() do
  232. assert( math.abs(rel_x - initial_x) < 256, "Could not find next mineshaft")
  233. assert(forward(), "Could not move towards next mineshaft")
  234. end
  235.  
  236. while turtle.inspect() do
  237. turtle.digUp() -- allowed to fail if block doesn't exist
  238. assert(up(), "Could not move towards the top of the next mineshaft")
  239. -- max rel z will stop this loop from doing anything too harmful
  240. shaft_height = shaft_height 1
  241. end
  242. end
  243.  
  244. assert(forward(), "Could not move on top of the next mineshaft")
  245. assert(turnRight(), "Could not orient the turtle along the next mineshaft")
  246. else
  247. turtle.digUp() -- allowed to fail
  248. -- we've already dug down
  249. assert(turnRight(), "Could not orient the turtle back towards the current mineshaft")
  250. assert(turnRight(), "Could not orient the turtle into the current mineshaft")
  251.  
  252. no_err, action_taken = pcall(function()
  253. mine_down_until(function()
  254. return turtle.inspect()
  255. end)
  256. end)
  257.  
  258. if not no_err then
  259. assert(turnLeft(), "Could not orient the turtle back away from the current mineshaft")
  260. assert(turnLeft(), "Could not orient the turtle out of the current mineshaft")
  261. return true -- we hit bedrock. yield with the expectation that the first case will be called
  262. end
  263.  
  264. assert(turtle.dig(), "Could not mine the starting block of the current mineshaft")
  265. assert(turtle.forward(), "Could not move into the current mineshaft")
  266. end
  267. return true
  268. end
  269. return false
  270. end
  271.  
  272. --------- END MINE ---------
  273. --------- BEGIN DEPOSIT ---------
  274. function get_num_empty_slots()
  275. local empty_slots = SLOTS_EMPTY
  276. for i=1,16 do
  277. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  278. empty_slots -= 1
  279. end
  280. end
  281. return empty_slots
  282. end
  283.  
  284.  
  285.  
  286. function deposit_to_chest(slot)
  287. print("Depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  288. local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
  289. local prev_slot = turtle.getSelectedSlot()
  290. turtle.select(slot)
  291. assert(place(), "Could not place chest")
  292. for i=1,16 do
  293. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST then
  294. turtle.select(i)
  295. assert(drop(), "Could not insert item into chest")
  296. end
  297. assert(dig(), "Could not remove chest")
  298. print("Finished depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  299. turtle.select(prev_slot)
  300. end
  301.  
  302. function handle_inventory()
  303. if get_num_empty_slots() < 2 then
  304. try_with_rotation(function()
  305. deposit_to_chest(SLOT_DEPOSIT_CHEST)
  306. end)
  307. return true
  308. end
  309. return false
  310. end
  311. --------- END DEPOSIT ---------
  312.  
  313. function loop_logic()
  314. while true do
  315. logic_funcs = {handle_fuel, handle_inventory, maintain_bounds, mine_into_ground, mine_into_shaft, mine_forward_shaft}
  316. for _, func in ipairs(logic_funcs) do
  317. if func() then
  318. break
  319. end
  320. end
  321. print("Hit enter to continue...")
  322. io.read()
  323. end
  324. end
  325.  
  326. loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement