Posted 23 September 2012 - 08:03 PM
This is my goto x y z program. It only works once, the first time it runs on a turtle it works fine but the next time it runs it does not work. The second time it runs it not an error but it does go anywhere on the x, z axis
tArgs = {...}
face = 0
local up = 0
local fuel
local gx = tonumber(tArgs[1]) --target x pos
local gy = tonumber(tArgs[2]) --target y pos
local gz = tonumber(tArgs[3]) --target z pos
local x, y, z --defines the amount to move
function prepare() --setup
rednet.open("right") --activate right modem
cx,cy,cz = gps.locate(3) -- sets current x, y, z
if #tArgs ~= 3 then -- if incorrect amount of args
print("Please input x, y, z coordinates")
end
fuel = turtle.getFuelLevel() -- the fuel level of robot
print("Fuel Level: "..fuel)
if fuel < 1000 then -- random # choice should be enough for long distance
print("Fuel Level might be too low")
print("Would you like to refuel? yes/no")
input = read() --looking at user input
if input == "yes" then
turtle.select(1)
print("Place fuel source in selected slot")
turtle.refuel(20) --refuel with 20
print("Refuelling")
fuel = turtle.getFuelLevel() -- rechecking fuel level
print("Fuel Level is now: ".. fuel)
else
print("Refueling Canceled") -- if person type no
end
else
print("There should be enough fuel for the travel")
print("No refuelling necessary")
end
print("Current position: "..cx..", "..cy..", "..cz) --print cur pos
print("Going to: "..gx..", "..gy..", "..gz) --print target pos
end
function currentFace() --find what pos the turtle is facing
while not turtle.forward() do -- try go forward
if not turtle.up() then -- try to do up
turtle.digUp() -- dig up
end
end
local nx,ny,nz = gps.locate(3) --take new pos
print("New position: "..nx..", "..ny..", "..nz)
if cx < nx then -- this determines where the turtle is facing
face = 3
elseif cx > nx then
face = 1
elseif cz < nz then
face = 0
elseif cz > nz then
face = 2
end
print("I am facing: "..face)
end
function turnToFace(f) --turn until facing desired direction
if face ~= f then
while face ~= f do
turtle.turnRight()
os.sleep(.1)
face = face + 1
if face == 5 then
face = 0
end
end
else
print("Facing the right way")
end
end
function determineFaceX() -- determine the desired way to face on the x axis (1st step)
nx,ny,nz = gps.locate(3)
if gx < nx then
turnToFace(1)
elseif gx > nx then
turnToFace(3)
end
end
function determineFaceZ() -- determine the desired way to face on the z axis (2st step)
if gz < nz then
turnToFace(2)
elseif gz > nz then
turnToFace(4)
end
end
function determineMoveX() -- determine how much to move on the x axis
x = gx - nx
if x < 0 then
x = -x
end
end
function determineMoveY() -- determine how much to move on the y axis
y = gy - ny
end
function determineMoveZ() -- determine how much to move on the z axis
z = gz - nz
if z < 0 then
z = -z
end
end
function move (m) -- the actual movement on x and z axis
if x ==0 or z == 0 then
print("Don't need to move")
else
for i=1, m do
while not turtle.forward() do
up = up + 1
if not turtle.up() then
turtle.digUp()
end
end
end
end
end
function moveY (:P/>/> -- the actual movement on y axis
--print(y)
if y > 0 then
for i=1, y do
turtle.up()
end
elseif y < 0 then
for i = 1,-y do
while not turtle.down() do
turtle.digDown()
end
end
end
for p=1, up do
turtle.down()
end
end
-- calling all necessary function
prepare()
currentFace()
determineFaceX()
determineMoveX()
move(x)
determineFaceZ()
determineMoveZ()
move(z)
determineMoveY()
moveY(y)