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

Is there a better way to get world orientation without gps?

Started by Quadrapod, 15 December 2015 - 11:44 AM
Quadrapod #1
Posted 15 December 2015 - 12:44 PM
As part of a miner script in the infinity mod pack I've been making use of enderchests as form of mobile storage just dialing in the frequency I wanted, but I was a little bothered by how precarious that was, since any unexpected item in the chest would basically ruin everything. turtle.suck() lacks any kind of awareness of what it's looking for, it it will always grab the first stack it sees, even if that stack is too small, or if it contains the wrong items. I noticed that I could mount enderchests as peripherals, letting me look carefully through their inventories and at the items contained within. (Though that might be because of other mods) , but the peripheral method .pushItem() requires a direction to be given in terms of the world. I.e. "NORTH", "EAST", or the number which those translate to. I rewrote my program to simply use .swapStack() to put the right item into the first slot before trying to take it, but from this I did come up with an incredibly cheaty way to determine my world direction after placing a chest with turtle.place().

Basically I attempt to transfer a stack of zero items from the chest into an inventory in every direction. If the turtle is the only block around with an inventory, then when the transfer completes without throwing an error you know you're facing the right direction.


local function enderGetWorldDirection()
	--This is a string compare, hence the weird layout
	if not (peripheral.getType("front") == "ender_chest") then error("ERROR: Enderchest could not be found") end
	local failed
	local errReturn
	local chest = peripheral.wrap("front")
	local retVal = nil
	-- directions are reversed from standard values because your facing an opposite direction to the chests direction toward you
	local directionTable =  {
		[1] = "WEST",
		[2] = "EAST",
		[3] = "SOUTH",
		[4] = "NORTH"
		}


	for i = 1, 4, 1 do
		failed, errReturn = pcall(chest.pushItem, i, 0)
		if (err == 0) then
			assert(retVal)
			if retVal == nil then
				retVal = i
			end
		end
	end
	assert(not returnVal)
	return directionTable[retVal]
end

It's really sketchy but it does work.
Edited on 15 December 2015 - 11:10 PM
Lion4ever #2
Posted 29 December 2015 - 02:35 PM
Like this?
http://www.computercraft.info/forums2/index.php?/topic/24098-turtle-compass-using-turtleinspect-with-a-torch-or-a-ladder/
KingofGamesYami #3
Posted 29 December 2015 - 03:01 PM
Wow, that's very interesting. I'd never have thought of doing that - by the way, OpenPeripherals is adding the chest as a peripheral :)/>. I use LAMA, which simply remembers the position and direction; you have to tell it where you are and which way you're facing the first time it's run. Luckily, it's reboot-persistent!
CometWolf #4
Posted 30 December 2015 - 01:42 AM
As Yami mentioned, the usual way of handling this is having the turtle always know it's own direction and location, it's how i handle it myself aswell. However, i believe you can specify the direction as "unknown" and OpenP will handle the direction for you instead.
Lyqyd #5
Posted 07 January 2016 - 12:52 AM
Moved to Ask a Pro.