This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
De Tronazox's profile picture

Tunnel Miner With Distance Input [Tutorial] | For N00bs

Started by De Tronazox, 23 December 2012 - 10:17 PM
De Tronazox #1
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
Ulthean #2
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
Ulthean #3
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:
SpoilerSome code

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. …
De Tronazox #4
Posted 24 December 2012 - 11:58 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:
SpoilerSome code

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.
De Tronazox #5
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.
De Tronazox #6
Posted 24 December 2012 - 08:16 PM
No one needs this?
Ulthean #7
Posted 24 December 2012 - 10:13 PM
BTW: script kiddie is a forum rank, not a user name :)/>
ChunLing #8
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 on 25 December 2012 - 12:35 AM
De Tronazox #9
Posted 25 December 2012 - 09:34 PM
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.
De Tronazox #10
Posted 25 December 2012 - 09:36 PM
BTW: script kiddie is a forum rank, not a user name :)/>
*Facepalms all the way*
De Tronazox #11
Posted 26 December 2012 - 10:26 PM
Fixed that up
dkittrell #12
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.
De Tronazox #13
Posted 01 January 2013 - 08:09 PM
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!
Bpwoods #14
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.
De Tronazox #15
Posted 02 January 2013 - 05:15 PM
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)
De Tronazox #16
Posted 03 January 2013 - 06:52 PM
Rawr
Ulthean #17
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.
Churik #18
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
De Tronazox #19
Posted 04 January 2013 - 05:36 PM
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!
De Tronazox #20
Posted 04 January 2013 - 05:38 PM
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 -_-/>.
torpdeo #21
Posted 06 January 2013 - 08:15 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

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!

You could make the turtle go in the middle of the tunnel and place tracks under it
civilwargeeky #22
Posted 07 January 2013 - 04:31 PM
Wow, does no one here use this? Instead of detecting, just get straight to the point and move
 while not turtle.forward() do
if not turtle.dig() then
turtle.attack()
end
end 
It's what I use for all my mining turtles. I think it is the most simple solution for gravel, sand, and annoying zombies/players.
civilwargeeky #23
Posted 07 January 2013 - 04:32 PM
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!
Or instead of having it at the bottom and digging up, then it could be at the top and dig down, then place track.
DielsAlder #24
Posted 30 May 2013 - 05:11 PM
i posted this to pastebin for easy turtle acquisition

http://pastebin.com/1XfGjgae