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).
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