Advertisement
Justin_P69

Advanced Mining Turtle Program

Nov 7th, 2024 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 KB | None | 0 0
  1. -- Author: Justin_P69
  2. -- Date: 11/07/2024
  3. -- Desc: Adjusted turtle mining program
  4.  
  5.  
  6. ---- Configs ----
  7.  
  8. local numSlots = 16
  9. local heading = "north"
  10. local widthX = 5
  11. local heightY = 2
  12. local depthZ = 5
  13.  
  14. -- List of all unwanted items to drop
  15. local droppedItems = {
  16.     "minecraft:stone",
  17.     "minecraft:dirt",
  18.     "minecraft:gravel",
  19.     "minecraft:flint",
  20.     "minecraft:sand",
  21.     "minecraft:cobblestone",
  22.     "minecraft:redstone",
  23.     "minecraft:dye",
  24.     "thaumcraft:nugget",
  25.     "thaumcraft:crystal_essence",
  26.     "thaumcraft:ore_cinnabar",
  27.     "thermalfoundation:material",
  28.     "railcraft:ore_metal",
  29.     "extrautils2:ingredients",
  30.     "projectred-core:resource_item",
  31.     "deepresonance:resonating_ore",
  32.     "forestry:apatite"
  33. }
  34.  
  35. -- Assigning dimensions to the area to be mined
  36. if(#arg == 3) then
  37.     widthX = tonumber(arg[1], 10)
  38.     heightY = tonumber(arg[2], 10)
  39.     depthZ = tonumber(arg[3], 10)
  40. end
  41.  
  42. ---- Main functions ----
  43.  
  44. -- Function to drop the items listed in "droppedItems"
  45. function DropItems()
  46.     for slot = 1, numSlots, 1 do
  47.         local item = turtle.getItemDetail(getEnderSlot)
  48.         if(item ~= nil) then
  49.             for droppedItemsIndex = 1, #droppedItems, 1 do
  50.                 if(item["name"] == droppedItems[droppedItemsIndex]) then
  51.                     turtle.select(DropItems)
  52.                     turtle.dropDown()
  53.                 end
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. -- Function to get the inventory slot containg an ender chest
  60. function GetEnderSlot()
  61.     for slot = 1, numSlots, 1 do
  62.         local item = turtle.getItemDetail(GetEnderSlot)
  63.         if(item ~= nil) then
  64.             if(item["name"] == "enderstorage:ender_storage") then
  65.                 return slot
  66.             end
  67.         end
  68.     end
  69.     return nil
  70. end
  71.  
  72. -- Function to place an ender chest and place items not included in droppedItems in the chest
  73. function ManageInventory()
  74.     DropItems()
  75.     enderSlot = GetEnderSlot()
  76.  
  77.     -- If ender chest is present in inventory: dig the block above and place ender chest
  78.     if(enderslot ~= nil) then
  79.         turtle.select(enderSlot)
  80.         turtle.digUp()
  81.         turtle.placeUp()
  82.     end
  83.  
  84.     -- Put all items except ender chests, and coal in ender chest
  85.     for slot = 1, numSlots, 1 do
  86.         local item = turtle.getItemDetail(slot)
  87.         if(item ~= nil) then
  88.             if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "enderstorage:ender_storage") then
  89.                 turtle.select(slot)
  90.                 turtle.dropUp()
  91.             end
  92.         end
  93.     end
  94.     turtle.digUp()
  95. end
  96.  
  97. -- Function to check fuel level and refuel if necessary
  98. function CheckFuel()
  99.     turtle.select(1)
  100.     if(turtle.getFuelLevel() < 50) then
  101.         print("Attempting to refuel...")
  102.         for slot = 1, numSlots, 1 do
  103.             turtle.select(slot)
  104.             if(turtle.refuel(1)) then
  105.                 return true
  106.             end
  107.         end
  108.         return false
  109.     else
  110.         return true
  111.     end
  112. end
  113.  
  114. -- Function to detect blocks around the turtle and mine them
  115. function DetectAndDig()
  116.     while(turtle.detectUp() or turtle.detectDown() or turtle.detect()) do
  117.         turtle.dig()
  118.         turtle.digUp()
  119.         turtle.digDown()
  120.     end
  121. end
  122.  
  123.  
  124. function Forward()
  125.     DetectAndDig()
  126.     turtle.forward()
  127. end
  128.  
  129. function RightHalfTurn()
  130.     turtle.turnRight()
  131.     DetectAndDig()
  132.     turtle.forward()
  133.     turtle.turnRight()
  134.     DetectAndDig()
  135. end
  136.  
  137. function LeftHalfTurn()
  138.     turtle.turnLeft()
  139.     DetectAndDig()
  140.     turtle.forward()
  141.     turtle.turnLeft()
  142.     DetectAndDig()
  143. end
  144.  
  145. -- Function to tell the turtle which direction it is facing
  146. function InvertDirection()
  147.     if(heading == "north") then
  148.         heading = "south"
  149.     elseif(heading == "south") then
  150.         heading = "north"
  151.     elseif(heading == "west") then
  152.         heading = "east"
  153.     elseif(heading == "east") then
  154.         heading = "west"
  155.     end
  156. end
  157.  
  158.  
  159. function TurnAround(level)
  160.     if(level % 2 == 1) then
  161.         if(heading == "north" or heading == "west") then
  162.             LeftHalfTurn()
  163.         elseif(heading == "south" or heading == "east") then
  164.             RightHalfTurn()
  165.         end
  166.     else
  167.         if(heading == "north" or heading == "west") then
  168.             RightHalfTurn()
  169.         elseif(heading == "south" or heading == "east") then
  170.             LeftHalfTurn()
  171.         end
  172.     end
  173.     InvertDirection()
  174. end
  175.  
  176. -- Function to go up to the next level to mine
  177. function Up3Levels()
  178.     turtle.turnRight()
  179.     turtle.turnRight()
  180.     InvertDirection()
  181.     turtle.digDown()
  182.     turtle.digUp()
  183.     turtle.up()
  184.     turtle.digUp()
  185.     turtle.up()
  186.     turtle.digUp()
  187.     turtle.up()
  188. end
  189.  
  190.  
  191. function Main()
  192.     for level = 1, heightY, 1 do
  193.         for x = 1, widthX, 1 do
  194.             for z = 1, depthZ, 1 do
  195.                 if(not CheckFuel()) then
  196.                     print("Turtle is out of fuel.")
  197.                     return
  198.                 end
  199.                 Forward()
  200.                 print(string.format("X: %d      Z: %d", x, z))
  201.             end
  202.             if(x ~= widthX) then
  203.                 TurnAround(level)
  204.             end
  205.             ManageInventory()
  206.         end
  207.         Up3Levels()
  208.     end
  209. end
  210.  
  211.  
  212. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement