jamesonBradfield

computercraft mining

Jul 13th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. -- Simple Excavation Script with EnderIO Ender Chest Support
  2.  
  3. -- Configuration
  4. local MaxForward = 16
  5. local MaxRight = 16
  6. local SkipHoles = 0
  7.  
  8. -- Slot configuration
  9. local EnderChestSlot = 15
  10. local FuelSlot = 16
  11. local WorkingSlots = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
  12.  
  13. -- Position tracking
  14. local pos = {0, 0, 0}
  15. local facing = 0
  16. local curMessage = ''
  17. local totalBlocks = 0
  18.  
  19. -- Display function
  20. function tWrite(txt)
  21. term.clear()
  22. term.setCursorPos(1, 1)
  23. if txt then
  24. term.write(txt)
  25. else
  26. term.write(curMessage)
  27. term.setCursorPos(1, 2)
  28. term.write("Total blocks mined: " .. totalBlocks)
  29. end
  30. end
  31.  
  32. -- Count free inventory slots
  33. function getInventorySpace()
  34. local freeSlots = 0
  35. for _, slot in ipairs(WorkingSlots) do
  36. if turtle.getItemCount(slot) == 0 then
  37. freeSlots = freeSlots + 1
  38. end
  39. end
  40. return freeSlots
  41. end
  42.  
  43. -- Simple dig function
  44. function dig(direction)
  45. local digFunc, detectFunc
  46.  
  47. if direction == "forward" then
  48. digFunc = turtle.dig
  49. detectFunc = turtle.detect
  50. elseif direction == "up" then
  51. digFunc = turtle.digUp
  52. detectFunc = turtle.detectUp
  53. elseif direction == "down" then
  54. digFunc = turtle.digDown
  55. detectFunc = turtle.detectDown
  56. else
  57. return false
  58. end
  59.  
  60. if detectFunc() and digFunc() then
  61. totalBlocks = totalBlocks + 1
  62. return true
  63. end
  64.  
  65. return false
  66. end
  67.  
  68. -- Movement functions
  69. function turnRight(n)
  70. n = n or 1
  71. for i = 1, n do
  72. facing = (facing + 1) % 4
  73. turtle.turnRight()
  74. end
  75. end
  76.  
  77. function turnLeft(n)
  78. n = n or 1
  79. for i = 1, n do
  80. facing = (facing - 1) % 4
  81. turtle.turnLeft()
  82. end
  83. end
  84.  
  85. function checkFuel(n)
  86. if turtle.getFuelLevel() < n then
  87. tWrite('Refueling...')
  88. turtle.select(FuelSlot)
  89. while turtle.getFuelLevel() < n do
  90. turtle.refuel(1)
  91. end
  92. tWrite()
  93. end
  94. end
  95.  
  96. function up(n)
  97. n = n or 1
  98. checkFuel(n)
  99. for i = 1, n do
  100. if turtle.up() then
  101. pos[3] = pos[3] + 1
  102. end
  103. end
  104. end
  105.  
  106. function down(n)
  107. n = n or 1
  108. checkFuel(n)
  109. for i = 1, n do
  110. if turtle.down() then
  111. pos[3] = pos[3] - 1
  112. end
  113. end
  114. end
  115.  
  116. function forward(n)
  117. n = n or 1
  118. checkFuel(n)
  119.  
  120. if facing == 0 then
  121. for i = 1, n do
  122. if turtle.forward() then
  123. pos[2] = pos[2] + 1
  124. end
  125. end
  126. elseif facing == 1 then
  127. turnLeft()
  128. for i = 1, n do
  129. if turtle.forward() then
  130. pos[2] = pos[2] + 1
  131. end
  132. end
  133. elseif facing == 2 then
  134. for i = 1, n do
  135. if turtle.back() then
  136. pos[2] = pos[2] + 1
  137. end
  138. end
  139. else
  140. turnRight()
  141. for i = 1, n do
  142. if turtle.forward() then
  143. pos[2] = pos[2] + 1
  144. end
  145. end
  146. end
  147. end
  148.  
  149. function backward(n)
  150. n = n or 1
  151. checkFuel(n)
  152.  
  153. if facing == 0 then
  154. for i = 1, n do
  155. if turtle.back() then
  156. pos[2] = pos[2] - 1
  157. end
  158. end
  159. elseif facing == 1 then
  160. turnRight()
  161. for i = 1, n do
  162. if turtle.forward() then
  163. pos[2] = pos[2] - 1
  164. end
  165. end
  166. elseif facing == 2 then
  167. for i = 1, n do
  168. if turtle.forward() then
  169. pos[2] = pos[2] - 1
  170. end
  171. end
  172. else
  173. turnLeft()
  174. for i = 1, n do
  175. if turtle.forward() then
  176. pos[2] = pos[2] - 1
  177. end
  178. end
  179. end
  180. end
  181.  
  182. function right(n)
  183. n = n or 1
  184. checkFuel(n)
  185.  
  186. if facing == 0 then
  187. turnRight()
  188. for i = 1, n do
  189. if turtle.forward() then
  190. pos[1] = pos[1] + 1
  191. end
  192. end
  193. elseif facing == 1 then
  194. for i = 1, n do
  195. if turtle.forward() then
  196. pos[1] = pos[1] + 1
  197. end
  198. end
  199. elseif facing == 2 then
  200. turnLeft()
  201. for i = 1, n do
  202. if turtle.forward() then
  203. pos[1] = pos[1] + 1
  204. end
  205. end
  206. else
  207. for i = 1, n do
  208. if turtle.back() then
  209. pos[1] = pos[1] + 1
  210. end
  211. end
  212. end
  213. end
  214.  
  215. function left(n)
  216. n = n or 1
  217. checkFuel(n)
  218.  
  219. if facing == 0 then
  220. turnLeft()
  221. for i = 1, n do
  222. if turtle.forward() then
  223. pos[1] = pos[1] - 1
  224. end
  225. end
  226. elseif facing == 1 then
  227. for i = 1, n do
  228. if turtle.back() then
  229. pos[1] = pos[1] - 1
  230. end
  231. end
  232. elseif facing == 2 then
  233. turnRight()
  234. for i = 1, n do
  235. if turtle.forward() then
  236. pos[1] = pos[1] - 1
  237. end
  238. end
  239. else
  240. for i = 1, n do
  241. if turtle.forward() then
  242. pos[1] = pos[1] - 1
  243. end
  244. end
  245. end
  246. end
  247.  
  248. function getTo(nPos)
  249. if not nPos[1] then nPos[1] = pos[1] end
  250. if not nPos[2] then nPos[2] = pos[2] end
  251. if not nPos[3] then nPos[3] = pos[3] end
  252. local diff = {nPos[1] - pos[1], nPos[2] - pos[2], nPos[3] - pos[3]}
  253.  
  254. if diff[1] > 0 then
  255. right(diff[1])
  256. elseif diff[1] < 0 then
  257. left(-diff[1])
  258. end
  259.  
  260. if diff[2] > 0 then
  261. forward(diff[2])
  262. elseif diff[2] < 0 then
  263. backward(-diff[2])
  264. end
  265.  
  266. if diff[3] > 0 then
  267. up(diff[3])
  268. elseif diff[3] < 0 then
  269. down(-diff[3])
  270. end
  271. end
  272.  
  273. -- EnderIO Ender Chest functions
  274. function transferToEnderChest()
  275. curMessage = 'Using ender chest...'
  276. tWrite()
  277.  
  278. -- First dig the block below if there is one
  279. if turtle.detectDown() then
  280. if turtle.digDown() then
  281. totalBlocks = totalBlocks + 1
  282. end
  283. end
  284.  
  285. turtle.select(EnderChestSlot)
  286. if turtle.placeDown() then
  287. local itemsTransferred = 0
  288.  
  289. for _, slot in ipairs(WorkingSlots) do
  290. turtle.select(slot)
  291. if turtle.getItemCount(slot) > 0 then
  292. itemsTransferred = itemsTransferred + turtle.getItemCount(slot)
  293. turtle.dropDown()
  294. end
  295. end
  296.  
  297. turtle.select(EnderChestSlot)
  298. turtle.digDown()
  299.  
  300. curMessage = 'Transferred ' .. itemsTransferred .. ' items'
  301. tWrite()
  302. return true
  303. else
  304. curMessage = 'Failed to place ender chest'
  305. tWrite()
  306. return false
  307. end
  308. end
  309.  
  310. -- Mining functions
  311. function drillSides()
  312. for i = 0, 3 do
  313. dig("forward")
  314. if i ~= 3 then
  315. turnLeft()
  316. end
  317. end
  318. end
  319.  
  320. function ascend()
  321. curMessage = 'Ascending...'
  322. tWrite()
  323.  
  324. while pos[3] < 0 do
  325. dig("up")
  326. up()
  327. end
  328.  
  329. tWrite()
  330. end
  331.  
  332. function drill()
  333. curMessage = 'Drilling...'
  334. tWrite()
  335.  
  336. while not turtle.detectDown() or turtle.digDown() do
  337. if turtle.detectDown() and turtle.digDown() then
  338. totalBlocks = totalBlocks + 1
  339. end
  340.  
  341. down()
  342. drillSides()
  343.  
  344. if getInventorySpace() <= 2 then
  345. transferToEnderChest()
  346. end
  347. end
  348.  
  349. ascend()
  350. tWrite()
  351. end
  352.  
  353. function startingPoint(row)
  354. return (2 * (row-1)) % 5 + 1
  355. end
  356.  
  357. function drillRow(row)
  358. getTo({row, startingPoint(row)})
  359.  
  360. local willMove = 0
  361.  
  362. while (pos[2] + willMove) <= (MaxForward - 2) do
  363. if getInventorySpace() <= 3 then
  364. transferToEnderChest()
  365. end
  366.  
  367. if willMove == 0 then
  368. willMove = 5
  369. else
  370. forward(willMove)
  371. end
  372.  
  373. if SkipHoles == 0 then
  374. drill()
  375. else
  376. SkipHoles = SkipHoles - 1
  377. end
  378. end
  379. end
  380.  
  381. -- Main execution
  382. tWrite('Starting excavation...')
  383.  
  384. for i = 1, MaxRight - 2 do
  385. curMessage = 'Excavating row ' .. i
  386. tWrite()
  387. drillRow(i)
  388. end
  389.  
  390. tWrite('Excavation complete!')
  391. tWrite('Total blocks mined: ' .. totalBlocks)
Add Comment
Please, Sign In to add comment