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

Decent goto program for turtle.

Started by YoureFunny, 06 September 2012 - 01:44 PM
YoureFunny #1
Posted 06 September 2012 - 03:44 PM
Basically you enter "goto <x> <y> <z>" and it goes to that position, it finds its own direction its facing and it will guide itself, hit me up if you have any improvements to add. Btw thanks for the help from everyone in "Ask a Pro"

local tArgs = {...}
local face = 0
local r = 1
rednet.open("right")
local cx,cy,cz = gps.locate(2,false)
if #tArgs ~= 3 then
  print("Incorrect usage of Function.")
  print("goto <x> <y> <z>")
else
local gx = tonumber(tArgs[1])
local gy = tonumber(tArgs[2])
local gz = tonumber(tArgs[3])
local x
local y
local z
function PL()
  print("Going to:")
  write(gx) write(" ") write(gy) write(" ") print(gz)
  end
function c_face()
if face == 5 then
  face = 1
elseif face == 0 then
  face = 4
end
end
function get_Face()
while turtle.forward() == false do
   if turtle.up() == false then
	turtle.digUp()
   end
  end

nx,ny,nz = gps.locate(3,false)
if nx > cx then
  face = 1
end
if nx < cx then
  face = 3
end
if nz > cz then
  face = 2
end
if nz < cz then
  face = 4
end
end
function calc()
x = nx - gx
y = ny - gy
z = nz - gz
end
function setDir(dir)
while dir ~= face do
  turtle.turnLeft()
  face = face - 1
  c_face()
  end
end
function f()
while not turtle.forward() do
   if not turtle.up()  then
	turtle.digUp()
   end
   y = y + 1
  end
end
function go()
if x < 0 then
  setDir(1)
  while x ~= 0 do
   f()
   x = x + 1
  end
end
if x > 0 then
  setDir(3)
  while x ~= 0 do
   f()
   x = x - 1
  end
end
if z < 0 then
  setDir(2)
  while z ~= 0 do
   f()
   z = z + 1
  end
end
if z > 0 then
  setDir(4)
  while z ~= 0 do
   f()
   z = z - 1
  end
end
while y < 0 do
  if turtle.up() ~= true then
   turtle.digUp()
  end
  y = y + 1
end
while y > 0 do
  if turtle.down() ~= true then
   turtle.digDown()
  end
  y = y - 1
end
end



tonumber(gx)
tonumber(gy)
tonumber(gz)
PL()
get_Face()
calc()
go()

end
deactivated1712 #2
Posted 06 September 2012 - 05:52 PM
Thanks :D/>/>
Been trying to make one myself for ages! Thanks!!!
etopsirhc #3
Posted 12 September 2012 - 08:26 AM
nice , i made one just recently as an api , you tell it to go to x,y,z and if it doesnt know what direction its facing it checks it then heads out untill it gets there
if breaker is turned off it wont break any blocks , but if its on it will break any block in the way.
if it needs to go up , it does to immedatly , but if it needs to go down it waits untill it gets to the right x,z
good for 3d printing type operations :)/>/>
slango20 #4
Posted 07 October 2012 - 01:37 PM
Put the code in spoiler tags
gloin366 #5
Posted 01 November 2012 - 11:03 PM
Can you add a pastebin link?
xInDiGo #6
Posted 23 January 2013 - 03:42 PM
how do we make this so it doesn't break blocks?
loepie #7
Posted 26 January 2013 - 09:58 AM
Hi EPIC program, helped me a lot.

since I could not find the pastebin link, I've made one.

All credits go to the author!!

again.. thanks!

http://pastebin.com/download.php?i=BC5tyiSV
Roonagooo #8
Posted 02 February 2013 - 11:51 PM
thank you for the program it works very nicely for me