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

Direwolf20's 'goto' Program

Started by TheHappyBukkit, 07 November 2012 - 06:24 AM
TheHappyBukkit #1
Posted 07 November 2012 - 07:24 AM
Here is direwolf20's goto program, used to move a turtle to an xyz position.
I DID NOT create this, I just grabbed it from one of direwolf20's recent videos.
Spoiler

local tArgs = {...}
local x = tonumber(tArgs[1])
local y = tonumber(tArgs[2])
local z = tonumber(tArgs[3])
local curx, cury, curz, dir

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 turtle.forward() do
         while not turtle.up() do
            turtle.digUp()
         end
      end
   end
end

function moveZ()
   distz = z - curz
   if (z < curz) then
      setDir(0)
   else
      setDir(2)
   end
   distz = math.abs(distz)
   --print(distz)

   for i = 1, distz do
      while not turtle.forward() do
         while not turtle.up() do
            turtle.digUp()
         end
      end
   end
end

function moveY()
   disty = y - cury
   disty = math.abs(disty)
   if (y < cury) then
      for i = 1, disty do
         while not turtle.down() do
            turtle.digDown()
         end
      end
   else
      for i = 1, disty do
         while not turtle.up() do
            turtle.digUp()
          end
       end
   end

end

--=====================--
if not x or not y or not z then
   print("Must supply 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 ("Current: "..curx..","..cury..","..curz)
--print ("Distance: "..distx..","..disty..","..distz)

moveX()
curx, cury, curz = getPos()
moveZ()
curx, cury, curz = getPos()
moveY()
curx, cury, curz = getPos()
print ("Current: "..curx..","..cury..","..curz)
rednet.close("right")
Leo Verto #2
Posted 07 November 2012 - 07:26 AM
Yay, direderp!
jag #3
Posted 07 November 2012 - 08:21 AM
Almost the entire forum is filled with the direarmy! >: D
TheHappyBukkit #4
Posted 07 November 2012 - 09:25 AM
Almost the entire forum is filled with the direarmy! >: D

River_Chief #5
Posted 07 November 2012 - 02:24 PM
Thanks for this!
jag #6
Posted 07 November 2012 - 05:22 PM
Almost the entire forum is filled with the direarmy! >: D

What is that?

EDIT: Oh wait it's your minecraft face..
TheHappyBukkit #7
Posted 08 November 2012 - 01:20 AM
Almost the entire forum is filled with the direarmy! >: D

What is that?

EDIT: Oh wait it's your minecraft face..

LOL! I didn't know if you would figure that out!
Leo Verto #8
Posted 08 November 2012 - 03:43 AM
Hey, back to topic please!
(Cruor's gonna kill me for stealing his job again)

Crush, kill, destroy, swag! ~Cruor
Edited on 08 November 2012 - 03:41 AM
jag #9
Posted 08 November 2012 - 04:13 AM
Hey, back to topic please!
(Cruor's gonna kill me for stealing his job again)

Crush, kill, destroy, swag! ~Cruor
I think it's awkward that I know where that comes from :/
trondaron #10
Posted 08 November 2012 - 04:16 AM
How do you guys feel about this rewrite of the setDir (setDirection) method?

http://pastebin.com/DRFgAwbz
Spoiler
local function setDir(toDir)
   local spinCount = math.abs(dir-toDir);
   local spin = ((toDir > dir and spinCount < 3) or (dir > toDir and spinCount > 2)) and turtle.turnRight or turtle.turnLeft;
   spinCount = (spinCount > 2) and 4-spinCount or spinCount;
   for i=1,spinCount do
	  spin();
   end
   dir = toDir;
end
AgentRenamon #11
Posted 08 November 2012 - 05:05 AM
How do you guys feel about this rewrite of the setDir (setDirection) method?

http://pastebin.com/DRFgAwbz
Spoiler
local function setDir(toDir)
   local spinCount = math.abs(dir-toDir);
   local spin = ((toDir > dir and spinCount < 3) or (dir > toDir and spinCount > 2)) and turtle.turnRight or turtle.turnLeft;
   spinCount = (spinCount > 2) and 4-spinCount or spinCount;
   for i=1,spinCount do
	  spin();
   end
   dir = toDir;
end

If it works, winner-winner, as far as I'm concerned…..