Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let ns
- let hacknet
- let maxNodes
- const rate = 500
- const logging = false
- const maxRam = 64
- const ramBaseCost = 30e3
- const upgradeRamMult = 1.28
- const maxCores = 16
- const upgradeCoreMult = 1.48
- const coreBaseCost = 500e3
- const maxLevel = 200
- const upgradeLevelMult = 1.04
- const levelBaseCost = 1
- const baseCost = 1000
- /** @param {NS} ns **/
- export async function main(nsenv) {
- ns = nsenv
- hacknet = ns.hacknet
- maxNodes = ns.args[0] ? ns.args[0] : -1
- if (logging) console.log(`HACKNET - STARTED SCRIPT`)
- await ns.sleep(2000)
- let newNodeAmount = 0
- await allNodesSameLevel()
- while (true) {
- //buy inital node
- if (hacknet.numNodes() == 0) {
- newNodeAmount = 1
- }
- if (newNodeAmount > 0) {
- if (maxNodes != -1 && maxNodes <= hacknet.numNodes() + newNodeAmount) {
- if (logging) console.log(`HACKNET - Maximum Nodes of ${maxNodes} reached! Terminating process...`)
- ns.exit()
- }
- let newNodeIndex = hacknet.numNodes() - 1 + newNodeAmount
- if (logging) console.log(`HACKNET - Purchasing ${newNodeAmount} new Node!}`)
- let num = -1
- while (num != newNodeIndex) {
- num = hacknet.purchaseNode()
- await ns.sleep(rate)
- }
- await allNodesSameLevel()
- newNodeAmount = 0
- }
- //get current node 0 upgrades/stats
- let index = 0
- let nodeStats = hacknet.getNodeStats(index)
- let level = nodeStats.level
- let ram = nodeStats.ram
- let ramLevel = Math.round(Math.log2(ram))
- let cores = nodeStats.cores
- let currentGain = getMoneyGainRate(level, ram, cores)
- //calculate the increased money gain per cost for ram
- let ramCost = hacknet.getRamUpgradeCost(index, 1)
- let ramMoneyGain = getMoneyGainRate(level, Math.pow(2, ramLevel + 1), cores) - currentGain
- let ramGainPerCost = ramMoneyGain / ramCost
- //calculate the increased money gain per cost for cores
- let coreCost = hacknet.getCoreUpgradeCost(index, 1)
- let coreMoneyGain = getMoneyGainRate(level, ram, cores + 1) - currentGain
- let coreGainPerCost = coreMoneyGain / coreCost
- //calculates the increased money gain per cost for level
- let newLevel = getPurchasableLevel(index, ramGainPerCost > coreGainPerCost ? ramCost : coreCost)
- let newLevelCost = hacknet.getLevelUpgradeCost(index, newLevel)
- let levelMoneyGain = getMoneyGainRate(level + newLevel, ram, cores) - currentGain
- let levelMoneyGainPerCost = newLevelCost != 0 ? levelMoneyGain / newLevelCost : Infinity
- //calculates the increased money gain per cost for a new node with current upgrades
- let newNodeCost = hacknet.getPurchaseNodeCost() + getMoneySpend(0)
- let newNodeGainPerCost = hacknet.getNodeStats(0).production / newNodeCost
- let gains = [
- levelMoneyGainPerCost == Infinity ? -1 : levelMoneyGainPerCost,
- ramGainPerCost == Infinity ? -1 : ramGainPerCost,
- coreGainPerCost == Infinity ? -1 : coreGainPerCost,
- newNodeGainPerCost == Infinity ? -1 : newNodeGainPerCost
- ]
- //check which option is the most valuable
- switch (Math.max(...gains)) {
- case levelMoneyGainPerCost:
- //level += newLevel
- level += 1
- break;
- case ramGainPerCost:
- ramLevel += 1
- break;
- case coreGainPerCost:
- cores += 1
- break;
- case newNodeGainPerCost:
- newNodeAmount++
- break;
- default:
- if (logging) console.log(`HACKNET - default case => Should not happen`)
- }
- if (newNodeAmount == 0)
- for (let i = 0; i < hacknet.numNodes(); i++) {
- nodeStats = hacknet.getNodeStats(i)
- let additionalLevel = level - nodeStats.level
- let additionalRamLevel = ramLevel - Math.round(Math.log2(nodeStats.ram))
- let additionalCoreLevel = cores - nodeStats.cores
- if (additionalLevel > 0) {
- for (let l = 0; l < additionalLevel; l++) {
- while (!hacknet.upgradeLevel(i, 1)) await ns.sleep(rate)
- }
- }
- if (additionalRamLevel > 0) {
- for (let l = 0; l < additionalRamLevel; l++) {
- while (!hacknet.upgradeRam(i, 1)) await ns.sleep(rate)
- }
- }
- if (additionalCoreLevel > 0) {
- for (let l = 0; l < additionalCoreLevel; l++) {
- while (!hacknet.upgradeCore(i, 1)) await ns.sleep(rate)
- }
- }
- }
- await ns.sleep(50)
- }
- }
- //calculates the money spent on a node
- function getMoneySpend(index) {
- const nodeStats = hacknet.getNodeStats(index)
- let level = nodeStats.level - 1
- let ramLevel = Math.round(Math.log2(nodeStats.ram))
- let coreLevel = nodeStats.cores - 1
- let totalSpend = 0
- totalSpend += calculateLevelUpgradeCost(1, level, ns.getPlayer().hacknet_node_level_cost_mult)
- totalSpend += calculateRamUpgradeCost(1, ramLevel, ns.getPlayer().hacknet_node_ram_cost_mult)
- totalSpend += calculateCoreUpgradeCost(1, coreLevel, ns.getPlayer().hacknet_node_core_cost_mult)
- return totalSpend
- }
- // from offical bitburner github src/Hacknet/formulas/HacknetNodes.ts
- function calculateLevelUpgradeCost(startingLevel, extraLevels = 1, costMult = 1) {
- const sanitizedLevels = Math.round(extraLevels);
- if (isNaN(sanitizedLevels) || sanitizedLevels < 1) {
- return 0;
- }
- if (startingLevel >= maxLevel) {
- return Infinity;
- }
- const mult = upgradeLevelMult;
- let totalMultiplier = 0;
- let currLevel = startingLevel;
- for (let i = 0; i < sanitizedLevels; ++i) {
- totalMultiplier += levelBaseCost * Math.pow(mult, currLevel);
- ++currLevel;
- }
- return (baseCost / 2) * totalMultiplier * costMult;
- }
- // from offical bitburner github src/Hacknet/formulas/HacknetNodes.ts
- function calculateRamUpgradeCost(startingRam, extraLevels = 1, costMult = 1) {
- const sanitizedLevels = Math.round(extraLevels);
- if (isNaN(sanitizedLevels) || sanitizedLevels < 1) {
- return 0;
- }
- if (startingRam >= maxRam) {
- return Infinity;
- }
- let totalCost = 0;
- let numUpgrades = Math.round(Math.log2(startingRam));
- let currentRam = startingRam;
- for (let i = 0; i < sanitizedLevels; ++i) {
- const baseCost = currentRam * ramBaseCost;
- const mult = Math.pow(upgradeRamMult, numUpgrades);
- totalCost += baseCost * mult;
- currentRam *= 2;
- ++numUpgrades;
- }
- totalCost *= costMult;
- return totalCost;
- }
- // from offical bitburner github src/Hacknet/formulas/HacknetNodes.ts
- function calculateCoreUpgradeCost(startingCore, extraLevels = 1, costMult = 1) {
- const sanitizedCores = Math.round(extraLevels);
- if (isNaN(sanitizedCores) || sanitizedCores < 1) {
- return 0;
- }
- if (startingCore >= maxCores) {
- return Infinity;
- }
- const mult = upgradeCoreMult;
- let totalCost = 0;
- let currentCores = startingCore;
- for (let i = 0; i < sanitizedCores; ++i) {
- totalCost += coreBaseCost * Math.pow(mult, currentCores - 1);
- ++currentCores;
- }
- totalCost *= costMult;
- return totalCost;
- }
- //return number of level which can be purchased with the specified money
- function getPurchasableLevel(index, money) {
- let costs = 0
- let levels = 1
- while (costs < money && !(hacknet.getNodeStats(index).level + levels > 200)) {
- costs = hacknet.getLevelUpgradeCost(index, levels)
- levels++
- }
- return levels - 1
- }
- //calculates the money per second for a node with specified upgrades
- function getMoneyGainRate(level, ram, cores) {
- const gainPerLevel = 1.5 * ns.getPlayer().hacknet_node_money_mult
- const levelMult = level * gainPerLevel;
- const ramMult = Math.pow(1.035, ram - 1);
- const coresMult = (cores + 5) / 6;
- return levelMult * ramMult * coresMult;
- }
- //upgrades all nodes according to stats of node 0
- async function allNodesSameLevel() {
- if (hacknet.numNodes() == 0) return
- let nodeStats = hacknet.getNodeStats(0)
- let level = nodeStats.level
- let ramLevel = Math.round(Math.log2(nodeStats.ram))
- let cores = nodeStats.cores
- if (logging) console.log(`HACKNET - upgrading all nodes to ${level} Levels, ${Math.pow(2, ramLevel)} GB Ram, ${cores} Cores`)
- for (let i = 1; i < hacknet.numNodes(); i++) {
- nodeStats = hacknet.getNodeStats(i)
- let additionalLevel = level - nodeStats.level
- let additionalRamLevel = ramLevel - Math.round(Math.log2(nodeStats.ram))
- let additionalCoreLevel = cores - nodeStats.cores
- if (additionalLevel > 0) {
- for (let l = 0; l < additionalLevel; l++) {
- while (!hacknet.upgradeLevel(i, 1)) await ns.sleep(rate)
- }
- }
- if (additionalRamLevel > 0) {
- for (let l = 0; l < additionalRamLevel; l++) {
- while (!hacknet.upgradeRam(i, 1)) await ns.sleep(rate)
- }
- }
- if (additionalCoreLevel > 0) {
- for (let l = 0; l < additionalCoreLevel; l++) {
- while (!hacknet.upgradeCore(i, 1)) await ns.sleep(rate)
- }
- }
- }
- if (logging) console.log(`HACKNET - done`)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement