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

[gps] attempting to face a turtle

Started by Esperologist, 05 February 2013 - 02:33 PM
Esperologist #1
Posted 05 February 2013 - 03:33 PM
Title: [gps] attempting to face a turtle

I'm sure I'm missing something to make this easier, but since I can't find it myself, I am here to ask.

I am attempting to get a turtle to determine the direction it is facing.

I am using the gps that comes with it (FTB MindCrack pack)

I am looking for either an inbuilt call to get the facing of the computer, or a way to find it myself.

— read the rest to prevent suggesting something I have already tried, or give me more info so I can succeed.

I checked the documentation on the ComputerCraft wiki. The gps does not seem to include facing.

I attempted to use gps locate and a move so I could build a facing function… however, gps locate returns true.
My goal was to use the location of the turtle in two positions to determine facing.

I then tried to find the script for gps locate in the folders so I could make a custom version… and I didn't find it.

I did some searching online and in these forums, and I did not find anything to help me get facing or make a custom locate.

—–

With that, any resources, directions or sample code would be appreciated.
Orwell #2
Posted 05 February 2013 - 04:14 PM
There's no standard way to get the facing of the turtle. I once developed a method using a torch being placed with only horizontally adjacent blocks, but that one was quite elaborate. I would go for the gps + moving method.

gps.locate() should return the coords as specified on the wiki. I don't know why it would return true, maybe someone else can help you with that.
I can help on the moving to retrieve facing part. This is a sample:

local dirNames = {"South", "West", "North", "East"}

local x,y,z = gps.locate(5) or error("Couldn't get gps fix.")
local first = vector.new( x,y,z )  -- get first coordinate as vector
while not turtle.forward() do turtle.dig() end  -- move one block or dig it if necessary
x,y,z = gps.locate(5) or error("Couldn't get gps fix.")
local second = vector.new( x,y,z )  -- get second coordinate as vector
local dirVect = second-first
local direction = (dirVect.x>0 and 4 or 2) or (dirVect.z>0 and 1 or 3)  -- get the index in the table for the direction
local dirName = dirNames[direction]
This should at least get you in the right direction (if you didn't have this already). I hope you get help with the getting the true returned.
Esperologist #3
Posted 05 February 2013 - 07:40 PM
Ah… I see now. I didn't understand the difference between the gps api and gps program. Showing my newb. >_<
Now, to figure out how to set two turtle to run the same program and still communicate with each other.
I'll have to go learn how the rednet works now.
I'm just jumping in without testing the water.
Let's just say I want to make a pair of 'dancing' turtles for someone.

- edit -
Useful idea there, subtracting one location from the other.
I opted to bypass the vectoring and just subtracted the x,z of the second location from the first location.
It means I have to use absolutes so I don't try subtracting a negative or from a negative… could get messy.
I may try out the vector things if the method I am using gets out of hand.
PhilHibbs #4
Posted 06 February 2013 - 01:16 AM

while not turtle.forward() do turtle.dig() end  -- move one block or dig it if necessary
x,y,z = gps.locate(5) or error("Couldn't get gps fix.")
This should at least get you in the right direction…
Or, it should make inconvenient holes in your base with loot spilling out of chests all over the floor. :)/>

"Don't give me problems, give me solutions!"

OK then. When building a moving turtle system such as refuelling or crafting or item sorting, always ensure that your turtle's route has space behind where the turtle might be at any point. Approach forwards, depart backwards. Leave a gap of 3 horizontal blocks in between any blocks that the turtle needs to approach, so that when it turns to face it to approach, there is a gap behind it. Then, when your server restarts or the chunk unloads and your turtles need to find out where they are and what direction they are facing, they can always use the above process by moving backwards instead of forwards.
Orwell #5
Posted 06 February 2013 - 01:27 AM
* snip *
Well, you could also simply detect blocks in the 6 directions, if you have no space in either of those directions, then obviously some stuff has changed since your turtle moved there. :P/> This could also suggest that the turtle is not in your base, since you wouldn't put stuff in front of your turtle. Of course, when working in your base, you wouldn't want it to dig around, you'd use custom code and set-up like you proposed.
PhilHibbs #6
Posted 06 February 2013 - 02:57 AM
Well, you could also simply detect blocks in the 6 directions, if you have no space in either of those directions, then obviously some stuff has changed since your turtle moved there. :P/> This could also suggest that the turtle is not in your base, since you wouldn't put stuff in front of your turtle.
It could have been doing maintenance inside a huge over-engineered cobblestone generator
Orwell #7
Posted 06 February 2013 - 03:12 AM
Well, you could also simply detect blocks in the 6 directions, if you have no space in either of those directions, then obviously some stuff has changed since your turtle moved there. :P/> This could also suggest that the turtle is not in your base, since you wouldn't put stuff in front of your turtle.
It could have been doing maintenance inside a huge over-engineered cobblestone generator
That's one hell of a youtube vid. :D/> Well, that's what I mean with custom code… The more custom your scenario is, the more specific your code shall need to be. I couldn't ever cover every possible scenario in one sample snippet. :P/> My purpose was rather showing the use of direction vectors.