xX_Nobody_Xx

Mining turtle Miner

Feb 7th, 2021 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. -- Using the program
  2. local height = 0
  3. local width = 0
  4. local depth = 0
  5.  
  6. if #arg == 3 then
  7.     height = tonumber(arg[1])
  8.     width = tonumber(arg[2])
  9.     depth = tonumber(arg[3])
  10.  
  11.     if width % 2 == 0 or height % 2 == 0 then
  12.         print("The Width and Height need to be odd numbers")
  13.         return
  14.     elseif width == 1 or height == 1 then
  15.         print("The Width and Height need to be more that one")
  16.         return
  17.     end
  18. else
  19.     print("There needs to be three correct arguments (e.g. mine 5 5 10)")
  20.     return
  21. end
  22.  
  23. -- useful Vars
  24. local INVENTORY_SIZE = 16
  25. local mineHeight = math.floor(height / 2)
  26. local mineWidth = math.floor(width / 2)
  27.  
  28. -- List of fuels
  29. local FUELS = {
  30.     "minecraft:coal_block",
  31.     "minecraft:coal",
  32.     "minecraft:lava_bucket"
  33. }
  34.  
  35. -- Whitelist for inventory
  36. local WHITELISTED_ITEMS = {
  37.     "minecraft:coal",
  38.     "minecraft:coal_block",
  39.     "minecraft:redstone",
  40.     "minecraft:dye",
  41.     "minecraft:emerald",
  42.     "minecraft:gold_ore",
  43.     "minecraft:iron_ore",
  44.     "minecraft:diamond",
  45.     "minecraft:lava_bucket",
  46.     "thermalfoundation:ore",
  47.     "bigreactors:oreyellorite",
  48.     "tconstruct:ore",
  49.     "ic2:nuclear",
  50.     "draconicevolution:draconium_dust"
  51. }
  52.  
  53. -- Checks Inventory
  54. function InventoryCheck()
  55.     -- Checks for shit items then drops them if bad
  56.     for i = 1, INVENTORY_SIZE do
  57.         local currentItem = turtle.getItemDetail(i)
  58.         if currentItem ~= nil then
  59.             local isAcceptedItem = false
  60.             for x = 1, #WHITELISTED_ITEMS do
  61.                 if currentItem.name == WHITELISTED_ITEMS[x] then
  62.                     isAcceptedItem = true
  63.                 end
  64.             end
  65.             if not isAcceptedItem then
  66.                 turtle.select(i)
  67.                 turtle.dropUp()
  68.             end
  69.         end
  70.     end
  71.  
  72.     -- keeps stuff in stacks
  73.     for j = 1, INVENTORY_SIZE do
  74.         local selectedSlot = turtle.getItemDetail(j)
  75.  
  76.         if selectedSlot ~= nil then
  77.            turtle.select(j)
  78.            for k = j, INVENTORY_SIZE do
  79.                if turtle.compareTo(k) then
  80.                     turtle.select(k)
  81.                     turtle.transferTo(j)
  82.                     turtle.select(j)
  83.                end
  84.            end
  85.         end
  86.     end
  87. end
  88.  
  89. -- Refuel using the found fuel
  90. function refuel(slot_number)
  91.     print("[TURTLE] Refueling... ")
  92.     turtle.select(slot_number)
  93.     turtle.refuel()
  94.     print("[TURTLE] Nom. Nom. Nom.")
  95. end
  96.  
  97. -- Check the current fuel level
  98. function checkFuelLevel()
  99.     local requiredFuelLevel = math.ceil((height * width * depth) + (mineHeight + mineWidth))
  100.     local currentFuelLevel = turtle.getFuelLevel()
  101.  
  102.     print("[TURTLE] Current fuel level is: "..currentFuelLevel.." - Required: "..requiredFuelLevel)
  103.  
  104.     if currentFuelLevel < requiredFuelLevel then
  105.  
  106.         print("[TURTLE] Attempting to locate fuel.")
  107.  
  108.         for i = 1, INVENTORY_SIZE do
  109.             local currentItem = turtle.getItemDetail(i)
  110.             if currentItem ~= nil then
  111.                 for x = 1, #FUELS do
  112.                     if currentItem.name == FUELS[x] then
  113.                         print("[TURTLE] Acceptable fuel found: " ..FUELS[x])
  114.  
  115.                         if currentFuelLevel < requiredFuelLevel then
  116.                             refuel(i)
  117.                         else
  118.                             return true
  119.                         end
  120.                     end
  121.                 end
  122.             end
  123.         end
  124.         print("[TURTLE] No acceptable fuel or not enough found, terminating program...")
  125.         return false
  126.     else
  127.         return true
  128.     end
  129. end
  130.  
  131. -- Combat gravel/sand
  132. function moveUpAndDig()
  133.     while turtle.up() == false do
  134.         turtle.digUp()
  135.     end
  136. end
  137.  
  138. function moveForwardAndDig()
  139.     while turtle.forward() == false do
  140.         turtle.dig()
  141.         InventoryCheck()
  142.     end
  143. end
  144.  
  145. function moveDownAndDig()
  146.     while turtle.down() == false do
  147.         turtle.digDown()
  148.     end
  149. end
  150.  
  151. -- Move to start position
  152. function moveToStartPosition()
  153.    
  154.     -- Move to horizontal start position
  155.     turtle.turnLeft()
  156.     for i = 1, mineWidth do
  157.         moveForwardAndDig()
  158.     end
  159.     turtle.turnRight()
  160.  
  161.     -- Move to vertical start postion
  162.     for i = 1, mineHeight do
  163.         moveUpAndDig()
  164.     end
  165.  
  166. end
  167.  
  168. -- Mining Sequence
  169. function mineSequence()
  170.  
  171.     moveToStartPosition()
  172.  
  173.     for x = 1, depth do
  174.    
  175.         moveForwardAndDig()
  176.  
  177.         for i = 1, height do
  178.  
  179.             if x % 2 == 0 then
  180.                 turtle.turnLeft()
  181.             else
  182.                 turtle.turnRight()
  183.             end
  184.  
  185.             for y = 1, width - 1 do
  186.                 moveForwardAndDig()
  187.             end
  188.  
  189.             if i ~= height then
  190.                 if x % 2 == 0 then
  191.                     turtle.turnLeft()
  192.                     moveUpAndDig()
  193.                 else
  194.                     turtle.turnRight()
  195.                     moveDownAndDig()
  196.                 end
  197.             end
  198.  
  199.         end
  200.  
  201.         if x % 2 == 0 then
  202.             turtle.turnRight()
  203.         else
  204.             turtle.turnLeft()
  205.         end
  206.  
  207.         InventoryCheck()
  208.  
  209.     end
  210.  
  211. end
  212.  
  213. if checkFuelLevel() then
  214.     mineSequence()
  215. end
  216.  
  217.  
  218.  
Add Comment
Please, Sign In to add comment