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

Turtle detecting what blocks is around it?

Started by ComputerCraftFan11, 09 March 2012 - 03:29 AM
ComputerCraftFan11 #1
Posted 09 March 2012 - 04:29 AM
I made a camera program but the only thing i'm missing is something for the turtle to detect the surrounding blocks.
Is this possible?

Or did I make that program for nothing :mellow:/>/>
Liraal #2
Posted 09 March 2012 - 06:01 AM
right now it can detect the presence of block.. nothing else
Astrognome #3
Posted 09 March 2012 - 02:06 PM
right now it can detect the presence of block.. nothing else
This ability may be added in a future update however.
ComputerCraftFan11 #4
Posted 09 March 2012 - 05:45 PM
right now it can detect the presence of block.. nothing else
This ability may be added in a future update however.
So I can't do something like

If(turtle.detect()) == "grass" then

Or

If(turtle.detect("grass"))then?
Liraal #5
Posted 09 March 2012 - 05:47 PM
nope. there is a little way around, but it's mostly useful only for mining
ComputerCraftFan11 #6
Posted 09 March 2012 - 05:49 PM
nope. there is a little way around, but it's mostly useful only for mining
K
Astrognome #7
Posted 09 March 2012 - 08:53 PM
nope. there is a little way around, but it's mostly useful only for mining
Did you just say what I think you said?

I MUST KNOW!!!!!
Liraal #8
Posted 09 March 2012 - 09:04 PM
do it the same way i programmed my digSlot function.
1. Provide a sample of material.
2. Check the state of every slot in the turtle.
3. Dig.
4. Check state again.
5. Has the slot containing the sample received additional item?
Neowulf #9
Posted 09 March 2012 - 09:05 PM
Think he means start the turtle with a piece of cobble (and possibly dirt) in a specific inventory spot, and check the size of the stack after every break event. If the cobble stack went up by 1, it was a stone block. If not, something more interesting.
ComputerCraftFan11 #10
Posted 09 March 2012 - 09:41 PM
How do I make it detect what item is in slot 1?

EDIT:

nvm, i made a program that can detect 8 different block types

Code:


turtle.dig()
if(turtle.getItemCount(1) == 2)then
 turtle.select(1)
 print("dirt") --change to name of item in slot 1
 turtle.place()
elseif(turtle.getItemCount(2) == 2) then
 turtle.select(2)
 print("dirt") --change to name of item in slot 2
 turtle.place()
elseif(turtle.getItemCount(3) == 2) then
 turtle.select(3)
 print("dirt") --change to name of item in slot 3
 turtle.place()
elseif(turtle.getItemCount(4) == 2) then
 turtle.select(4)
 print("dirt") --change to name of item in slot 4
 turtle.place()
elseif(turtle.getItemCount(5) == 2) then
 turtle.select(5)
 print("dirt") --change to name of item in slot 5
 turtle.place()
elseif(turtle.getItemCount(6) == 2) then
 turtle.select(6)
 print("dirt") --change to name of item in slot 6
 turtle.place()
elseif(turtle.getItemCount(7) == 2) then
 turtle.select(7)
 print("dirt") --change to name of item in slot 7
 turtle.place()
elseif(turtle.getItemCount(8) == 2) then
 turtle.select(8)
 print("dirt") --change to name of item in slot 8
 turtle.place()
else
 turtle.select(9)
 print("Block Unknown")
 turtle.place()
end
Follow the instructions and change "dirt"
Espen #11
Posted 10 March 2012 - 11:57 AM
SpoilerHow do I make it detect what item is in slot 1?

EDIT:

nvm, i made a program that can detect 8 different block types

Code:


turtle.dig()
if(turtle.getItemCount(1) == 2)then
turtle.select(1)
print("dirt") --change to name of item in slot 1
turtle.place()
elseif(turtle.getItemCount(2) == 2) then
turtle.select(2)
print("dirt") --change to name of item in slot 2
turtle.place()
elseif(turtle.getItemCount(3) == 2) then
turtle.select(3)
print("dirt") --change to name of item in slot 3
turtle.place()
elseif(turtle.getItemCount(4) == 2) then
turtle.select(4)
print("dirt") --change to name of item in slot 4
turtle.place()
elseif(turtle.getItemCount(5) == 2) then
turtle.select(5)
print("dirt") --change to name of item in slot 5
turtle.place()
elseif(turtle.getItemCount(6) == 2) then
turtle.select(6)
print("dirt") --change to name of item in slot 6
turtle.place()
elseif(turtle.getItemCount(7) == 2) then
turtle.select(7)
print("dirt") --change to name of item in slot 7
turtle.place()
elseif(turtle.getItemCount(8) == 2) then
turtle.select(8)
print("dirt") --change to name of item in slot 8
turtle.place()
else
turtle.select(9)
print("Block Unknown")
turtle.place()
end
Follow the instructions and change "dirt"

Hey ComputerCraftFan11,
I hope you don't mind, I was in the mood and tried to put the code it into a more compact and generalized form, so you can get an idea for how you could use this more dynamically. It's all packed into a function with two optional parameters.
I've commented the code, the explanation of the two parameters is at the top of the function:

-- If bPrintBlock == false or nil, then it will just dig.
-- If bPrintBlock == true, then it will print out the predefined block name (See tBlockType).
-- If bDrop == false or nil, then it will re-place the block after scan.
-- If bDrop == false, then it will drop the digged block, but retain one block for further scanning.
-- Returns true if turtle.dig() was successful, false otherwise.
function dig( bPrintBlock, bDrop )
  local bDug = false
  local tBlockType = { "dirt", "cobble", "wool" }  -- Define the block of the 9 turtle slots here. First entry equals first slot, etc.

  -- Ensure there are only 9 defined entries in the table by removing the excess tail.
  while #tBlockType > 9 do
	  table.remove( tBlockType )
  end
  turtle.select(1)  -- Ensures correct pickup-behavior for reliable scanning results.

  -- Digs in any case, but only checks blocks and prints them if bPrintBlock is true.
  bDug = turtle.dig()
  if bDug and bPrintBlock then
	for i = 1, #tBlockType do  -- Iterate through all defined slots.
	  if ( turtle.getItemCount(i) > 1 ) then  -- Did the current slot count rise above 1?
		
		turtle.select(i)
		print( tBlockType[i] )
		turtle.place()  -- Temporarily re-place a block.
		if bDrop then  -- If we are in drop mode, we drop the block. If not, we re-place the block.
		  turtle.drop()  -- Drop the whole stack, but then ...
		  turtle.dig()  -- ... reclaim the temporarily placed block to be used for further scanning.
		end
		print(i)
		return bDug  -- Stops checking after the first match and returns success or failure of turtle.dig().
		
	  end
	end
  end

  return bDug  -- Return result of turtle.dig()
end

Hope it's of any use to you. It's really just a 'stub' and can easily be extended to include all three digging directions.
I'll leave that as an excercise to anyone who feels up for it though. Because I'm too lazy at the moment. ^^
If there are any questions or ideas for further improvement, especially for how to make this even more compact, please do tell. :mellow:/>/>
Cheers