Advertisement
Cadevine

cadeMine

Jan 17th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. -- get user input for parameters
  2. --[[
  3. print("Quarry length: ")
  4. local l = tonumber(io.read())
  5. print("Quarry width: ")
  6. local w = tonumber(io.read())
  7. print("Turtle Y: ")
  8. local z0 = tonumber(io.read())
  9. local listtype = 12
  10. while(listtype ~= 1 and listtype ~= 0) do
  11. print("Whitelist y/n?")
  12. listtype = io.read()
  13. if(listtype == "yes" or listtype == "Yes" or listtype == "y" or listtype == "Y") then
  14. listtype = 1
  15. elseif(listtype == "no" or listtype == "No" or listtype == "n" or listtype == "N") then
  16. listtype = 0
  17. else
  18. print("Invalid answer, please type either yes or no")
  19. end
  20. end
  21. print("Do you want to dig the whole chunk, y/n?")
  22. local dummy = io.read()
  23. if dummy == "n" or dummy == "N" or dummy == "no" or dummy == "No" then
  24. print("Layers to dig?")
  25. fin = tonumber(io.read())
  26. else fin = z0 end
  27. ]]--
  28. --------------------------------------
  29.  
  30. parameters = {...}
  31. local l = parameter[1]
  32. local w = parameter[2]
  33. local z0 = parameter[3]
  34. local listtype = parameter[4]
  35. local fin = parameter[5]
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. -- set up variables
  43.  
  44. local x = 0
  45. local y = 0
  46. local z = 0
  47. local rev = 1
  48. local face = 0
  49. local counter = 0
  50. arr = {0} -- for trash removal
  51. local cobble = false
  52. local stone = false
  53. local slot = 16
  54. trashtable = {}
  55. fuel = turtle.getItemDetail(16).name
  56.  
  57. -- define operational functions
  58.  
  59. function refuel() -- checks if bot needs fuel, and if it does uses internal items to refuel
  60. local f = false
  61. if turtle.getFuelLevel() < 500 then
  62. for i = 1,16 do
  63. fuelvar = false
  64. if not(turtle.getItemDetail(i) == nil) then
  65. if turtle.getItemDetail(i).name == fuel then
  66. fuelvar = true
  67. end
  68. end
  69. if fuelvar then
  70. turtle.select(i)
  71. while turtle.getItemCount() > 0 do
  72. turtle.refuel(1)
  73. if turtle.getFuelLevel() >= 500 then
  74. f = true
  75. turtle.select(1)
  76. break
  77. end
  78. end
  79. end
  80. if f then break end
  81. end
  82. end
  83. end
  84.  
  85. function moveForward() -- moves bot forward 1 block and updates position
  86. refuel()
  87. while not turtle.forward() do
  88. turtle.dig()
  89. end
  90. if face == 0 then y=y+1 end
  91. if face == 1 then x=x+1 end
  92. if face == 2 then y=y-1 end
  93. if face == 3 then x=x-1 end
  94. end
  95.  
  96. function turn(num) -- turns bot either left (-1) or right (+1) depending on input and updates face value
  97. if num == 1 then
  98. turtle.turnRight()
  99. face = (face+1)%4
  100. elseif num == -1 then
  101. turtle.turnLeft()
  102. face = (face-1)%4
  103. end
  104. end
  105.  
  106. function trashlist() -- generates white or black list depending on user input and stores block IDs in a table for reference | has some forge tags functionality for cobble and stone
  107. for i=1,15 do
  108. if turtle.getItemCount(i) > 0 then
  109. trashtable[i] = turtle.getItemDetail(i).name
  110. if trashtable[i] == "minecraft:cobblestone" then
  111. cobble = true
  112. end
  113. if trashtable[i] == "minecraft:stone" then
  114. stone = true
  115. end
  116. else
  117. slot = i
  118. break
  119. end
  120. end
  121. print("Item data saved")
  122.  
  123. while face ~= 2 do turn(1) end
  124. for i=1,slot-1 do
  125. turtle.select(i)
  126. turtle.drop()
  127. end
  128. turn(-1)
  129. turn(-1)
  130. turtle.select(1)
  131. end
  132.  
  133. function dispense() -- deposits mined items into chest behind where bot was initially placed. checks if bot needs fuel before dropping fuel into chest
  134. for i=1,15 do
  135. turtle.select(i)
  136. if not turtle.refuel(0) then
  137. turtle.drop()
  138. else turtle.transferTo(16) turtle.drop()
  139. end
  140. end
  141. turtle.select(1)
  142. end
  143.  
  144. function goHome(state) -- returns bot to starting location and handles different reasons for returning. If state == mine the bot returns to its last location when mining
  145. -- if bot returns because of a full inventory (state == full) then it will dispense items and then return to where it was in the quarry
  146. -- if bot returns due to a lack of fuel (state == fuel) then it will prompt user to input more fuel and return once they have done so
  147. -- if bot returns becuase it finished (state == comp) then it will face the starting direction and end the program
  148. print(state)
  149. xp = x
  150. yp = y
  151. zp = z
  152. facep = face
  153. while y > 0 do
  154. if face == 0 then turn(1) end
  155. if face == 1 then turn(1) end
  156. if face == 2 then moveForward() end
  157. if face == 3 then turn(-1) end
  158. end
  159. while x > 0 do
  160. if face == 0 then turn(-1) end
  161. if face == 1 then turn(-1) end
  162. if face == 2 then turn(1) end
  163. if face == 3 then moveForward() end
  164. end
  165.  
  166. if(state == "full" or state == "fuel") then trashRemoval() end
  167.  
  168. while z > 0 do
  169. turtle.up()
  170. z=z-1
  171. end
  172. while(face ~= 2) do turn(-1) end
  173. suc2,dat2 = turtle.inspect()
  174. if not suc2 then
  175. turn(-1)
  176. turn(-1)
  177. error()
  178. end
  179. while state == "fuel" do
  180. sleep(10)
  181. refuel()
  182. if turtle.getFuelLevel() >= 500 then state = "full" end -- set state to full instead of mine to dispense before returning
  183. end
  184. if state == "full" then
  185. dispense()
  186. arr = {0}
  187. state = "mine"
  188. end
  189. if state == "comp" then
  190. dispense()
  191. while face ~= 0 do turn(1) end
  192. error()
  193. end
  194. if state == "mine" then
  195. while z < zp do
  196. turtle.down() z = z+1
  197. end
  198. while x < xp do
  199. if face == 0 then turn(1) end
  200. if face == 1 then moveForward() end
  201. if face == 2 then turn(-1) end
  202. if face == 3 then turn(-1) end
  203. end
  204. while y < yp do
  205. if face == 0 then moveForward() end
  206. if face == 1 then turn(-1) end
  207. if face == 2 then turn(1) end
  208. if face == 3 then turn(1) end
  209. end
  210. while face ~= facep do
  211. turn(1)
  212. end
  213. end
  214. end
  215.  
  216. function compare(dir) -- checks block depending on (dir) against the list generated by trashtable and returns whether or not it matches something on the list as tf
  217. local suc = true
  218. local dat = nil
  219. local tf = true
  220. if(listtype == 1) then
  221. tf = false
  222. end
  223. if dir == "up" then
  224. suc,dat = turtle.inspectUp()
  225. elseif dir == "front" then
  226. suc,dat = turtle.inspect()
  227. elseif dir == "down" then
  228. suc,dat = turtle.inspectDown()
  229. elseif dir == "in" then
  230. dat = turtle.getItemDetail()
  231. end
  232. if suc then
  233. for i=1,slot-1 do
  234. if trashtable[i] == dat.name or listtype == 1 and "minecraft:coal_ore" == dat.name then
  235. return tf
  236. end
  237. if cobble and dat.tags["forge:cobblestone"] or stone and dat.tags["forge:stone"] then
  238. return tf
  239. end
  240. end
  241. end
  242. return not(tf)
  243. end
  244.  
  245.  
  246. function digUp() -- mines block above bot if the bot is supposed to (it is on the whitelist or is not on the blacklist)
  247. if not compare("up") then
  248. while turtle.digUp() do
  249. -- turtle.digUp()
  250. end
  251. end
  252. end
  253.  
  254. function digDown() -- digUp but down
  255. if not compare("down") then
  256. while turtle.digDown() do
  257. -- turtle.digDown()
  258. end
  259. end
  260. end
  261.  
  262.  
  263. function trashRemoval() -- removes internal items that either match against the blacklist or dont match against the whitelist (necessary becuase the bot has to mine unwanted blocks to move underground)
  264. for i=1,15 do
  265. if(arr[i+1] == nil) then
  266. local dispose = true
  267. for j=1,slot-1 do
  268. if turtle.getItemCount(i) > 0 then
  269. if listtype == 0 then
  270. if turtle.getItemDetail(i).name == trashtable[j] then
  271. turtle.select(i)
  272. turtle.drop()
  273. elseif cobble or stone then
  274. dat = turtle.getItemDetail(i,true)
  275. if cobble and dat.tags["forge:cobblestone"] or stone and dat.tags["forge:stone"] then
  276. turtle.select(i)
  277. turtle.drop()
  278. end
  279. end
  280. else
  281. if turtle.getItemDetail(i).name == trashtable[j] then
  282. dispose = false
  283. elseif(turtle.getItemDetail(i).name == turtle.getItemDetail(16).name) then
  284. turtle.select(i)
  285. turtle.transferTo(16)
  286. dispose = false
  287. end
  288. end
  289. end
  290. end
  291. if(listtype == 1 and dispose) then
  292. turtle.select(i)
  293. turtle.drop()
  294. end
  295. if(turtle.getItemCount(i) > 0) then
  296. arr[i+1] = 1
  297. arr[1] = arr[1]+1
  298. end
  299. end
  300. end
  301. turtle.select(1)
  302. end
  303.  
  304. function isFull() -- checks if there is a free inventory space
  305. local ret = true
  306. for i=0,14 do
  307. if turtle.getItemCount(15-i) == 0 then ret = false break end
  308. end
  309. return ret
  310. end
  311.  
  312. function checkfuel() -- refuels bot and then checks if there is enough fuel to make it back to the starting location and mine the next layer, returns for fuel if there is not
  313. refuel()
  314. if turtle.getFuelLevel() < (x+y+z)+l*w then
  315. goHome("fuel")
  316. end
  317. end
  318.  
  319. function mine() -- checks for sufficient fuel every 16 operations then mines the block infront of the bot, moves forward, then mines the block above and below if it should
  320. -- also checks for a full inventory and returns to drop off items if needed
  321. if counter%16 == 0 then checkfuel() counter = 1
  322. else counter = counter+1 end
  323. moveForward()
  324. digDown()
  325. digUp()
  326. if isFull() then
  327. trashRemoval()
  328. if arr[1] >= 14 then goHome("full") end
  329. end
  330. end
  331.  
  332. function Bore() -- moves turtle to z = z0-3 (in case of uneven bedrock)
  333. while z < z0-3 do
  334. while not turtle.down() do turtle.digDown() end
  335. z = z+1
  336. end
  337. end
  338.  
  339. function moveY() -- mines out a line while keeping track of location and facing
  340. if y == 0 then
  341. while y < l-1 do
  342. if face == 0 then
  343. mine()
  344. elseif face == 1 or face == 2 then
  345. turn(-1)
  346. else turn(1)
  347. end
  348. end
  349. else
  350. while y > 0 do
  351. if face == 2 then
  352. mine()
  353. elseif face == 1 or face == 0 then
  354. turn(1)
  355. else turn(-1)
  356. end
  357. end
  358. end
  359. end
  360.  
  361. function quarry() -- uses moveY to mine out a square
  362. refuel()
  363. for i=0,w-1 do
  364. moveY()
  365. if(i < w-1) then
  366. if(i%2 == 0) then
  367. turn(rev)
  368. else
  369. turn(-rev)
  370. end
  371. mine()
  372. end
  373. end
  374. end
  375.  
  376. function Mastermind() -- runs the other functions in the proper order to mine out the user defined area, returns once complete
  377. trashlist()
  378. refuel()
  379. if turtle.getFuelLevel() < 500 then
  380. print("Not enough fuel, please insert more fuel")
  381. while turtle.getFuelLevel() < 500 do
  382. sleep(5)
  383. refuel()
  384. end
  385. end
  386. print(z)
  387. Bore()
  388. print(fin)
  389. for i=0,fin-3 do
  390. print(i)
  391. if i%3 == 0 then
  392. turtle.digUp()
  393. quarry()
  394. if(w%2 == 0) then
  395. rev=0-rev
  396. end
  397. trashRemoval()
  398. end
  399. if i < fin-3 then
  400. while not turtle.up() do turtle.digUp() end
  401. z=z-1
  402. end
  403. end
  404. trashRemoval()
  405. print("Job's done")
  406. goHome("comp")
  407.  
  408. end
  409.  
  410. Mastermind() -- queue evil laughter
  411.  
  412. -- todo:
  413. -- track checked items to increase trashRemoval speed especially when inventory is nearly full
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement