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

Unkown problem with navigation program

Started by lordofttude, 15 December 2015 - 11:01 PM
lordofttude #1
Posted 16 December 2015 - 12:01 AM
I'm having trouble getting this code to work as intended. I'm not getting any error messages, but when I run this navigation program the turtle won't actually move to the specified coords. Not really sure what is wrong here ….any help is appreciated.

Thanks in advance.


xCoord = -6
zCoord = 701
yCoord = 56

orientation = 4
orientations = {"north", "east", "south", "west"}

zDiff = {-1, 0, 1, 0}
xDiff = {0, 1, 0, -1}

function left()
	  orientation = orientation - 1
	  orientation = (orientation - 1) % 4
	  orientation = orientation + 1
	  turtle.turnLeft()
end
function right()
	  orientation = orientation - 1
	  orientation = (orientation + 1) % 4
	  orientation = orientation + 1
	  turtle.turnRight()
end
function moveForward()
	  xCoord = xCoord + xDiff[orientation]
	  zCoord = zCoord + zDiff[orientation]
	  turtle.dig()
	  moved = false
	  while not(moved) do
		    moved = turtle.forward()
	  end
end
function moveUp()
	  yCoord = yCoord + 1
	  turtle.digUp()
	  moved = false
	  while not(moved) do
		    moved = turtle.up()
	  end
end
function moveDown()
	  yCoord = yCoord - 1
	  turtle.digDown()
	  moved = false
	  while not(moved) do
		    moved = turtle.down()
	  end
end
function look(direction)
	  while direction ~= orientations[orientation] do
		    right()
	  end
end
function goto(xTarget, zTarget, yTarget)
	  while yTarget < yCoord do
		    moveDown()
	  end

	  while yTarget > yCoord do
		    moveUp()
	  end
	  if xTarget < xCoord then
		    look("west")
		    while xTarget < xCoord do
				  moveForward()
	  end
end
	  if xTarget > xCoord then
		    look("east")
		    while xTarget > xCoord do
				  moveForward()
	  end
end
	  if zTarget < zCoord then
		    look("north")
		    while zTarget < zCoord do
				  moveForward()
	  end
end

	  if zTarget > zCoord then
		    look("south")
		    while zTarget > zCoord do
				  moveForward()
	  end
   end
end

goto(-12, 706, 56)

Edited on 16 December 2015 - 12:07 AM
Bomb Bloke #2
Posted 16 December 2015 - 03:18 AM
What does the turtle do instead of "moving to the specified coords"? If it doesn't move at all, have you checked its fuel?

This sort of function will fail on any obstacle:

function moveUp()
          yCoord = yCoord + 1
          turtle.digUp()
          moved = false
          while not(moved) do
                    moved = turtle.up()
          end
end

… as once it enters the "while" loop, it'll simply keep trying to move without doing anything about whatever might be blocking it. Consider switching to something like:

function moveUp()
	yCoord = yCoord + 1
	while not turtle.up() do
		turtle.digUp()
		turtle.attackUp()
	end
end
lordofttude #3
Posted 16 December 2015 - 03:43 AM
Thanks! That fixed it from failing!