This commit is contained in:
2024-01-09 17:47:03 +01:00
parent 435add26de
commit b7197c909c
10 changed files with 442 additions and 119 deletions
+67 -61
View File
@@ -159,6 +159,7 @@ export async function main(ns) {
let buyCounter = 120;
let prevMaxMoneyHost = '';
ns.write('autoHack.txt', '', 'w');
while (true) {
// Buy all available exploits
@@ -174,10 +175,13 @@ export async function main(ns) {
ns.purchaseServer('home', serverRAM);
}
// Upgrade RAM
for (const server of ns.getPurchasedServers()) {
const serverStats = analyzeServer(server);
if (ns.getPurchasedServerUpgradeCost(serverStats.name, serverStats.RAM * 2) < ns.getServerMoneyAvailable('home') * 0.2) {
ns.upgradePurchasedServer(serverStats.name, serverStats.RAM * 2);
if (serverStats.RAM < 128) {
if (ns.getPurchasedServerUpgradeCost(serverStats.name, serverStats.RAM * 2) < ns.getServerMoneyAvailable('home') * 0.2) {
ns.upgradePurchasedServer(serverStats.name, serverStats.RAM * 2);
}
}
}
@@ -212,75 +216,77 @@ export async function main(ns) {
prevMaxMoneyHost = maxMoneyHost.name;
}
// TODO: convert to global RAM usage
// TODO: convert to global RAM usage, steal 50%, regrow, weaken
// Hack servers
for (const host of network) {
for (let i = 1; i < network.length; i++) {
const host = network[i];
if (!ns.scriptRunning('grow.js', host.name) && !ns.scriptRunning('weaken.js', host.name) && !ns.scriptRunning('hack.js', host.name) && host.rootAccess && host.RAM > 0) {
// if (false) {//(host.name != 'home' && host.currentMoney > 0) {
// const freeRAM = ns.getServerMaxRam(host.name) - ns.getServerUsedRam(host.name);
// const hackRAM = ns.getScriptRam('hack.js', host.name);
// const growRAM = ns.getScriptRam('grow.js', host.name);
// const weakenRAM = ns.getScriptRam('weaken.js', host.name);
// const hackTime = ns.getHackTime(host.name);
// const growTime = ns.getGrowTime(host.name);
// const weakenTime = ns.getWeakenTime(host.name);
// const hackWait = hackTime <= growTime ? growTime - hackTime + 50 : 0;
// const hackAnalyze = ns.hackAnalyze(host.name);
// const growThreadsNeededPerHack = Math.ceil(ns.growthAnalyze(host.name, 1 + hackAnalyze));
if (host.name != 'home' && host.currentMoney > 0) {
const freeRAM = ns.getServerMaxRam(host.name) - ns.getServerUsedRam(host.name);
const hackRAM = ns.getScriptRam('hack.js', host.name);
const growRAM = ns.getScriptRam('grow.js', host.name);
const weakenRAM = ns.getScriptRam('weaken.js', host.name);
const hackTime = ns.getHackTime(host.name);
const growTime = ns.getGrowTime(host.name);
const weakenTime = ns.getWeakenTime(host.name);
const hackWait = hackTime <= growTime ? growTime - hackTime + 50 : 0;
const hackAnalyze = ns.hackAnalyze(host.name);
const growThreadsNeededPerHack = Math.ceil(ns.growthAnalyze(host.name, 1 + hackAnalyze));
// let hackThreads = Math.floor(freeRAM / (hackRAM + growRAM * growThreadsNeededPerHack));
// hackThreads = hackThreads == 0 ? 1 : hackThreads;
// const growThreads = Math.floor((freeRAM - hackThreads * hackRAM) / growRAM);
// const growLoops = Math.ceil(growThreadsNeededPerHack / growThreads);
// const weakenThreads = Math.floor(freeRAM / weakenRAM);
// const log = `host:${host.name} target:${maxMoneyHost.name} | security:${(maxMoneyHost.currentSecurity / maxMoneyHost.minSecurity).toFixed(2)} | hThreads:${hackThreads} hTime:${(growTime * (growLoops - 1) + hackWait + hackTime / 60000).toFixed(2)}m | gThreads:${growThreads} gLoops:${growLoops} gTime:${(growTime * growLoops / 60000).toFixed(2)}m | wThreads:${weakenThreads} wTime:${(weakenTime / 60000).toFixed(2)}m`;
// ns.print(log);
// ns.write('autoHack.txt', log + '\n', 'a');
let hackThreads = Math.floor(freeRAM / (hackRAM + growRAM * growThreadsNeededPerHack));
hackThreads = hackThreads == 0 ? 1 : hackThreads;
const growThreads = Math.floor((freeRAM - hackThreads * hackRAM) / growRAM);
const growLoops = Math.ceil(growThreadsNeededPerHack / growThreads);
const weakenThreads = Math.floor(freeRAM / weakenRAM);
const log = `host:${host.name} target:${host.name} | security:${(host.currentSecurity / host.minSecurity * 100).toFixed(0)}% | money:${(host.currentMoney / host.maxMoney * 100).toFixed(0)}% | hThreads:${hackThreads} hTime:${((growTime * (growLoops - 1) + hackWait + hackTime) / 60000).toFixed(2)}m | gThreads:${growThreads} gLoops:${growLoops} gTime:${(growTime * growLoops / 60000).toFixed(2)}m`;
ns.print(log);
ns.write('autoHack.txt', log + '\n', 'a');
// if (host.currentSecurity / host.minSecurity > 2) {
// if (weakenThreads > 0) {
// ns.exec('weaken.js', host.name, weakenThreads, host.name);
// }
// } else {
// ns.exec('grow.js', host.name, growThreads, host.name, growLoops);
// ns.exec('hack.js', host.name, hackThreads, host.name, growTime * (growLoops - 1) + hackWait);
// }
// } else {
const freeRAM = ns.getServerMaxRam(host.name) - ns.getServerUsedRam(host.name) - (host.name == 'home' ? 1.8 : 0);
const hackRAM = ns.getScriptRam('hack.js', host.name);
const growRAM = ns.getScriptRam('grow.js', host.name);
const weakenRAM = ns.getScriptRam('weaken.js', host.name);
const hackTime = ns.getHackTime(maxMoneyHost.name);
const growTime = ns.getGrowTime(maxMoneyHost.name);
const weakenTime = ns.getWeakenTime(maxMoneyHost.name);
const hackWait = hackTime <= growTime ? growTime - hackTime + 50 : 0;
const hackAnalyze = ns.hackAnalyze(maxMoneyHost.name);
const growThreadsNeededPerHack = Math.ceil(ns.growthAnalyze(maxMoneyHost.name, 1 + hackAnalyze));
let hackThreads = Math.floor(freeRAM / (hackRAM + growRAM * growThreadsNeededPerHack));
hackThreads = hackThreads == 0 ? 1 : hackThreads;
const growThreads = Math.floor((freeRAM - hackThreads * hackRAM) / growRAM);
// ns.tprint(`Math.floor((${freeRAM} - ${hackThreads} * ${hackRAM}) / ${growRAM}) = ${growThreads}`);
const growLoops = Math.ceil(growThreadsNeededPerHack / growThreads);
const weakenThreads = Math.floor(freeRAM / weakenRAM);
const log = `host:${host.name} target:${maxMoneyHost.name} | security:${(maxMoneyHost.currentSecurity / maxMoneyHost.minSecurity).toFixed(2)}% | money:${(maxMoneyHost.currentMoney / maxMoneyHost.maxMoney * 100).toFixed(0)}% | hThreads:${hackThreads} hTime:${((growTime * (growLoops - 1) + hackWait + hackTime) / 60000).toFixed(2)}m | gThreads:${growThreads} gLoops:${growLoops} gTime:${(growTime * growLoops / 60000).toFixed(2)}m`; // | wThreads:${weakenThreads} wTime:${(weakenTime / 60000).toFixed(2)}m`;
ns.print(log);
ns.write('autoHackMax.txt', log + '\n', 'a');
if (maxMoneyHost.currentSecurity / maxMoneyHost.minSecurity > 5) {
if (weakenThreads > 0) {
ns.exec('weaken.js', host.name, weakenThreads, maxMoneyHost.name);
if (host.currentSecurity / host.minSecurity > 2) {
if (weakenThreads > 0) {
ns.exec('weaken.js', host.name, weakenThreads, host.name);
}
} else {
ns.exec('grow.js', host.name, growThreads, host.name, growLoops);
ns.exec('hack.js', host.name, hackThreads, host.name, growTime * (growLoops - 1) + hackWait);
}
} else {
if (maxMoneyHost.currentMoney / maxMoneyHost.maxMoney < 0.2) {
ns.exec('grow.js', host.name, Math.floor(freeRAM / growRAM), maxMoneyHost.name);
const freeRAM = ns.getServerMaxRam(host.name) - ns.getServerUsedRam(host.name) - (host.name == 'home' ? 1.8 : 0);
const hackRAM = ns.getScriptRam('hack.js', host.name);
const growRAM = ns.getScriptRam('grow.js', host.name);
const weakenRAM = ns.getScriptRam('weaken.js', host.name);
const hackTime = ns.getHackTime(maxMoneyHost.name);
const growTime = ns.getGrowTime(maxMoneyHost.name);
const weakenTime = ns.getWeakenTime(maxMoneyHost.name);
const hackWait = hackTime <= growTime ? growTime - hackTime + 50 : 0;
const hackAnalyze = ns.hackAnalyze(maxMoneyHost.name);
const growThreadsNeededPerHack = Math.ceil(ns.growthAnalyze(maxMoneyHost.name, 1 + hackAnalyze));
let hackThreads = Math.floor(freeRAM / (hackRAM + growRAM * growThreadsNeededPerHack));
hackThreads = hackThreads == 0 ? 1 : hackThreads;
const growThreads = Math.floor((freeRAM - hackThreads * hackRAM) / growRAM);
// ns.tprint(`Math.floor((${freeRAM} - ${hackThreads} * ${hackRAM}) / ${growRAM}) = ${growThreads}`);
const growLoops = Math.ceil(growThreadsNeededPerHack / growThreads);
const weakenThreads = Math.floor(freeRAM / weakenRAM);
const log = `host:${host.name} target:${maxMoneyHost.name} | security:${(maxMoneyHost.currentSecurity / maxMoneyHost.minSecurity * 100).toFixed(0)}% | money:${(maxMoneyHost.currentMoney / maxMoneyHost.maxMoney * 100).toFixed(0)}% | hThreads:${hackThreads} hTime:${((growTime * (growLoops - 1) + hackWait + hackTime) / 60000).toFixed(2)}m | gThreads:${growThreads} gLoops:${growLoops} gTime:${(growTime * growLoops / 60000).toFixed(2)}m`; // | wThreads:${weakenThreads} wTime:${(weakenTime / 60000).toFixed(2)}m`;
ns.print(log);
ns.write('autoHackMax.txt', log + '\n', 'a');
// ns.tprint(`${maxMoneyHost.currentSecurity} / ${maxMoneyHost.minSecurity} > 5 = ${maxMoneyHost.currentSecurity / maxMoneyHost.minSecurity > 5}`)
if (maxMoneyHost.currentSecurity / maxMoneyHost.minSecurity > 5) {
if (weakenThreads > 0) {
ns.exec('weaken.js', host.name, weakenThreads, maxMoneyHost.name);
}
} else {
ns.exec('grow.js', host.name, growThreads, maxMoneyHost.name, growLoops);
ns.exec('hack.js', host.name, hackThreads, maxMoneyHost.name, growTime * (growLoops - 1) + hackWait);
if (maxMoneyHost.currentMoney / maxMoneyHost.maxMoney < 0.2) {
ns.exec('grow.js', host.name, Math.floor(freeRAM / growRAM), maxMoneyHost.name);
} else {
ns.exec('grow.js', host.name, growThreads, maxMoneyHost.name, growLoops);
ns.exec('hack.js', host.name, hackThreads, maxMoneyHost.name, growTime * (growLoops - 1) + hackWait);
}
}
}
// }
}
}