Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns **/
- /** weaken.js
- export async function main(ns) {
- if (ns.args[1]) {
- await ns.sleep(ns.args[1])
- }
- await ns.weaken(ns.args[0])
- }
- */
- export async function main(ns) {
- const disablelogfns = ["disableLog", "purchaseServer", "deleteServer", "travelToCity", "universityCourse", "scp", "purchaseAugmentation", "workForFaction", "joinFaction", "purchaseProgram", "connect", "scan", "getHackingLevel", "getServerUsedRam", "getServerMaxRam", "getServerSecurityLevel", "getServerMinSecurityLevel", "getServerMaxMoney", "getServerMaxMoney", "sleep", "exec", "brutessh", "ftpcrack", "relaysmtp", "httpworm", "sqlinject", "getServerNumPortsRequired", "nuke"]
- for (const fn of disablelogfns) {
- ns.disableLog(fn)
- }
- let TARGET = ns.getServer(ns.args[0] ? ns.args[0] : "n00dles")
- const DELAY = 25 // time between consecutive h/g completions
- const SAFETYDELAY = 15 // accounts for code execution time during burst()
- const RESERVERAM = 8 // reserve ram on home
- const SCPFILES = ["weaken.js", "grow.js", "hack.js"]
- const THREADRAM = 1.75
- let HOSTSERVERS = ns.getPurchasedServers()
- HOSTSERVERS.push("home")
- let MAXTHREADS = 0 // max threads of w|g|h across all host servers
- for (const server of HOSTSERVERS) {
- MAXTHREADS += Math.floor(ns.getServerMaxRam(server) / THREADRAM)
- if (server == "home") {
- MAXTHREADS -= Math.ceil((RESERVERAM + ns.getScriptRam(ns.getScriptName())) / THREADRAM)
- }
- await ns.scp(SCPFILES, server)
- }
- let COUNT = 0
- function execany(script, threads, ...args) {
- // exec script on any free host server
- // does not raise error on 0 threads
- const scriptram = ns.getScriptRam(script)
- for (const server of HOSTSERVERS) {
- if (threads <= 0) {
- return
- }
- let thisthreads = Math.floor((ns.getServerMaxRam(server) - ns.getServerUsedRam(server)) / scriptram) // threads allocated to this server
- if (server == "home") {
- thisthreads -= Math.ceil(RESERVERAM / THREADRAM)
- }
- if (thisthreads <= 0) {
- continue
- }
- if (thisthreads > threads) {
- thisthreads = threads
- }
- threads -= thisthreads
- ns.exec(script, server, thisthreads, ...args)
- }
- if (threads > 0) {
- ns.print("execany excess threads ", threads)
- }
- }
- function anyscriptsrunning() {
- for (const server of HOSTSERVERS) {
- if (ns.getServerUsedRam(server) > 0) {
- for (const script of SCPFILES) {
- if (ns.scriptRunning(script, server)) {
- return true
- }
- }
- }
- }
- return false
- }
- function killall() {
- let killed = 0
- for (const server of HOSTSERVERS) {
- for (const script of SCPFILES) {
- killed += ns.scriptKill(script, server)
- }
- }
- ns.print("killed ", killed, " scripts")
- }
- function getthreadsweakengrow() {
- let weakenthreads = (TARGET.hackDifficulty - TARGET.minDifficulty) / 0.05
- if (weakenthreads < MAXTHREADS) {
- weakenthreads += (MAXTHREADS - weakenthreads) * 0.004 / (0.054) // to counter grow threads
- } else {
- weakenthreads = MAXTHREADS
- }
- weakenthreads = Math.ceil(weakenthreads)
- let growthreads = MAXTHREADS - weakenthreads
- return [weakenthreads, growthreads]
- }
- async function weakengrow() {
- // weakens and grows the TARGET
- let weakenthreads, growthreads
- [weakenthreads, growthreads] = getthreadsweakengrow()
- ns.print(`${TARGET.hostname} sec+ ${TARGET.hackDifficulty - TARGET.minDifficulty}`
- + ` money% ${Math.floor(100 * TARGET.moneyAvailable / TARGET.moneyMax)}`
- + ` w ${weakenthreads} g ${growthreads} | ${Math.floor(ns.getWeakenTime(TARGET.hostname) / 1000)}s`)
- execany("weaken.js", weakenthreads, TARGET.hostname)
- execany("grow.js", growthreads, TARGET.hostname)
- await ns.sleep(ns.getWeakenTime(TARGET.hostname) + DELAY)
- while (anyscriptsrunning()) {
- await ns.sleep(100)
- }
- }
- function getthreadsburst() {
- let hackamount = 0.4 // portion of server's money per hack()
- let hackthreads, growthreads, weakenthreads
- while (true) {
- hackthreads = Math.floor(ns.hackAnalyzeThreads(TARGET.hostname, hackamount * TARGET.moneyMax))
- hackthreads = Math.max(1, hackthreads)
- growthreads = Math.ceil(ns.growthAnalyze(TARGET.hostname, 2.2 / (1 - hackamount)))
- weakenthreads = Math.ceil((hackthreads * 0.002 + growthreads * 0.004) / 0.05)
- if (hackthreads + growthreads + weakenthreads > MAXTHREADS) {
- hackamount *= 0.9 * MAXTHREADS / (hackthreads + growthreads + weakenthreads)
- } else {
- break
- }
- }
- return [hackthreads, growthreads, weakenthreads]
- }
- async function burst() {
- // precisely timed burst avoids levelup interference
- // hacks the TARGET using alternating hacks and grows in rapid succession followed by a weaken
- let hackthreads, growthreads, weakenthreads
- [hackthreads, growthreads, weakenthreads] = getthreadsburst()
- let rounds = MAXTHREADS / (hackthreads + growthreads + weakenthreads)
- let wtime = ns.getWeakenTime(TARGET.hostname)
- rounds = Math.floor(Math.min(rounds, 0.25 * wtime / (2 * DELAY + SAFETYDELAY) - 1))
- execany("weaken.js", MAXTHREADS - rounds * (hackthreads + growthreads), TARGET.hostname, 0.05 * wtime)
- ns.print(TARGET.hostname, " r|g|h ", rounds, "|", hackthreads, "|", growthreads,
- " | ", COUNT, " | ", Math.floor(1.05 * wtime / 1000), "s")
- for (let i = 0; i < rounds; i++) {
- execany("grow.js", growthreads, TARGET.hostname, 0, i)
- execany("hack.js", hackthreads, TARGET.hostname, 0.55 * wtime - DELAY, i)
- await ns.sleep(2 * DELAY)
- TARGET = ns.getServer(TARGET.hostname)
- }
- await ns.sleep(0.8 * wtime)
- let now = ns.getTimeSinceLastAug()
- while (anyscriptsrunning()) {
- await ns.sleep(100)
- }
- let diff = ns.getTimeSinceLastAug() - now
- // ns.print("slack time ", diff / 1000, "s ", Math.round(100 * diff / (0.25 * wtime)), "%")
- }
- // START
- ns.tail()
- killall()
- while (true) {
- TARGET = ns.getServer(ns.args[0])
- if (TARGET.hackDifficulty > TARGET.minDifficulty || TARGET.moneyAvailable < TARGET.moneyMax) {
- COUNT = 0
- await weakengrow()
- } else {
- COUNT++
- await burst()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement