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

Direwolf20's Goto Script

Started by deactivated1712, 05 September 2012 - 10:58 AM
deactivated1712 #1
Posted 05 September 2012 - 12:58 PM
Hi,
I watched Direwolf20's SMP videos, and if you haven't seen it, he makes a program in which you type the x y z coords of where you want the turtle to go and it will go there. I copied what was in the video ( :D/>/> ) and here is what I got:


local tArgs = {...}
local x = tonumber(tArgs[1])
local y = tonumber(tArgs[2])
local z = tonumber(tArgs[3])
local curx, cury, 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..","..
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 - cury
disty = y - cury



I am not great at Lua (yet!) and I can't figure out what is wrong with it. Currently, it says unexpected symbol on line 22.

H
Grim Reaper #2
Posted 05 September 2012 - 02:39 PM
At line 19 a print statement trails of with a concatenation operator, but it is never given a string to concatenate.

-- Line 19
print("New: "..nx..","..ny..","..
The LUA interpreter was expecting another string or parenthesis without that final: ..

I am assuming that you wanted to concatenate the value 'nz' into that print statement, so I took the liberty of adding that small adjustment to the script.
Spoiler


local tArgs = {...}
local x = tonumber(tArgs[1])
local y = tonumber(tArgs[2])
local z = tonumber(tArgs[3])
local curx, cury, 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) -- Here is where I added the change. Prev code: print("New: "..nx..","..ny..","..
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 - cury
disty = y - cury
deactivated1712 #3
Posted 05 September 2012 - 04:08 PM
Thanks, getting a few more errors, but I am going through them now…
deactivated1712 #4
Posted 05 September 2012 - 04:11 PM
Right, fixed a few other things, but not it says 106: attempt to perform arithmetic __sub on nil and number (on Grim Reapers edited script, also, changed

distx = x - cury
disty = y - cury
To

distx = x - curx
disty = y - cury
kazagistar #5
Posted 06 September 2012 - 08:08 PM
The problem is that either x or cury are nil, and not a number, and you cant do math with a nil. Put a "print(x, " ", cury) on the line before it and see what each variable is before they enter that part of code.
dustrider #6
Posted 21 December 2012 - 01:24 PM
You are missing the lines that calls the functions. Since I cannot edit your pastebin i've added it to mine.

http://pastebin.com/mudkzHDE

NOTE: I did not take the time to cleanup the spaces in the code so you can if you want LOL. This works as of TurtleOS 1.4



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) -- Here is where I added the change. Prev code: print("New: "..nx..","..ny..","..
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 - cury
disty = y - cury
moveX()
curx, cury, curz = getPos()
moveZ()
curx, cury, curz = getPos()
moveY()
curx, cury, curz = getPos()
print("Current: "..curx..","..cury..","..curz.."")
rednet.close("right")