Merge branch 'dev' into patch-1

This commit is contained in:
Mughur
2023-01-06 15:31:19 +02:00
committed by GitHub
79 changed files with 541 additions and 369 deletions

View File

@@ -14,4 +14,4 @@ brutessh() Netscript Function
.. code-block:: javascript
brutessh("foodnstuff");
ns.brutessh("foodnstuff");

View File

@@ -12,3 +12,10 @@ deleteServer() Netscript Function
The ``hostname`` argument can be any data type, but it will be converted to
a string. Whitespace is automatically removed from the string. This function
will not delete a server that still has scripts running on it.
Examples:
.. code-block:: javascript
ns.killall("dummyServer");
ns.deleteServer("dummyServer"); //returns: true if purhcased server 'dummyServer'existed

View File

@@ -24,17 +24,17 @@ exec() Netscript Function
The simplest way to use the :doc:`exec<exec>` command is to call it with
just the script name and the target server. The following example will try
to run ``generic-hack.script`` on the ``foodnstuff`` server::
to run ``generic-hack.js`` on the ``foodnstuff`` server::
exec("generic-hack.script", "foodnstuff");
ns.exec("generic-hack.js", "foodnstuff");
The following example will try to run the script ``generic-hack.script`` on
The following example will try to run the script ``generic-hack.js`` on
the ``joesguns`` server with 10 threads::
exec("generic-hack.script", "joesguns", 10);
ns.exec("generic-hack.js", "joesguns", 10);
This last example will try to run the script ``foo.script`` on the
This last example will try to run the script ``foo.js`` on the
``foodnstuff`` server with 5 threads. It will also pass the number 1 and the
string "test" in as arguments to the script::
exec("foo.script", "foodnstuff", 5, 1, "test");
ns.exec("foo.js", "foodnstuff", 5, 1, "test");

View File

@@ -22,8 +22,8 @@ fileExists() Netscript Function
.. code-block:: javascript
fileExists("foo.script", "foodnstuff"); // returns: false
fileExists("ftpcrack.exe"); // returns: true
ns.fileExists("foo.js", "foodnstuff"); // returns: false
ns.fileExists("ftpcrack.exe"); // returns: true
The first example above will return true if the script named ``foo.script`` exists on the ``foodnstuff`` server, and false otherwise.
The first example above will return true if the script named ``foo.js`` exists on the ``foodnstuff`` server, and false otherwise.
The second example above will return true if the current server contains the ``FTPCrack.exe`` program, and false otherwise.

View File

@@ -14,4 +14,4 @@ ftpcrack() Netscript Function
.. code-block:: javascript
ftpcrack("foodnstuff");
ns.ftpcrack("foodnstuff");

View File

@@ -10,4 +10,4 @@ getHackingLevel() Netscript Function
.. code-block:: javascript
getHackingLevel(); // returns: 124
ns.getHackingLevel(); // returns: 124

View File

@@ -21,6 +21,6 @@ getHackingMultipliers() Netscript Function
.. code-block:: javascript
mults = getHackingMultipliers();
print(mults.chance);
print(mults.growth);
const mults = ns.getHackingMultipliers();
ns.print(mults.chance);
ns.print(mults.growth);

View File

@@ -22,6 +22,6 @@ getHacknetMultipliers() Netscript Function
.. code-block:: javascript
mults = getHacknetMultipliers();
print(mults.production);
print(mults.purchaseCost);
const mults = ns.getHacknetMultipliers();
ns.print(mults.production);
ns.print(mults.purchaseCost);

View File

@@ -5,11 +5,13 @@ getPurchasedServerCost() Netscript Function
:RAM cost: 0.25 GB
:param number ram: Amount of RAM of a potential purchased server. Must be a power of 2 (2, 4, 8, 16, etc.). Maximum value of 1048576 (2^20)
:param number ram: Amount of RAM of a potential purchased server. Must be a power of 2 (2, 4, 8, 16, etc.). Maximum value of :doc:`getPurchasedServerMaxRam<getPurchasedServerMaxRam>`
:returns: Cost to purchase a server with the specified amount of ``ram``.
Giving any non-power-of-2 as an argument results in the function returning `Infinity`
Example:
.. code-block:: javascript
getPurchasedServerCost(8192); // returns: 450560000
ns.getPurchasedServerCost(8192); // returns: 450560000

View File

@@ -10,4 +10,4 @@ getPurchasedServerLimit() Netscript Function
.. code-block:: javascript
getPurchasedServerLimit() // returns: 25
ns.getPurchasedServerLimit() // returns: 25

View File

@@ -10,4 +10,4 @@ getPurchasedServerMaxRam() Netscript Function
.. code-block:: javascript
getPurchasedServerMaxRam(); // returns: 1048576
ns.getPurchasedServerMaxRam(); // returns: 1048576

View File

@@ -0,0 +1,18 @@
getPurchasedServerUpgradeCost() Netscript Function
===========================================
.. js:function:: getPurchasedServerUpgradeCost(hostname, ram)
:RAM cost: 0.25 GB
:param string hostname: Hostname of target purchased server.
:param number ram: Target amount of RAM for purchased server. Must be a power of 2 (2, 4, 8, 16, etc.). Maximum value of :doc:`getPurchasedServerMaxRam<getPurchasedServerMaxRam>`
:returns: Cost to purchase a server with the specified amount of ``ram``.
Giving any non-power-of-2 as an argument results in the function returning `-1`
Example:
.. code-block:: javascript
ns.purchaseServer("smallServer",2) //costs 110000
ns.getPurchasedServerUpgradeCost("smallServer",8); // returns: 330000

View File

@@ -10,4 +10,4 @@ getPurchasedServers() Netscript Function
.. code-block:: javascript
getPurchasedServers(); // returns: ['grow-server-0', 'grow-server-1', 'weaken-server-0']
ns.getPurchasedServers(); // returns: ['grow-server-0', 'grow-server-1', 'weaken-server-0']

View File

@@ -13,4 +13,4 @@ getScriptRam() Netscript Function
.. code-block:: javascript
getScriptRam("grow.script"); // returns: 1.75
ns.getScriptRam("grow.js"); // returns: 1.75

View File

@@ -11,4 +11,4 @@ getServerMaxMoney() Netscript Function
.. code-block:: javascript
getServerMaxMoney('foodnstuff'); // returns: 50000000
ns.getServerMaxMoney('foodnstuff'); // returns: 50000000

View File

@@ -11,5 +11,5 @@ getServerMaxRam() Netscript Function
.. code-block:: javascript
maxRam = getServerMaxRam("helios"); // returns: 16
print("helios has "+maxRam + "GB");
const maxRam = ns.getServerMaxRam("helios"); // returns: 16
ns.print("helios has "+maxRam + "GB");

View File

@@ -11,4 +11,4 @@ getServerMinSecurityLevel() Netscript Function
.. code-block:: javascript
getServerMinSecurityLevel('foodnstuff'); // returns: 3
ns.getServerMinSecurityLevel('foodnstuff'); // returns: 3

View File

@@ -15,5 +15,5 @@ getServerMoneyAvailable() Netscript Function
.. code-block:: javascript
getServerMoneyAvailable("foodnstuff"); // returns: 120000
getServerMoneyAvailable("home"); // returns: 1000
ns.getServerMoneyAvailable("foodnstuff"); // returns: 120000
ns.getServerMoneyAvailable("home"); // returns: 1000

View File

@@ -12,4 +12,4 @@ getServerNumPortsRequired() Netscript Function
.. code-block:: javascript
getServerNumPortsRequired("unitalife"); // returns: 4
ns.getServerNumPortsRequired("unitalife"); // returns: 4

View File

@@ -11,4 +11,4 @@ getServerRequiredHackingLevel() Netscript Function
.. code-block:: javascript
getServerRequiredHackingLevel("foodnstuff"); // returns: 5
ns.getServerRequiredHackingLevel("foodnstuff"); // returns: 5

View File

@@ -11,4 +11,4 @@ getServerSecurityLevel() Netscript Function
.. code-block:: javascript
getServerSecurityLevel("foodnstuff"); // returns: 3.45
ns.getServerSecurityLevel("foodnstuff"); // returns: 3.45

View File

@@ -11,5 +11,5 @@ getServerUsedRam() Netscript Function
.. code-block:: javascript
usedRam = getServerUsedRam("harakiri-sushi"); // returns: 5.6
print("harakiri-sushi uses "+usedRam + "GB");
const usedRam = ns.getServerUsedRam("harakiri-sushi"); // returns: 5.6
ns.print("harakiri-sushi uses "+ usedRam + "GB"); // prints: "harakiri-sushi uses 5.6GB"

View File

@@ -17,8 +17,8 @@ grow() Netscript Function
Increase the amount of money available on a server. The time it takes to
execute depends on your hacking level and the target server's security
level. When :doc:`grow<grow>` completes, the money available on a target
server will be increased by a certain, fixed percentage. This percentage is
determined by the target server's growth rate (which varies between servers)
server will be increased by the number of threads used and a certain, fixed percentage.
The percentage is determined by the target server's growth rate (which varies between servers)
and security level. Generally, higher-level servers have higher growth
rates.
@@ -34,5 +34,5 @@ grow() Netscript Function
.. code-block:: javascript
while(true) {
grow("foodnstuff");
await ns.grow("foodnstuff");
}

View File

@@ -33,6 +33,6 @@ hack() Netscript Function
.. code-block:: javascript
hack("foodnstuff");
hack("10.1.2.3");
hack("foodnstuff", { threads: 5 }); // Only use 5 threads to hack
await ns.hack("foodnstuff");
await ns.hack("10.1.2.3");
await ns.hack("foodnstuff", { threads: 5 }); // Only use 5 threads to hack

View File

@@ -11,6 +11,12 @@ hasRootAccess() Netscript Function
.. code-block:: javascript
if (hasRootAccess("foodnstuff") == false) {
nuke("foodnstuff");
if (ns.hasRootAccess("foodnstuff") == false) {
ns.nuke("foodnstuff");
}
.. code-block:: javascript
if (ns.hasRootAccess("foodnstuff")) {
ns.exec("foo.js", 1, "foodnstuff");
}

View File

@@ -13,4 +13,4 @@ httpworm() Netscript Function
.. code-block:: javascript
httpworm("foodnstuff");
ns.httpworm("foodnstuff");

View File

@@ -11,4 +11,4 @@ isLogEnabled() Netscript Function
.. code-block:: javascript
isLogEnabled('hack'); // returns: true
ns.isLogEnabled('hack'); // returns: true

View File

@@ -16,20 +16,20 @@ isRunning() Netscript Function
**Examples:**
In this first example below, the function call will return true if there is
a script named ``foo.script`` with no arguments running on the
a script named ``foo.js`` with no arguments running on the
``foodnstuff`` server, and false otherwise:
.. code-block:: javascript
isRunning("foo.script", "foodnstuff");
ns.isRunning("foo.js", "foodnstuff");
In this second example below, the function call will return true if there is
a script named ``foo.script`` with no arguments running on the current
a script named ``foo.js`` with no arguments running on the current
server, and false otherwise:
.. code-block:: javascript
isRunning("foo.script", getHostname());
ns.isRunning("foo.js", ns.getHostname());
In this next example below, the function call will return true if there is a
script named ``foo.script`` running with the arguments 1, 5, and "test" (in
@@ -37,7 +37,7 @@ isRunning() Netscript Function
.. code-block:: javascript
isRunning("foo.script", "joesguns", 1, 5, "test");
ns.isRunning("foo.js", "joesguns", 1, 5, "test");
.. js:function:: isRunning(scriptPid)
@@ -51,4 +51,4 @@ isRunning() Netscript Function
.. code-block:: javascript
isRunning(39);
ns.isRunning(39);

View File

@@ -1,42 +1,42 @@
kill() Netscript Function
=========================
.. js:function:: kill(script, hostname, [args...])
.. js:function:: kill(script, [hostname=current hostname, [args...]])
:RAM cost: 0.5 GB
:param string script: Filename of the script to kill.
:param string hostname: Hostname of the server on which to kill the script.
:param string hostname: Hostname of the server on which to kill the script.
:param args...: Arguments to identify which script to kill.
:returns: ``true`` is that script was killed.
Kills the script on the target server specified by the script's name and
arguments. Remember that scripts are uniquely identified by both their name
and arguments. For example, if ``foo.script`` is run with the argument 1,
then this is not the same as ``foo.script`` run with the argument 2, even
and arguments. For example, if ``foo.js`` is run with the argument 1,
then this is not the same as ``foo.js`` run with the argument 2, even
though they have the same code.
Examples:
The following example will try to kill a script named ``foo.script`` on the
The following example will try to kill a script named ``foo.js`` on the
``foodnstuff`` server that was ran with no arguments:
.. code-block:: javascript
kill("foo.script", "foodnstuff");
ns.kill("foo.js", "foodnstuff");
The following will try to kill a script named ``foo.script`` on the current
The following will try to kill a script named ``foo.js`` on the current
server that was ran with no arguments:
.. code-block:: javascript
kill("foo.script", getHostname());
ns.kill("foo.js");
The following will try to kill a script named ``foo.script`` on the current
The following will try to kill a script named ``foo.js`` on the current
server that was ran with the arguments 1 and "foodnstuff":
.. code-block:: javascript
kill("foo.script", getHostname(), 1, "foodnstuff");
ns.kill("foo.js", ns.getHostname(), 1, "foodnstuff");
.. js:function:: kill(scriptPid)
@@ -53,6 +53,6 @@ kill() Netscript Function
.. code-block:: javascript
if (kill(10)) {
print("Killed script with PID 10!");
if (ns.kill(10)) {
ns.print("Killed script with PID 10!");
}

View File

@@ -1,10 +1,11 @@
killall() Netscript Function
============================
.. js:function:: killall(hostname)
.. js:function:: killall([hostname = current hostname,[safetyguard = true]])
:RAM cost: 0.5 GB
:param string hostname: Hostname of the server on which to kill all scripts.
:param boolean safetyguard: Whether the function will safeguard the current script or not.
:returns: ``true`` if scripts were killed on target server.
Kills all running scripts on the specified server.
@@ -14,4 +15,10 @@ killall() Netscript Function
.. code-block:: javascript
killall('foodnstuff'); // returns: true
ns.killall('foodnstuff'); // returns: true
.. code-block:: javascript
ns.killall(); // returns: true, kills all scripts on the current server, except the current script
ns.killall(); // returns: false, because all no available scripts are running anymore
ns.killall(ns.getHostname(),false) // returns: true, but also kills the current script

View File

@@ -12,4 +12,6 @@ ls() Netscript Function
.. code-block:: javascript
ls("home"); // returns: ["demo.script", "msg1.txt"]
ns.ls("home"); // returns: ["demo.js", "msg1.txt"]
ns.ls("home", ".txt"); // returns: ["msg1.txt"]
ns.ls("home", ".script"); // returns: []

View File

@@ -7,11 +7,12 @@ nuke() Netscript Function
:param string hostname: Hostname of the target server.
Runs the ``NUKE.exe`` program on the target server. ``NUKE.exe`` must exist
on your home computer.
on your home computer. Requires the targeted server to have enough ports opened,
otherwise will throw an error.
Example:
.. code-block:: javascript
nuke("foodnstuff");
ns.nuke("foodnstuff");

View File

@@ -12,5 +12,7 @@ print() Netscript Function
.. code-block:: javascript
print("Hello world!"); // Prints "Hello world!" in the logs.
print({a:5}); // Prints '{"a":5}' in the logs.
ns.print("Hello world!"); // Prints "Hello world!" in the logs.
ns.print({a:5}); // Prints '{"a":5}' in the logs.
const text = "can"
ns.print("I "+ text +" use variables :)") // Prints "I can use variables :)"

View File

@@ -23,9 +23,9 @@ ps() Netscript Function
.. code-block:: javascript
processes = ps("home");
for (let i = 0; i < processes.length; ++i) {
tprint(processes[i].filename + ' ' + processes[i].threads);
tprint(processes[i].args);
tprint(processes[i].pid);
const processes = ns.ps("home");
for (const i = 0; i < processes.length; ++i) {
ns.tprint(processes[i].filename + ' ' + processes[i].threads);
ns.tprint(processes[i].args);
ns.tprint(processes[i].pid);
}

View File

@@ -9,7 +9,7 @@ purchaseServer() Netscript Function
2. Maximum value of :doc:`getPurchasedServerMaxRam<getPurchasedServerMaxRam>`
:returns: The hostname of the newly purchased server. Empty string on failure.
Purchased a server with the specified hostname and amount of RAM.
Purchases a server with the specified hostname and amount of RAM.
The ``hostname`` argument can be any data type, but it will be converted to
a string and have whitespace removed. Anything that resolves to an empty
@@ -27,8 +27,8 @@ purchaseServer() Netscript Function
.. code-block:: javascript
ram = 64;
hn = "pserv-";
for (i = 0; i < 5; ++i) {
purchaseServer(hn + i, ram);
const ram = 64;
const name = "pserv-";
for (const i = 0; i < 5; ++i) {
ns.purchaseServer(name + i, ram);
}

View File

@@ -13,4 +13,4 @@ relaysmtp() Netscript Function
.. code-block:: javascript
relaysmtp("foodnstuff");
ns.relaysmtp("foodnstuff");

View File

@@ -21,23 +21,23 @@ run() Netscript Function
less will cause a runtime error.
The simplest way to use the :doc:`run<run>` command is to call it with just
the script name. The following example will run ``foo.script``
the script name. The following example will run ``foo.js``
single-threaded with no arguments:
.. code-block:: javascript
run("foo.script");
ns.run("foo.js");
The following example will run 'foo.script' but with 5 threads instead of
The following example will run 'foo.js' but with 5 threads instead of
single-threaded:
.. code-block:: javascript
run("foo.script", 5);
ns.run("foo.js", 5);
This next example will run ``foo.script`` single-threaded, and will pass the
This next example will run ``foo.js`` single-threaded, and will pass the
string ``foodnstuff`` into the script as an argument:
.. code-block:: javascript
run("foo.script", 1, 'foodnstuff');
ns.run("foo.sj", 1, 'foodnstuff');

View File

@@ -12,4 +12,4 @@ scan() Netscript Function
.. code-block:: javascript
scan("home"); // returns: ["foodnstuff", "sigma-cosmetics", "joesguns", "hong-fang-tea", "harakiri-sushi", "iron-gym"]
ns.scan("home"); // returns: ["foodnstuff", "sigma-cosmetics", "joesguns", "hong-fang-tea", "harakiri-sushi", "iron-gym"]

View File

@@ -23,11 +23,11 @@ scp() Netscript Function
.. code-block:: javascript
//Copies "hack-template.script" from the current server to "foodnstuff"
scp("hack-template.script", "foodnstuff"); // returns: true
ns.scp("hack-template.script", "foodnstuff"); // returns: true
//Copies "foo.lit" from the helios server to the "home" computer
scp("foo.lit", "home", "helios"); // returns: true
ns.scp("foo.lit", "home", "helios"); // returns: true
//Tries to copy three files from "rothman-uni" to "home" computer
files = ["foo1.lit", "foo2.script", "foo3.script"];
scp(files, "home", "rothman-uni"); // returns: true
const files = ["foo1.lit", "foo2.script", "foo3.script"];
ns.scp(files, "home", "rothman-uni"); // returns: true

View File

@@ -15,4 +15,4 @@ scriptKill() Netscript Function
.. code-block:: javascript
scriptKill("demo.script", "home"); // returns: true
ns.scriptKill("demo.js", "home"); // returns: true

View File

@@ -16,15 +16,15 @@ scriptRunning() Netscript Function
Examples:
The example below will return true if there is any script named
``foo.script`` running on the ``foodnstuff`` server, and false otherwise:
``foo.js`` running on the ``foodnstuff`` server, and false otherwise:
.. code-block:: javascript
scriptRunning("foo.script", "foodnstuff");
ns.scriptRunning("foo.js", "foodnstuff");
The example below will return true if there is any script named
``foo.script`` running on the current server, and false otherwise:
``foo.js`` running on the current server, and false otherwise:
.. code-block:: javascript
scriptRunning("foo.script", getHostname());
ns.scriptRunning("foo.js", ns.getHostname());

View File

@@ -11,4 +11,4 @@ serverExists() Netscript Function
.. code-block:: javascript
serverExists("foodnstuff"); // returns: true
ns.serverExists("foodnstuff"); // returns: true

View File

@@ -12,4 +12,4 @@ sleep() Netscript Function
.. code-block:: javascript
sleep(3000); // Will wait 3 seconds.
await ns.sleep(3000); // Will wait 3 seconds.

View File

@@ -23,4 +23,4 @@ spawn() Netscript Function
.. code-block:: javascript
spawn('foo.script', 10, 'foodnstuff', 90); // "run foo.script foodnstuff 90 -t 10" in 10 seconds.
ns.spawn('foo.js', 10, 'foodnstuff', 90); // "run foo.js foodnstuff 90 -t 10" in 10 seconds.

View File

@@ -13,4 +13,4 @@ sqlinject() Netscript Function
.. code-block:: javascript
sqlinject("foodnstuff");
ns.sqlinject("foodnstuff");

View File

@@ -12,5 +12,5 @@ tprint() Netscript Function
.. code-block:: javascript
tprint("Hello world!"); // Prints "Hello world!" to the terminal.
tprint({a:5}); // Prints '{"a":5}' to the terminal.
ns.tprint("Hello world!"); // Prints "Hello world!" to the terminal.
ns.tprint({a:5}); // Prints '{"a":5}' to the terminal.

View File

@@ -0,0 +1,27 @@
upgradePurchasedServer() Netscript Function
===================================
.. js:function:: upgradePurchasedServer(hostname, ram)
:RAM cost: 0.25 GB
:param string hostname: Hostname of the purchased server.
:param number ram: Amount of RAM of the purchased server. Must be a power of
2. Maximum value of :doc:`getPurchasedServerMaxRam<getPurchasedServerMaxRam>`
:returns: ``true`` if the upgrade succeeded, ``false`` otherwise
Upgrades the purchased server with the specified hostname to have specified amount of RAM.
The ``hostname`` argument can be any data type, but it will be converted to
a string and have whitespace removed. New RAM amount has to be higher than the current RAM
and a power of 2. Upgrading a server costs the difference of old RAM server cost and new RAM
server cost.
Example:
.. code-block:: javascript
const ram = 64;
const name = "pserv-";
for (const i = 0; i < 5; ++i) {
ns.upgradePurchasedServer(name + i, ram);
}

View File

@@ -27,5 +27,5 @@ weaken() Netscript Function
.. code-block:: javascript
weaken("foodnstuff");
weaken("foodnstuff", { threads: 5 }); // Only use 5 threads to weaken
await ns.weaken("foodnstuff");
await ns.weaken("foodnstuff", { threads: 5 }); // Only use 5 threads to weaken

View File

@@ -2,6 +2,10 @@
Netscript 1.0
=============
.. note:: Note that NS1/.script is being deprecated, avoid using and migrate
scripts to NS2/.js if possible.
Netscript 1.0 is implemented using a modified version of Neil Fraser's
`JS-Interpreter <https://github.com/NeilFraser/JS-Interpreter>`_.

View File

@@ -52,9 +52,11 @@ This includes information such as function signatures, what they do, and their r
getPurchasedServerCost() <basicfunctions/getPurchasedServerCost>
purchaseServer() <basicfunctions/purchaseServer>
deleteServer() <basicfunctions/deleteServer>
upgradePurchasedServer() <basicfunctions/upgradePurchasedServer>
getPurchasedServers() <basicfunctions/getPurchasedServers>
getPurchasedServerLimit() <basicfunctions/getPurchasedServerLimit>
getPurchasedServerMaxRam() <basicfunctions/getPurchasedServerMaxRam>
getPurchasedServerUpgradeCost() <basicfunctions/getPurchasedServerUpgradeCost>
scriptRunning() <basicfunctions/scriptRunning>
scriptKill() <basicfunctions/scriptKill>
getScriptRam() <basicfunctions/getScriptRam>

View File

@@ -72,59 +72,64 @@ The following is an example of one way a script can be used to automate the
purchasing and upgrading of Hacknet Nodes.
This script attempts to purchase Hacknet Nodes until the player has a total of 8. Then
it gradually upgrades those Node's to a minimum of level 80, 16 GB RAM, and 8 cores
it gradually upgrades those Node's to level 80, 16 GB RAM, and 8 cores
.. code:: javascript
function myMoney() {
return getServerMoneyAvailable("home");
}
disableLog("getServerMoneyAvailable");
disableLog("sleep");
var cnt = 8;
while(hacknet.numNodes() < cnt) {
res = hacknet.purchaseNode();
print("Purchased hacknet Node with index " + res);
};
for (var i = 0; i < cnt; i++) {
while (hacknet.getNodeStats(i).level <= 80) {
var cost = hacknet.getLevelUpgradeCost(i, 10);
while (myMoney() < cost) {
print("Need $" + cost + " . Have $" + myMoney());
sleep(3000);
export async function main(ns) {
function myMoney() {
return ns.getServerMoneyAvailable("home");
}
res = hacknet.upgradeLevel(i, 10);
};
};
print("All nodes upgraded to level 80");
ns.disableLog("getServerMoneyAvailable");
ns.disableLog("sleep");
for (var i = 0; i < cnt; i++) {
while (hacknet.getNodeStats(i).ram < 16) {
var cost = hacknet.getRamUpgradeCost(i, 2);
while (myMoney() < cost) {
print("Need $" + cost + " . Have $" + myMoney());
sleep(3000);
}
res = hacknet.upgradeRam(i, 2);
};
};
const cnt = 8;
print("All nodes upgraded to 16GB RAM");
while (ns.hacknet.numNodes() < cnt) {
res = ns.hacknet.purchaseNode();
if (res != -1) ns.print("Purchased hacknet Node with index " + res);
await ns.sleep(1000);
};
for (var i = 0; i < cnt; i++) {
while (hacknet.getNodeStats(i).cores < 8) {
var cost = hacknet.getCoreUpgradeCost(i, 1);
while (myMoney() < cost) {
print("Need $" + cost + " . Have $" + myMoney());
sleep(3000);
}
res = hacknet.upgradeCore(i, 1);
};
};
ns.tprint("All " + cnt + " nodes purchased")
print("All nodes upgraded to 8 cores");
for (const i = 0; i < cnt; i++) {
while (ns.hacknet.getNodeStats(i).level <= 80) {
var cost = ns.hacknet.getLevelUpgradeCost(i, 1);
while (myMoney() < cost) {
ns.print("Need $" + cost + " . Have $" + myMoney());
await ns.sleep(3000);
}
res = ns.hacknet.upgradeLevel(i, 1);
};
};
ns.tprint("All nodes upgraded to level 80");
for (var i = 0; i < cnt; i++) {
while (ns.hacknet.getNodeStats(i).ram < 16) {
var cost = ns.hacknet.getRamUpgradeCost(i, 1);
while (myMoney() < cost) {
ns.print("Need $" + cost + " . Have $" + myMoney());
await ns.sleep(3000);
}
res = ns.hacknet.upgradeRam(i, 1);
};
};
ns.tprint("All nodes upgraded to 16GB RAM");
for (var i = 0; i < cnt; i++) {
while (ns.hacknet.getNodeStats(i).cores < 8) {
var cost = ns.hacknet.getCoreUpgradeCost(i, 1);
while (myMoney() < cost) {
ns.print("Need $" + cost + " . Have $" + myMoney());
await ns.sleep(3000);
}
res = ns.hacknet.upgradeCore(i, 1);
};
};
ns.tprint("All nodes upgraded to 8 cores");
}

View File

@@ -36,8 +36,12 @@ Now assume we ran the following simple script
.. code-block:: js
for (i = 0; i < 10; ++i) {
writePort(1, i); //Writes the value of i to port 1
.. code:: javascript
export async function main(ns) {
for (const i = 0; i < 10; ++i) {
ns.writePort(1, i); //Writes the value of i to port 1
}
}
After this script executes, our script will contain every number from 0 through 9, as so::
@@ -48,8 +52,12 @@ Then, assume we run the following script
.. code-block:: js
for (i = 0; i < 3; ++i) {
print(readPort(1)); //Reads a value from port 1 and then prints it
.. code:: javascript
export async function main(ns) {
for (const i = 0; i < 3; ++i) {
ns.print(ns.readPort(1)); //Reads a value from port 1 and then prints it
}
}
This script above will read the first three values from port 1 and then print them to the script's log. The log will end up looking like::
@@ -119,22 +127,25 @@ Port Handle Example
.. code-block:: js
port = getPortHandle(5);
back = port.data.pop(); //Get and remove last element in port
.. code:: javascript
//Wait for port data before reading
while(port.empty()) {
sleep(10000);
export async function main(ns) {
port = ns.getPortHandle(5);
back = port.data.pop(); //Get and remove last element in port
//Wait for port data before reading
while(port.empty()) {
await ns.sleep(10000);
}
res = port.read();
//Wait for there to be room in a port before writing
while (!port.tryWrite(5)) {
await ns.sleep(5000);
}
//Successfully wrote to port!
}
res = port.read();
//Wait for there to be room in a port before writing
while (!port.tryWrite(5)) {
sleep(5000);
}
//Successfully wrote to port!
Comments
--------
@@ -145,7 +156,7 @@ Comments are not evaluated as code, and can be used to document and/or explain c
/* Multi
* line
* comment */
print("This code will actually get executed");
ns.print("This code will actually get executed");
.. _netscriptimporting:
@@ -159,41 +170,48 @@ There are two ways of doing this::
import * as namespace from "script filename"; //Import all functions from script
import {fn1, fn2, ...} from "script filename"; //Import specific functions from script
Suppose you have a library script called *testlibrary.script*
Suppose you have a library script called *testlibrary.js*::
.. code-block:: js
function foo1(args) {
.. code:: javascript
export function foo1(args) {
//function definition...
}
function foo2(args) {
export function foo2(args) {
//function definition...
}
function foo3(args) {
export async function foo3(args) {
//function definition...
}
function foo4(args) {
export function foo4(args) {
//function definition...
}
Then, if you wanted to use these functions in another script, you can import them like so
export async function main(ns) {
//main function definition, can be empty but must exist...
}
.. code-block:: js
Then, if you wanted to use these functions in another script, you can import them like so::
import * as testlib from "testlibrary.script";
.. code:: javascript
values = [1,2,3];
import * as testlib from "testlibrary.js";
//The imported functions must be specified using the namespace
someVal1 = testlib.foo3(values);
someVal2 = testlib.foo1(values);
if (someVal1 > someVal2) {
//...
} else {
//...
export async function main(ns) {
const values = [1,2,3];
//The imported functions must be specified using the namespace
const someVal1 = await testlib.foo3(...values); //'...' separates the array into separate values
const someVal2 = testlib.foo1(values[0]);
if (someVal1 > someVal2) {
//...
} else {
//...
}
}
If you only wanted to import certain functions, you can do so without needing
@@ -201,21 +219,24 @@ to specify a namespace for the import
.. code-block:: js
import {foo1, foo3} from "testlibrary.script"; //Saves RAM since not all functions are imported!
.. code:: javascript
values = [1,2,3];
import {foo1, foo3} from "testlibrary.js"; //Saves RAM since not all functions are imported!
//No namespace needed
someVal1 = foo3(values);
someVal2 = foo1(values);
if (someVal1 > someVal2) {
//...
} else {
//...
export async function main(ns) {
const values = [1,2,3];
//No namespace needed
const someVal1 = await foo3(...values);
const someVal2 = foo1(values[1]);
if (someVal1 > someVal2) {
//...
} else {
//...
}
}
.. warning:: For those who are experienced with JavaScript, note that the `export`
keyword should **NOT** be used in :ref:`netscript1`, as this will break the script.
.. warning:: Note that the `export` keyword can **NOT** be used in :ref:`netscript1` as it's not supported.
It can, however, be used in :ref:`netscriptjs` (but it's not required).
Standard, Built-In JavaScript Objects

View File

@@ -15,6 +15,8 @@ argument will be a number. This generic script will run the
script specified in the first argument with the amount of threads
specified in the second argument. The code would look like::
.. code:: javascript
run(args[0], args[1]);
And it could be ran from the terminal like:
@@ -23,9 +25,11 @@ And it could be ran from the terminal like:
In .js / ns2, the above script would look like::
export async function main(ns) {
ns.run(ns.args[0], ns.args[1]);
}
.. code:: javascript
export async function main(ns) {
ns.run(ns.args[0], ns.args[1]);
}
It is also possible to get the number of arguments that were passed
into a script using ``args.length``.