From 849bcb2601e082bd36ee12ecd40fc5a2dcdd6d85 Mon Sep 17 00:00:00 2001
From: catloversg <152669316+catloversg@users.noreply.github.com>
Date: Sun, 11 May 2025 09:14:15 +0700
Subject: [PATCH] MISC: Add formulas API for calculating share power and move
UI of sharing RAM (#2126)
---
markdown/bitburner.ns.getsharepower.md | 2 +-
markdown/bitburner.ns.md | 4 +--
markdown/bitburner.ns.share.md | 2 +-
markdown/bitburner.reputationformulas.md | 1 +
...bitburner.reputationformulas.sharepower.md | 27 +++++++++++++++++++
src/Faction/ui/FactionRoot.tsx | 2 --
src/Faction/ui/FactionsRoot.tsx | 7 +++++
src/Faction/ui/ShareOption.tsx | 7 ++---
src/Netscript/RamCostGenerator.ts | 1 +
src/NetscriptFunctions/Formulas.ts | 9 +++++++
src/NetworkShare/Share.ts | 22 +++++++--------
src/ScriptEditor/NetscriptDefinitions.d.ts | 16 ++++++++---
12 files changed, 77 insertions(+), 23 deletions(-)
create mode 100644 markdown/bitburner.reputationformulas.sharepower.md
diff --git a/markdown/bitburner.ns.getsharepower.md b/markdown/bitburner.ns.getsharepower.md
index 6be43f8bd..375505574 100644
--- a/markdown/bitburner.ns.getsharepower.md
+++ b/markdown/bitburner.ns.getsharepower.md
@@ -4,7 +4,7 @@
## NS.getSharePower() method
-Share Power has a multiplicative effect on rep/second while doing work for a faction. Share Power increases incrementally for every thread of share running on your server network, but at a sharply decreasing rate.
+Share power has a multiplicative effect on rep/second while doing work for a faction. Share power increases incrementally for every thread of share running on your server network, but at a sharply decreasing rate.
**Signature:**
diff --git a/markdown/bitburner.ns.md b/markdown/bitburner.ns.md
index 5dbb2a900..314d621bd 100644
--- a/markdown/bitburner.ns.md
+++ b/markdown/bitburner.ns.md
@@ -112,7 +112,7 @@ export async function main(ns) {
| [getServerRequiredHackingLevel(host)](./bitburner.ns.getserverrequiredhackinglevel.md) | Returns the required hacking level of the target server. |
| [getServerSecurityLevel(host)](./bitburner.ns.getserversecuritylevel.md) | Get server security level. |
| [getServerUsedRam(host)](./bitburner.ns.getserverusedram.md) | Get the used RAM on a server. |
-| [getSharePower()](./bitburner.ns.getsharepower.md) | Share Power has a multiplicative effect on rep/second while doing work for a faction. Share Power increases incrementally for every thread of share running on your server network, but at a sharply decreasing rate. |
+| [getSharePower()](./bitburner.ns.getsharepower.md) | Share power has a multiplicative effect on rep/second while doing work for a faction. Share power increases incrementally for every thread of share running on your server network, but at a sharply decreasing rate. |
| [getTimeSinceLastAug()](./bitburner.ns.gettimesincelastaug.md) | Returns the amount of time in milliseconds that have passed since you last installed Augmentations. |
| [getTotalScriptExpGain()](./bitburner.ns.gettotalscriptexpgain.md) | Get the exp gain of all scripts. |
| [getTotalScriptIncome()](./bitburner.ns.gettotalscriptincome.md) | Get the income of all scripts. |
@@ -161,7 +161,7 @@ export async function main(ns) {
| [self()](./bitburner.ns.self.md) | Returns the currently running script. |
| [serverExists(host)](./bitburner.ns.serverexists.md) | Returns a boolean denoting whether or not the specified server exists. |
| [setTitle(title, pid)](./bitburner.ns.settitle.md) | Set the title of the tail window of a script. This function is deprecated and will be removed in a later version. |
-| [share()](./bitburner.ns.share.md) | Share the server's ram with your factions. |
+| [share()](./bitburner.ns.share.md) | Share the server's ram with your factions to increase the reputation gain rate of faction work. This boost is applied to all faction work of all factions. |
| [sleep(millis)](./bitburner.ns.sleep.md) | Suspends the script for n milliseconds. |
| [spawn(script, threadOrOptions, args)](./bitburner.ns.spawn.md) | Terminate current script and start another in a defined number of milliseconds. |
| [sprintf(format, args)](./bitburner.ns.sprintf.md) | Format a string. |
diff --git a/markdown/bitburner.ns.share.md b/markdown/bitburner.ns.share.md
index 6e0b7e20f..e2fa4fb48 100644
--- a/markdown/bitburner.ns.share.md
+++ b/markdown/bitburner.ns.share.md
@@ -4,7 +4,7 @@
## NS.share() method
-Share the server's ram with your factions.
+Share the server's ram with your factions to increase the reputation gain rate of faction work. This boost is applied to all faction work of all factions.
**Signature:**
diff --git a/markdown/bitburner.reputationformulas.md b/markdown/bitburner.reputationformulas.md
index c147e27c2..c4bfc5253 100644
--- a/markdown/bitburner.reputationformulas.md
+++ b/markdown/bitburner.reputationformulas.md
@@ -20,4 +20,5 @@ interface ReputationFormulas
| [calculateRepToFavor(rep)](./bitburner.reputationformulas.calculatereptofavor.md) | Calculate the resulting faction favor of a total amount of reputation. (Faction favor is gained whenever you install an Augmentation.) |
| [donationForRep(reputation, player)](./bitburner.reputationformulas.donationforrep.md) | Calculate the donation needed to gain an amount of reputation. |
| [repFromDonation(amount, player)](./bitburner.reputationformulas.repfromdonation.md) | Calculate how much rep would be gained. |
+| [sharePower(threads, cpuCores)](./bitburner.reputationformulas.sharepower.md) | Calculate the share power if you call [ns.share](./bitburner.ns.share.md) with the specified number of threads on a server having the specified number of CPU cores. |
diff --git a/markdown/bitburner.reputationformulas.sharepower.md b/markdown/bitburner.reputationformulas.sharepower.md
new file mode 100644
index 000000000..708e1f576
--- /dev/null
+++ b/markdown/bitburner.reputationformulas.sharepower.md
@@ -0,0 +1,27 @@
+
+
+[Home](./index.md) > [bitburner](./bitburner.md) > [ReputationFormulas](./bitburner.reputationformulas.md) > [sharePower](./bitburner.reputationformulas.sharepower.md)
+
+## ReputationFormulas.sharePower() method
+
+Calculate the share power if you call [ns.share](./bitburner.ns.share.md) with the specified number of threads on a server having the specified number of CPU cores.
+
+**Signature:**
+
+```typescript
+sharePower(threads: number, cpuCores?: number): number;
+```
+
+## Parameters
+
+| Parameter | Type | Description |
+| --- | --- | --- |
+| threads | number | Number of threads. Must be a positive integer. |
+| cpuCores | number | _(Optional)_ Number of CPU cores. Must be a positive integer. The default value is 1. |
+
+**Returns:**
+
+number
+
+The calculated share power.
+
diff --git a/src/Faction/ui/FactionRoot.tsx b/src/Faction/ui/FactionRoot.tsx
index 643224cb1..63b671fb9 100644
--- a/src/Faction/ui/FactionRoot.tsx
+++ b/src/Faction/ui/FactionRoot.tsx
@@ -22,7 +22,6 @@ import { GangButton } from "./GangButton";
import { FactionWork } from "../../Work/FactionWork";
import { useCycleRerender } from "../../ui/React/hooks";
import { repNeededToDonate } from "../formulas/donation";
-import { ShareOption } from "./ShareOption";
type FactionRootProps = {
faction: Faction;
@@ -132,7 +131,6 @@ function MainPage({ faction, rerender, onAugmentations }: IMainProps): React.Rea
{!isPlayersGang && factionInfo.offersWork() && (
)}
- {!isPlayersGang && factionInfo.offersWork() && }
{canPurchaseSleeves && (
<>
diff --git a/src/Faction/ui/FactionsRoot.tsx b/src/Faction/ui/FactionsRoot.tsx
index 0c7942bd8..5e7eb5c01 100644
--- a/src/Faction/ui/FactionsRoot.tsx
+++ b/src/Faction/ui/FactionsRoot.tsx
@@ -16,6 +16,7 @@ import { Requirement } from "../../ui/Components/Requirement";
import { Faction } from "../Faction";
import { getFactionAugmentationsFiltered, joinFaction } from "../FactionHelpers";
import { Factions } from "../Factions";
+import { ShareOption } from "./ShareOption";
export const InvitationsSeen = new Set();
@@ -290,6 +291,12 @@ export function FactionsRoot(): React.ReactElement {
+
+
+ Share RAM
+
+
+
{rumoredFactions.length > 0 && (
<>
diff --git a/src/Faction/ui/ShareOption.tsx b/src/Faction/ui/ShareOption.tsx
index 934d44f95..e713feaca 100644
--- a/src/Faction/ui/ShareOption.tsx
+++ b/src/Faction/ui/ShareOption.tsx
@@ -55,9 +55,10 @@ export function ShareOption({ rerender }: { rerender: () => void }): React.React
return (
- You can share free RAM of your home computer with your faction to get a bonus multiplier for reputation gain.
- Each time you share your free RAM, you get a boost for {ShareBonusTime / 1000} seconds. You can share free RAM
- of other servers that you have admin rights by using ns.share() API.
+ You can share free RAM of your home computer with your factions to get a bonus multiplier for reputation gain.
+ Each time you share your free RAM, you get a boost for {ShareBonusTime / 1000} seconds. After that, you lose the
+ boost and get back your shared RAM. You can share free RAM of other servers that you have admin rights on by
+ using the ns.share() API.
Free RAM on home computer: {formatRam(home.maxRam - home.ramUsed)}.
diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts
index 236d87a79..092056bb1 100644
--- a/src/Netscript/RamCostGenerator.ts
+++ b/src/Netscript/RamCostGenerator.ts
@@ -641,6 +641,7 @@ export const RamCosts: RamCostTree = {
calculateRepToFavor: 0,
repFromDonation: 0,
donationForRep: 0,
+ sharePower: 0,
},
skills: {
calculateSkill: 0,
diff --git a/src/NetscriptFunctions/Formulas.ts b/src/NetscriptFunctions/Formulas.ts
index 716b96f96..2c894e650 100644
--- a/src/NetscriptFunctions/Formulas.ts
+++ b/src/NetscriptFunctions/Formulas.ts
@@ -53,6 +53,7 @@ import { CompanyPositions } from "../Company/CompanyPositions";
import { findCrime } from "../Crime/CrimeHelpers";
import { Skills } from "../Bladeburner/data/Skills";
import type { PositiveNumber } from "../types";
+import { calculateEffectiveSharedThreads, calculateShareBonus } from "../NetworkShare/Share";
export function NetscriptFormulas(): InternalAPI {
const checkFormulasAccess = function (ctx: NetscriptContext): void {
@@ -134,6 +135,14 @@ export function NetscriptFormulas(): InternalAPI {
checkFormulasAccess(ctx);
return donationForRep(reputation, person);
},
+ sharePower:
+ (ctx) =>
+ (_threads, _cpuCores = 1) => {
+ const threads = helpers.positiveInteger(ctx, "threads", _threads);
+ const cpuCores = helpers.positiveInteger(ctx, "cpuCores", _cpuCores);
+ checkFormulasAccess(ctx);
+ return calculateShareBonus(calculateEffectiveSharedThreads(threads, cpuCores));
+ },
},
skills: {
calculateSkill:
diff --git a/src/NetworkShare/Share.ts b/src/NetworkShare/Share.ts
index 01d206076..806c7cc11 100644
--- a/src/NetworkShare/Share.ts
+++ b/src/NetworkShare/Share.ts
@@ -3,7 +3,7 @@ import { calculateIntelligenceBonus } from "../PersonObjects/formulas/intelligen
import { getCoreBonus } from "../Server/ServerHelpers";
import { clampNumber } from "../utils/helpers/clampNumber";
-let sharePower = 1;
+let shareThreads = 1;
export const ShareBonusTime = 10000;
@@ -26,22 +26,22 @@ export function calculateEffectiveSharedThreads(threads: number, cpuCores: numbe
export function startSharing(threads: number, cpuCores: number): () => void {
const effectiveThreads = calculateEffectiveSharedThreads(threads, cpuCores);
- sharePower += effectiveThreads;
+ shareThreads += effectiveThreads;
return () => {
/**
- * Due to floating point inaccuracy, sharePower may be slightly higher or lower than 1 after many times the player
+ * Due to floating point inaccuracy, shareThreads may be slightly higher or lower than 1 after many times the player
* shares their RAM. We need to make sure that it's not smaller than 1.
*/
- sharePower = clampNumber(sharePower - effectiveThreads, 1);
- // sharePower may be slightly higher than 1. Reset sharePower if it's smaller than a threshold.
- if (sharePower < 1.00001) {
- sharePower = 1;
+ shareThreads = clampNumber(shareThreads - effectiveThreads, 1);
+ // shareThreads may be slightly higher than 1. Reset shareThreads if it's smaller than a threshold.
+ if (shareThreads < 1.00001) {
+ shareThreads = 1;
}
};
}
-function calculateShareBonus(sharePower: number): number {
- const bonus = 1 + Math.log(sharePower) / 25;
+export function calculateShareBonus(shareThreads: number): number {
+ const bonus = 1 + Math.log(shareThreads) / 25;
if (!Number.isFinite(bonus)) {
return 1;
}
@@ -49,9 +49,9 @@ function calculateShareBonus(sharePower: number): number {
}
export function calculateShareBonusWithAdditionalThreads(threads: number, cpuCores: number): number {
- return calculateShareBonus(sharePower + calculateEffectiveSharedThreads(threads, cpuCores));
+ return calculateShareBonus(shareThreads + calculateEffectiveSharedThreads(threads, cpuCores));
}
export function calculateCurrentShareBonus(): number {
- return calculateShareBonus(sharePower);
+ return calculateShareBonus(shareThreads);
}
diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts
index fbc6a5eae..37284f6c9 100644
--- a/src/ScriptEditor/NetscriptDefinitions.d.ts
+++ b/src/ScriptEditor/NetscriptDefinitions.d.ts
@@ -5277,6 +5277,15 @@ interface ReputationFormulas {
* @param player - Player info, typically from {@link NS.getPlayer | getPlayer}
*/
donationForRep(reputation: number, player: Person): number;
+
+ /**
+ * Calculate the share power if you call {@link NS.share | ns.share} with the specified number of threads on a server
+ * having the specified number of CPU cores.
+ * @param threads - Number of threads. Must be a positive integer.
+ * @param cpuCores - Number of CPU cores. Must be a positive integer. The default value is 1.
+ * @returns The calculated share power.
+ */
+ sharePower(threads: number, cpuCores?: number): number;
}
/**
@@ -8355,7 +8364,8 @@ export interface NS {
flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg | string[] };
/**
- * Share the server's ram with your factions.
+ * Share the server's ram with your factions to increase the reputation gain rate of faction work. This boost is
+ * applied to all faction work of all factions.
* @remarks
* RAM cost: 2.4 GB
*
@@ -8365,8 +8375,8 @@ export interface NS {
share(): Promise;
/**
- * Share Power has a multiplicative effect on rep/second while doing work for a faction.
- * Share Power increases incrementally for every thread of share running on your server network, but at a sharply decreasing rate.
+ * Share power has a multiplicative effect on rep/second while doing work for a faction.
+ * Share power increases incrementally for every thread of share running on your server network, but at a sharply decreasing rate.
* @remarks
* RAM cost: 0.2 GB
*/