NETSCRIPT: Add additionalMsec to BasicHGWOptions

This option adds additional time to the sleep in hack/grow/weaken before
the command takes effect. The critical difference between this and doing
your own sleep is that it creates a single, uninterruptible sleep: This
opens up multiple new avenues of gameplay for batching.

Note that use of this new feature is theoretically always suboptimal,
since extending the sleep time wastes RAM.
This commit is contained in:
David Walker 2023-01-10 18:18:33 -08:00
parent af0ed1dbb0
commit 53755dd573
3 changed files with 26 additions and 15 deletions

@ -434,17 +434,22 @@ function hack(
ctx: NetscriptContext, ctx: NetscriptContext,
hostname: string, hostname: string,
manual: boolean, manual: boolean,
{ threads: requestedThreads, stock }: BasicHGWOptions = {}, { threads: requestedThreads, stock, additionalMsec: requestedSec }: BasicHGWOptions = {},
): Promise<number> { ): Promise<number> {
const ws = ctx.workerScript; const ws = ctx.workerScript;
const threads = helpers.resolveNetscriptRequestedThreads(ctx, requestedThreads); 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); const server = getServer(ctx, hostname);
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
throw makeRuntimeErrorMsg(ctx, "Cannot be executed on this server."); throw makeRuntimeErrorMsg(ctx, "Cannot be executed on this server.");
} }
// Calculate the hacking time // 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 // No root access or skill level too low
const canHack = netscriptCanHack(server); const canHack = netscriptCanHack(server);

@ -144,7 +144,7 @@ export const ns: InternalAPI<NSFull> = {
// TODO 2.2: better type safety rework for functions using assertObjectType, then remove function. // TODO 2.2: better type safety rework for functions using assertObjectType, then remove function.
const optsValidator: BasicHGWOptions = {}; const optsValidator: BasicHGWOptions = {};
assertObjectType(ctx, "opts", opts, optsValidator); 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) => { hackAnalyzeThreads: (ctx) => (_hostname, _hackAmount) => {
const hostname = helpers.string(ctx, "hostname", _hostname); const hostname = helpers.string(ctx, "hostname", _hostname);
@ -241,11 +241,11 @@ export const ns: InternalAPI<NSFull> = {
const hostname = helpers.string(ctx, "hostname", _hostname); const hostname = helpers.string(ctx, "hostname", _hostname);
const optsValidator: BasicHGWOptions = {}; const optsValidator: BasicHGWOptions = {};
assertObjectType(ctx, "opts", opts, optsValidator); assertObjectType(ctx, "opts", opts, optsValidator);
const requestedThreads = const threads = helpers.resolveNetscriptRequestedThreads(ctx, opts.threads);
opts.threads === undefined const additionalMsec = helpers.number(ctx, "opts.additionalMsec", opts.additionalMsec ?? 0);
? ctx.workerScript.scriptRef.threads if (additionalMsec < 0) {
: helpers.number(ctx, "opts.threads", opts.threads); throw helpers.makeRuntimeErrorMsg(ctx, `additionalMsec must be non-negative, got ${additionalMsec}`);
const threads = helpers.resolveNetscriptRequestedThreads(ctx, requestedThreads); }
const server = helpers.getServer(ctx, hostname); const server = helpers.getServer(ctx, hostname);
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
@ -264,7 +264,7 @@ export const ns: InternalAPI<NSFull> = {
throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || ""); throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || "");
} }
const growTime = calculateGrowTime(server, Player); const growTime = calculateGrowTime(server, Player) + additionalMsec / 1000.0;
helpers.log( helpers.log(
ctx, ctx,
() => () =>
@ -348,11 +348,11 @@ export const ns: InternalAPI<NSFull> = {
const hostname = helpers.string(ctx, "hostname", _hostname); const hostname = helpers.string(ctx, "hostname", _hostname);
const optsValidator: BasicHGWOptions = {}; const optsValidator: BasicHGWOptions = {};
assertObjectType(ctx, "opts", opts, optsValidator); assertObjectType(ctx, "opts", opts, optsValidator);
const requestedThreads = const threads = helpers.resolveNetscriptRequestedThreads(ctx, opts.threads);
opts.threads === undefined const additionalMsec = helpers.number(ctx, "opts.additionalMsec", opts.additionalMsec ?? 0);
? ctx.workerScript.scriptRef.threads if (additionalMsec < 0) {
: helpers.number(ctx, "opts.threads", opts.threads); throw helpers.makeRuntimeErrorMsg(ctx, `additionalMsec must be non-negative, got ${additionalMsec}`);
const threads = helpers.resolveNetscriptRequestedThreads(ctx, requestedThreads); }
const server = helpers.getServer(ctx, hostname); const server = helpers.getServer(ctx, hostname);
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
@ -366,7 +366,7 @@ export const ns: InternalAPI<NSFull> = {
throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || ""); throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || "");
} }
const weakenTime = calculateWeakenTime(server, Player); const weakenTime = calculateWeakenTime(server, Player) + additionalMsec / 1000.0;
helpers.log( helpers.log(
ctx, ctx,
() => () =>

@ -256,6 +256,12 @@ interface BasicHGWOptions {
threads?: number; threads?: number;
/** Set to true this action will affect the stock market. */ /** Set to true this action will affect the stock market. */
stock?: boolean; 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;
} }
/** /**