Advertisement
Cadevine

_cadeMine.lua

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