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

Programming noob, help with gps program?

Started by nuclear Winter, 16 December 2015 - 05:43 AM
nuclear Winter #1
Posted 16 December 2015 - 06:43 AM
Hello! I am a noob at programming so if my questions are dumb i'm sorry! I need this goto program from direwolf20 to run after rebooting but I don't know how. I tried shell.run(goto) but it didnt do anything. I would also like to know how to make the turtle face either north south east or west. Here is the code:


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 = 1s
	  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")



thanks all!
Bomb Bloke #2
Posted 16 December 2015 - 08:45 AM
On startup, a turtle (or other ComputerCraft system) will boot any code found in a file called "startup" stored on the root of its drive.

Say Dire's code was saved as "goto", you might enter into the startup file:

shell.run("goto", x, y, z)

… replacing x/y/z with the position you wish the turtle to travel to.

You could add in an extra line under that:

setDir(dir)

… where dir would be 0 for north, 1 for west, 2 for south or 3 for east. (Speaking of which, that "dir = 1s" line is a typo; ditch the "s".)

If you're still stuck, I suggest providing some more info on what your goals are.
nuclear Winter #3
Posted 16 December 2015 - 06:57 PM
On startup, a turtle (or other ComputerCraft system) will boot any code found in a file called "startup" stored on the root of its drive.

Say Dire's code was saved as "goto", you might enter into the startup file:

shell.run("goto", x, y, z)

… replacing x/y/z with the position you wish the turtle to travel to.

You could add in an extra line under that:

setDir(dir)

… where dir would be 0 for north, 1 for west, 2 for south or 3 for east. (Speaking of which, that "dir = 1s" line is a typo; ditch the "s".)

If you're still stuck, I suggest providing some more info on what your goals are.

thank you very much nothing got stuck
nuclear Winter #4
Posted 16 December 2015 - 07:07 PM
On startup, a turtle (or other ComputerCraft system) will boot any code found in a file called "startup" stored on the root of its drive.

Say Dire's code was saved as "goto", you might enter into the startup file:

shell.run("goto", x, y, z)

… replacing x/y/z with the position you wish the turtle to travel to.

You could add in an extra line under that:

setDir(dir)

… where dir would be 0 for north, 1 for west, 2 for south or 3 for east. (Speaking of which, that "dir = 1s" line is a typo; ditch the "s".)

If you're still stuck, I suggest providing some more info on what your goals are.

But I just realized that when I place it down start up doesn't run until I open the gui, is there a fix for that?
Bomb Bloke #5
Posted 16 December 2015 - 11:31 PM
Newly placed systems are in their "off" state, so they won't launch their startup scripts until something occurs to turn them "on". Generally, yeah, that'll be you right-clicking them.

It is possible to have running computers/turtles turn on new turtles; say one turtle just placed another, it can then boot that turtle in front of it with:

peripheral.call("front", "turnOn")

If a running computer/turtle is in a world chunk that unloads, it should automatically turn itself back on when that chunk reloads.
Lupus590 #6
Posted 16 December 2015 - 11:38 PM
If a running computer/turtle is in a world chunk that unloads, it should automatically turn itself back on when that chunk reloads.

There is a known bug which sometimes prevents this behaviour though. There is no known cause or fix.
Bomb Bloke #7
Posted 17 December 2015 - 12:11 AM
Hence "should". ;)/> But newly placed systems shouldn't turn themselves on - that much is expected behaviour!