diff --git a/src/Netscript/NetscriptHelpers.ts b/src/Netscript/NetscriptHelpers.ts index 542581597..868e76c74 100644 --- a/src/Netscript/NetscriptHelpers.ts +++ b/src/Netscript/NetscriptHelpers.ts @@ -434,17 +434,22 @@ function hack( ctx: NetscriptContext, hostname: string, manual: boolean, - { threads: requestedThreads, stock }: BasicHGWOptions = {}, + { threads: requestedThreads, stock, additionalMsec: requestedSec }: BasicHGWOptions = {}, ): Promise { const ws = ctx.workerScript; const threads = helpers.resolveNetscriptRequestedThreads(ctx, requestedThreads); + const additionalMsec = number(ctx, "opts.additionalMsec", requestedSec ?? 0); + if (additionalMsec < 0) { + throw makeRuntimeErrorMsg(ctx, `additionalMsec must be non-negative, got ${additionalMsec}`); + } const server = getServer(ctx, hostname); if (!(server instanceof Server)) { throw makeRuntimeErrorMsg(ctx, "Cannot be executed on this server."); } // Calculate the hacking time - const hackingTime = calculateHackingTime(server, Player); // This is in seconds + // This is in seconds + const hackingTime = calculateHackingTime(server, Player) + additionalMsec / 1000.0; // No root access or skill level too low const canHack = netscriptCanHack(server); diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 3933265e4..acefb97c6 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -144,7 +144,7 @@ export const ns: InternalAPI = { // TODO 2.2: better type safety rework for functions using assertObjectType, then remove function. const optsValidator: BasicHGWOptions = {}; assertObjectType(ctx, "opts", opts, optsValidator); - return helpers.hack(ctx, hostname, false, { threads: opts.threads, stock: opts.stock }); + return helpers.hack(ctx, hostname, false, opts); }, hackAnalyzeThreads: (ctx) => (_hostname, _hackAmount) => { const hostname = helpers.string(ctx, "hostname", _hostname); @@ -241,11 +241,11 @@ export const ns: InternalAPI = { const hostname = helpers.string(ctx, "hostname", _hostname); const optsValidator: BasicHGWOptions = {}; assertObjectType(ctx, "opts", opts, optsValidator); - const requestedThreads = - opts.threads === undefined - ? ctx.workerScript.scriptRef.threads - : helpers.number(ctx, "opts.threads", opts.threads); - const threads = helpers.resolveNetscriptRequestedThreads(ctx, requestedThreads); + const threads = helpers.resolveNetscriptRequestedThreads(ctx, opts.threads); + const additionalMsec = helpers.number(ctx, "opts.additionalMsec", opts.additionalMsec ?? 0); + if (additionalMsec < 0) { + throw helpers.makeRuntimeErrorMsg(ctx, `additionalMsec must be non-negative, got ${additionalMsec}`); + } const server = helpers.getServer(ctx, hostname); if (!(server instanceof Server)) { @@ -264,7 +264,7 @@ export const ns: InternalAPI = { throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || ""); } - const growTime = calculateGrowTime(server, Player); + const growTime = calculateGrowTime(server, Player) + additionalMsec / 1000.0; helpers.log( ctx, () => @@ -348,11 +348,11 @@ export const ns: InternalAPI = { const hostname = helpers.string(ctx, "hostname", _hostname); const optsValidator: BasicHGWOptions = {}; assertObjectType(ctx, "opts", opts, optsValidator); - const requestedThreads = - opts.threads === undefined - ? ctx.workerScript.scriptRef.threads - : helpers.number(ctx, "opts.threads", opts.threads); - const threads = helpers.resolveNetscriptRequestedThreads(ctx, requestedThreads); + const threads = helpers.resolveNetscriptRequestedThreads(ctx, opts.threads); + const additionalMsec = helpers.number(ctx, "opts.additionalMsec", opts.additionalMsec ?? 0); + if (additionalMsec < 0) { + throw helpers.makeRuntimeErrorMsg(ctx, `additionalMsec must be non-negative, got ${additionalMsec}`); + } const server = helpers.getServer(ctx, hostname); if (!(server instanceof Server)) { @@ -366,7 +366,7 @@ export const ns: InternalAPI = { throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || ""); } - const weakenTime = calculateWeakenTime(server, Player); + const weakenTime = calculateWeakenTime(server, Player) + additionalMsec / 1000.0; helpers.log( ctx, () => diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 6badd6a82..e7990421f 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -256,6 +256,12 @@ interface BasicHGWOptions { threads?: number; /** Set to true this action will affect the stock market. */ stock?: boolean; + /** Number of additional milliseconds that will be spent waiting between the + * start of the function and when it completes. There will only be one, + * contiguous wait, which is relevant because stats such as server security + * level are computed at the *start* of the function. + * Must be non-negative. */ + additionalMsec?: number; } /**