Make grow formulas faster and more accurate. (#1044)

This commit is contained in:
David Walker
2024-01-31 16:27:31 -08:00
committed by GitHub
parent b6b4788845
commit 55e21d1e19
5 changed files with 184 additions and 140 deletions
+19 -12
View File
@@ -2,24 +2,31 @@ import { CONSTANTS } from "../../Constants";
import { currentNodeMults } from "../../BitNode/BitNodeMultipliers";
import { Person as IPerson, Server as IServer } from "@nsdefs";
export function calculateServerGrowth(server: IServer, threads: number, p: IPerson, cores = 1): number {
if (!server.serverGrowth) return 0;
// Returns the log of the growth rate. When passing 1 for threads, this gives a useful constant.
export function calculateServerGrowthLog(server: IServer, threads: number, p: IPerson, cores = 1): number {
if (!server.serverGrowth) return -Infinity;
const hackDifficulty = server.hackDifficulty ?? 100;
const numServerGrowthCycles = Math.max(Math.floor(threads), 0);
const numServerGrowthCycles = Math.max(threads, 0);
//Get adjusted growth rate, which accounts for server security
const growthRate = CONSTANTS.ServerBaseGrowthRate;
let adjGrowthRate = 1 + (growthRate - 1) / hackDifficulty;
if (adjGrowthRate > CONSTANTS.ServerMaxGrowthRate) {
adjGrowthRate = CONSTANTS.ServerMaxGrowthRate;
//Get adjusted growth log, which accounts for server security
//log1p computes log(1+p), it is far more accurate for small values.
let adjGrowthLog = Math.log1p(CONSTANTS.ServerBaseGrowthIncr / hackDifficulty);
if (adjGrowthLog >= CONSTANTS.ServerMaxGrowthLog) {
adjGrowthLog = CONSTANTS.ServerMaxGrowthLog;
}
//Calculate adjusted server growth rate based on parameters
const serverGrowthPercentage = server.serverGrowth / 100;
const numServerGrowthCyclesAdjusted =
numServerGrowthCycles * serverGrowthPercentage * currentNodeMults.ServerGrowthRate;
const serverGrowthPercentageAdjusted = serverGrowthPercentage * currentNodeMults.ServerGrowthRate;
//Apply serverGrowth for the calculated number of growth cycles
const coreBonus = 1 + (cores - 1) / 16;
return Math.pow(adjGrowthRate, numServerGrowthCyclesAdjusted * p.mults.hacking_grow * coreBonus);
const coreBonus = 1 + (cores - 1) * (1 / 16);
// It is critical that numServerGrowthCycles (aka threads) is multiplied last,
// so that it rounds the same way as numCycleForGrowthCorrected.
return adjGrowthLog * serverGrowthPercentageAdjusted * p.mults.hacking_grow * coreBonus * numServerGrowthCycles;
}
export function calculateServerGrowth(server: IServer, threads: number, p: IPerson, cores = 1): number {
if (!server.serverGrowth) return 0;
return Math.exp(calculateServerGrowthLog(server, threads, p, cores));
}