From 4c113c19eb9f8aae22ffc0c5f93de6deb05815f7 Mon Sep 17 00:00:00 2001 From: chris380 Date: Wed, 13 Apr 2022 09:16:59 +0200 Subject: [PATCH] take into account max threads needed --- src/NetscriptFunctions.ts | 22 +++++++++++++++++++--- src/ScriptEditor/NetscriptDefinitions.d.ts | 4 +++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 5134fd41d..8c06637b0 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -189,7 +189,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { throw makeRuntimeRejectMsg( workerScript, `Invalid scriptArgs argument passed into getRunningScript() from ${callingFnName}(). ` + - `This is probably a bug. Please report to game developer`, + `This is probably a bug. Please report to game developer`, ); } @@ -752,9 +752,25 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { return numCycleForGrowth(server, Number(growth), Player, cores); }, - growthAnalyzeSecurity: function (_threads: unknown): number { + growthAnalyzeSecurity: function (_threads: unknown, _hostname: unknown, _cores?: unknown): number { updateDynamicRam("growthAnalyzeSecurity", getRamCost(Player, "growthAnalyzeSecurity")); - const threads = helper.number("growthAnalyzeSecurity", "threads", _threads); + let threads = helper.number("growthAnalyzeSecurity", "threads", _threads); + if (_hostname) { + const cores = helper.number("growthAnalyzeSecurity", "cores", _cores) || 1; + const hostname = helper.string("growthAnalyzeSecurity", "hostname", _hostname); + const server = safeGetServer(hostname, "growthAnalyzeSecurity"); + + if (!(server instanceof Server)) { + workerScript.log("growthAnalyzeSecurity", () => "Cannot be executed on this server."); + return 0; + } + + const growthFactor = server.moneyMax / server.moneyAvailable; + const maxThreadsNeeded = Math.ceil(numCycleForGrowth(server, growthFactor, Player, cores)); + + threads = Math.min(threads, maxThreadsNeeded); + } + return 2 * CONSTANTS.ServerFortifyAmount * threads; }, weaken: async function (_hostname: unknown, { threads: requestedThreads }: BasicHGWOptions = {}): Promise { diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 569bb8478..e8956df8f 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4635,9 +4635,11 @@ export interface NS { * Returns the security increase that would occur if a grow with this many threads happened. * * @param threads - Amount of threads that will be used. + * @param hostname - Hostname of the target server. The number of threads is limited to the number needed to hack the servers maximum amount of money. + * @param cores - Optional. The number of cores of the server that would run grow. * @returns The security increase. */ - growthAnalyzeSecurity(threads: number): number; + growthAnalyzeSecurity(threads: number, hostname: string, cores?: number): number; /** * Suspends the script for n milliseconds.