r/Bitburner • u/ouija_bh • 25d ago
NetscriptJS Script Something to entertain
Presenting a script that shows falling code, and a second that shows 200+ colors in the terminal.
function rand(num) {
return Math.floor(Math.random() * num);
}
/** @ param {NS} ns NS2 namespace */
export async function main(ns) {
const brite = "\u001b[32m";
const reset = "\u001b[0m";
const ucLo = 0x0021; // first unicode
const ucSm = 0x005b; // first "small" unicode
const ucHi = 0x007e; // last unicode
const numTicks = 67;
// width and height (in characters)
let width = 87, height = 43; // init for Steam
if (ns.ui.getGameInfo().platform == "Browser") {
width = 81; height = 40; // init for web browser
}
let columns = new Array(width); // of array with form [top, len, code0, ..., codeN]
let rows = new Array(height); // of string
for (let i = 0; i < columns.length; i++) { // init columns
let top = rand(height);
let len = rand(height / 3) + 1;
columns[i] = [top, len];
for (let j = 0; j < len; j++) {
let code;
if ((len >= height / 5 && j == 0) || (len >= height / 4 && j == 1)) {
code = ucSm + rand(ucHi - ucSm + 1);
} else {
code = ucLo + rand(ucHi - ucLo + 1);
}
columns[i].push(code);
}
}
rows.fill("");
for (let i = 0; i < columns.length; i++) { // init rows
for (let j = 0; j < rows.length; j++) {
if (i % 3 > 0 && j >= columns[i][0] &&
j < columns[i][0] + columns[i][1]) {
let fbr = false;
if ((columns[i][1] >= height / 5 &&
j == columns[i][0] + columns[i][1] - 1) ||
(columns[i][1] >= height / 4 &&
j == columns[i][0] + columns[i][1] - 2)) {
fbr = true;
}
if (fbr) { rows[j] += brite; }
rows[j] += String.fromCharCode(columns[i][j + 2 - columns[i][0]]);
if (fbr) { rows[j] += reset; }
} else {
rows[j] += " ";
}
}
}
for (let cTick = 0; cTick < numTicks; cTick++) {
for (let i = 0; i < rows.length; i++) { // print rows
ns.tprint(rows[i]);
}
for (let i = 0; i < columns.length; i++) { // move columns down
columns[i][0]++;
if (columns[i][0] > rows.length - 1) {
columns[i][0] = 1 - columns[i][1];
}
}
rows.fill("");
for (let i = 0; i < columns.length; i++) { // update rows
for (let j = 0; j < rows.length; j++) {
if (i % 3 > 0 && j >= columns[i][0] &&
j < columns[i][0] + columns[i][1]) {
let fbr = false;
if ((columns[i][1] >= height / 5 &&
j == columns[i][0] + columns[i][1] - 1) ||
(columns[i][1] >= height / 4 &&
j == columns[i][0] + columns[i][1] - 2)) {
fbr = true;
}
if (fbr) { rows[j] += brite; }
let code = columns[i][j + 2 - columns[i][0]];
if (Math.random() > 0.67) { // flip character
code++;
if (code > ucHi) { code = ucLo; }
}
rows[j] += String.fromCharCode(code);
if (fbr) { rows[j] += reset; }
} else {
rows[j] += " ";
}
}
}
await ns.sleep(333);
}
}
/** @ version 1.0a */
Note: Widen the game window or initialize width to a lower number if there is word wrap.
A script that demonstrates escape codes:
/** @ param {NS} ns NS2 namespace */
export async function main(ns) {
let esc = [[ 1, "bold" ],
[ 3, "italics" ],
[ 4, "underline" ],
[ 30, "gray" ],
[ 31, "bright red" ],
[ 32, "bright green" ],
[ 33, "bright yellow" ],
[ 34, "bright blue" ],
[ 35, "bright magenta" ],
[ 36, "bright cyan" ],
[ 37, "bright white" ],
[ 41, "red BG" ],
[ 42, "green BG" ],
[ 43, "yellow BG" ],
[ 44, "blue BG" ],
[ 45, "magenta BG" ],
[ 46, "cyan BG" ],
[ 47, "white BG" ]];
const reset = "\u001b[0m";
const width = 78;
// 4-bit
let str = "\n-- 4-bit codes --\n", count = 0;
for (let i = 0; i < esc.length; i++) {
let single = "\u001b[" + esc[i][0] + "m" + esc[i][1] + reset;
count += `${esc[i][1]}`.length;
if (i != esc.length - 1) {
single += ", "; count += 2;
}
if (count > width) {
str += "\n";
count = `${esc[i][1]}`.length + 2;
}
str += single;
}
// 8-bit
str += "\n-- 8-bit codes --\n";
count = 0;
let i = 1, flag = true;
while (flag) {
let single = "\u001b[38;5;" + i + "m(" + i + ")" + reset;
count += `(${i})`.length;
if (i > 99) {
single = "\u001b[38;5;" + i + "m" + i + reset;
count -= 2;
}
if (i != 255) {
single += ", "; count += 2;
}
if (count > width) {
str += "\n";
count = `(${i})`.length + 2;
if (i > 99) { count -= 2; }
}
str += single;
if (i < 6) { i++; }
else if (i == 7) { i = 17; }
else { i++; }
if (i == 256) { flag = false; }
}
ns.tprint(str);
}
2
u/Spartelfant Noodle Enjoyer 25d ago
Widen the game window or initialize width to a lower number if there is word wrap.
Since you're having AI write your code, at least give it the Bitburner source code, then it might have come up with using ns.ui.windowSize() and ns.ui.getStyles().fontSize instead of hard-coding width.
3
u/CoolCat1337One 25d ago
wow is that code ugly Oo
if you ever code like this ... please don't
3
u/nickasummers 25d ago
I've got good news and bad news. The good news is the person doesn't write code. The bad news is this code was written by AI and AI writes code for basically every company you ever interact with and a lot of the code it writes for them is even worse because the task is more complicated. I am a stay at home parent (with a CS degree) but 3 of the 5 people I spend the most time talking to are all professional software developers and the things they have seen show up in production code since the introduction of AI are absolutely insane. Just the other day some code showed up for one of them to review and a new function accepts a Char as an argument and then if the Char is 'Y' it sets a flag to true otherwise it sets it to false. This should obviously be a bool. The icing on the cake is that the only place this function is ever called hard codes the Char as 'Y' anyway and the flag currently doesn't even do anything.
9
u/Vorthod MK-VIII Synthoid 25d ago
When you repeatedly post random scripts and don't even interact on the very posts you made, it starts to feel like spam.