Advertisement
Zalgo2462

Untitled

Jan 18th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 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. if not turtle.up() then
  65. return false
  66. end
  67. rel_z = rel_z + 1
  68. return true
  69. end
  70.  
  71. function down()
  72. if not turtle.down() then
  73. return false
  74. end
  75. rel_z = rel_z - 1
  76. return true
  77. end
  78.  
  79. --------- END NAVIGATION ---------
  80. --------- BEGIN FUEL ---------
  81. function get_place_dig_suck_drop_in_empty_space()
  82. local success, data = turtle.inspect()
  83. if not success then
  84. return turtle.place, turtle.dig, turtle.suck, turtle.drop
  85. end
  86.  
  87. success, data = turtle.inspectUp()
  88. if not success then
  89. return turtle.placeUp, turtle.digUp, turtle.suckUp, turtle.dropUp
  90. end
  91.  
  92. success, data = turtle.inspectDown()
  93. if not success then
  94. return turtle.placeDown, turtle.digDown, turtle.suckDown, turtle.dropDown
  95. end
  96.  
  97. error("No empty space in front, above, or below the turtle")
  98. end
  99.  
  100. function try_with_rotation(func)
  101. for i=1,4 do
  102. vals = {pcall(func)}
  103. if vals[1] then
  104. for j = 1,(i-1) do
  105. turnRight()
  106. end
  107. table.remove(vals, 1)
  108. return unpack(vals)
  109. else
  110. print(vals[2])
  111. end
  112. turnLeft()
  113. end
  114.  
  115. error("Could not execute function in any orientation")
  116. end
  117.  
  118. function refuel_from_chest(slot)
  119. print("Refueling from chest in slot " .. tostring(slot) .. ". Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
  120. local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
  121. local prev_slot = turtle.getSelectedSlot()
  122. turtle.select(slot)
  123. local success = place()
  124. assert(success, "Could not place chest")
  125. while turtle.getFuelLevel() ~= turtle.getFuelLimit() do
  126. success = suck()
  127. assert(success, "Could not obtain fuel from chest")
  128. success = turtle.refuel()
  129. assert(success, "Could not use item obtained from chest as fuel")
  130. end
  131. turtle.select(slot)
  132. success = dig()
  133. assert(success, "Could not remove chest")
  134. print("Finished refueling. Fuel: " .. tostring(turtle.getFuelLevel()) .. "/" .. tostring(turtle.getFuelLimit()))
  135. turtle.select(prev_slot)
  136. end
  137.  
  138. function handle_fuel()
  139. if turtle.getFuelLevel() / turtle.getFuelLimit() < 0.1 then
  140. try_with_rotation(function()
  141. refuel_from_chest(SLOT_FUEL_CHEST)
  142. end)
  143. return true
  144. end
  145. return false
  146. end
  147.  
  148. --------- END FUEL ---------
  149. --------- BEGIN MINE ---------
  150. function inspect_vertical()
  151. local _, up_data = turtle.inspectUp()
  152. local _, front_data = turtle.inspect()
  153. local _, down_data = turtle.inspectDown()
  154. return {down_data, front_data, up_data}
  155. end
  156.  
  157. function inspect_vertical_simple()
  158. local up_data = turtle.inspectUp()
  159. local front_data = turtle.inspect()
  160. local down_data = turtle.inspectDown()
  161. return {down_data, front_data, up_data}
  162. end
  163.  
  164. function mine_forward_one()
  165. local i = 0
  166. while not forward() do
  167. turtle.dig()
  168. assert (i < 300, "Failed to move forward down the mineshaft")
  169. i = i + 1
  170. end
  171. return true
  172. end
  173.  
  174. function mine_up_one()
  175. local i = 0
  176. while not up() do
  177. turtle.digUp()
  178. assert(i < 300, "Failed to move up the mineshaft")
  179. i = i + 1
  180. end
  181. return true
  182. end
  183.  
  184. function mine_down_until(condition)
  185. local i = 0
  186. while not condition(i) do
  187. local block_exists = turtle.inspectDown()
  188. if block_exists then
  189. assert(turtle.digDown(), "Could not dig down")
  190. end
  191. assert(down(), "Could not move down")
  192. i = i + 1
  193. end
  194. return i
  195. end
  196.  
  197. function mine_up_until(condition)
  198. local i = 0
  199. while not condition(i) do
  200. mine_up_one()
  201. i = i + 1
  202. end
  203. return i
  204. end
  205.  
  206. function mine_into_ground()
  207. no_err, distance = pcall(function()
  208. return mine_down_until(function(i)
  209. local v_state = inspect_vertical_simple()
  210. return v_state[1] == true and v_state[2] == true
  211. end)
  212. end)
  213.  
  214. if not no_err then
  215. return false -- allow miner to work on bedrock
  216. end
  217. return distance ~= 0 -- could also be interpreted as condition resolved
  218. end
  219.  
  220. function mine_into_shaft()
  221. local v_state = inspect_vertical_simple()
  222. if v_state[1] == true and v_state[2] == true and v_state[3] == false then
  223. local success = turtle.digDown()
  224. if not success then
  225. return false -- We can't mine into the shaft (likely bedrock). Go ahead and act as if we were in 3 high shaft.
  226. end
  227. assert(down(), "Could not move down")
  228. turtle.digDown() -- Allowed to fail, just to keep things nice...
  229. return mine_forward_one()
  230. end
  231. return false
  232. end
  233.  
  234. function mine_forward_shaft()
  235. turtle.digUp() -- allowed to fail
  236. turtle.digDown() -- allowed to fail
  237. return mine_forward_one()
  238. end
  239.  
  240. function maintain_bounds()
  241. if rel_y == MINESHAFT_WIDTH and rel_bearing == 0 or rel_y == 1 and rel_bearing == 2 then
  242. local at_bedrock = not turtle.digDown() and turtle.inspectDown()
  243. local turnFunc = turnRight
  244. local oppTurnFunc = turnLeft
  245. if rel_bearing == 2 then
  246. turnFunc = turnLeft
  247. oppTurnFunc = turnRight
  248. end
  249.  
  250. if at_bedrock then
  251. assert(turnFunc(), "Could not face towards next mineshaft")
  252.  
  253. local shaft_height = 0
  254. local initial_x = rel_x
  255. local MAX_X_SEARCH = 256
  256.  
  257. while shaft_height < 3 do
  258. while not turtle.inspect() do
  259. assert( math.abs(rel_x - initial_x) < 256, "Could not find next mineshaft")
  260. assert(forward(), "Could not move towards next mineshaft")
  261. end
  262. shaft_height = mine_up_until(function(i)
  263. return not turtle.inspect()
  264. end)
  265. end
  266.  
  267. assert(forward(), "Could not move on top of the next mineshaft")
  268. assert(turnFunc(), "Could not orient the turtle along the next mineshaft")
  269. else
  270. turtle.digUp() -- allowed to fail
  271. -- we've already dug down
  272. assert(turnFunc(), "Could not orient the turtle back towards the current mineshaft")
  273. assert(turnFunc(), "Could not orient the turtle into the current mineshaft")
  274.  
  275. no_err, dist = pcall(function()
  276. return mine_down_until(function(i)
  277. return turtle.inspect()
  278. end)
  279. end)
  280.  
  281. if not no_err then
  282. assert(oppTurnFunc(), "Could not orient the turtle back away from the current mineshaft")
  283. assert(oppTurnFunc(), "Could not orient the turtle out of the current mineshaft")
  284. return true -- we hit bedrock. yield with the expectation that the first case will be called
  285. end
  286. end
  287. return true
  288. end
  289. return false
  290. end
  291.  
  292. --------- END MINE ---------
  293. --------- BEGIN DEPOSIT ---------
  294. function get_num_empty_slots()
  295. local empty_slots = SLOTS_EMPTY
  296. for i=1,16 do
  297. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  298. empty_slots = empty_slots - 1
  299. end
  300. end
  301. return empty_slots
  302. end
  303.  
  304.  
  305.  
  306. function deposit_to_chest(slot)
  307. print("Depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  308. local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
  309. local prev_slot = turtle.getSelectedSlot()
  310. turtle.select(slot)
  311. assert(place(), "Could not place chest")
  312. for i=1,16 do
  313. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  314. turtle.select(i)
  315. assert(drop(), "Could not insert item into chest")
  316. end
  317. end
  318. turtle.select(slot)
  319. assert(dig(), "Could not remove chest")
  320. print("Finished depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  321. turtle.select(prev_slot)
  322. end
  323.  
  324. function handle_inventory()
  325. if get_num_empty_slots() < 2 then
  326. try_with_rotation(function()
  327. deposit_to_chest(SLOT_DEPOSIT_CHEST)
  328. end)
  329. return true
  330. end
  331. return false
  332. end
  333. --------- END DEPOSIT ---------
  334.  
  335. function loop_logic()
  336. while true do
  337. logic_funcs = {handle_fuel, handle_inventory, maintain_bounds, mine_into_ground, mine_into_shaft, mine_forward_shaft}
  338. for i, func in ipairs(logic_funcs) do
  339. if func() then
  340. print("Took action " .. i)
  341. break
  342. end
  343. end
  344. --print("Hit enter to continue...")
  345. --io.read()
  346. end
  347. end
  348.  
  349. loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement