added action cap. improved scoring system. fixed parts of security and cash predictors. made lshw loopable.
[?]
Apr 12, 2020, 11:54 PM
VMXI7PS4GFDOWPFLHQ7TYKJ5K3K7FXNPJCBEVYHTTRN7YB5ZHEQACDependencies
- [2]
YANDOFZ4added readme and licenses - [3]
6MBUKAG6remove unneeded functions and comments. update readme. - [4]
3TYF4BJStarget only hackable servers. replace servers continually. - [5]
2BKHJI2Sinit
Change contents
- edit in sbin/weaken.js at line 3
// const start = Date.now();// const s = new Date(start); - edit in sbin/weaken.js at line 4
// const finish = Date.now();// const f = new Date(finish);// ns.tprint("\t#" + ns.args[3] + "\t" + ns.args[0] + " weakening " + ns.args[1] + "\nWeaken started:\t\t" + s.toISOString() + "\nWeaken finished:\t" + f.toISOString()); - edit in sbin/hack.js at line 3
// const start = Date.now();// const s = new Date(start); - edit in sbin/hack.js at line 4
// const finish = Date.now();// const f = new Date(finish);// ns.tprint("\t#" + ns.args[3] + "\t" + ns.args[0] + " hacking " + ns.args[1] + "\nHack started:\t\t" + s.toISOString() + "\nHack finished:\t\t" + f.toISOString()); - edit in sbin/grow.js at line 3
// const start = Date.now();// const s = new Date(start); - edit in sbin/grow.js at line 4
// const finish = Date.now();// const f = new Date(finish);// ns.tprint("\t#" + ns.args[3] + "\t" + ns.args[0] + " growing " + ns.args[1] + "\nGrow started:\t\t" + s.toISOString() + "\nGrow finished:\t\t" + f.toISOString()); - replacement in lib/lib.js at line 12
ns.tprint(`${new Date(Date.now()).toISOString()}: ${array_arguments[integer_indices_0].name}(`);ns.tprint(`${new Date().toISOString()}: ${array_arguments[integer_indices_0].name}(`); - replacement in lib/lib.js at line 168
// object_constants are from https://github.com/danielyxie/bitburner/blob/master/src/Constants.js// object_constants are from https://github.com/danielyxie/bitburner/blob/master/src/Constants.js - replacement in lib/lib.js at line 230
// name of current host (usually "home")// name of current host (usually "home") - replacement in lib/lib.js at line 355
if (// ram is at least equal to the minimum ram possible for purchased serversif (// you have no bought servers yet(ns.getPurchasedServers().length === 0) &&// ram is at least equal to the minimum ram possible for purchased servers - replacement in lib/lib.js at line 359
// ram is at least equal to the ram of "home", probably a bad idea to hardcode this since the string_name of "home" might change in the future. tags: warning, potential, problem(integer_server_ram_biggest_afforded >= integer_get_server_ram_total(ns, object_constants.string_host)) &&// you have no bought servers yet(ns.getPurchasedServers().length === 0)// ram is at least equal to the ram of "home" (probably a bad idea to hardcode this since the string_name of "home" might change in the future) or max ram.(integer_server_ram_biggest_afforded >= integer_get_server_ram_total(ns, "home") ||integer_server_ram_biggest_afforded >= object_constants.PurchasedServerMaxRam) - replacement in lib/lib.js at line 372
const integer_servers_bought_limit = object_get_constants(ns).PurchasedServerLimit;const object_constants = object_get_constants(ns); - replacement in lib/lib.js at line 377
(integer_servers_bought_amount < integer_servers_bought_limit) &&(integer_servers_bought_amount < object_constants.PurchasedServerLimit) && - replacement in lib/lib.js at line 379
(float_ram_server_bought_biggest != integer_servers_bought_limit) &&(integer_get_server_ram_biggest_afforded(ns) > float_ram_server_bought_biggest)(float_ram_server_bought_biggest != object_constants.PurchasedServerMaxRam) &&(float_ram_server_bought_biggest < integer_get_server_ram_biggest_afforded(ns)) - replacement in lib/lib.js at line 393
if (// you have one or more servers already(integer_servers_bought_amount > 0) &&// you currently own less than the maximum amount of purchased servers allowedif (// you currently own less than the maximum amount of purchased servers allowed - replacement in lib/lib.js at line 397
(integer_get_server_ram_biggest_afforded(ns) == float_ram_server_bought_maximum)(integer_get_server_ram_biggest_afforded(ns) >= float_ram_server_bought_maximum) - edit in lib/lib.js at line 461
ns.tprint(JSON.stringify(error)); - edit in lib/lib.js at line 476
ns.tprint(JSON.stringify(error)); - replacement in lib/lib.js at line 490
// returns the score of a server which is calculated by taking into account its max cash, growth, minimum security level and server difficulty// returns the score of a server which is calculated by taking into account its max cash, growth, and required hacking level. adapted from `calculatePercentMoneyHacked` in Hacking.js - replacement in lib/lib.js at line 493
return ns.getServerMaxMoney(string_server_target) * ns.getServerGrowth(string_server_target) * Math.pow(ns.getServerMinSecurityLevel(string_server_target), -1) * Math.pow(ns.getServerRequiredHackingLevel(string_server_target), -1);const float_player_hacking_level = ns.getHackingLevel();return ns.getServerMaxMoney(string_server_target) * ns.getServerGrowth(string_server_target) * ((float_player_hacking_level - (ns.getServerRequiredHackingLevel(string_server_target) - 1)) * Math.pow(float_player_hacking_level, -1)); - edit in lib/lib.js at line 601
};// returns the security increase from the growth threads used. Adapted from `processSingleServerGrowth` in ServerHelpers.ts and `fortify` in Server.tsconst float_get_security_increase_from_grow = function(ns, integer_threads_grow) {void_debug(float_get_security_increase_from_grow, Array.from(arguments));return 2 * object_get_constants(ns).ServerFortifyAmount * integer_threads_grow; - replacement in lib/lib.js at line 618
const skillMult = (object_constants.object_stats.hacking - (ns.getServerRequiredHackingLevel(string_server_target) - 1)) / object_constants.object_stats.hacking;let percentMoneyHacked = difficultyMult * skillMult * object_constants.object_hacking_multipliers.money / balanceFactor;if (percentMoneyHacked < 0) {return 0;}if (percentMoneyHacked > 1) {return 1;}const skillMult = (ns.getHackingLevel() - (ns.getServerRequiredHackingLevel(string_server_target) - 1)) / ns.getHackingLevel();const percentMoneyHacked = difficultyMult * skillMult * object_constants.object_hacking_multipliers.money / balanceFactor;if (percentMoneyHacked < 0) { return 0; }if (percentMoneyHacked > 1) { return 1; } - replacement in lib/lib.js at line 629
return float_percentage_to_steal / float_get_percentage_of_cash_from_available_per_hack(ns, string_server_target, float_server_target_security);return Math.ceil(float_percentage_to_steal / float_get_percentage_of_cash_from_available_per_hack(ns, string_server_target, float_server_target_security)); - edit in lib/lib.js at line 638
};// returns the security increase from the hack threads. adapted from `hack` in NetscriptFunctions.js and `fortify` in Server.tsconst float_get_security_increase_from_hack = function(ns, string_server_target, float_server_target_security, float_server_target_cash, integer_threads_hack) {void_debug(float_get_security_increase_from_hack, Array.from(arguments));let maxThreadNeeded = Math.ceil(1/float_get_percentage_of_cash_from_available_per_hack(ns, string_server_target, float_server_target_security)*(float_server_target_cash/ns.getServerMaxMoney(string_server_target)));if (isNaN(maxThreadNeeded)) {// Server has a 'max money' of 0 (probably). We'll set this to an arbitrarily large valuemaxThreadNeeded = 1e6;}return object_get_constants(ns).ServerFortifyAmount * Math.min(integer_threads_hack, maxThreadNeeded); - replacement in lib/lib.js at line 661
// returns true if there is enough ram to provide the threads required by grow to grow string_server_target's cash by float_percentage_to_steal if possible, otherwise, returns false. assumes security is at float_server_target_securityconst bool_is_ram_enough_for_growth_percentage = function(ns, float_server_used_ram_free, string_server_target, float_percentage_to_steal, float_server_target_security) {void_debug(bool_is_ram_enough_for_growth_percentage, Array.from(arguments));// returns true if there is enough ram to provide the threads required by weaken to weaken to minimum security, then by grow to grow string_server_target's cash by float_percentage_to_steal, then by weaken to weaken to minimum security again if possible, otherwise, returns false. assumes security is at float_server_target_securityconst bool_is_ram_enough_after_hack_percentage = function(ns, float_server_used_ram_free, string_server_target, float_server_target_cash_before, float_server_target_security, float_percentage_to_steal) {void_debug(bool_is_ram_enough_after_hack_percentage, Array.from(arguments)); - replacement in lib/lib.js at line 666
const integer_threads_required = integer_get_threads_required_for_cash_grow_after_percentage_stolen(ns, string_server_target, float_server_target_security, float_percentage_to_steal);const integer_threads_required = integer_get_threads_required_for_min_security_weaken(ns, string_server_target, float_get_security_increase_from_hack(ns, string_server_target, float_server_target_security, float_server_target_cash_before, integer_get_threads_hack(ns, float_server_used_ram_free, string_server_target, float_server_target_security, float_percentage_to_steal))) + integer_get_threads_required_for_cash_grow_after_percentage_stolen(ns, string_server_target, float_server_target_security, float_percentage_to_steal) + integer_get_threads_required_for_min_security_weaken(ns, string_server_target, float_get_security_increase_from_grow(ns, integer_get_threads_required_for_cash_grow_after_percentage_stolen(ns, string_server_target, float_server_target_security, float_percentage_to_steal))); - replacement in lib/lib.js at line 681[4.3768381]→[3.1741:1980](∅→∅),[3.1980]→[4.3768609:3768770](∅→∅),[4.3768609]→[4.3768609:3768770](∅→∅)
// this should return optimum percentage to steal such that cash stolen is as high as float_cap and string_server_target's cash still able to be grown to 100% after one grow with the ram it has remaining by using a binary search algorithmconst float_get_percentage_to_steal = function(ns, float_server_used_ram_free, string_server_target, float_server_target_security, float_precision, float_cap) {// this should return optimum percentage to steal such that cash stolen is as high as float_steal_cap and string_server_target's cash still able to be grown to 100% after one grow with the ram it has remaining by using a binary search algorithmconst float_get_percentage_to_steal = function(ns, float_server_used_ram_free, string_server_target, float_server_target_cash_before, float_server_target_security, float_precision, float_steal_cap) { - edit in lib/lib.js at line 684
// use half for the hack and half for the growth? would also need to take into account that ram for weaken is also needed?const float_server_used_ram_free_halved = float_server_used_ram_free * 0.5; - replacement in lib/lib.js at line 688
if (bool_is_ram_enough_for_growth_percentage(ns, float_server_used_ram_free_halved, string_server_target, float_percentage_to_steal, float_server_target_security)) {if (bool_is_ram_enough_after_hack_percentage(ns, float_server_used_ram_free, string_server_target, float_server_target_cash_before, float_server_target_security, float_percentage_to_steal)) { - replacement in lib/lib.js at line 697
if (float_percentage_to_steal > float_cap) {return float_cap;if (float_percentage_to_steal > float_steal_cap) {return float_steal_cap; - edit in lib/lib.js at line 736
// iterate through array in reverse - replacement in lib/lib.js at line 771
const float_get_time_hack = function(ns, string_server_target, float_server_target_security, hack, int) {const float_get_time_hack = function(ns, string_server_target, float_server_target_security) { - replacement in lib/lib.js at line 780
if (hack == null) {hack = object_constants.object_stats.hacking;}if (int == null) {int = object_constants.object_stats.intelligence;}const hack = object_constants.object_stats.hacking;const int = object_constants.object_stats.intelligence; - replacement in lib/lib.js at line 793
const float_get_time_grow = function(ns, string_server_target, float_server_target_security, hack, int) {const float_get_time_grow = function(ns, string_server_target, float_server_target_security) { - replacement in lib/lib.js at line 797
return growTimeMultiplier * float_get_time_hack(ns, string_server_target, float_server_target_security, hack, int);return growTimeMultiplier * float_get_time_hack(ns, string_server_target, float_server_target_security); - replacement in lib/lib.js at line 801
const float_get_time_weaken = function(ns, string_server_target, float_server_target_security, hack, int) {const float_get_time_weaken = function(ns, string_server_target, float_server_target_security) { - replacement in lib/lib.js at line 805
return weakenTimeMultiplier * float_get_time_hack(ns, string_server_target, float_server_target_security, hack, int);return weakenTimeMultiplier * float_get_time_hack(ns, string_server_target, float_server_target_security); - replacement in lib/lib.js at line 808
const array_make_schedule = function(ns, float_precision, float_cap, float_padding_seconds) {const array_make_schedule = function(ns, integer_action_cap, float_precision, float_steal_cap, float_padding_seconds) { - edit in lib/lib.js at line 824
let integer_array_schedule_length = 0; - replacement in lib/lib.js at line 829
for (let integer_indices_0 = 0; integer_indices_0 < array_servers_rooted_sorted_by_ram.length; ++integer_indices_0) {// iterate through servers in reverse to use servers with the biggest rams firstfor (let integer_indices_0 = array_servers_rooted_sorted_by_ram.length - 1; integer_indices_0 >= 0; --integer_indices_0) {if (integer_array_schedule_length >= integer_action_cap) {break;} - replacement in lib/lib.js at line 837
while (float_server_used_ram_free_current > 0) {while (float_server_used_ram_free_current > 0 &&integer_array_schedule_length < integer_action_cap) { - replacement in lib/lib.js at line 861
let float_server_target_security_uncorrected = float_server_target_security_current - (integer_threads_weaken * object_constants.ServerWeakenAmount);const float_server_target_security_uncorrected = float_server_target_security_current - (integer_threads_weaken * object_constants.ServerWeakenAmount); - edit in lib/lib.js at line 877
++integer_array_schedule_length; - replacement in lib/lib.js at line 892
let float_server_target_cash_current_uncorrected = float_server_target_cash_current * float_get_growth_from_threads(ns, string_server_target, float_server_target_security_current, integer_threads_grow);const float_server_target_cash_current_uncorrected = float_server_target_cash_current * float_get_growth_from_threads(ns, string_server_target, float_server_target_security_current, integer_threads_grow); - replacement in lib/lib.js at line 901
float_server_target_security_current += 2 * object_constants.ServerFortifyAmount * integer_threads_grow;float_server_target_security_current += float_get_security_increase_from_grow(ns, integer_threads_grow); - edit in lib/lib.js at line 911
++integer_array_schedule_length; - replacement in lib/lib.js at line 922
const integer_threads_hack = integer_get_threads_hack(ns, float_server_used_ram_free_current, string_server_target, float_server_target_security_current, float_get_percentage_to_steal (ns, float_server_used_ram_free_current, string_server_target, float_server_target_security_current, float_precision, float_cap));// this is supposed to be the section that uses the `float_get_percentage_to_steal` function to calculate and hack the optimum amount of cash, but it doesn't seem to work properly and I still haven't figured why or how to fix it, so for now I think it just hacks whatever `float_steal_cap` percent of cash the server has.//const integer_threads_hack = integer_get_threads_hack(ns, float_server_used_ram_free_current, string_server_target, float_server_target_security_current, float_get_percentage_to_steal(ns, float_server_used_ram_free_current, string_server_target, float_server_target_cash_current, float_server_target_security_current, float_precision, float_steal_cap)); - replacement in lib/lib.js at line 925
float_server_used_ram_free_current -= integer_threads_hack * object_scripts.hack.ram;const integer_threads_hack = integer_get_threads_hack(ns, float_server_used_ram_free_current, string_server_target, float_server_target_security_current, float_steal_cap); - replacement in lib/lib.js at line 927
float_server_target_cash_current -= float_server_target_cash_current * float_get_percentage_of_cash_from_available_per_hack(ns, string_server_target, float_server_target_security_current);float_server_used_ram_free_current -= integer_threads_hack * object_scripts.hack.ram; - replacement in lib/lib.js at line 930
let maxThreadNeeded = Math.ceil(1/float_get_percentage_of_cash_from_available_per_hack(ns, string_server_target, float_server_target_security_current)*(float_server_target_cash_current/float_server_target_cash_maximum));if (isNaN(maxThreadNeeded)) {// Server has a 'max money' of 0 (probably). We'll set this to an arbitrarily large valuemaxThreadNeeded = 1e6;const float_server_target_cash_before = float_server_target_cash_current;const float_server_target_cash_current_uncorrected = float_server_target_cash_current - Math.floor(float_server_target_cash_current * float_get_percentage_of_cash_from_available_per_hack(ns, string_server_target, float_server_target_security_current)) * integer_threads_hack;if (float_server_target_cash_current_uncorrected < 0) {float_server_target_cash_current = 0; - replacement in lib/lib.js at line 936
float_server_target_security_current += object_constants.ServerFortifyAmount * Math.min(integer_threads_hack, maxThreadNeeded);else {float_server_target_cash_current = float_server_target_cash_current_uncorrected;}float_server_target_security_current += float_get_security_increase_from_hack(ns, string_server_target, float_server_target_security_current, float_server_target_cash_before, integer_threads_hack); - edit in lib/lib.js at line 950
++integer_array_schedule_length; - edit in lib/lib.js at line 956
return array_schedule; - replacement in lib/lib.js at line 983
export const void_schedule_runner = async function(ns, string_servers_bought_name, float_precision, float_cap, float_padding_seconds) {export const void_schedule_runner = async function(ns, string_servers_bought_name, integer_action_cap, float_precision, float_steal_cap, float_padding_seconds) { - replacement in lib/lib.js at line 998
const array_schedule = array_make_schedule(ns, float_precision, float_cap, float_padding_seconds);const array_schedule = array_make_schedule(ns, integer_action_cap, float_precision, float_steal_cap, float_padding_seconds); - replacement in bin/main.js at line 7
// variables// name of purchased servers// variables// name of purchased servers - replacement in bin/main.js at line 10
// duration between each action// maximum amount of actions to spawn per cycle, used to prevent using up too much IRL RAMconst integer_action_cap = 200;// duration between each action - replacement in bin/main.js at line 14
// precision of the percentage to steal calculator// precision of the percentage to steal calculator - replacement in bin/main.js at line 16[4.3794060]→[4.3794060:3794130](∅→∅),[4.3794130]→[3.2828:2854](∅→∅),[3.2854]→[4.3794156:3794209](∅→∅),[4.3794156]→[4.3794156:3794209](∅→∅)
// the maximum percentage of cash that should be stolen from a serverconst float_cap = 0.98;// time period used for checking the time in seconds// the maximum percentage of cash that should be stolen from a serverconst float_steal_cap = 0.98;// time period used for checking the time in seconds - replacement in bin/main.js at line 21
// main loop// main loop - replacement in bin/main.js at line 27
integer_time_finishes = await void_schedule_runner(ns, string_servers_bought_name, float_precision, float_cap, float_padding_seconds) + Date.now();integer_time_finishes = await void_schedule_runner(ns, string_servers_bought_name, integer_action_cap, float_precision, float_steal_cap, float_padding_seconds) + Date.now(); - edit in bin/lshw.js at line 1
const void_print_information = function (ns, string_server) {const float_cash_max = ns.getServerMaxMoney(string_server);const float_cash_current = ns.getServerMoneyAvailable(string_server);const float_security_minimum = ns.getServerMinSecurityLevel(string_server);const float_security_current = Math.round(ns.getServerSecurityLevel(string_server));const array_ram = ns.getServerRam(string_server);const float_ram_total = array_ram[0];const float_ram_used = array_ram[1];const float_ram_free = array_ram[0] - array_ram[1];const string_server_information =// comment out unneeded info`Time: ${new Date().toISOString()}Name: ${string_server}Root access: ${ns.hasRootAccess(string_server)}Maximum cash ($): ${float_cash_max}Current cash ($): ${float_cash_current.toFixed(2)}Current cash (%): ${(float_cash_current * 100 / float_cash_max).toFixed(2)}Minimum security: ${float_security_minimum}Current security: ${float_security_current}Current security (x): ${(float_security_current / float_security_minimum).toFixed(2)}Growth rate: ${ns.getServerGrowth(string_server)}Hacking level needed: ${ns.getServerRequiredHackingLevel(string_server)}Ports needed for root: ${ns.getServerNumPortsRequired(string_server)}RAM total (GB): ${float_ram_total}RAM used (GB): ${float_ram_used.toFixed(2)}RAM used (%): ${(float_ram_used * 100 / float_ram_total).toFixed(2)}RAM free (GB): ${float_ram_free.toFixed(2)}RAM free (%): ${(float_ram_free * 100 / float_ram_total).toFixed(2)}`;ns.tprint(string_server_information);}; - replacement in bin/lshw.js at line 35
for (let indices_0 = 0; indices_0 < ns.args.length; ++indices_0) {const float_cash_max = ns.getServerMaxMoney(ns.args[indices_0]);const float_cash_current = ns.getServerMoneyAvailable(ns.args[indices_0]);const float_security_minimum = ns.getServerMinSecurityLevel(ns.args[indices_0]);const float_security_current = Math.round(ns.getServerSecurityLevel(ns.args[indices_0]));const array_ram = ns.getServerRam(ns.args[indices_0]);const float_ram_total = array_ram[0];const float_ram_used = array_ram[1];const float_ram_free = array_ram[0] - array_ram[1];const data = [// comment out unneeded info"Name:\t\t\t" + ns.args[indices_0],"Root access:\t\t" + ns.hasRootAccess(ns.args[indices_0]),"Maximum cash ($):\t" + float_cash_max,"Current cash ($):\t" + float_cash_current.toFixed(2),"Current cash (%):\t" + (float_cash_current * 100 / float_cash_max).toFixed(2),"Growth:\t\t" + ns.getServerGrowth(ns.args[indices_0]),"Minimum security:\t" + float_security_minimum,"Current security:\t" + float_security_current,"Current security (x):\t" + (float_security_current / float_security_minimum).toFixed(2),"Hacking level needed:\t" + ns.getServerRequiredHackingLevel(ns.args[indices_0]),"Ports needed for root:\t" + ns.getServerNumPortsRequired(ns.args[indices_0]),"RAM total (Gb):\t" + float_ram_total,"RAM used (Gb):\t\t" + float_ram_used.toFixed(2),"RAM used (%):\t\t" + (float_ram_used * 100 / float_ram_total).toFixed(2),"RAM free (Gb):\t\t" + float_ram_free.toFixed(2),"RAM free (%):\t\t" + (float_ram_free * 100 / float_ram_total).toFixed(2),"\n"];for (let indices_0 = 0; indices_0 < data.length; ++indices_0) {ns.tprint(data[indices_0]);}let array_arguments = ns.args;let float_sleep_duration = 0;while (true) {for (let integer_indices_0 = 0; integer_indices_0 < array_arguments.length; ++integer_indices_0) {const string_argument = array_arguments[integer_indices_0];if ((string_argument === "--sleep") ||(string_argument === "-s")) {float_sleep_duration = array_arguments[integer_indices_0 + 1];array_arguments.splice(integer_indices_0, 2);}try {void_print_information(ns, string_argument);}catch (error) {ns.tprint(JSON.stringify(error));}}if (float_sleep_duration < 1) {break;}else {await ns.sleep(float_sleep_duration);} - replacement in README.md at line 20
### `lshw.js <"SERVER1" ...>` (2.45 GB)### `lshw.js [-s INTERVAL] <SERVER1 ...>` (2.45 GB) - edit in README.md at line 23
* Optionally, display the information at regular intervals. The INTERVAL parameter accepts a value >= 1 in milliseconds. - edit in README.md at line 31
For scripts that don't take arguments: - replacement in README.md at line 35
orFor scripts that take arguments: - replacement in README.md at line 38
run lshw.js "home" "foodnstuff"run lshw.js -s 1000 home foodnstuff - edit in README.md at line 40
The above will cause the terminal to output up-to-date information about the "home" "foodnstuff" servers every second.