hornedcommando

Minecraft Turtle megaMine

Apr 16th, 2024 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.95 KB | Gaming | 0 0
  1. --v1.0
  2. --A "Swiss Cheese" Turtle Mining Program that leaves the environment relatively undamaged by skipping junk and dangerous blocks
  3. --by hornedcommando
  4.  
  5. --Known issues:
  6. --Turtle is not optimized to keep only one slot occupied with fuel, also if chests are before the fuel slot it will burn chests for fuel
  7. --Runtime can be very long, and if you log off / chunk is unloaded the turtle will be at the bottom of a deep deep hole
  8. --Sometimes (somehow) the turtle returns to the surface without cobblestone, and doesn't cover the hole, probably could just have it use junk rather than hard coded cobblestone
  9. --Could dig up at Surface + New Mineshaft to make the trail human walkable
  10. --Torches are spread far enough that spawns will happen in the mine.
  11. --Turtles (STILL) will just go back to mining after returning home
  12.  
  13. --Import Relative Location Libraries (run this on the turtle: pastebin get KFa0XQqf TrackingTurtle )
  14. local tTurtle = require("TrackingTurtle")
  15.  
  16. local myTurtle = tTurtle.create()
  17.  
  18. --Global Variables
  19. --Start Location (so the turtle can return home
  20. startx, starty, startz = myTurtle.getPosition()
  21. --Starting face
  22. startFace = myTurtle.getFacing()
  23. --Max distance (to prevent the turtle from getting lost)
  24. MAX_DISTANCE = 150
  25. --Flag to check if lootchest needs to be placed
  26. isJunkShuffle = false
  27. -- Function to check if a block is forbidden
  28. local function isForbiddenBlock(blockName)
  29.     local forbiddenBlocks = {
  30.         "forbidden_arcanus:stella_arcanum",
  31.         "minecraft:bedrock"
  32.     }
  33.     for _, forbiddenBlock in ipairs(forbiddenBlocks) do
  34.         if blockName == forbiddenBlock then
  35.             return true
  36.         end
  37.     end
  38.     return false
  39. end
  40. --Function to help the turtle navigate to given coordinates (mostly home for now)
  41. function navigate(x, y, z, face)
  42.     local currentX, currentY, currentZ = myTurtle.getPosition()
  43.     local currentFace = myTurtle.getFacing()
  44.     local xDiff = x - currentX
  45.     local yDiff = y - currentY
  46.     local zDiff = z - currentZ
  47.     local faceDiff = face - currentFace
  48.     print("I'm at: " .. currentX .. ", " .. currentY .. ", " .. currentZ .. " facing: " .. currentFace)
  49.     if xDiff > 0 then
  50.         print("I'm west of target, heading east")
  51.         myTurtle.face(1)
  52.         for i = 1, xDiff do
  53.             myTurtle.forward()
  54.         end
  55.     elseif xDiff < 0 then
  56.         print("I'm east of target, heading west")
  57.         myTurtle.face(3)
  58.         for i = 1, math.abs(xDiff) do
  59.             myTurtle.forward()
  60.         end
  61.     end
  62.     if yDiff > 0 then
  63.         print("I'm below target, heading up")
  64.         for i = 1, yDiff do
  65.             myTurtle.up()
  66.         end
  67.     elseif yDiff < 0 then
  68.         print("I'm above target, heading down")
  69.         for i = 1, math.abs(yDiff) do
  70.             myTurtle.down()
  71.         end
  72.     end
  73.     if zDiff > 0 then
  74.         print("I'm north of target, heading south")
  75.         myTurtle.face(2)
  76.         for i = 1, zDiff do
  77.             myTurtle.forward()
  78.         end
  79.     elseif zDiff < 0 then
  80.         print("I'm south of target, heading north")
  81.         myTurtle.face(0)
  82.         for i = 1, math.abs(zDiff) do
  83.             myTurtle.forward()
  84.         end
  85.     end
  86.     myTurtle.face(face)
  87.     print("I'm home!")
  88.     os.shutdown()
  89. end
  90.  
  91.  
  92.  
  93. --Strong up, checks block, digs, then goes up
  94. function gu()
  95.     print("Going up")
  96.     local success, block = turtle.inspectUp()
  97.     if isForbiddenBlock(block.name) then
  98.         print("Detected block is forbidden")
  99.     else
  100.         while not myTurtle.up() do
  101.             print("Digging up")
  102.             turtle.digUp()
  103.         end
  104.     end
  105. end
  106. --Strong down, checks block, digs, then goes down
  107. function gd()
  108.     print("Going down")
  109.     local success, block = turtle.inspectDown()
  110.     if isForbiddenBlock(block.name) then
  111.         print("Detected block is forbidden")
  112.         return false
  113.     else
  114.         while not myTurtle.down() do
  115.             print("Digging down")
  116.             turtle.digDown()
  117.         end
  118.         return true
  119.     end
  120. end
  121. --Strong forward, checks block, digs, then goes forward
  122. function gf()
  123.     print("Going forward")
  124.     local success, block = turtle.inspect()
  125.     if isForbiddenBlock(block.name) then
  126.         print("Detected block is forbidden")
  127.         return false
  128.     else
  129.         while not myTurtle.forward() do
  130.             print("Digging forward")
  131.             turtle.dig()
  132.         end
  133.     end
  134. end
  135. --Check if any slot is empty
  136. local function inventoryFull()
  137.     print("Checking inventory")
  138.     for slot = 1, 16 do
  139.         local count = turtle.getItemCount(slot)
  140.         if turtle.getItemCount(slot) == 0 then
  141.             print("Slot: " .. slot .. " is empty. Inventory is not full.")
  142.             return false  -- If any slot is empty, return false
  143.         end
  144.     end
  145.     return true  -- If all slots are occupied, return true
  146. end
  147. -- Function to search the inventory for an item
  148. local function searchInventory(name)
  149.     for slot = 1, 16 do
  150.         turtle.select(slot)
  151.         local slotDetail = turtle.getItemDetail()
  152.         if slotDetail and slotDetail.name:find(name) then
  153.             return true, slot
  154.             -- Return the slot number if item found
  155.         end
  156.     end
  157.     return false -- Return false if item not found
  158. end
  159. -- Dictionary of items which the turtle will drop
  160. local junk = {
  161.     "minecraft:dirt",
  162.     "minecraft:cobblestone",
  163.     "create:dolomite_cobblestone",
  164.     "create:limestone_cobblestone",
  165.     "create:gabbro_cobblestone",
  166.     "create:granite_cobblestone",
  167.     "forbidden_arcanus:darkstone",
  168.     "extcaves:sedimentstone",
  169.     "extcaves:lavastone",
  170.     "extcaves:pebble_sedimentstone",
  171.     "create:andesite_cobblestone",
  172.     "create:diorite_cobblestone",
  173.     "minecraft:gravel",
  174.     "occultism:datura_seeds",
  175.     "minecraft:wheat_seeds",
  176.     "extcaves:pebble_stone",
  177.     "minecraft:stone",
  178.     "minecraft:granite",
  179.     "minecraft:diorite",
  180.     "minecraft:andesite",
  181.     "minecraft:gravel",
  182.     "minecraft:deepslate",
  183.     "railcraft:quarried_stone",
  184.     "minecraft:cobbled_deepslate",
  185.     "minecraft:tuff",
  186.     "minecraft:basalt",
  187.     "minecraft:blackstone",
  188.     "minecraft:calcite",
  189.     "minecraft:smooth_basalt",
  190.     "minecraft:sand",
  191.     "minecraft:gravel",
  192.     "minecraft:deepslate",
  193.     "minecraft:cobbled_deepslate",
  194.     "darkerdepths:cobbled_sandstone",
  195.     "minecraft:sandstone"
  196.  
  197.     -- Add more junk items as needed
  198. }
  199. --Function to check if a block is junk
  200. local function isJunk(blockName)
  201.     for _, junkBlock in ipairs(junk) do
  202.         if blockName == junkBlock then
  203.             return true
  204.         end
  205.     end
  206.     return false
  207. end
  208. --Function to drop junk from inventory
  209. local function dropJunk()
  210.     print("Dropping junk")
  211.     for slot = 1, 16 do
  212.         turtle.select(slot)
  213.         local itemDetail = turtle.getItemDetail()
  214.         if itemDetail and isJunk(itemDetail.name) then
  215.             turtle.drop()
  216.         end
  217.     end
  218. end
  219. --Because the turtle digs down, there is a tendency, as inventory fills up, for good items to fall on top of the turtle.  If the turtle detects it is full it will attempt to drop junk, and pickup loot
  220. local function junkShuffle()
  221.     print("Shuffling junk")
  222.     myTurtle.face(0)
  223.     local success, block = turtle.inspect()
  224.     if isForbiddenBlock(block.name) then
  225.         print("Detected block is forbidden")
  226.     else
  227.         turtle.dig()
  228.         dropJunk()
  229.         while turtle.suckUp() do
  230.             turtle.suckUp()
  231.             dropJunk()
  232.         end
  233.         isJunkShuffle = true
  234.     end
  235. end
  236. --Function to refuel the turtle, attempts to maintain a specified fuel level, this could be adjusted based on abundance of fuel or time between fuel calls
  237. local function smartRefuel()
  238.     print("Checking Fuel")
  239.     -- Check if fuel level is below 1000
  240.     while turtle.getFuelLevel() < 1000 do
  241.         print("Refueling")
  242.         for slot = 1, 16 do
  243.             turtle.select(slot) -- Select the slot
  244.             if turtle.refuel(0) then
  245.                 -- Check if the selected item is a fuel
  246.                 turtle.refuel(1) -- Refuel with the selected item
  247.                 break -- Stop searching for fuel after refueling
  248.             end
  249.         end
  250.     end
  251. end
  252. --If a Junk Shuffle has been necessary, the turtle will place a chest and torch down to offload extra items at the top of the mineshaft
  253. function lootChest()
  254.     print("storing goodies")
  255.     dropJunk()
  256.     myTurtle.face(1)
  257.     local success, block = turtle.inspect()
  258.     if isForbiddenBlock(block.name) then
  259.         print("Detected block is forbidden")
  260.     else
  261.         turtle.dig()
  262.         searchInventory("minecraft:chest")
  263.         if turtle.place() then
  264.             for slot = 1, 16 do
  265.                 turtle.select(slot)
  266.                 local itemdetail = turtle.getItemDetail()
  267.                 if itemdetail and itemdetail.name ~= "minecraft:chest" and itemdetail.name ~= "thermal:charcoal_block" and itemdetail.name ~= "minecraft:torch" and itemdetail.name ~= "minecraft:coal" then
  268.                     turtle.drop()
  269.                     isJunkShuffle = false
  270.                 end
  271.             end
  272.         else
  273.             print("No chest found, going home")
  274.             navigate(startx, starty, startz, startFace)
  275.             return nil
  276.         end
  277.     end
  278.     myTurtle.face(3)
  279.     if isForbiddenBlock(block.name) then
  280.         print("Detected block is forbidden")
  281.     else
  282.         turtle.dig()
  283.         searchInventory("minecraft:torch")
  284.         turtle.place()
  285.     end
  286.     myTurtle.face(0)
  287.     newMineshaft()
  288. end
  289.  
  290. -- Function to create a new mineshaft
  291. function newMineshaft()
  292.     print("Creating new mineshaft")
  293.     local currentX, currentY, currentZ = myTurtle.getPosition()
  294.     local distanceX = math.abs(currentX)
  295.     local distanceY = math.abs(currentY)
  296.     local distanceZ = math.abs(currentZ)
  297.     if distanceX > MAX_DISTANCE or distanceZ > MAX_DISTANCE then
  298.         print("I'm far away, heading home")
  299.         navigate(startx, starty, startz, startFace)
  300.         return nil
  301.     end
  302.     if isJunkShuffle == true then
  303.         lootChest()
  304.     end
  305.     for _ = 1, 3 do
  306.         local success, block = turtle.inspectUp()
  307.         if isForbiddenBlock(block.name) then
  308.             print("Detected block is forbidden")
  309.         else
  310.             turtle.digUp()
  311.         end
  312.         gf()
  313.     end
  314.     mine()
  315. end
  316.  
  317. function riseToSurface()
  318.     local currentX, currentY, currentZ = myTurtle.getPosition()
  319.     print("surfacing")
  320.     -- Loop until the turtle reaches Y=0
  321.     while currentY < 0 do
  322.         gu()
  323.         -- Update the current position
  324.         currentX, currentY, currentZ = myTurtle.getPosition()
  325.     end
  326.     -- find cobblestone in the turtle's inventory
  327.     -- Loop through all inventory slots
  328.     for slot = 1, 16 do
  329.         -- Select the current slot
  330.         turtle.select(slot)
  331.         -- Get information about the item in the selected slot
  332.         local itemDetail = turtle.getItemDetail()
  333.         -- Check if an item is present in the selected slot and it is cobblestone
  334.         if itemDetail and itemDetail.name == "minecraft:cobblestone" then
  335.             turtle.placeDown()
  336.         end
  337.     end
  338.     -- Cobblestone not found in the inventory, return nil
  339.     smartRefuel()
  340.     dropJunk()
  341.         myTurtle.face(startFace)
  342.         newMineshaft()
  343. end
  344.  
  345. -- Function to mine
  346. function mine()
  347.     print("Mining")
  348.     gd()
  349.     print("polite hole dug")
  350.     while true do
  351.         if inventoryFull() then
  352.             junkShuffle()
  353.         end
  354.         if not gd() then
  355.             print("can not descend, returning to surface")
  356.             riseToSurface()
  357.             break
  358.         else
  359.             for i = 1, 4 do
  360.                 local success, block = turtle.inspect()
  361.                 if isForbiddenBlock(block.name) or isJunk(block.name) then
  362.                     myTurtle.turnRight()
  363.                 else
  364.                     turtle.dig()
  365.                     myTurtle.turnRight()
  366.                 end
  367.             end
  368.         end
  369.     end
  370. end
  371.  
  372.  
  373. smartRefuel()
  374. mine() -- Call the mine function to start mining
  375.  
Add Comment
Please, Sign In to add comment