MISC: move server constants into their own constant (#1075)

This commit is contained in:
Caldwell
2024-02-10 10:13:42 +01:00
committed by GitHub
parent fd5b0f8241
commit 4d551915b3
13 changed files with 56 additions and 48 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ import { Server, IConstructorParams } from "./Server";
import { BaseServer } from "./BaseServer";
import { calculateServerGrowth, calculateServerGrowthLog } from "./formulas/grow";
import { CONSTANTS } from "../Constants";
import { ServerConstants } from "./data/Constants";
import { Player } from "@player";
import { CompletedProgramName, LiteratureName } from "@enums";
import { Person as IPerson } from "@nsdefs";
@@ -202,7 +202,7 @@ export function processSingleServerGrowth(server: Server, threads: number, cores
let usedCycles = numCycleForGrowthCorrected(server, server.moneyAvailable, oldMoneyAvailable, cores);
// Growing increases server security twice as much as hacking
usedCycles = Math.min(Math.max(0, Math.ceil(usedCycles)), threads);
server.fortify(2 * CONSTANTS.ServerFortifyAmount * usedCycles);
server.fortify(2 * ServerConstants.ServerFortifyAmount * usedCycles);
}
return server.moneyAvailable / oldMoneyAvailable;
}
+6 -5
View File
@@ -6,7 +6,7 @@ import { AddToAllServers, createUniqueRandomIp, GetServer, renameServer } from "
import { safelyCreateUniqueServer } from "./ServerHelpers";
import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
import { CONSTANTS } from "../Constants";
import { ServerConstants } from "./data/Constants";
import { Player } from "@player";
import { dialogBoxCreate } from "../ui/React/DialogBox";
@@ -20,6 +20,7 @@ import { workerScripts } from "../Netscript/WorkerScripts";
* @returns Cost of purchasing the given server. Returns infinity for invalid arguments
*/
export function getPurchaseServerCost(ram: number): number {
// TODO shift checks into
const sanitizedRam = Math.round(ram);
if (isNaN(sanitizedRam) || !isPowerOfTwo(sanitizedRam) || !(Math.sign(sanitizedRam) === 1)) {
return Infinity;
@@ -33,7 +34,7 @@ export function getPurchaseServerCost(ram: number): number {
return (
sanitizedRam *
CONSTANTS.BaseCostFor1GBOfRamServer *
ServerConstants.BaseCostFor1GBOfRamServer *
currentNodeMults.PurchasedServerCost *
Math.pow(currentNodeMults.PurchasedServerSoftcap, upg)
);
@@ -86,11 +87,11 @@ export const renamePurchasedServer = (hostname: string, newName: string): void =
};
export function getPurchaseServerLimit(): number {
return Math.round(CONSTANTS.PurchasedServerLimit * currentNodeMults.PurchasedServerLimit);
return Math.round(ServerConstants.PurchasedServerLimit * currentNodeMults.PurchasedServerLimit);
}
export function getPurchaseServerMaxRam(): number {
const ram = Math.round(CONSTANTS.PurchasedServerMaxRam * currentNodeMults.PurchasedServerMaxRam);
const ram = Math.round(ServerConstants.PurchasedServerMaxRam * currentNodeMults.PurchasedServerMaxRam);
// Round this to the nearest power of 2
return 1 << (31 - Math.clz32(ram));
@@ -155,7 +156,7 @@ export function purchaseRamForHomeComputer(): void {
}
const homeComputer = Player.getHomeComputer();
if (homeComputer.maxRam >= CONSTANTS.HomeComputerMaxRam) {
if (homeComputer.maxRam >= ServerConstants.HomeComputerMaxRam) {
dialogBoxCreate(`You cannot upgrade your home computer RAM because it is at its maximum possible value`);
return;
}
+24
View File
@@ -0,0 +1,24 @@
export const ServerConstants: {
BaseCostFor1GBOfRamHome: number;
BaseCostFor1GBOfRamServer: number;
HomeComputerMaxRam: number;
ServerBaseGrowthIncr: number;
ServerMaxGrowthLog: number;
ServerFortifyAmount: number;
ServerWeakenAmount: number;
PurchasedServerLimit: number;
PurchasedServerMaxRam: number;
} = {
// Base RAM costs
BaseCostFor1GBOfRamHome: 32000,
BaseCostFor1GBOfRamServer: 55000, //1 GB of RAM
// Server-related constants
HomeComputerMaxRam: 1073741824, // 2 ^ 30
ServerBaseGrowthIncr: 0.03, // Unadjusted growth increment (growth rate is this * adjustment + 1)
ServerMaxGrowthLog: 0.00349388925425578, // Maximum possible growth rate accounting for server security, precomputed as log1p(.0035)
ServerFortifyAmount: 0.002, // Amount by which server's security increases when its hacked/grown
ServerWeakenAmount: 0.05, // Amount by which server's security decreases when weakened
PurchasedServerLimit: 25,
PurchasedServerMaxRam: 1048576, // 2^20
};
+4 -4
View File
@@ -1,6 +1,6 @@
import { CONSTANTS } from "../../Constants";
import { currentNodeMults } from "../../BitNode/BitNodeMultipliers";
import { Person as IPerson, Server as IServer } from "@nsdefs";
import { ServerConstants } from "../data/Constants";
// 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 {
@@ -10,9 +10,9 @@ export function calculateServerGrowthLog(server: IServer, threads: number, p: IP
//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;
let adjGrowthLog = Math.log1p(ServerConstants.ServerBaseGrowthIncr / hackDifficulty);
if (adjGrowthLog >= ServerConstants.ServerMaxGrowthLog) {
adjGrowthLog = ServerConstants.ServerMaxGrowthLog;
}
//Calculate adjusted server growth rate based on parameters