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

Making Turtle gravel-proof + placing torches

Started by Amdusias, 30 December 2012 - 08:23 AM
Amdusias #1
Posted 30 December 2012 - 09:23 AM
Hey community,

i m writing a digging program for my turtle, but i got following problems:
  • if the turtle hits gravel it will run strange ways, this is because it mined the block but another fell down so it turns. I already used the search function but i m realy new to this topic (noob), so please show me how do it with my own program
STILL needing help with this point: (get more Information by reading the posts below vvv)
  • the other problem are monsters which spawn when the turtle diggs out a room which is like 3x2x3 or sth. So if its 2 layer high or even higher mobs can spawn. there is a pretty easy way to solve this problem: the solution is placing torches. BUT how can i tell the turtle to place torches AND this ONLY on the first layer? because its obviously not able to place torches in the air.
(Tekkit, TurtleOS 1.3)

Do you have any suggestions or know the answer? Please help me!

EDIT: Thanks to remiX who helped me to make the program/turtle gravel-proof!!!

Oldest Version (Find newer ones in the posts below):

Spoiler

for y=1,dy do
for x=1,dx do
  if dz0 == true then

   if directionX == false then
	if turned == false then
	 turtle.turnLeft()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
   else
	if turned == false then
	 turtle.turnRight()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
   end
  else


   if (x % 2) == 0 then
	odd = false
   else
	odd = true
   end
   for z=1,dz do
	while turtle.detect() do
	 turtle.dig()
	end
	turtle.forward()
   end
   if odd == false then
	if directionX == true then
	  turtle.turnLeft()
	 else
	  turtle.turnRight()
	end
	if  x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	 end
	 turtle.forward()
	end
	if directionX == true then
	 turtle.turnLeft()
	else
	 turtle.turnRight()
	end

   elseif odd == true then
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
	if x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	 end
	turtle.forward()
	end
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
   end
  end
end
if y==dy then
else
  if directionY == true then
   while turtle.detectdirection() do
   turtle.digdirection()
   end
   turtle.direction()
  else
   while turtle.detectUp() do
   turtle.digUp()
   end
   turtle.up()
  end
end
end
Luanub #2
Posted 30 December 2012 - 12:13 PM
The gravel solution is easy. Use this or some variation of it.

local function forward()
while not turtle.forward() do
  turtle.dig()
  sleep(.25)
end
end

For the torch problem just add a variable to keep track of layer the turtle is on like so..

local sLayer = 1

local function up()
  turtle.up()
  sLayer = sLayer + 1
end

local function down()
  turtle.down()
  sLayer = sLayer - 1
end
Then just use an if statement to verify that sLayer == 1 prior to placing the torch.
theoriginalbit #3
Posted 30 December 2012 - 05:43 PM
Another option for the torches is if you want it to place them every say 4 blocks to use the modulo. All you need is tracking of how far it's moved

Something like this, may be some typos tho as I'm on my phone

xDist = 0

-- move it forward however you do
xDist = xDist + 1
If xDist % 4 == 0 then -- where 4 is the frequency of placement
  --place torch
end
Amdusias #4
Posted 31 December 2012 - 05:36 AM
Are you sure this works with the gravel?
For me it does not i pasted it in here:

Maybe its on the wrong place, but i don't think so. just have a look:


(Remember this is not the full programm!)
Spoiler

print("Starting...")

local function forward()
while not turtle.forward() do
turtle.dig()
sleep(.25)
end
end
if directionZ == true then
turtle.turnRight()
turtle.turnRight()
if directionX == true then
  directionX = false
else
  directionX = true
end
end
for y=1,dy do
for x=1,dx do
  if dz0 == true then

   if directionX == false then
	if turned == false then
	 turtle.turnLeft()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
   else
	if turned == false then
	 turtle.turnRight()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
   end
  else


   if (x % 2) == 0 then
	odd = false
   else
	odd = true
   end
   for z=1,dz do
	while turtle.detect() do
	 turtle.dig()
	end
	turtle.forward()
   end
   if odd == false then
	if directionX == true then
	  turtle.turnLeft()
	 else
	  turtle.turnRight()
	end
	if  x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	 end
	 turtle.forward()
	end
	if directionX == true then
	 turtle.turnLeft()
	else
	 turtle.turnRight()
	end
  
   elseif odd == true then
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
	if x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	 end
	turtle.forward()
	end
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
   end
  end
end
if y==dy then
else
  if directionY == true then
   while turtle.detectdirection() do
   turtle.digdirection()
   end
   turtle.direction()
  else
   while turtle.detectUp() do
   turtle.digUp()
   end
   turtle.up()
  end
end
end

The turtle does not always digg all the gravel it hits + it still running strange ways but ONLY if it hits gravel i tested it now very often. this problem only appears with gravel and sand.
snoble2022 #5
Posted 31 December 2012 - 08:29 AM
The problem is that when a turtle mines a falling block minecraft bugs out. Just add a this to the begginging of your dig code.

while turtle.detect() == "gravel" or turtle.detect() == "sand" do
  turtle.dig()
  sleep(1)
end

EDIT: You need the sleep(1) to let the falling block fall to the next level before mining it!
Kingdaro #6
Posted 31 December 2012 - 08:47 AM
Since when can turtle.detect() get the type of block?
remiX #7
Posted 31 December 2012 - 08:50 AM
Since when can turtle.detect() get the type of block?

If it can, I never knew either lol, maybe the new CC or this guy is wrong…

Rather do this:

while turtle.detect() do
turtle.dig()
sleep(0.4) -- The time it needs before each dig is exactly 0.4 seconds (for the least time)
Amdusias #8
Posted 01 January 2013 - 07:48 AM
First of all I want to thank everybody who replied to my question so far. The solution for the gravel-proof thingy by remiX was working very well for me. So again a big thanks dude. But I'm still having trouble with the torch placer.
I actually don't know where i failed but it must be very obvious so I can't find it :P/>.The code is below, please have a look and tell me what i've done wrong. By the way I tried to find a solution by my self because the ones you gave me were hard to implement for me, because there had to be two statements given and both had to be true so the turtle places a torch.

I WISH YOU ALL A HAPPY NEW YEAR AND THANKS!

The torchthing is the one with the sLayer and the xDist.
(And remember this is not the full program, but the most important pice of it)

When i watched the turtle digging I saw that the turtle selects slot 2 and never slot 1 in which the torches are. Why is this happening?
Thats very strange because it should select slot 1 when it moved 7 blocks forward. Seems like the loop does not work there?!?!
The turtle is still running normaly and diggs out the room which I advised it to do but it never places torches, like there would be nothing about in the code

Spoiler

print("Starting...")
if directionZ == true then
turtle.turnRight()
turtle.turnRight()
if directionX == true then
  directionX = false
else
  directionX = true
end
end
local sLayer = 1
local function up()		
  turtle.up()
  sLayer = sLayer + 1
end
local function down()
  turtle.down()
  sLayer = sLayer - 1
end
local xDist = 1
local function forward()
  turtle.forward()
  xDist = xDist + 1
end
if sLayer == 1 then
	  if xDist == 7 then
		  turtle.turnLeft()
		  turtle.dig()
		  turtle.place()
		  turtle.turnRight()
		  xDist = xDist - 6
	else
		  turtle.select(2)
		  turtle.placeDown()
	  end
end  
for y=1,dy do
for x=1,dx do
  if dz0 == true then

   if directionX == false then
	if turned == false then
	 turtle.turnLeft()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
   else
	if turned == false then
	 turtle.turnRight()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
   end
  else


   if (x % 2) == 0 then
	odd = false
   else
	odd = true
   end
   for z=1,dz do
	while turtle.detect() do
	 turtle.dig()
	 sleep(0.4)
	end
	turtle.forward()
   end
   if odd == false then
	if directionX == true then
	  turtle.turnLeft()
	 else
	  turtle.turnRight()
	end
	if  x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	  sleep(0.4)
	 end
	 turtle.forward()
	end
	if directionX == true then
	 turtle.turnLeft()
	else
	 turtle.turnRight()
	end
  
   elseif odd == true then
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
	if x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	  sleep(0.4)
	 end
	turtle.forward()
	end
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
   end
  end
end
if y==dy then
else
  if directionY == true then
   while turtle.detectdirection() do
   turtle.digdirection()
   sleep(0.4)
   end
   turtle.direction()
  else
   while turtle.detectUp() do
   turtle.digUp()
   sleep(0.4)
   end
   turtle.up()
  end
end
end
ChunLing #9
Posted 01 January 2013 - 11:06 AM
You don't have, anywhere in your code, any other turtle select command than that turtle.select(2)

And the part of the code which is evidently supposed to handle placing torches, but will not because you don't select your torch slot, is in an if that only executes once, because it is not part of any loop or function. When it executes, xDist is still 1, as you initialized it, so the torch bit doesn't execute, instead the other bit does.
Amdusias #10
Posted 01 January 2013 - 01:03 PM
Maybe suggestions how i could fix it?
ChunLing #11
Posted 01 January 2013 - 01:19 PM
Ah, okay.

First, change this bit so that it actually selects your torch slot before placing if xDist == 7
if sLayer == 1 then
    if xDist == 7 then
        turtle.turnLeft()
        turtle.dig()
        turtle.select(1) -- or wherever your torches are
        turtle.place()
        turtle.turnRight()
        xDist = xDist - 6
    else
        turtle.select(2)
        turtle.placeDown()
    end
end
Then take this whole chunk and put it somewhere in those for loops right after a call to your forward() function (which is the only way xDist gets incremented). Where exactly that should be I can't tell because you never call that function.

You could even make this part of the forward function (since I don't know that you want to be incrementing xDist without checking to see if it's time for a torch), but you'd still need to call the function somewhere in those loops.
Amdusias #12
Posted 01 January 2013 - 01:50 PM
Oh well this seems quite difficult to me xD. I m already happy that i just done it so far.
Couldn't i do sth like this?


if turtle.forward() then
if sLayer == 1 then
	  if xDist == 7 then
		  turtle.turnLeft()
		  turtle.dig()
		  turtle.select(1)
		  turtle.place()
		  turtle.turnRight()
		  xDist = xDist - 6
	else
			   -- how can I just tell it here just to continue digging when the statement isn't given?
		  
	  end
end	

So everytime it moves forward it will check if it needs to place a torch.
ChunLing #13
Posted 01 January 2013 - 01:59 PM
Yeah, that's about right, but you can't forget your xDist incrementation (might as well only do it if sLayer is 1). And you still need to actually call the function somewhere.

On incrementing and all that, you'll be safer using a modulus function, like "xDist = (xDist % 7)+1" so that you don't have to risk xDist getting past 7 somehow and never coming round for another pass.
Amdusias #14
Posted 01 January 2013 - 03:45 PM
i can't get this sh1t working. i don't know why lol.
i tried now to wrote xDist = xDist + 1 in every loop, well actually behind every forward() function.
the xDist should now update/ count +1 when the turtle moved one block forward. It still working perfectly, but its not placing any damn torches!!!!
OMG realy ^^
please help me i guess there is something wrong with this part:
seems like it does check it only one time although i've implemented all the xDist things


local sLayer = 1
local function up()		
  turtle.up()
  sLayer = sLayer + 1
end
local function down()
  turtle.down()
  sLayer = sLayer - 1
end
local xDist = 1

if sLayer == 1 then
   if xDist % 7 == 0 then
	turtle.turnLeft()
	turtle.dig()
	turtle.select(1) -- or wherever your torches are
	turtle.place()
	turtle.turnRight()
  end
end

This is what i ve done so far:
(Not the full code)

Spoiler

print("Starting...")
if directionZ == true then
turtle.turnRight()
turtle.turnRight()
if directionX == true then
  directionX = false
else
  directionX = true
end
end
local sLayer = 1
local function up()		
  turtle.up()
  sLayer = sLayer + 1
end
local function down()
  turtle.down()
  sLayer = sLayer - 1
end
local xDist = 1
if sLayer == 1 then
   if xDist % 7 == 0 then
	turtle.turnLeft()
	turtle.dig()
	turtle.select(1) -- or wherever your torches are
	turtle.place()
	turtle.turnRight()
  end
end
for y=1,dy do
for x=1,dx do
  if dz0 == true then

   if directionX == false then
	if turned == false then
	 turtle.turnLeft()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
	xDist = xDist +1
   else
	if turned == false then
	 turtle.turnRight()
	 turned = true
	end
	turtle.dig()
	turtle.forward()
	xDist = xDist +1
   end
  else


   if (x % 2) == 0 then
	odd = false
   else
	odd = true
   end
   for z=1,dz do
	while turtle.detect() do
	 turtle.dig()
	 sleep(0.4)
	end
	turtle.forward()
	xDist = xDist +1
   end
   if odd == false then
	if directionX == true then
	  turtle.turnLeft()
	 else
	  turtle.turnRight()
	end
	if  x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	  sleep(0.4)
	 end
	 turtle.forward()
	 xDist = xDist +1
	end
	if directionX == true then
	 turtle.turnLeft()
	else
	 turtle.turnRight()
	end
  
   elseif odd == true then
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
	if x == dx then
	else
	 while turtle.detect() do
	  turtle.dig()
	  sleep(0.4)
	 end
	turtle.forward()
	xDist = xDist +1
	end
	if directionX == true then
	  turtle.turnRight()
	 else turtle.turnLeft()
	end
   end
  end
end
if y==dy then
else
  if directionY == true then
   while turtle.detectdirection() do
   turtle.digdirection()
   sleep(0.4)
   end
   turtle.direction()
  else
   while turtle.detectUp() do
   turtle.digUp()
   sleep(0.4)
   end
   turtle.up()
  end
end
end
ChunLing #15
Posted 02 January 2013 - 02:33 AM
This part:
if sLayer == 1 then
   if xDist % 7 == 0 then
	    turtle.turnLeft()
	    turtle.dig()
	    turtle.select(1) -- or wherever your torches are
	    turtle.place()
	    turtle.turnRight()
  end
end
is still not part of any function, thus only gets called once, when xDist is still at it's initial value of 1, so it doesn't actually do anything.
Amdusias #16
Posted 02 January 2013 - 03:10 PM
Thank you all for your help. Especially ChunLing and remiX. With your help I was able to get this programm running with all the extras.
Its now working perfectly for me!
I wish you all a happy new year.

(This Thread can be closed)
snoble2022 #17
Posted 02 January 2013 - 03:40 PM
Since when can turtle.detect() get the type of block?

Yeah, sorry. It sadly doesn't :(/>. Again, sorry. As an execuse I'm going to say i've been up 48 hours straight (On that day)