Skip to content

Commit

Permalink
BUGFIX: Grow log shows invalid values in edge cases (#1872)
Browse files Browse the repository at this point in the history
  • Loading branch information
catloversg authored Jan 9, 2025
1 parent 5b6380a commit 8c19165
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/NetscriptFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,15 @@ export const ns: InternalAPI<NSFull> = {
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)}).`,
);
Expand All @@ -312,7 +311,7 @@ export const ns: InternalAPI<NSFull> = {
if (stock) {
influenceStockThroughServerGrow(server, moneyAfter - moneyBefore);
}
return Promise.resolve(moneyAfter / moneyBefore);
return Promise.resolve(server.moneyMax === 0 ? 0 : growth);
});
},
growthAnalyze:
Expand Down
18 changes: 11 additions & 7 deletions src/Script/ScriptHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Server/ServerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Terminal/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
);
Expand Down

0 comments on commit 8c19165

Please sign in to comment.