Advertisement
kreezxil

5 by 5 shaft mining 30 depth

Sep 22nd, 2024 (edited)
116
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.96 KB | Gaming | 0 0
  1. -- ComputerCraft Mining Turtle Script
  2. -- Mines a 5x5 grid of shafts, each 30 blocks deep
  3. -- Harvests non-standard blocks, refuels with coal, and manages inventory
  4.  
  5. -- Configuration
  6. local shaftDepth = 30
  7. local gridSize = 5
  8. local moveBetweenShafts = 2
  9. local chestLocation = "back" -- Direction where the chest is placed relative to starting position
  10.  
  11. -- Block Types to Ignore
  12. local ignoredBlocks = {
  13.     ["minecraft:stone"] = true,
  14.     ["minecraft:granite"] = true,
  15.     ["minecraft:diorite"] = true,
  16.     ["minecraft:andesite"] = true,
  17.     ["minecraft:chest"] = true,
  18.     ["minecraft:gravel"] = true,  -- Added gravel to ignored blocks
  19.     ["minecraft:sand"] = true,    -- Added sand to ignored blocks
  20.     ["minecraft:dirt"] = true     -- Added dirt to ignored blocks
  21. }
  22.  
  23. -- Initialize variables
  24. local currentShaft = 1
  25. local totalShafts = gridSize * gridSize
  26. local startX, startY, startZ = 0, 0, 0 -- Starting coordinates (can be extended if needed)
  27. local miningPosition = {x = 0, y = 0, z = 0}
  28.  
  29. -- Utility Functions
  30.  
  31. -- Function to turn right
  32. local function turnRight()
  33.     turtle.turnRight()
  34. end
  35.  
  36. -- Function to turn left
  37. local function turnLeft()
  38.     turtle.turnLeft()
  39. end
  40.  
  41. -- Function to move forward with digging if needed
  42. local function moveForward()
  43.     while not turtle.forward() do
  44.         turtle.dig()
  45.         sleep(0.5)
  46.     end
  47. end
  48.  
  49. -- Function to move backward with digging if needed
  50. local function moveBackward()
  51.     while not turtle.back() do
  52.         turtle.dig()
  53.         sleep(0.5)
  54.     end
  55. end
  56.  
  57. -- Function to move up with digging if needed
  58. local function moveUp()
  59.     while not turtle.up() do
  60.         turtle.digUp()
  61.         sleep(0.5)
  62.     end
  63. end
  64.  
  65. -- Function to move down with digging if needed
  66. local function moveDown()
  67.     while not turtle.down() do
  68.         turtle.digDown()
  69.         sleep(0.5)
  70.     end
  71. end
  72.  
  73. -- Function to inspect and harvest blocks around the turtle
  74. local function harvestSurroundings()
  75.     for i = 1, 4 do
  76.         turtle.turnRight()
  77.         local success, data = turtle.inspect()
  78.         if success then
  79.             local blockName = data.name
  80.             if not ignoredBlocks[blockName] then
  81.                 turtle.dig()
  82.                 sleep(0.5)
  83.                 -- Check if the block was coal
  84.                 if blockName:find("coal") then
  85.                     turtle.select(1) -- Assuming slot 1 is always coal
  86.                     if not turtle.refuel(1) then
  87.                         -- If can't refuel, keep the coal in inventory
  88.                         -- Implement logic if needed
  89.                     end
  90.                 end
  91.             end
  92.         end
  93.     end
  94.     -- Return to original orientation
  95.     for i = 1, 4 do
  96.         turtle.turnLeft()
  97.     end
  98. end
  99.  
  100. -- Function to check if inventory is full
  101. local function isInventoryFull()
  102.     for i = 1, 16 do
  103.         if turtle.getItemCount(i) == 0 then
  104.             return false
  105.         end
  106.     end
  107.     return true
  108. end
  109.  
  110. -- Function to return to the starting position of a shaft
  111. local function returnToStart(depth)
  112.     for i = 1, depth do
  113.         moveUp()
  114.     end
  115. end
  116.  
  117. -- Function to deposit items into chest
  118. local function depositItems()
  119.     -- Save current position and orientation
  120.     local originalDirection = 0 -- 0: forward, 1: right, 2: back, 3: left
  121.     -- Adjust based on current orientation if needed
  122.  
  123.     -- Move to chest
  124.     turtle.turnRight()
  125.     moveForward()
  126.     -- Deposit all items except the first slot (coal)
  127.     for i = 2, 16 do
  128.         turtle.select(i)
  129.         if turtle.getItemCount(i) > 0 then
  130.             turtle.drop()
  131.         end
  132.     end
  133.     -- Return to mining position
  134.     moveBackward()
  135.     turtle.turnLeft()
  136. end
  137.  
  138. -- Function to mine a single shaft
  139. local function mineShaft()
  140.     -- Mine down
  141.     for i = 1, shaftDepth do
  142.         if isInventoryFull() then
  143.             depositItems()
  144.         end
  145.         harvestSurroundings()
  146.         moveDown()
  147.     end
  148.  
  149.     -- Mine back up
  150.     returnToStart(shaftDepth)
  151. end
  152.  
  153. -- Function to navigate the grid
  154. local function navigateGrid(shaftNumber)
  155.     if shaftNumber % gridSize ~= 0 then
  156.         -- Move to next shaft in the row
  157.         for i = 1, moveBetweenShafts do
  158.             moveForward()
  159.         end
  160.     else
  161.         -- Move to next row
  162.         if shaftNumber < totalShafts then
  163.             if math.floor((shaftNumber - 1) / gridSize) % 2 == 0 then
  164.                 -- Even row: turn right, move forward, turn right
  165.                 turnRight()
  166.                 moveForward()
  167.                 turnRight()
  168.             else
  169.                 -- Odd row: turn left, move forward, turn left
  170.                 turnLeft()
  171.                 moveForward()
  172.                 turnLeft()
  173.             end
  174.         end
  175.     end
  176. end
  177.  
  178. -- Main Mining Loop
  179. for shaft = 1, totalShafts do
  180.     mineShaft()
  181.     navigateGrid(shaft)
  182. end
  183.  
  184. -- After mining all shafts, return to starting position
  185. -- Implement any additional logic if needed
  186.  
  187. print("Mining operation completed.")
  188.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement