Advertisement
Guest User

Kupos BitBurner HWGW Manager

a guest
Jun 25th, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns **/
  2.  
  3. // Used for importing targets from other scripts
  4. import { FileHandler } from "/data/file-handler.js";
  5.  
  6. export async function main(ns) {
  7.     //Logs are nice to know whats going on
  8.     ns.disableLog('sleep');
  9.     ns.disableLog('getServerMaxRam');
  10.     ns.disableLog('getServerUsedRam');
  11.     ns.disableLog('getServerSecurityLevel');
  12.     ns.disableLog('getServerMinSecurityLevel');
  13.     ns.disableLog('getServerMaxMoney');
  14.     ns.disableLog('getServerMoneyAvailable');
  15.     ns.disableLog('getHackingLevel');
  16.     // If you do not want an open tail window with information what this script is doing on every start, you can remove/comment the tail line below.
  17.     ns.tail();
  18.  
  19.     // Used for importing targets from other scripts
  20.     const fileHandlerServers = new FileHandler(ns, "/database/servers.txt");
  21.     const fileHandlerTarget = new FileHandler(ns, "/database/target.txt");
  22.  
  23.     const ramUsePerThread = ns.getScriptRam("/shared/weaken.js");
  24.     const weakenPerThread = ns.weakenAnalyze(1, 1);
  25.     const reservedHomeRam = 50; //GB - change if you want to reserve more of you home ram
  26.     const sleepPerIteration = 10; // ms - reduce if you need to go even faster, or raise if you get lag spikes
  27.     const iterationPause = 10 // seconds - Pause after the script did touch every server and every target - how often should it run again
  28.  
  29.     // This will be used to make sure that every batch goes through and is not blocked my "script already running with same arguments"
  30.     let randomArgument = 1;
  31.  
  32.     let earlyGameCheck = false;
  33.  
  34.     while (true) {
  35.  
  36.         // read (new) targets - if you do not use fileHandlers like i do, just throw in an array of targets or a function or anything really.
  37.         // If desperate or no clue, use the commented lines instead and change target to your highest/best target you currently have
  38.         const targets = await fileHandlerServers.read();
  39.         //const targets = ["n00dles","foodnstuff","sigma-cosmetics","joesguns","hong-fang-tea","harakiri-sushi","iron-gym","darkweb","home","zer0","CSEC","nectar-net","max-hardware","neo-net","silver-helix","phantasy","omega-net","computek","netlink","johnson-ortho","the-hub","crush-fitness","avmnite-02h","catalyst","I.I.I.I","summit-uni","syscore","rothman-uni","zb-institute","lexo-corp","rho-construction","millenium-fitness","alpha-ent","aevum-police","aerocorp","snap-fitness","galactic-cyber","global-pharm","omnia","deltaone","unitalife","solaris","defcomm","icarus","univ-energy","zeus-med","taiyang-digital","zb-def","infocomm","nova-med","titan-labs","applied-energetics","microdyne","run4theh111z","stormtech","helios","vitalife","fulcrumtech","4sigma","kuai-gong",".","omnitek","b-and-a","powerhouse-fitness","nwo","clarkinc","blade","ecorp","megacorp","fulcrumassets","The-Cave"];
  40.  
  41.         // The best server we know for our current state. Or just a random one you can tackle.
  42.         const primeTarget = await fileHandlerTarget.read();
  43.         //const primeTarget = "foodnstuff";
  44.  
  45.         // get (new) servers + home
  46.         let servers = ns.getPurchasedServers();
  47.         servers = servers.concat("home");
  48.  
  49.         // If we are so early in the game that we can't even effectively farm ALL servers...
  50.         const goodHackingLevel = 750;
  51.         if (ns.getHackingLevel() < goodHackingLevel && ns.serverExists(primeTarget) && ns.hasRootAccess(primeTarget) && ns.getServerMaxMoney(primeTarget) > 1)
  52.             earlyGameCheck = true;
  53.  
  54.         for (let target of targets) {
  55.             // ...we stick with THE BEST one. Adjust the criteria to your liking.
  56.             if (earlyGameCheck)
  57.                 target = primeTarget;
  58.  
  59.             //Is our given server even useable?
  60.             if (!ns.serverExists(target) || !ns.hasRootAccess(target) || ns.getServerMaxMoney(target) < 1)
  61.                 continue;
  62.  
  63.             //Target variables
  64.             const maxMoney = ns.getServerMaxMoney(target);
  65.             const minSecurityLevel = ns.getServerMinSecurityLevel(target);
  66.             let currentSecLevel = ns.getServerSecurityLevel(target);
  67.             let availableMoney = Math.max(1, ns.getServerMoneyAvailable(target));
  68.             let growthCalculated = false;
  69.             let requiredGrowThreads = 0;
  70.             let batchDelay = 0;
  71.  
  72.             for (let myServer of servers) {
  73.                 if (!ns.serverExists(myServer) || !ns.hasRootAccess(myServer))
  74.                     continue;
  75.                 //Calculate possible threads - If "home", we want some spare some spare RAM
  76.                 let threadsToUse = Math.floor((ns.getServerMaxRam(myServer) - ns.getServerUsedRam(myServer) - (myServer == "home" ? reservedHomeRam : 0)) / ramUsePerThread);
  77.  
  78.                 //Come on get out of here we have no use for you if you have no capacity
  79.                 if(threadsToUse < 1)
  80.                     continue;
  81.  
  82.                 //Break the server down to its minimum sec level.
  83.                 if (currentSecLevel > minSecurityLevel) {
  84.                     const reducedSecLevel = weakenPerThread * threadsToUse;
  85.                     //Too strong? Only use needed threads then
  86.                     if (currentSecLevel - reducedSecLevel < minSecurityLevel) {
  87.                         threadsToUse = Math.ceil((currentSecLevel - minSecurityLevel) / weakenPerThread);
  88.                         currentSecLevel = minSecurityLevel;
  89.                     }
  90.                     else
  91.                         currentSecLevel -= reducedSecLevel;
  92.                     ns.exec("/shared/weaken.js", myServer, threadsToUse, target, 0, randomArgument++);
  93.                 }
  94.                 //Grow the server
  95.                 else if (availableMoney < maxMoney && (requiredGrowThreads != 0 || !growthCalculated)) {
  96.                     if (!growthCalculated) {
  97.                         requiredGrowThreads = Math.ceil(ns.growthAnalyze(target, maxMoney / availableMoney));
  98.                         growthCalculated = true;
  99.                     }
  100.                     //Do not use more than needed
  101.                     threadsToUse = Math.min(requiredGrowThreads, threadsToUse)
  102.  
  103.                     //requiredGrowThreads will save how many more threads are needed, if any, for this iteration    
  104.                     requiredGrowThreads -= threadsToUse;
  105.  
  106.                     //Let's also not forget that this will raise the security level.
  107.                     currentSecLevel += ns.growthAnalyzeSecurity(threadsToUse);
  108.                     ns.exec("/shared/grow.js", myServer, threadsToUse, target, 0, randomArgument++);
  109.                 }
  110.                 // Fully prepped? Let's do batching then
  111.                 else {
  112.                     //How many threads per batch with one Hack thread? Adjust if needed
  113.                     const minBatchThreads = 6;
  114.  
  115.                     //Below this value we risk that not all HWGW execute, so lets not even try that and wait for more threads or other tasks in next rotation.
  116.                     if (threadsToUse < minBatchThreads)
  117.                         continue;
  118.  
  119.                     const moneyPerHack = ns.hackAnalyze(target);
  120.  
  121.                     // If we are so weak we cant even get a single dime out of a server, lets just not even try it. And also get notified.
  122.                     if (moneyPerHack == 0) {
  123.                         ns.tprint("INFO - We can not effectively hack this server: " + target);
  124.                         continue;
  125.                     }
  126.  
  127.                     // Let's dream big and completely take 90% of the availableMoney! how many threads we need for that?
  128.                     let hackThreads = Math.ceil(0.9 / moneyPerHack);
  129.  
  130.                     // Reality check. are we eben able to do so? Do we have lots of threads to waste ehm grow i mean? If not, just take a part of our threads for hack and go.
  131.                     if (hackThreads * minBatchThreads * 2 > threadsToUse) { // If we do hack 90% of availableMoney we will need more grow threads to compensate, so we double the needed threadsToUse.
  132.                         hackThreads = Math.floor(threadsToUse / minBatchThreads);
  133.                     }
  134.  
  135.                     //Let's calculate how we divide our threads for HWGW.
  136.                     const growThreads = Math.ceil(ns.growthAnalyze(target, 1 / (1 - Math.min(0.99, moneyPerHack * hackThreads))));
  137.                     const weakenThreads = Math.ceil(ns.hackAnalyzeSecurity(hackThreads) / weakenPerThread);
  138.                     const weakenThreads2 = Math.ceil(ns.growthAnalyzeSecurity(growThreads) / weakenPerThread);
  139.  
  140.                     if (hackThreads + growThreads + weakenThreads + weakenThreads2 > threadsToUse)
  141.                         ns.tprint("ERROR: some threads did not start, because we need more minimum threads per batch. raise 'minBatchThreads'");
  142.  
  143.                     //Now get these threads executed.
  144.                     const threadDelay = 150; //ms
  145.                     ns.exec("/shared/weaken.js", myServer, weakenThreads, target, batchDelay, randomArgument);
  146.                     ns.exec("/shared/weaken.js", myServer, weakenThreads2, target, threadDelay * 2 + batchDelay, randomArgument);
  147.                     ns.exec("/shared/grow.js", myServer, growThreads, target, ns.getWeakenTime(target) - ns.getGrowTime(target) + threadDelay + batchDelay, randomArgument);
  148.                     ns.exec("/shared/hack.js", myServer, hackThreads, target, ns.getWeakenTime(target) - ns.getHackTime(target) - threadDelay + batchDelay, randomArgument++);
  149.                     //If we would fire the next HWGW without this batchDelay, they might intersect. we could also use ns.sleep but ain't nobody got time for that
  150.                     batchDelay += 4*threadDelay;
  151.                 }
  152.                 await ns.sleep(sleepPerIteration);
  153.             }
  154.             await ns.sleep(sleepPerIteration);
  155.         }
  156.         await ns.sleep(iterationPause*1000);
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement