if os.getComputerLabel()==nil then
	os.setComputerLabel("Noel "..os.getComputerID())
	print("set up nom")
end
if fs.exists("config") then
	print("charge config")
	file=fs.open("config","r")
	config=	textutils.unserialize(file.readAll())
	file.close()
	print(textutils.serialize(config))
else
	print("creation config")
	local x, y, z = gps.locate(5)
	config={
		x=math.floor(x),
		y=math.floor(y),
		z=math.floor(z)
	}
	file=fs.open("config","w")
	file.write(textutils.serialize(config))
	file.close()
end

local compass=peripheral.find("compass")



print("Attente carburant")
local event, p1 = os.pullEvent("turtle_inventory")
turtle.refuel(1)
print("ok")

local modem=peripheral.find("modem")
modem.open(4242)
event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")

while true do
	local x, y, z = gps.locate(5)
	x=math.floor(x)
	z=math.floor(z)
	y=math.floor(y)
	direction=compass.getFacing()
	if config.x==x and config.y==y and config.z==z then return end
	if config.y~=y then
		if config.y>y then
			turtle.up()
		else
			turtle.down()
		end
	elseif config.x~=x then
		print(direction)
		if direction=='north' then -- z --			
			if config.x>x then
				turtle.turnRight()
			else
				turtle.turnLeft()
			end
		elseif direction=='east' then -- x ++
			if config.x>x then
				turtle.forward()
			else 
				turtle.turnRight()
				turtle.turnRight()
			end
		elseif direction=='south' then -- z ++
			if config.x>x then
				turtle.turnLeft()
			else
				turtle.turnRight()
			end
		else -- x --
			if config.x>x then
				turtle.turnRight()
				turtle.turnRight()
			else 
				turtle.forward() 
			end
		end		
	else
		print(direction)
		if direction=='north' then -- z --
			if config.z<z then
				turtle.forward()
			else 
				turtle.turnRight()
				turtle.turnRight()
			end
		elseif direction=='east' then -- x ++
			if config.z>z then
				turtle.turnRight()
			else
				turtle.turnLeft()
			end
		elseif direction=='south' then -- z ++
			if config.z<z then
				turtle.turnRight()
				turtle.turnRight()
			else 
				turtle.forward()
			end
		else -- x --
			if config.z>z then
				turtle.turnLeft()
			else
				turtle.turnRight()
			end
		end
	end
end