Advertisement
ViktorP04

Mining bot

Sep 18th, 2023 (edited)
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.91 KB | None | 0 0
  1. local distanceMoved = 0
  2. local torchCount = 0
  3.  
  4. local sideDistance = 0
  5. local lengthDistance = 0
  6.  
  7. local facing = "forward"
  8.  
  9. local returnCommandSent = false
  10. local stopCommandSent = false
  11.  
  12. local lastBlockAlreadyTorch = false
  13.  
  14. local maxSideDistance = 30
  15.  
  16. local lastFullLength = 5
  17.  
  18. local blocksSinceLastDump = 0
  19. local dumps = 0
  20.  
  21.  
  22.  
  23. function MainLoop()
  24.     while not stopCommandSent do
  25.         term.clear()
  26.         sideDistance = 0
  27.         lengthDistance = 0
  28.         facing = "forward"
  29.  
  30.         print("Starting")
  31.  
  32.         Restock()
  33.  
  34.         if (not GoodFuelLevel(maxSideDistance + lastFullLength)) then
  35.             print("Not enough fuel!")
  36.         else
  37.             StripMine()
  38.  
  39.             print("Returning")
  40.  
  41.             ReturnHome()
  42.         end
  43.     end
  44. end
  45.  
  46. function FindStripmine()
  47.  
  48. end
  49.  
  50. function StripMine()
  51.     turtle.turnLeft()
  52.     turtle.turnLeft()
  53.  
  54.     --Start by moving forward 5 blocks
  55.     for i = 1, lastFullLength, 1 do
  56.         --Dig
  57.         if turtle.detect() and CanCollect() then
  58.             --print("Dig")
  59.             turtle.dig()
  60.         end
  61.         if turtle.detectUp() and CanCollect() then
  62.             turtle.digUp()
  63.         end
  64.         if returnCommandSent then
  65.             break
  66.         end
  67.  
  68.         if turtle.forward() then
  69.             lengthDistance = lengthDistance + 1
  70.             blocksSinceLastDump = blocksSinceLastDump + 1
  71.         end
  72.  
  73.         distanceMoved = distanceMoved + 1
  74.     end
  75.  
  76.  
  77.     while GoodFuelLevel() and CanCollect() do
  78.         --term.clear()
  79.         --print("Moved to block " .. tostring(distanceMoved))
  80.         --print("Fuel at: " .. tostring(turtle.getFuelLevel()))
  81.  
  82.  
  83.         if (facing == "forward" and lengthDistance % 3 == 0) then
  84.             if (sideDistance > 0) then
  85.                 turtle.turnRight()
  86.                 facing = "right"
  87.             else
  88.                 turtle.turnLeft()
  89.                 facing = "left"
  90.             end
  91.         end
  92.  
  93.         if (facing == "left" and sideDistance >= maxSideDistance) then
  94.             turtle.turnRight()
  95.             facing = "forward"
  96.         else
  97.             if (facing == "right" and sideDistance <= -maxSideDistance) then
  98.                 turtle.turnLeft()
  99.                 facing = "forward"
  100.             end
  101.         end
  102.  
  103.         --Sets the respawn point for the next time the turtle goes out
  104.         if (sideDistance == 0) then
  105.             lastFullLength = lengthDistance
  106.         end
  107.  
  108.  
  109.  
  110.  
  111.         --Check if there is a torch above
  112.         if math.abs(sideDistance) % 10 == 5 - 1 or math.abs(sideDistance) % 10 == 5 + 1 then
  113.             local success, data = turtle.inspectUp()
  114.             if (success and data.name == "minecraft:wall_torch") then
  115.                 lastBlockAlreadyTorch = true
  116.             end
  117.         end
  118.  
  119.  
  120.         --Dig
  121.         while turtle.detect() do
  122.             turtle.dig()
  123.         end
  124.         while turtle.detectUp() and not lastBlockAlreadyTorch do
  125.             turtle.digUp()
  126.         end
  127.  
  128.  
  129.         print("return: ", returnCommandSent)
  130.         if returnCommandSent then
  131.             return
  132.         end
  133.  
  134.  
  135.  
  136.  
  137.         --print("Torch check: " .. torchCount .. ", " .. lastTorchDistance  .. ", " ..  setTorchDistance)
  138.  
  139.         if math.abs(sideDistance) % 10 == 5 then
  140.             if torchCount > 0 and GoodFuelLevel(2) and not lastBlockAlreadyTorch then
  141.                 if turtle.back() then
  142.                     turtle.select(LeastAmountOfTorchSlot())
  143.                     turtle.placeUp()
  144.                     turtle.forward()
  145.                     distanceMoved = distanceMoved + 2
  146.                     torchCount = torchCount - 1
  147.                 end
  148.             end
  149.             lastBlockAlreadyTorch = false
  150.         end
  151.  
  152.  
  153.         distanceMoved = distanceMoved + 1
  154.  
  155.  
  156.         if turtle.forward() then
  157.             if (facing == "forward") then
  158.                 lengthDistance = lengthDistance + 1;
  159.                 blocksSinceLastDump = blocksSinceLastDump + 1;
  160.             else
  161.                 if (facing == "left") then
  162.                     sideDistance = sideDistance + 1;
  163.                 else
  164.                     if (facing == "right") then
  165.                         sideDistance = sideDistance - 1;
  166.                     end
  167.                 end
  168.             end
  169.         end
  170.     end
  171.     returnCommandSent = true
  172. end
  173.  
  174. function GoodFuelLevel(extra)
  175.     if extra == nil then
  176.         extra = 0
  177.     end
  178.  
  179.  
  180.     --If the bot has gone 100 blocks without dumping it will go dump anyway
  181.     if (blocksSinceLastDump == dumps*100 + 100) then
  182.         return false
  183.     end
  184.  
  185.     if turtle.getFuelLevel() <= lengthDistance + math.abs(sideDistance) + 1 + extra then
  186.         --Find the slot with the least amount of coal
  187.         local slot = LeastAmountOfCoalSlot()
  188.         print("Best slot: ", slot)
  189.  
  190.         if slot ~= -1 then
  191.             turtle.select(slot)
  192.             turtle.refuel(1)
  193.             return true
  194.         else
  195.             return false
  196.         end
  197.     end
  198.     return true
  199. end
  200.  
  201. function ReturnHome()
  202.     if (facing == "forward") then
  203.         print("In a forward tunnel")
  204.         turtle.turnLeft()
  205.         turtle.turnLeft()
  206.  
  207.         while (lengthDistance % 3 ~= 0) do
  208.             if turtle.forward() then
  209.                 lengthDistance = lengthDistance - 1
  210.             end
  211.         end
  212.  
  213.         if (sideDistance > 0) then
  214.             turtle.turnLeft()
  215.             facing = "right"
  216.         end
  217.  
  218.         if (sideDistance < 0) then
  219.             turtle.turnRight()
  220.             facing = "left"
  221.         end
  222.     end
  223.  
  224.  
  225.     if (facing == "left" and sideDistance > 0) then
  226.         print("Backing from left side")
  227.         turtle.turnLeft()
  228.         turtle.turnLeft()
  229.         facing = "right"
  230.     end
  231.  
  232.     if (facing == "right" and sideDistance < 0) then
  233.         print("Backing from right side")
  234.         turtle.turnLeft()
  235.         turtle.turnLeft()
  236.         facing = "left"
  237.     end
  238.  
  239.     print("Getting back")
  240.  
  241.     while sideDistance ~= 0 do
  242.         print("Side distance: ", sideDistance)
  243.         --Dig
  244.  
  245.         while turtle.detect() do
  246.             turtle.dig()
  247.         end
  248.  
  249.         while turtle.detectUp() do
  250.             turtle.digUp()
  251.         end
  252.  
  253.  
  254.         if turtle.forward() then
  255.             if (facing == "left") then
  256.                 sideDistance = sideDistance + 1
  257.             else
  258.                 sideDistance = sideDistance - 1
  259.             end
  260.         end
  261.     end
  262.  
  263.  
  264.     if (facing == "left") then
  265.         print("Final turn")
  266.         turtle.turnLeft()
  267.     end
  268.     if (facing == "right") then
  269.         print("Final turn")
  270.         turtle.turnRight()
  271.     end
  272.  
  273.     print("Final stretch")
  274.     while lengthDistance > 0 do
  275.         print("Final distance: ", lengthDistance)
  276.         --Dig
  277.         if turtle.detect() then
  278.             turtle.dig()
  279.         end
  280.         if turtle.detectUp() then
  281.             turtle.digUp()
  282.         end
  283.  
  284.         if turtle.forward() then
  285.             lengthDistance = lengthDistance - 1
  286.         end
  287.     end
  288.  
  289.  
  290.  
  291.     print("I'm Home!")
  292.     returnCommandSent = false
  293. end
  294.  
  295. --Finds the slot with the least amount of coal. IF none is found return -1
  296. function LeastAmountOfCoalSlot()
  297.     local slot = -1
  298.     local topCount = 100
  299.     for i = 1, 16, 1 do
  300.         print("checking item: ", i)
  301.         local item = turtle.getItemDetail(i)
  302.         print("Item: ", item)
  303.         if item then
  304.             print("Is this coal? ", item.name)
  305.             print("Count:  ", item.count)
  306.             if ((item.name == "minecraft:coal" or item.name == "minecraft:charcoal" or item.name == "modern_industrialization:lignite_coal") and item.count < topCount) then
  307.                 print("yes")
  308.                 slot = i
  309.                 topCount = item.count
  310.             end
  311.         end
  312.     end
  313.     return slot
  314. end
  315.  
  316. --Finds the slot with the least amount of torch. IF none is found return -1
  317. function LeastAmountOfTorchSlot()
  318.     local slot = -1
  319.     local topCount = 100
  320.     for i = 1, 16, 1 do
  321.         print("checking item: ", i)
  322.         local item = turtle.getItemDetail(i)
  323.         print("Item: ", item)
  324.         if item then
  325.             print("Is this coal? ", item.name)
  326.             print("Count:  ", item.count)
  327.             if ((item.name == "minecraft:torch") and item.count < topCount) then
  328.                 print("yes")
  329.                 slot = i
  330.                 topCount = item.count
  331.             end
  332.         end
  333.     end
  334.     return slot
  335. end
  336.  
  337. --Checks if there is space for the given item
  338. function CanCollect()
  339.     local fullSlots = 0
  340.     for i = 1, 16, 1 do
  341.         local item = turtle.getItemDetail(i)
  342.         if item then
  343.             fullSlots = fullSlots + 1
  344.         end
  345.     end
  346.  
  347.     if fullSlots >= 15 then
  348.         print("I'm full. Will try to dispose")
  349.         Dispose()
  350.  
  351.         fullSlots = 0
  352.  
  353.         --Checks again if its full after dispose
  354.         for i = 1, 16, 1 do
  355.             local item = turtle.getItemDetail(i)
  356.             if item then
  357.                 fullSlots = fullSlots + 1
  358.             end
  359.         end
  360.     end
  361.  
  362.  
  363.  
  364.  
  365.  
  366.     if fullSlots >= 15 then
  367.         print("I'm full of good stuff")
  368.         Dispose()
  369.         returnCommandSent = true
  370.         return false
  371.     end
  372.     return true
  373. end
  374.  
  375. function Restock()
  376.     for i = 1, 16, 1 do
  377.         local item = turtle.getItemDetail(i)
  378.  
  379.         if item ~= nil then
  380.             --Makes sure its not touching the fuel or torch stack if they exist
  381.             if ((item.name == "minecraft:coal") or (item.name == "minecraft:torch") or (item.name == "minecraft:charcoal") or (item.name == "minecraft:soul_torch")) then
  382.  
  383.             else
  384.                 turtle.select(i)
  385.                 turtle.drop()
  386.             end
  387.         end
  388.     end
  389.     turtle.select(1)
  390.  
  391.     local torchStack = turtle.getItemDetail(2)
  392.     if torchStack ~= nil and (torchStack.name == "minecraft:torch" or torchStack.name == "minecraft:soul_torch") then
  393.         torchCount = turtle.getItemCount(2)
  394.     end
  395. end
  396.  
  397. function Dispose()
  398.     for i = 1, 16, 1 do
  399.         local item = turtle.getItemDetail(i)
  400.  
  401.         if item ~= nil then
  402.             if ((item.name == "minecraft:cobblestone") or (item.name == "minecraft:cobbled_deepslate") or (item.name == "minecraft:diorite") or (item.name == "minecraft:granite") or (item.name == "byg:soapstone") or (item.name == "minecraft:gravel") or (item.name == "minecraft:dirt") or (item.name == "minecraft:tuff") or (item.name == "minecraft:smooth_basalt") or (item.name == "ad_astra:moon_cobblestone") or (item.name == "minecraft:soul_soil") or (item.name == "minecraft:netherrack") or (item.name == "byg:scoria_cobblestone")) then
  403.                 turtle.select(i)
  404.                 turtle.drop()
  405.             end
  406.         end
  407.     end
  408.     turtle.select(1)
  409.  
  410.     local torchStack = turtle.getItemDetail(2)
  411.     if torchStack ~= nil and (torchStack.name == "minecraft:torch" or torchStack.name == "minecraft:soul_torch") then
  412.         torchCount = turtle.getItemCount(2)
  413.     end
  414. end
  415.  
  416. function TryPlaceTorch()
  417.  
  418. end
  419.  
  420.  
  421.  
  422.  
  423.  
  424. MainLoop()
  425.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement