mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 16:52:55 +02:00
Initial commit for converting workerScripts pool to Map data structure
This commit is contained in:
+46
-1
@@ -407,6 +407,42 @@ function processNetscript1Imports(code, workerScript) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
let pidCounter = 1;
|
||||
function generateNextPid() {
|
||||
let tempCounter = pidCounter;
|
||||
|
||||
// Cap the number of search iterations at some arbitrary value to avoid
|
||||
// infinite loops. We'll assume that players wont have 1mil+ running scripts
|
||||
let found = false;
|
||||
for (let i = 0; i < 1e6;) {
|
||||
if (!workerScripts.has(tempCounter + i)) {
|
||||
found = true;
|
||||
tempCounter = tempCounter + i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (i === Number.MAX_SAFE_INTEGER - 1) {
|
||||
i = 1;
|
||||
} else {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
pidCounter = tempCounter + 1;
|
||||
if (pidCounter >= Number.MAX_SAFE_INTEGER) {
|
||||
pidCounter = 1;
|
||||
}
|
||||
|
||||
return tempCounter;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a script
|
||||
*
|
||||
@@ -438,8 +474,17 @@ export function addWorkerScript(runningScriptObj, server) {
|
||||
}
|
||||
server.ramUsed = roundToTwo(server.ramUsed + ramUsage);
|
||||
|
||||
// Get the pid
|
||||
const pid = getNextPid();
|
||||
if (pid === -1) {
|
||||
throw new Error(
|
||||
`Failed to start script because could not find available PID. This is most ` +
|
||||
`because you have too many scripts running.`
|
||||
);
|
||||
}
|
||||
|
||||
// Create the WorkerScript
|
||||
const s = new WorkerScript(runningScriptObj, NetscriptFunctions);
|
||||
const s = new WorkerScript(runningScriptObj, pid, NetscriptFunctions);
|
||||
s.ramUsage = ramUsage;
|
||||
|
||||
// Start the script's execution
|
||||
|
||||
Reference in New Issue
Block a user