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

Turtle Feller Code Problems[Updated, now with video]

Started by Geowil, 23 June 2013 - 09:51 PM
Geowil #1
Posted 23 June 2013 - 11:51 PM
Title: Turtle Feller Code Problems

Post contents:

Hello everyone, I am attempting my own variation of the advanced feller program posted on the wiki. I have it doing everything that the officially supplied code does but for some reason odd things happen, like the turtle somehow getting knocked off course and ending up a row or two off track. I have hunted for the problem in my code but I cannot seem to locate it and was wondering if someone could have a look. I just started using LUA two days ago but I am not new to programming (Java, C#, C++, VB).


shell.run('clear')

print("Please put Dirt in Slot 1, Saplings in Slot 2, a log in Slot 3, and fuel in Slot 16.")
print("Number of Trees To Plant?")
local x = tonumber(io.read())
print("How high do you want to plant the trees?")
local height = tonumber(io.read())
local dirt, sapling, log = 1, 2, 3
local i, i2


-- refuel

function tRefuel()
  if turtle.getFuelLevel() < 1 then
	turtle.select(16)

	if turtle.refuel(1) then
	  print("Refueled.")
  
	else
	  print("Could not refuel.")
	end
  end
end

-- Turtle Movement Functions

function orient()
  turtle.turnLeft()
  turtle.turnLeft()
end

function move(Str)
  if Str == "Plant" then
	for i = 1, 7 do
	  turtle.back()
	end
  end

  if Str == "PostPlant" then
	turtle.turnRight()
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
  end
	
  if Str == "PreCheck" then
	turtle.turnLeft()
  end		  

  if Str == "PostCheck" then
	turtle.turnRight()
  end

  if Str == "PostCut" then
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
  end

  if Str == "FF" then
	for i2=1, 7 do
	  turtle.forward()
	end
  end  

  if Str == "ForwardFourXTree" then
	for i=1, x-1 do
	  for i2=1, 7 do
		turtle.forward()
	  end
	end
  end
	
  if Str == "Return" then
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
  end
end

function toGround()
  while not turtle.detectDown() do
	turtle.down()
  end
end


-- Turtle Action Functions

function setup()
  turtle.select(dirt)
  if not turtle.compare() then
	turtle.place()
  end
end

function plantSapling()
  turtle.select(sapling)
  turtle.place()
end

function plantSPostCut()
  turtle.back()
  turtle.select(sapling)
  turtle.place()
end

function checkTree()
  turtle.select(log)
  if turtle.compare() then
	turtle.dig()
	turtle.forward()
	cutTree()
  end
end

function cutTree()
  while turtle.compareUp() do
	turtle.digUp()
	turtle.up()
	
	if not turtle.compareUp() then
	  toGround()
	  plantSPostCut() -- Call from here so we only try to plant if a cut has happened
	  return  -- If no more logs break the loop immediatly after back on ground
	end	
  end  
end

function checkFuel(fuel)
  if turtle.getFuelLevel() < fuel then
	tRefuel()
  end
end


-- Start Program

checkFuel(10)

--Plant initial saplings

orient()

for i=1,x do
  checkFuel(50)

  for i2 = 1,height do
	setup()
	turtle.up()
  end

  plantSapling()

  if i < x then -- To deal with turtle moving too far
	toGround()
	move("Plant")
  end
end

--Return After Plant

move("PostPlant")
move("ForwardFourXTree")
move("Return")

--Start looping

while true do
  -- Wait a day for trees to grow (currently set to 10 seconds for debugging)
  os.sleep(10)

  checkFuel(80)

  --Start checking trees
  for i=1,x do  
	move("PreCheck")
	checkTree()
	move("PostCheck")
	
	if i < x then -- To deal with turtle moving too far
	  move("FF")
	end
  end

  -- Get turtle back to start

  checkFuel(20)

  --Return After Cut

  move("PostCut")
  move("ForwardFourXTree")
  move("Return")
end
apemanzilla #2
Posted 24 June 2013 - 10:31 AM
The only possible problems I see would be external interference… Are there any blocks or mobs/players that could be getting in the way? Also, if the chunk unloads or the server restarts the turtle will stop and have to be manually put back. (Unless you code it to be persistent)
Zudo #3
Posted 24 June 2013 - 01:38 PM
Urgh… At least use the spoiler tags?
Geowil #4
Posted 24 June 2013 - 04:32 PM
The only possible problems I see would be external interference… Are there any blocks or mobs/players that could be getting in the way? Also, if the chunk unloads or the server restarts the turtle will stop and have to be manually put back. (Unless you code it to be persistent)

Hmm, that was a problem at first but I made it start adding height with dirt blocks to deal with it. Maybe I need to go higher. I do have the Mo Monsters mod installed so I have tons of animals every freaking where. When exactly does a chunk unload? Is it when you can no longer see a specific area? That may be what caused it if so as I was burning down part of the forest that was getting too close to my house. It is only 50 to 120 blocks away from where my turtle was working though. I will give it another whirl and watch it this time and see what happens. It may have been a bloody rabbit or some other flying creature as well that could have gotten in its way.

Thanks for looking the code over though.

Edit 1:

All right I figured it out I think. It was running out of fuel before it could complete the return trip after checking the trees and so would get stuck one block before it should have. I changed my code a bit so that checkFuel now loops until the desired fuel level is reached and also increased the fuel values passed throughout the program. Have yet to test it but this is the only thing that makes sense as even with an extra two forward moves added to the return trip it would still get stuck on that same spot.
Geowil #5
Posted 24 June 2013 - 05:31 PM
Well I am still having problems and I have no clue what is wrong as the code looks perfectly fine. Here is a video of my problem. I let it cycle through two checks to illustrate just how random the turtle is. One time it goes too short on the return trip and the second time it goes too far:

[media]http://youtu.be/L0fkYk7SXKQ[/media]

it may still be processing.

Also here is my revised code. Nothing was changed in the way the turtle moves but I have worked on it since my op:

Spoiler

shell.run('clear')

print("Please put Dirt in Slot 1, Saplings in Slot 2, a log in Slot 3, and fuel in Slot 16.")
print("Number of Trees To Plant?")
local x = tonumber(io.read())
print("How high do you want to plant the trees?")
local height = tonumber(io.read())
local dirt, sapling, log = 1, 2, 3
local i, i2


-- refuel

function tRefuel(fuel)
  if turtle.getFuelLevel() < 1 then
	turtle.select(16)

	if turtle.refuel(fuel) then
	  print("Refueled.")

	else
	  print("Could not refuel.")
	end
  end
end

-- Turtle Movement Functions

function orient()
  turtle.turnLeft()
  turtle.turnLeft()
end

function move(Str)
  if Str == "Plant" then
	for i = 1, 7 do
	  turtle.back()
	end
  end

  if Str == "PostPlant" then
	turtle.turnRight()
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
  end
		
  if Str == "PreCheck" then
	turtle.turnLeft()
  end			

  if Str == "PostCheck" then
	turtle.turnRight()
  end

  if Str == "PostCut" then
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
  end

  if Str == "FF" then
		for i2=1, 7 do
		  turtle.forward()
		end
  end  

  if Str == "ForwardFourXTree" then
	for i=1, x-1 do
	  for i2=1, 7 do
		turtle.forward()
	  end
	end
  end	  
					
  if Str == "Return" then
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
	turtle.forward()
	turtle.turnLeft()
	turtle.forward()
  end
end

function toGround()
  while not turtle.detectDown() do
	turtle.down()
  end
end


-- Turtle Action Functions

function setup()
  turtle.select(dirt)

  if not turtle.compare() then
	turtle.place()
  end
end

function plantSapling()
  turtle.select(sapling)
  turtle.place()
end

function plantSPostCut()
  turtle.back()
  turtle.select(sapling)
  turtle.place()
end

function checkTree()
  turtle.select(log)
  if turtle.compare() then
	turtle.dig()
	turtle.forward()
	cutTree()
  end
end

function cutTree()
  while turtle.compareUp() do
	turtle.digUp()
	turtle.up()
		
	if not turtle.compareUp() then
	  toGround()
	  plantSPostCut() -- Call from here so we only try to plant if a cut has happened
	  return  -- If no more logs break the loop immediatly after back on ground
	end	
  end  
end

function checkFuel(fuelLevel,fuel)
  if turtle.getFuelLevel() < fuelLevel then
	tRefuel(fuel)
  end
end


-- Start Program

checkFuel(80,2)

--Plant initial saplings

orient()

for i=1,x do
  for i2 = 1,height do
	setup()
	turtle.up()
  end

  plantSapling()

  if i < x then -- To deal with turtle moving too far
	toGround()
	move("Plant")
  end
end

--Return After Plant

move("PostPlant")
move("ForwardFourXTree")
move("Return")

--Start looping

while true do
  -- Wait a day for trees to grow (currently set to 10 seconds for debugging)
  os.sleep(10)

  --Start checking trees
  for i=1,x do  
	move("PreCheck")
	checkTree()
	move("PostCheck")
		
	if i < x then -- To deal with turtle moving too far
	  move("FF")
	end
  end

  -- Get turtle back to start

  checkFuel(30,4)

  --Return After Cut

  move("PostCut")
  move("ForwardFourXTree")
  move("Return2")
end

Geowil #6
Posted 25 June 2013 - 04:08 AM
So I did a bunch of testing and then put the code into a new file and low and behold it start working for no apparent reason. Then, after I had my back turned for about fifteen seconds it started spazzing out again even worse than before. I think there is something Minecraft is doing that is causing the mod code to hang and not behave correctly. It may have to do with other mods I have installed. Will keep trying to figure out what is going on though.
apemanzilla #7
Posted 25 June 2013 - 07:01 AM
I am honestly not sure what would be causing this… Are there any error showing up in the Minecraft console? (Not the turtle) And if so, could you post them?

Btw, chunks usually start to unload when you get about 10 chunks (160 blocks) away from them. ChickenChunks adds a block called a ChunkLoader that will prevent chunks from unloading; I'd recommend using it.

EDIT: Was the program on the disk or the turtle itself? I've seen some problems regarding disks being inaccessible…
Geowil #8
Posted 25 June 2013 - 03:06 PM
I am honestly not sure what would be causing this… Are there any error showing up in the Minecraft console? (Not the turtle) And if so, could you post them?

Btw, chunks usually start to unload when you get about 10 chunks (160 blocks) away from them. ChickenChunks adds a block called a ChunkLoader that will prevent chunks from unloading; I'd recommend using it.

EDIT: Was the program on the disk or the turtle itself? I've seen some problems regarding disks being inaccessible…

I am not sure if it is the same console you are talking about but the Single Player Commands window does not show any errors. If what you are talking about is not this then let me know how to access the actual console. If chuncks are unloaded 160 blocks away then it was not that. My house is big, but not that big.

And no, I am, however, copying the file from a disk to the turtle.

In any case I have figured out what is causing my problems. For whatever reason when the Turtle refuels it causes it to stop processing events correctly and I end up with behavior like in the video. I let it run for 15 minutes and in that time it managed to work its way out over the ocean. Each time it would move back a tick further it would coincide with refueling.

I am going to try and just do the refueling directly instead of getFuelLevel and my custom fuel check and see what happens.

Edit:

Yes, confirmed. Whenever turtle.refuel() is called it causes the turtle to stop working properly. I am not sure if it causes any commands that follow it to not execute correctly or just the very next one but after refueling, while in a loop and/or off of the ground and/or with code following it, it seems to be causing behavioral issues.

Edit 2:

Placing refuel at the end of my loop has fixed my problem however I am not sure if it is turtle.refuel() or turtle.getFuelLevel() that is causing the problem. If someone is going to look into this on the developer's end either could be the problem; I will not be testing anymore today as I have my own mods and games that have been neglected over the past week as I have been messing around in MC and trying to figure this out. Will post more info tomorrow when I make some more test programs to figure out what exactly is going on.
Geowil #9
Posted 27 June 2013 - 03:16 AM
So I ran some extensive test programs and found that for whatever reason using refuel() in the middle of my code caused the turtle to stop processing directional movement properly. I do not know if this is because of something with my code or if it has to deal with the limitations set in Computercraft for low end systems. Either way I have it working properly now.