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

Tunnelling Turtle Help, Please

Started by DukeofGames50, 10 July 2016 - 01:06 AM
DukeofGames50 #1
Posted 10 July 2016 - 03:06 AM
hello, i'm brand new to programming turtles and need help writing my first one so it does what i want it to. i want it to do the standard "tunnel" function it already knows, but return to its starting position once the 16th slot has something in it to avoid overflowi already have the "tunnel" motion successfully programmed, where it will: dig, move forward, turn left, dig, move up, dig, turn right twice, dig, move down, dig, turn left.
how do i make this sequence loop itself until slot 16 has something inside, then return to the chest i put in its path on the way back?
keep in mind im extremely new to all of this mod so please forgive me if i ask a question that seems obvious to you.
Emma #2
Posted 10 July 2016 - 04:43 AM
Hey, welcome to the forums btw as I see this is your first post. One way you can achieve your desired behaviour is to check every time it digs a block (or row/slice, doesn't really matter) you can check the amount of items in slot 16 with this function:

local amount = turtle.getItemCount(16)
The 16 indicates which slot to look in. Then you just check if amount is larger than zero.

if amount > 0 then
  --Go back to the chest
end
Or

while turtle.getItemCount(16) == 0 do --keep going until its not empty

end
Now returning to the chest is pretty easy too. The easiest way to do it is to keep track of how far you have traveled by simply incrementing a variable each time you move forward (as it is tunneling) and when you want to return, just turn around and move forward (or just use turtle.back) the amount of times you counted. It would like something like this:

local howFarAmI = 0
while turtle.getItemCount(16) == 0 do --In this example I am going to mine until the 16th slot is not empty
  --Mine, and progress
  howFarAmI = howFarAmI + 1
end
--Now there is something in the 16th slot, so lets go back
turtle.turnLeft()
turtle.turnLeft()
for i=1,howFarAmI do
  turtle.forward() --The for loop repeats the code inside its body for "howFarAmI" many times
end

Have fun! :D/>
Edited on 10 July 2016 - 02:44 AM
DukeofGames50 #3
Posted 10 July 2016 - 05:29 AM
can i cutomize a function to mimic the commands stated in my original Post and put it in the program? I titled it Mining, and it does the dig forward, move forward, dig up… etc. Can i turn that into a function and put that into the program? Cuz i want to dig a 2x3 tunnel just like a regular function.
Emma #4
Posted 10 July 2016 - 06:17 AM
can i cutomize a function to mimic the commands stated in my original Post and put it in the program? I titled it Mining, and it does the dig forward, move forward, dig up… etc. Can i turn that into a function and put that into the program? Cuz i want to dig a 2x3 tunnel just like a regular function.

Not quite sure what your asking, but what I think you're asking is how can you make everything run as a function? If so, all you need to do is wrap all of your code inside a function, actually, all programs that are run technically already are functions, that's how they are run anyways, they are get stuck into the lua interpreter and returned as a chunk which can be called like a function, but that's a topic for another post. Sorry if this didn't help you, as I'm not really sure what you would like to do. If you could elaborate that would be nice :)/>

PS: If you could also post the code you have right now, that would be cool
DukeofGames50 #5
Posted 10 July 2016 - 07:02 AM
can i cutomize a function to mimic the commands stated in my original Post and put it in the program? I titled it Mining, and it does the dig forward, move forward, dig up… etc. Can i turn that into a function and put that into the program? Cuz i want to dig a 2x3 tunnel just like a regular function.

Not quite sure what your asking, but what I think you're asking is how can you make everything run as a function? If so, all you need to do is wrap all of your code inside a function, actually, all programs that are run technically already are functions, that's how they are run anyways, they are get stuck into the lua interpreter and returned as a chunk which can be called like a function, but that's a topic for another post. Sorry if this didn't help you, as I'm not really sure what you would like to do. If you could elaborate that would be nice :)/>

PS: If you could also post the code you have right now, that would be cool

Imagine a tunnel 3x3 hundreds of blocks long. at the start of this tunnel, there is a chest. To the left of the chest, there is a turtle facing the left wall. I want this turtle to mine a 2x3 tunnel as far as it can until the 16th slot is filled then I want it to return back to the start and drop its items into the chest.

Can I put that all into one program so I can forget about it until the turtle returns back to the chest?
Also, if I want to get rid of cobblestone, where and how do I set up blacklist?
DukeofGames50 #6
Posted 10 July 2016 - 01:55 PM
can i cutomize a function to mimic the commands stated in my original Post and put it in the program? I titled it Mining, and it does the dig forward, move forward, dig up… etc. Can i turn that into a function and put that into the program? Cuz i want to dig a 2x3 tunnel just like a regular function.

PS: If you could also post the code you have right now, that would be cool

a = turtle.dig()
b = turtle.forward()
c = turtle.digUp()
d = turtle.turnLeft()
a = turtle.dig()
e = turtle.up()
a = turtle.dig()
f = turtle.turnRight()
f = turtle.turnRight()
a = turtle.dig()
g = turtle.down()
a = turtle.dig()
d = turtle.turnLeft()

that final left turn is the end of the sequence and puts the turtle in the same position as it started, just one block forward, (sorry i forgot to add this in my last post)
The Crazy Phoenix #7
Posted 10 July 2016 - 04:17 PM
Yes, you can do that. If you use a variable to store how far the turtle has dug, you can easily have the turtle turn around, walk home, then walk back to where it was mining. The methods inside the Turtle API (inventory manipulation can be found there too), as well as for loops, should help you achieve your goal.
DukeofGames50 #8
Posted 11 July 2016 - 02:20 AM
Yes, you can do that. If you use a variable to store how far the turtle has dug, you can easily have the turtle turn around, walk home, then walk back to where it was mining. The methods inside the Turtle API (inventory manipulation can be found there too), as well as for loops, should help you achieve your goal.

that's great and all, but idk what that means or how to do it to make my program loop. that's my point, i am brand new to this computer coding thing, not just computercraft.
DukeofGames50 #9
Posted 11 July 2016 - 02:55 AM
i think i just realized the way I've been wishing to get this done is impractical, and the turtle would have a 2 in 3 chance of moving "back" into one of the 2 walls it has made once something is in the 16th slot.

so instead, is there a way i can make it run my first program "Mining (does one 2x3 spot then stops)" for let's say 50 blocks of distance and just make it blacklist cobblestone, and once it reaches 50 blocks, make it "back" 49 blocks which will put it right where it started, and "drop" everything in the chest (i know how to make it do the drop part, im pretty sure)

the point im asking this is i want my turtle to mine a great distance, return, and drop it's finds without me having to tell it what to do more than the 1 time at the start. and im clueless about computer coding.
DukeofGames50 #10
Posted 11 July 2016 - 01:56 PM
guys i got my program to create the tunnel it's standard "tunnel program" does so im halfway there, now i need help telling it to stop after it's traveled 50 blocks away and after stopping moving 49 blocks back to exactly where it starts, all in the same program
The Crazy Phoenix #11
Posted 12 July 2016 - 12:12 AM
Why specifically do that? You can declare a variable to store how far the turtle has travelled.

local step = 0 --# Create a variable called "step" and assign 0 to it
step = step + 3 --# Adds 3 to step and assigns it to step, step is now 3
step = 2 * step --# Multiplies step by 2 and assigns it to step, step is now 6
Then you increment (increase by 1) the value of step every time the turtle mines a layer. If it ever needs to return to where it started, just use a for loop.

for index = 1, step do
--# Whatever code you write here will run step times
--# This is perfect. Just write something like turtle.back(), or more complex if needed (such as verifying whether the turtle moved)
end
Drop off the items, refuel if needed, and head back off using the for loop to instead return where it stopped digging.
Edited on 11 July 2016 - 10:13 PM
DukeofGames50 #12
Posted 12 July 2016 - 03:44 AM
Why specifically do that? You can declare a variable to store how far the turtle has travelled.

local step = 0 --# Create a variable called "step" and assign 0 to it
step = step + 3 --# Adds 3 to step and assigns it to step, step is now 3
step = 2 * step --# Multiplies step by 2 and assigns it to step, step is now 6
Then you increment (increase by 1) the value of step every time the turtle mines a layer. If it ever needs to return to where it started, just use a for loop.

for index = 1, step do
--# Whatever code you write here will run step times
--# This is perfect. Just write something like turtle.back(), or more complex if needed (such as verifying whether the turtle moved)
end
Drop off the items, refuel if needed, and head back off using the for loop to instead return where it stopped digging.

i am going to take the next couple hours to slowly read thru this and try to add it to my mining program… but turtle.back() has a 66% chance of backing up into a wall if i relate it to when my inventory is full. that's why i want it to go a certain distance then stop looping the tunnel actions and back up

so something like "if step < 100 then turtle.back() in the same program "Mining"
however writhing that is possible give me advice if you may
Edited on 12 July 2016 - 01:46 AM
DukeofGames50 #13
Posted 12 July 2016 - 04:44 AM
Why specifically do that? You can declare a variable to store how far the turtle has travelled.

local step = 0 --# Create a variable called "step" and assign 0 to it
step = step + 3 --# Adds 3 to step and assigns it to step, step is now 3
step = 2 * step --# Multiplies step by 2 and assigns it to step, step is now 6
Then you increment (increase by 1) the value of step every time the turtle mines a layer. If it ever needs to return to where it started, just use a for loop.

for index = 1, step do
--# Whatever code you write here will run step times
--# This is perfect. Just write something like turtle.back(), or more complex if needed (such as verifying whether the turtle moved)
end
Drop off the items, refuel if needed, and head back off using the for loop to instead return where it stopped digging.

i am going to take the next couple hours to slowly read thru this and try to add it to my mining program… but turtle.back() has a 66% chance of backing up into a wall if i relate it to when my inventory is full. that's why i want it to go a certain distance then stop looping the tunnel actions and back up

so something like "if step < 100 then turtle.back() in the same program "Mining"
however writhing that is possible give me advice if you may

i think i got it finally, thanks. im not sure if im ready to call this post solved, but close
DukeofGames50 #14
Posted 12 July 2016 - 04:53 AM
no i still cant get it to stop its mining to go back when it travels a certain distance
The Crazy Phoenix #15
Posted 12 July 2016 - 04:58 AM
no i still cant get it to stop its mining to go back when it travels a certain distance

Use an if statement to detect whether it meets the required conditions to go back.


if condition then	 --# Replace condition with your actual condition
    --# Code here only runs if condition is true
end
DukeofGames50 #16
Posted 12 July 2016 - 05:57 AM
no i still cant get it to stop its mining to go back when it travels a certain distance

Use an if statement to detect whether it meets the required conditions to go back.


if condition then	 --# Replace condition with your actual condition
	--# Code here only runs if condition is true
end

so if (idk what to frigging write here) then turtle.back()

i hope you understand how so very clueless i am now
The Crazy Phoenix #17
Posted 12 July 2016 - 03:40 PM
no i still cant get it to stop its mining to go back when it travels a certain distance
Use an if statement to detect whether it meets the required conditions to go back.
 if condition then --# Replace condition with your actual condition --# Code here only runs if condition is true end 
so if (idk what to frigging write here) then turtle.back() i hope you understand how so very clueless i am now
More like this…

if condition then
--# Write ALL the code that handles returning, dropping off items, and resuming here.
end
The condition is when you want the turtle to return. For example, if you're digging a 3x3 tunnel, you could check if there's enough space inside the last slot (#16) to store 9 items.

if turtle.getItemSpace(16) < 9 then
--# For loop to back, dump inventory, for loop to go forward
end
Assuming you store how many layers you've mined in the step variable, the code inside the if could look something like this.

turtle.turnLeft() --# Turn around
turtle.turnLeft()
for index = 1, step do
turtle.forward() --# Repeat step times, this should take you back to where you started
end
for slot = 1, 16 do --# Dump all 16 slots (ground or chest if present)
turtle.select(slot)
turtle.drop()
end
turtle.select(1) --# Make sure slot 1 is selected to prioritize mined items to appear in it
turtle.turnLeft()
turtle.turnLeft()
for index = 1, step do
turtle.forward() --# Head back to where you left off
end
Edited on 12 July 2016 - 03:48 PM
DukeofGames50 #18
Posted 12 July 2016 - 03:52 PM
thank you for being so patient with me, i'll look thru your last post after i sleep on it to see if i can understand enough to get what i need done