Advertisement
Zalgo2462

Untitled

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