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

simple lumberjack program - problem

Started by TyCamden, 25 November 2014 - 03:00 PM
TyCamden #1
Posted 25 November 2014 - 04:00 PM
Trying to make a lumberjack program by altering someone else's code.

It has produced error (do expected on line 7?). Maybe you can't have an And inside of a While Do line?

Any help fixing would be appreciated.


-- place saplings in slot 1
-- place fuel in slot 16
mission = 1
function tree()
   while turtle.detect() and mission=1 do
	  print("Chopping")
	  turtle.dig()
	  turtle.digUp()
	  print("Moving")
	  turtle.up()
   end
   while not turtle.detect() and not turtle.detectDown() and mission=1 do
	  print("Moving down")
	  turtle.down()
	  if turtle.detectDown() then
		 print("Job done")
	  end
   end
end
while true and mission=1 do
   turtle.select(1)
   turtle.place()
   if turtle.compare() then
   else
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		    turtle.select(16) -- choose slot 16 on turtle
		    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		    print("Refueling")
		    turtle.select(1) -- choose slot 1 on turtle (default?)
		 else
		    print("Need more fuel in slot 16 then rerun program")
		    mission = 0
		 end
	  end
	  tree()
   end
end
Lyqyd #2
Posted 25 November 2014 - 04:03 PM
Use == for comparison. = is for assignment. Also, `true and foo == value` is the same as `foo == value`, so you can get rid of the `true and` part.
TyCamden #3
Posted 25 November 2014 - 04:25 PM
Oops. That worked. Thanks! Two more questions…

1.) Will the program run forever until Ctrl-T terminated, or only once for that one tree growth?

2.) How can I make the turtle collect saplings when they drop?
KingofGamesYami #4
Posted 25 November 2014 - 04:37 PM
It will run forever until you change the mission variable.

turtle's will automatically pick up saplings from leaves they mine, otherwise look at turtle.suck()
TyCamden #5
Posted 25 November 2014 - 05:06 PM
I decided to get saplings from having the turtle mine the leaves, this might be a huge waste of fuel, but I am giving it a shot. Note that I am using the white trees.

I set it in front of a white tree and it looked ok at first, but then it got hung-up somehow, and didnt work.

Maybe u could see how to fix (OR) perhaps show me a simpler way to get saplings… like maybe just waiting and sucking off ground or something.

Either way, thanks!

code…


-- place saplings in slot 1
-- place fuel in slot 16
mission = 1
function clear()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.turnRight()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnRight()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnRight()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
   turtle.dig()
   turtle.forward()
   turtle.turnLeft()
   turtle.dig()
   turtle.forward()
end
function tree()
   while turtle.detect() and mission==1 do
	  print("Chopping")
	  turtle.dig()
	  turtle.digUp()
	  print("Moving")
	  turtle.up()
	  print("Clearing Some Leaves")
	  clear()
   end
   while not turtle.detect() and not turtle.detectDown() and mission==1 do
	  print("Moving down")
	  turtle.down()
	  if turtle.detectDown() then
		 print("Job done")
	  end
   end
end
while true and mission==1 do
   turtle.select(1)
   turtle.place()
   if turtle.compare() then
   else
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		    turtle.select(16) -- choose slot 16 on turtle
		    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		    print("Refueling")
		    turtle.select(1) -- choose slot 1 on turtle (default?)
		 else
		    print("Need more fuel in slot 16 then rerun program")
		    mission = 0
		 end
	  end
	  tree()
   end
end
KingofGamesYami #6
Posted 25 November 2014 - 05:49 PM
I'm going to write my own tree program real quick, it may give you an idea (note: this may not work for all trees).

I also only mine the core leaves, leaving any that I would have to move to reach.


while true do --#looping
  if turtle.detect() then --#if the tree has grown
    turtle.dig()
    turtle.forward()
    while turtle.digUp() do --#dig to top of tree
      turtle.up()
      for i = 1, 4 do --#spin, mining all leaves around the turtle
        turtle.dig()
        turtle.turnRight()
      end
    end
    repeat until not turtle.down() --#go down until I hit something
    turtle.digDown() --#mine the stump
    turtle.select( 15 ) --#15 is the slot for saplings
    turtle.placeDown() --#place a sapling
    turtle.back()
  else
    sleep( 10 ) --#wait ten seconds if the tree hasn't grown
  end
end
Edited on 25 November 2014 - 04:51 PM
TyCamden #7
Posted 25 November 2014 - 06:12 PM
Problem with your version of the program. If digs forward, but doesnt then move forward, so it then digs down into dirt, and does nothing else.

To be fair, the following is the code I ran (I included a fueling section to the code. slot 15 is saplings, 16 is coal)…


-- place saplings in slot 15
-- place fuel in slot 16
mission = 1
while true and mission==1 do --#looping
  if turtle.detect() then --#if the tree has grown
    turtle.dig()
    turtle.forward()
    while turtle.digUp() do --#dig to top of tree
	  turtle.up()
	  for i = 1, 4 do --#spin, mining all leaves around the turtle
	    turtle.dig()
	    turtle.turnRight()
	  end
    end
    repeat until not turtle.down() --#go down until I hit something
    turtle.digDown() --#mine the stump
    turtle.select( 15 ) --#15 is the slot for saplings
    turtle.placeDown() --#place a sapling
    turtle.back()
  else
    sleep( 10 ) --#wait ten seconds if the tree hasn't grown
  end
  FuelLeft = turtle.getFuelLevel() + 1
  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
	 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
	    turtle.select(16) -- choose slot 16 on turtle
	    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
	    print("Refueling")
	    turtle.select(1) -- choose slot 1 on turtle (default?)
	 else
	    print("Need more fuel in slot 16 then rerun program")
	    mission = 0
	 end
  end
end
KingofGamesYami #8
Posted 25 November 2014 - 06:17 PM
Problem with your version of the program. If digs forward, but doesnt then move forward, so it then digs down into dirt, and does nothing else.

Did you put fuel in the turtle? It should at least move forward. Also, I forgot to mention where to start the turtle.

T
 S
 D
T = Turtle
S = Sapling
D = Dirt
TyCamden #9
Posted 25 November 2014 - 06:35 PM
Yes, fueled. Started turtle in proper spot. Same problem. It only digs 1 spot of the grown tree then doesnt move at all, even forward.

To try to debug, I changed the program to read as shown below.

When I run it, I click on the turtle and it shows the following messages:

Dig
Forward
Down
Place-Sapling
Move-Backward
Refueling

but again note that it never actually does any of these things, except Dig… once.


-- place saplings in slot 15
-- place fuel in slot 16
mission = 1
while true and mission==1 do --#looping
  if turtle.detect() then --#if the tree has grown
    turtle.dig()
    print("Dig")
    turtle.forward()
    print("Forward")
    while turtle.digUp() do --#dig to top of tree
	  turtle.up()
	  print("Up")
	  for i = 1, 4 do --#spin, mining all leaves around the turtle
	    turtle.dig()
	    turtle.turnRight()
	  end
	  print("Spin/Dig-Leaves")
    end
    repeat until not turtle.down() --#go down until I hit something
    turtle.digDown() --#mine the stump
    print("Down")
    turtle.select( 15 ) --#15 is the slot for saplings
    turtle.placeDown() --#place a sapling
    print("Place-Sapling")
    turtle.back()
	  print("Move-Backward")
  else
    sleep( 10 ) --#wait ten seconds if the tree hasn't grown
  end
  FuelLeft = turtle.getFuelLevel() + 1
  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
	 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
	    turtle.select(16) -- choose slot 16 on turtle
	    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
	    print("Refueling")
	    turtle.select(1) -- choose slot 1 on turtle (default?)
	 else
	    print("Need more fuel in slot 16 then rerun program")
	    mission = 0
	 end
  end
end
KingofGamesYami #10
Posted 25 November 2014 - 06:38 PM
Strange. It works when I run it.
TyCamden #11
Posted 25 November 2014 - 06:43 PM
Got it working. Refueling section was in bad place.

Here it is. Thanks for your help and code !


-- place saplings in slot 15
-- place fuel in slot 16
mission = 1
while true and mission==1 do --#looping
  if turtle.detect() then --#if the tree has grown
    FuelLeft = turtle.getFuelLevel() + 1
    if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
	   if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		  turtle.select(16) -- choose slot 16 on turtle
		  turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		  print("Refueling")
		  turtle.select(1) -- choose slot 1 on turtle (default?)
	   else
		  print("Need more fuel in slot 16 then rerun program")
		  mission = 0
	   end
    end
    turtle.dig()
    print("Dig")
    turtle.forward()
    print("Forward")
    while turtle.digUp() do --#dig to top of tree
	  turtle.up()
	  print("Up")
	  for i = 1, 4 do --#spin, mining all leaves around the turtle
	    turtle.dig()
	    turtle.turnRight()
	  end
	  print("Spin/Dig-Leaves")
    end
    repeat until not turtle.down() --#go down until I hit something
    turtle.digDown() --#mine the stump
    print("Down")
    turtle.select( 15 ) --#15 is the slot for saplings
    turtle.placeDown() --#place a sapling
    print("Place-Sapling")
    turtle.back()
	  print("Move-Backward")
  else
    sleep( 10 ) --#wait ten seconds if the tree hasn't grown
  end
end
TyCamden #12
Posted 25 November 2014 - 07:12 PM
Any chance you could have it dig the leaves out up to 2 squares away from trunk, instead of 1 ?

(it would increase the chances of getting saplings)
KingofGamesYami #13
Posted 25 November 2014 - 07:47 PM
Any chance you could have it dig the leaves out up to 2 squares away from trunk, instead of 1 ?

(it would increase the chances of getting saplings)

Easily, although that would multiply the turtle's movements by 9…

Movements Consuming Fuel Per Level (original)
up

Movements Consuming Fuel Per Level (edited)
up
forward
back
forward
back
forward
back
forward
back
TyCamden #14
Posted 25 November 2014 - 07:49 PM
I don't get it. Wouldn't this just move it back and forth accomplishing nothing?

forward
back
forward
back
forward
back
forward
back

Perhaps you could post my code with revisions so I could see what you meant.
KingofGamesYami #15
Posted 25 November 2014 - 07:52 PM

-- place saplings in slot 15
-- place fuel in slot 16
mission = 1
while true and mission==1 do --#looping
  if turtle.detect() then --#if the tree has grown
    FuelLeft = turtle.getFuelLevel() + 1
    if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
           if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
                  turtle.select(16) -- choose slot 16 on turtle
                  turtle.refuel(1) -- refuel turtle with 1 item from slot 16
                  print("Refueling")
                  turtle.select(1) -- choose slot 1 on turtle (default?)
           else
                  print("Need more fuel in slot 16 then rerun program")
                  mission = 0
           end
    end
    turtle.dig()
    print("Dig")
    turtle.forward()
    print("Forward")
    while turtle.digUp() do --#dig to top of tree
          turtle.up()
          print("Up")
          for i = 1, 4 do --#spin, mining all leaves around the turtle
            turtle.dig()
            --#begin revision
            turtle.forward()
            turtle.dig()
            turtle.back()
            --#end revision
            turtle.turnRight()
          end
          print("Spin/Dig-Leaves")
    end
    repeat until not turtle.down() --#go down until I hit something
    turtle.digDown() --#mine the stump
    print("Down")
    turtle.select( 15 ) --#15 is the slot for saplings
    turtle.placeDown() --#place a sapling
    print("Place-Sapling")
    turtle.back()
          print("Move-Backward")
  else
    sleep( 10 ) --#wait ten seconds if the tree hasn't grown
  end
end
TyCamden #16
Posted 25 November 2014 - 08:49 PM
Great! Works better now. I even added a section of code that merges all of the saplings it collects down into slot-15.

Finished product as follows…


-- place saplings in slot 15
-- place fuel in slot 16
mission = 1
while true and mission==1 do --#looping
  if turtle.detect() then --#if the tree has grown
    FuelLeft = turtle.getFuelLevel() + 1
    if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		   if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
				  turtle.select(16) -- choose slot 16 on turtle
				  turtle.refuel(1) -- refuel turtle with 1 item from slot 16
				  print("Refueling")
				  turtle.select(1) -- choose slot 1 on turtle (default?)
		   else
				  print("Need more fuel in slot 16 then rerun program")
				  mission = 0
		   end
    end
    turtle.dig()
    print("Dig")
    turtle.forward()
    print("Forward")
    while turtle.digUp() do --#dig to top of tree
		  turtle.up()
		  print("Up")
		  for i = 1, 4 do --#spin, mining all leaves around the turtle
		    turtle.dig()
		    --#begin revision
		    turtle.forward()
		    turtle.dig()
		    turtle.back()
		    --#end revision
		    turtle.turnRight()
		  end
		  print("Spin/Dig-Leaves")
    end
    repeat until not turtle.down() --#go down until I hit something
    turtle.digDown() --#mine the stump
    print("Down")
    -- This next part will try to merge saplings into slot 15
	   for i = 1, 14 do
		  turtle.select(i)
		  if turtle.compareTo(15) then
			 turtle.transferTo(15)
		  end
	   end
    print("Merge saplings into slot 15 (if any)")
    turtle.select(15) --#15 is the slot for saplings
    turtle.placeDown() --#place a sapling
    print("Place-Sapling")
    turtle.back()
		  print("Move-Backward")
  else
    sleep( 10 ) --#wait ten seconds if the tree hasn't grown
  end
end