In this blog, we will break down a Lua turtle program and rebuild it step by step to create an easy-to-understand version. The program uses a turtle in ComputerCraft (CC) to build a pattern of blocks in a specified size.
The original program is as follows:
lua
local maxX, maxY = ...
function right()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
function left()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end
maxX = tonumber(maxX)
maxY = tonumber(maxY)
turtle.select(1)
turtle.place()
for y = 1, maxY do
for x = 1, maxX do
if x < maxX then
if y % 2 == 1 then
right()
else
left()
end
turtle.place()
end
end
if y < maxY then
turtle.up()
end
turtle.place()
end
for y = maxY, 1, -1 do
if y > 1 then
turtle.down()
end
end
if maxY % 2 == 1 then
turtle.turnLeft()
for x = maxX, 1, -1 do
if x > 1 then
turtle.forward()
end
end
turtle.turnRight()
end
Step 1: Setting the Maximum Size
The program starts by defining two variables, maxX
and maxY
, which represent the maximum width and height of the pattern, respectively. These values are received as input parameters.
lua
local maxX, maxY = ...
Step 2: Defining Movement Functions
To simplify the code, we can extract the repetitive movement logic into separate functions. The right()
function turns the turtle to the right, moves it forward, and then turns it to the left. Similarly, the left()
function turns the turtle to the left, moves it forward, and then turns it to the right.
lua
function right()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
function left()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end
Step 3: Converting Input to Numbers
Next, the code converts the maxX
and maxY
variables from strings to numbers using the tonumber()
function.
lua
maxX = tonumber(maxX)
maxY = tonumber(maxY)
Step 4: Placing the First Block
The turtle is then selected and instructed to place a block using the turtle.select(1)
and turtle.place()
functions. This sets the block type in the turtle’s inventory slot 1 and places a block in front of it.
lua
turtle.select(1)
turtle.place()
Step 5: Building the Pattern
The program uses nested loops to build the pattern. The outer loop iterates over the y
variable from 1 to maxY
, and the inner loop iterates over the x
variable from 1 to maxX
.
lua
for y = 1, maxY do
for x = 1, maxX do
-- Code to be added in the next steps
end
-- Code to be added in the next steps
end
Step 6: Alternating Movement and Block Placement
Within the inner loop, there is a conditional statement that checks if x
is less than maxX
. If true, it determines whether the turtle should turn right or left based on the value of y
using the modulus operator %
.
lua
if x < maxX then
if y % 2 == 1 then
right()
else
left()
end
turtle.place()
end
Step 7: Moving Up and Placing a Block
After each row is completed, the program checks if y
is less than maxY
. If true, it moves the turtle up and places a block at the new level using the turtle.up()
and turtle.place()
functions.
lua
if y < maxY then
turtle.up()
end
turtle.place()
Step 8: Moving Down to the Starting Level
Once all the rows are completed, the program uses a loop to move the turtle back down to the starting level. It iterates over the y
variable in reverse from maxY
to 1 and moves the turtle down with the turtle.down()
function.
lua
for y = maxY, 1, -1 do
if y > 1 then
turtle.down()
end
end
Step 9: Handling Odd Number of Rows
If the number of rows (maxY
) is odd, the turtle needs to move to the left side of the pattern after reaching the bottom-right corner. The program achieves this by turning the turtle left, moving it backward to the starting column, and then turning it right again.
lua
if maxY % 2 == 1 then
turtle.turnLeft()
for x = maxX, 1, -1 do
if x > 1 then
turtle.forward()
end
end
turtle.turnRight()
end