How to upgrade the basic mining program for CC:Tweaked

CC: Tweaked is a mod for Minecraft that adds programmable computers and turtles to the game. Turtles are robots that can move around, dig, place blocks, and interact with the world using Lua scripts. In this tutorial, I will show you how to upgrade the basic turtle mining program using some simple functions and loops.

The code

The code for this tutorial is as follows:

local maxX,maxY,maxZ = ... -- the dimensions of the cuboid area

maxX = tonumber(maxX) -- convert the arguments to numbers
maxY = tonumber(maxY)
maxZ = tonumber(maxZ)

local function forward() -- a function to move forward and dig if needed
	local result = turtle.forward()
	if result == false then
		turtle.dig()
		turtle.forward()
	end
end

local function up() -- a function to move up and dig if needed
	local result = turtle.up()
	if result == false then
		turtle.digUp()
		turtle.up()
	end
end

y = 0 -- a variable to keep track of the current y level

local volume = maxX * maxY * maxZ -- the volume of the cuboid area

if turtle.getFuelLevel() > volume+20 then -- check if the turtle has enough fuel
    while y < maxY do -- loop until the maximum y level is reached
        for z = 2, maxZ do -- loop through the z axis
            for x = 2, maxX do -- loop through the x axis
                forward() -- move forward and dig
            end
            if z % 2 == 0 then -- check if z is even or odd
                turtle.turnRight() -- turn right at the end of the row
                forward() -- move forward and dig
                turtle.turnRight() -- turn right again to face the next row
            else
                turtle.turnLeft() -- turn left at the end of the row
                forward() -- move forward and dig
                turtle.turnLeft() -- turn left again to face the next row
            end
        end
        for x = 2, maxX do -- loop through the last row of the layer
            forward() -- move forward and dig
        end
        y = y+1 -- increment the y level by 1
        if y < maxY then -- check if the maximum y level is reached
            up() -- move up and dig
            turtle.turnRight() -- turn around to face the opposite direction
            turtle.turnRight()
        end
    end
    
    turtle.turnRight() -- turn around to face the original direction
    turtle.turnRight()
    y = maxY 
    for i = 2, maxY do -- loop down to return to the original y level
        turtle.down()
    end
    
    turtle.turnRight() -- turn right to return to the original x position
    for i = 2, maxZ do -- loop forward to return to the original z position
        turtle.forward()
    end
    turtle.turnRight() -- turn right again to face the original direction
else
    print("Please insert more fuel, required amount is: "..volume+20) -- print an error message if not enough fuel is present
end

The explanation

The code starts by taking three arguments from the command line: maxXmaxY, and maxZ. These are the dimensions of the cuboid area that we want to excavate. For example, if we want to dig a 5x5x5 area, we would run the script as excavate 5 5 5.

The arguments are then converted to numbers using the tonumber function. This is necessary because Lua treats all command line arguments as strings by default.

Next, we define two helper functions: forward and up. These functions make the turtle move forward or up by one block, and dig any block in front of or above it if needed. We use these functions instead of the built-in turtle.forward and turtle.up functions because they handle cases where there is a block in the way.

We also declare a variable y to keep track of the current y level of the turtle. We initialize it to zero.

Then, we calculate the volume of the cuboid area by multiplying maxXmaxY, and maxZ. We use this value to check if the turtle has enough fuel.

Leave a Reply

Your email address will not be published. Required fields are marked *