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

tree cutter in anny squere

Started by magnus97, 05 April 2014 - 08:50 PM
magnus97 #1
Posted 05 April 2014 - 10:50 PM
this is the code if anny one want it (PS: pastebin code: JizVMrfX)
Spoiler

local sap
local tArgs = { ... }
local curx, cury, curz, dir
local x = --turtle location of home x
local y = --turtle location of home y
local z = --turtle location of home z
sap = tonumber(tArgs[1]) --the slot the saplings is inn
local function printUsage()
  print("Usages:")
  print("chop <<sap slot>>") --rename chop to what you have the program named!!!!!
end
if #tArgs < 1 then
  printUsage()
  return
end
function dig()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
end
function plant()
turtle.select(sap)
turtle.place()
turtle.back()
turtle.back()
turtle.back()
turtle.back()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.back()
turtle.turnRight()
end
function rDig()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.turnRight()
end
function rPlant()
turtle.select(sap)
turtle.place()
turtle.back()
turtle.back()
turtle.back()
turtle.back()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.back()
turtle.turnLeft()
end
function MRB()
turtle.turnLeft()
for i = 1, 4 do
  turtle.forward()
end
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end
function MRF()
turtle.turnRight()
for i = 1, 4 do
  turtle.forward()
end
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end
function returnHomeNGPS()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
for i = 1, 21 do
  turtle.forward()
end
turtle.turnRight()
end
function getPos()
return gps.locate(3)
end
function getDir()
local dir, x, y, z
x, y, z = getPos()
--print("Old:"..x..","..y..","..z)
while not turtle.forward() do
  while not turtle.up() do
   turtle.digUp()
  end
end
nx, ny, nz = getPos()
--print("New:"..nx..","..ny..","..nz)
if(x == nx) then
  if(nz > z) then
   dir = 2
  else
   dir = 0
  end
else
  if(nx > x) then
   dir = 3
  else
   dir = 1
  end
end
return dir
end
function setDir(toDir)
while toDir ~= dir do
  turtle.turnLeft()
  if dir == 3 then
   dir = 0
  else
   dir = dir + 1
  end
end
end
function moveX()
distx = x - curx
--print(distx)
if(x > curx) then
  setDir(3)
else
  setDir(1)
end
distx = math.abs(distx)
--print(distx)
for i = 1, distx do
  while not turlte.forward() do
   while not turtle.up() do
	turtle.digUp()
   end
  end
end
end
function moveZ()
distz = z - curz
--print(distz)
if(z < curz) then
  setDir(0)
else
  setDir(2)
end
distz = math.abs(distz)
--print(distz)
for i = 1, distz do
  while not turlte.forward() do
   while not turtle.up() do
	turtle.digUp()
   end
  end
end
end
function moveY()
disty = y - cury
disty = math.abs(disty)
--print(distx)
if(y < cury) then
  for i = 1, distx do
   while not turtle.down() do
	turtle.digDown()
   end
  end
else
  for i = 1, distx do
   while not turtle.up() do
	turtle.digUp()
   end
  end
end
end
function returnHomeGPS()
if not x or not y or not z then
print("Must suply X Y Z")
exit()
end
rednet.open("right")
--print(x..","..y..","..z)
dir = getDir()
curx, cury, curz = getPos()
distx = x - curx
disty = y - cury
distz = z - curz
--print("Curent: "..curx..","..cury..","..curz)
--print("Distance: "..distx..","..disty..","..distz)
moveX()
curx, cury, curz = getPos()
moveZ()
curx, cury, curz = getPos()
moveY()
curx, cury, curz = getPos()
--print("Curent: "..curx..","..cury..","..curz)
rednet.close("right")
end
for i = 1, 1 do
dig()
plant()
dig()
plant()
dig()
plant()
dig()
plant()
dig()
turtle.place()
returnHomeNGPS()
--returnHomeGPS() -- if this is going to be used use two -- in front of returnHomeNGPS
end


how the code is:

part one dig an plant function:

Spoiler

function dig()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
end
function plant()
turtle.select(sap)
turtle.place()
turtle.back()
turtle.back()
turtle.back()
turtle.back()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.back()
turtle.turnRight()
end
function rDig()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.turnRight()
end
function rPlant()
turtle.select(sap)
turtle.place()
turtle.back()
turtle.back()
turtle.back()
turtle.back()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.back()
turtle.turnLeft()
end
dig and rDig is nearly the same only rDig is the mirror direction of dig, the same is for plant and rPlant


part two the row mover:

Spoiler

function MRB()
turtle.turnLeft()
for i = 1, 4 do
  turtle.forward()
end
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end

function MRF()
turtle.turnRight()
for i = 1, 4 do
  turtle.forward()
end
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end

the MRB(aka Move Row Back) is moving the turtle to the left from where the row is, and the MRF(aka Move Row Forward) is moving the turle to the right from where the row is.



part tree home return options:

Spoiler

function returnHomeNGPS()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
for i = 1, 21 do
  turtle.forward()
end
turtle.turnRight()
end
function getPos()
return gps.locate(3)
end
function getDir()
local dir, x, y, z
x, y, z = getPos()
--print("Old:"..x..","..y..","..z)
while not turtle.forward() do
  while not turtle.up() do
   turtle.digUp()
  end
end
nx, ny, nz = getPos()
--print("New:"..nx..","..ny..","..nz)
if(x == nx) then
  if(nz > z) then
   dir = 2
  else
   dir = 0
  end
else
  if(nx > x) then
   dir = 3
  else
   dir = 1
  end
end
return dir
end
function setDir(toDir)
while toDir ~= dir do
  turtle.turnLeft()
  if dir == 3 then
   dir = 0
  else
   dir = dir + 1
  end
end
end
function moveX()
distx = x - curx
--print(distx)
if(x > curx) then
  setDir(3)
else
  setDir(1)
end
distx = math.abs(distx)
--print(distx)
for i = 1, distx do
  while not turlte.forward() do
   while not turtle.up() do
	turtle.digUp()
   end
  end
end
end
function moveZ()
distz = z - curz
--print(distz)
if(z < curz) then
  setDir(0)
else
  setDir(2)
end
distz = math.abs(distz)
--print(distz)
for i = 1, distz do
   while not turlte.forward() do
	while not turtle.up() do
	 turtle.digUp()
	end
   end
end
end
function moveY()
disty = y - cury
disty = math.abs(disty)
--print(distx)
if(y < cury) then
   for i = 1, distx do
	while not turtle.down() do
	 turtle.digDown()
   end
  end
else
  for i = 1, distx do
   while not turtle.up() do
	turtle.digUp()
	end
   end
end
end
function returnHomeGPS()
if not x or not y or not z then
print("Must suply X Y Z")
exit()
end
rednet.open("right")
--print(x..","..y..","..z)
dir = getDir()
curx, cury, curz = getPos()
distx = x - curx
disty = y - cury
distz = z - curz
--print("Curent: "..curx..","..cury..","..curz)
--print("Distance: "..distx..","..disty..","..distz)
moveX()
curx, cury, curz = getPos()
moveZ()
curx, cury, curz = getPos()
moveY()
curx, cury, curz = getPos()
--print("Curent: "..curx..","..cury..","..curz)
rednet.close("right")
end

the returnHomeNGPS is used if you only have one row, and the returnHomeGPS uses gps as it's oportunety to get to where it started from cuttin trees (the returnHomeGPS is based on Direwolf20's goto program)



part four the operating center:

Spoiler

for i = 1, 1 do
dig()
plant()
dig()
plant()
dig()
plant()
dig()
plant()
dig()
turtle.place()
returnHomeNGPS()
--returnHomeGPS() -- if this is going to be used use two -- in front of returnHomeNGPS
end

this is the control point of the program and the last bit of it it is here you set in dig(), plant(), rDig(), rPlant(), MRF() and MRB() to say what its going to do

feal free to ask me questions about the code if you did not under stand it and if you found anny bugs so i can make my program fully functional
leviathan01 #2
Posted 06 April 2014 - 05:29 AM
Some FOR loops might clean up the code.