Advertisement
Fusion1227

Turtle Miner

Sep 29th, 2023 (edited)
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. ---- Introduction
  2. -- This is a Lua script written for the Mining Turtle, which is included in a mod ComputerCraft (www.computercraft.info) created for Minecraft (minecraft.net).
  3. -- The script makes the turtle mine shafts in parallel lines until it runs out of fuel.
  4. -- Uploaded to (and explained): github.com/Franco22hu/MiningTurtle
  5. -- Version: 2.4
  6.  
  7.  
  8. ---- Constants
  9. local SHAFT_LENGTH = 20 -- The length of a shaft mined
  10. local GO_LEFT = false -- Create shafts to the left or right
  11. local START_SHAFT = 0 -- Which shaft to begin from
  12. local BLOCKS_BETWEEN = 2 -- How far apart shafts should be
  13.  
  14.  
  15. ---- Functions
  16. local function tryGoForward()
  17.     while not turtle.forward() do
  18.         if turtle.detect() then
  19.             turtle.dig()
  20.         elseif turtle.attack() then
  21.         else sleep( 0.5 )
  22.         end
  23.     end
  24. end
  25. local function tryGoUp()
  26.     while not turtle.up() do
  27.         if turtle.detectUp() then
  28.             turtle.digUp()
  29.         elseif turtle.attackUp() then
  30.         else sleep( 0.5 )
  31.         end
  32.     end
  33. end
  34. local function tryGoDown()
  35.     while not turtle.down() do
  36.         if turtle.detectDown() then
  37.             turtle.digDown()
  38.         elseif turtle.attackDown() then
  39.         else sleep( 0.5 )
  40.         end
  41.     end
  42. end
  43. local function tryDigUp()
  44.     while turtle.detectUp() do
  45.         turtle.digUp()
  46.     end
  47. end
  48. local function unload()
  49.     for n=1,16 do
  50.         local nCount = turtle.getItemCount(n)
  51.         if nCount > 0 then
  52.             turtle.select(n)
  53.             turtle.dropDown()
  54.         end
  55.     end
  56.     turtle.select(1)
  57. end
  58.  
  59.  
  60. ---- Main
  61. print("Input start shaft number. Ignore to start at the first shaft.")
  62. local shaftsMined = tonumber(read()) or 0
  63. -- local shaftsMined = START_SHAFT
  64.  
  65. while true do
  66.    
  67.     -- Calculate fuel needed
  68.     local fuelNeeded = (shaftsMined * (BLOCKS_BETWEEN+1) * 2) + (SHAFT_LENGTH * 4) + (2 * 6)
  69.     --   (getting to the shaft) + (through the shaft) + (ups and downs)
  70.    
  71.     -- Refuel
  72.     print("Refueling. Fuel needed: ", fuelNeeded)
  73.     if turtle.getFuelLevel() < fuelNeeded then
  74.         turtle.turnLeft()
  75.         turtle.turnLeft()
  76.         while turtle.getFuelLevel() < fuelNeeded do
  77.             if not turtle.suck(1) then
  78.                 print("Not enough fuel detected! Stopping")
  79.                 turtle.turnRight()
  80.                 turtle.turnRight()
  81.                 return
  82.             end
  83.             turtle.refuel()
  84.         end
  85.         turtle.turnRight()
  86.         turtle.turnRight()
  87.     end
  88.  
  89.     -- Go to shaft
  90.     print("Starting next shaft: ", shaftsMined)
  91.     if shaftsMined > 0 then
  92.         if GO_LEFT then turtle.turnLeft()
  93.         else turtle.turnRight() end
  94.  
  95.         for n=1,(shaftsMined*(BLOCKS_BETWEEN+1)) do
  96.             tryGoForward()
  97.             tryDigUp()
  98.         end
  99.  
  100.         if GO_LEFT then turtle.turnRight()
  101.         else turtle.turnLeft() end
  102.     end
  103.  
  104.     -- Dig shaft
  105.     for n=1,2 do
  106.        
  107.         for n=1,SHAFT_LENGTH do
  108.             tryGoForward()
  109.             tryDigUp()
  110.         end
  111.  
  112.         turtle.turnLeft()
  113.         turtle.turnLeft()
  114.  
  115.         tryGoUp()
  116.         tryGoUp()
  117.  
  118.         for n=1,SHAFT_LENGTH do
  119.             tryDigUp()
  120.             tryGoForward()
  121.         end
  122.  
  123.         turtle.turnRight()
  124.         turtle.turnRight()
  125.        
  126.         if n ~= 2 then
  127.             tryGoUp()
  128.             tryGoUp()
  129.         end
  130.     end
  131.  
  132.     for n=1,6 do
  133.         tryGoDown()
  134.     end
  135.  
  136.     -- Go to chests
  137.     if shaftsMined > 0 then
  138.         if GO_LEFT then turtle.turnRight()
  139.         else turtle.turnLeft() end
  140.        
  141.         for n=1,(shaftsMined*(BLOCKS_BETWEEN+1)) do
  142.             tryGoForward()
  143.         end
  144.        
  145.         if GO_LEFT then turtle.turnLeft()
  146.         else turtle.turnRight() end
  147.     end
  148.  
  149.     -- Finish
  150.     print("Finished. Unloading")
  151.     unload()
  152.     shaftsMined = shaftsMined + 1
  153.  
  154.     -- Repeat
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement