NETSCRIPT: add formulas.hacking.growAmount() (#1090)

Also, improve docs.
This commit is contained in:
David Walker
2024-03-26 03:26:50 -07:00
committed by GitHub
parent d8de22a273
commit 75dabd10be
10 changed files with 132 additions and 32 deletions
+2 -20
View File
@@ -1,13 +1,12 @@
import { GetServer, createUniqueRandomIp, ipExists } from "./AllServers";
import { Server, IConstructorParams } from "./Server";
import { BaseServer } from "./BaseServer";
import { calculateServerGrowth, calculateServerGrowthLog } from "./formulas/grow";
import { calculateGrowMoney, calculateServerGrowthLog } from "./formulas/grow";
import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
import { ServerConstants } from "./data/Constants";
import { Player } from "@player";
import { CompletedProgramName, LiteratureName } from "@enums";
import { Person as IPerson } from "@nsdefs";
import { isValidNumber } from "../utils/helpers/isValidNumber";
import { Server as IServer } from "@nsdefs";
import { workerScripts } from "../Netscript/WorkerScripts";
import { killWorkerScriptByPid } from "../Netscript/killWorkerScript";
@@ -177,25 +176,8 @@ export function numCycleForGrowthCorrected(
//Applied server growth for a single server. Returns the percentage growth
export function processSingleServerGrowth(server: Server, threads: number, cores = 1): number {
let serverGrowth = calculateServerGrowth(server, threads, Player, cores);
if (serverGrowth < 1) {
console.warn("serverGrowth calculated to be less than 1");
serverGrowth = 1;
}
const oldMoneyAvailable = server.moneyAvailable;
server.moneyAvailable += threads; // It can be grown even if it has no money
server.moneyAvailable *= serverGrowth;
// in case of data corruption
if (isValidNumber(server.moneyMax) && isNaN(server.moneyAvailable)) {
server.moneyAvailable = server.moneyMax;
}
// cap at max
if (isValidNumber(server.moneyMax) && server.moneyAvailable > server.moneyMax) {
server.moneyAvailable = server.moneyMax;
}
server.moneyAvailable = calculateGrowMoney(server, threads, Player, cores);
// if there was any growth at all, increase security
if (oldMoneyAvailable !== server.moneyAvailable) {
+25
View File
@@ -1,6 +1,7 @@
import { currentNodeMults } from "../../BitNode/BitNodeMultipliers";
import { Person as IPerson, Server as IServer } from "@nsdefs";
import { ServerConstants } from "../data/Constants";
import { isValidNumber } from "../../utils/helpers/isValidNumber";
// 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 {
@@ -30,3 +31,27 @@ export function calculateServerGrowth(server: IServer, threads: number, p: IPers
if (!server.serverGrowth) return 0;
return Math.exp(calculateServerGrowthLog(server, threads, p, cores));
}
// This differs from calculateServerGrowth in that it includes the additive
// factor and all the boundary checks.
export function calculateGrowMoney(server: IServer, threads: number, p: IPerson, cores = 1): number {
let serverGrowth = calculateServerGrowth(server, threads, p, cores);
if (serverGrowth < 1) {
console.warn("serverGrowth calculated to be less than 1");
serverGrowth = 1;
}
let moneyAvailable = server.moneyAvailable ?? Number.NaN;
moneyAvailable += threads; // It can be grown even if it has no money
moneyAvailable *= serverGrowth;
// cap at max (or data corruption)
if (
server.moneyMax !== undefined &&
isValidNumber(server.moneyMax) &&
(moneyAvailable > server.moneyMax || isNaN(moneyAvailable))
) {
moneyAvailable = server.moneyMax;
}
return moneyAvailable;
}