This little script starts off by asking the turtle's current location, and stores it as .txt files.
To have movements reflected in this script you would use "forward()", "turn("south")", "turn("north")", "turn("east")", "turn("west")", "up()", and finally "down()"
Note: This script's movements are "descructive" (The turtle will destroy nearly everything to get from point A to point B)/>/>/>.
I'm updating the code most procedural AI tasks (ie. tree farming, mining, quarry building, etc), but this is only a start.
Lua is a new language to me, so if you've got tips, I'd love to hear.
details:
getPos() = get the position of turtle as table
setPos(table) = set position after movement the inputted numbers must be a table
forward() = move turtle forward (Note: it's destructive) and track position
turn("north") = turn, reflect this turn in forward and facing.txt
turn("south") = turn, reflect this turn in forward and facing.txt
turn("west") = turn, reflect this turn in forward and facing.txt
turn("east") = turn, reflect this turn in forward and facing.txt
the homeX, homeY, homeX, xPos, yPos, zPos, and facing are all stored in the root of the turtle's folder as .txt files.
I've also just added the ability to "go home"
To test this, save this file as "directions" and run it 5-or-so times. Now run "directions goHome" to watch the turtle make it back.
Spoiler
--[[
getFacing() SETTINGS:
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
--]]
--[[ START POS FUNCTIONS --]]
local args = {...}
local curPos = {}
local dir = "positionTracking/"
if not shell.run("mkdir positionTracking") then
end
function checkPos()
file = io.open(dir.."curPos.txt","r")
if file == nil then
return false
end
pos = file:read()
posTable = textutils.unserialize(pos)
if not posTable then
print("The coords file is a bit weird. Can't load from it.")
file:close()
return false
end
file:close()
return true
end
function setPos(pos)
file = io.open(dir.."curPos.txt", "w")
pos = textutils.serialize(pos)
file:write(pos)
file:close()
end
function getPos()
file = io.open(dir.."curPos.txt", "r")
pos = file.read("*all")
posTable = textutils.unserialize(pos)
if not posTable then
print("The coords file is a bit weird. Can't load from it.")
end
posTable[1] = tonumber(posTable[1])
posTable[2] = tonumber(posTable[2])
posTable[3] = tonumber(posTable[3])
file:close()
return(posTable)
end
function clearPos()
file = io.open(dir.."curPos.txt","w")
file:write("")
file:close()
end
--[[ STOP POS FUNCTIONS --]]
--[[ START HOME FUNCTIONS --]]
function checkHome()
file = io.open(dir.."homePos.txt","r")
if file == nil then
return false
end
pos = file:read()
posTable = textutils.unserialize(pos)
if not posTable then
print("The coords file is a bit weird. Can't load from it.")
file:close()
return false
end
file:close()
return true
end
function setHome(pos)
file = io.open(dir.."homePos.txt", "w")
pos = textutils.serialize(pos)
file:write(pos)
file:close()
end
function getHome()
file = io.open(dir.."homePos.txt", "r")
pos = file.read("*all")
posTable = textutils.unserialize(pos)
if not posTable then
print("The coords file is a bit weird. Can't load from it.")
end
posTable[1] = tonumber(posTable[1])
posTable[2] = tonumber(posTable[2])
posTable[3] = tonumber(posTable[3])
file:close()
return(posTable)
end
function clearHome()
file = io.open(dir.."homePos.txt","w")
file:write("")
file:close()
end
--[[ STOP HOME FUNCTIONS --]]
--[[ START FACING FUNCTIONS --]]
function checkFacing()
file = io.open(dir.."facing.txt","r")
if not file then
return false
end
file:close()
return true
end
function setFacing(facing)
file = io.open(dir.."facing.txt","w")
file:write(facing)
file:close()
end
function getFacing()
if not checkFacing() then
return false
end
file = io.open(dir.."facing.txt", "r")
facing = file:read()
file:close()
facing = tonumber(facing)
return(facing)
end
function clearFacing()
file = io.open(dir.."facing.txt","w")
file:write("")
file:close()
end
--[[ STOP FACING FUNCTIONS --]]
function turn(direction)
if direction == "north" then
toFace = 0
elseif direction == "east" then
toFace = 1
elseif direction == "south" then
toFace = 2
elseif direction == "west" then
toFace = 3
end
facing = tonumber(getFacing())
while facing ~= toFace do
turtle.turnRight()
facing = facing+1
if facing == 4 then
facing = 0
end
end
setFacing(toFace)
end
function forward()
facing = getFacing()
pos = getPos()
x = pos[1]
z = pos[2]
y = pos[3]
if facing == 0 then
z=z-1
elseif facing == 1 then
x=x+1
elseif facing == 3 then
x=x-1
elseif facing == 2 then
z=z+1
end
pos = {x, z, y}
setPos(pos)
while not turtle.forward() do
turtle.dig()
end
end
function down()
pos = getPos()
x = pos[1]
z = pos[2]
y = pos[3]
y = y-1
pos = {x, z, y}
setPos(pos)
while not turtle.down() do
turtle.digDown()
end
end
function up()
pos = getPos()
x = pos[1]
z = pos[2]
y = pos[3]
y = y+1
pos = {x, z, y}
setPos(pos)
while not turtle.up() do
turtle.digUp()
end
end
function goHome()
if not checkHome() then
print("Problem getting Home")
return false
end
if not checkPos() then
print("Problem getting Position")
return false
end
print("Retrived cords, moving now.")
home = getHome()
homeX = home[1]
homeZ = home[2]
homeY = home[3]
homeR = home[4]
pos = getPos()
posX = pos[1]
posZ = pos[2]
posY = pos[3]
facing = getFacing()
while posY < homeY do
up()
pos = getPos()
posY = pos[3]
end
while posY > homeY do
down()
pos = getPos()
posY = pos[3]
end
while posX < homeX do
turn("east")
forward()
pos = getPos()
posX = pos[1]
end
while posX > homeX do
turn("west")
forward()
pos = getPos()
posX = pos[1]
end
while posZ < homeZ do
turn("south")
forward()
pos = getPos()
posZ = pos[2]
end
while posZ > homeZ do
turn("north")
forward()
pos = getPos()
posZ = pos[2]
end
if homeR == 0 then
turn("north")
elseif homeR == 1 then
turn("east")
elseif homeR == 2 then
turn("south")
elseif homeR == 3 then
turn("west")
end
print("I am at home")
end
function main()
if not getFacing() or getFacing() == "" then
print("What direction am I facing?")
print("ie. north, east, south, west")
write("")
facing = read()
if facing == "north" then
toFace = 0
elseif facing == "east" then
toFace = 1
elseif facing == "south" then
toFace = 2
elseif facing == "west" then
toFace = 3
end
setFacing(toFace)
end
if not checkHome() then
print("What is my X position?")
write("")
curXPos = tonumber(read())
print("What is my Z position?")
write("")
curZPos = tonumber(read())
print("What is my Y position?")
write("")
curYPos = tonumber(read())
face = tonumber(getFacing())
home = {curXPos, curZPos, curYPos, face}
setHome(home)
end
if not checkPos() then
home = getHome()
xPos = home[1]
zPos = home[2]
yPos = home[3]
pos = {xPos, zPos, yPos}
setPos(pos)
end
--[[ Begin optional testing code --]]
forward()
forward()
forward()
forward()
down()
down()
turn("east")
forward()
forward()
up()
turn("south")
pos = getPos()
posX = pos[1]
posZ = pos[2]
posY = pos[3]
home = getHome()
homeX = home[1]
homeZ = home[2]
homeY = home[3]
print("My Position:")
print("X="..posX..", Z="..posZ..", Y="..posY.."")
print("My Home:")
print("X="..homeX..", Z="..homeZ..", Y="..homeY.."")
--[[ End optional testing code --]]
end
if args[1] and args[1] == "goHome" then
goHome()
else
main()
end
you can get this code onto your bot via "pastebin get 2DJPAtYp (name your file)" inside the turtle's/computer's console.
Edit: Friendly people let me know about code and spoiler tags.
Edit2: I've rewritten a good portion. Thanks to Bubba, I've added in tables to simplify, and make less resource intensive.
Hopefully this is turning out to be something :P/>/>