310 lines
15 KiB
TypeScript
310 lines
15 KiB
TypeScript
import { Player as IPerson, Server as IServer } from "@ns";
|
|
import { currentNodeMults } from "./exports";
|
|
import { clampNumber } from "./utils";
|
|
import { Player } from "./player";
|
|
|
|
export const ServerConstants = {
|
|
// 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
|
|
} as const;
|
|
|
|
/**
|
|
* Checks that a variable is a valid number. A valid number
|
|
* must be a "number" type and cannot be NaN
|
|
*/
|
|
export function isValidNumber(n: number): boolean {
|
|
return typeof n === "number" && !isNaN(n);
|
|
}
|
|
|
|
export function calculateIntelligenceBonus(intelligence: number, weight = 1): number {
|
|
const effectiveIntelligence =
|
|
Player.bitNodeOptions.intelligenceOverride !== undefined
|
|
? Math.min(Player.bitNodeOptions.intelligenceOverride, intelligence)
|
|
: intelligence;
|
|
return 1 + (weight * Math.pow(effectiveIntelligence, 0.8)) / 600;
|
|
}
|
|
|
|
/** Returns the chance the person has to successfully hack a server */
|
|
export function calculateHackingChance(server: IServer, person: IPerson): number {
|
|
const hackDifficulty = server.hackDifficulty ?? 100;
|
|
const requiredHackingSkill = server.requiredHackingSkill ?? 1e9;
|
|
// Unrooted or unhackable server
|
|
if (!server.hasAdminRights || hackDifficulty >= 100) return 0;
|
|
const hackFactor = 1.75;
|
|
const difficultyMult = (100 - hackDifficulty) / 100;
|
|
const skillMult = clampNumber(hackFactor * person.skills.hacking, 1);
|
|
const skillChance = (skillMult - requiredHackingSkill) / skillMult;
|
|
const chance =
|
|
skillChance *
|
|
difficultyMult *
|
|
person.mults.hacking_chance *
|
|
calculateIntelligenceBonus(person.skills.intelligence, 1);
|
|
return clampNumber(chance, 0, 1);
|
|
}
|
|
|
|
/**
|
|
* Returns the amount of hacking experience the person will gain upon
|
|
* successfully hacking a server
|
|
*/
|
|
export function calculateHackingExpGain(server: IServer, person: IPerson): number {
|
|
const baseDifficulty = server.baseDifficulty;
|
|
if (!baseDifficulty) return 0;
|
|
const baseExpGain = 3;
|
|
const diffFactor = 0.3;
|
|
let expGain = baseExpGain;
|
|
expGain += baseDifficulty * diffFactor;
|
|
return expGain * person.mults.hacking_exp * currentNodeMults.HackExpGain;
|
|
}
|
|
|
|
/**
|
|
* Returns the percentage of money that will be stolen from a server if
|
|
* it is successfully hacked (returns the decimal form, not the actual percent value)
|
|
*/
|
|
export function calculatePercentMoneyHacked(server: IServer, person: IPerson): number {
|
|
const hackDifficulty = server.hackDifficulty ?? 100;
|
|
if (hackDifficulty >= 100) return 0;
|
|
const requiredHackingSkill = server.requiredHackingSkill ?? 1e9;
|
|
// Adjust if needed for balancing. This is the divisor for the final calculation
|
|
const balanceFactor = 240;
|
|
|
|
const difficultyMult = (100 - hackDifficulty) / 100;
|
|
const skillMult = (person.skills.hacking - (requiredHackingSkill - 1)) / person.skills.hacking;
|
|
const percentMoneyHacked =
|
|
(difficultyMult * skillMult * person.mults.hacking_money * currentNodeMults.ScriptHackMoney) / balanceFactor;
|
|
|
|
return Math.min(1, Math.max(percentMoneyHacked, 0));
|
|
}
|
|
|
|
/** Returns time it takes to complete a hack on a server, in seconds */
|
|
export function calculateHackingTime(server: IServer, person: IPerson): number {
|
|
const { hackDifficulty, requiredHackingSkill } = server;
|
|
if (typeof hackDifficulty !== "number" || typeof requiredHackingSkill !== "number") return Infinity;
|
|
const difficultyMult = requiredHackingSkill * hackDifficulty;
|
|
|
|
const baseDiff = 500;
|
|
const baseSkill = 50;
|
|
const diffFactor = 2.5;
|
|
let skillFactor = diffFactor * difficultyMult + baseDiff;
|
|
skillFactor /= person.skills.hacking + baseSkill;
|
|
|
|
const hackTimeMultiplier = 5;
|
|
const hackingTime =
|
|
(hackTimeMultiplier * skillFactor) /
|
|
(person.mults.hacking_speed *
|
|
currentNodeMults.HackingSpeedMultiplier *
|
|
calculateIntelligenceBonus(person.skills.intelligence, 1));
|
|
|
|
return hackingTime;
|
|
}
|
|
|
|
/** Returns time it takes to complete a grow operation on a server, in seconds */
|
|
export function calculateGrowTime(server: IServer, person: IPerson): number {
|
|
const growTimeMultiplier = 3.2; // Relative to hacking time. 16/5 = 3.2
|
|
|
|
return growTimeMultiplier * calculateHackingTime(server, person);
|
|
}
|
|
|
|
/** Returns time it takes to complete a weaken operation on a server, in seconds */
|
|
export function calculateWeakenTime(server: IServer, person: IPerson): number {
|
|
const weakenTimeMultiplier = 4; // Relative to hacking time
|
|
|
|
return weakenTimeMultiplier * calculateHackingTime(server, person);
|
|
}
|
|
|
|
// 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(threads, 0);
|
|
|
|
//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(ServerConstants.ServerBaseGrowthIncr / hackDifficulty);
|
|
if (adjGrowthLog >= ServerConstants.ServerMaxGrowthLog) {
|
|
adjGrowthLog = ServerConstants.ServerMaxGrowthLog;
|
|
}
|
|
|
|
//Calculate adjusted server growth rate based on parameters
|
|
const serverGrowthPercentage = server.serverGrowth / 100;
|
|
const serverGrowthPercentageAdjusted = serverGrowthPercentage * currentNodeMults.ServerGrowthRate;
|
|
|
|
//Apply serverGrowth for the calculated number of growth cycles
|
|
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));
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
/**
|
|
* Returns the number of "growth cycles" needed to grow the specified server by the specified amount, taking into
|
|
* account only the multiplicative factor. Does not account for the additive $1/thread. Only used for growthAnalyze.
|
|
* @param server - Server being grown
|
|
* @param growth - How much the server is being grown by, in DECIMAL form (e.g. 1.5 rather than 50)
|
|
* @param p - Reference to Player object
|
|
* @returns Number of "growth cycles" needed
|
|
*/
|
|
export function numCycleForGrowth(server: IServer, growth: number, cores = 1): number {
|
|
if (!server.serverGrowth) return Infinity;
|
|
return Math.log(growth) / calculateServerGrowthLog(server, 1, Player, cores);
|
|
}
|
|
|
|
/**
|
|
* This function calculates the number of threads needed to grow a server from one $amount to a higher $amount
|
|
* (ie, how many threads to grow this server from $200 to $600 for example).
|
|
* It protects the inputs (so putting in INFINITY for targetMoney will use moneyMax, putting in a negative for start will use 0, etc.)
|
|
* @param server - Server being grown
|
|
* @param targetMoney - How much you want the server grown TO (not by), for instance, to grow from 200 to 600, input 600
|
|
* @param startMoney - How much you are growing the server from, for instance, to grow from 200 to 600, input 200
|
|
* @param cores - Number of cores on the host performing grow
|
|
* @returns Integer threads needed by a single ns.grow call to reach targetMoney from startMoney.
|
|
*/
|
|
export function numCycleForGrowthCorrected(
|
|
server: IServer,
|
|
targetMoney: number,
|
|
startMoney: number,
|
|
cores = 1,
|
|
person: IPerson = Player,
|
|
): number {
|
|
if (!server.serverGrowth) return Infinity;
|
|
const moneyMax = server.moneyMax ?? 1;
|
|
|
|
if (startMoney < 0) startMoney = 0; // servers "can't" have less than 0 dollars on them
|
|
if (targetMoney > moneyMax) targetMoney = moneyMax; // can't grow a server to more than its moneyMax
|
|
if (targetMoney <= startMoney) return 0; // no growth --> no threads
|
|
|
|
const k = calculateServerGrowthLog(server, 1, person, cores);
|
|
/* To understand what is done below we need to do some math. I hope the explanation is clear enough.
|
|
* First of, the names will be shortened for ease of manipulation:
|
|
* n:= targetMoney (n for new), o:= startMoney (o for old), k:= calculateServerGrowthLog, x:= threads
|
|
* x is what we are trying to compute.
|
|
*
|
|
* After growing, the money on a server is n = (o + x) * exp(k*x)
|
|
* x appears in an exponent and outside it, this is usually solved using the productLog/lambert's W special function,
|
|
* but it turns out that due to floating-point range issues this approach is *useless* to us, so it will be ignored.
|
|
*
|
|
* Instead, we proceed directly to Newton-Raphson iteration. We first rewrite the equation in
|
|
* log-form, since iterating it this way has faster convergence: log(n) = log(o+x) + k*x.
|
|
* Now our goal is to find the zero of f(x) = log((o+x)/n) + k*x.
|
|
* (Due to the shape of the function, there will be a single zero.)
|
|
*
|
|
* The idea of this method is to take the horizontal position at which the horizontal axis
|
|
* intersects with of the tangent of the function's curve as the next approximation.
|
|
* It is equivalent to treating the curve as a line (it is called a first order approximation)
|
|
* If the current approximation is x then the new approximated value is x - f(x)/f'(x)
|
|
* (where f' is the derivative of f).
|
|
*
|
|
* In our case f(x) = log((o+x)/n) + k*x, f'(x) = d(log((o+x)/n) + k*x)/dx
|
|
* = 1/(o + x) + k
|
|
* And the update step is x[new] = x - (log((o+x)/n) + k*x)/(1/(o+x) + k)
|
|
* We can simplify this by bringing the first term up into the fraction:
|
|
* = (x * (1/(o+x) + k) - log((o+x)/n) - k*x) / (1/(o+x) + k)
|
|
* = (x/(o+x) - log((o+x)/n)) / (1/(o+x) + k) [multiplying top and bottom by (o+x)]
|
|
* = (x - (o+x)*log((o+x)/n)) / (1 + (o+x)*k)
|
|
*
|
|
* The main question to ask when using this method is "does it converge?"
|
|
* (are the approximations getting better?), if it does then it does quickly.
|
|
* Since the derivative is always positive but also strictly decreasing, convergence is guaranteed.
|
|
* This also provides the useful knowledge that any x which starts *greater* than the solution will
|
|
* undershoot across to the left, while values *smaller* than the zero will continue to find
|
|
* closer approximations that are still smaller than the final value.
|
|
*
|
|
* Of great importance for reducing the number of iterations is starting with a good initial
|
|
* guess. We use a very simple starting condition: x_0 = n - o. We *know* this will always overshot
|
|
* the target, usually by a vast amount. But we can run it manually through one Newton iteration
|
|
* to get a better start with nice properties:
|
|
* x_1 = ((n - o) - (n - o + o)*log((n-o+o)/n)) / (1 + (n-o+o)*k)
|
|
* = ((n - o) - n * log(n/n)) / (1 + n*k)
|
|
* = ((n - o) - n * 0) / (1 + n*k)
|
|
* = (n - o) / (1 + n*k)
|
|
* We can do the same procedure with the exponential form of Newton's method, starting from x_0 = 0.
|
|
* This gives x_1 = (n - o) / (1 + o*k), (full derivation omitted) which will be an overestimate.
|
|
* We use a weighted average of the denominators to get the final guess:
|
|
* x = (n - o) / (1 + (1/16*n + 15/16*o)*k)
|
|
* The reason for this particular weighting is subtle; it is exactly representable and holds up
|
|
* well under a wide variety of conditions, making it likely that the we start within 1 thread of
|
|
* correct. It particularly bounds the worst-case to 3 iterations, and gives a very wide swatch
|
|
* where 2 iterations is good enough.
|
|
*
|
|
* The accuracy of the initial guess is good for many inputs - often one iteration
|
|
* is sufficient. This means the overall cost is two logs (counting the one in calculateServerGrowthLog),
|
|
* possibly one exp, 5 divisions, and a handful of basic arithmetic.
|
|
*/
|
|
const guess = (targetMoney - startMoney) / (1 + (targetMoney * (1 / 16) + startMoney * (15 / 16)) * k);
|
|
let x = guess;
|
|
let diff;
|
|
do {
|
|
const ox = startMoney + x;
|
|
// Have to use division instead of multiplication by inverse, because
|
|
// if targetMoney is MIN_VALUE then inverting gives Infinity
|
|
const newx = (x - ox * Math.log(ox / targetMoney)) / (1 + ox * k);
|
|
diff = newx - x;
|
|
x = newx;
|
|
} while (diff < -1 || diff > 1);
|
|
/* If we see a diff of 1 or less we know all future diffs will be smaller, and the rate of
|
|
* convergence means the *sum* of the diffs will be less than 1.
|
|
|
|
* In most cases, our result here will be ceil(x).
|
|
*/
|
|
const ccycle = Math.ceil(x);
|
|
if (ccycle - x > 0.999999) {
|
|
// Rounding-error path: It's possible that we slightly overshot the integer value due to
|
|
// rounding error, and more specifically precision issues with log and the size difference of
|
|
// startMoney vs. x. See if a smaller integer works. Most of the time, x was not close enough
|
|
// that we need to try.
|
|
const fcycle = ccycle - 1;
|
|
if (targetMoney <= (startMoney + fcycle) * Math.exp(k * fcycle)) {
|
|
return fcycle;
|
|
}
|
|
}
|
|
if (ccycle >= x + ((diff <= 0 ? -diff : diff) + 0.000001)) {
|
|
// Fast-path: We know the true value is somewhere in the range [x, x + |diff|] but the next
|
|
// greatest integer is past this. Since we have to round up grows anyway, we can return this
|
|
// with no more calculation. We need some slop due to rounding errors - we can't fast-path
|
|
// a value that is too small.
|
|
return ccycle;
|
|
}
|
|
if (targetMoney <= (startMoney + ccycle) * Math.exp(k * ccycle)) {
|
|
return ccycle;
|
|
}
|
|
return ccycle + 1;
|
|
}
|