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

Redwood Turtle Program + Questions?

Started by aarondm, 01 January 2013 - 12:17 PM
aarondm #1
Posted 01 January 2013 - 01:17 PM
Hey guys, first post.

I wrote this program to plant, grow, cut down, and deliver Redwood to a chest. I was looking for feedback on my code as well as some commands I'm not sure how to implement.

I would like for the functions to be contained within a program that would make sure the turtle has flint/tinder, saplings, and fuel and if any of these things were false he would send a message to a computer that would notify me. This is where my lack of knowledge stops me.

As far as this program is concerned, it works perfectly. It's just manually activated each time.

Pastebin link where it is properly formatted: http://pastebin.com/T16Xkz5t

Spoiler

--Basic fuel check function
function checkFuel()
  if turtle.getFuelLevel() < 100 then
	repeat
	turtle.select(16)
turtle.refuel(2)
turtle.select(1)
	until turtle.getFuelLevel() >= 100
  end --if
end --checkFuel()
--Plants 4 Redwood saplings in a 2x2
function plantTree()
turtle.select(14)
turtle.forward()
turtle.place()
turtle.back()
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.place()
turtle.back()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end --plantTree()
--Applies bonemeal and checks if tree has grown. If not, sleeps and repeats.
function growTree()
turtle.select(15)
turtle.place()
turtle.up()
  if turtle.forward() == true then
	repeat
turtle.back()
turtle.down()
sleep(120)
turtle.place()
turtle.up()
	until turtle.forward() == false
end --if
turtle.down()
end --growTree()
--Cuts down the Redwood. Up the left side, then down the right side.
function cutTree()
turtle.dig()
sleep(0.25)
turtle.forward()
turtle.dig()
  local height = 0
  if turtle.detectUp() == true then
	repeat
turtle.dig()
turtle.digUp()
turtle.up()
height = height + 1
	until turtle.detectUp() == false
end --if
checkFuel()
turtle.turnRight()
  if turtle.detect() == true then
turtle.dig()
end --if
turtle.forward()
turtle.turnLeft()
  if turtle.detectDown() == true then
	repeat
turtle.dig()
turtle.digDown()
turtle.down()
height = height - 1
	until height == 0
end --if
turtle.dig()
end --cutTree()
--Deposits the first 7 slots in a chest behind the turtle.
function dropOff()
checkFuel()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.select(1)
turtle.drop()
turtle.select(2)
turtle.drop()
turtle.select(3)
turtle.drop()
turtle.select(4)
turtle.drop()
turtle.select(5)
turtle.drop()
turtle.select(6)
turtle.drop()
turtle.select(7)
turtle.drop()
turtle.select(1)
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
checkFuel()
end --dropOff()
--Attempts to light leftover leaves on fire, then returns to original position.
function burnTree()
  if turtle.detectUp() == false then
	repeat
turtle.up()
	until turtle.detectUp() == true
end
turtle.down()
turtle.select(13)
turtle.placeUp()
  if turtle.detectDown() == false then
	repeat
turtle.down()
	until turtle.detectDown() == true
end
turtle.select(1)
end --burnTree()
--Main Script
checkFuel()
plantTree()
growTree()
checkFuel()
cutTree()
checkFuel()
dropOff()
burnTree()
remiX #2
Posted 01 January 2013 - 01:22 PM
I don't think there is a function to check if the turtle has a specific item in it's inventory.

Tip: Instead of typing turtle.forward() a lot, make a simple function:

function goForward(nTimes)
    for i = 1, (nTimes or 1) do -- I think this will work, if nTimes is nil, it will default to 1, not sure if it will work though, else just do 'if not nTimes then nTimes = 1 end' just before the loop
	    turtle.forward()
    end
end