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

While loop gets skipped

Started by 0oo2, 23 December 2014 - 10:14 PM
0oo2 #1
Posted 23 December 2014 - 11:14 PM
i wrote my first program that is a automated tree cutter and replanter. But the while loop to place bonemeal on the saplings gets skipped.

here is the program that i wrote. the while loop is at line 15. Any help would be most appreciated. thanks

height = 0
turtle.select(3)
turtle.forward()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.place()
turtle.turnLeft()
turtle.back()
turtle.place()
turtle.turnRight()
turtle.back()
turtle.place()
while not turtle.compare() do
  turtle.select(4)
  turtle.place()
  turtle.select(2)

end
turtle.select(5)
turtle.dig()
turtle.forward()
while turtle.detectUp() do
  turtle.dig()
  turtle.digUp()
  turtle.up()
  height = height + 1

end
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnRight()
for i = 1 , height do
  turtle.dig()
  turtle.digDown()
  turtle.down()

  end

turtle.select(2)
turtle.transferTo(16,63)
turtle.dig()
turtle.back()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
turtle.select(5)
turtle.drop()
turtle.select(6)
turtle.drop()
turtle.select(7)
turtle.drop()
turtle.select(8)
turtle.drop()
turtle.select(9)
turtle.drop()
turtle.select(10)
turtle.drop()
turtle.select(11)
turtle.drop()
turtle.select(12)
turtle.drop()
turtle.select(13)
turtle.drop()
turtle.select(14)
turtle.drop()
turtle.select(15)
turtle.drop()
turtle.select(16)
turtle.drop()
if turtle.getFuelLevel() < 150 then
  turtle.select(1)
  turtle.refuel(5)
  print "Refueled"

end
turtle.turnRight()
turtle.turnRight()

theoriginalbit #2
Posted 24 December 2014 - 12:43 AM
The problem is that because tree saplings have their metadata change based on how close they are to growing into a tree, meaning they will rarely match the sapling in your inventory that has no metadata. Your best solution is to hit it with bonemeal when the block in front of the Turtle isn't a log.
0oo2 #3
Posted 24 December 2014 - 06:06 AM
Oh sorry, i forgot to tell what is in which slot. So in slot 1 is coal, in slot 2 is a Fir Log to be compared to, in slot 3 is the tree saplings, and in slot 4. I ended up remove the "not" in the line 15 compare, and it seems to work unless there is something in the way of the tree. then it just digs the saplings then does the rest of the program. BTW nice website.
Bomb Bloke #4
Posted 24 December 2014 - 11:30 AM
If you have a fir log in slot 2 to "compare to", why do you never compare anything to it? As BIT correctly spotted, you're checking against your first sapling slot - and that's not going to work, because a sapling in the world won't always match a sapling in an inventory.

Personally I like to have my turtle hover a block above ground level. The turtle simply chops out anything in front of it as it moves forward. If it floats over a filled space, and blocks are above it, it chops them out too. If it floats over an empty space (or a space with a log it can chop out), it'll stick a sapling there. A snippet of the chunk in concern looks like this:

			if turtle.detectDown() then
				-- Dig out the current column if need be.
				turtle.select(1)  -- First slot for storing harvested stuff.
				while turtle.detectUp() do
					while not turtle.up() do turtle.digUp() end
					pos.z = pos.z + 1
					for i=1,4 do
						while turtle.detect() do turtle.dig() end
						while not turtle.turnRight() do end
					end
				end
				while pos.z > node[1][3] do
					while not turtle.down() do turtle.digDown() end
					pos.z = pos.z - 1
				end
			end
			
			-- Re-plant.
			turtle.select(16)  -- Log slot.
			if turtle.compareDown() or not turtle.detectDown() then
				while turtle.compareDown() do turtle.digDown() end
				turtle.select(15)  -- Sapling slot.
				turtle.placeDown()
			end

By the way, remember you can use the counter variable from your "for" loops. Eg:

for i = 5, 16 do
  turtle.select(i)
  turtle.drop()
end