Posted 05 April 2014 - 03:48 AM
                Hey guys, here's the thing…I need a favor. I've been testing this code for days now and can't seem to figure out a solution. I need a fresh set of eyes to look at this thing. I've designed it to be an api as I intend to use it for turtle navigation later. The issue occurs when I tell the turtle to go to a specific y coordinate(testing on an "overworld" preset world). Instead of going to the y coordinate I designate, it just goes back to ground level. The code will not throw any errors at you, it just doesn't work properly. I don't know why.
Here's the code:
The only requirement for the program to function correctly is for the turtle to face south before you start calling functions. it will store any directional changes in the global "heading" variable. I am running the beta version of computercraft(1.6.1) and minecraft(1.6.4) if that helps.
I'd like to thank you all in advance for your time. Cheers!
                
            Here's the code:
--0 = south, 1 = west, 2 = north, 3 = east
--x = +east,-west
--z = -north,+south
--[[Global Variables]]--
xpos,ypos,zpos = gps.locate(10)
heading = 0
--[[functions]]--
function turnLeft()
turtle.turnLeft()
heading = heading - 1
    if heading == -1 then heading = 3 end
end
function turnRight()
turtle.turnRight()
heading = heading + 1
if heading == 4 then heading = 0 end
end
function faceDir(h)
if h >= 4 then h = 0 end
repeat turnRight() until heading == h
end
function forward(x)
local i = nil
x=tonumber(x)
for i=1,x do
turtle.forward()
gps.locate(10)
end
end
function up(x)
local i = nil
x=tonumber(x)
for i=1,x do
if not turtle.detectUp() then do
turtle.up()
end
xpos,ypos,zpos=gps.locate(10)
end
end
end
function down(x)
local i = nil
x=tonumber(x)
for i=1,x do
if not turtle.detectDown() then do turtle.down() end
xpos,ypos,zpos=gps.locate(10)
end
end
end
function checkFuel(x)
if turtle.getFuelLevel() > x then return false end
return true
end
function refuel(x)
turtle.select(16)
turtle.placeUp()
turtle.suckUp(x)
turtle.digUp()
turtle.select(1)
end
function goto(x,y,z)
for i = 1,10 do turtle.up() end
xpos,ypos,zpos=gps.locate(10)
if (xpos - x) > 0 then faceDir(1) else faceDir(3) end
if checkFuel(math.abs(xpos-x)) then do refuel() end end
    print("x - "..(math.abs(xpos-x)))
    forward(math.abs(xpos-x))
    xpos = xpos-x
if (zpos - z) > 0 then faceDir(2) else faceDir(0) end
    print("z - "..(math.abs(zpos-z)))
    forward(math.abs(zpos-z))
    zpos=zpos-z
print("facing direction: "..heading)
--for b=1,10 do turtle.down() end
print("moving y - "..(math.abs(ypos-y)))
if (ypos - y) > 0 then do up(math.abs(ypos-y)) end end
if (ypos - y) < 0 then do down(math.abs(ypos-y)) end end
xpos,ypos,zpos=gps.locate(10)
end
The only requirement for the program to function correctly is for the turtle to face south before you start calling functions. it will store any directional changes in the global "heading" variable. I am running the beta version of computercraft(1.6.1) and minecraft(1.6.4) if that helps.
I'd like to thank you all in advance for your time. Cheers!
