Advertisement
Zalgo2462

Untitled

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