Advertisement
Justin_P69

Turtle Mining 2

Aug 20th, 2023 (edited)
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | Gaming | 0 0
  1. local numSlots = 16
  2. local HDG = "north"
  3. local width = 5
  4. local depth = 5
  5. local height = 2
  6.  
  7.  
  8. if(#arg == 3) then
  9.     width = tonumber(arg[1])
  10.     depth = tonumber(arg[2])
  11.     height = tonumber(arg[3])
  12. else
  13.     print("Digging 5x5x2")
  14. end
  15.  
  16.  
  17. Dropped_Items = {
  18.     "minecraft:stone",
  19.     "minecraft:dirt",
  20.     "minecraft:gravel",
  21.     "minecraft:flint",
  22.     "minecraft:sand",
  23.     "minecraft:cobblestone",
  24.     "minecraft:redstone",
  25.     "minecraft:dye",
  26.     "thaumcraft:nugget",
  27.     "thaumcraft:crystal_essence",
  28.     "thaumcraft:ore_cinnabar",
  29.     "thermalfoundation:material",
  30.     "railcraft:ore_metal",
  31.     "extrautils2:ingredients",
  32.     "projectred-core:resource_item",
  33.     "deepresonance:resonating_ore",
  34.     "forestry:apatite"
  35. }
  36.  
  37.  
  38. -- Function for dropping unwanted items
  39.  
  40. function dropItems()
  41.     for slot = 1, numSlots, 1 do
  42.         local item = turtle.getItemDetail(slot)
  43.         if(item ~= nil) then
  44.             for droppedItemsIndex = 1, #Dropped_Items, 1 do
  45.                 if(item["name"] == Dropped_Items[droppedItemsIndex]) then
  46.                     turtle.select(item)
  47.                     turtle.dropDown()
  48.                 end
  49.             end
  50.         end
  51.     end
  52. end
  53.  
  54.  
  55. --Function to find ender chest in the inventory
  56.  
  57. function getEnderSlot()
  58.     for slot = 1, numSlots, 1 do
  59.         local item = turtle.getItemDetail(slot)
  60.         if(item ~= nil) then
  61.             if(item["name"] == "enderstorage:ender_storage") then
  62.                 return slot
  63.             end
  64.         end
  65.     end
  66.     return nil
  67. end
  68.  
  69.  
  70. --Function to drop items after each row
  71.  
  72. function manageInventory()
  73.     dropItems()
  74.     enderSlot = getEnderSlot()
  75.     if(enderSlot ~= nil) then
  76.        turtle.select(enderSlot)
  77.         turtle.digUp()
  78.         turtle.placeUp()
  79.     end
  80. --Ender chest has been placed
  81.     for slot = 1, numSlots, 1 do
  82.         local item = turtle.getItemDetail(slot)
  83.         if(item ~= nil) then
  84.             if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "enderstorage:ender_storage") then
  85.                 turtle.select(slot)
  86.                 turtle.dropUp()
  87.             end
  88.         end
  89.     end
  90. --Wanted items have been stored
  91.     turtle.digUp()
  92. end
  93.  
  94.  
  95. --Checks fuel level
  96. function checkFuel()
  97.     if(turtle.getFuelLevel() < 50) then
  98.         print("Attempting to refuel")
  99.         for slot = 1, numSlots, 1 do
  100.             turtle.select(slot)
  101.             if(turtle.refuel(1)) then
  102.                 return true
  103.             end
  104.         end
  105.     return false
  106.     else
  107.         return true
  108.     end
  109. end
  110.  
  111.  
  112. function detectAndDig()
  113.     while(turtle.detectUp() or turtle.detectDown() or turtle.detect()) do
  114.         turtle.dig()
  115.         turtle.digUp()
  116.         turtle.digDown()
  117.     end
  118. end
  119.  
  120.  
  121. function forward()
  122.     detectAndDig()
  123.     turtle.forward()
  124. end
  125.  
  126.  
  127. function leftHalfTurn()
  128.     turtle.turnLeft()
  129.     detectAndDig()
  130.     turtle.forward()
  131.     turtle.turnLeft()
  132.     detectAndDig()
  133. end
  134.  
  135.  
  136. function rightHalfTurn()
  137.     turtle.turnRight()
  138.     detectAndDig()
  139.     turtle.forward()
  140.     turtle.turnRight()
  141.     detectAndDig()
  142. end
  143.  
  144.  
  145. function flipDirection()
  146.     if(HDG == "north") then
  147.         HDG = "south"
  148.     elseif(HDG == "south") then
  149.         HDG = "north"
  150.     elseif(HDG == "west") then
  151.         HDG = "east"
  152.     elseif(HDG == "east") then
  153.         HDG = "west"
  154.     end  
  155. end
  156.  
  157.  
  158. function turnAround(level)
  159.     if(level % 2 == 1) then
  160.         if(HDG == "north" or HDG == "west") then
  161.             leftHalfTurn()
  162.         elseif(HDG == "south" or HDG == "east") then
  163.             rightHalfTurn()
  164.         end
  165.     else
  166.         if(HDG == "north" or HDG == "west") then
  167.             rightHalfTurn()
  168.         elseif(HDG == "south" or HDG == "east") then
  169.             leftHalfTurn()
  170.         end
  171.     end
  172.     flipDirection()
  173. end
  174.  
  175.  
  176.  function upLevelX3()
  177.     turtle.turnRight()
  178.     turtle.turnRight()
  179.     flipDirection()
  180.     turtle.digUp()
  181.     turtle.up()
  182.     turtle.digUp()
  183.     turtle.up()
  184.     turtle.digUp()
  185.     turtle.up()
  186. end
  187.  
  188.  
  189. function main()
  190.     for level = 1, height, 1 do
  191.         for X = 1, width, 1 do
  192.             for Y = 1, depth, 1 do
  193.                 if(not checkFuel()) then
  194.                     print("Turtle is out of fuel.")
  195.                     return
  196.                 end
  197.                 forward()
  198.                 print(string.format("X: %d      Y: %d", X, Y))
  199.             end
  200.             if(X ~= width) then
  201.                 turnAround(level)
  202.             end
  203.             manageInventory()
  204.         end
  205.         upLevelX3()
  206.     end
  207. end
  208.  
  209.  
  210. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement