Advertisement
Justin_P69

Turtle mining lua

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