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

Remote controlled turtle [1.45] with map type thing.

Started by sirdabalot, 21 October 2012 - 04:38 PM
sirdabalot #1
Posted 21 October 2012 - 06:38 PM
Make sure you have CC 1.45

If you request an addition, I might add it if I consider it "small".

Umm… so yeah, after the implementation of the new advanced computers (thanks dan/cloudy) I decided to throw together a little test program that controls turtles.

Transmitter computer.
Spoiler

function getCoords()
	for i = 1, 4 do
	id, msg, distance = rednet.receive()
	if id == rid then
		if i == 1 then
			xpos = tonumber(msg)
		elseif i == 2 then
			ypos = tonumber(msg)
		elseif i == 3 then
			zpos = tonumber(msg)
		elseif i == 4 then
			face = tonumber(msg)
		end
	end
	end
end

sgui.clearScreen(term)

write("ID: ")
rid = tonumber(read())
write("OriginalX: ")
orx = tonumber(read())
write("OriginalY: ")
ory = tonumber(read())
write("OriginalZ: ")
orz = tonumber(read())

tw, tl = term.getSize()

cx = tw/2
cy = tl/2

sgui.clearScreen(term)

rednet.open("top")

while true do
	event, key = os.pullEvent("key")
	rednet.send(rid, tostring(key))
	getCoords()
	sgui.clearScreen(term)
	sgui.fillBack(term, " ", colours.grey)
	paintutils.drawPixel(cx, cy, colours.red)
	paintutils.drawPixel(1, cy, colours.red)
	paintutils.drawPixel((cx + orx - xpos), (cy + orz - zpos), colours.green)
	paintutils.drawPixel(1, (cy + ory - ypos), colours.yellow)
	term.setCursorPos(1,1)
	term.setBackgroundColour(colours.blue)
	write("FACE: " .. face)
end

Receiver turtle.
Spoiler

write("ID: ")
tid = tonumber(read())
write("XPOS: ")
xpos = tonumber(read())
write("YPOS: ")
ypos = tonumber(read())
write("ZPOS: ")
zpos = tonumber(read())
write("FACE: ")
face = tonumber(read())

rednet.open("right")

function rec()
	id, msg, distance = rednet.receive()
	msg = tonumber(msg)
	if id == tid then
		if msg == 200 then
			forward()
		elseif msg == 208 then
			back()
		elseif msg == 203 then
			left()
		elseif msg == 205 then
			right()
		elseif msg == 57 then
			up()
		elseif msg == 42 then
			down()
		elseif msg == 27 then
			turtle.dig()
		elseif msg == 13 then
			turtle.digUp()
		elseif msg == 41 then
			turtle.digDown()
		elseif msg == 26 then
			turtle.place()
		elseif msg == 12 then
			turtle.placeUp()
		elseif msg == 39 then
			turtle.placeDown()
		elseif msg == 19 then
			rs.setOutput("front", true)
			sleep(0.1)
			rs.setOutput("front", false)
		end
	end
end

function forward()
	if turtle.forward() == true then
		if face == 0 then
			zpos = zpos + 1
		elseif face == 1 then
			xpos = xpos - 1
		elseif face == 2 then
			zpos = zpos - 1
		elseif face == 3 then
			xpos = xpos + 1
		end
	end
end

function back()
	if turtle.back() == true then
		if face == 0 then
			zpos = zpos - 1
		elseif face == 1 then
			xpos = xpos + 1
		elseif face == 2 then
			zpos = zpos + 1
		elseif face == 3 then
			xpos = xpos - 1
		end
	end
end

function left()
	turtle.turnLeft()
		if face > 0 then
			face = face - 1
		else face = 3
		end
end

function right()
	turtle.turnRight()
		if face < 3 then
			face = face + 1
		else face = 0
		end
end

function up()
	if turtle.up() == true then
		ypos = ypos + 1
	end
end

function down()
	if turtle.down() == true then
		ypos = ypos - 1
	end
end

function sendCoords()
	rednet.send(tid, tostring(xpos))
	sleep(0.05)
	rednet.send(tid, tostring(ypos))
	sleep(0.05)
	rednet.send(tid, tostring(zpos))
	sleep(0.05)
	rednet.send(tid, tostring(face))
end

while true do
	rec()
	sendCoords()
end

A little GUI API that I made (you need this).
Spoiler

function printCentered(per, str, ypos)
    pw, pl = per.getSize()
    per.setCursorPos(((pw/2) - (#str/2)), ypos)
    per.write(str)
end

function drawHeader(per, str)
    pw, pl = per.getSize()
    printCentered(per, str, 1)
    per.setCursorPos(1,2)
    write(string.rep("=", pw))
end

function addSpaces(str, len)
    str = str .. string.rep(" ", len - #str)
end

function clearScreen(per)
    per.clear()
    per.setCursorPos(1,1)
end

function fillBack(per, str, colour)
    pw, pl = per.getSize()
    for x = 1, pw do
        for y = 1, pl do
            per.setCursorPos(x, y)
            per.setBackgroundColour(colour)
            write(str)
        end
    end
end

function errMsg(per, str, dtime)
    pw, pl = per.getSize()
    per.setTextColour(colours.red)
    for i = 1, dtime do
        per.clear()
        sleep(0.5)
        printCentered(per, "SYS.FLR:", pl/2)
        printCentered(per, str, (pl/2) + 1)
        sleep(0.5)
    end
    clearScreen(per)
end

Extra info:

Modem goes on top.
Look at the debug screen to get the correct face number.
On request for original positions put the block coordinate that you want to measure from.
Face 0 is facing towards the top of the screen.
Think of the red squares on the screen as an origin.


Controls:
Spoilerwasd space shift - move
#]= - dig
'[- - place
r - redstone current through front
ChunLing #2
Posted 21 October 2012 - 08:37 PM
Hey, a little terminal screen map of the turtle's movement. Everybody always talks about doing that but hardly anybody actually does it.