Posted 13 April 2013 - 10:40 PM
Okay so I am VERY noobish at this Lua and Computercraft stuff and although I am NOT a programer I am pretty good with technical stuff…So I have been bashing my head against the wall on this one. I have a turtle program that keeps the turtle going forward until it detects a block at which point it "tests" to see if its a wood or grass (based on compare comands and slots 1-5 …1-4 are wood, 5 is grass). If it sees wood it fells it and moves up until no more wood then back down it goes and continues searching. If it sees grass it turns right or left, forward, and right or left ready to process the next row. it alternates between rights and lefts as to keep it in a pattern that will completely cover the intended area…think of it as mowing the grass…
Okay my problem is that IF it sees a tree because of how the code is it can cause a reverse in the "pattern" as it will skip to the next section after cutting the tree down. I think I need to find a way to have the pattern in a function of its own and call on the function to cut down trees as needed…but maybe there is a simplier way?
Here is the code in it's entirety:
Okay my problem is that IF it sees a tree because of how the code is it can cause a reverse in the "pattern" as it will skip to the next section after cutting the tree down. I think I need to find a way to have the pattern in a function of its own and call on the function to cut down trees as needed…but maybe there is a simplier way?
Here is the code in it's entirety:
function turtleFarmWood1() -- What this does is selects slot 1
while true do -- which has a wood type in it. If
turtle.select(1) -- the turtle has this type of wood
if turtle.compare() then -- in front of it, it will mine it
turtle.dig() -- and a space above the turtle and
turtle.digUp() -- then move upwards, otherwise it
turtle.up() -- breaks the loop. Wood2-4 do same
else -- thing but for slots 2-4 on turtle.
break
end
end
end
function turtleFarmWood2()
while true do
turtle.select(2)
if turtle.compare() then
turtle.dig()
turtle.digUp()
turtle.up()
else
break
end
end
end
function turtleFarmWood3()
while true do
turtle.select(3)
if turtle.compare() then
turtle.dig()
turtle.digUp()
turtle.up()
else
break
end
end
end
function turtleFarmWood4()
while true do
turtle.select(4)
if turtle.compare() then
turtle.dig()
turtle.digUp()
turtle.up()
else
break
end
end
end
function turtleDoneFarm() -- Once turtle has colected all the wood
while true do -- it will come bac down until is detects
turtle.select(5) -- slot 5 which will be grass. This will
if turtle.compareDown() then -- make sure the turtle comes to ground
break -- level.
else
turtle.digDown()
turtle.down()
end
end
end
function turtlelj() --This function combines the above functions into one
turtleFarmWood1()
turtleFarmWood2()
turtleFarmWood3()
turtleFarmWood4()
turtleDoneFarm()
end
function turtleLeft() -- Once our turtle detects a block in front it
while true do -- will test to see if it is wood (see above).
turtle.select(5) -- this will test to see if it is grass in front
if turtle.compare() then -- and force the turtle to turn around in the row
turtle.turnLeft() -- next to its current location.
turtlelj()
turtle.dig()
turtle.digUp()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
else
turtle.dig()
turtle.digUp()
break
end
end
end
function turtlelRight() -- this does the same as Left but goes right.
while true do
turtle.select(5)
if turtle.compare() then
turtle.turnRight()
turtlelj()
turtle.dig()
turtle.digUp()
turtle.forward()
turtle.digUp()
turtle.turnRight()
else
turtle.dig()
turtle.digUp()
break
end
end
end
while true do -- This is where it comes together. Loop
local fuel = turtle.getFuelLevel() -- entire program, loop the program while
while fuel > 100 do -- it has fuel to run.
while not turtle.detect() do -- no block in front then loop
if turtle.detectDown() then -- see if a block is beneath it if it is:
turtle.forward() -- go forward
else -- if there isnt a block bellow:
turtle.back() -- pretty self explanitory, go back, turn
turtle.turnRight() -- right check, for trees, dig forward and
turtlelj() -- up, go forward, dig above self, and turn
turtle.dig() -- right. At this point if it sees a block
turtle.digUp() -- it breaks the loop.
turtle.forward()
turtle.digUp()
turtle.turnRight()
if turtle.detect() then
break
end
end
end
turtlelj() -- above loop was broken because it found a
turtleRight() -- block in front so it runs tree test and
while not turtle.detect() do -- mines or find grass and goes right
if turtle.detectDown() then
turtle.forward() -- looping again and looking for blocks in front
else
turtle.back()
turtle.turnLeft()
turtlelj()
turtle.dig()
turtle.digUp()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
if turtle.detect() then
break
end
end
end
turtlelj() --same as before but this time it goes left
turtleLeft()
end
end