cc-libs/programs/creeper.lua

101 lines
2.3 KiB
Lua

local createVersion = require('/apis/libversion');
local args = table.pack(...);
local FACE = {
'0000000000000000',
'0000000000000000',
'0011110000111100',
'0011110000111100',
'0011110000111100',
'0011110000111100',
'0000001111000000',
'0000001111000000',
'0000111111110000',
'0000111111110000',
'0000111111110000',
'0000111111110000',
'0000110000110000',
'0000110000110000',
'0000110000110000',
'0000000000000000',
};
local function printUsage()
print('creeper usage:');
print();
print(' creeper');
print(' creeper --version');
print(' creeper --help');
end
local function drawRun(x, y, len, color)
term.setBackgroundColor(color);
term.setCursorPos(x, y);
term.write(string.rep(' ', len));
end
local function drawCreeper()
local width, height = term.getSize();
local pixelW = math.max(1, math.floor(width / 20));
local pixelH = math.max(1, math.floor(height / 18));
if pixelW > 3 then pixelW = 3; end
if pixelH > 2 then pixelH = 2; end
local artW = #FACE[1] * pixelW;
local artH = #FACE * pixelH;
local startX = math.max(1, math.floor((width - artW) / 2) + 1);
local startY = math.max(1, math.floor((height - artH) / 2) + 1);
term.setBackgroundColor(colors.green);
term.setTextColor(colors.white);
term.clear();
for row = 1, #FACE do
for sy = 0, pixelH - 1 do
local y = startY + ((row - 1) * pixelH) + sy;
local col = 1;
while col <= #FACE[row] do
local bit = FACE[row]:sub(col, col);
local run = 1;
while col + run <= #FACE[row]
and FACE[row]:sub(col + run, col + run) == bit do
run = run + 1;
end
local color = colors.green;
if bit == '1' then
color = colors.black;
end
drawRun(startX + ((col - 1) * pixelW), y, run * pixelW, color);
col = col + run;
end
end
end
term.setBackgroundColor(colors.green);
term.setTextColor(colors.white);
term.setCursorPos(1, height);
end
local command = args[1];
if command == '-help' or command == '--help' or command == 'help' then
printUsage();
return;
end
if command == '-version' or command == '--version' or command == 'version' then
print('v' .. createVersion().forSelf());
return;
end
if command ~= nil then
printUsage();
return;
end
drawCreeper();