HomeGame GuidesIntermediate Auto Farm And Modules - Bitburner

Intermediate Auto Farm And Modules – Bitburner

Published on

ADVERTISEMENT

By PG SDVX

A plug and play, set and forget script that’ll farm in a moderately optimised fashion

Here we go again, V2 has arrived!

Hello once more, today I bring you a better chunk of code! But it’s slightly less easy to understand

100% plug and play, but put it in a .ns file instead of .script, create in and run from home

NOTICE: It will automatically create/overwrite 3 files in the home directory called “weak.script”, “grow.script” and “hack.script”

This guide has a little less documentation, but much higher gains than the last

Let me know if there is any issue you can see, or any way to improve it further πŸ™‚

Please note that it takes a while to warm up the wallets before the farming can start

Stable Code

/** @param {NS} ns**/

export async function main(ns) {

ns.disableLog(“ALL”); //Visual clarity

//Welcome to the Auto Farm part 2: Electric Boogaloo

//This script is a little more complicated to explain easily, it dedicates high RAM servers to attack high profit servers

//This is also set and forget, your EXEs and hacking level are reacquired each second, so new servers are added without needing to reboot it

//Well I hope this brings you ideas, knowledge and or profits πŸ˜€

var files = [“weak.script”, “grow.script”, “hack.script”];//No touching, unless you understand everything here

await ns.write(files[0], “weaken(args)”, “w”); await ns.write(files[1], “grow(args)”, “w”); await ns.write(files[2], “hack(args)”, “w”);

var serverList; var targetList; var hostList; var exes; var temp; var manager = false;

var cycle = [0, “─”, “\\”, “|”, “/”]; var latest = [[“-“, “-“], [“-“, “-“], [“-“, “-“]];

if (false) { brutessh(); ftpcrack(); relaysmtp(); httpworm(); sqlinject() } //Avoid RAM cost bypass error

var pServers = await ns.prompt(“Use player servers as hosts?”);

async function scanExes() {

exes = [“BruteSSH”, “FTPCrack”, “relaySMTP”, “SQLInject”, “HTTPWorm”];

for (let i = 0; i <= exes.length – 1; i++) { if (!ns.fileExists(exes[i] + “.exe”)) { exes.splice(i, 1); i– } }//Removes EXEs you don’t have

}

function arraySort(array) { return array.sort(function (a, b) { return b[0] – a[0] }) }//Sorts nested arrays

function logBalance(server) {//For balance in display

return [ns.nFormat(ns.getServerMoneyAvailable(server), ‘0a’)] + ” / ” + [ns.nFormat(ns.getServerMaxMoney(server), ‘0a’)]

+ ” : ” + ns.nFormat(ns.getServerMoneyAvailable(server) / ns.getServerMaxMoney(server), ‘0%’)

}

async function log() {//The display

if (cycle[0] >= 4) { cycle[0] = 0 }; cycle[0]++;//Speen

ns.clearLog();

ns.print(“╔═══╦═╣ HOST ╠════════════════╣ TARGET ╠═╗”);

ns.print(“β•‘ G β•‘ ” + latest[0][0] + latest[0][1].padStart(34 – latest[0][0].length) + ” β•‘”)

ns.print(“β•‘ W β•‘ ” + latest[1][0] + latest[1][1].padStart(34 – latest[1][0].length) + ” β•‘”)

ns.print(“β•‘ H β•‘ ” + latest[2][0] + latest[2][1].padStart(34 – latest[2][0].length) + ” β•‘”)

ns.print(“β•‘ ” + cycle[cycle[0]] + ” ╠════════════════════════════════════╣”)

if (targetList.length < 6) { ns.print(“β•šβ•β•β•β•                                    β•‘”) } else {

ns.print(“╠═══╝ Priority Servers       Balance     β•‘”)

for (let i = 0; i < 6; i++) {

temp = targetList[i][1];

ns.print(“β•‘ > ” + temp + logBalance(temp).padStart(36 – temp.length) + ” β•‘”)

}

ns.print(“╠════════════════════════════════════════╝”)

ns.print(“β•‘ EXE ” + exes.length + “/5 β•‘ HOSTS ” + hostList.length + ” β•‘ TARGETS ” + targetList.length)

ns.print(“╠════════════════════════════════════════╗”)

if (manager) {

ns.print(“╠══════╣ Managing ” + ns.hacknet.numNodes() + ” HNet Nodes β• ”.padEnd(21, “═”) + “β•£”)

}

}

}

async function scanServers() {//Finds all servers

serverList = ns.scan(“home”); let serverCount = [serverList.length, 0]; let depth = 0; let checked = 0; let scanIndex = 0;

while (scanIndex <= serverCount[depth] – 1) {

let results = ns.scan(serverList[checked]); checked++;

for (let i = 0; i <= results.length – 1; i++) {

if (results[i] != “home” && !serverList.includes(results[i])) {

serverList.push(results[i]); serverCount[depth + 1]++

}

}

if (scanIndex == serverCount[depth] – 1) { scanIndex = 0; depth++; serverCount.push(0) } else { scanIndex++ };

}

}

async function checkServers() {//Sorts servers into lists based on RAM and money/hack time ratio: hostList and targetList

targetList = []; hostList = [[ns.getServerMaxRam(“home”), “home”]];

if (pServers) {//Adds in player servers

temp = ns.getPurchasedServers();

for (let i = 0; i < temp.length; i++) {

hostList.push([ns.getServerMaxRam(temp[i]), temp[i]])

await ns.scp(files, “home”, temp[i]);

}

}

for (let i = 0; i <= serverList.length – 1; i++) {

let cTarget = serverList[i];

if (ns.getServerMoneyAvailable(cTarget) > 0 || ns.getServerMaxRam(cTarget) > 2) {//Filters out servers like darkweb

if (ns.getServerNumPortsRequired(cTarget) <= exes.length) {

for (let i = 0; i <= exes.length – 1; i++) { ns[exes[i].toLowerCase()](cTarget) }//Runs all EXEs you have

ns.nuke(cTarget);//Ghandi.jpeg

temp = [Math.floor(ns.getServerMaxMoney(cTarget) / ns.getServerMinSecurityLevel(cTarget)), cTarget];

if (ns.getServerMoneyAvailable(cTarget) != 0 && !targetList.includes(temp) && ns.getServerRequiredHackingLevel(cTarget) <= ns.getHackingLevel()) {

targetList.push(temp); targetList = arraySort(targetList);

}

temp = [ns.getServerMaxRam(cTarget), cTarget];

if (ns.getServerMaxRam(cTarget) > 2 && !hostList.includes(cTarget)) {

hostList.push(temp); hostList = arraySort(hostList)

}

await ns.scp(files, “home”, cTarget);

}

}

}

}

async function hackAll() {//Dedicates high RAM servers to attack high profit per second servers

let tarIndex = 0; let loop = false;

for (let i = 0; i <= hostList.length – 1; i++) {

if (tarIndex > targetList.length – 1) { tarIndex = 0; loop = true };

let hHost = hostList[i][1]; let hTarget = targetList[tarIndex][1]; let freeRam;

if (hHost == “home”) { freeRam = Math.max(ns.getServerMaxRam(hHost) – ns.getServerUsedRam(hHost) – 50, 0) } else {

freeRam = ns.getServerMaxRam(hHost) – ns.getServerUsedRam(hHost)

}

if (freeRam >= 4) {

let threads = Math.floor(freeRam / 1.75); let bThreads = 0;

if (ns.getServerMoneyAvailable(hTarget) < ns.getServerMaxMoney(hTarget) * .70 || loop) {//Server money target here

latest[0][0] = hHost; latest[0][1] = hTarget;

if (threads > 2) {

ns.exec(“weak.script”, hHost, Math.ceil(0.08 * threads), hTarget);

ns.exec(“grow.script”, hHost, Math.floor(0.92 * threads), hTarget);

} else { ns.exec(“grow.script”, hHost, threads, hTarget) }

} else if (ns.getServerSecurityLevel(hTarget) > ns.getServerMinSecurityLevel(hTarget) + 5) {//Security target here

latest[1][0] = hHost; latest[1][1] = hTarget;

ns.exec(“weak.script”, hHost, threads, hTarget);

} else {

while (parseFloat(ns.hackAnalyze(hTarget)) * threads > .4) { threads–; bThreads++ }//Hack limit here

latest[2][0] = hHost; latest[2][1] = hTarget;

ns.exec(“hack.script”, hHost, threads, hTarget);

if (bThreads > 0) { ns.exec(“weak.script”, hHost, bThreads, hTarget) }

}

}

tarIndex++

}

}

//Put modules below here

manager = await ns.prompt(“Activate Hacknet Manager?”);

async function hnManager() {

let mode = [“Level”, “Ram”, “Core”]

function check(q) { return eval(q < ns.getPlayer().money / 5) }

if (check(ns.hacknet.getPurchaseNodeCost())) {

ns.hacknet.purchaseNode();

}

for (let i = 0; i < ns.hacknet.numNodes(); i++) {

for (let n = 0; n < 3; n++) {

if (check(ns.hacknet[“get” + mode[n] + “UpgradeCost”](i))) {

ns.hacknet[“upgrade” + mode[n]](i);

}

}

}

}

//But above here

ns.tail()

while (true) {//Keeps everything running once per second

await scanExes()

await scanServers()

await checkServers()

await hackAll()

if (manager) { await hnManager() }

await log()

await ns.asleep(1000)

}}

Untested Code

This code has not had intense testing yet, but is estimated to be an improvement, will be moved to stable once I know it’s decent

/** @param {NS} ns**/

export async function main(ns) {

ns.disableLog(“ALL”); //Visual clarity

//Welcome to the Auto Farm part 2: Electric Boogaloo

//This script is a little more complicated to explain easily, it dedicates high RAM servers to attack high profit servers

//This is also set and forget, your EXEs and hacking level are reacquired each second, so new servers are added without needing to reboot it

//Well I hope this brings you ideas, knowledge and or profits πŸ˜€

var files = [“grow.script”, “weak.script”, “hack.script”];//Modify the scripts as needed, if you know what you’re doing

await ns.write(files[0], “grow(args)”, “w”);

await ns.write(files[1], “weaken(args)”, “w”);

await ns.write(files[2], “hack(args)”, “w”);

var serverList; var targetList; var hostList; var exes; var temp; var manager = false;

var cycle = [0, “─”, “\\”, “|”, “/”]; var latest = [[“-“, “-“], [“-“, “-“], [“-“, “-“]];

if (false) { brutessh(); ftpcrack(); relaysmtp(); httpworm(); sqlinject() } //Avoid RAM cost bypass error

var pServers = await ns.prompt(“Use player servers as hosts?”);

async function scanExes() {

exes = [“BruteSSH”, “FTPCrack”, “relaySMTP”, “SQLInject”, “HTTPWorm”];

for (let i = 0; i <= exes.length – 1; i++) { if (!ns.fileExists(exes[i] + “.exe”)) { exes.splice(i, 1); i– } }//Removes EXEs you don’t have

}

function arraySort(array) { return array.sort(function (a, b) { return b[0] – a[0] }) }//Sorts nested arrays

function logBalance(server) {//For balance in display

return [ns.nFormat(ns.getServerMoneyAvailable(server), ‘0a’)] + ” / ” + [ns.nFormat(ns.getServerMaxMoney(server), ‘0a’)]

+ ” : ” + ns.nFormat(ns.getServerMoneyAvailable(server) / ns.getServerMaxMoney(server), ‘0%’)

}

async function log() {//The display

if (cycle[0] >= 4) { cycle[0] = 0 }; cycle[0]++;//Speen

ns.clearLog();

ns.print(“╔═══╦═╣ HOST ╠════════════════╣ TARGET ╠═╗”);

ns.print(“β•‘ G β•‘ ” + latest[0][0] + latest[0][1].padStart(34 – latest[0][0].length) + ” β•‘”)

ns.print(“β•‘ W β•‘ ” + latest[1][0] + latest[1][1].padStart(34 – latest[1][0].length) + ” β•‘”)

ns.print(“β•‘ H β•‘ ” + latest[2][0] + latest[2][1].padStart(34 – latest[2][0].length) + ” β•‘”)

ns.print(“β•‘ ” + cycle[cycle[0]] + ” ╠════════════════════════════════════╣”)

if (targetList.length < 6) { ns.print(“β•šβ•β•β•β•                                    β•‘”) } else {

ns.print(“╠═══╝ Priority Servers       Balance     β•‘”)

for (let i = 0; i < 6; i++) {

temp = targetList[i][1];

ns.print(“β•‘ > ” + temp + logBalance(temp).padStart(36 – temp.length) + ” β•‘”)

}

ns.print(“╠════════════════════════════════════════╝”)

ns.print(“β•‘ EXE ” + exes.length + “/5 β•‘ HOSTS ” + hostList.length + ” β•‘ TARGETS ” + targetList.length)

ns.print(“╠════════════════════════════════════════╗”)

if (manager) {

ns.print(“╠══════╣ Managing ” + ns.hacknet.numNodes() + ” HNet Nodes β• ”.padEnd(21, “═”) + “β•£”)

}

}

}

async function scanServers() {//Finds all servers

serverList = ns.scan(“home”); let serverCount = [serverList.length, 0]; let depth = 0; let checked = 0; let scanIndex = 0;

while (scanIndex <= serverCount[depth] – 1) {

let results = ns.scan(serverList[checked]); checked++;

for (let i = 0; i <= results.length – 1; i++) {

if (results[i] != “home” && !serverList.includes(results[i])) {

serverList.push(results[i]); serverCount[depth + 1]++

}

}

if (scanIndex == serverCount[depth] – 1) { scanIndex = 0; depth++; serverCount.push(0) } else { scanIndex++ };

}

}

async function checkServers() {//Sorts servers into lists based on RAM and money: hostList and targetList

targetList = []; hostList = [[ns.getServerMaxRam(“home”), “home”]];

for (let i = 0; i <= serverList.length – 1; i++) {

let cTarget = serverList[i];

if (ns.getServerMoneyAvailable(cTarget) > 0 || ns.getServerMaxRam(cTarget) > 0) {//Filters out servers like darkweb

temp = ns.getPurchasedServers().includes(cTarget)

if ((ns.getServerNumPortsRequired(cTarget) <= exes.length && !temp) || (temp && pServers)) {

if (!temp) {

for (let i = 0; i <= exes.length – 1; i++) { ns[exes[i].toLowerCase()](cTarget) }//Runs all EXEs you have

ns.nuke(cTarget);//Ghandi.jpeg

}

temp = [Math.floor(ns.getServerMaxMoney(cTarget) / ns.getServerMinSecurityLevel(cTarget)), cTarget];

if (ns.getServerMoneyAvailable(cTarget) != 0 && ns.getServerRequiredHackingLevel(cTarget) <= ns.getHackingLevel() && ns.getServerMinSecurityLevel(cTarget) < 100) {

targetList.push(temp); targetList = arraySort(targetList);

}

temp = [ns.getServerMaxRam(cTarget), cTarget];

if (ns.getServerMaxRam(cTarget) > 2) {

hostList.push(temp); hostList = arraySort(hostList)

}

await ns.scp(files, “home”, cTarget);

}

}

}

}

async function hackAll() {//Dedicates high RAM servers to high value ones

let tarIndex = 0; let loop = false; let hType; let threads;

for (let i = 0; i <= hostList.length – 1; i++) {

if (tarIndex > targetList.length – 1) { tarIndex = 0; loop = true };

let hHost = hostList[i][1]; let hTarget = targetList[tarIndex][1]; let freeRam;

temp = ns.getServerMaxRam(hHost) – ns.getServerUsedRam(hHost)

if (hHost == “home”) { freeRam = Math.max(temp – 50, 0) } else { freeRam = temp }

if (ns.getServerMoneyAvailable(hTarget) < ns.getServerMaxMoney(hTarget) * .80 || loop) { hType = 0 }

else if (ns.getServerSecurityLevel(hTarget) > ns.getServerMinSecurityLevel(hTarget) + 5) { hType = 1 }

else { latest[2][0] = hHost; latest[2][1] = hTarget; hType = 2 }

threads = Math.floor(freeRam / ns.getScriptRam(files[hType]));

while (hType == 2 && parseFloat(ns.hackAnalyze(hTarget)) * threads > .30) { threads– }

if (threads > 0) { ns.exec(files[hType], hHost, threads, hTarget); latest[hType][0] = hHost; latest[hType][1] = hTarget }

tarIndex++

}

}

//Put modules below here

manager = await ns.prompt(“Activate Hacknet Manager?”);

async function hnManager() {

let mode = [“Level”, “Ram”, “Core”]

function check(q) { return eval(q < ns.getPlayer().money / 6) }

if (check(ns.hacknet.getPurchaseNodeCost())) { ns.hacknet.purchaseNode() }

for (let i = 0; i < ns.hacknet.numNodes(); i++) {

for (let n = 0; n < 3; n++) {

if (check(ns.hacknet[“get” + mode[n] + “UpgradeCost”](i))) {

ns.hacknet[“upgrade” + mode[n]](i);

}

}

}

}

//But above here

ns.tail()

while (true) {//Keeps everything running once per second

await scanExes()

await scanServers()

await checkServers()

await hackAll()

if (manager) { await hnManager() }

await log()

await ns.asleep(1000)

}}

Latest articles

β€˜The Retaliators’ Is Blood-Soaked Tale of Morality and Vengeance Backed by a Hard Rocking Soundtrack

β€œWhen do the sins of a good man make him bad?”. This quote...

Round Up: The Reviews Are In For NieR:Automata The End of YoRHa Edition

We're again as soon as once more with one other overview round-up and this...

β€˜Surrogate’ Is a Chilling Ghost Story Filled With Strong Characters and Palpable Dread

β€œThere is no such thing as monsters.” These are the phrases dad and...

Aespa’s NingNing Gains Attention For Her Unreal Body Proportions β€” And Her Insane Duality

β€œParis Fashion Week NingNing” is already a legend. ...
ADVERTISEMENT

More like this

β€˜The Retaliators’ Is Blood-Soaked Tale of Morality and Vengeance Backed by a Hard Rocking Soundtrack

β€œWhen do the sins of a good man make him bad?”. This quote...

Round Up: The Reviews Are In For NieR:Automata The End of YoRHa Edition

We're again as soon as once more with one other overview round-up and this...

β€˜Surrogate’ Is a Chilling Ghost Story Filled With Strong Characters and Palpable Dread

β€œThere is no such thing as monsters.” These are the phrases dad and...