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

GPS system

Started by Xeriscape36, 29 January 2018 - 03:46 AM
Xeriscape36 #1
Posted 29 January 2018 - 04:46 AM
Hey guys, i was looking for a program that allows me to type coordinates to my turtle and make it go there, i looked at the forum posted in 2012 and its almost what i want, but it doesnt appear that there is a way to type something simple like, goto x= 25 y=66 z=101, if someone could point me into something that would do that, or if you could help me figure out how to do it with the previous gps code, that would be great

Link to original post: http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/
valithor #2
Posted 30 January 2018 - 12:57 PM
Assuming you have a gps cluster setup like what is described in the link you posted you can do this on a turtle (that has a wireless modem):


local x,y,z = gps.locate() --# getting the initial location of the turtle
turtle.forward()
local x2,y2,z2 = gps.locate() --# getting the new location of the turtle, so we can determine which direction the turtle is facing
local facing --# setting up a variable to hold the facing
if x2-x ~= 0  then
  if x2-x > 0 then --# we are facing positive x
    facing = 0 --# just saying positive x is the arbitrary value 0
  elseif x2-x < 0 then --# we are facing negative x
    facing = 2 --# just saying negative x is the arbitrary value 2
  end
else
  if z2-z > 0 then
    facing = 1
  elseif z2-z<0 then
    facing = 3
  end
end

--# getting input from user
print("What x coord would you like me to go to?")
local inputX = tonumber(read())
print("What y coord would you like me to go to?")
local inputY = tonumber(read())

print("What z coord would you like me to go to?")
local inputZ = tonumber(read())

local function face(newFacing)
  while facing~=newFacing do
    turtle.turnRight()
    facing = facing+1 --# adjusting facing to account for us turning
    facing=facing%4
  end
end

while(x2 ~= inputx) do
  if x2>inputX then
    face(2) --# facing the direction to go the negative direction
    turtle.forward()
    x2=x2-1 --# adjusting x2 
  elseif x2<inputX then
    face(0)
    turtle.forward()
    x2=x2+1
  end
end


while(z2 ~= inputx) do
  if z2>inputX then
    face(2)
    turtle.forward()
    z2=z2-1
  elseif z2<inputX then
    face(0)
    turtle.forward()
    z2=z2+1
  end
end

while y2~=inputY do
  if y2<inputY  then
    y2=y2+1
    turtle.up()
  elseif y2 > inputY then
    y2=y2-1
    turtle.down()
  end
end

This is completely untested, the general idea will work, but there might be typos in places. I have been doing a lot of c++ lately and the c++ syntax has been bleeding over into my lua lately.