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

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

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

@ -2,7 +2,7 @@ import { GetServer, createUniqueRandomIp, ipExists } from "./AllServers";
import { Server, IConstructorParams } from "./Server"; import { Server, IConstructorParams } from "./Server";
import { BaseServer } from "./BaseServer"; import { BaseServer } from "./BaseServer";
import { calculateServerGrowth, calculateServerGrowthLog } from "./formulas/grow"; import { calculateServerGrowth, calculateServerGrowthLog } from "./formulas/grow";
import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
import { ServerConstants } from "./data/Constants"; import { ServerConstants } from "./data/Constants";
import { Player } from "@player"; import { Player } from "@player";
import { CompletedProgramName, LiteratureName } from "@enums"; import { CompletedProgramName, LiteratureName } from "@enums";
@ -258,3 +258,8 @@ export function getCoreBonus(cores = 1): number {
const coreBonus = 1 + (cores - 1) / 16; const coreBonus = 1 + (cores - 1) / 16;
return coreBonus; 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 { checkIfConnectedToDarkweb } from "../DarkWeb/DarkWeb";
import { iTutorialNextStep, iTutorialSteps, ITutorial } from "../InteractiveTutorial"; import { iTutorialNextStep, iTutorialSteps, ITutorial } from "../InteractiveTutorial";
import { processSingleServerGrowth } from "../Server/ServerHelpers"; import { processSingleServerGrowth, getWeakenEffect } from "../Server/ServerHelpers";
import { parseCommand, parseCommands } from "./Parser"; import { parseCommand, parseCommands } from "./Parser";
import { SpecialServers } from "../Server/data/SpecialServers"; import { SpecialServers } from "../Server/data/SpecialServers";
import { Settings } from "../Settings/Settings"; import { Settings } from "../Settings/Settings";
@ -280,14 +280,16 @@ export class Terminal {
if (!(server instanceof Server)) throw new Error("server should be normal server"); if (!(server instanceof Server)) throw new Error("server should be normal server");
const expGain = calculateHackingExpGain(server, Player); const expGain = calculateHackingExpGain(server, Player);
const oldSec = server.hackDifficulty; const oldSec = server.hackDifficulty;
server.weaken(ServerConstants.ServerWeakenAmount); const weakenAmt = getWeakenEffect(1, server.cpuCores);
server.weaken(weakenAmt);
const newSec = server.hackDifficulty; const newSec = server.hackDifficulty;
Player.gainHackingExp(expGain); Player.gainHackingExp(expGain);
this.print( this.print(
`Security decreased on '${server.hostname}' from ${formatSecurity(oldSec)} to ${formatSecurity( `Security decreased on '${server.hostname}' by ${formatSecurity(weakenAmt)} from ${formatSecurity(
newSec, oldSec,
)} (min: ${formatSecurity(server.minDifficulty)})` + ` and Gained ${formatExp(expGain)} hacking exp.`, )} to ${formatSecurity(newSec)} (min: ${formatSecurity(server.minDifficulty)})` +
` and Gained ${formatExp(expGain)} hacking exp.`,
); );
} }