BUGFIX: clamp Hackchance to prevent infinity / infinity (#1349)

This commit is contained in:
Caldwell 2024-06-05 03:19:41 +02:00 committed by GitHub
parent 18ae6ce215
commit c42d4143c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -2,6 +2,7 @@ import { currentNodeMults } from "./BitNode/BitNodeMultipliers";
import { Person as IPerson } from "@nsdefs"; import { Person as IPerson } from "@nsdefs";
import { calculateIntelligenceBonus } from "./PersonObjects/formulas/intelligence"; import { calculateIntelligenceBonus } from "./PersonObjects/formulas/intelligence";
import { Server as IServer } from "@nsdefs"; import { Server as IServer } from "@nsdefs";
import { clampNumber } from "./utils/helpers/clampNumber";
/** Returns the chance the person has to successfully hack a server */ /** Returns the chance the person has to successfully hack a server */
export function calculateHackingChance(server: IServer, person: IPerson): number { export function calculateHackingChance(server: IServer, person: IPerson): number {
@ -11,14 +12,14 @@ export function calculateHackingChance(server: IServer, person: IPerson): number
if (!server.hasAdminRights || hackDifficulty >= 100) return 0; if (!server.hasAdminRights || hackDifficulty >= 100) return 0;
const hackFactor = 1.75; const hackFactor = 1.75;
const difficultyMult = (100 - hackDifficulty) / 100; const difficultyMult = (100 - hackDifficulty) / 100;
const skillMult = hackFactor * person.skills.hacking; const skillMult = clampNumber(hackFactor * person.skills.hacking, 1);
const skillChance = (skillMult - requiredHackingSkill) / skillMult; const skillChance = (skillMult - requiredHackingSkill) / skillMult;
const chance = const chance =
skillChance * skillChance *
difficultyMult * difficultyMult *
person.mults.hacking_chance * person.mults.hacking_chance *
calculateIntelligenceBonus(person.skills.intelligence, 1); calculateIntelligenceBonus(person.skills.intelligence, 1);
return Math.min(1, Math.max(chance, 0)); return clampNumber(chance, 0, 1);
} }
/** /**