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

Internal Navigation API v1.0

Started by dd4235, 13 October 2012 - 05:53 PM
dd4235 #1
Posted 13 October 2012 - 07:53 PM
This API lets you track exactly where your turtles are without using the rednet-based GPS connection. Basically, it tracks every movement your turtle makes and updates the location and direction.

Usage
SpoilerInternal Navigation works a little differently than most other APIs. Rather than using lots of different functions, you just call one function, inav.createInternalNav() to create a navigation object, then call functions from that object.

For example, to create a navigation object called "myNav", you would use:

myNav = inav.createInternalNav()

After creating that object, all other functions are called by using "myNav" (or whatever you called your object earlier), then a period, then the function name. For example, to call the "forward()" function, you would use:

myNav.forward()

To use this API, you replace all of your turtle movement functions (like turtle.forward and turtle.back) with functions called just like the one above.

Here is a list of all of the available functions. They are written for the navigation object called "myNav".

myNav.forward()
- Replaces turtle.forward()
- Returns true if the movement succeeds, false if the movement fails

myNav.back()
- Replaces turtle.back()
- Returns true if the movement succeeds, false if the movement fails

myNav.up()
- Replaces turtle.up()
- Returns true if the movement succeeds, false if the movement fails

myNav.down()
- Replaces turtle.down()
- Returns true if the movement succeeds, false if the movement fails

myNav.turnLeft()
- Replaces turtle.turnLeft()
- Returns true if the movement succeeds, false if the movement fails

myNav.turnRight()
- Replaces turtle.turnRight()
- Returns true if the movement succeeds, false if the movement fails

myNav.calibrate(x,y,z,direction)
- Sets the current position of the turtle
- Defaults to 0

myNav.getPos()
- Returns a table containing the current position
- "x", "y", and "z" are the coordinates, "dir" is the current direction

Download
http://pastebin.com/3KHq3K2R

Code
Spoiler


function createInternalNav()

	intab = {}

	intab.xpos = 0
	intab.ypos = 0
	intab.zpos = 0
	intab.dir  = 0


	function intab.forward()
		local moveworked = turtle.forward()
		if moveworked == true then		  
			if intab.dir%4 == 0 then intab.xpos = intab.xpos+1
			elseif intab.dir%4 == 1 then intab.zpos = intab.zpos+1
			elseif intab.dir%4 == 2 then intab.xpos = intab.xpos-1
			elseif intab.dir%4 == 3 then intab.zpos = intab.zpos-1
			else print("Massive Error in intab.forward()") end
		end
		return moveworked
	end

	function intab.back()
		local moveworked = turtle.back()
		if moveworked == true then
			if intab.dir%4 == 0 then intab.xpos = intab.xpos - 1
			elseif intab.dir%4 == 1 then intab.zpos = intab.zpos - 1
			elseif intab.dir%4 == 2 then intab.xpos = intab.xpos + 1
			elseif intab.dir%4 == 3 then intab.zpos = intab.zpos + 1
			else print("Massive Error in intab.back()") end
		end
		return moveworked
	end

	function intab.up()
		local moveworked = turtle.up()
		if moveworked == true then intab.ypos = intab.ypos + 1 end
		return moveworked
	end

	function intab.down()
		local moveworked = turtle.down()
		if moveworked == true then intab.ypos = intab.ypos - 1 end
		return moveworked
	end

	function intab.turnLeft()
		local turnworked = turtle.turnLeft()
		if turnworked == true then intab.dir = intab.dir - 1 end
		return turnworked
	end

	function intab.turnRight()
		local turnworked = turtle.turnRight()
		if turnworked == true then intab.dir = intab.dir + 1 end
		return turnworked
	end

	function intab.printPos()
		print("Current Location: ("..intab.xpos..","..intab.ypos..","..intab.zpos..","..intab.dir%4..")")
	end

	function intab.getPos()
		currentPos = {"x"=intab.xpos,"y"=intab.ypos,"z"=intab.zpos,"dir"=intab.dir%4}
		return currentPos
	end

	function intab.calibrate(x,y,z,d)
		if type(x) == "table" then
			inputTable = x
			x = inputTable.x
			y = inputTable.y
			z = inputTable.z
			d = inputTable.d
		end

		if type(x) ~= "number" then x = 0 end
		if type(y) ~= "number" then y = 0 end
		if type(z) ~= "number" then z = 0 end
		if type(d) ~= "number" then d = 0 end

		intab.xpos = x
		intab.ypos = y
		intab.zpos = z
		intab.dpos = d
	end


	return intab
end
billysback #2
Posted 13 October 2012 - 08:05 PM
how does it get your initial coordinates?
PixelToast #3
Posted 13 October 2012 - 08:07 PM
how does it get your initial coordinates?
it dosent, it stores offsets not actual coords
dd4235 #4
Posted 14 October 2012 - 12:42 AM
it dosent, it stores offsets not actual coords

But if you want, you can use calibrate() to put in real coords.
Trabber Shir #5
Posted 18 January 2013 - 12:36 AM
Sorrry for the necro.
This is a very useful API, however it has a bug I figured it would be polite to point out.

The calibrate function fails to set the direction becuase it sets 'dpos' where 'dir' is used throughout the rest of the code. Fix that and it is golden.
coaster3000 #6
Posted 24 May 2013 - 03:12 PM
May I get permission to extend this api and publish it on this thread? Well actually I did make it already for personal private use. But wondering if I could publish the addition?

It adds a simulated turtle for non turtle computers.. So you can check if computers work.