For fun I wanted to level an area, and I specifically wanted to fill in annoying holes and messy open caves. I decided to divide this into separate problems. First is having the turtle cover a rectangular area. Second is to fill in the holes below each block in the area.

I decided to create a function which takes a Lua table like the following:

The size is the width and length of the area. Forward, left and right are for moving the turtle and callback is the function to call for each point in the rectangle. This way the function is decoupled from turtle specifics and what I actually want to do.

Move in a rectangle

Struggled embarrassingly long with this (so seems this was a useful exercise for my programming brain), but figured it out in the end

Please let me know if you have a better way to do it. Either way, now the callbacks handed to this function.

Moving the turtle

As you saw, the function requires three callbacks for moving. For turning left and right we can just use the turtle API directly. However, since I wanted to level an area, we need to do some digging if we hit any obstructions.

This function will keep trying to move forward and whenever it fails it will wait for a short while and then try to dig in front of itself. This will usually only run once, but if you’re standing in its way it will keep trying until you move. This way you being in the way won’t mess it up

Filling the hole

First of all I wanted to fill up whatever hole is below me. I also wanted to replace dirt with stone and have a single layer of dirt on top. This is what I ended up with.

-- Fill holefunction fill()-- How far down we have gonelocal depth =0

-- Make sure we have an empty slot for picking up junk-- This is an attempt to not mess up the convention by-- the blocks we dig that are picked up. Not 100%-- reliable still...
turtle.select(9)
turtle.drop()

-- Move down to first block we can findwhilenot turtle.detectDown()and turtle.down()do
depth = depth +1end

-- As long as we have something to placeif selectSource(depth)>0then-- Place it below us
turtle.placeDown()endendend

-- Returns the count of an appropriate slotfunction selectSource(depth)-- If we're at the topif depth==1then-- Select slot 1 and return its count
turtle.select(1)return turtle.getItemCount(1)-- Otherwiseelse-- Move through the rest of the slotsfor i=2,9do
turtle.select(i)local count = turtle.getItemCount(i)-- Until we find one with something in itif count >0thenreturn countendendendreturn0end

Works pretty well, but any advice on improvements are more than welcome

Running it

Create a file on a Mining Turtle called for example “fill” and add the above functions together with the following snippet of code: