#Bitburner #Games #Snippets September 26, 2023

Bitburner: Starter Scripts

It's probably more than two years since I started playing Bitburner. It's a really good idle game I usually leave in the background running while working, sleeping, or literally anything.

But in the middle of those two years, I think I've stopped playing this game seriously twice (?) and returned three times now. Every time, I will reset my progress since I've forgotten how to play it.

This is a guide I've written mostly for future self.

Here we go.

To start, create a hacking script:
nano early-hack-template.js
const target = "joesguns";
const moneyThresh = ns.getServerMaxMoney(target);
const securityThresh = ns.getServerMinSecurityLevel(target);

while (true) {
  if (ns.getServerSecurityLevel(target) > securityThresh) {
    // If the server's security level is above our threshold, weaken it
    await ns.weaken(target);
  } else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
    // If the server's money is less than our threshold, grow it
    await ns.grow(target);
  } else {
    // Otherwise, hack it
    await ns.hack(target);
  }
}

No need to run it manually. Now create the following scripts:
nano phish.js
let companies = new Set();
let companiesToScan = ["home"];

while (companiesToScan.length > 0) {
  const company = companiesToScan.shift();
  const scannedData = ns.scan(company);
  scannedData.forEach((newCompany) => {
    if (!companies.has(newCompany)) {
      companies.add(newCompany);
      companiesToScan.push(newCompany);
    }
  });
}

const result = [...companies].filter(company => company !== "home");

result.forEach(company => {
  if (ns.getServerMaxRam(company) >= 2.40) {
    ns.scp("early-hack-template.js", company, "home")
  }
});
ns.exec("crack.js", "home")

nano crack.js
let companies = new Set();
let companiesToScan = ["home"];

while (companiesToScan.length > 0) {
  const company = companiesToScan.shift();
  const scannedData = ns.scan(company);
  scannedData.forEach((newCompany) => {
    if (!companies.has(newCompany)) {
      companies.add(newCompany);
      companiesToScan.push(newCompany);
    }
  });
}

const result = [...companies].filter(company => company !== "home");

result.forEach(company => {
  if (ns.getServerMaxRam(company) >= 2.40) {
    let ports = 0;
    ns.fileExists("BruteSSH.exe", "home") ? (ns.brutessh(company), ports = 1) : "";
    ns.fileExists("FTPCrack.exe", "home") ? (ns.ftpcrack(company), ports = 2) : "";
    ns.fileExists("relaySMTP.exe", "home") ? (ns.relaysmtp(company), ports = 3) : "";
    ns.fileExists("HTTPWorm.exe", "home") ? (ns.httpworm(company), ports = 4) : "";
    ns.fileExists("SQLInject.exe", "home") ? (ns.sqlinject(company), ports = 5) : "";

    ns.getServerNumPortsRequired(company) > ports ? "" : ns.nuke(company);
    ns.exec("early-hack-template.js", company, Math.floor(ns.getServerMaxRam(company) / 2.40))
  }
});

Create a hacknet script:
nano hacknet.js
while (true) {
  let newNodeCost = ns.hacknet.getPurchaseNodeCost();
  let playerMoney = ns.getServerMoneyAvailable("home");
  let ownedNodes = ns.hacknet.numNodes();
  let nodes = [];

  ownedNodes === 0 && ns.hacknet.purchaseNode();

  for (let i = 0; i < ownedNodes; i++) {
    let nodeLevel = ns.hacknet.getNodeStats(i).level;
    let nodeRam = ns.hacknet.getNodeStats(i).ram;
    let nodeCores = ns.hacknet.getNodeStats(i).cores;
    nodes.push({ index: i, level: nodeLevel, ram: nodeRam, cores: nodeCores });
  }

  nodes.sort((a, b) => a.ram - b.ram);

  nodes.forEach(data => {
    playerMoney > ns.hacknet.getRamUpgradeCost(data.index, 1) && ns.hacknet.upgradeRam(data.index, 1);
  });

  playerMoney > newNodeCost && ns.hacknet.purchaseNode();

  nodes.sort((a, b) => a.level - b.level);

  nodes.forEach(data => {
    playerMoney > ns.hacknet.getLevelUpgradeCost(data.index, 1) && ns.hacknet.getLevelUpgradeCost(data.index, 1) < (0.3 * newNodeCost) && ns.hacknet.upgradeLevel(data.index, 1);
  });

  nodes.sort((a, b) => a.cores - b.cores);
  nodes.forEach(data => {
    playerMoney > ns.hacknet.getCoreUpgradeCost(data.index, 1) && ns.hacknet.getCoreUpgradeCost(data.index, 1) < (0.3 * newNodeCost) && ns.hacknet.upgradeCore(data.index, 1);
  });

  await ns.sleep(1000);
}

And lastly, a script to purchase and upgrade servers automatically:

nano purchase-servers.js
const ram = 8;
let i = 1;

while (i < 26) {
  if (ns.getServerMoneyAvailable("home") > ns.getPurchasedServerCost(ram)) {
    let hostname = ns.purchaseServer("cloud-" + i, ram);
    ns.scp("early-hack-template.js", hostname);
    ns.exec("early-hack-template.js", hostname, 3);
    ++i;
  }
  await ns.sleep(1000);
}

nano upgrade-servers.js
let servers = ns.getPurchasedServers();
let maxRam = ns.getPurchasedServerMaxRam();
let baseRamX = ns.getServerMaxRam(servers[0]) * 4
let upgradeRam = baseRamX < maxRam ? baseRamX : maxRam;
let upgradeCost = ns.getPurchasedServerUpgradeCost(servers[0], upgradeRam);
let hackScript = "early-hack-template.js";

for (let i = 0; i < servers.length; i++) {
  while (ns.getServerMaxRam(servers[i]) !== upgradeRam) {
    if (ns.getServerMoneyAvailable("home") > upgradeCost) {
      ns.upgradePurchasedServer(servers[i], upgradeRam);
      ns.kill(hackScript, servers[i]);
      ns.exec(hackScript, servers[i], Math.floor(upgradeRam / 2.40));
      ns.tprint(`Done upgrading ${servers[i]}`);
    }
    await ns.sleep(1000);
  }
}

ns.tprint("Done upgrading all servers.");
ns.spawn("upgrade-servers.js", 1);

And that's it! That should be enough to get started smoothly.

SEARCH