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

(Request)Someone can make an advanced turtle tutorial

Started by Loopin, 13 September 2012 - 10:37 PM
Loopin #1
Posted 14 September 2012 - 12:37 AM
Hi,
I need a tutorial that shows how to make an advanced turtle program like an turtle builder, that can read a especified design and, build it.

An example:

X=cobblestone

xxxxx
X—-x
Xxxxx
Read this and make it.

Sorry for my bad spelling,i'm not English and i'm on the phone.
Bubba #2
Posted 14 September 2012 - 01:55 AM
Making turtles build structures from a preset design is challenging and requires you to make several different apis and programs that are specific to different tasks.

The first thing you'll want to make is a navigation api. Make an api that is capable of navigating to a set of coordinates (x,y,z). I'll give you that at the bottom of this post, but I would suggest that you try to make your own first. Only a suggestion though :)/>/> You can use mine by putting the file into your api folder and calling it with filename.navigate(x,y,z)

Next you'll need a program that allows you to create designs for your house and interpret that design into coordinates so your navigation api can read it. Check out Biposy's biodesign for ideas.

The third and final thing you'll need to make is something that uses the coordinates from the design program. I would suggest that you build in layers. Start from the bottom and build up.

Like I said at the beginning of this post, making advanced turtles is challenging and you'll need a lot of patience. With that said, I wish you the best of luck and hope that this post helped you get on the right track :P/>/>
Spoiler

--LPS: Local Positioning Service
local coordX = 0
local coordY = 0
local coordZ = 0
local rotation = 1
local loadFile = "coords"

function readFile()
	if fs.exists(loadFile) == true then
		 f = fs.open(loadFile, "r")
		coordX = tonumber(f.readLine())
		coordY = tonumber(f.readLine())
		coordZ = tonumber(f.readLine())
		rotation = tonumber(f.readLine())
		f.close()
	end
end

function updateFile(x,y,z,rot)
	if x == nil or y == nil or z == nil or rot == nil then print("no") return false else
		f = fs.open(loadFile, "w")
		f.write(x.."n")
		f.write(y.."n")
		f.write(z.."n")
		f.write(rot.."n")
		f.close()
	end
	readFile()
	return true
end

function rotateTo(direction)
	if rotation == 1 then
		if direction == 2 then
			turtle.turnRight()
		elseif direction == 3 then
			turtle.turnRight()
			turtle.turnRight()
		elseif direction == 4 then
			turtle.turnLeft()
		end
	elseif rotation == 2 then
		if direction == 3 then
			turtle.turnRight()
		elseif direction == 4 then
			turtle.turnRight()
			turtle.turnRight()
		elseif direction == 1 then
			turtle.turnLeft()
		end
	elseif rotation == 3 then
		if direction == 4 then
			turtle.turnRight()
		elseif direction == 1 then
			turtle.turnRight()
			turtle.turnRight()
		elseif direction == 2 then
			turtle.turnLeft()
		end
	elseif rotation == 4 then
		if direction == 1 then
			turtle.turnRight()
		elseif direction == 2 then
			turtle.turnRight()
			turtle.turnRight()
		elseif direction == 3 then
			turtle.turnLeft()
		end
	end
	updateFile(coordX,coordY,coordZ,direction)
end

function move()
	if turtle.detect() == true then
		turtle.dig()
		turtle.forward()
	else
		turtle.forward()
	end
end

function navigate(x,y,z,rot)
	if rot == nil then rot = 1 end
	if y < coordY then
		rotateTo(3)
		while coordY ~= y do
			move()
			updateFile(coordX, coordY - 1, coordZ, 3)
		end
	elseif y > coordY then
		rotateTo(1)
		while coordY ~= y do
			move()
			updateFile(coordX, coordY + 1, coordZ, 1)
		end
	end
	if x < coordX then
		rotateTo(4)
		while coordX ~= x do
			move()
			updateFile(coordX-1, coordY, coordZ, 4)
		end
	elseif x > coordX then
		rotateTo(2)
		while coordX ~= x do
			move()
			updateFile(coordX + 1, coordY, coordZ, 2)
		end
	end
	if z > coordZ then
		while coordZ ~= z do
			if turtle.up() == true then
				updateFile(coordX, coordY, coordZ + 1, rotation)
			else
				io.read()
			end
		end
	elseif z < coordZ then
		while coordZ ~= z do
			if turtle.down() == true then
				updateFile(coordX, coordY, coordZ - 1, rotation)
			else
				io.read()
			end
		end
	end
	rotateTo(rot)
	return true
end
Bubba #3
Posted 14 September 2012 - 02:01 AM
Oops. Doublepost
Loopin #4
Posted 14 September 2012 - 02:15 AM
Thank you,
this should help me a lot.

this will be a challenge.