Create ports on demand. Increase max to infinity.

This commit is contained in:
Zoë Hoekstra 2022-09-01 16:13:10 +02:00
parent 2592c6ccd8
commit ec81fb60c3
No known key found for this signature in database
GPG Key ID: F9B7B7D8130F3323
4 changed files with 22 additions and 13 deletions

@ -124,7 +124,7 @@ export const CONSTANTS: {
// NeuroFlux Governor Augmentation cost multiplier
NeuroFluxGovernorLevelMult: 1.14,
NumNetscriptPorts: 20,
NumNetscriptPorts: Infinity,
// Server-related constants
HomeComputerMaxRam: 1073741824, // 2 ^ 30

@ -20,7 +20,7 @@ import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { CONSTANTS } from "../Constants";
import { influenceStockThroughServerHack } from "../StockMarket/PlayerInfluencing";
import { IPort } from "../NetscriptPort";
import { IPort, NetscriptPort } from "../NetscriptPort";
import { NetscriptPorts } from "../NetscriptWorker";
import { IPlayer } from "../PersonObjects/IPlayer";
import { FormulaGang } from "../Gang/formulas/formulas";
@ -492,10 +492,16 @@ function getValidPort(ctx: NetscriptContext, port: number): IPort {
`Trying to use an invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
);
}
const iport = NetscriptPorts[port - 1];
let iport = NetscriptPorts.get(port);
if (iport == null || !(iport instanceof Object)) {
throw makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`);
NetscriptPorts.set(port, NetscriptPort());
}
// Try again.
iport = NetscriptPorts.get(port);
if (iport == null || !(iport instanceof Object)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`);
}
return iport;
}

@ -54,7 +54,7 @@ import { NetscriptCorporation } from "./NetscriptFunctions/Corporation";
import { NetscriptFormulas } from "./NetscriptFunctions/Formulas";
import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket";
import { NetscriptGrafting } from "./NetscriptFunctions/Grafting";
import { IPort } from "./NetscriptPort";
import { NetscriptPort, IPort } from "./NetscriptPort";
import {
NS,
Player as INetscriptPlayer,
@ -1533,7 +1533,12 @@ const base: InternalAPI<NS> = {
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
);
}
const iport = NetscriptPorts[port - 1];
let iport = NetscriptPorts.get(port);
if (iport == null || !(iport instanceof Object)) {
NetscriptPorts.set(port, NetscriptPort());
}
// Try again.
iport = NetscriptPorts.get(port);
if (iport == null || !(iport instanceof Object)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`);
}

@ -13,7 +13,7 @@ import { CONSTANTS } from "./Constants";
import { Interpreter } from "./ThirdParty/JSInterpreter";
import { NetscriptFunctions } from "./NetscriptFunctions";
import { executeJSScript, Node } from "./NetscriptJSEvaluator";
import { NetscriptPort, IPort } from "./NetscriptPort";
import { IPort } from "./NetscriptPort";
import { RunningScript } from "./Script/RunningScript";
import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers";
import { scriptCalculateOfflineProduction } from "./Script/ScriptHelpers";
@ -38,17 +38,15 @@ import { ScriptArg } from "./Netscript/ScriptArg";
import { helpers } from "./Netscript/NetscriptHelpers";
// Netscript Ports are instantiated here
export const NetscriptPorts: IPort[] = [];
for (let i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) {
NetscriptPorts.push(NetscriptPort());
}
export const NetscriptPorts: Map<number, IPort> = new Map();
export function prestigeWorkerScripts(): void {
for (const ws of workerScripts.values()) {
ws.env.stopFlag = true;
killWorkerScript(ws);
}
for (const port of NetscriptPorts) {
for (const [__, port] of NetscriptPorts.entries()) {
port.clear();
}