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
+13 -13
View File
@@ -144,7 +144,7 @@ export const ns: InternalAPI<NSFull> = {
// 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<NSFull> = {
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<NSFull> = {
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<NSFull> = {
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<NSFull> = {
throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || "");
}
const weakenTime = calculateWeakenTime(server, Player);
const weakenTime = calculateWeakenTime(server, Player) + additionalMsec / 1000.0;
helpers.log(
ctx,
() =>