Syldarion

SylMiner -- ComputerCraft Turtle Program

Dec 1st, 2020 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. -- SylMiner
  2.  
  3. local tunnelPairCount
  4. local tunnelLength
  5. local fuelRequired
  6. local totalItems = 0
  7. local torchSpacing = 7
  8.  
  9. local function calculateFuelRequired()
  10. local mainFuel = 20 * tunnelPairCount + 4
  11. local fuelPerPair = 10 * tunnelLength + 8
  12. return mainFuel + (tunnelPairCount * fuelPerPair)
  13. end
  14.  
  15. local function fuelBotForDig()
  16. if fuelRequired > turtle.getFuelLimit() then
  17. print("Job requires more fuel than the limit (20000)")
  18. return false
  19. end
  20.  
  21. local currentFuel = turtle.getFuelLevel()
  22. if currentFuel > fuelRequired then
  23. return true
  24. end
  25.  
  26. local startFuel = turtle.getFuelLevel()
  27. -- select the fuel slot
  28. turtle.select(1)
  29. local fuelInSlot = turtle.getItemCount()
  30. if fuelInSlot == 0 then
  31. -- There's nothing in the fuel slot
  32. print("Nothing in fuel slot")
  33. return false
  34. end
  35.  
  36. if not turtle.refuel(1) then
  37. -- The slot didn't have a fuel item
  38. print("Non-fuel item in fuel slot")
  39. return false
  40. end
  41.  
  42. local fuelDifference = currentFuel - startFuel
  43. local fuelItemsRequired = math.ceil(fuelRequired / fuelDifference)
  44. local fuelToUse = math.min(fuelItemsRequired, fuelInSlot - 1)
  45.  
  46. turtle.refuel(fuelToUse)
  47. -- At this point, the fuel slot should be empty
  48. return true
  49. end
  50.  
  51. local function unload()
  52. -- assumes the turtle is over a chest
  53. for n=4,16 do
  54. local nCount = turtle.getItemCount(n)
  55. if nCount > 0 then
  56. turtle.select(n)
  57. turtle.dropDown()
  58. end
  59. end
  60. totalItems = 0
  61. end
  62.  
  63. local function checkCollected()
  64. local newTotalItems = 0
  65. for n=1,16 do
  66. newTotalItems = newTotalItems + turtle.getItemCount(n)
  67. end
  68. if newTotalItems > totalItems then
  69. totalItems = newTotalItems
  70. return true
  71. end
  72. return false
  73. end
  74.  
  75. local function digAndMoveForward()
  76. while not turtle.forward() do
  77. if turtle.detect() then
  78. if not turtle.dig() then
  79. return false
  80. end
  81. else
  82. turtle.attack()
  83. sleep(0.5)
  84. end
  85. end
  86. return true
  87. end
  88.  
  89. local function digUp()
  90. if turtle.detectUp() then
  91. if not turtle.digUp() then
  92. return false
  93. end
  94. else
  95. turtle.attackUp()
  96. end
  97. return true
  98. end
  99.  
  100. local function digDown()
  101. if turtle.detectDown() then
  102. if not turtle.digDown() then
  103. return false
  104. end
  105. else
  106. turtle.attackDown()
  107. end
  108. return true
  109. end
  110.  
  111. local function placeTorchBelow()
  112. turtle.select(3)
  113. turtle.placeDown()
  114. end
  115.  
  116. local function digSlice(sliceNumber)
  117. -- This function assumes that for an odd slice, we start left
  118. -- for an even slice, we start right
  119. if sliceNumber % 2 == 0 then
  120. digAndMoveForward()
  121. digUp()
  122. digDown()
  123. turtle.turnLeft()
  124.  
  125. digAndMoveForward()
  126. digUp()
  127. digDown()
  128.  
  129. digAndMoveForward()
  130. digUp()
  131. digDown()
  132. turtle.turnRight()
  133. else
  134. digAndMoveForward()
  135. digUp()
  136. digDown()
  137. turtle.turnRight()
  138.  
  139. digAndMoveForward()
  140. digUp()
  141. digDown()
  142.  
  143. digAndMoveForward()
  144. digUp()
  145. digDown()
  146. turtle.turnLeft()
  147. end
  148.  
  149. -- the problem with checking after the whole slice is done
  150. -- is that we might miss some stuff
  151. if not checkCollected() then
  152. -- return to pair chest
  153. if sliceNumber % 2 == 0 then
  154. turtle.turnRight()
  155. turtle.forward()
  156. turtle.turnRight()
  157. else
  158. turtle.turnLeft()
  159. turtle.forward()
  160. turtle.turnLeft()
  161. end
  162.  
  163. -- now facing pair chest
  164. for i=1,sliceNumber+1 do
  165. turtle.forward()
  166. end
  167.  
  168. unload()
  169.  
  170. turtle.turnRight()
  171. turtle.turnRight()
  172.  
  173. for i=1,sliceNumber+1 do
  174. turtle.forward()
  175. end
  176.  
  177. -- return to spot
  178. if sliceNumber % 2 == 0 then
  179. turtle.turnLeft()
  180. turtle.forward()
  181. turtle.turnRight()
  182. else
  183. turtle.turnRight()
  184. turtle.forward()
  185. turtle.turnLeft()
  186. end
  187. end
  188.  
  189. if sliceNumber % 7 == 0 then
  190. placeTorchBelow()
  191. end
  192. end
  193.  
  194. local function digTunnel(isLeft)
  195. -- assumes you're positioned at left center of tunnel face
  196. placeTorchBelow()
  197. for slice = 1,tunnelLength do
  198. digSlice(slice)
  199. end
  200.  
  201. -- center turtle in tunnel
  202. turtle.turnRight()
  203. if turtle.detect() then
  204. turtle.turnRight()
  205. turtle.turnRight()
  206. turtle.forward()
  207. turtle.turnLeft()
  208. else
  209. turtle.forward()
  210. turtle.turnRight()
  211. end
  212.  
  213. --return to center
  214. for i=1,tunnelLength+1 do
  215. turtle.forward()
  216. end
  217.  
  218. if isLeft then
  219. turtle.turnLeft()
  220. else
  221. turtle.turnRight()
  222. end
  223.  
  224. unload()
  225. end
  226.  
  227. local function placePairChest()
  228. -- This function assumes you're in the exact center of the pair
  229. turtle.select(2)
  230. turtle.placeDown()
  231. end
  232.  
  233. local function digPair()
  234. -- assumes you're in pair center
  235. -- go to left start
  236. turtle.turnLeft()
  237. turtle.forward()
  238. turtle.turnLeft()
  239. turtle.forward()
  240. turtle.turnRight()
  241. digTunnel(true)
  242. -- go to right start
  243. turtle.turnRight()
  244. turtle.forward()
  245. turtle.turnLeft()
  246. turtle.forward()
  247. turtle.turnRight()
  248. digTunnel(false)
  249. end
  250.  
  251. local function runDig()
  252. -- dig main tunnel
  253. local mainTunnelLength = 4 * tunnelPairCount
  254. for slice = 1,mainTunnelLength do
  255. digSlice(slice)
  256. end
  257.  
  258. -- return to first pair center
  259. turtle.turnRight()
  260. if turtle.detect() then
  261. turtle.turnRight()
  262. turtle.turnRight()
  263. turtle.forward()
  264. turtle.turnLeft()
  265. else
  266. turtle.forward()
  267. turtle.turnRight()
  268. end
  269.  
  270. for i=1,mainTunnelLength-3 do
  271. turtle.forward()
  272. end
  273.  
  274. turtle.turnRight()
  275. turtle.turnRight()
  276.  
  277. --place first chest
  278. placePairChest()
  279.  
  280. --dig first pair
  281. digPair()
  282.  
  283. --dig the remaining pairs
  284. for pair = 1,tunnelPairCount-1 do
  285. for i=1,4 do
  286. turtle.forward()
  287. end
  288. placePairChest()
  289. digPair()
  290. end
  291.  
  292. --return to the tunnel start
  293. turtle.turnRight()
  294. turtle.turnRight()
  295.  
  296. for i=1,mainTunnelLength-3 do
  297. turtle.forward()
  298. end
  299. end
  300.  
  301. term.clear()
  302. textutils.slowPrint("SylMiner v0.1")
  303.  
  304. print("Enter number of tunnel pairs to dig: ")
  305. local input = read()
  306. tunnelPairCount = tonumber(input)
  307. print("Enter length of each tunnel: ")
  308. input = read()
  309. tunnelLength = tonumber(input)
  310.  
  311. fuelRequired = calculateFuelRequired()
  312. torchesRequired = tunnelPairCount * 2 * (math.ceil(tunnelLength / torchSpacing) + 1)
  313.  
  314. print(string.format("This job requires %d units of fuel.", fuelRequired))
  315. print(string.format("This job requires %d chests", tunnelPairCount))
  316. print(string.format("This job requires %d torches", torchesRequired))
  317. print("Slot 1: Fuel | Slot 2: Chests | Slot 3: Torches")
  318. print("Press any key to start...")
  319.  
  320. read()
  321.  
  322. if fuelBotForDig() then
  323. runDig()
  324. end
  325.  
Add Comment
Please, Sign In to add comment