Advertisement
t4ggno

CC:Tweaked - Miner

Sep 2nd, 2022 (edited)
1,943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | Source Code | 0 0
  1. -- Predefine enum
  2. local DIRECTION = {
  3.     left = 1,
  4.     right = 2
  5. }
  6.  
  7. -- Predefine variables
  8. local version = "0.1"
  9. local accepted_fuels = {"minecraft:coal_block", "minecraft:coal", "minecraft:charcoal"}
  10. local inventory_size = 16
  11. local check_inventory_every = 20
  12. local depth = nil
  13. local width = nil
  14. local height = nil
  15. local currentDepth = 1
  16. local currentWidth = 1
  17. local currentHeight = 1
  18. local direction;
  19.  
  20. -- Read arguments
  21. local args = {...}
  22.  
  23. -- Check arguments. Possible arguments are:
  24. -- -h, --help: Print this help message
  25. -- -v, --version: Print version information
  26. -- --depth <depth>: Set the depth of the square
  27. -- --width <width>: Set the width of the square
  28. -- --height <height>: Set the height of the square
  29. for i = 1, #args do
  30.     if args[i] == "-h" or args[i] == "--help" then
  31.         print("Usage: mine_tutrle_square.lua [OPTIONS]")
  32.         print("Options:")
  33.         print("-h, --help: Print this help message")
  34.         print("-v, --version: Print version information")
  35.         print("--depth <depth>: Set the depth of the square")
  36.         print("--width <width>: Set the width of the square")
  37.         print("--height <height>: Set the height of the square")
  38.         return
  39.     elseif args[i] == "-v" or args[i] == "--version" then
  40.         print("mine_tutrle_square.lua " .. version)
  41.         return
  42.     elseif args[i] == "--depth" and args[i + 1] then
  43.         depth = tonumber(args[i + 1])
  44.         if not depth then
  45.             print("Error: Invalid depth")
  46.             return
  47.         end
  48.     elseif args[i] == "--width" and args[i + 1] then
  49.         width = tonumber(args[i + 1])
  50.         if not width then
  51.             print("Error: Invalid width")
  52.             return
  53.         end
  54.     elseif args[i] == "--height" and args[i + 1] then
  55.         height = tonumber(args[i + 1])
  56.         if not height then
  57.             print("Error: Invalid height")
  58.             return
  59.         end
  60.     end
  61. end
  62.  
  63. -- Create the refill function
  64. function Refill()
  65.     print("Refueling...")
  66.     for i = 1, inventory_size do
  67.         for j = 1, #accepted_fuels do
  68.             if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == accepted_fuels[j] then
  69.                 turtle.select(i)
  70.                 turtle.refuel()
  71.                 break
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. -- Create the mining function
  78. function Mine()
  79.     print("Mining...")
  80.     for i = 1, height do
  81.  
  82.         print("Mining height " .. i .. " of " .. height)
  83.  
  84.         -- Move up if height > 1
  85.         if height > 1 then
  86.             if turtle.detectUp() then
  87.                 turtle.up()
  88.             else
  89.                 turtle.digUp()
  90.                 turtle.up()
  91.             end
  92.             i = i + 1
  93.             print("Mining layer " .. i .. " of " .. height)
  94.         end
  95.  
  96.         for j = 1, width do
  97.             print("Mining width " .. j .. " of " .. width)
  98.             for k = 1, depth do
  99.                 print("Mining depth " .. k .. " of " .. depth)
  100.  
  101.                 -- Dig below if heigh > 1 and there is a block below
  102.                 if height > 1 and turtle.detectDown() then
  103.                     turtle.digDown()
  104.                 end
  105.  
  106.                 -- Dig above if height is i - 1 and there is a block above
  107.                 if height > 1 and i == height - 1 and turtle.detectUp() then
  108.                     turtle.digUp()
  109.                 end
  110.  
  111.                 -- Dig infront of the turtle
  112.                 if turtle.detect() then
  113.                     turtle.dig()
  114.                 end
  115.  
  116.                 turtle.forward()
  117.             end
  118.  
  119.             -- Turn around
  120.             if j % 2 == 1 then -- Odd = Start to end, ebven = end to start
  121.                 if direction == DIRECTION.left then
  122.                     turtle.turnLeft()
  123.                 else
  124.                     turtle.turnRight()
  125.                 end
  126.  
  127.                 if turtle.detect() then
  128.                     turtle.dig()
  129.                 end
  130.                 turtle.forward()
  131.                 if direction == DIRECTION.left then
  132.                     turtle.turnLeft()
  133.                 else
  134.                     turtle.turnRight()
  135.                 end
  136.             else
  137.                 if direction == DIRECTION.left then
  138.                     turtle.turnRight()
  139.                 else
  140.                     turtle.turnLeft()
  141.                 end
  142.  
  143.                 if turtle.detect() then
  144.                     turtle.dig()
  145.                 end
  146.                 turtle.forward()
  147.                 if direction == DIRECTION.left then
  148.                     turtle.turnRight()
  149.                 else
  150.                     turtle.turnLeft()
  151.                 end
  152.             end
  153.         end
  154.     end
  155. end
  156.  
  157. -- Create the main function
  158. function Main()
  159.  
  160.     -- Check direction
  161.     turtle.turnLeft()
  162.     if turtle.detect() then
  163.         direction = DIRECTION.right
  164.     else
  165.         direction = DIRECTION.left
  166.     end
  167.     turtle.turnRight()
  168.  
  169.     -- Measure depth if not set
  170.     if depth == nil or depth < 1 then
  171.  
  172.         print("Depth not set. Measuring depth...")
  173.         depth = 1
  174.        
  175.         -- Move forward until we hit a wall
  176.         while turtle.detect() == false do
  177.             turtle.forward()
  178.             depth = depth + 1
  179.         end
  180.  
  181.         -- Turn around and move back
  182.         turtle.turnLeft()
  183.         turtle.turnLeft()
  184.         for i = 1, depth do
  185.             turtle.forward()
  186.         end
  187.         turtle.turnLeft()
  188.         turtle.turnLeft()
  189.  
  190.         print("Depth: " .. depth)
  191.     end
  192.  
  193.     -- Measure width if not set
  194.     if width == nil or width < 1 then
  195.  
  196.         print("Width not set. Measuring width...")
  197.        
  198.         -- Rotate to "direction"
  199.         if direction == DIRECTION.left then
  200.             turtle.turnLeft()
  201.         else
  202.             turtle.turnRight()
  203.         end
  204.  
  205.         width = 1
  206.  
  207.         -- Move forward until we hit a wall
  208.         while turtle.detect() == false do
  209.             turtle.forward()
  210.             width = width + 1
  211.         end
  212.  
  213.         -- Turn around and move back
  214.         turtle.turnLeft()
  215.         turtle.turnLeft()
  216.         for i = 1, width do
  217.             turtle.forward()
  218.         end
  219.         if direction == DIRECTION.left then
  220.             turtle.turnLeft()
  221.         else
  222.             turtle.turnRight()
  223.         end
  224.  
  225.         print("Width: " .. width)
  226.     end
  227.  
  228.     -- Measure height if not set
  229.     if height == nil or height < 1 then
  230.  
  231.         print("Height not set. Measuring height...")
  232.         height = 1
  233.  
  234.         -- Move up until we hit a wall
  235.         while turtle.detectUp() == false do
  236.             turtle.up()
  237.             height = height + 1
  238.         end
  239.  
  240.         -- Move down
  241.         for i = 1, height do
  242.             turtle.down()
  243.         end
  244.  
  245.         print("Height: " .. height)
  246.     end
  247.  
  248.     print("Starting...")
  249.     while true do
  250.         if turtle.getFuelLevel() < 100 then
  251.             Refill()
  252.         end
  253.         Mine()
  254.     end
  255. end
  256.  
  257. -- Run the main function
  258. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement