My problem lies at line 257 and later upon calling it at 277. I'm telling the turtle to get an item to the east when it's facing north, and in theory, it should just turn left 3 times and then stop, but it doesn't. It just keeps on turning no matter what. No idea why. Maybe the current 'pos' is not accurate? I don't know.
Previous issue that is resolved.
Spoiler
I'm working on a (to be) ticker program to count off my resources, store them, etc. I'm going to have the turtles pull items out of nearby barrels so I don't have to worry about fuel and then transport them through an enderchest. Now what I'm working on right now is a very basic framework for the turtle to: 1. Remember it's position and orientation even with server restarts. 2. Have an easy method of controlling the turtle while still remembering it's position. Right now I'm working on the turning of the turtle, and after that is when I will be working on the server and other client portion of this project. I believe my problem lies in the fs.flush function but I can't seem to find proper documentation to figure out whether or not I'm using the proper syntax, but there are no errors with the program. It outputs "It exists, friend." as in the 'pos' file does, but it does not turn like I would expect it to with the test calls of the changeTurtle function at the end. No "written" is output, no turning occurs. No idea what's going on. Ideas would be appreciated, as well as if you have any ideas on how to optimize the code - I know in the turning function that could be worked out differently and more efficiently but I'm tired and my brain couldn't think of anything but to copy/paste and change the minor details.Spoiler
-- This is the turtle client code.
-- Just some prerequisite stuff.
rednet.open("left")
-- Reading, or if needed, creating the position file.
function getPosition()
if fs.exists("pos") == true then
print("It exists, friend.")
pos = fs.open("pos","r")
-- Putting the turtle on the games grid
x = pos.readLine()
y = pos.readLine()
z = pos.readLine()
-- values are as following: 1, west. 2, north. 3, east. 4, south
f = pos.readLine()
pos.close()
else
-- Getting the input Coords.
pos = fs.open("pos", "w")
print("Insert the current X, Y, Z, and F (Direction facing value.) respectively.")
write("X: ")
local inpX = read()
write("Y: ")
local inpY = read()
write("Z: ")
local inpZ = read()
write("F: ")
local inpF = read()
-- Writing the input coords.
pos.writeLine(inpX)
pos.writeLine(inpY)
pos.writeLine(inpZ)
pos.writeLine(inpF)
pos.close()
end
end
function changeTurtle(func, var)
-- Both changing the position and writing the new one to the 'pos' file.
-- exclusively for turning the turtle
local function turtleTurn(turnD)
-- turnD is either 'l' or 'r', simply for left or right.
if f == "1" and turnD == "r" then
getPosition()
turtle.turnRight()
f = 2
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "1" and turnD == "l" then
getPosition()
turtle.turnLeft()
f = 4
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "4" and turnD == "r" then
getPosition()
turtle.turnRight()
f = 1
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "4" and turnD == "l" then
getPosition()
turtle.turnLeft()
f = 3
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "3" and turnD == "r" then
getPosition()
turtle.turnRight()
f = 4
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "3" and turnD == "l" then
getPosition()
turtle.turnLeft()
f = 2
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "2" and turnD == "r" then
getPosition()
turtle.turnRight()
f = 3
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
elseif f == "2" and turnD == "l" then
getPosition()
turtle.turnLeft()
f = 1
pos = fs.open("pos","a")
pos.flush()
pos.writeLine(x)
pos.writeLine(y)
pos.writeLine(z)
pos.writeLine(newF)
pos.close()
print("Written.")
end
if func == "tturn" then
turtleTurn(var)
end
end
end
getPosition()
--serverCall()
-- test turns
sleep(1)
changeTurtle("tturn", "l")
sleep(1)
changeTurtle("tturn", "r")
sleep(1)
changeTurtle("tturn", "r")