function isFull()
  for i=1,16 do
    if turtle.getItemCount(i) == 0 then
      return false
    end
  end
  return true
end
 
function turnLeft(xDir,zDir)
  turtle.turnLeft()
  if xDir ~= nil and zDir ~= nil then
    xDir, zDir = -zDir, xDir
  end
  return xDir,zDir
end
 
function turnRight(xDir,zDir)
  turtle.turnRight()
  if xDir ~= nil and zDir ~= nil then
    xDir, zDir = zDir, -xDir
  end
  return xDir,zDir
end
 
function goForward(xPos,zPos,xDir,zDir,force)
  while turtle.forward() == false do
    if force == true or force == nil then
      if turtle.detect() == true then
        turtle.dig()
      else
        turtle.attack()
      end
    else
      turtle.attack()
    end
  end
  if xPos ~= nil and zPos ~= nil and xDir ~= nil and zDir ~= nil then
    xPos = xPos + xDir
    zPos = zPos + zDir
  end
  return xPos,zPos
end
 
function goDown(yPos,force)
  while turtle.down() == false do
    if force == true or force == nil then
      if turtle.detectDown() == true then
        turtle.digDown()
      else
        turtle.attackDown()
      end
    else
      turtle.attackDown()
    end
  end
  if yPos~= nil then
    yPos = yPos - 1
  end
  return yPos
end
 
function goUp(yPos,force)
  while turtle.up() == false do
    if force == true or force == nil then
      if turtle.detectUp() == true then
        turtle.digUp()
      else
        turtle.attackUp()
      end
    else
      turtle.attack()
    end
  end
  if yPos ~= nil then
    yPos = yPos + 1
  end
  return yPos
end
 
function checkFuel(x,y,z,xPos,yPos,zPos)
  local required = math.abs(x-xPos) + math.abs(y-yPos) + math.abs(z-zPos) + 1
  while true do
    if turtle.getFuelLevel() >= required then
      return true
    else
      for i=1,16 do
        turtle.select(i)
        if turtle.refuel() == true then
          break
        elseif turtle.refuel() == false and i == 16 then
          return false
        end 
      end   
    end
  end
end
 
function xGoto(x,xPos,zPos,xDir,zDir,force)
  while true do
    if checkFuel(x,0,0,xPos,0,0) == true then
     
      while x > xPos do
        while xDir ~= 1 do
          xDir,zDir = turnLeft(xDir,zDir)
        end
        xPos,zPos = goForward(xPos,zPos,xDir,zDir,force)
      end
     
      while x < xPos do
        while xDir ~= -1 do
          xDir,zDir = turnLeft(xDir,zDir)
        end
        xPos,zPos = goForward(xPos,zPos,xDir,zDir,force)
      end
     
      if x == xPos then
        return xPos,zPos,xDir,zDir
      end
   
    else
      print("Need fuel. Type 'refuel' to continue.")
      while ready ~= "refuel" do
        ready = read()
      end
    end
  end
end
 
function zGoto(z,xPos,zPos,xDir,zDir,force)
  while true do
    if checkFuel(0,0,z,0,0,zPos) == true then
     
      while z > zPos do
        while zDir ~= 1 do 
          xDir,zDir = turnLeft(xDir,zDir)
        end
        xPos,zPos = goForward(xPos,zPos,xDir,zDir,force)
      end
     
      while z < zPos do
        while zDir ~= -1 do
          xDir,zDir = turnLeft(xDir,zDir)
        end
        xPos,zPos = goForward(xPos,zPos,xDir,zDir,force)
      end
     
      if z == zPos then
        return xPos,zPos,xDir,zDir
      end
     
    else
      print("Need fuel. Type 'refuel' to continue.")
      while ready ~= "refuel" do
        ready = read()
      end
    end
  end
end
 
function yGoto(y,yPos,force)
  while true do
    if checkFuel(0,y,0,0,yPos,0) == true then
     
      while y > yPos do
        yPos = goUp(yPos,force)
      end
     
      while y < yPos do
        yPos = goDown(yPos,force)
      end
   
      if y == yPos then
        return yPos
      end
   
    else
      print("Need fuel. Type 'refuel' to continue.")
      while ready ~= "refuel" do
        ready = read()
      end
    end
  end
end
 
function goto(x,y,z,xFace,zFace,xPos,yPos,zPos,xDir,zDir,force,order)
  if checkFuel(x,y,z,xPos,yPos,zPos) == true then
    if order[1] == "x" then
      xPos,zPos,xDir,zDir = xGoto(x,xPos,zPos,xDir,zDir,force)
      if order[2] == "y" then
        yPos = yGoto(y,yPos,force)
        xPos,zPos,xDir,zDir = zGoto(z,xPos,zPos,xDir,zDir,force)
      else
        xPos,zPos,xDir,zDir = zGoto(z,xPos,zPos,xDir,zDir,force)
        yPos = yGoto(y,yPos,force)
      end
    elseif order [1] == "y" then
      yPos = yGoto(y,yPos,force)
      if order[2] == "x" then
        xPos,zPos,xDir,zDir = xGoto(x,xPos,zPos.xDir,zDir,force)
        xPos,zPos,xDir,zDir = zGoto(z,xPos,zPos,xDir,zDir,force)
      else
        xPos,zPos,xDir,zDir = zGoto(z,xPos,zPos,xDir,zDir,force)
        xPos,zPos,xDir,zDir = xGoto(x,xPos,zPos,xDir,zDir,force)
      end
    else
      xPos,zPos,xDir,zDir = zGoto(z,xPos,zPos,xDir,zDir,force)
      if order[2] == "x" then
        xPos,zPos,xDir,zDir = xGoto(x,xPos,zPos,xDir,zDir,force)
        yPos = yGoto(y,yPos,force)
      else
        yPos = yGoto(y,yPos,force)
        xPos,zPos,xDir,zDir = xGoto(x,xPos,zPos,xDir,zDir,force)
      end
    end
    print("xFace: "..xFace.." zFace: " ..zFace)
    print("xDir: "..xDir.." zDir: "..zDir)
    while xFace ~= xDir or zFace ~= zDir do
      xDir,zDir = turnLeft(xDir,zDir)
    end
    return xPos,yPos,zPos,xDir,zDir
  else
    print("Need fuel. Type 'refuel' to continue.")
    while ready ~= "refuel" do
      ready = read()
    end
  end         
end