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

advansed tree farm using turtle with occasional error

Started by tebbenjo, 20 July 2013 - 07:03 PM
tebbenjo #1
Posted 20 July 2013 - 09:03 PM
title:advansed tree farm using turtle with occasional error

Ok, I like to think I'm a fairly good programer, but this lua stuff is different enough from VB or C# to cause me extreme frustration. I just wrote a tree farm program that <should> detect the with of the tree and the side of a 2 wide tree it was placed on and cut it down every 5 minutes, remove sapling drops from a chest below put everything in a "return chest" above it and keep charcoal stocked from it's left. but about every 3 tree cycles very consistently it only cuts down 1/2 the 2 wide tree. I'm frustrated with the whole thing and am fed up with debugging if someone could help I would be very greatfull. pleas excuse my spelling, it's not my speciality and I would appreciate the pointing out of any bad practises in my code, I'v never before programed computercraft of lua. here is all you should need to know:

the code:

--/lua
--tebbenjo
replant = true
hight = 0
function fuel()
if turtle.getFuelLevel() < 1 then
  turtle.select(2)
  if turtle.refuel(1) then
   print "refueled"
  else
   for n=1,16 do
    if turtle.getItemCount(n) > 0 then
	 if turtle.refuel(n) then
	  break
	 end
    end
   end
  end 
  turtle.select(1)
end
end
function down()
for h=0,hight do
  fuel()
  turtle.down()
end
end
function up()
while not turtle.up() do
  fuel()
  turtle.digUp()
  turtle.up()
  hight = hight + 1
end
end
function digDown()
for h=0,hight do
  fuel()
  turtle.digDown()
  turtle.down()
end
end
function side()
up()
turtle.dig()
turtle.forward()
digDown()
end
while true do
os.startTimer(300)
local event = os.pullEvent("timer")

for d=1,8 do
  fuel()
  turtle.forward()
end

if turtle.detect() then
 
  fuel()
  turtle.down()
  fuel()
  turtle.dig()
  fuel()
  turtle.forward()
 
  if turtle.detect() then
   hight = 0
   print "2x2 gerth detected"
   side()
   hight = 0
   fuel()
   turtle.turnRight()
   if turtle.detect() then
    print "I started on the Left side"
    fuel()
    turtle.dig()
    fuel()
    turtle.forward()
    fuel()
    turtle.turnRight()
    side()
    if replant then
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.turnRight()
	 turtle.place()
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.forward()
	 fuel()
	 turtle.turnRight()
	 turtle.place()
	 fuel()
	 turtle.turnRight()
	 turtle.place()
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.back()
	 turtle.place()
    else
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.forward()
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.back()
    end
   else
    print "I started on the Right side"
    fuel()
    turtle.turnLeft()
    fuel()
    turtle.turnLeft()
    turtle.dig()
    fuel()
    turtle.forward()
    fuel()
    turtle.turnLeft()
    side()
    if replant then
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.turnLeft()
	 turtle.place()
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.forward()
	 fuel()
	 turtle.turnLeft()
	 turtle.place()
	 fuel()
	 turtle.turnLeft()
	 turtle.place()
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.back()
	 turtle.place()
    else
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.forward()
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.back()
    end
   end
  else
   hight = 0
   print "1x1 gerth detected"
   up()
   down()
   fuel()
   turtle.back()
   if replant then
    turtle.place()
   end
  end
  
  fuel()
  turtle.up()
 
  for d=1,8 do
   fuel()
   turtle.back()
  end

  while turtle.suckDown() do
  end
 
  for s=3,16 do
   if turtle.getItemCount(s) > 0 then
    turtle.select(s)
    turtle.dropUp(turtle.getItemCount(s))
   end
  end
  turtle.select(1)
else
  print ("tree not grown at: " .. textutils.formatTime(os.time(), true))
  for d=1,8 do
   fuel()
   turtle.back()
  end
end

if turtle.getItemCount(2) < 64 then
  turtle.select(2)
  fuel()
  turtle.turnLeft()
  turtle.suck()
  turtle.select(3)
  turtle.drop(turtle.getItemCount(3))
  turtle.select(1)
  fuel()
  turtle.turnRight()
end
end
--/eof

pics of the error:




the turtle should have been one block forward and the whole tree should have been cut and replanted, the previous 2 times, you can see in the last pic, went fine.

please help a fellow programer, sencerly
tebbenjo
Lyqyd #2
Posted 20 July 2013 - 11:21 PM
Split into new topic.
tebbenjo #3
Posted 21 July 2013 - 03:14 PM
Split into new topic.

thanks
tebbenjo #4
Posted 21 July 2013 - 07:30 PM
redid/added some things

it has only occurred once since I changed these things; I also updated marcelOS. so one or the other made it much less often to happen, but still does.

here is the new code:


--/marcelOS
--/lua/file="treefarm.lua"/Sun,July__,2013/19:04/main/window=false/mouse=false/glodScreen=false/main=false/noIdle=false/dedicated=true/noEvents=false/turtle=true/goldTurtle="NA"/pidPrefrence=69|
--/OS-argus:noPersistant noGUI noMOUSE useFuel RUNon_OS-PowerOn RUNon_ServerOn RUNon_INTERACT USE_TurtleShell INCLUDElib_turtle INCLUDE-lib_os DoNotSend DoNotReport							 |
--/eoh
--tebbenjo
--/dec
replant = true
hight = 0
--/fuc
function fuel()
if turtle.getFuelLevel() < 1 then
  turtle.select(2)
  if turtle.refuel(1) then
   print "refueled"
  else
   for n=1,16 do
	if turtle.getItemCount(n) > 0 then
	 if turtle.refuel(n) then
	  break
	 end
	end
   end
  end
  turtle.select(1)
end
end
function down()
for h=0,hight do
  fuel()
  turtle.down()
end
end
function up()
while not turtle.up() do
  fuel()
  turtle.digUp()
  turtle.up()
  hight = hight + 1
end
end
function digDown()
for h=0,hight do
  fuel()
  turtle.digDown()
  turtle.down()
end
end
function side()
up()
if not turtle.forward() then
  if turtle.detectUp() then
   while not turtle.up() do
	fuel()
	turtle.digUp()
	turtle.up()
	hight = hight + 1
   end
   fuel()
   turtle.forward()
  else
   fuel()
   turtle.dig()
   turtle.forward()
  end
end
digDown()
end
--/srt
while true do
os.startTimer(600)
local event = os.pullEvent("timer")

for d=1,8 do
  fuel()
  while not turtle.forward() do
   fuel()
   turtle.dig()
   turtle.forward()
  end
end

if turtle.detect() then

  fuel()
  turtle.down()
  fuel()
  turtle.dig()
  fuel()
  turtle.forward()

  if turtle.detect() then
   hight = 0
   print "2x2 gerth detected"
   side()
   hight = 0
   fuel()
   turtle.turnRight()
   if turtle.detect() then
	print "I started on the Left side"
	fuel()
	turtle.dig()
	fuel()
	turtle.forward()
	fuel()
	turtle.turnRight()
	side()
	if replant then
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.turnRight()
	 turtle.suck()
	 turtle.place()
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.forward()
	 fuel()
	 turtle.turnRight()
	 turtle.suck()
	 turtle.place()
	 fuel()
	 turtle.turnRight()
	 turtle.suck()
	 turtle.place()
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.back()
	 turtle.suck()
	 turtle.place()
	else
	 fuel()
	 turtle.suck()
	 turtle.turnRight()
	 fuel()
	 turtle.suck()
	 turtle.forward()
	 fuel()
	 turtle.suck()
	 turtle.turnRight()
	 fuel()
	 turtle.suck()
	 turtle.back()
	 turtle.suck()
	end
   else
	print "I started on the Right side"
	fuel()
	turtle.turnLeft()
	fuel()
	turtle.turnLeft()
	turtle.dig()
	fuel()
	turtle.forward()
	fuel()
	turtle.turnLeft()
	side()
	if replant then
	 fuel()
	 turtle.turnLeft()
	 fuel()
	 turtle.turnLeft()
	 turtle.suck()
	 turtle.place()
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.forward()
	 fuel()
	 turtle.turnLeft()
	 turtle.suck()
	 turtle.place()
	 fuel()
	 turtle.turnLeft()
	 turtle.suck()
	 turtle.place()
	 fuel()
	 turtle.turnRight()
	 fuel()
	 turtle.back()
	 turtle.suck()
	 turtle.place()
	else
	 fuel()
	 turtle.suck()
	 turtle.turnLeft()
	 turtle.suck()
	 fuel()
	 turtle.forward()
	 turtle.suck()
	 fuel()
	 turtle.turnLeft()
	 turtle.suck()
	 fuel()
	 turtle.suck()
	 turtle.back()
	 turtle.suck()
	end
   end
  else
   hight = 0
   print "1x1 gerth detected"
   up()
   down()
   fuel()
   turtle.back()
   if replant then
	turtle.place()
   end
  end
  
  fuel()
  turtle.up()

  for d=1,8 do
   fuel()
   turtle.back()
  end

  while turtle.suckDown() do
  end

  for s=3,16 do
   if turtle.getItemCount(s) > 0 then
	turtle.select(s)
	turtle.dropUp(turtle.getItemCount(s))
   end
  end
  turtle.select(1)
else
  print ("tree not grown at: " .. textutils.formatTime(os.time(), true))
  for d=1,8 do
   fuel()
   turtle.back()
  end
end

if turtle.getItemCount(2) < 64 then
  turtle.select(2)
  fuel()
  turtle.turnLeft()
  turtle.suck()
  turtle.select(3)
  turtle.drop(turtle.getItemCount(3))
  turtle.select(1)
  fuel()
  turtle.turnRight()
end
end
--/end
--tebbenjo
--/eof