import { clampNumber, isValidNumber } from "@/utils/utils"; import { Player as IPerson, Server as IServer } from "@ns"; import { ServerConstants } from "./constants"; import { currentNodeMults } from "./exports"; import { Player } from "./player"; export const hacking = { growAmount: calculateGrowMoney, growPercent: calculateServerGrowth, growThreads, growTime: calculateGrowTime, hackChance: calculateHackingChance, hackExp: calculateHackingExpGain, hackPercent: calculatePercentMoneyHacked, hackTime: calculateHackingTime, weakenTime: calculateWeakenTime, numCycleForGrowthCorrected, calculateServerGrowthLog, numCycleForGrowth, getWeakenEffect, }; 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 */ 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 */ 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) */ 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 */ 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 */ 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 */ 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. 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; } 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. function calculateGrowMoney(server: IServer, p: IPerson, threads: number, 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 */ function numCycleForGrowth(server: IServer, growth: number, cores = 1): number { if (!server.serverGrowth) return Infinity; return Math.log(growth) / calculateServerGrowthLog(server, 1, Player, cores); } /** * Wrapper of the function `numCycleForGrowthCorrected`. * * Calculate how many threads it will take to grow server to targetMoney. Starting money is server.moneyAvailable. * Note that when simulating the effect of {@link NS.grow | grow}, what matters is the state of the server and player * when the grow *finishes*, not when it is started. * * The growth amount depends both linearly *and* exponentially on threads; see {@link NS.grow | grow} for more details. * * The inverse of this function is {@link HackingFormulas.growAmount | formulas.hacking.growAmount}, * although it can work with fractional threads. * @param server - Server info, typically from {@link NS.getServer | getServer} * @param player - Player info, typically from {@link NS.getPlayer | getPlayer} * @param targetMoney - Desired final money, capped to server's moneyMax * @param cores - Number of cores on the computer that will execute grow. * @returns The calculated grow threads as an integer, rounded up. */ function growThreads(server: IServer, player: IPerson, targetMoney: number, cores = 1): number { return numCycleForGrowthCorrected(server, player, targetMoney, server.moneyAvailable ?? 0, 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. Usually this will just be the server's current money `server.available`, but it is a parameter to allow for more flexible use. * @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. */ function numCycleForGrowthCorrected( server: IServer, person: IPerson = Player, targetMoney: number, startMoney: number, cores = 1, ): 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; } function getCoreBonus(cores = 1): number { const coreBonus = 1 + (cores - 1) / 16; return coreBonus; } function getWeakenEffect(threads: number, cores: number): number { const coreBonus = getCoreBonus(cores); return ServerConstants.ServerWeakenAmount * threads * coreBonus * currentNodeMults.ServerWeakenRate; }