diff --git a/Turtle/movement.lua b/Turtle/movement.lua new file mode 100644 index 0000000..7005640 --- /dev/null +++ b/Turtle/movement.lua @@ -0,0 +1,250 @@ +function digForward() + while turtle.forward() ~= true do turtle.dig() end +end + +function detectTree() + if turtle.detect() == false then return false end + local has_block, data = turtle.inspect() + if data.tags["minecraft:logs"] == true then + return true + end + return false +end + +function plant4() + if countItem(Sappling) >= 4 then + digForward() + digForward() + turtle.turnLeft() + selectItem(Sappling) + turtle.place() + turtle.turnRight() + digBack() + selectItem(Sappling) + turtle.place() + turtle.turnLeft() + selectItem(Sappling) + turtle.place() + turtle.turnRight() + digBack() + selectItem(Sappling) + turtle.place() + return true + end + return false +end + +function goToStart(startPos) + d = getDif(startPos) + success = true + for i = 1, 3 do -- check if it's already at the right position + if d[i] ~= 0 then success = false end + end + if success == true then return true end + if d[2] > 0 then -- check if it's too high (too low won't be programmed since the turtle should never go below the startposition) + while d[2] > 0 do + if not turtle.detectDown then success = turtle.down() -- if no block is below the turtle tries to move down + else + if detectWoodDown() then + avoidWoodDown() + else + turtle.digDown() + success = turtle.down() + end + end + if success then d[2] = d[2] - 1 end -- if the turtle moved down then subtract 1 + + end + end + -- get x cords right + faceDir(4) + d = getDif(startPos) + while d[1] > 0 do + safeForward() + d = getDif(startPos) + end + turtle.turnRight() + turtle.turnRight() + while d[1] < 0 do + safeForward() + d = getDif(startPos) + end + d = getDif(startPos) + turtle.turnLeft() + while d[3] > 0 do + safeForward() + d = getDif(startPos) + end + turtle.turnLeft() + turtle.turnLeft() + while d[3] < 0 do + safeForward() + d = getDif(startPos) + end + turtle.turnRight() + success = true + for i = 1, 3 do -- check if it's already at the right position + if d[i] ~= 0 then success = false end + end + if success == true then return true end + return false +end + +function getDir() + local x, y, z = gps.locate() + local initPos = {x,y,z} + while turtle.forward() == false do + if turtle.detect() == true then + if detectWood() then + turtle.turnRight() + else + turtle.dig() + end + end + end + x, y, z = gps.locate() + digBack() + local newPos = {x,y,z} + if initPos[3]-newPos[3] == 1 then dir = 1 + elseif initPos[3]-newPos[3] == -1 then dir = 3 + elseif initPos[1]-newPos[1] == -1 then dir = 2 + else dir = 4 end + return dir + +end + +function faceDir(newDir, oldDir) + if (type(oldDir) ~= "number") then + oldDir = getDir() + end + i = (newDir - oldDir)%4 + while i > 0 do + turtle.turnRight() + i = i - 1 + end +end + +function digBack() + turtle.turnLeft() + turtle.turnLeft() + digForward() + turtle.turnRight() + turtle.turnRight() +end + +function digUp() + while turtle.up() ~= true do turtle.digUp() end +end + +function digDown() + while turtle.down() ~= true do turtle.digDown() end +end + +function moveTo(xTarget, yTarget, zTarget, dirTarget, dirCurrent) + local x, y, z = gps.locate() + local dir + if (type(dirCurrent) == "number") then + dir = dirCurrent + else + dir = getDir() + end + + local toGo + if (yTarget > y) then + toGo = yTarget -y + while (toGo > 0) do + digUp() + toGo = toGo - 1 + end + else + toGo = y - yTarget + while (toGo > 0) do + digDown() + toGo = toGo - 1 + end + end + + if (xTarget - x > 0) then + faceDir(2, dir) + dir = 2 + toGo = xTarget - x + else + faceDir(4, dir) + dir = 4 + toGo = x -xTarget + end + while toGo > 0 do + digForward() + toGo = toGo -1 + end + + if (zTarget - z > 0) then + faceDir(3, dir) + dir = 3 + toGo = zTarget -z + else + faceDir(1, dir) + dir = 1 + toGo = z - zTarget + end + while toGo > 0 do + digForward() + toGo = toGo -1 + end + faceDir(dirTarget, dir) +end + +function hoverTo(xTarget, yTarget, zTarget, dirTarget, dirCurrent) + local x, y, z = gps.locate() + local dir + if (type(dirCurrent) == "number") then + dir = dirCurrent + else + dir = getDir() + end + + local toGo + if (xTarget - x > 0) then + faceDir(2, dir) + dir = 2 + toGo = xTarget - x + else + faceDir(4, dir) + dir = 4 + toGo = x -xTarget + end + while toGo > 0 do + digForward() + toGo = toGo -1 + end + + if (zTarget - z > 0) then + faceDir(3, dir) + dir = 3 + toGo = zTarget -z + else + faceDir(1, dir) + dir = 1 + toGo = z - zTarget + end + while toGo > 0 do + digForward() + toGo = toGo -1 + end + + if (yTarget > y) then + toGo = yTarget -y + while (toGo > 0) do + digUp() + toGo = toGo - 1 + end + else + toGo = y - yTarget + while (toGo > 0) do + digDown() + toGo = toGo - 1 + end + end + + faceDir(dirTarget, dir) +end \ No newline at end of file