Posted 29 July 2012 - 11:50 PM
This location API adds a class that can be used to manipulate turtles and keep track of their location. You can either designate its location manually, or use this bit of code to automatically determine the location (but the turtle must be free to move forward one space in the process, and requires GPS hosts in the vicinity):
This gets the location of the turtle in one position (stored as a vector), gets it after moving the turtle forward one, then subtracts the two vectors (to get a x,z of -1,0 or similar), then creates a new instance of the location class with the turtle's location and heading. The x, y and z keys hold the x, y and z coordinates of the location, and the h key holds the heading. Heading is one of four values:
-x 1 (west)
-z 2 (north)
+x 3 (east)
+z 4 (south)
You may note that we use 1-4 instead of 0-3, as minecraft does.
This location class has various replacement methods. For instance, instead of calling turtle.forward(), one would call loc:forward() (if you were using the name loc for the location class of your turtle). These methods automatically adjust the coordinates stored in the location class, based on the heading. The following methods are added (assuming a class named loc):
You can find the location API on github.
loc1 = vector.new(gps.locate(2, false))
turtle.forward()
loc2 = vector.new(gps.locate(2, false))
heading = loc2 - loc1
turtle.back()
loc = location.new(loc1.x, loc1.y, loc1.z, location.getOrientation(heading.x, heading.z))
This gets the location of the turtle in one position (stored as a vector), gets it after moving the turtle forward one, then subtracts the two vectors (to get a x,z of -1,0 or similar), then creates a new instance of the location class with the turtle's location and heading. The x, y and z keys hold the x, y and z coordinates of the location, and the h key holds the heading. Heading is one of four values:
-x 1 (west)
-z 2 (north)
+x 3 (east)
+z 4 (south)
You may note that we use 1-4 instead of 0-3, as minecraft does.
This location class has various replacement methods. For instance, instead of calling turtle.forward(), one would call loc:forward() (if you were using the name loc for the location class of your turtle). These methods automatically adjust the coordinates stored in the location class, based on the heading. The following methods are added (assuming a class named loc):
loc:forward()
loc:back()
loc:up()
loc:down()
loc:left()
loc:right()
loc:moveDeltas() --returns x, z where x and z are the coordinate change that would occur if the turtle were to move forward. One will always be zero, and the other will always be -1 or 1.
loc:setHeading(num) --sets the heading of the turtle to 1-4, turning the turtle appropriately.
loc:tovector() --returns a vector using the x, y and z coordinates.
--and the location methods themselves:
location.new(x, y, z, h) --returns a new location class instance.
location.getOrientation(x, z) --returns a heading number (1-4) from a set of movement deltas (One value zero, the other either -1 or 1).
You can find the location API on github.