Advertisement
Zalgo2462

Untitled

Jan 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 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_forward_shaft()
  207. turtle.digUp() -- allowed to fail
  208. turtle.digDown() -- allowed to fail
  209. return mine_forward_one()
  210. end
  211.  
  212. function maintain_bounds()
  213. if rel_y == MINESHAFT_WIDTH and rel_bearing == 0 or rel_y == 1 and rel_bearing == 2 then
  214. local at_bedrock = not turtle.digDown() and turtle.inspectDown()
  215. local turnFunc = turnRight
  216. local oppTurnFunc = turnLeft
  217. if rel_bearing == 2 then
  218. turnFunc = turnLeft
  219. oppTurnFunc = turnRight
  220. end
  221.  
  222. if at_bedrock then
  223. mine_up_until(function(i)
  224. return rel_z == 0
  225. end)
  226.  
  227. assert(turnFunc(), "Could not face towards next mineshaft")
  228. mine_forward_one()
  229. assert(turnFunc(), "Could not orient the turtle along the next mineshaft")
  230. else
  231. turtle.digUp() -- allowed to fail
  232. -- we've already dug down
  233. assert(turnFunc(), "Could not orient the turtle back towards the current mineshaft")
  234. assert(turnFunc(), "Could not orient the turtle into the current mineshaft")
  235.  
  236. no_err, dist = pcall(function()
  237. return mine_down_until(function(i)
  238. return i == 1
  239. end)
  240. end)
  241.  
  242. if not no_err then
  243. assert(oppTurnFunc(), "Could not orient the turtle back away from the current mineshaft")
  244. assert(oppTurnFunc(), "Could not orient the turtle out of the current mineshaft")
  245. return true -- we hit bedrock. yield with the expectation that the first case will be called
  246. end
  247. end
  248. return true
  249. end
  250. return false
  251. end
  252.  
  253. --------- END MINE ---------
  254. --------- BEGIN DEPOSIT ---------
  255. function get_num_empty_slots()
  256. local empty_slots = SLOTS_EMPTY
  257. for i=1,16 do
  258. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  259. empty_slots = empty_slots - 1
  260. end
  261. end
  262. return empty_slots
  263. end
  264.  
  265.  
  266.  
  267. function deposit_to_chest(slot)
  268. print("Depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  269. local place, dig, suck, drop = get_place_dig_suck_drop_in_empty_space()
  270. local prev_slot = turtle.getSelectedSlot()
  271. turtle.select(slot)
  272. assert(place(), "Could not place chest")
  273. for i=1,16 do
  274. if i ~= SLOT_DEPOSIT_CHEST and i ~= SLOT_FUEL_CHEST and turtle.getItemCount(i) ~= 0 then
  275. turtle.select(i)
  276. assert(drop(), "Could not insert item into chest")
  277. end
  278. end
  279. turtle.select(slot)
  280. assert(dig(), "Could not remove chest")
  281. print("Finished depositing into chest in slot " .. tostring(slot) .. ". Empty slots: " .. tostring(get_num_empty_slots()))
  282. turtle.select(prev_slot)
  283. end
  284.  
  285. function handle_inventory()
  286. if get_num_empty_slots() < 2 then
  287. try_with_rotation(function()
  288. deposit_to_chest(SLOT_DEPOSIT_CHEST)
  289. end)
  290. return true
  291. end
  292. return false
  293. end
  294. --------- END DEPOSIT ---------
  295.  
  296. function loop_logic()
  297. while true do
  298. logic_funcs = {handle_fuel, handle_inventory, maintain_bounds, mine_forward_shaft}
  299. for i, func in ipairs(logic_funcs) do
  300. if func() then
  301. print("Took action " .. i)
  302. break
  303. end
  304. end
  305. --print("Hit enter to continue...")
  306. --io.read()
  307. end
  308. end
  309.  
  310. loop_logic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement