├── README.md ├── analyze_server.js ├── basic_hack.js ├── custom-stats.js ├── deploy.js ├── find_coding_contract.js ├── find_server.js ├── monitor.js └── opened_servers.js /README.md: -------------------------------------------------------------------------------- 1 | # bitburner-scripts 2 | Repository of Bitburner scripts 3 | 4 | These scripts help demonstrate the possibilities that bitburner offers, however 5 | they are insufficient to complete the game. 6 | 7 | Most of these scripts can be run with `--help` to explain what they do and how they should be used. 8 | 9 | Example: 10 | 11 | run find_server.js --help -------------------------------------------------------------------------------- /analyze_server.js: -------------------------------------------------------------------------------- 1 | export async function main(ns) { 2 | const args = ns.flags([["help", false]]); 3 | const server = ns.args[0]; 4 | if (args.help || !server) { 5 | ns.tprint("This script does a more detailed analysis of a server."); 6 | ns.tprint(`Usage: run ${ns.getScriptName()} SERVER`); 7 | ns.tprint("Example:"); 8 | ns.tprint(`> run ${ns.getScriptName()} n00dles`); 9 | return; 10 | } 11 | const ram = ns.getServerRam(server); 12 | const money = ns.getServerMoneyAvailable(server); 13 | const maxMoney = ns.getServerMaxMoney(server); 14 | const minSec = ns.getServerMinSecurityLevel(server); 15 | const sec = ns.getServerSecurityLevel(server); 16 | ns.tprint(` 17 | 18 | ${server}: 19 | RAM : ${ram[1]} / ${ram[0]} (${ram[1] / ram[0] * 100}%) 20 | $ : ${ns.nFormat(money, "$0.000a")} / ${ns.nFormat(maxMoney, "$0.000a")} (${(money / maxMoney * 100).toFixed(2)}%) 21 | security : ${minSec.toFixed(2)} / ${sec.toFixed(2)} 22 | growth : ${ns.getServerGrowth(server)} 23 | hack time : ${ns.tFormat(ns.getHackTime(server))} 24 | grow time : ${ns.tFormat(ns.getGrowTime(server))} 25 | weaken time: ${ns.tFormat(ns.getWeakenTime(server))} 26 | grow x2 : ${(ns.growthAnalyze(server, 2)).toFixed(2)} threads 27 | grow x3 : ${(ns.growthAnalyze(server, 3)).toFixed(2)} threads 28 | grow x4 : ${(ns.growthAnalyze(server, 4)).toFixed(2)} threads 29 | hack 10% : ${(.10 / ns.hackAnalyze(server)).toFixed(2)} threads 30 | hack 25% : ${(.25 / ns.hackAnalyze(server)).toFixed(2)} threads 31 | hack 50% : ${(.50 / ns.hackAnalyze(server)).toFixed(2)} threads 32 | hackChance : ${(ns.hackAnalyzeChance(server) * 100).toFixed(2)}% 33 | `); 34 | } -------------------------------------------------------------------------------- /basic_hack.js: -------------------------------------------------------------------------------- 1 | export async function main(ns) { 2 | const args = ns.flags([['help', false]]); 3 | const hostname = args._[0]; 4 | if(args.help || !hostname) { 5 | ns.tprint("This script will generate money by hacking a target server."); 6 | ns.tprint(`USAGE: run ${ns.getScriptName()} SERVER_NAME`); 7 | ns.tprint("Example:"); 8 | ns.tprint(`> run ${ns.getScriptName()} n00dles`); 9 | return; 10 | } 11 | while (true) { 12 | if (ns.getServerSecurityLevel(hostname) > ns.getServerMinSecurityLevel(hostname)) { 13 | await ns.weaken(hostname); 14 | } else if (ns.getServerMoneyAvailable(hostname) < ns.getServerMaxMoney(hostname)) { 15 | await ns.grow(hostname); 16 | } else { 17 | await ns.hack(hostname); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /custom-stats.js: -------------------------------------------------------------------------------- 1 | /** @param {NS} ns **/ 2 | export async function main(ns) { 3 | const args = ns.flags([["help", false]]); 4 | if (args.help) { 5 | ns.tprint("This script will enhance your HUD (Heads up Display) with custom statistics."); 6 | ns.tprint(`Usage: run ${ns.getScriptName()}`); 7 | ns.tprint("Example:"); 8 | ns.tprint(`> run ${ns.getScriptName()}`); 9 | return; 10 | } 11 | 12 | const doc = document; // This is expensive! (25GB RAM) Perhaps there's a way around it? ;) 13 | const hook0 = doc.getElementById('overview-extra-hook-0'); 14 | const hook1 = doc.getElementById('overview-extra-hook-1'); 15 | while (true) { 16 | try { 17 | const headers = [] 18 | const values = []; 19 | // Add script income per second 20 | headers.push("ScrInc"); 21 | values.push(ns.getScriptIncome()[0].toPrecision(5) + '/sec'); 22 | // Add script exp gain rate per second 23 | headers.push("ScrExp"); 24 | values.push(ns.getScriptExpGain().toPrecision(5) + '/sec'); 25 | // TODO: Add more neat stuff 26 | 27 | // Now drop it into the placeholder elements 28 | hook0.innerText = headers.join(" \n"); 29 | hook1.innerText = values.join("\n"); 30 | } catch (err) { // This might come in handy later 31 | ns.print("ERROR: Update Skipped: " + String(err)); 32 | } 33 | await ns.sleep(1000); 34 | } 35 | } -------------------------------------------------------------------------------- /deploy.js: -------------------------------------------------------------------------------- 1 | /** @param {NS} ns **/ 2 | export async function main(ns) { 3 | const args = ns.flags([["help", false]]); 4 | if (args.help || args._.length < 2) { 5 | ns.tprint("This script deploys another script on a server with maximum threads possible."); 6 | ns.tprint(`Usage: run ${ns.getScriptName()} HOST SCRIPT ARGUMENTS`); 7 | ns.tprint("Example:"); 8 | ns.tprint(`> run ${ns.getScriptName()} n00dles basic_hack.js foodnstuff`); 9 | return; 10 | } 11 | 12 | const host = args._[0]; 13 | const script = args._[1]; 14 | const script_args = args._.slice(2); 15 | 16 | if (!ns.serverExists(host)) { 17 | ns.tprint(`Server '${host}' does not exist. Aborting.`); 18 | return; 19 | } 20 | if (!ns.ls(ns.getHostname()).find(f => f === script)) { 21 | ns.tprint(`Script '${script}' does not exist. Aborting.`); 22 | return; 23 | } 24 | 25 | const threads = Math.floor((ns.getServerMaxRam(host) - ns.getServerUsedRam(host)) / ns.getScriptRam(script)); 26 | ns.tprint(`Launching script '${script}' on server '${host}' with ${threads} threads and the following arguments: ${script_args}`); 27 | await ns.scp(script, ns.getHostname(), host); 28 | ns.exec(script, host, threads, ...script_args); 29 | } 30 | -------------------------------------------------------------------------------- /find_coding_contract.js: -------------------------------------------------------------------------------- 1 | function scan(ns, parent, server, list) { 2 | const children = ns.scan(server); 3 | for (let child of children) { 4 | if (parent == child) { 5 | continue; 6 | } 7 | list.push(child); 8 | 9 | scan(ns, server, child, list); 10 | } 11 | } 12 | 13 | export function list_servers(ns) { 14 | const list = []; 15 | scan(ns, '', 'home', list); 16 | return list; 17 | } 18 | 19 | export async function main(ns) { 20 | const args = ns.flags([["help", false]]); 21 | if (args.help) { 22 | ns.tprint("This script helps you find an unsolved coding contract."); 23 | ns.tprint(`Usage: run ${ns.getScriptName()}`); 24 | ns.tprint("Example:"); 25 | ns.tprint(`> run ${ns.getScriptName()}`); 26 | return; 27 | } 28 | 29 | let servers = list_servers(ns); 30 | const boughtServers = ns.getPurchasedServers(ns); 31 | servers = servers.filter(s => !boughtServers.includes(s)); 32 | const hostname = servers.find(s => ns.ls(s).find(f => f.endsWith(".cct"))) 33 | if(!hostname) { 34 | ns.tprint("No coding contract found."); 35 | return; 36 | } 37 | 38 | ns.tprint(`Found coding contract on '${hostname}'.`) 39 | } -------------------------------------------------------------------------------- /find_server.js: -------------------------------------------------------------------------------- 1 | function recursiveScan(ns, parent, server, target, route) { 2 | const children = ns.scan(server); 3 | for (let child of children) { 4 | if (parent == child) { 5 | continue; 6 | } 7 | if (child == target) { 8 | route.unshift(child); 9 | route.unshift(server); 10 | return true; 11 | } 12 | 13 | if (recursiveScan(ns, server, child, target, route)) { 14 | route.unshift(server); 15 | return true; 16 | } 17 | } 18 | return false; 19 | } 20 | 21 | export async function main(ns) { 22 | const args = ns.flags([["help", false]]); 23 | let route = []; 24 | let server = args._[0]; 25 | if (!server || args.help) { 26 | ns.tprint("This script helps you find a server on the network and shows you the path to get to it."); 27 | ns.tprint(`Usage: run ${ns.getScriptName()} SERVER`); 28 | ns.tprint("Example:"); 29 | ns.tprint(`> run ${ns.getScriptName()} n00dles`); 30 | return; 31 | } 32 | 33 | recursiveScan(ns, '', 'home', server, route); 34 | for (const i in route) { 35 | await ns.sleep(500); 36 | const extra = i > 0 ? "└ " : ""; 37 | ns.tprint(`${" ".repeat(i)}${extra}${route[i]}`); 38 | } 39 | } 40 | 41 | export function autocomplete(data, args) { 42 | return data.servers; 43 | } -------------------------------------------------------------------------------- /monitor.js: -------------------------------------------------------------------------------- 1 | export async function main(ns) { 2 | const flags = ns.flags([ 3 | ['refreshrate', 200], 4 | ['help', false], 5 | ]) 6 | if (flags._.length === 0 || flags.help) { 7 | ns.tprint("This script helps visualize the money and security of a server."); 8 | ns.tprint(`USAGE: run ${ns.getScriptName()} SERVER_NAME`); 9 | ns.tprint("Example:"); 10 | ns.tprint(`> run ${ns.getScriptName()} n00dles`) 11 | return; 12 | } 13 | ns.tail(); 14 | ns.disableLog('ALL'); 15 | while (true) { 16 | const server = flags._[0]; 17 | let money = ns.getServerMoneyAvailable(server); 18 | if (money === 0) money = 1; 19 | const maxMoney = ns.getServerMaxMoney(server); 20 | const minSec = ns.getServerMinSecurityLevel(server); 21 | const sec = ns.getServerSecurityLevel(server); 22 | ns.clearLog(server); 23 | ns.print(`${server}:`); 24 | ns.print(` $_______: ${ns.nFormat(money, "$0.000a")} / ${ns.nFormat(maxMoney, "$0.000a")} (${(money / maxMoney * 100).toFixed(2)}%)`); 25 | ns.print(` security: +${(sec - minSec).toFixed(2)}`); 26 | ns.print(` hack____: ${ns.tFormat(ns.getHackTime(server))} (t=${Math.ceil(ns.hackAnalyzeThreads(server, money))})`); 27 | ns.print(` grow____: ${ns.tFormat(ns.getGrowTime(server))} (t=${Math.ceil(ns.growthAnalyze(server, maxMoney / money))})`); 28 | ns.print(` weaken__: ${ns.tFormat(ns.getWeakenTime(server))} (t=${Math.ceil((sec - minSec) * 20)})`); 29 | await ns.sleep(flags.refreshrate); 30 | } 31 | } 32 | 33 | export function autocomplete(data, args) { 34 | return data.servers; 35 | } -------------------------------------------------------------------------------- /opened_servers.js: -------------------------------------------------------------------------------- 1 | function scan(ns, parent, server, list) { 2 | const children = ns.scan(server); 3 | for (let child of children) { 4 | if (parent == child) { 5 | continue; 6 | } 7 | list.push(child); 8 | 9 | scan(ns, server, child, list); 10 | } 11 | } 12 | 13 | export function list_servers(ns) { 14 | const list = []; 15 | scan(ns, '', 'home', list); 16 | return list; 17 | } 18 | 19 | /** @param {NS} ns **/ 20 | export async function main(ns) { 21 | const args = ns.flags([["help", false]]); 22 | if (args.help) { 23 | ns.tprint("This script lists all servers on which you can run scripts."); 24 | ns.tprint(`Usage: run ${ns.getScriptName()}`); 25 | ns.tprint("Example:"); 26 | ns.tprint(`> run ${ns.getScriptName()}`); 27 | return; 28 | } 29 | 30 | const servers = list_servers(ns).filter(s => ns.hasRootAccess(s)).concat(['home']); 31 | for(const server of servers) { 32 | const used = ns.getServerUsedRam(server); 33 | const max = ns.getServerMaxRam(server); 34 | ns.tprint(`${server} is opened. ${used} GB / ${max} GB (${(100*used/max).toFixed(2)}%)`) 35 | } 36 | } --------------------------------------------------------------------------------