Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Grow log shows invalid values in edge cases #1872

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/NetscriptFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,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 @@ -314,7 +313,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 @@ -306,12 +306,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
Loading