From 4d551915b3dc6a57a76749e89b19f30e741bc514 Mon Sep 17 00:00:00 2001 From: Caldwell <115591472+Caldwell-74@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:13:42 +0100 Subject: [PATCH] MISC: move server constants into their own constant (#1075) --- src/Achievements/Achievements.ts | 3 ++- src/Constants.ts | 23 ------------------ src/Locations/ui/RamButton.tsx | 4 ++-- src/Netscript/NetscriptHelpers.tsx | 3 ++- src/NetscriptFunctions.ts | 9 +++---- src/NetscriptFunctions/Singularity.ts | 3 ++- .../Player/PlayerObjectServerMethods.ts | 4 ++-- src/Script/ScriptHelpers.ts | 3 ++- src/Server/ServerHelpers.ts | 4 ++-- src/Server/ServerPurchases.ts | 11 +++++---- src/Server/data/Constants.ts | 24 +++++++++++++++++++ src/Server/formulas/grow.ts | 8 +++---- src/Terminal/Terminal.ts | 5 ++-- 13 files changed, 56 insertions(+), 48 deletions(-) create mode 100644 src/Server/data/Constants.ts diff --git a/src/Achievements/Achievements.ts b/src/Achievements/Achievements.ts index c04985719..374387120 100644 --- a/src/Achievements/Achievements.ts +++ b/src/Achievements/Achievements.ts @@ -30,6 +30,7 @@ import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; import { workerScripts } from "../Netscript/WorkerScripts"; import { getRecordValues } from "../Types/Record"; +import { ServerConstants } from "../Server/data/Constants"; // Unable to correctly cast the JSON data into AchievementDataJson type otherwise... const achievementData = ((data)).achievements; @@ -232,7 +233,7 @@ export const achievements: Record = { MAX_RAM: { ...achievementData.MAX_RAM, Icon: "maxram", - Condition: () => Player.getHomeComputer().maxRam === CONSTANTS.HomeComputerMaxRam, + Condition: () => Player.getHomeComputer().maxRam === ServerConstants.HomeComputerMaxRam, }, MAX_CORES: { ...achievementData.MAX_CORES, diff --git a/src/Constants.ts b/src/Constants.ts index 0e6fd04f1..b416579c1 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -12,8 +12,6 @@ export const CONSTANTS: { OfflineHackingIncome: number; CorpFactionRepRequirement: number; BaseFocusBonus: number; - BaseCostFor1GBOfRamHome: number; - BaseCostFor1GBOfRamServer: number; TravelCost: number; BaseFavorToDonate: number; DonateMoneyToRepDivisor: number; @@ -23,13 +21,6 @@ export const CONSTANTS: { CompanyReputationToFavorMult: number; NeuroFluxGovernorLevelMult: number; NumNetscriptPorts: number; - HomeComputerMaxRam: number; - ServerBaseGrowthIncr: number; - ServerMaxGrowthLog: number; - ServerFortifyAmount: number; - ServerWeakenAmount: number; - PurchasedServerLimit: number; - PurchasedServerMaxRam: number; MultipleAugMultiplier: number; TorRouterCost: number; HospitalCostPerHp: number; @@ -103,10 +94,6 @@ export const CONSTANTS: { // How much reputation is needed to join a megacorporation's faction CorpFactionRepRequirement: 400e3, - // Base RAM costs - BaseCostFor1GBOfRamHome: 32000, - BaseCostFor1GBOfRamServer: 55000, //1 GB of RAM - // Cost to travel to another city TravelCost: 200e3, @@ -123,16 +110,6 @@ export const CONSTANTS: { NumNetscriptPorts: Number.MAX_SAFE_INTEGER, - // 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 - // Augmentation Constants MultipleAugMultiplier: 1.9, diff --git a/src/Locations/ui/RamButton.tsx b/src/Locations/ui/RamButton.tsx index e0f8d2ed1..76f2fc0d9 100644 --- a/src/Locations/ui/RamButton.tsx +++ b/src/Locations/ui/RamButton.tsx @@ -3,7 +3,6 @@ import Button from "@mui/material/Button"; import Tooltip from "@mui/material/Tooltip"; import Typography from "@mui/material/Typography"; -import { CONSTANTS } from "../../Constants"; import { Player } from "@player"; import { purchaseRamForHomeComputer } from "../../Server/ServerPurchases"; @@ -12,6 +11,7 @@ import { formatRam } from "../../ui/formatNumber"; import { MathJax } from "better-react-mathjax"; import { currentNodeMults } from "../../BitNode/BitNodeMultipliers"; +import { ServerConstants } from "../../Server/data/Constants"; interface IProps { rerender: () => void; @@ -19,7 +19,7 @@ interface IProps { export function RamButton(props: IProps): React.ReactElement { const homeComputer = Player.getHomeComputer(); - if (homeComputer.maxRam >= CONSTANTS.HomeComputerMaxRam) { + if (homeComputer.maxRam >= ServerConstants.HomeComputerMaxRam) { return ; } diff --git a/src/Netscript/NetscriptHelpers.tsx b/src/Netscript/NetscriptHelpers.tsx index 581ce2cf8..652fc3f25 100644 --- a/src/Netscript/NetscriptHelpers.tsx +++ b/src/Netscript/NetscriptHelpers.tsx @@ -38,6 +38,7 @@ import { Engine } from "../engine"; import { resolveFilePath, FilePath } from "../Paths/FilePath"; import { hasScriptExtension, ScriptFilePath } from "../Paths/ScriptFilePath"; import { CustomBoundary } from "../ui/Components/CustomBoundary"; +import { ServerConstants } from "../Server/data/Constants"; export const helpers = { string, @@ -540,7 +541,7 @@ function hack(ctx: NetscriptContext, hostname: string, manual: boolean, opts: un expGainedOnSuccess, )} exp (t=${formatThreads(threads)})`, ); - server.fortify(CONSTANTS.ServerFortifyAmount * Math.min(threads, maxThreadNeeded)); + server.fortify(ServerConstants.ServerFortifyAmount * Math.min(threads, maxThreadNeeded)); if (stock) { influenceStockThroughServerHack(server, moneyDrained); } diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index f09ad84cf..475e0a5be 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -106,6 +106,7 @@ import { hasContractExtension } from "./Paths/ContractFilePath"; import { getRamCost } from "./Netscript/RamCostGenerator"; import { getEnumHelper } from "./utils/EnumHelper"; import { setDeprecatedProperties, deprecationWarning } from "./utils/DeprecationHelper"; +import { ServerConstants } from "./Server/data/Constants"; export const enums: NSEnums = { CityName, @@ -220,7 +221,7 @@ export const ns: InternalAPI = { } } - return CONSTANTS.ServerFortifyAmount * threads; + return ServerConstants.ServerFortifyAmount * threads; }, hackAnalyzeChance: (ctx) => (_hostname) => { const hostname = helpers.string(ctx, "hostname", _hostname); @@ -346,7 +347,7 @@ export const ns: InternalAPI = { threads = Math.min(threads, maxThreadsNeeded); } - return 2 * CONSTANTS.ServerFortifyAmount * threads; + return 2 * ServerConstants.ServerFortifyAmount * threads; }, weaken: (ctx) => async (_hostname, opts?) => { const hostname = helpers.string(ctx, "hostname", _hostname); @@ -381,7 +382,7 @@ export const ns: InternalAPI = { } const cores = host.cpuCores; const coreBonus = getCoreBonus(cores); - const weakenAmt = CONSTANTS.ServerWeakenAmount * threads * coreBonus; + const weakenAmt = ServerConstants.ServerWeakenAmount * threads * coreBonus; server.weaken(weakenAmt); ctx.workerScript.scriptRef.recordWeaken(server.hostname, threads); const expGain = calculateHackingExpGain(server, Player) * threads; @@ -404,7 +405,7 @@ export const ns: InternalAPI = { const threads = helpers.number(ctx, "threads", _threads); const cores = helpers.number(ctx, "cores", _cores); const coreBonus = getCoreBonus(cores); - return CONSTANTS.ServerWeakenAmount * threads * coreBonus * currentNodeMults.ServerWeakenRate; + return ServerConstants.ServerWeakenAmount * threads * coreBonus * currentNodeMults.ServerWeakenRate; }, share: (ctx) => () => { const cores = helpers.getServer(ctx, ctx.workerScript.hostname).cpuCores; diff --git a/src/NetscriptFunctions/Singularity.ts b/src/NetscriptFunctions/Singularity.ts index 6a2efb32b..91f520bd2 100644 --- a/src/NetscriptFunctions/Singularity.ts +++ b/src/NetscriptFunctions/Singularity.ts @@ -56,6 +56,7 @@ import { ScriptFilePath, resolveScriptFilePath } from "../Paths/ScriptFilePath"; import { root } from "../Paths/Directory"; import { getRecordEntries } from "../Types/Record"; import { JobTracks } from "../Company/data/JobTracks"; +import { ServerConstants } from "../Server/data/Constants"; export function NetscriptSingularity(): InternalAPI { const runAfterReset = function (cbScript: ScriptFilePath) { @@ -640,7 +641,7 @@ export function NetscriptSingularity(): InternalAPI { // Check if we're at max RAM const homeComputer = Player.getHomeComputer(); - if (homeComputer.maxRam >= CONSTANTS.HomeComputerMaxRam) { + if (homeComputer.maxRam >= ServerConstants.HomeComputerMaxRam) { helpers.log(ctx, () => `Your home computer is at max RAM.`); return false; } diff --git a/src/PersonObjects/Player/PlayerObjectServerMethods.ts b/src/PersonObjects/Player/PlayerObjectServerMethods.ts index 94cd8ebdb..c8bc47676 100644 --- a/src/PersonObjects/Player/PlayerObjectServerMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectServerMethods.ts @@ -1,5 +1,5 @@ // Server and HacknetServer-related methods for the Player class (PlayerObject) -import { CONSTANTS } from "../../Constants"; +import { ServerConstants } from "../../Server/data/Constants"; import { currentNodeMults } from "../../BitNode/BitNodeMultipliers"; import { Server } from "../../Server/Server"; @@ -35,7 +35,7 @@ export function getUpgradeHomeRamCost(this: PlayerObject): number { //Calculate cost //Have cost increase by some percentage each time RAM has been upgraded const mult = Math.pow(1.58, numUpgrades); - const cost = currentRam * CONSTANTS.BaseCostFor1GBOfRamHome * mult * currentNodeMults.HomeComputerRamCost; + const cost = currentRam * ServerConstants.BaseCostFor1GBOfRamHome * mult * currentNodeMults.HomeComputerRamCost; return cost; } diff --git a/src/Script/ScriptHelpers.ts b/src/Script/ScriptHelpers.ts index edc6cbe8c..f6da4054e 100644 --- a/src/Script/ScriptHelpers.ts +++ b/src/Script/ScriptHelpers.ts @@ -10,6 +10,7 @@ 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,7 +84,7 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript): ); runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`); const coreBonus = 1 + (host.cpuCores - 1) / 16; - serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened * coreBonus); + serv.weaken(ServerConstants.ServerWeakenAmount * timesWeakened * coreBonus); } } } diff --git a/src/Server/ServerHelpers.ts b/src/Server/ServerHelpers.ts index 0b0911e51..05796e8ae 100644 --- a/src/Server/ServerHelpers.ts +++ b/src/Server/ServerHelpers.ts @@ -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; } diff --git a/src/Server/ServerPurchases.ts b/src/Server/ServerPurchases.ts index 849e1daaa..b25114f32 100644 --- a/src/Server/ServerPurchases.ts +++ b/src/Server/ServerPurchases.ts @@ -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; } diff --git a/src/Server/data/Constants.ts b/src/Server/data/Constants.ts new file mode 100644 index 000000000..ce2ee7a86 --- /dev/null +++ b/src/Server/data/Constants.ts @@ -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 +}; diff --git a/src/Server/formulas/grow.ts b/src/Server/formulas/grow.ts index 22ae675c6..4e2416e01 100644 --- a/src/Server/formulas/grow.ts +++ b/src/Server/formulas/grow.ts @@ -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 diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index 88f38d611..0c8498a13 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -81,6 +81,7 @@ import { Directory, resolveDirectory, root } from "../Paths/Directory"; import { FilePath, isFilePath, resolveFilePath } from "../Paths/FilePath"; import { hasTextExtension } from "../Paths/TextFilePath"; import { ContractFilePath } from "../Paths/ContractFilePath"; +import { ServerConstants } from "../Server/data/Constants"; export class Terminal { // Flags to determine whether the player is currently running a hack or an analyze @@ -227,7 +228,7 @@ export class Terminal { Player.gainIntelligenceExp(expGainedOnSuccess / CONSTANTS.IntelligenceTerminalHackBaseExpGain); const oldSec = server.hackDifficulty; - server.fortify(CONSTANTS.ServerFortifyAmount); + server.fortify(ServerConstants.ServerFortifyAmount); const newSec = server.hackDifficulty; this.print( @@ -279,7 +280,7 @@ 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(CONSTANTS.ServerWeakenAmount); + server.weaken(ServerConstants.ServerWeakenAmount); const newSec = server.hackDifficulty; Player.gainHackingExp(expGain);