more work on bn13

This commit is contained in:
Olivier Gagnon
2021-11-13 22:44:17 -05:00
990 changed files with 58453 additions and 9515 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

View File

@@ -10,7 +10,7 @@ getPlayer() Netscript Function
Returns an object with the Player's stats. The object has the following properties::
{
hacking_skill: Current Hacking skill level
hacking: Current Hacking skill level
hp: Current health points
max_hp: Maximum health points
strength: Current Strength skill level

View File

@@ -54,7 +54,7 @@ In :ref:`netscriptjs`::
getTeamSize() <bladeburnerapi/getTeamSize>
setTeamSize() <bladeburnerapi/setTeamSize>
getCityEstimatedPopulation() <bladeburnerapi/getCityEstimatedPopulation>
getCityEstimatedCommunities() <bladeburnerapi/getCityEstimatedCommunities>
getCityCommunities() <bladeburnerapi/getCityCommunities>
getCityChaos() <bladeburnerapi/getCityChaos>
getCity() <bladeburnerapi/getCity>
switchCity() <bladeburnerapi/switchCity>

View File

@@ -93,6 +93,9 @@ This includes information such as function signatures, what they do, and their r
wget() <basicfunctions/wget>
getFavorToDonate() <basicfunctions/getFavorToDonate>
flags() <basicfunctions/flags>
alert() <basicfunctions/alert>
toast() <basicfunctions/toast>
tprintf() <basicfunctions/tprintf>
.. toctree::
:caption: Deprecated:

View File

@@ -13,25 +13,27 @@ TIX API can be purchased by visiting the World Stock Exchange in-game.
Access to the TIX API currently costs $5 billion. After you purchase it, you will retain this
access even after you 'reset' by installing Augmentations
**TIX API functions must be accessed through the stock namespace**
.. toctree::
:caption: API Functions:
getStockSymbols() <tixapi/getStockSymbols>
getStockPrice() <tixapi/getStockPrice>
getStockAskPrice() <tixapi/getStockAskPrice>
getStockBidPrice() <tixapi/getStockBidPrice>
getStockPosition() <tixapi/getStockPosition>
getStockMaxShares() <tixapi/getStockMaxShares>
getStockPurchaseCost() <tixapi/getStockPurchaseCost>
getStockSaleGain() <tixapi/getStockSaleGain>
buyStock() <tixapi/buyStock>
sellStock() <tixapi/sellStock>
shortStock() <tixapi/shortStock>
getSymbols() <tixapi/getSymbols>
getPrice() <tixapi/getPrice>
getAskPrice() <tixapi/getAskPrice>
getBidPrice() <tixapi/getBidPrice>
getPosition() <tixapi/getPosition>
getMaxShares() <tixapi/getMaxShares>
getPurchaseCost() <tixapi/getPurchaseCost>
getSaleGain() <tixapi/getSaleGain>
buy() <tixapi/buy>
sell() <tixapi/sell>
short() <tixapi/short>
sellShort() <tixapi/sellShort>
placeOrder() <tixapi/placeOrder>
cancelOrder() <tixapi/cancelOrder>
getOrders() <tixapi/getOrders>
getStockVolatility() <tixapi/getStockVolatility>
getStockForecast() <tixapi/getStockForecast>
getVolatility() <tixapi/getVolatility>
getForecast() <tixapi/getForecast>
purchase4SMarketData() <tixapi/purchase4SMarketData>
purchase4SMarketDataTixApi() <tixapi/purchase4SMarketDataTixApi>

View File

@@ -1,37 +1,37 @@
.. _netscriptjs:
NetscriptJS (Netscript 2.0)
===========================
Netscript 2.0, or Netscript JS, is the new and improved version of Netscript that
allows users to write (almost) full-fledged Javascript code in their scripts, while
NS2
===
The improved version of Netscript that
allows users to write full-fledged Javascript code in their scripts, while
still being able to access the Netscript functions.
NetscriptJS was developed primarily by `Github user jaguilar <https://github.com/jaguilar>`_
ns2 was developed primarily by `Github user jaguilar <https://github.com/jaguilar>`_
On top of having almost all of the features and capabilities of JavaScript, NetscriptJS is also
significantly faster than Netscript 1.0.
On top of having almost all of the features and capabilities of JavaScript, ns2 is also
significantly faster than ns1.
This documentation will not go over any of the additional features of NetscriptJS, since
This documentation will not go over any of the additional features of ns2, since
there is plenty of documentation on Javascript available on the web.
Browser compatibility
---------------------
As of the time of writing this, a few browsers do not support `dynamic import <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>`_ functionality and therefore cannot run NetscriptJS scripts. These browsers will thus only be capable of using Netscript 1.0.
As of the time of writing this, a few browsers do not support `dynamic import <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>`_ functionality and therefore cannot run ns2 scripts. These browsers will thus only be capable of using ns1.
How to use NetscriptJS
How to use ns2
----------------------
Working with NetscriptJS scripts is the same as Netscript 1.0 scripts. The only difference
is that NetscriptJS scripts use the ".ns" or ".js" extension rather than ".script". E.g.::
Working with ns2 scripts is the same as ns1 scripts. The only difference
is that ns2 scripts use the ".ns" or ".js" extension rather than ".script". E.g.::
$ nano foo.ns
$ run foo.ns -t 100 arg1 arg2 arg3
exec("foo.ns", "purchasedServer1", "100", "randomArg");
The caveat when using NetscriptJS to write scripts is that your code must be
The caveat when using ns2 to write scripts is that your code must be
asynchronous. Furthermore, instead of using the global scope and executing your code
sequentially, NetscriptJS uses a :code:`main()` function as an entry point.
sequentially, ns2 uses a :code:`main()` function as an entry point.
Furthermore, the "Netscript environment" must be passed into a NetscriptJS script through
Furthermore, the "Netscript environment" must be passed into a ns2 script through
the main function. This environment includes all of the pre-defined Netscript functions
(:code:`hack()`, :code:`exec`, etc.) as well as the arguments you pass to the script.
@@ -53,6 +53,9 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
* sleep
* prompt
* wget
* scp
* write
* writePort
* Any function that contains :code:`await` must be declared as :code:`async`
@@ -60,9 +63,9 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
* Any functions that you want to be visible from other scripts must be marked with :code:`export`.
* **Do not write any infinite loops without using a** :code:`sleep` **or one of the timed Netscript functions like** :code:`hack`. Doing so will crash your game.
* **Do not write any infinite loops without using a** :code:`sleep` **or one of the timed Netscript functions like** :code:`hack`. Doing so will freeze your game.
* Any global variable declared in a NetscriptJS script is shared between all instances of that
* Any global variable declared in a ns2 script is shared between all instances of that
script. For example, assume you write a script *foo.ns* and declared a global variable like so::
//foo.ns
@@ -88,93 +91,68 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
the script will repeatedly print the value 5).
These global variables can be thought of as `C++ static class members <https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm>`_,
where a NetscriptJS script is a class and a global variable is a static member within that class.
where a ns2 script is a class and a global variable is a static member within that class.
Warnings
--------
The NetscriptJS evaluation engine works by converting your code into a blob URL and then
using a dynamic import to load your code as a module. Every unique NetscriptJS script
is loaded as its own module. This means that
making a small edit to a NetscriptJS script results in a new module being generated.
Example
-------
At this point, we have been unable to find a method for deleting modules from browsers so that
they get garbage collected.
early-hack-template.script
The result is that these modules from NetscriptJS scripts accumulate in your browser,
using memory that never gets released. Over time, this results in a memory-leak type
situation that can slow down your computer.
.. code-block:: javascript
Therefore, there are two recommendations for those who decide to use NetscriptJS:
1. Every now and then, close and re-open the game. This will clear all of the modules.
To be safe, I recommend **completely** closing the game's tab and then re-opening it.
Depending on your browser, a refresh or page reload does not always clear the modules.
2. Only use NetscriptJS scripts when needed. It is very unlikely that NetscriptJS
is needed for very simple scripts. By doing this, you will reduce the number of modules
that are loaded.
Examples
--------
**Script Scheduler (scriptScheduler.ns)**
This script shows some of the new functionality that is available in NetscriptJS,
including objects and object constructors, changing an object's prototype, and
importing other NetscriptJS scripts::
import {tprintColored} from "tprintColored.ns"; //Importing from other NetscriptJS scripts works!
function ScriptJob(params) {
if (params.fn == null) {
throw new Error("No Filename (fn) passed into ScriptJob ctor");
var target = args[0];
var moneyThresh = getServerMaxMoney(target) * 0.75;
var securityThresh = getServerMinSecurityLevel(target) + 5;
if (fileExists("BruteSSH.exe", "home")) {
brutessh(target);
}
nuke(target);
while(true) {
if (getServerSecurityLevel(target) > securityThresh) {
weaken(target);
} else if (getServerMoneyAvailable(target) < moneyThresh) {
grow(target);
} else {
hack(target);
}
this.fn = params.fn;
this.threads = params.threads ? params.threads : 1;
this.args = params.args ? params.args : [];
}
ScriptJob.prototype.run = function(ns) {
let runArgs = [this.fn, this.threads].concat(this.args);
if (!ns.run.apply(this, runArgs)) {
throw new Error("Unable to run " + this.fn + " on " +ns.getHostname());
}
tprintColored("Running " + this.fn + " on " + ns.getHostname(), "blue");
}
early-hack-template.ns
ScriptJob.prototype.exec = function(ns, target) {
ns.scp(this.fn, target);
let execArgs = [this.fn, target, this.threads].concat(this.args);
if (!ns.exec.apply(this, execArgs)) {
throw new Error("Unable to execute " + this.fn + " on " + target);
}
tprintColored("Executing " + this.fn + " on " + target, "blue");
}
.. code-block:: javascript
export async function main(ns) {
tprintColored("Starting scriptScheduler.ns", "red");
try {
let job = new ScriptJob({
fn: "test.js",
threads: 1,
args: ["foodnstuff"]
});
job.run(ns);
job.exec(ns, "foodnstuff");
} catch (e) {
ns.tprint("Exception thrown in scriptScheduler.ns: " + e);
var target = ns.args[0];
var moneyThresh = ns.getServerMaxMoney(target) * 0.75;
var securityThresh = ns.getServerMinSecurityLevel(target) + 5;
if (ns.fileExists("BruteSSH.exe", "home")) {
ns.brutessh(target);
}
ns.nuke(target);
while(true) {
if (ns.getServerSecurityLevel(target) > securityThresh) {
await ns.weaken(target);
} else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
await ns.grow(target);
} else {
await ns.hack(target);
}
}
}
Final Note
----------
NetscriptJS opens up a lot of possibilities when scripting. I look forward to seeing
the scripts that people come up with. Just remember that the power and capabilities of
NetscriptJS come with risks. Please backup your save if you're going to experiment with
NetscriptJS and report any serious exploits.
What's with the weird comment
-----------------------------
With great power comes great responsibility
You may have noticed that every new ns2 file will contains the following comment.
Happy hacking
.. code-block:: javascript
/**
* @param {NS} ns
**/
This command is used to help the text editor autocomplete functions in the Netscript API. You can enabling it by pressing ctrl+space after `ns.`
.. image:: autocomplete.png
The comment can be safely removed but it is recommended to keep it as it will help you.

View File

@@ -22,5 +22,4 @@ into a script using::
args.length
**WARNING: Do not try to modify the args array. This will break the game.
I will do my best to prevent players from doing this.**
**WARNING: Do not try to modify the args array. This will break the game.**

View File

@@ -1,7 +1,7 @@
buyStock() Netscript Function
buy() Netscript Function
=============================
.. js:function:: buyStock(sym, shares)
.. js:function:: buy(sym, shares)
:RAM cost: 2.5 GB
:param string sym: Symbol of stock to purchase

View File

@@ -1,7 +1,7 @@
getStockAskPrice() Netscript Function
getAskPrice() Netscript Function
=====================================
.. js:function:: getStockAskPrice(sym)
.. js:function:: getAskPrice(sym)
:RAM cost: 2 GB
:param string sym: Stock symbol

View File

@@ -1,7 +1,7 @@
getStockBidPrice() Netscript Function
getBidPrice() Netscript Function
=====================================
.. js:function:: getStockBidPrice(sym)
.. js:function:: getBidPrice(sym)
:RAM cost: 2 GB
:param string sym: Stock symbol

View File

@@ -1,7 +1,7 @@
getStockForecast() Netscript Function
getForecast() Netscript Function
=====================================
.. js:function:: getStockForecast(sym)
.. js:function:: getForecast(sym)
:RAM cost: 2.5 GB
:param string sym: Symbol of stock

View File

@@ -1,7 +1,7 @@
getStockMaxShares() Netscript Function
getMaxShares() Netscript Function
======================================
.. js:function:: getStockMaxShares(sym)
.. js:function:: getMaxShares(sym)
:RAM cost: 2 GB
:param string sym: Stock symbol

View File

@@ -1,7 +1,7 @@
getStockPosition() Netscript Function
getPosition() Netscript Function
=====================================
.. js:function:: getStockPosition(sym)
.. js:function:: getPosition(sym)
:RAM cost: 2 GB
:param string sym: Stock symbol
@@ -21,7 +21,7 @@ getStockPosition() Netscript Function
Example::
pos = getStockPosition("ECP");
pos = getPosition("ECP");
shares = pos[0];
avgPx = pos[1];
sharesShort = pos[2];

View File

@@ -1,7 +1,7 @@
getStockPrice() Netscript Function
getPrice() Netscript Function
==================================
.. js:function:: getStockPrice(sym)
.. js:function:: getPrice(sym)
:RAM cost: 2 GB
:param string sym: Stock symbol
@@ -15,4 +15,4 @@ getStockPrice() Netscript Function
Example::
getStockPrice("FSIG");
getPrice("FSIG");

View File

@@ -1,7 +1,7 @@
getStockPurchaseCost() Netscript Function
getPurchaseCost() Netscript Function
=========================================
.. js:function:: getStockPurchaseCost(sym, shares, posType)
.. js:function:: getPurchaseCost(sym, shares, posType)
:RAM cost: 2 GB
:param string sym: Stock symbol

View File

@@ -1,7 +1,7 @@
getStockSaleGain() Netscript Function
getSaleGain() Netscript Function
=====================================
.. js:function:: getStockSaleGain(sym, shares, posType)
.. js:function:: getSaleGain(sym, shares, posType)
:RAM cost: 2 GB
:param string sym: Stock symbol

View File

@@ -1,7 +1,7 @@
getStockSymbols() Netscript Function()
getSymbols() Netscript Function()
======================================
.. js:function:: getStockSymbols()
.. js:function:: getSymbols()
:RAM cost: 2 GB

View File

@@ -1,7 +1,7 @@
sellStock() Netscript Function
sell() Netscript Function
==============================
.. js:function:: sellStock(sym, shares)
.. js:function:: sell(sym, shares)
:RAM cost: 2.5 GB
:param string sym: Symbol of stock to sell

View File

@@ -1,7 +1,7 @@
shortStock() Netscript Function
short() Netscript Function
===============================
.. js:function:: shortStock(sym, shares)
.. js:function:: short(sym, shares)
:RAM cost: 2.5 GB
:param string sym: Symbol of stock to short