Advertisement
Cadevine

cadeMine

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