MISC: refactor weaken effect calculation (#1076)

so far we calculate the effect of weaken in three +1 places
ns.weaken
ns.weakenAnalyze
terminal weaken

and server.weaken where the bn mult is applied

i extracted the logic into a new netscript helper function getWeakenEffect
this gives us one place if we want to change the formula

a side effect i added the server.cpuCores to the terminal weaken to future proof it if the npc server core pr (#963) is merged
This commit is contained in:
Caldwell 2024-02-17 02:18:16 +01:00 committed by GitHub
parent 93b9a10e41
commit 8c8af38a3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 17 deletions

@ -36,6 +36,7 @@ import {
processSingleServerGrowth,
safelyCreateUniqueServer,
getCoreBonus,
getWeakenEffect,
} from "./Server/ServerHelpers";
import {
getPurchasedServerUpgradeCost,
@ -380,9 +381,7 @@ export const ns: InternalAPI<NSFull> = {
helpers.log(ctx, () => "Server is null, did it die?");
return Promise.resolve(0);
}
const cores = host.cpuCores;
const coreBonus = getCoreBonus(cores);
const weakenAmt = ServerConstants.ServerWeakenAmount * threads * coreBonus;
const weakenAmt = getWeakenEffect(threads, host.cpuCores);
server.weaken(weakenAmt);
ctx.workerScript.scriptRef.recordWeaken(server.hostname, threads);
const expGain = calculateHackingExpGain(server, Player) * threads;
@ -396,7 +395,7 @@ export const ns: InternalAPI<NSFull> = {
ctx.workerScript.scriptRef.onlineExpGained += expGain;
Player.gainHackingExp(expGain);
// Account for hidden multiplier in Server.weaken()
return Promise.resolve(weakenAmt * currentNodeMults.ServerWeakenRate);
return Promise.resolve(weakenAmt);
});
},
weakenAnalyze:
@ -404,8 +403,7 @@ export const ns: InternalAPI<NSFull> = {
(_threads, _cores = 1) => {
const threads = helpers.number(ctx, "threads", _threads);
const cores = helpers.number(ctx, "cores", _cores);
const coreBonus = getCoreBonus(cores);
return ServerConstants.ServerWeakenAmount * threads * coreBonus * currentNodeMults.ServerWeakenRate;
return getWeakenEffect(threads, cores);
},
share: (ctx) => () => {
const cores = helpers.getServer(ctx, ctx.workerScript.hostname).cpuCores;

@ -3,14 +3,13 @@ import { Player } from "@player";
import { BaseServer } from "../Server/BaseServer";
import { Server } from "../Server/Server";
import { RunningScript } from "./RunningScript";
import { processSingleServerGrowth } from "../Server/ServerHelpers";
import { getWeakenEffect, processSingleServerGrowth } from "../Server/ServerHelpers";
import { GetServer } from "../Server/AllServers";
import { formatPercent } from "../ui/formatNumber";
import { workerScripts } from "../Netscript/WorkerScripts";
import { scriptKey } from "../utils/helpers/scriptKey";
import type { ScriptFilePath } from "../Paths/ScriptFilePath";
import { ServerConstants } from "../Server/data/Constants";
export function scriptCalculateOfflineProduction(runningScript: RunningScript): void {
//The Player object stores the last update time from when we were online
@ -83,8 +82,8 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
((0.5 * runningScript.dataMap[hostname][3]) / runningScript.onlineRunningTime) * timePassed,
);
runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
const coreBonus = 1 + (host.cpuCores - 1) / 16;
serv.weaken(ServerConstants.ServerWeakenAmount * timesWeakened * coreBonus);
const weakenAmount = getWeakenEffect(runningScript.threads, host.cpuCores);
serv.weaken(weakenAmount * timesWeakened);
}
}
}

@ -141,7 +141,7 @@ export class Server extends BaseServer {
/** Lowers the server's security level (difficulty) by the specified amount) */
weaken(amt: number): void {
this.hackDifficulty -= amt * currentNodeMults.ServerWeakenRate;
this.hackDifficulty -= amt;
this.capDifficulty();
}

@ -2,7 +2,7 @@ import { GetServer, createUniqueRandomIp, ipExists } from "./AllServers";
import { Server, IConstructorParams } from "./Server";
import { BaseServer } from "./BaseServer";
import { calculateServerGrowth, calculateServerGrowthLog } from "./formulas/grow";
import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
import { ServerConstants } from "./data/Constants";
import { Player } from "@player";
import { CompletedProgramName, LiteratureName } from "@enums";
@ -258,3 +258,8 @@ export function getCoreBonus(cores = 1): number {
const coreBonus = 1 + (cores - 1) / 16;
return coreBonus;
}
export function getWeakenEffect(threads: number, cores: number): number {
const coreBonus = getCoreBonus(cores);
return ServerConstants.ServerWeakenAmount * threads * coreBonus * currentNodeMults.ServerWeakenRate;
}

@ -17,7 +17,7 @@ import { GetServer } from "../Server/AllServers";
import { checkIfConnectedToDarkweb } from "../DarkWeb/DarkWeb";
import { iTutorialNextStep, iTutorialSteps, ITutorial } from "../InteractiveTutorial";
import { processSingleServerGrowth } from "../Server/ServerHelpers";
import { processSingleServerGrowth, getWeakenEffect } from "../Server/ServerHelpers";
import { parseCommand, parseCommands } from "./Parser";
import { SpecialServers } from "../Server/data/SpecialServers";
import { Settings } from "../Settings/Settings";
@ -280,14 +280,16 @@ export class Terminal {
if (!(server instanceof Server)) throw new Error("server should be normal server");
const expGain = calculateHackingExpGain(server, Player);
const oldSec = server.hackDifficulty;
server.weaken(ServerConstants.ServerWeakenAmount);
const weakenAmt = getWeakenEffect(1, server.cpuCores);
server.weaken(weakenAmt);
const newSec = server.hackDifficulty;
Player.gainHackingExp(expGain);
this.print(
`Security decreased on '${server.hostname}' from ${formatSecurity(oldSec)} to ${formatSecurity(
newSec,
)} (min: ${formatSecurity(server.minDifficulty)})` + ` and Gained ${formatExp(expGain)} hacking exp.`,
`Security decreased on '${server.hostname}' by ${formatSecurity(weakenAmt)} from ${formatSecurity(
oldSec,
)} to ${formatSecurity(newSec)} (min: ${formatSecurity(server.minDifficulty)})` +
` and Gained ${formatExp(expGain)} hacking exp.`,
);
}