Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const prefix = "!niccc";
- const botname = "BONZICCC (!niccc)";
- const defaultPolyURL = "https://files.catbox.moe/r5mp9l.bin";
- let polyStream = null;
- let polyOffset = 0;
- let polyColors = Array(16).fill().map(() => ({r:0,g:0,b:0,a:255}));
- let polyAlign = false;
- let polyRunning = false;
- let polyAutoInterval = null;
- let polyAutoSpeed = 1000;
- let polyFrame = 0;
- let polyDisplay = Array(256*200).fill(0);
- function sendMsg(msg) {
- setTimeout(() => {
- socket.emit("talk", { text: msg });
- }, 1100);
- }
- setTimeout(() => { socket.emit("command", { list: ["name", botname] }) }, 1000);
- setTimeout(() => { socket.emit("command", { list: ["name", botname] }) }, 2100);
- setTimeout(() => {
- sendMsg("BONZICCC is online! Type !niccc to run the Demo!");
- setInterval(() => { sendMsg("BONZICCC is online! Type !niccc to run the Demo!"); }, 60000);
- }, 3200);
- function resetPoly() {
- polyOffset = 0;
- polyColors = Array(16).fill().map(() => ({r:0,g:0,b:0,a:255}));
- polyAlign = false;
- polyRunning = false;
- polyFrame = 0;
- polyDisplay.fill(0);
- stopPolyAuto();
- }
- function stopPolyAuto() {
- if (polyAutoInterval !== null) {
- clearInterval(polyAutoInterval);
- polyAutoInterval = null;
- }
- }
- function startPolyAuto(intervalMs = polyAutoSpeed) {
- stopPolyAuto();
- polyAutoInterval = setInterval(() => {
- if (!polyRunning) {
- stopPolyAuto();
- return;
- }
- for (let i = 0; i < 2; i++) polyStep();
- sendMsg(renderPolyBraille14x6());
- }, intervalMs);
- }
- function polyDecode(raw) {
- raw &= 0xF;
- let bits = (raw >> 3) & 1;
- bits |= (raw << 1) & 0xE;
- return bits;
- }
- function polyReadU8() {
- if (polyOffset >= polyStream.length) return 0;
- return polyStream[polyOffset++];
- }
- function polyReadU16() {
- let hi = polyReadU8();
- let lo = polyReadU8();
- return (hi << 8) | lo;
- }
- function polyStep() {
- if (!polyStream) return;
- polyDisplay.fill(0);
- let flags = polyReadU8();
- if (polyOffset >= polyStream.length) { polyRunning = false; return; }
- if (flags & 2) {
- let colors = polyReadU16();
- for (let i = 0; i < 16; i++) {
- if (colors & (1 << (15 - i))) {
- let color = polyReadU16();
- polyColors[i] = {
- r: polyDecode(color) * 17,
- g: polyDecode(color >> 12) * 17,
- b: polyDecode(color >> 8) * 17,
- a: 255
- };
- }
- }
- }
- if (flags & 1) {
- for (let i = 0; i < polyDisplay.length; i++) polyDisplay[i] = 0;
- }
- if (flags & 4) {
- let vertices = polyReadU8();
- let points = [];
- for (let i = 0; i < vertices; i++) {
- let x = polyReadU8();
- let y = polyReadU8();
- points.push({x, y});
- }
- while (true) {
- let bits = polyReadU8();
- if (bits === 0xFF) break;
- if (bits === 0xFE) { polyAlign = true; break; }
- if (bits === 0xFD) { polyRunning = false; break; }
- let colorIndex = bits >> 4;
- let numPoints = bits & 0xF;
- let poly = [];
- for (let i = 0; i < numPoints; i++) {
- let idx = polyReadU8();
- poly.push(points[idx]);
- }
- drawPolygonToBuffer(poly, colorIndex);
- }
- } else {
- while (true) {
- let bits = polyReadU8();
- if (bits === 0xFF) break;
- if (bits === 0xFE) { polyAlign = true; break; }
- if (bits === 0xFD) { polyRunning = false; break; }
- let colorIndex = bits >> 4;
- let numPoints = bits & 0xF;
- let poly = [];
- for (let i = 0; i < numPoints; i++) {
- let x = polyReadU8();
- let y = polyReadU8();
- poly.push({x, y});
- }
- drawPolygonToBuffer(poly, colorIndex);
- }
- }
- polyFrame++;
- if (polyAlign) {
- polyOffset = (polyOffset + 0x10000) & ~0xFFFF;
- }
- }
- function drawPolygonToBuffer(points, colorIndex) {
- for (let i = 0; i < points.length; i++) {
- let p0 = points[i];
- let p1 = points[(i+1)%points.length];
- drawLineToBuffer(p0.x, p0.y, p1.x, p1.y, colorIndex);
- }
- }
- function drawLineToBuffer(x0, y0, x1, y1, colorIndex) {
- let dx = Math.abs(x1 - x0), dy = Math.abs(y1 - y0);
- let sx = x0 < x1 ? 1 : -1, sy = y0 < y1 ? 1 : -1;
- let err = dx - dy;
- while (true) {
- if (x0 >= 0 && x0 < 256 && y0 >= 0 && y0 < 200)
- polyDisplay[x0 + y0*256] = colorIndex+1;
- if (x0 === x1 && y0 === y1) break;
- let e2 = 2*err;
- if (e2 > -dy) { err -= dy; x0 += sx; }
- if (e2 < dx) { err += dx; y0 += sy; }
- }
- }
- function renderPolyBraille14x6() {
- const HI_W = 28, HI_H = 12;
- const PIXEL_W = 256 / HI_W, PIXEL_H = 200 / HI_H;
- let hiGrid = [];
- for (let by = 0; by < HI_H; by++) {
- let line = [];
- for (let bx = 0; bx < HI_W; bx++) {
- let code = 0x2800;
- for (let dy = 0; dy < 4; dy++) {
- for (let dx = 0; dx < 2; dx++) {
- let px = Math.floor(bx * PIXEL_W + dx * (PIXEL_W / 2));
- let py = Math.floor(by * PIXEL_H + dy * (PIXEL_H / 4));
- let idx = px + py * 256;
- let dot = (idx < polyDisplay.length) ? (polyDisplay[idx] ? 1 : 0) : 0;
- if (dot) {
- const dotIndex = [
- [0, 1, 2, 6],
- [3, 4, 5, 7]
- ][dx][dy];
- code |= (1 << dotIndex);
- }
- }
- }
- line.push(code);
- }
- hiGrid.push(line);
- }
- const LO_W = 14, LO_H = 6;
- let out = [];
- for (let by = 0; by < LO_H; by++) {
- let line = "";
- for (let bx = 0; bx < LO_W; bx++) {
- let merged = 0x2800;
- for (let dy = 0; dy < 2; dy++) {
- for (let dx = 0; dx < 2; dx++) {
- let srcY = by*2 + dy, srcX = bx*2 + dx;
- if (srcY < HI_H && srcX < HI_W) {
- let cell = hiGrid[srcY][srcX] - 0x2800;
- merged |= cell;
- }
- }
- }
- line += String.fromCharCode(merged);
- }
- out.push(line);
- }
- return out.join("\n");
- }
- socket.on("talk", function (message) {
- if (message.text.trim() !== prefix) return;
- stopPolyAuto();
- resetPoly();
- sendMsg("Downloading official scene1.bin polygon stream...");
- fetch(defaultPolyURL)
- .then(response => response.arrayBuffer())
- .then(buffer => {
- polyStream = new Uint8Array(buffer);
- polyRunning = true;
- sendMsg("Polygon stream loaded! Running demo...");
- startPolyAuto(polyAutoSpeed);
- })
- .catch(() => sendMsg("Failed to download polygon stream from Catbox."));
- });
Add Comment
Please, Sign In to add comment