archlyn

autominer

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