CC to do - Mining a hall

How can I make a turtle mine and move? That's the question I was asking myself when I started this project. That wasn't even the hardest part but it is very important.


In this post I show you how I got to the point where I can make the turtle mine 2 wide 4 high 13 long mining hall for me. A goal kind of difficult for someone with no experience but easy enough that I could still succeed.


The mining turtle busy doing half of the hall
(the file size made me shorten the gif sorry xd)


So, how did I do it? Well, I started out very basic. If you read my previous posts (which you might have to for a little context) I started out using the basic commands. I first wanted to make the turtle mine and move forward. That's not too hard. I can do that by writing this.

turtle.dig()

turtle.forward()


Ad nauseam but..... that's kind of a lot of repeating the same thing over and over again. There had to be a different way to do it right? To find this out I did what any student would do and ask the (sort of) all knowing language model ChatGPT how I could make it more efficient. 

ChatGPT taught me with all its grace about loops in lua. It showed me that by using this code:

for a = 1, 13 do

    turtle.dig()

    turtle.forward()

end


I could get a turtle to dig and move forward 13 times without having to write down 26 lines of just turtle.dig() and forward(). You can imagine my joy when I learned about this. This is basically what I used here the entire time but also what I will be using throughout the rest of the project. Thanks GPT :) 

Now that we learned about loops lets see what I want to let the turtle do. 

  • Dig once and move forward
  • Mine and move upwards 3 times
  • Go down 3 times
  • repeat the previous 3 points 13 times
  • Go back to its original starting position
  • Turn left once
  • Move forward once
  • Turn right once
  • repeat the other actions again 13 times
Should be doable. Let's do it one by one and then I'll also show the entire code at the end.


Dig once and move forward

Easy peasy.
turtle.dig()
turtle.foward()


Mine and move upwards 3 times

Alright let's use the loop here!

for a = 1, 3 do
    turtle.digUp()
  turtle.up()
end

Go down 3 times

I don't know if I can reuse letters in the loops so to be safe I use different letters every time. 

for b = 1, 3 do
    turtle.down()
end


Repeat this 13 times

Okay so we are going to add a loop over these loops so it does it 13 times. It will result in this

for c = 1, 13 do
    turtle.dig()
  turtle.forward()

    for a = 1, 3 do
        turtle.digUp()
        turtle.up()
      end
    for b = 1, 3 do
        turtle.down()
      end
end
Okay so now we are already 50% done with the mining hall!

Go back to its original starting position

Okay so the last loop ends with it going back down to ground level. This means all we have to do it go back 13 times. Easy enough.

for d = 1, 13 do
    turtle.back()
end


Orienting for the other 50% of the hall

Let's do orienting towards the second half in one go!

turtle.turnLeft()
turtle.forward()
turtle.turnRight()

Alright so that's done too. Now all we got to do is copy the loop before turtle.back() and change the lettes. This will result in code that looks something like this.


for c = 1, 13 do
    turtle.dig()
    turtle.forward()

    for a = 1, 3 do
        turtle.digUp()
        turtle.up()
    end

    for b = 1, 3 do
        turtle.down()
    end
end

for d = 1, 13 do
    turtle.back()
end
turtle.turnLeft()
turtle.forward()
turtle.turnRight()

for g = 1, 13 do
    turtle.dig()
    turtle.forward()

    for e = 1, 3 do
        turtle.digUp()
        turtle.up()
    end

    for f = 1, 3 do
        turtle.down()
    end
end

for h = 1, 13 do
    turtle.back()
end

And that's how it can mine a hallway for the windmill! Stay tuned for the rest. :) 

Comments

Popular posts from this blog

Design Guidelines. What, Why, How

Design principles, what and how.

I'm back!