FireDemonite icon

Turtle go home with posXY

FireDemonite | PRO | 08/06/19 05:22:28 PM UTC | 0 ⭐ | 208 👁️ | Never ⏰ | []
text |

1.45 KB

|

None

|

0 👍

/

0 👎

rednet.open("left")
print("Started")
local directionX = 1
local directionY = 1
if not (fs.exists("./pos.txt")) then
    temp = fs.open("./pos.txt","w")
    temp.writeLine("0")
    temp.writeLine("0")
    temp.close()
end
file = fs.open("./pos.txt","r")
--line = file.readLine()
 pos = {X=0,Y=0}
pos.X = file.readLine()
pos.Y = file.readLine()
file.close()
print("Current X: "..pos.X)
print("Current Y: "..pos.Y)
 function turnAround()
    turtle.turnRight()
    turtle.turnRight()
end
 function forward(spaces)
    if spaces < 0 then
        spaces = spaces*-1
        turnAround()
        countX = -1
        --turtle.turnLeft()
    else
        countX = 1
        --turtle.turnRight()
    end
    for i = 1,spaces do
        turtle.forward()
        pos.X = pos.X+countX
    end
    if countX < 0 then
        turnAround()
    end
    file = fs.open("./pos.txt","w")
    file.writeLine(pos.X)
    file.writeLine(pos.Y)
    file.close()
end
 function up(spaces)
    if spaces < 0 then
        spaces = spaces*-1
        countY = -1
    else
        countY = 1
    end
    for i = 1,spaces do
        if countY > 0 then turtle.up() else turtle.down() end
        pos.Y = pos.Y+countY
    end
    file = fs.open("./pos.txt","w")
    file.writeLine(pos.X)
    file.writeLine(pos.Y)
    file.close()
end
 print("Moving to:\nX: 0 \nY: 0")
turtle.turnRight()
forward(0-pos.X)
up(0-pos.Y)
turtle.turnLeft()
 os.reboot()

Comments