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

GPS Enabled Forester

Started by BlackDragon, 14 July 2012 - 03:52 PM
BlackDragon #1
Posted 14 July 2012 - 05:52 PM
I couldn't find a decent Forester turtle so I made this little script. I'm currently using tekkit and works perfectly.

Features:
Uses GPS to find and locate tree spots.
Designed to be fully automated.
Simple to use.
Redstone output bottom for each log collected. @ homepoint
Redstone output behind for each sapling used. @ homepoint

Installing:
Place the BDAPI in your "ComputerCraft/lua/rom/apis/" folder
done :P/>/>

Simple Use:
1. Make sure you have GPS hosts so your turtle can get its position. (min 4)
2. Place your turtle inside your GPS area.
3. Place Dirt in slot 1, Saplings in slot 2 and wood(matching sapling type) in slot 3.
4. Type lua and hit return.
5. Type BDAPI.start().

Your turtle will now ask if you need to plant saplings. Hit "Y" and you will be asked if you wish to plant 8.
Either hit "Y" to accept 8 or "N" and the number your want to plant to continue.

Your turtle will now collect wood until its full or out of saplings.
If the turtle is out of saplings it will wait for you to add more. Just hit a key to continue.

Advanced setup: (fully autonomous) (tekkit)
  1. Place filter at your desired home point.
  2. Add a log to match the tree type.
  3. Pipe it to a chest.
  4. Place an ejector(or similar) behind the Start point piped to a sapling supply chest.(or full of saplings)
  5. Place your turtle on the start point.
  6. Type "gps locate" and get the co-ords. ( x, y, z )
  7. Type edit startup
  8. Type BDAPI.start(x,y,z,2,[amount to plant]) e.g. BDAPI.start(150,200,70,2,12)
  9. Press control and then right and hit enter to save.
  10. Place the required Items as above in Simple Use method.
  11. Now reboot the Turtle.
  12. Use water to funnel saplings dropped into the ejector supply chest.
  13. Done.
If you download this txt file make sure you remove the .txt when placing in the API folder.

Spoiler

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
MathManiac #2
Posted 14 July 2012 - 05:56 PM
Great Job! :P/>/>
Ponder #3
Posted 15 July 2012 - 07:43 PM
One thing is not really clear for me, how exactly does it refill its sapplings?
BlackDragon #4
Posted 16 July 2012 - 03:33 AM
You can set up a dispenser like an ejector from the redpower mod behind the homePoint, the turtle will send out a redstone signal at the back for each sapling used between drop offs.
Ponder #5
Posted 17 July 2012 - 11:47 PM
Ah, kay. Thanks.