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

Sirdabalots APIs

Started by sirdabalot, 17 November 2012 - 06:01 AM
sirdabalot #1
Posted 17 November 2012 - 07:01 AM
A small collection of functions I have made that have really helped me, if you aren't sure how one works please ask me. :D/>/>

Key:
per = peripheral(term, monitor etc…)
str = string
ypos = y position
len = length
col = colour
w = width
l = length
h = height

SGUI:
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 fillBackground(per, str, col)
	pw, pl = per.getSize()
	for x = 1, pw do
		for y = 1, pl do
			per.setCursorPos(x, y)
			per.setBackgroundColour(col)
			write(str)
		end
	end
end

function drawWindow(per, str, col, x1, y1, x2, y2)
	per.setCursorPos(x1, y1)
	per.setBackgroundColour(col)
	for x = x1, x2 do
		for y = y1, y2 do
			per.setCursorPos(x, y)
			per.write(" ")
		end
	end
	per.setCursorPos(x1,y1)
	per.write(str)
end

function drawTabs(per, tabs, ypos, sel)
	pw, pl = per.getSize()
	per.setBackgroundColour(colours.blue)
	for i = 1, pw do
		per.setCursorPos(i, ypos)
		per.write(" ")
	end
	per.setCursorPos(1, ypos)
	for i = 1, #tabs do
		ccx, ccy = term.getCursorPos()
		per.setCursorPos(ccx + 1, ypos)
		if i == sel then
			per.setBackgroundColour(colours.white)
			per.setTextColour(colours.blue)
		else
			per.setBackgroundColour(colours.blue)
			per.setTextColour(colours.white)
		end
		per.write(tabs[i])
	end
end

function drawIMG(per, img, x, y)
	for i = 1, #img do
		per.setCursorPos(x,y-1+i)
		per.write(img[i])
	end
end

function drawAnim(per, anim, x, y, delay)
	while true do
		for i = 1, #anim do
			for n = 1, #anim[i] do
				per.setCursorPos(x,y-1+n)
				per.write(anim[i][n])
			end
			sleep(delay)
		end
	end
end

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

function drawCircle(per, col, x, y, r)
	per.setBackgroundColour(col)
	for i = 1,360 do
		per.setCursorPos(x+(math.cos(i)*r), y+(math.sin(i)*r))
		per.write(" ")
	end
end

function drawProgressBar(per, x, y, wi, len, fill)
	pw, pl = per.getSize()
	per.setBackgroundColour(colours.red)
	for i = 1, wi do
		for n = 1, len do
			per.setCursorPos((x-1)+i, (y-1)+n)
			per.write(" ")
		end
	end
	per.setBackgroundColour(colours.lime)
	for i = 1, math.ceil((wi/100) * fill) do
		for n = 1, len do
			per.setCursorPos((x-1)+i, (y-1)+n)
			per.write(" ")
		end
	end
end

SBUILD:
Spoiler

function fTurn()
	if math.random(0, 1) == 1 then
		for i = 1, 2 do
			turtle.turnRight()
		end
	else
		for i = 1, 2 do
			turtle.turnLeft()
		end
	end
end

function selectSlot()
	for i = 1, 16 do
		if turtle.getItemCount(i) > 0 and turtle.getItemCount(i) ~= nil then
			turtle.select(i)
		end
	end
end

function buildLine(l)
	for i = 1, l - 1 do
		selectSlot()
		turtle.placeDown()
		turtle.forward()
	end
	selectSlot()
	turtle.placeDown()
end

function buildQuad(w, l)
	for i = 1, w - 1 do
		buildLine(l)
		for n = 1, l - 1 do
			turtle.back()
		end
		turtle.turnRight()
		turtle.forward()
		turtle.turnLeft()
	end
	buildLine(l)
end

function buildWall(w, l, h)
	for i = 1, h-1 do
		buildLine(l)
		turtle.turnRight()
		turtle.forward()
		buildLine(w-1)
		turtle.turnRight()
		turtle.forward()
		buildLine(l-1)
		turtle.turnRight()
		turtle.forward()
		buildLine(w-2)
		turtle.up()
		turtle.forward()
		turtle.turnRight()
	end
	buildLine(l)
	turtle.turnRight()
	turtle.forward()
	buildLine(w-1)
	turtle.turnRight()
	turtle.forward()
	buildLine(l-1)
	turtle.turnRight()
	turtle.forward()
	buildLine(w-2)
end

function buildHollow(w, l, h)
	buildQuad(w, l)
	fTurn()
	turtle.up()
	buildWall(w, l, h-2)
	turtle.forward()
	turtle.turnRight()
	turtle.up()
	buildQuad(w, l)
end

function buildCube(w, l, h)
	for i = 1, h do
		buildQuad(w, l)
		for n = 1, l - 1 do
			turtle.back()
		end
		turtle.turnRight()
		for n = 1, w - 1 do
			turtle.back()
		end
		turtle.turnLeft()
		turtle.up()
	end
end

function digLine(l)
	for i = 1, l - 1 do
		turtle.digDown()
		turtle.forward()
	end
	turtle.digDown()
end

function digQuad(w, l)
	for i = 1, w - 1 do
		digLine(l)
		for n = 1, l - 1 do
			turtle.back()
		end
		turtle.turnRight()
		turtle.forward()
		turtle.turnLeft()
	end
	digLine(l)
end

function digCube(w, l, h)
	for i = 1, h do
		digQuad(w, l)
		for n = 1, l - 1 do
			turtle.back()
		end
		turtle.turnRight()
		for n = 1, w - 1 do
			turtle.back()
		end
		turtle.turnLeft()
		turtle.down()
	end
end
Zudo #2
Posted 08 December 2012 - 09:20 AM
what do these do?
sirdabalot #3
Posted 22 December 2012 - 02:24 AM
what do these do?

Well the GUI APIs help draw stuff on different peripherals and the build api add basic turtle build functions.