Posted 29 September 2012 - 12:14 PM
OK, this one more difficult, I think… basically, I have two questions.
This is the "navi" api - simple turtle navigation:
And I don't like it. That goTo function is ugly… I have written it that way because I wanted turtle to follow the same path if I have it move to one location an then move it back to the initial location. Is there any way to clean this code up a bit?
And the other one: I am trying to make an excavation program using that API. It seems like if I call navi.simpleForward (), it doesn't update the turtle coordinates. Am I doing wrong something?
This is the "navi" api - simple turtle navigation:
Spoiler
x = 0
y = 0
z = 0
xDir = 1
zDir = 0
function simpleForward ()
if turtle.forward () then
x = x + xDir
z = z + zDir
return true
else
return false
end
end
function simpleUp ()
if turtle.up () then
y = y + 1
return true
else
return false
end
end
function simpleDown ()
if turtle.down () then
y = y - 1
return true
else
return false
end
end
function destructiveForward ()
while not simpleForward () do
if turtle.detect () then
if not turtle.dig () then
return false
end
else
turtle.attack ()
end
end
return true
end
function destructiveUp ()
while not simpleUp () do
if turtle.detectUp () then
if not turtle.digUp () then
return false
end
else
turtle.attackUp ()
end
end
return true
end
function destructiveDown ()
while not simpleDown () do
if turtle.detectDown () then
if not turtle.digDown () then
return false
end
else
turtle.attackDown ()
end
end
return true
end
up = simpleUp
down = simpleDown
forward = simpleForward
function setMode (mode)
if mode == "simple" then
up = simpleUp
down = simpleDown
forward = simpleForward
return true
elseif mode == "destructive" then
up = destructiveUp
down = destructiveDown
forward = destructiveForward
return true
else
return false
end
end
function turnLeft ()
turtle.turnLeft ()
xDir, zDir = zDir, -xDir
end
function turnRight ()
turtle.turnRight ()
xDir, zDir = -zDir, xDir
end
function turnTo (xd, zd)
if zd+zDir == 0 and xd+xDir == 0 then
turnLeft ()
turnLeft ()
elseif xDir == -zd and zDir == xd then
turnLeft ()
elseif xDir == zd and zDir == -xd then
turnRight ()
end
end
function refuelSlot (amount, slot)
if turtle.getFuelLevel () >= amount then
return true
end
if turtle.getItemCount (slot) > 0 then
turtle.select (slot)
while turtle.getFuelLevel () < amount do
if not turtle.refuel (1) then
turtle.select (1)
return false
end
end
turtle.select (1)
return true
else
turtle.select (1)
return false
end
end
function refuelAll (amount)
for i = 1,16 do
if refuelSlot (amount, i) then
return true
end
end
return false
end
function goTo (nx, ny, nz, nxd, nzd)
if y < ny then
print ("Going to y = "..ny)
while y < ny do
if not up () then
print ("Cannot move up in current mode! Aborting...")
return false
end
end
end
if x < nx then
turnTo (1, 0)
print ("Going to x = "..nx)
while x < nx do
if not forward () then
print ("Cannot move forward in current mode! Aborting...")
return false
end
end
end
if z < nz then
turnTo (0, 1)
print ("Going to z = "..nz)
while z < nz do
if not forward () then
print ("Cannot move forward in current mode! Aborting!")
return false
end
end
end
if z > nz then
turnTo (0, -1)
print ("Going to z = "..nz)
while z > nz do
if not forward () then
print ("Cannot move forward in current mode! Aborting!")
return false
end
end
end
if x > nx then
turnTo (-1, 0)
print ("Going to x = "..nx)
while x > nx do
if not forward () then
print ("Cannot move forward in current mode! Aborting!")
return false
end
end
end
if y > ny then
print ("Going to y = "..ny)
while y > ny do
if not down () then
print ("Cannot move down in current mode! Aborting!")
return false
end
end
end
turnTo (nxd, nzd)
return true
end
And the other one: I am trying to make an excavation program using that API. It seems like if I call navi.simpleForward (), it doesn't update the turtle coordinates. Am I doing wrong something?