2019-03-03 04:08:54 +01:00
|
|
|
.. _netscript_hacknetnodeapi:
|
2018-08-12 03:05:32 +02:00
|
|
|
|
2018-01-09 21:48:06 +01:00
|
|
|
Netscript Hacknet Node API
|
|
|
|
==========================
|
|
|
|
|
|
|
|
Netscript provides the following API for accessing and upgrading your Hacknet Nodes
|
|
|
|
through scripts.
|
|
|
|
|
|
|
|
Note that none of these functions will write to the script's logs. If you want
|
|
|
|
to see what your script is doing you will have to print to the logs yourself.
|
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
**Hacknet Node API functions must be accessed through the hacknet namespace**
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
In Netscript 1.0::
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
hacknet.purchaseNode();
|
|
|
|
hacknet.getNodeStats(3).level;
|
2018-05-06 09:53:34 +02:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
In :ref:`netscriptjs`::
|
2018-05-06 09:53:34 +02:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
ns.hacknet.purchaseNode();
|
|
|
|
ns.hacknet.getNodeStats(3).level;
|
2018-05-06 09:53:34 +02:00
|
|
|
|
2019-03-03 04:08:54 +01:00
|
|
|
.. toctree::
|
|
|
|
:caption: API Functions:
|
|
|
|
|
|
|
|
numNodes() <hacknetnodeapi/numNodes>
|
|
|
|
purchaseNode() <hacknetnodeapi/purchaseNode>
|
|
|
|
getPurchaseNodeCost() <hacknetnodeapi/getPurchaseNodeCost>
|
|
|
|
getNodeStats() <hacknetnodeapi/getNodeStats>
|
|
|
|
upgradeLevel() <hacknetnodeapi/upgradeLevel>
|
|
|
|
upgradeRam() <hacknetnodeapi/upgradeRam>
|
|
|
|
upgradeCore() <hacknetnodeapi/upgradeCore>
|
2019-03-31 04:53:57 +02:00
|
|
|
upgradeCache() <hacknetnodeapi/upgradeCache>
|
2019-03-03 04:08:54 +01:00
|
|
|
getLevelUpgradeCost() <hacknetnodeapi/getLevelUpgradeCost>
|
|
|
|
getRamUpgradeCost() <hacknetnodeapi/getRamUpgradeCost>
|
|
|
|
getCoreUpgradeCost() <hacknetnodeapi/getCoreUpgradeCost>
|
2019-03-31 04:53:57 +02:00
|
|
|
getCacheUpgradeCost() <hacknetnodeapi/getCacheUpgradeCost>
|
2019-03-03 04:08:54 +01:00
|
|
|
|
|
|
|
.. _netscript_hacknetnodeapi_referencingahacknetnode:
|
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
Referencing a Hacknet Node
|
|
|
|
--------------------------
|
|
|
|
Most of the functions in the Hacknet Node API perform an operation on a single
|
|
|
|
Node. Therefore, a numeric index is used to identify and specify which Hacknet
|
|
|
|
Node a function should act on. This index number corresponds to the number
|
|
|
|
at the end of the name of the Hacknet Node. For example, the first Hacknet Node you
|
|
|
|
purchase will have the name "hacknet-node-0" and is referenced using index 0.
|
|
|
|
The fifth Hacknet Node you purchase will have the name "hacknet-node-4" and is
|
|
|
|
referenced using index 4.
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2019-01-03 09:39:32 +01:00
|
|
|
RAM Cost
|
|
|
|
--------
|
|
|
|
Accessing the `hacknet` namespace incurs a one time cost of 4 GB of RAM.
|
|
|
|
In other words, using multiple Hacknet Node API functions in a script will not cost
|
2019-03-03 04:08:54 +01:00
|
|
|
more than 4 GB of RAM.
|
2018-09-26 07:00:28 +02:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
Utilities
|
|
|
|
---------
|
|
|
|
The following functions are not officially part of the Hacknet Node API, but they
|
|
|
|
can be useful when writing Hacknet Node-related scripts. Since they are not part
|
|
|
|
of the API, they do not need to be accessed using the *hacknet* namespace.
|
2018-05-06 20:22:29 +02:00
|
|
|
|
2019-03-03 04:08:54 +01:00
|
|
|
* :js:func:`getHacknetMultipliers`
|
2018-05-06 20:22:29 +02:00
|
|
|
|
2018-01-09 21:48:06 +01:00
|
|
|
Example(s)
|
2019-03-03 04:08:54 +01:00
|
|
|
----------
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
The following is an example of one way a script can be used to automate the
|
|
|
|
purchasing and upgrading of Hacknet Nodes.
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
This script attempts to purchase Hacknet Nodes until the player has a total of 8. Then
|
2019-03-31 04:53:57 +02:00
|
|
|
it gradually upgrades those Node's to a minimum of level 140, 64 GB RAM, and 8 cores
|
|
|
|
|
|
|
|
.. code:: javascript
|
2018-01-09 21:48:06 +01:00
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
function myMoney() {
|
2019-03-31 04:53:57 +02:00
|
|
|
return getServerMoneyAvailable("home");
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:33:22 +02:00
|
|
|
disableLog("getServerMoneyAvailable");
|
|
|
|
disableLog("sleep");
|
|
|
|
|
2019-03-31 04:53:57 +02:00
|
|
|
var cnt = 8;
|
2018-07-18 22:33:22 +02:00
|
|
|
|
|
|
|
while(hacknet.numNodes() < cnt) {
|
|
|
|
res = hacknet.purchaseNode();
|
|
|
|
print("Purchased hacknet Node with index " + res);
|
|
|
|
};
|
|
|
|
|
2019-03-31 04:53:57 +02:00
|
|
|
for (var i = 0; i < cnt; i++) {
|
2018-07-18 22:33:22 +02:00
|
|
|
while (hacknet.getNodeStats(i).level <= 80) {
|
|
|
|
var cost = hacknet.getLevelUpgradeCost(i, 10);
|
|
|
|
while (myMoney() < cost) {
|
|
|
|
print("Need $" + cost + " . Have $" + myMoney());
|
|
|
|
sleep(3000);
|
|
|
|
}
|
|
|
|
res = hacknet.upgradeLevel(i, 10);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-03-31 04:53:57 +02:00
|
|
|
print("All nodes upgraded to level 80");
|
2018-07-18 22:33:22 +02:00
|
|
|
|
2019-03-31 04:53:57 +02:00
|
|
|
for (var i = 0; i < cnt; i++) {
|
2018-07-18 22:33:22 +02:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-03-31 04:53:57 +02:00
|
|
|
print("All nodes upgraded to 16GB RAM");
|