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

Redwood lumberjack code help

Started by tagged, 07 October 2013 - 01:43 PM
tagged #1
Posted 07 October 2013 - 03:43 PM
Topic: Redwood lumberjack code help

Hello all!

I am new to programming and quite happy with how far I got, but cannot get my turtle to stop when there is no more tree left.

Program Goal:
1. Have user state how wide a Redwood is.
2. Chop down said tree, refueling along the way and dumping contents into an ender chest
3. Stop when done.

Problem:
Turtle is not stopping when there is no more tree. This started after adding adding code at line 77, i think i have it wrong to tell how many loops to make. Was hoping to have it take 1/2 the number input by the user.

Again, I have never programmed before writing this code, would love to know why the turtle keeps moving and how I can fix it.

Thank you for your help and support!

Spoiler

function needFuel()
  if turtle.getFuelLevel() <= 230 then
	turtle.select(2)
	turtle.refuel(1)
  end
end

function digTop()
  while turtle.detect() do
	turtle.digUp()
	turtle.dig()
	turtle.up()
	needFuel()
  end
  while not turtle.detectDown() do
	turtle.down()
  end
  turtle.forward()
end

function nextLeft()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
  while not turtle.detect() do
	turtle.forward()
  end
end

function nextRight()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
  while not turtle.detect() do
	turtle.forward()
  end
end

function mailHome()
  if turtle.getItemCount(3) ~= 0 then
  turtle.dig()
  turtle.select(1)
  turtle.place()
  end
  for i = 3,16 do
	turtle.select(i)
	turtle.drop()
  end
  turtle.select(1)
  turtle.dig()
end

function upBack()
  while turtle.detect() do
	digTop()
  end
  mailHome()
  nextLeft()
  while turtle.detect() do
	digTop()
  end
  mailHome()
  nextRight()
end

local tree = ""
  print("Lets cut down some trees!")
  print("Slot 1 - Ender Chest")
  print("Slot 2 - Fuel")

  write("Tree width? ")
  tree = read()
  print("Tree width "..tree)
  print("This tree is coming down, you may want to back up...")
	for i = 1, tree, 2 do
		upBack()
	end

or http://pastebin.com/pqxfQAEX
MR_nesquick #2
Posted 07 October 2013 - 05:37 PM
local tree = 0
tonumber(tree) = read()
and maybe do
 if turtle.detect() then
on line 60/65

edit: you are running two
for i =....
at the same time. maybe rename one to
for i2 =....
not sure if this can case a problem
tagged #3
Posted 07 October 2013 - 08:19 PM
ok, fixed it, had to change line 28 and line 40…
final code is pastebin.com/mNg9EkZF

it does go an extra line to nowhere but for my first program i am very happy :)/>
campicus #4
Posted 07 October 2013 - 08:19 PM
EDIT: Looks like you fixed the problem :)/> Also, thanks for formatting your code correctly!
SpoilerThere are a few ways to detect if the tree is no longer there. The easy way is:


repeat
	cutTreeFunction()
until not turtle.detect()

or you have a log in the turtles inventory and compare that with what is in front of him:


repeat
	cutTreeFunction()
until not turtle.compare()
Just make sure you have the correct slot selected.

Here is my logging program for a reference if you need it:
Spoilerhttp://pastebin.com/gMxYEKtc
tagged #5
Posted 07 October 2013 - 09:15 PM
Code looks good, i need to learn more of the programming commands, like repeat :)/>

Problem with your code on the redwoods in my mod (FTB Unleashed) is that the trees are usually 9 to 13 wide, and range from 4 to 13 long, so this code needs to account for no tree being there when it turns, or extra tree being there when it turns…. i started the turtle on the right most edge facing the tree. I think because my code is even lines (up and then back) i need to start the turtle next to the tree and make the first move to turn and cut… that way if no tree it would stop sooner.

Not sure if that makes since. Thank you for your feedback and you code, will be tweaking my some more to see if i can shorten it :)/>

Tagged
campicus #6
Posted 07 October 2013 - 11:48 PM
Problem with your code on the redwoods in my mod (FTB Unleashed) is that the trees are usually 9 to 13 wide, and range from 4 to 13 long, so this code needs to account for no tree being there when it turns, or extra tree being there when it turns

My code should be fine with any tree (as long as it is a square base). If the base is greater than 2 it runs this function:


turtle.dig()
turtle.forward()
for x = 1,4 do
    for x = 1,(wide-1) do
        cut()
        turtle.forward()
    end
    turtle.turnLeft()
end
Zaflis #7
Posted 14 January 2014 - 09:24 PM
Has anyone made a script to cut down the other redwood tree that is 7x7 saplings in size? Anytime i do search for redwood tree, i get these 2x2 ones. I'm especially curious how the bot would go around cutting the tree branches, or if it's too ineffective process to think about? But damn, it would look cool to see these enormous trees being cut by turtles as a farm.