-- Turtle autosorter with the items.txt file
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
itemFile = fs.open("./items.txt","r")
items = textutils.unserialise(itemFile.readAll())
itemFile.close()
file = fs.open("./pos.txt","r")
--line = file.readLine()
pos = {X=0,Y=0}
home = {X=1,Y=6}
drive = {X=0,Y=3}
posInBetween = {X=0,Y=6}
posInBetween2 = {X=-1,Y=6}
posInBetweenDrive = {X=1,Y=3}
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
while not turtle.forward() do end
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 while not turtle.up() do end else while not turtle.down() do end end
pos.Y = pos.Y+countY
end
file = fs.open("./pos.txt","w")
file.writeLine(pos.X)
file.writeLine(pos.Y)
file.close()
end
function goPos(position,bool)
if not (bool) then while not turtle.back() do end end
turtle.turnRight()
up(position.Y-pos.Y)
forward(position.X-pos.X)
--up(position.Y-pos.Y)
turtle.turnLeft()
if not (bool) then while not turtle.forward() do end end
end
goPos(posInBetween2)
goPos(posInBetween,true)
goPos(home,true)
while true do
file = fs.open("items.txt","r")
items = textutils.unserialise(file.readAll())
file.close()
while not turtle.suck() do end
itemFound = false
detail = turtle.getItemDetail(1)
print(detail.name.."\n"..detail.damage)
for i,v in pairs(items) do
--print(items[i].detail.name)
if (items[i].detail.name == detail.name) then
--print("SUCCESSFUL")
if (items[i].detail.damage == detail.damage) then
--print("YESH")
itemFound = true
itemReset = false
goPos(items[i])
turtle.drop()
up(1)
goPos(posInBetween2)
goPos(posInBetween,true)
goPos(home,true)
end
end
end
if itemReset then
print("make new one")
rednet.send(819,detail,"Tsorter")
itemReset = false
id,msg = rednet.receive("Tsorter")
goPos({X=msg.X,Y=msg.Y})
turtle.drop()
rednet.send(id,"DONE","Tsorter")
id,confirm = rednet.receive("Tsorter")
goPos(posInBetween2)
goPos(posInBetween,true)
goPos(home,true)
end
if not itemFound then
goPos(posInBetweenDrive,true)
goPos(drive,true)
shell.run("delete","items.txt")
shell.run("copy","disk/items.txt","items.txt")
goPos(posInBetween2)
goPos(posInBetween,true)
goPos(home,true)
itemReset = true
turtle.drop()
end
end
Comments