arodalex2000

Turtle1

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