originalceebs

CCTurtleMining

Dec 21st, 2020 (edited)
4,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1. --[[
  2.  
  3. Script Created by CEEBS
  4. YouTube: https://www.youtube.com/c/OriginalCEEBS
  5. Twitter: https://twitter.com/OnlyCeebs
  6.  
  7. Really appreciate all the support you have given me, so thank you!
  8.  
  9. -----------------------------------------------------------------
  10.  
  11. I'll be interested to see any modifications made, so please feel free to let me know what you've come up with. Happy coding, happy gaming. Peace!
  12.  
  13. One little issue with the checkFuelLevel and refuel function. The script may refuse to run after refueling, just run the script again and it'll be fine. Please feel free to troubleshoot, not a difficult fix, will just require some conditional logic.
  14.  
  15. ]]--
  16.  
  17. -- Receive arguments and perform some basic validation
  18. local height = 0
  19. local width = 0
  20. local depth = 0
  21.  
  22. if #arg == 3 then
  23.     height = tonumber(arg[1])
  24.     width = tonumber(arg[2])
  25.     depth = tonumber(arg[3])
  26.  
  27.     if width % 2 == 0 or height % 2 == 0 then
  28.         print("Both the height and width arguments must be an odd number")
  29.         return
  30.     elseif width == 1 or height == 1 then
  31.         print("Both the height and width arguments must be greater and 1")
  32.         return
  33.     end
  34. else
  35.     print("Please enter the correct arguments when executing this script. The height width and depth are required (e.g. mining 5 5 10)")
  36.     return
  37. end
  38.  
  39. local INVENTORY_SIZE = 16
  40. local heightMovement = math.floor(height / 2)
  41. local widthMovement = math.floor(width / 2)
  42.  
  43. -- List of accepted fuels
  44. local ACCEPTED_FUELS = {
  45.     "minecraft:coal_block",
  46.     "minecraft:coal"
  47. }
  48.  
  49. -- List of whitelisted items
  50. local ACCEPTED_ITEMS = {
  51.     "minecraft:coal",
  52.     "minecraft:iron_ore",
  53.     "minecraft:gold_ore",
  54.     "minecraft:raw_iron",
  55.     "minecraft:raw_gold",
  56.     "minecraft:raw_copper",
  57.     "minecraft:redstone",
  58.     "minecraft:diamond",
  59.     "minecraft:emerald",
  60.     "minecraft:dye",
  61.     "minecraft:lapis_lazuli",
  62.     "thermalfoundation:ore",
  63.     "appliedenergistics2:material",
  64.     "tconstruct:ore",
  65. }
  66.  
  67. -- Perform inventory check
  68. function inventoryCheck()
  69.     -- Check for rubbish items
  70.     for i = 1, INVENTORY_SIZE do
  71.         local currentItem = turtle.getItemDetail(i)
  72.         if currentItem ~= nil then
  73.             local isAcceptedItem = false
  74.             for x = 1, #ACCEPTED_ITEMS do
  75.                 if currentItem.name == ACCEPTED_ITEMS[x] then
  76.                     isAcceptedItem = true
  77.                 end
  78.             end
  79.             if not isAcceptedItem then
  80.                 turtle.select(i)
  81.                 turtle.dropUp()
  82.             end
  83.         end
  84.     end
  85.  
  86.     -- Group items together
  87.     for j = 1, INVENTORY_SIZE do
  88.         local currentItem = turtle.getItemDetail(j)
  89.  
  90.         if currentItem ~= nil then
  91.             turtle.select(j)
  92.             for k = j, INVENTORY_SIZE do
  93.                 if turtle.compareTo(k) then
  94.                     turtle.select(k)
  95.                     turtle.transferTo(j)
  96.                     turtle.select(j)
  97.                 end
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103. -- Refuel using the found fuel
  104. function refuel(slot_number)
  105.     print("[TURTLE] Refueling... ")
  106.     turtle.select(slot_number)
  107.     turtle.refuel()
  108.     print("[TURTLE] Nom. Nom. Nom.")
  109. end
  110.  
  111. -- Check the current fuel level
  112. function checkFuelLevel()
  113.     local requiredFuelLevel = math.ceil((height * width * depth) + (heightMovement + widthMovement))
  114.     local currentFuelLevel = turtle.getFuelLevel()
  115.  
  116.     print("[TURTLE] Current fuel level is: "..currentFuelLevel.." - Required: "..requiredFuelLevel)
  117.  
  118.     if currentFuelLevel < requiredFuelLevel then
  119.  
  120.         print("[TURTLE] Attempting to locate fuel.")
  121.  
  122.         for i = 1, INVENTORY_SIZE do
  123.             local currentItem = turtle.getItemDetail(i)
  124.             if currentItem ~= nil then
  125.                 for x = 1, #ACCEPTED_FUELS do
  126.                     if currentItem.name == ACCEPTED_FUELS[x] then
  127.                         print("[TURTLE] Acceptable fuel found: " ..ACCEPTED_FUELS[x])
  128.  
  129.                         if currentFuelLevel < requiredFuelLevel then
  130.                             refuel(i)
  131.                         else
  132.                             return true
  133.                         end
  134.                     end
  135.                 end
  136.             end
  137.         end
  138.         print("[TURTLE] No acceptable fuel or not enough found, terminating program...")
  139.         return false
  140.     else
  141.         return true
  142.     end
  143. end
  144.  
  145. -- Combat gravel/sand
  146. function moveUpAndDig()
  147.     while turtle.up() == false do
  148.         turtle.digUp()
  149.     end
  150. end
  151.  
  152. function moveForwardAndDig()
  153.     while turtle.forward() == false do
  154.         turtle.dig()
  155.     end
  156. end
  157.  
  158. function moveDownAndDig()
  159.     while turtle.down() == false do
  160.         turtle.digDown()
  161.     end
  162. end
  163.  
  164. -- Move to start position
  165. function moveToStartPosition()
  166.    
  167.     -- Move to horizontal start position
  168.     turtle.turnLeft()
  169.     for i = 1, widthMovement do
  170.         moveForwardAndDig()
  171.     end
  172.     turtle.turnRight()
  173.  
  174.     -- Move to vertical start postion
  175.     for i = 1, heightMovement do
  176.         moveUpAndDig()
  177.     end
  178.  
  179. end
  180.  
  181. -- Mining Sequence
  182. function mineSequence()
  183.  
  184.     moveToStartPosition()
  185.  
  186.     for x = 1, depth do
  187.    
  188.         moveForwardAndDig()
  189.  
  190.         for i = 1, height do
  191.  
  192.             if x % 2 == 0 then
  193.                 turtle.turnLeft()
  194.             else
  195.                 turtle.turnRight()
  196.             end
  197.  
  198.             for y = 1, width - 1 do
  199.                 moveForwardAndDig()
  200.             end
  201.  
  202.             if i ~= height then
  203.                 if x % 2 == 0 then
  204.                     turtle.turnLeft()
  205.                     moveUpAndDig()
  206.                 else
  207.                     turtle.turnRight()
  208.                     moveDownAndDig()
  209.                 end
  210.             end
  211.  
  212.         end
  213.  
  214.         if x % 2 == 0 then
  215.             turtle.turnRight()
  216.         else
  217.             turtle.turnLeft()
  218.         end
  219.  
  220.         inventoryCheck()
  221.  
  222.     end
  223.  
  224. end
  225.  
  226. if checkFuelLevel() then
  227.     mineSequence()
  228. end
Add Comment
Please, Sign In to add comment