Advertisement
Guest User

Hacknet Node

a guest
Mar 23rd, 2022
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let ns
  2. let hacknet
  3. let maxNodes
  4.  
  5. const rate = 500
  6. const logging = false
  7.  
  8. const maxRam = 64
  9. const ramBaseCost = 30e3
  10. const upgradeRamMult = 1.28
  11. const maxCores = 16
  12. const upgradeCoreMult = 1.48
  13. const coreBaseCost = 500e3
  14. const maxLevel = 200
  15. const upgradeLevelMult = 1.04
  16. const levelBaseCost = 1
  17. const baseCost = 1000
  18.  
  19.  
  20. /** @param {NS} ns **/
  21. export async function main(nsenv) {
  22.     ns = nsenv
  23.     hacknet = ns.hacknet
  24.  
  25.     maxNodes = ns.args[0] ? ns.args[0] : -1
  26.  
  27.     if (logging) console.log(`HACKNET - STARTED SCRIPT`)
  28.  
  29.     await ns.sleep(2000)
  30.  
  31.     let newNodeAmount = 0
  32.  
  33.     await allNodesSameLevel()
  34.  
  35.     while (true) {
  36.  
  37.         //buy inital node
  38.         if (hacknet.numNodes() == 0) {
  39.             newNodeAmount = 1
  40.         }
  41.  
  42.         if (newNodeAmount > 0) {
  43.  
  44.             if (maxNodes != -1 && maxNodes <= hacknet.numNodes() + newNodeAmount) {
  45.                 if (logging) console.log(`HACKNET - Maximum Nodes of ${maxNodes} reached! Terminating process...`)
  46.                 ns.exit()
  47.             }
  48.  
  49.             let newNodeIndex = hacknet.numNodes() - 1 + newNodeAmount
  50.             if (logging) console.log(`HACKNET - Purchasing ${newNodeAmount} new Node!}`)
  51.  
  52.             let num = -1
  53.             while (num != newNodeIndex) {
  54.                 num = hacknet.purchaseNode()
  55.                 await ns.sleep(rate)
  56.             }
  57.  
  58.             await allNodesSameLevel()
  59.             newNodeAmount = 0
  60.         }
  61.  
  62.         //get current node 0 upgrades/stats
  63.         let index = 0
  64.         let nodeStats = hacknet.getNodeStats(index)
  65.         let level = nodeStats.level
  66.         let ram = nodeStats.ram
  67.         let ramLevel = Math.round(Math.log2(ram))
  68.         let cores = nodeStats.cores
  69.         let currentGain = getMoneyGainRate(level, ram, cores)
  70.  
  71.         //calculate the increased money gain per cost for ram
  72.         let ramCost = hacknet.getRamUpgradeCost(index, 1)
  73.         let ramMoneyGain = getMoneyGainRate(level, Math.pow(2, ramLevel + 1), cores) - currentGain
  74.         let ramGainPerCost = ramMoneyGain / ramCost
  75.  
  76.         //calculate the increased money gain per cost for cores
  77.         let coreCost = hacknet.getCoreUpgradeCost(index, 1)
  78.         let coreMoneyGain = getMoneyGainRate(level, ram, cores + 1) - currentGain
  79.         let coreGainPerCost = coreMoneyGain / coreCost
  80.  
  81.         //calculates the increased money gain per cost for level
  82.         let newLevel = getPurchasableLevel(index, ramGainPerCost > coreGainPerCost ? ramCost : coreCost)
  83.         let newLevelCost = hacknet.getLevelUpgradeCost(index, newLevel)
  84.         let levelMoneyGain = getMoneyGainRate(level + newLevel, ram, cores) - currentGain
  85.         let levelMoneyGainPerCost = newLevelCost != 0 ? levelMoneyGain / newLevelCost : Infinity
  86.  
  87.         //calculates the increased money gain per cost for a new node with current upgrades
  88.         let newNodeCost = hacknet.getPurchaseNodeCost() + getMoneySpend(0)
  89.         let newNodeGainPerCost = hacknet.getNodeStats(0).production / newNodeCost
  90.  
  91.         let gains = [
  92.             levelMoneyGainPerCost == Infinity ? -1 : levelMoneyGainPerCost,
  93.             ramGainPerCost == Infinity ? -1 : ramGainPerCost,
  94.             coreGainPerCost == Infinity ? -1 : coreGainPerCost,
  95.             newNodeGainPerCost == Infinity ? -1 : newNodeGainPerCost
  96.         ]
  97.  
  98.         //check which option is the most valuable
  99.         switch (Math.max(...gains)) {
  100.             case levelMoneyGainPerCost:
  101.                 //level += newLevel
  102.                 level += 1
  103.                 break;
  104.             case ramGainPerCost:
  105.                 ramLevel += 1
  106.                 break;
  107.             case coreGainPerCost:
  108.                 cores += 1
  109.                 break;
  110.             case newNodeGainPerCost:
  111.                 newNodeAmount++
  112.                 break;
  113.             default:
  114.                 if (logging) console.log(`HACKNET - default case => Should not happen`)
  115.  
  116.         }
  117.  
  118.         if (newNodeAmount == 0)
  119.             for (let i = 0; i < hacknet.numNodes(); i++) {
  120.                 nodeStats = hacknet.getNodeStats(i)
  121.                 let additionalLevel = level - nodeStats.level
  122.                 let additionalRamLevel = ramLevel - Math.round(Math.log2(nodeStats.ram))
  123.                 let additionalCoreLevel = cores - nodeStats.cores
  124.  
  125.                 if (additionalLevel > 0) {
  126.                     for (let l = 0; l < additionalLevel; l++) {
  127.                         while (!hacknet.upgradeLevel(i, 1)) await ns.sleep(rate)
  128.                     }
  129.                 }
  130.  
  131.                 if (additionalRamLevel > 0) {
  132.                     for (let l = 0; l < additionalRamLevel; l++) {
  133.                         while (!hacknet.upgradeRam(i, 1)) await ns.sleep(rate)
  134.                     }
  135.                 }
  136.  
  137.                 if (additionalCoreLevel > 0) {
  138.                     for (let l = 0; l < additionalCoreLevel; l++) {
  139.                         while (!hacknet.upgradeCore(i, 1)) await ns.sleep(rate)
  140.                     }
  141.                 }
  142.             }
  143.  
  144.         await ns.sleep(50)
  145.     }
  146. }
  147.  
  148. //calculates the money spent on a node
  149. function getMoneySpend(index) {
  150.     const nodeStats = hacknet.getNodeStats(index)
  151.  
  152.     let level = nodeStats.level - 1
  153.     let ramLevel = Math.round(Math.log2(nodeStats.ram))
  154.     let coreLevel = nodeStats.cores - 1
  155.     let totalSpend = 0
  156.     totalSpend += calculateLevelUpgradeCost(1, level, ns.getPlayer().hacknet_node_level_cost_mult)
  157.     totalSpend += calculateRamUpgradeCost(1, ramLevel, ns.getPlayer().hacknet_node_ram_cost_mult)
  158.     totalSpend += calculateCoreUpgradeCost(1, coreLevel, ns.getPlayer().hacknet_node_core_cost_mult)
  159.     return totalSpend
  160. }
  161.  
  162. // from offical bitburner github src/Hacknet/formulas/HacknetNodes.ts
  163. function calculateLevelUpgradeCost(startingLevel, extraLevels = 1, costMult = 1) {
  164.     const sanitizedLevels = Math.round(extraLevels);
  165.     if (isNaN(sanitizedLevels) || sanitizedLevels < 1) {
  166.         return 0;
  167.     }
  168.  
  169.     if (startingLevel >= maxLevel) {
  170.         return Infinity;
  171.     }
  172.  
  173.     const mult = upgradeLevelMult;
  174.     let totalMultiplier = 0;
  175.     let currLevel = startingLevel;
  176.     for (let i = 0; i < sanitizedLevels; ++i) {
  177.         totalMultiplier += levelBaseCost * Math.pow(mult, currLevel);
  178.         ++currLevel;
  179.     }
  180.  
  181.     return (baseCost / 2) * totalMultiplier * costMult;
  182. }
  183.  
  184. // from offical bitburner github src/Hacknet/formulas/HacknetNodes.ts
  185. function calculateRamUpgradeCost(startingRam, extraLevels = 1, costMult = 1) {
  186.     const sanitizedLevels = Math.round(extraLevels);
  187.     if (isNaN(sanitizedLevels) || sanitizedLevels < 1) {
  188.         return 0;
  189.     }
  190.  
  191.     if (startingRam >= maxRam) {
  192.         return Infinity;
  193.     }
  194.  
  195.     let totalCost = 0;
  196.     let numUpgrades = Math.round(Math.log2(startingRam));
  197.     let currentRam = startingRam;
  198.  
  199.     for (let i = 0; i < sanitizedLevels; ++i) {
  200.         const baseCost = currentRam * ramBaseCost;
  201.         const mult = Math.pow(upgradeRamMult, numUpgrades);
  202.  
  203.         totalCost += baseCost * mult;
  204.  
  205.         currentRam *= 2;
  206.         ++numUpgrades;
  207.     }
  208.  
  209.     totalCost *= costMult;
  210.  
  211.     return totalCost;
  212. }
  213. // from offical bitburner github src/Hacknet/formulas/HacknetNodes.ts
  214. function calculateCoreUpgradeCost(startingCore, extraLevels = 1, costMult = 1) {
  215.     const sanitizedCores = Math.round(extraLevels);
  216.     if (isNaN(sanitizedCores) || sanitizedCores < 1) {
  217.         return 0;
  218.     }
  219.  
  220.     if (startingCore >= maxCores) {
  221.         return Infinity;
  222.     }
  223.  
  224.     const mult = upgradeCoreMult;
  225.     let totalCost = 0;
  226.     let currentCores = startingCore;
  227.     for (let i = 0; i < sanitizedCores; ++i) {
  228.         totalCost += coreBaseCost * Math.pow(mult, currentCores - 1);
  229.         ++currentCores;
  230.     }
  231.  
  232.     totalCost *= costMult;
  233.  
  234.     return totalCost;
  235. }
  236.  
  237.  
  238. //return number of level which can be purchased with the specified money
  239. function getPurchasableLevel(index, money) {
  240.     let costs = 0
  241.     let levels = 1
  242.  
  243.     while (costs < money && !(hacknet.getNodeStats(index).level + levels > 200)) {
  244.         costs = hacknet.getLevelUpgradeCost(index, levels)
  245.         levels++
  246.     }
  247.     return levels - 1
  248. }
  249.  
  250. //calculates the money per second for a node with specified upgrades
  251. function getMoneyGainRate(level, ram, cores) {
  252.  
  253.     const gainPerLevel = 1.5 * ns.getPlayer().hacknet_node_money_mult
  254.  
  255.     const levelMult = level * gainPerLevel;
  256.     const ramMult = Math.pow(1.035, ram - 1);
  257.     const coresMult = (cores + 5) / 6;
  258.  
  259.     return levelMult * ramMult * coresMult;
  260. }
  261.  
  262.  
  263. //upgrades all nodes according to stats of node 0
  264. async function allNodesSameLevel() {
  265.  
  266.     if (hacknet.numNodes() == 0) return
  267.  
  268.     let nodeStats = hacknet.getNodeStats(0)
  269.     let level = nodeStats.level
  270.     let ramLevel = Math.round(Math.log2(nodeStats.ram))
  271.     let cores = nodeStats.cores
  272.  
  273.     if (logging) console.log(`HACKNET - upgrading all nodes to ${level} Levels, ${Math.pow(2, ramLevel)} GB Ram, ${cores} Cores`)
  274.  
  275.  
  276.     for (let i = 1; i < hacknet.numNodes(); i++) {
  277.         nodeStats = hacknet.getNodeStats(i)
  278.         let additionalLevel = level - nodeStats.level
  279.         let additionalRamLevel = ramLevel - Math.round(Math.log2(nodeStats.ram))
  280.         let additionalCoreLevel = cores - nodeStats.cores
  281.  
  282.         if (additionalLevel > 0) {
  283.             for (let l = 0; l < additionalLevel; l++) {
  284.                 while (!hacknet.upgradeLevel(i, 1)) await ns.sleep(rate)
  285.             }
  286.         }
  287.  
  288.         if (additionalRamLevel > 0) {
  289.             for (let l = 0; l < additionalRamLevel; l++) {
  290.                 while (!hacknet.upgradeRam(i, 1)) await ns.sleep(rate)
  291.             }
  292.         }
  293.  
  294.         if (additionalCoreLevel > 0) {
  295.             for (let l = 0; l < additionalCoreLevel; l++) {
  296.                 while (!hacknet.upgradeCore(i, 1)) await ns.sleep(rate)
  297.             }
  298.         }
  299.     }
  300.     if (logging) console.log(`HACKNET - done`)
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement