Jump to content




[CC 1.76] Easy Drawing Characters


5 replies to this topic

#1 oli414

  • Members
  • 29 posts
  • LocationThe Netherlands

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 by oli414, 12 December 2015 - 05:12 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 12 December 2015 - 05:48 PM

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

Spoiler


#3 oli414

  • Members
  • 29 posts
  • LocationThe Netherlands

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.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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.

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 December 2015 - 06:11 AM

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. :)

#6 oli414

  • Members
  • 29 posts
  • LocationThe Netherlands

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users