local homex,homey,homez =0,0,0
local cx,cy,cz = 0,0,0
local treeCount = 8
local currentFacing = 0
local treePoints = {}
local debug = false
local currentTree = 1
local sapplingsSlot = 2
local plantsaps = false
local sapsPlantedCount = 0
-- PLANT SAPS -------------------------------------
function PlantSapling()
-- plant sap
CheckSaps()
TurnRight(1)
GoForward(1)
turtle.digDown()
turtle.select(1)
turtle.placeDown()
turtle.back()
turtle.select(sapplingsSlot)
turtle.place()
IncSapCount()
TurnLeft(1)
end
-- QUICK TURN ----------------------------------
function Turn(direction)
if direction == 0 then
if currentFacing == 1 then
TurnLeft(1)
elseif currentFacing == 2 then
TurnRight(2)
elseif currentFacing == 3 then
TurnRight(1)
end
elseif direction == 1 then
if currentFacing == 2 then
TurnLeft(1)
elseif currentFacing == 3 then
TurnRight(2)
elseif currentFacing == 0 then
TurnRight(1)
end
elseif direction == 2 then
if currentFacing == 3 then
TurnLeft(1)
elseif currentFacing == 0 then
TurnRight(2)
elseif currentFacing == 1 then
TurnRight(1)
end
else -- direction 3
if currentFacing == 0 then
TurnLeft(1)
elseif currentFacing == 1 then
TurnRight(2)
elseif currentFacing == 2 then
TurnRight(1)
end
end
end
-- CHECK SAPS ----------------------------------
function CheckSaps()
term.clear()
if turtle.getItemCount(sapplingsSlot) < 1 then
print("I need more sapplings.")
print("Are they in my inventory yet? Press any key to continue.")
local event, param1 = os.pullEvent ("char")
end
end
-- TURNING -------------------------------------
function TurnRight(times)
-- turning right
for i=1,times do
turtle.turnRight()
currentFacing = currentFacing +1
if currentFacing == 4 then
currentFacing = 0
end
end
end
function TurnLeft(times)
-- turning left
for i=1,times do
turtle.turnLeft()
currentFacing = currentFacing -1
if currentFacing < 0 then
currentFacing = 3
end
end
end
-- MOVING -------------------------------------
function GoForward(times)
-- move some
for i=1,times do
if turtle.detect() then
turtle.dig()
end
turtle.forward()
end
end
-- DROP OFF ------------------------------------
function DropOff()
if AtHome ~= true then
GoHome()
end
-- face north
Turn(0)
-- move back
turtle.back()
-- drop off stuff
turtle.select(3)
local logs = turtle.getItemCount(3)
for i=4,logs do
redstone.setOutput("bottom", true)
sleep(0.3)
redstone.setOutput("bottom", false)
sleep(0.3)
end
for i=1,GetSapCount() do
redstone.setOutput("back", true)
sleep(0.3)
redstone.setOutput("back", false)
sleep(0.3)
end
TurnRight(2)
end
-- GET FACING----------------------------------
function GetFace()
local oldx,oldy,oldz = GetPos()
GoForward(1)
GetPos()
print("check direction")
if cx > oldx then
currentFacing = 3
elseif cx < oldx then
currentFacing = 1
elseif cy > oldy then
currentFacing = 0
else
currentFacing = 2
end
end
-- AT HOME CHECK -------------------------------
function AtHome()
GetPos()
if cx == homex then
if cy == homey then
if cz == homez then
return true
else
return false
end
else
return false
end
else
return false
end
end
-- GO HOME -------------------------------------
function GoHome()
GetPos()
while AtHome() ~=true do
if cy > homey then -- too far north
Turn(2)
print("facing south")
print("moving "..math.abs(cy-homey).."meters")
GoForward(math.abs(cy-homey)) --go south
if cx > homex then -- to far west
Turn(1)
print("facing east")
elseif cx < homex then -- too far east
Turn(3)
print("facing west")
end
print("moving "..math.abs(cx-homex).."meters")
GoForward(math.abs(cx-homex)) -- go e or w
Turn(0)
print("facing north")
elseif cy < homey then -- too far south
Turn(0)
print("facing north")
print("moving "..math.abs(cy-homey).."meters")
GoForward(math.abs(cy-homey)) -- go north
if cx > homex then -- too far east
Turn(3)
print("facing south")
elseif cx < homex then -- to far west
Turn(1)
print("facing north")
end
print("moving "..math.abs(cx-homex).."meters")
GoForward(math.abs(cx-homex)) --go e or w
Turn(0)
else -- already on right y axis
if cx > homex then -- too far east
Turn(1)
print("facing west")
elseif cx < homex then -- to far west
Turn(3)
print("facing east")
end
print("moving "..math.abs(cx-homex).."meters")
GoForward(math.abs(cx-homex)) --go e or w
Turn(0)
end
while cz > homez do
print("moving down")
turtle.down()
GetPos()
end
end
Turn(0)
end
-- GO To TREE ----------------------------------
function GoToTree(nu)
GetPos()
if cx < treePoints[nu]['x']+1 then
-- not on east side of it
Turn(3)
GoForward(math.abs(cx-treePoints[nu]['x'])+1)
elseif cx > treePoints[nu]['x']+1 then
Turn(1)
GoForward(math.abs(cx-treePoints[nu]['x'])-1)
end
if cy < treePoints[nu]['y'] then
-- not near it
Turn(0)
GoForward(math.abs(cy-treePoints[nu]['y']))
elseif cy > treePoints[nu]['y'] then
Turn(2)
GoForward(math.abs(cy-treePoints[nu]['y']))
end
end
-- CHECK WOOD ----------------------------------
function CheckWood()
Turn(1)
turtle.select(2) -- mob nudge error
if turtle.compare() ~= true then
turtle.place()
end
turtle.select(3)
if turtle.compare() then
MineWood()
end
return false
end
-- MINE WOOD -----------------------------------
function MineWood()
turtle.dig()
turtle.forward()
while turtle.detectUp() do
turtle.digUp()
turtle.up()
end
while turtle.detectDown() ~= true do
turtle.down()
end
turtle.back()
CheckSaps()
turtle.select(sapplingsSlot)
IncSapCount()
turtle.place()
end
-- SET TREE PLACES -----------------------------
function SetTreePoints(treeCount)
for i=1,treeCount do
table.insert(treePoints, {x=homex,y=(i*4)+homey,z=homez})
if debug == true then
print(i .. treePoints[i]['x']..":X "..treePoints[i]['y']..":Y "..treePoints[i]['z']..":Z")
end
end
end
-- GET POSITION -------------------------------
function GetPos()
cx,cy,cz = gps.locate(1,false)
sleep(0.2) -- make sure gps gets signal (i think)
print("I am at "..cx..":x "..cy..":y "..cz..":z")
return gps.locate(1,false)
end
-- ADD SAP COUNT -------------------------------
function IncSapCount()
sapsPlantedCount = sapsPlantedCount +1
end
-- GET SAP COUNT AND RESET ---------------------
function GetSapCount()
local tmp = sapsPlantedCount
sapsPlantedCount = 0
return tmp
end
-- END OF FUNCTIONs-----------------------------
function start(x, y, z, sap, count)
term.clear()
if peripheral.isPresent("right")~=true then
print("Requires Wireless Turtle")
else
rednet.open("right")
homex,homey,homez = gps.locate(2, false)
local argCount = 0
if x~=nil then
homex = x
else
argCount = argCount +1
end
if y~=nil then
homey = y
else
argCount = argCount +1
end
if z~=nil then
homez = z
else
argCount = argCount +1
end
if argCount ~= 3 or argCount ~=0 then
term.clear()
print("Requires none or all positions | treeAPI.start( [x,y,z] )")
print("e.g. treeAPI.start()")
print("or")
print("treeAPI.start(10,5,50)")
print(argCount)
end
print("Place Dirt in slot 1, sapplings in 2 and wood block in 3.")
if sap==nil and count ==nil then -- manual input
print("do you want to plant saplings first? Y/N")
local event, param1 = os.pullEvent ("char")
if param1 == "y" then
plantsaps = true
end
term.clear()
print("Planting "..treeCount.." saplings. is this correct? Y/N")
local event, param1 = os.pullEvent ("char")
if param1 == "n" then
print("How many do you want to plant?")
local event, param1 = os.pullEvent ("char")
print("will now plant "..param1)
treeCount = param1
end
end
GetFace()
GoHome()
SetTreePoints(treeCount)
if plantsaps == true then
for i=1,treeCount do
GoToTree(i)
PlantSapling()
end
end
-- MAIN LOOP --------------------------------
while true do
for i=1,treeCount do
GoToTree(i)
CheckWood()
end
-- avoiding plants
-- TurnLeft(1)
-- GoForward(1)
-- TurnRight(1)
-- GoForward(2)
-- TurnRight(1)
GoHome()
DropOff()
end
end
end