Advertisement
Arinek

GetOptimalThreads

Sep 20th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. /** @param {NS} ns */
  2. export function getOptimalThreads(ns, target) {
  3. //Script paths
  4. const growPath = "Scelus_Saeclum/Peritus_Infiltra/Cresco.js";
  5. const weakenPath = "Scelus_Saeclum/Peritus_Infiltra/Imminuo.js";
  6. const hackPath = "Scelus_Saeclum/Peritus_Infiltra/Fur.js";
  7.  
  8. //Server Information
  9. const targetMaxMoney = ns.getServerMaxMoney(target);
  10. const targetAvailableMoney = ns.getServerMoneyAvailable(target) || 1;
  11. const hostCpuCores = ns.getServer(ns.getHostname())["cpuCores"];
  12. const hostRemainingRam = getServerRemainingRam(ns, ns.getHostname());
  13.  
  14. //ns.tprintf(targetAvailableMoney);
  15. //ns.tprintf(targetMaxMoney);
  16.  
  17. //Calculating Growth Threads
  18. const necessaryGrowthFactor = Math.max(1, targetMaxMoney / targetAvailableMoney);
  19. const necessaryGrowthThreads = ns.growthAnalyze(target, necessaryGrowthFactor, hostCpuCores);
  20. const possibleGrowthThreads = hostRemainingRam / ns.getScriptRam(growPath);
  21.  
  22. //Calculating Weaken Threads
  23. const necessarySecurityToWeaken = ns.getServerSecurityLevel(target); - ns.getServerMinSecurityLevel(target);
  24. const necessaryWeakenThreads = necessarySecurityToWeaken / ns.weakenAnalyze(1, 1);
  25. const possibleWeakenThreads = hostRemainingRam / ns.getScriptRam(weakenPath);
  26.  
  27. //Calculating Hack Threads
  28. const moneyToBeStolen = Math.min(targetMaxMoney, targetMaxMoney * possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores) / (1 + possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores)));
  29. const necessaryHackingThreads = moneyToBeStolen / ns.hackAnalyze(target) / targetMaxMoney;
  30. const possibleHackingThreads = hostRemainingRam / ns.getScriptRam(hackPath);
  31.  
  32. //Conclusion
  33. const growthThreads = Math.floor(Math.min(necessaryGrowthThreads + 1, possibleGrowthThreads)) || 1;
  34. const weakenThreads = Math.floor(Math.min(necessaryWeakenThreads + 1, possibleWeakenThreads)) || 1;
  35. const hackingThreads = Math.floor(Math.min(necessaryHackingThreads + 1, possibleHackingThreads)) || 1;
  36.  
  37. return [growthThreads, weakenThreads, hackingThreads];
  38.  
  39. /*
  40. Explanation for moneyToBeStolen:
  41. X is the amount of money stolen
  42.  
  43. (targetMaxMoney - (targetMaxMoney-X))/(targetMaxMoney-X) * 100 = (new-old)/old = percentual change
  44. (targetMaxMoney - (targetMaxMoney-X))/(targetMaxMoney-X) * 100 * ns.growthAnalyze(target, 1.01, hostCpuCores) = possibleGrowthThreads
  45. (targetMaxMoney - (targetMaxMoney-X))/(targetMaxMoney-X) = possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores)
  46. X / (targetMaxMoney-X) = possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores)
  47. X = (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores)) * (targetMaxMoney-X)
  48. X = targetMaxMoney * (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores)) - X * (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores))
  49. X + X * (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores)) = targetMaxMoney * (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores))
  50. X * (1 + (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores))) = targetMaxMoney * (possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores))
  51. X = targetMaxMoney * possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores) / (1 + possibleGrowthThreads / 100 / ns.growthAnalyze(target, 1.01, hostCpuCores))
  52.  
  53. */
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement