CC: Tweaked

CC: Tweaked

57M Downloads

Turtle stops when moving forward in random moments on server

wiktormalyska opened this issue · 2 comments

commented

Minecraft Version

1.20.x

Version

1.108.4

Details

No logs on server and on client

code:

-- Declare variables
startPos = {x = 0, y = 0, z = 0}
endPos = {x = 0, y = 0, z = 0}

-- Move
function goForward()
  mineForward()
  turtle.forward()
end

function goBack()
  turnRight()
  turnRight()
  goForward()
  turnRight()
  turnRight()
end

function goUp()
  mineUp()
  turtle.up()
end

function goDown()
  mineDown()
  turtle.down()
end

-- Rotate
function turnLeft()
  turtle.turnLeft()
end

function turnRight()
  turtle.turnRight()
end

function faceNorth()
  -- Move the turtle forward to get its new position
  local pos1 = getPosition()
  turtle.forward()
  local pos2 = getPosition()
  turtle.back()

  -- Determine direction based on movement
  if pos2[1]>pos1[1] and pos2[3]==pos1[3] then
    -- Facing East
      turnLeft()
  end
  if pos2[1]<pos1[1] and pos2[3]==pos1[3] then
    -- Facing West
      turnRight()
  end
  if pos2[1]==pos1[1] and pos2[3]>pos1[3] then
    -- Facing South
      turnRight()
      turnRight()
  end
end

-- Dig
function mineForward()
  while turtle.detect() do
    turtle.dig()
  end
end

function mineUp()
  while turtle.detectUp() do
    turtle.digUp()
  end
end

function mineDown()
  while turtle.detectDown() do
    turtle.digDown()
  end
end

-- Utils
function getPosition()
	local x, y, z = gps.locate()
  if x == nil then
    print("GPS not found. Waiting for GPS...")
    while x == nil do
      x, y, z = gps.locate()
      os.sleep(1)
    end
  end
	local pos = {x,y,z}
	return pos
end

function refuel()
  if turtle.getFuelLevel() > 30 then
    return true
  end
  turtle.select(1)
  while turtle.getFuelLevel() < 100 do
    if not turtle.refuel(1) then
      print("No fuel in slot. Waiting for fuel...")
      while not turtle.refuel(1) do
        os.sleep(1) -- wait for 1 second
      end
    end
  end
  return true
end

function moveTo(x, y, z)
  local pos = getPosition()

  -- Calculate the distance to move in each direction
  local deltaX = x - pos[1]
  local deltaY = y - pos[2]
  local deltaZ = z - pos[3]

  -- Move in the Z direction (North/South)
  if deltaZ > 0 then
    -- Move South
    turnRight()
    turnRight()
    for i = 1, deltaZ do
      goForward()
    end
    turnRight()
    turnRight()
  elseif deltaZ < 0 then
    -- Move North
    for i = 1, -deltaZ do
      goForward()
    end
  end

  -- Move in the X direction (East/West)
  if deltaX > 0 then
    -- Move East
    turnRight()
    for i = 1, deltaX do
      goForward()
    end
    turnLeft()
  elseif deltaX < 0 then
    -- Move West
    turnLeft()
    for i = 1, -deltaX do
      goForward()
    end
    turnRight()
  end

  -- Move in the Y direction (Up/Down)
  if deltaY > 0 then
    -- Move Up
    for i = 1, deltaY do
      goUp()
    end
  elseif deltaY < 0 then
    -- Move Down
    for i = 1, -deltaY do
      goDown()
    end
  end
end


function setup()
  print("Enter Start position X")
  startPos.x = tonumber(read())
  print("Enter Start position Y")
  startPos.y = tonumber(read())
  print("Enter Start position Z")
  startPos.z = tonumber(read())

  print("Enter End position X")
  endPos.x = tonumber(read())
  print("Enter End position Y")
  endPos.y = tonumber(read())
  print("Enter End position Z")
  endPos.z = tonumber(read())
  
  print("Fuel slot is 1")
  read()
end

function switchPosition()
  if startPos.x > endPos.x then startPos.x, endPos.x = endPos.x, startPos.x end
  if startPos.y > endPos.y then startPos.y, endPos.y = endPos.y, startPos.y end
  if startPos.z < endPos.z then startPos.z, endPos.z = endPos.z, startPos.z end
end

function printPos(pos)
  
end

function mineLine()
  local pos = getPosition()
  moveTo(pos[1], pos[2], endPos.z)
  moveTo(pos[1], pos[2], pos[3])
end

function nextLine()
  turnRight()
  goForward()
  turnLeft()
end

function mineFloor()
  local pos = getPosition()
  local line = 0
  while pos[1] <= endPos.x do
    line = line + 1
    print("Mining line: " .. line)
    mineLine()
    if pos[1] < endPos.x then
      nextLine()
    end
  end
  moveTo(pos[1], pos[2], pos[3])
end

function startMining()
  print("Mining started")
  local pos = getPosition()
  while pos[2]<=endPos.y do
    mineFloor()
    goUp()
    pos = getPosition()
  end
  mineFloor()
end

-- Main function
function main()
  setup()
  switchPosition()
  refuel()
  faceNorth()
  moveTo(startPos.x, startPos.y, startPos.z)
  startMining()
  moveTo(startPos.x, startPos.y, startPos.z)
  print("Mining complete")
end

-- Start 
main()
commented

Can you describe a bit more what issue you are experiencing? Or provide a video of it?

commented