Advertisement
Zalgo2462

Untitled

Jan 21st, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 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_one()
  185. success, data = turtle.inspectDown()
  186. assert(not (success and data.name == "minecraft:bedrock"), "Cannot move past bedrock")
  187.  
  188. local i = 0
  189. while not down() do
  190. turtle.digDown()
  191. assert(i < 300, "Failed to move down the mineshaft")
  192. i = i + 1
  193. end
  194. return true
  195. end
  196.  
  197. function mine_down_until(condition)
  198. local i = 0
  199. while not condition(i) do
  200. mine_down_one()
  201. i = i + 1
  202. end
  203. return i
  204. end
  205.  
  206. function mine_up_until(condition)
  207. local i = 0
  208. while not condition(i) do
  209. mine_up_one()
  210. i = i + 1
  211. end
  212. return i
  213. end
  214.  
  215. function mine_forward_shaft()
  216. turtle.digUp() -- allowed to fail
  217. turtle.digDown() -- allowed to fail
  218. return mine_forward_one()
  219. end
  220.  
  221.  
  222. local z_heading_up = false
  223. function maintain_bounds()
  224. if rel_y == MINESHAFT_WIDTH and rel_bearing == 0 or rel_y == 1 and rel_bearing == 2 then
  225. local at_end_of_z_func = function()
  226. success, data = turtle.inspectDown()
  227. return success and data.name == "minecraft:bedrock"
  228. end
  229. local mine_vert_3_func = function()
  230. return pcall(function()
  231. return mine_down_until(function(i)
  232. return i == 3
  233. end)
  234. end)
  235. end
  236. if z_heading_up then
  237. at_end_of_z_func = function()
  238. return rel_z >= 0
  239. end
  240. mine_vert_3_func = function()
  241. return pcall(function()
  242. return mine_up_until(function(i)
  243. return i == 3
  244. end)
  245. end)
  246. end
  247. end
  248.  
  249. local turnFunc = turnRight
  250. if rel_bearing == 2 then
  251. turnFunc = turnLeft
  252. end
  253.  
  254. if at_end_of_z_func() then
  255. assert(turnFunc(), "Could not face towards next mineshaft")
  256. mine_forward_one()
  257. assert(turnFunc(), "Could not orient the turtle along the next mineshaft")
  258. z_heading_up = not z_heading_up
  259. else
  260. turtle.digUp()
  261. turtle.digDown()
  262. assert(turnFunc(), "Could not orient the turtle back towards the current mineshaft")
  263. assert(turnFunc(), "Could not orient the turtle into the current mineshaft")
  264. mine_vert_3_func()
  265. end
  266. return true
  267. end
  268. return false
  269. end
  270.  
  271. --------- END MINE ---------
  272. --------- BEGIN DEPOSIT ---------
  273. function get_num_empty_slots()
  274. local empty_slots = SLOTS_EMPTY
  275. for i=1,16 do
  276. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  277. empty_slots = empty_slots - 1
  278. end
  279. end
  280. return empty_slots
  281. end
  282.  
  283.  
  284.  
  285. function deposit_to_chest(slot)
  286. print("Depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  287. local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
  288. local prev_slot = turtle.getSelectedSlot()
  289. turtle.select(slot)
  290. assert(place(), "Could not place chest")
  291. for i=1,16 do
  292. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  293. turtle.select(i)
  294. assert(drop(), "Could not insert item into chest")
  295. end
  296. end
  297. turtle.select(slot)
  298. assert(dig(), "Could not remove chest")
  299. print("Finished depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  300. turtle.select(prev_slot)
  301. end
  302.  
  303. function handle_inventory()
  304. if get_num_empty_slots() < 2 then
  305. try_with_rotation(function()
  306. deposit_to_chest(SLOT_DEPOSIT_CHEST)
  307. end)
  308. return true
  309. end
  310. return false
  311. end
  312. --------- END DEPOSIT ---------
  313.  
  314. function loop_logic()
  315. while true do
  316. logic_funcs = {handle_fuel, handle_inventory, maintain_bounds, mine_forward_shaft}
  317. for i, func in ipairs(logic_funcs) do
  318. if func() then
  319. print("Took action " .. i)
  320. break
  321. end
  322. end
  323. --print("Hit enter to continue...")
  324. --io.read()
  325. end
  326. end
  327.  
  328. loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement