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

[CC 1.76] Easy Drawing Characters

Started by oli414, 12 December 2015 - 03:50 PM
oli414 #1
Posted 12 December 2015 - 04:50 PM
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 on 12 December 2015 - 04:12 PM
KingofGamesYami #2
Posted 12 December 2015 - 05:48 PM
Since I love OOP, I'm going to write this as an object.

Spoiler

local pixel = {
  function draw( self, x, y )
    term.setBackgroundColor( self.inverted and self.fg or self.bg )
    term.setTextColor( self.inverted and self.bg or self.fg )
    term.write( self.char )
  end,
  function setBg( self, color )
    self.bg = color
  end,
  function setFg( self, color )
    self.fg = color
  end
}

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 setmetatable( {fg = colors.white, bg = colors.black, char = string.char(data), inverted = bottomRight}, {__index = pixel} )
end
oli414 #3
Posted 12 December 2015 - 07:34 PM
^ 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.
KingofGamesYami #4
Posted 12 December 2015 - 09:34 PM
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.
Bomb Bloke #5
Posted 13 December 2015 - 06:11 AM
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. :)/>
oli414 #6
Posted 13 December 2015 - 10:59 AM
^ 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.