←  APIs and Utilities

ComputerCraft | Programmable Computers for Minecraft

»

[CC 1.76] Easy Drawing Characters

oli414's Photo oli414 12 Dec 2015

Hey folks, to try out the new beta I played around a bit with the new characters. Especially the drawing characters which are characters made out of 6 pixels. Using these characters every possible combination can be made. However, it can be a bit hard to figure out what character you need. I've tried to figure out an easy method that would allow you to get the character you need based on the pixel-combination you want, here it is:

function getDrawingCharacter(topLeft, topRight, left, right, bottomLeft, bottomRight)
  local data = 128
  if not bottomRight then
	data = data + (topLeft and 1 or 0)
	data = data + (topRight and 2 or 0)
	data = data + (left and 4 or 0)
	data = data + (right and 8 or 0)
	data = data + (bottomLeft and 16 or 0)
  else
	data = data + (topLeft and 0 or 1)
	data = data + (topRight and 0 or 2)
	data = data + (left and 0 or 4)
	data = data + (right and 0 or 8)
	data = data + (bottomLeft and 0 or 16)
  end
  return {char = string.char(data), inverted = bottomRight}
end

Using this function you can specify which pixel you want to be on. As described before there are 6 pixels per character. Because of this the function has 6 boolean parameters to specify which pixel you want to be on. The function returns two values, the "char" and a boolean "inverted".
The char is the character you'd need to draw to get the pixels you want, however. It is possible that you'll have to switch the background and text color because there isn't a character for every possible combination. If this is the case "inverted" will be true.

Here's a little example:
pixels = getDrawingCharacter(true, true, true, false, true, true)
if not pixels.inverted then
  term.setBackgroundColor(16384)
  term.setTextColor(1)
else
  term.setBackgroundColor(1)
  term.setTextColor(16384)
end
term.write(pixels.char)


The function could probably be improved, please let me know if you have a better method.

Feel free to use this function wherever you want :-)

-Oli414
Edited by oli414, 12 December 2015 - 05:12 PM.
Quote

KingofGamesYami's Photo KingofGamesYami 12 Dec 2015

Since I love OOP, I'm going to write this as an object.

Spoiler
Quote

oli414's Photo oli414 12 Dec 2015

^ Neat! I've never really used OOP in Lua since it always feels a bit hacky and everyone is doing it differenly (just like with Javascript) but I think I'm gonna give it a try soon.
Quote

KingofGamesYami's Photo KingofGamesYami 12 Dec 2015

I've only seen two approaches to OOP in Lua, the metatable way (I use that) and the non-metatable way (the window API uses that). IIRC, metatables are slower at being accessed, but non-metatable uses more memory since it creates the function each time.
Quote

Bomb Bloke's Photo Bomb Bloke 13 Dec 2015

View Postoli414, on 12 December 2015 - 04:50 PM, said:

The function could probably be improved, please let me know if you have a better method.

I spotted a few reductions that could be made:

function getDrawingCharacter(...)  -- Bools for topLeft, topRight, left, right, bottomLeft, bottomRight
	local data = 0
	for i = 1, #arg - 1 do if arg[i] then data = data + 2^(i-1) end end
	return {char = string.char(arg[6] and 159 - data or 128 + data), inverted = arg[6]}
end

But the underlying principle you're using is as good as it gets. :)
Quote

oli414's Photo oli414 13 Dec 2015

^ I was thinking about doing something like that, however, I thought this method would be quicker and would make the parameters more clear.

Is does look a lot smaller though. Thanks.
Quote