Jump to content




Tunnel Miner With Distance Input [Tutorial] | For N00bs

turtle game api

23 replies to this topic

#1 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 23 December 2012 - 11:17 PM

This is my 3rd post in these forums, and i'd like to say some of the feedback i've been getting is good. The whole idea to these (probably repetitive) programs is to help beginners to understand Lua and Computer Craft turtles. I generally explain quite in depth and I hope people are enjoying/using what i'm trying to give.

Download at bottom of page!!

I would firstly like to thank

Ulthean

for helping clear up the code.

The program is a simple 2 X 1 tunnel digger with user input and block placing/torch placing. I left it simple because it's for beginners!

Now onto the code!

print("How far?")
dist = tonumber(read())
n = 1

for i = 1, dist do
  while turtle.detect() do
  turtle.dig()
  sleep(0.25)
end
  turtle.forward()
  turtle.digUp()
  n = n + 1
  if not turtle.detectDown() then
		turtle.select(2)
		turtle.placeDown()
  end
  if n == 7 then
		turtle.turnLeft()
		turtle.dig()
		turtle.select(1)
		turtle.place()
		turtle.turnRight()
		n = 1
  end
end

FIRSTLY, we state that if the turtle detects a block below, do the following:

The explanation for this one is fairly straight forward.
Firstly the program prints "How far?" and then states that n [or number] is equal to 1. Next it states that dist is equal to read which we have made it read as a number using tonumber, otherwise it will just read a string.

for i = 1, dist do

I don't know if you have noticed in my other tutorials, but I use this line of code all the time. It's an easy to understand input loop of sorts. It loops the following code "dist" times, which we stated before as a read number.

The following code:


turtle.select(1)
		  turtle.dig()
		  turtle.forward()
		  turtle.digUp()
		  n = n + 1

This is easy to understand, the turtle selects slot 1, digs, moves forward and mines up. It then sates that n, which before we made equal 1, is now n + 1. So n now equals 2.

Next in the code, we state that if n equals 7 (n gets 1 higher every time n = n + 1) then do the following:
The turtle then turns left, digs once, selects slot 1, places it (slot one should be torches) and then turns right to continue BUT we now minus 6 from n, leaving n to equal 1 once again.

SECONDLY we state an else. This else is from before, where we stated that if turtle.detectDown() then:
But if the turtle DOESN'T detect down, the else comes into play. All it does is:
select slot 2, place downwards. In slot 2 there SHOULD be cobble, and if there isn't a block below it, it places cobble. Making a nice path for you!

I hope you enjoyed this and found it useful. Download for the program is below:

RAWR

#2 Ulthean

  • Members
  • 171 posts

Posted 24 December 2012 - 03:38 AM

First off: I think this is an admirable idea. To create a tutorial for beginning LUA programmers. I do have some:

Questions and suggestions:
  • I suggest making a seperate tutorial topic, where you add all programs under a single post.
  • I know your goal is to keep it simple, however: your code doesn't always meet the specification (distance) since it does not factor in gravel
    I think it would be better if you structure your post like the example I give in the next post
About your code:

print("How far?")
n = 1
dist = tonumber read()
if turtle.detectDown() then
  for i = 1, dist do
    turtle.select(1)
    -- As far as I can tell there is no reason to have this select statement here.
    turtle.dig()
    turtle.forward()
    turtle.digUp()
    n = n + 1
    if n == 7 then
      turtle.turnLeft()
      turtle.dig()
      turtle.select(1)
      turtle.place()
      turtle.turnRight()
      n = n - 6
      -- I suggest using "n = 1", easier to understand and better
      -- If you want to change to program to place a torch every 4 blocks this code does not need to be changed
    else
      -- This else clause is wrong, it will place a block if n does not equal 7 and does not relate to the detectDown you placed earlier
      turtle.select(2)
      turtle.placeDown()
    end
  end
end

Correct code would be:

print("How far?")
dist = tonumber read()
n = 1

for i = 1, dist do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  n = n + 1
  if not turtle.detectDown() then
	turtle.select(2)
	turtle.placeDown()
  end
  if n == 7 then
	turtle.turnLeft()
	turtle.dig()
	turtle.select(1)
	turtle.place()
	turtle.turnRight()
	n = 1
  end
end


#3 Ulthean

  • Members
  • 171 posts

Posted 24 December 2012 - 03:41 AM

Goal:
The goals is to create a simple 2X1 tunnel digger with user input for the length of the tunnel and block placing/torch placing.

Finished code:
Spoiler

Steps:
1. First let's build a simple program that digs the block in front of the turtle, moves forward and then digs the block above the turtle (creating a 2 high tunnel):
turtle.dig()
					turtle.forward()
					turtle.digUp()

2. This code isn't perfect, imagine the turtle encounters gravel blocks, the turtle would dig the bottom block, but before it has the chance to move forward the
other blocks will have fallen, blocking the turtle. So let's try to remedy this:
while turtle.detect() do -- As long as there is a block in front of the turtle
					  turtle.dig()
					  sleep(0.25) -- Wait for 0.25 seconds
					end
					turtle.forward()
					turtle.up()
Now if the turtle encounters gravel, it will dig one block, wait for a quarter of a second and check again, it will do this until there no longer is a block in front of the turtle.
3. ...
4. ...
5. ...

#4 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 24 December 2012 - 11:58 AM

View PostUlthean, on 24 December 2012 - 03:41 AM, said:

Goal:
The goals is to create a simple 2X1 tunnel digger with user input for the length of the tunnel and block placing/torch placing.

Finished code:
Spoiler

Steps:
1. First let's build a simple program that digs the block in front of the turtle, moves forward and then digs the block above the turtle (creating a 2 high tunnel):
turtle.dig()
					turtle.forward()
					turtle.digUp()

2. This code isn't perfect, imagine the turtle encounters gravel blocks, the turtle would dig the bottom block, but before it has the chance to move forward the
other blocks will have fallen, blocking the turtle. So let's try to remedy this:
while turtle.detect() do -- As long as there is a block in front of the turtle
					  turtle.dig()
					  sleep(0.25) -- Wait for 0.25 seconds
					end
					turtle.forward()
					turtle.up()
Now if the turtle encounters gravel, it will dig one block, wait for a quarter of a second and check again, it will do this until there no longer is a block in front of the turtle.
3. ...
4. ...
5. ...

I think you meant turtle.digUp() not turtle.up().
And I see what you have done, based the tutorial through the code, whereas i've put the code, and then explain it. I guess it would make it easier,and I will incorporate the idea next time.

#5 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 24 December 2012 - 12:09 PM

And thanks for helping clear that up, yes I left out the gravel because I thought it might confuse some people, put it's generally easy to understand.

#6 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 24 December 2012 - 08:16 PM

No one needs this?

#7 Ulthean

  • Members
  • 171 posts

Posted 24 December 2012 - 10:13 PM

BTW: script kiddie is a forum rank, not a user name :)

#8 ChunLing

  • Members
  • 2,027 posts

Posted 25 December 2012 - 01:34 AM

I thought the:
while turtle.detect() do
  turtle.dig()
  sleep(0.25)
end
business was a gravel handler.

No, I guess you must have edited that in.

Edited by ChunLing, 25 December 2012 - 01:35 AM.


#9 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 25 December 2012 - 09:34 PM

View PostChunLing, on 25 December 2012 - 01:34 AM, said:

I thought the:
while turtle.detect() do
  turtle.dig()
  sleep(0.25)
end
business was a gravel handler.

No, I guess you must have edited that in.

Yep, probably need to upgrade the tutorial to add it in.

#10 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 25 December 2012 - 09:36 PM

View PostUlthean, on 24 December 2012 - 10:13 PM, said:

BTW: script kiddie is a forum rank, not a user name :)
*Facepalms all the way*

#11 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 26 December 2012 - 10:26 PM

Fixed that up

#12 dkittrell

  • Members
  • 15 posts

Posted 28 December 2012 - 09:07 AM

thank you for this. im learning for about computercraft and this helped a lot , especially with everyone chimeing in with stuff that would make your code better.

#13 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 01 January 2013 - 08:09 PM

View Postdkittrell, on 28 December 2012 - 09:07 AM, said:

thank you for this. im learning for about computercraft and this helped a lot , especially with everyone chimeing in with stuff that would make your code better.

Thanks for using! Yes, does show the nice people that are about these days.
Check out my other tutorials too!

#14 Bpwoods

  • New Members
  • 4 posts

Posted 02 January 2013 - 11:07 AM

Ok, so I'm getting an Error with this code. First of all, I'm using tekkit for minecraft 1.2.5, not for 1.4.6. When i try to run this program, it tells me "Tunnel :5: 'for' limit must be a number" (Tunnel being the name of the program). Does anyone know how to fix it?

Edit: I'm going to post a thread in the help section.

#15 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 02 January 2013 - 05:15 PM

View PostBpwoods, on 02 January 2013 - 11:07 AM, said:

Ok, so I'm getting an Error with this code. First of all, I'm using tekkit for minecraft 1.2.5, not for 1.4.6. When i try to run this program, it tells me "Tunnel :5: 'for' limit must be a number" (Tunnel being the name of the program). Does anyone know how to fix it?

Edit: I'm going to post a thread in the help section.

Nono,that error must be because you need to tonumber it.

Without doing so, it will be trying to read as a string (line of text) instead of an integer (number)
dist = tonumber read()
n = 1

for i = 1, dist do
  while turtle.detect() do
  turtle.dig()
  sleep(0.25)


#16 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 03 January 2013 - 06:52 PM

Rawr

#17 Ulthean

  • Members
  • 171 posts

Posted 04 January 2013 - 12:16 AM

dist = tonumber read()

I'm pretty sure this should be


dist = tonumber(read())

Not entirely sure about LUA, but better safe than sorry, and adding the brackets is good practice.

#18 Churik

  • New Members
  • 5 posts

Posted 04 January 2013 - 12:22 AM

Nice tutorial :P
Also, is there a way to add a little code that also places tracks?
So it would dig a tunnel, place torches in the side and places tracks on his way

#19 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 04 January 2013 - 05:36 PM

View PostChurik, on 04 January 2013 - 12:22 AM, said:

Nice tutorial :P
Also, is there a way to add a little code that also places tracks?
So it would dig a tunnel, place torches in the side and places tracks on his way

Minecart tracks?
Why of course! But the only problem would be that it cannot place a track infront of it, then move forward, seeing as the track is counted as a block. SO the turtle would have to turn around, place track then turn back. Slowing the process a lot!

#20 De Tronazox

  • Members
  • 43 posts
  • LocationSydney, Australia

Posted 04 January 2013 - 05:38 PM

View PostUlthean, on 04 January 2013 - 12:16 AM, said:

dist = tonumber read()

I'm pretty sure this should be


dist = tonumber(read())

Not entirely sure about LUA, but better safe than sorry, and adding the brackets is good practice.

And you would be right, I left that out -_-.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users