diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 2a65975e1d..ad06137b68 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -294,16 +294,15 @@ export const ns: InternalAPI = { if (host === null) { throw helpers.errorMessage(ctx, `Cannot find host of WorkerScript. Hostname: ${ctx.workerScript.hostname}.`); } - const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable; - processSingleServerGrowth(server, threads, host.cpuCores); + const moneyBefore = server.moneyAvailable; + const growth = processSingleServerGrowth(server, threads, host.cpuCores); const moneyAfter = server.moneyAvailable; ctx.workerScript.scriptRef.recordGrow(server.hostname, threads); const expGain = calculateHackingExpGain(server, Player) * threads; - const logGrowPercent = moneyAfter / moneyBefore - 1; helpers.log( ctx, () => - `Available money on '${server.hostname}' grown by ${formatPercent(logGrowPercent, 6)}. Gained ${formatExp( + `Available money on '${server.hostname}' grown by ${formatPercent(growth - 1, 6)}. Gained ${formatExp( expGain, )} hacking exp (t=${formatThreads(threads)}).`, ); @@ -312,7 +311,7 @@ export const ns: InternalAPI = { if (stock) { influenceStockThroughServerGrow(server, moneyAfter - moneyBefore); } - return Promise.resolve(moneyAfter / moneyBefore); + return Promise.resolve(server.moneyMax === 0 ? 0 : growth); }); }, growthAnalyze: diff --git a/src/Script/ScriptHelpers.ts b/src/Script/ScriptHelpers.ts index 5bff465e89..2001a4af8e 100644 --- a/src/Script/ScriptHelpers.ts +++ b/src/Script/ScriptHelpers.ts @@ -33,19 +33,23 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript): if (runningScript.dataMap[hostname][2] == 0 || runningScript.dataMap[hostname][2] == null) { continue; } - const serv = GetServer(hostname); - if (serv == null) { + const server = GetServer(hostname); + if (server == null) { continue; } const timesGrown = Math.round( ((0.5 * runningScript.dataMap[hostname][2]) / runningScript.onlineRunningTime) * timePassed, ); - runningScript.log(`Called on ${serv.hostname} ${timesGrown} times while offline`); + runningScript.log(`Called on ${server.hostname} ${timesGrown} times while offline`); const host = GetServer(runningScript.server); - if (host === null) throw new Error("getServer of null key?"); - if (!(serv instanceof Server)) throw new Error("trying to grow a non-normal server"); - const growth = processSingleServerGrowth(serv, timesGrown, host.cpuCores); - runningScript.log(`'${serv.hostname}' grown by ${formatPercent(growth - 1, 6)} while offline`); + if (host === null) { + throw new Error("getServer of null key?"); + } + if (!(server instanceof Server)) { + throw new Error("trying to grow a non-normal server"); + } + const growth = processSingleServerGrowth(server, timesGrown, host.cpuCores); + runningScript.log(`'${server.hostname}' grown by ${formatPercent(growth - 1, 6)} while offline`); } } diff --git a/src/Server/ServerHelpers.ts b/src/Server/ServerHelpers.ts index 81fafb06f7..959ea7bfbf 100644 --- a/src/Server/ServerHelpers.ts +++ b/src/Server/ServerHelpers.ts @@ -188,6 +188,14 @@ export function processSingleServerGrowth(server: Server, threads: number, cores usedCycles = Math.min(Math.max(0, Math.ceil(usedCycles)), threads); server.fortify(2 * ServerConstants.ServerFortifyAmount * usedCycles); } + // Prevent returning NaN. This happens when server.moneyMax is 0. + if (server.moneyAvailable === 0 && oldMoneyAvailable === 0) { + return 1; + } + // Prevent returning Infinity. If server's money before growing is 0, we "pretend" that it's 1. + if (oldMoneyAvailable === 0) { + return server.moneyAvailable; + } return server.moneyAvailable / oldMoneyAvailable; } diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index 01502efec7..1a329d0c36 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -310,12 +310,12 @@ export class Terminal { if (!(server instanceof Server)) throw new Error("server should be normal server"); const expGain = calculateHackingExpGain(server, Player); const oldSec = server.hackDifficulty; - const growth = processSingleServerGrowth(server, 25, server.cpuCores) - 1; + const growth = processSingleServerGrowth(server, 25, server.cpuCores); const newSec = server.hackDifficulty; Player.gainHackingExp(expGain); this.print( - `Available money on '${server.hostname}' grown by ${formatPercent(growth, 6)}. Gained ${formatExp( + `Available money on '${server.hostname}' grown by ${formatPercent(growth - 1, 6)}. Gained ${formatExp( expGain, )} hacking exp.`, );