mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-26 19:14:32 +02:00
NETSCRIPT: Add ramOverride() function (#1346)
This adds a way to dynamically change the static RAM limit of a script, which is also its current RAM usage. This makes it possible for scripts to dynamically change their memory footprint, opening up new strategies beyond current ram-dodging. Calling functions still permanently increases the *dynamic* memory limit; RAM-dodging is still the optimal strategy for avoiding RAM costs, in that sense. This also adds dynamicRamUsage to the info returned by `getRunningScript`, to allow introspection on the currently needed ram.
This commit is contained in:
@@ -63,6 +63,7 @@ import {
|
||||
formatNumber,
|
||||
} from "./ui/formatNumber";
|
||||
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
|
||||
import { roundToTwo } from "./utils/helpers/roundToTwo";
|
||||
import { LogBoxEvents, LogBoxCloserEvents } from "./ui/React/LogBoxManager";
|
||||
import { arrayToString } from "./utils/helpers/ArrayHelpers";
|
||||
import { NetscriptGang } from "./NetscriptFunctions/Gang";
|
||||
@@ -1478,8 +1479,31 @@ export const ns: InternalAPI<NSFull> = {
|
||||
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
|
||||
const runningScript = helpers.getRunningScript(ctx, ident);
|
||||
if (runningScript === null) return null;
|
||||
return helpers.createPublicRunningScript(runningScript);
|
||||
return helpers.createPublicRunningScript(runningScript, ctx.workerScript);
|
||||
},
|
||||
ramOverride: (ctx) => (_ram) => {
|
||||
const newRam = roundToTwo(helpers.number(ctx, "ram", _ram || 0));
|
||||
const rs = ctx.workerScript.scriptRef;
|
||||
const server = ctx.workerScript.getServer();
|
||||
if (newRam < roundToTwo(ctx.workerScript.dynamicRamUsage)) {
|
||||
// Impossibly small, return immediately.
|
||||
return rs.ramUsage;
|
||||
}
|
||||
const newServerRamUsed = roundToTwo(server.ramUsed + (newRam - rs.ramUsage) * rs.threads);
|
||||
if (newServerRamUsed >= server.maxRam) {
|
||||
// Can't allocate more RAM.
|
||||
return rs.ramUsage;
|
||||
}
|
||||
if (newServerRamUsed <= 0) {
|
||||
throw helpers.errorMessage(
|
||||
ctx,
|
||||
`Game error: Calculated impossible new server ramUsed ${newServerRamUsed} from new limit of ${_ram}`,
|
||||
);
|
||||
}
|
||||
server.updateRamUsed(newServerRamUsed);
|
||||
rs.ramUsage = newRam;
|
||||
return rs.ramUsage;
|
||||
},
|
||||
getHackTime:
|
||||
(ctx) =>
|
||||
(_hostname = ctx.workerScript.hostname) => {
|
||||
|
||||
Reference in New Issue
Block a user