-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculations.js
112 lines (105 loc) · 2.88 KB
/
calculations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/** @param {NS} ns **/
import { gainRootAccess } from "gainRoot.js";
export function calculateMostProfitableTarget(ns, targets) {
targets
.filter((server) => {
if (server == "home") {
// Skip home server.
return false;
}
if (ns.getPurchasedServers().includes(server)) {
// Skip purchased servers.
return false;
}
return true;
})
.map((server) => {
if (!ns.hasRootAccess(server)) {
gainRootAccess(ns, server);
}
});
const candidates = targets
.filter((server) => {
if (server == "home") {
// Skip home server.
return false;
}
if (ns.getPurchasedServers().includes(server)) {
// Skip purchased servers.
return false;
}
return true;
})
.filter((server) => {
return ns.hasRootAccess(server);
})
.filter((server) => {
return ns.getServerRequiredHackingLevel(server) <= ns.getHackingLevel();
});
const serverMoneyMaxes = candidates.map((target) =>
ns.getServerMaxMoney(target)
);
const bestIndex = serverMoneyMaxes.reduce(
(bestIndexSoFar, currentlyTestedValue, currentlyTestedIndex, array) =>
currentlyTestedValue > array[bestIndexSoFar]
? currentlyTestedIndex
: bestIndexSoFar,
0
);
return candidates[bestIndex];
}
export function calculateHackThreads(ns, target) {
return ns.hackAnalyzeThreads(target, ns.getServerMoneyAvailable(target) / 2);
}
export function calculateSecondIncrease(ns, hack_threads, grow_threads) {
return (
ns.hackAnalyzeSecurity(hack_threads) +
ns.growthAnalyzeSecurity(grow_threads)
);
}
export function calculateWeakenThreads(ns, second_increase) {
let weakenThreads = 1;
const factor = 1.1;
while (ns.weakenAnalyze(weakenThreads) < second_increase * factor) {
weakenThreads++;
}
return weakenThreads;
}
export function calculateRequiredRam(
ns,
hackThreads,
growThreads,
weakenThreads
) {
const server = "home";
return (
hackThreads * ns.getScriptRam("hackTarget.js", server) +
growThreads * ns.getScriptRam("growTarget.js", server) +
weakenThreads * ns.getScriptRam("weakenTarget.js", server)
);
}
export function calculateInitialGrowth(ns, target) {
const factor = 0.5;
return (
(factor * ns.getServerMaxMoney(target)) / ns.getServerMoneyAvailable(target)
);
}
export function calculateInitialWeaken(ns, target, growth_rate) {
let weakenRate = 1;
while (
ns.weakenAnalyze(weakenRate) <
ns.getServerSecurityLevel(target) +
ns.growthAnalyzeSecurity(growth_rate) -
ns.getServerMinSecurityLevel(target)
) {
weakenRate++;
}
return weakenRate;
}
export function calculateStagingRam(ns, growth_rate, weaken_rate) {
const server = "home";
return (
ns.getScriptRam("growTarget.js", server) * growth_rate +
ns.getScriptRam("weakenTarget.js", server) * weaken_rate
);
}