This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Marsunpaisti's profile picture

Turtle smart movement and coordinate tracking

Started by Marsunpaisti, 12 April 2012 - 01:40 PM
Marsunpaisti #1
Posted 12 April 2012 - 03:40 PM

Hello, this is the first code that I guess is worth posting on the forums. What I made is an API (I think?) that keeps track of the turtles coordinates and you can give it commands to go to a certain coordinate with the flyto(X,Y,Z) function. It has its own forward, back and down movements (to keep track of the co-ordinates via variables) as well as a function that makes it face north/south/east/west. All it requires is the execution of the calibrate() function which sets the initial facing and XYZ coordinates of the turtle, after this, assuming you use the APIs movement functions you can use the co-ordinate position for all kinds of stuff. (I made a bombard function that makes the turtle go somewhere, drop some tnt and go back to where it was). The only issue there MIGHT be is when sand falls on top of the turtle or animals / players block it, otherwise it just destroys blocks before going there to make sure it doesn't get stuck and that the coordinates don't get off sync.

Update 1: Added tright and tleft functions to control the turtles turning properly.


function correctfacing()
		if (facingnum == 5) then
			facingnum = 1
			elseif (facingnum == 0) then
			facingnum = 4
		end
end

function facedir(dir)
if (dir == "n") then
dirnum = 1
elseif (dir == "e") then
dirnum = 2
elseif (dir == "s") then
dirnum = 3
elseif (dir == "w") then
dirnum = 4
end
facingprocess = 1
while (facingprocess == 1) do
		if (facingnum < dirnum) then
			turtle.turnRight()
			facingnum = (facingnum + 1)
			elseif (facingnum > dirnum) then
			turtle.turnLeft()
			facingnum = (facingnum - 1)
			elseif (facingnum == dirnum) then
			facingprocess = 0
		end
	correctfacing()
end
end


function tforward()
	turtle.dig()
	turtle.forward()
	if (facingnum == 1) then
	z = (z - 1)
	elseif (facingnum == 2) then
	x = (x + 1)
	elseif (facingnum == 3) then
	z = (z + 1)
	elseif (facingnum == 4) then
	x = (x - 1)
	end
end

function tup()
	turtle.digUp()
	turtle.up()
	y = (y+1)
end

function tdown()
	turtle.digDown()
	turtle.down()
	y = (y-1)
end

function tright()
turtle.turnRight()
facingnum = (facingnum + 1)
correctfacing()
end

function tleft()
turtle.turnLeft()
facingnum = (facingnum - 1)
correctfacing()
end

function flyto(tarx,tary,tarz)
tx = tonumber(tarx)
ty = tonumber(tary)
tz = tonumber(tarz)
fly = 1
flyingloop()
end

function flyingloop()
	while (fly == 1) do
		if (tonumber(y) < tonumber(ty)) then
		tup()
		elseif (tonumber(z) > tonumber(tz)) then
		facedir("n")
		tforward()
		elseif (tonumber(z) < tonumber(tz)) then
		facedir("s")
		tforward()
		elseif (tonumber(x) < tonumber(tx)) then
		facedir("e")
		tforward()
		elseif (tonumber(x) > tonumber(tx)) then
		facedir("w")
		tforward()
		elseif (tonumber(y) > tonumber(ty)) then
		tdown()
		else
		fly = 0
		end
	end
end

function setcoords()
term.clear()
term.setCursorPos(1,1)
print("Please input target coordinates")
write("X: ")
targetx = read()
write("Y: ")
targety = read()
write("Z: ")
targetz = read()
print("Coordinates set.")
sleep(2)
end

function status()
term.clear()
term.setCursorPos(1,1)
if (facingnum == 1) then
facing = "n"
elseif (facingnum == 2) then
facing = "e"
elseif (facingnum == 3) then
facing = "s"
elseif (facingnum == 4) then
facing = "w"
end
print("X: "..x.." Y: "..y.." Z: "..z.." Facing: "..facing)
sleep(4)
end

function calibrate()
print("Please input turtles current X, Y and Z coordinates.")
write("X: ")
tempx = read()
x = tonumber(tempx)
write("Y: ")
tempy = read()
y = tonumber(tempy)
write("Z: ")
tempz = read()
z = tonumber(tempz)
print("")
print("Coordinates set.")
print("Please input the facing of the turtle, n / e / s / w")
write("Facing: ")
facingloop = 1
while (facingloop == 1) do
facing = read()
if (facing == "n") then
facingnum = 1
facingloop = 0
elseif (facing == "e") then
facingloop = 0
facingnum = 2
elseif (facing == "s") then
facingloop = 0
facingnum = 3
elseif (facing == "w") then
facingloop = 0
facingnum = 4
else
print("Invalid input")
end
end
print("Facing set.")
sleep(1)
main()
end
Teraminer #2
Posted 17 April 2012 - 08:27 PM
Nice! Could you give us a demonstation.. (like a video if it is posible)
Marsunpaisti #3
Posted 19 April 2012 - 05:43 PM
Nice! Could you give us a demonstation.. (like a video if it is posible)
I added a video demonstration as you requested.
Teraminer #4
Posted 19 April 2012 - 06:23 PM
I like the new bombardmend aircraft the F-23 Turtle.
Lyqyd #5
Posted 08 May 2012 - 06:58 PM
Why not put your turtle.forward() calls in an if block to detect whether or not forward movement was successful?