From fa29d2d9a81c31fbdcb04c40e92c78d1cf55a52b Mon Sep 17 00:00:00 2001 From: chris380 Date: Tue, 12 Apr 2022 17:42:49 +0200 Subject: [PATCH] take into account max thread needed --- dist/bitburner.d.ts | 3 ++- src/NetscriptFunctions.ts | 19 +++++++++++++++++-- src/ScriptEditor/NetscriptDefinitions.d.ts | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/dist/bitburner.d.ts b/dist/bitburner.d.ts index ec8f84056..1d312c99f 100644 --- a/dist/bitburner.d.ts +++ b/dist/bitburner.d.ts @@ -2825,9 +2825,10 @@ export declare interface NS extends Singularity { * Returns the security increase that would occur if a hack 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. * @returns The security increase. */ - hackAnalyzeSecurity(threads: number): number; + hackAnalyzeSecurity(threads: number, hostname?: string): number; /** * Get the chance of successfully hacking a server. diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 0ced4545f..80a86e3c7 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -585,9 +585,24 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { return calculatePercentMoneyHacked(server, Player); }, - hackAnalyzeSecurity: function (_threads: unknown): number { + hackAnalyzeSecurity: function (_threads: unknown, hostname?: string): number { updateDynamicRam("hackAnalyzeSecurity", getRamCost(Player, "hackAnalyzeSecurity")); - const threads = helper.number("hackAnalyzeSecurity", "threads", _threads); + let threads = helper.number("hackAnalyzeSecurity", "threads", _threads); + if (hostname) { + const server = safeGetServer(hostname, "hackAnalyze"); + if (!(server instanceof Server)) { + workerScript.log("hackAnalyzeSecurity", () => "Cannot be executed on this server."); + return 0; + } + + const percentHacked = calculatePercentMoneyHacked(server, Player); + + if (percentHacked > 0) { + // thread count is limited to the maximum number of threads needed + threads = Math.ceil(1 / percentHacked); + } + } + return CONSTANTS.ServerFortifyAmount * threads; }, hackAnalyzeChance: function (_hostname: unknown): number { diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 18da4e065..5191e4829 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4555,9 +4555,10 @@ export interface NS extends Singularity { * Returns the security increase that would occur if a hack 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. * @returns The security increase. */ - hackAnalyzeSecurity(threads: number): number; + hackAnalyzeSecurity(threads: number, hostname?: string): number; /** * Get the chance of successfully hacking a server.