Advertisement
t4ggno

CC:Tweaked - Miner

Mar 13th, 2021 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  81.      -- Move up if height > 2
  82.      if height > 2 then
  83.         if turtle.detectUp() then
  84.             turtle.up()
  85.         else
  86.             turtle.digUp()
  87.             turtle.up()
  88.         end
  89.     end
  90.  
  91.     -- Detect start height
  92.     if height > 2 then
  93.         currentHeight = 2
  94.     else
  95.         currentHeight = 1
  96.     end
  97.  
  98.     while true do
  99.  
  100.         print("Mining height " .. currentHeight .. " of " .. height)
  101.  
  102.         for j = 1, width do
  103.  
  104.             print("Mining width " .. j .. " of " .. width)
  105.  
  106.             for k = 1, depth - 1 do
  107.  
  108.                 print("Mining depth " .. k .. " of " .. depth)
  109.  
  110.                 -- Dig below if heigh > 1 and there is a block below
  111.                 if currentHeight > 1 and turtle.detectDown() then
  112.                     turtle.digDown()
  113.                 end
  114.  
  115.                 -- Dig above if height is i - 1 and there is a block above
  116.                 if height > 1 and currentHeight == height - 1 and turtle.detectUp() then
  117.                     turtle.digUp()
  118.                 end
  119.  
  120.                 -- Dig infront of the turtle
  121.                 if turtle.detect() then
  122.                     turtle.dig()
  123.                 end
  124.  
  125.                 turtle.forward()
  126.             end
  127.  
  128.             -- Dig below if heigh > 1 and there is a block below
  129.             if currentHeight > 1 and turtle.detectDown() then
  130.                 turtle.digDown()
  131.             end
  132.  
  133.             -- Dig above if currentHeight is less than height - 1 and there is a block above
  134.             if height > 1 and currentHeight < height and turtle.detectUp() then
  135.                 turtle.digUp()
  136.             end
  137.  
  138.             -- Turn around
  139.             if j % 2 == 1 then -- Odd = Start to end, ebven = end to start
  140.  
  141.                 if direction == DIRECTION.left then
  142.                     turtle.turnLeft()
  143.                 else
  144.                     turtle.turnRight()
  145.                 end
  146.  
  147.                 -- Dig infront of the turtle
  148.                 if turtle.detect() then
  149.                     turtle.dig()
  150.                 end
  151.  
  152.                 turtle.forward()
  153.                 if direction == DIRECTION.left then
  154.                     turtle.turnLeft()
  155.                 else
  156.                     turtle.turnRight()
  157.                 end
  158.             else
  159.                 if direction == DIRECTION.left then
  160.                     turtle.turnRight()
  161.                 else
  162.                     turtle.turnLeft()
  163.                 end
  164.  
  165.                 -- Dig infront of the turtle
  166.                 if turtle.detect() then
  167.                     turtle.dig()
  168.                 end
  169.                 turtle.forward()
  170.                 if direction == DIRECTION.left then
  171.                     turtle.turnRight()
  172.                 else
  173.                     turtle.turnLeft()
  174.                 end
  175.             end
  176.         end
  177.  
  178.         -- Move up if height not reached
  179.         if currentHeight < height - 1 then
  180.  
  181.             -- Dectect how often the turtle should move up
  182.             local moveUp;
  183.             if(height - currentHeight) > 2 then
  184.                 moveUp = 3
  185.             else
  186.                 moveUp = height - currentHeight - 1
  187.             end
  188.  
  189.             -- Move up
  190.             for i = 1, moveUp do
  191.                 if turtle.detectUp() == false then
  192.                     turtle.up()
  193.                 else
  194.                     turtle.digUp()
  195.                     turtle.up()
  196.                 end
  197.                 currentHeight = currentHeight + 1
  198.             end
  199.         else
  200.             break
  201.         end
  202.     end
  203. end
  204.  
  205. -- Create the main function
  206. function Main()
  207.  
  208.     -- Check direction
  209.     turtle.turnLeft()
  210.     if turtle.detect() then
  211.         direction = DIRECTION.right
  212.     else
  213.         direction = DIRECTION.left
  214.     end
  215.     turtle.turnRight()
  216.  
  217.     -- Measure depth if not set
  218.     if depth == nil or depth < 1 then
  219.  
  220.         print("Depth not set. Measuring depth...")
  221.         depth = 1
  222.        
  223.         -- Move forward until we hit a wall
  224.         while turtle.detect() == false do
  225.             turtle.forward()
  226.             depth = depth + 1
  227.         end
  228.  
  229.         -- Turn around and move back
  230.         if depth > 1 then
  231.             turtle.turnLeft()
  232.             turtle.turnLeft()
  233.             for i = 1, depth do
  234.                 turtle.forward()
  235.             end
  236.             turtle.turnLeft()
  237.             turtle.turnLeft()
  238.         end
  239.  
  240.         print("Depth: " .. depth)
  241.     end
  242.  
  243.     -- Measure width if not set
  244.     if width == nil or width < 1 then
  245.  
  246.         print("Width not set. Measuring width...")
  247.        
  248.         -- Rotate to "direction"
  249.         if direction == DIRECTION.left then
  250.             turtle.turnLeft()
  251.         else
  252.             turtle.turnRight()
  253.         end
  254.  
  255.         width = 1
  256.  
  257.         -- Move forward until we hit a wall
  258.         while turtle.detect() == false do
  259.             turtle.forward()
  260.             width = width + 1
  261.         end
  262.  
  263.         -- Turn around and move back
  264.         turtle.turnLeft()
  265.         turtle.turnLeft()
  266.         for i = 1, width do
  267.             turtle.forward()
  268.         end
  269.         if direction == DIRECTION.left then
  270.             turtle.turnLeft()
  271.         else
  272.             turtle.turnRight()
  273.         end
  274.  
  275.         print("Width: " .. width)
  276.     end
  277.  
  278.     -- Measure height if not set
  279.     if height == nil or height < 1 then
  280.  
  281.         print("Height not set. Measuring height...")
  282.         height = 1
  283.  
  284.         -- Move up until we hit a wall
  285.         while turtle.detectUp() == false do
  286.             turtle.up()
  287.             height = height + 1
  288.         end
  289.  
  290.         -- Move down
  291.         for i = 1, height do
  292.             turtle.down()
  293.         end
  294.  
  295.         print("Height: " .. height)
  296.     end
  297.  
  298.     print("Starting...")
  299.     if turtle.getFuelLevel() < 100 then
  300.         Refill()
  301.     end
  302.     Mine()
  303. end
  304.  
  305. -- Run the main function
  306. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement