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

[Turtle]Turtle not detecting saplings properly?[SOLVED]

Started by Ikkalyzte, 09 July 2012 - 03:03 AM
Ikkalyzte #1
Posted 09 July 2012 - 05:03 AM
So I have my mining turtle running a tree farm (like many others) and it seems to be working fairly efficiently. However, I recently made a change to the tree detection code so that it will check if the block is a sapling, rather than if it is wood. Now, the turtle will occasionally break a sapling and replace it, as if it thought it was a tree. Is there a problem in the code (which I can't find :P/>/> ) or is this a bug with the turtle.compare() using saplings?

Here is the tree farming program:
Spoiler


function anykey()
  while true do
	local keypress=os.pullEvent("key")
	if keypress=="key" then
	  break
	end
  end
end

shell.run('clear')
textutils.slowPrint("Initialization in progress...")
sleep(5)
print("This Turtle is programmed for a fenced in, rectangular birch tree farm, with 2 spaces between each tree, and a one-block space around the whole farm.")
sleep(1)
print("I am supposed to be placed in the bottom-left corner of the farm. If this is not the case, please move me there and try again.")
print("Press any key to continue.")
anykey()
shell.run('clear')
print("Please fill the upper-left slot with saplings.")
print("When finished, press any key to continue.")
anykey()
shell.run('clear')
print("Auto-Measuring...")

--Auto-init:

local L=0 --length of farm
local W=0 --width of farm

--Measure length

while not turtle.detect() do
  turtle.forward()
  turtle.turnLeft()
  if turtle.detect() then
	L=L+1
  else
	turtle.turnLeft()
	turtle.forward()
	turtle.forward()
	turtle.turnRight()
	break
  end
  turtle.turnRight()
  turtle.forward()
  if not turtle.detect() then
	turtle.forward()
  end
end
turtle.turnLeft()
turtle.turnLeft()

--Return to start

local x=3*L-1

for e=1,x do
  turtle.forward()
end
turtle.turnRight()

--Measure width

while not turtle.detect() do
  turtle.forward()
  turtle.turnRight()
  if turtle.detect() then
	W=W+1
  else
	turtle.turnRight()
	turtle.forward()
	turtle.forward()
	turtle.turnLeft()
  end
  turtle.turnLeft()
  turtle.forward()
  if not turtle.detect() then
	turtle.forward()
  end
end
turtle.turnRight()
turtle.turnRight()

--Return to start

local y=3*W-1

for f=1,y do
  turtle.forward()
end
turtle.turnLeft()

print("Initialization complete! Beginning farming...")


--Actual Farming

local farmloop=true
turtle.select(1)

while(farmloop) do
  while turtle.getItemCount(1)>16 do
	for a=1,W do
	  for b=1,L do
		turtle.forward()
		turtle.turnLeft()
		if turtle.compare()==false then
		  turtle.dig()
		  turtle.forward()
		  while turtle.detectUp() do
			turtle.digUp()
			turtle.up()
		  end
		  while not turtle.detectDown() do
			turtle.down()
		  end
		  turtle.back()
		  turtle.place()
		else
		  turtle.place()
		end
		if b<L then
		  turtle.turnRight()
		  turtle.forward()
		  turtle.forward()
		end
	  end
	  turtle.turnLeft()
	  for c=1,(x-1) do
		turtle.forward()
	  end
	  if a<W then
		turtle.turnRight()
		turtle.forward()
		turtle.forward()
		turtle.forward()
		turtle.turnRight()
	  end
	end
	turtle.turnLeft()
	for d=1,(y-1) do
	  turtle.forward()
	end
	turtle.turnLeft()
  end

  --When saplings run low

  if turtle.getCount(1)>0 then
	turtle.drop()
  end

  for f=1,8 do
	rs.setOutput("back",true)
	rs.setOutput("back",false)
	sleep(1)
  end

  rs.setOutput("right",true)
  rs.setOutput("right",false)
end

print("Farming Complete! This tree farming program courtesy of ClAnta B)/>/>/>")

edit: Spoiler tags!
OmegaVest #2
Posted 09 July 2012 - 06:32 PM
Compare doesn't play nicely with item-to-block or item-to-tile entity detections. Dan is supposedly working on this, but until then, saplings, redstone, ingots and the like will never work out correctly.
Ikkalyzte #3
Posted 10 July 2012 - 06:05 AM
Ok, I guess I will have to make it compare with a piece of wood then. Not impossible, but more challenging for a fully automatic system. Thanks!