Automation for Hacknet Nodes

Automation for Hacknet Nodes

How It Works


Automation for Hacknet Nodes image 1

Algorithm is looking for the most profitable ratio.

Ratio = Production growth / Upgrade Cost

Production growth = Production after upgrade - Current production

I recommend you to pass bigger loop iteration time (e.g. 100 000) or leave the default to let hacknet nodes collect more money so that they can compare more upgrades instead of constantly upgrading the cheapest one.

Usage


Automation for Hacknet Nodes image 7

Step-by-step

Open the terminal

Type "nano hacknet-bot.js" to create a file

Clear the file and paste the code below there

Save the file

Go back to the terminal

Type "run hacknet-bot.js" to run a script

You can define a loop frequency time by passing an additional script argument

e.g. "run hacknet-bot.js 10000"

Default loop freq. is 30 000 ms = 30 s.

You can also copy the code from https://www.paste.org/121037.

Code

/** @param {NS} ns **/ export async function main(ns) { // helpers const getMoney = () => ns.getPlayer().money; const getProduction = (level, ram, cores) => (level * 1.5) * Math.pow(1.035, ram - 1) * ((cores + 5) / 6); const getRatio = (money, cost, growth) => money >= cost ? growth / cost : 0; // loop interval passed as a script parameter, default interval is 30s const interval = ns.args[0] || 30000; while (true) { // loop through all hacknet nodes for (var index = 0; index < await ns.hacknet.numNodes(); index++) { // your current money const money = getMoney(); // your production multiplier from installed augmentations const prodMultiplier = await ns.getHacknetMultipliers().production; // get current node stats const nodeStats = await ns.hacknet.getNodeStats(index); const curRam = nodeStats.ram; const curLevel = nodeStats.level; const curCores = nodeStats.cores; const curProd = nodeStats.production; // get costs of upgrades const ramUpgradeCost = await ns.hacknet.getRamUpgradeCost(index); const levelUpgradeCost = await ns.hacknet.getLevelUpgradeCost(index); const coreUpgradeCost = await ns.hacknet.getCoreUpgradeCost(index); // check upgrades profitability and if you have enough money // UPGRADE RATIO = production growth after upgrade / upgrade cost const prodGrowthAfterLevelUpgrade = (getProduction(curLevel + 1, curRam, curCores) * prodMultiplier) - curProd; const levelUpgradeRatio = getRatio(money, levelUpgradeCost, prodGrowthAfterLevelUpgrade); const prodGrowthAfterRamUpgrade = (getProduction(curLevel, curRam * 2, curCores) * prodMultiplier) - curProd; const ramUpgradeRatio = getRatio(money, ramUpgradeCost, prodGrowthAfterRamUpgrade); const prodGrowthAfterCoresUpgrade = (getProduction(curLevel, curRam, curCores + 1) * prodMultiplier) - curProd; const coresUpgradeRatio = getRatio(money, coreUpgradeCost, prodGrowthAfterCoresUpgrade); // find the most profitability upgrade const ratios = [levelUpgradeRatio, ramUpgradeRatio, coresUpgradeRatio]; const mostProfitUpgrade = Math.max(...ratios); if (mostProfitUpgrade === 0) { continue; } const mostProfitUpgradeIndex = ratios.findIndex(ratio => ratio === mostProfitUpgrade); // execute the most profitability upgrade switch (mostProfitUpgradeIndex) { case 0: await ns.hacknet.upgradeLevel(index, 1); break; case 1: await ns.hacknet.upgradeRam(index, 1); break; case 2: await ns.hacknet.upgradeCore(index, 1); break; default: continue; } } // check if you can buy new node machine if (getMoney() > await ns.hacknet.getPurchaseNodeCost()) { await ns.hacknet.purchaseNode(); } // sleep to prevent crash because of infinite loop await ns.sleep(interval); } }

Source: https://steamcommunity.com/sharedfiles/filedetails/?id=2711358884					

More Bitburner guilds