bitburner-src/doc/build/html/netscriptmisc.html

395 lines
25 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="English">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Netscript Miscellaneous &#8212; Bitburner 1.0 documentation</title>
<link rel="stylesheet" href="_static/agogo.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
2018-03-12 20:39:04 +01:00
<link rel="next" title="Keyboard Shortcuts" href="shortcuts.html" />
<link rel="prev" title="Netscript Singularity Functions" href="netscriptsingularityfunctions.html" />
</head>
<body>
<div class="header-wrapper" role="banner">
<div class="header">
<div class="headertitle"><a
href="index.html">Bitburner 1.0 documentation</a></div>
<div class="rel" role="navigation" aria-label="related navigation">
<a href="netscriptsingularityfunctions.html" title="Netscript Singularity Functions"
accesskey="P">previous</a> |
2018-03-12 20:39:04 +01:00
<a href="shortcuts.html" title="Keyboard Shortcuts"
accesskey="N">next</a> |
<a href="genindex.html" title="General Index"
accesskey="I">index</a>
</div>
</div>
</div>
<div class="content-wrapper">
<div class="content">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="netscript-miscellaneous">
<h1>Netscript Miscellaneous<a class="headerlink" href="#netscript-miscellaneous" title="Permalink to this headline"></a></h1>
<div class="section" id="netscript-ports">
<h2>Netscript Ports<a class="headerlink" href="#netscript-ports" title="Permalink to this headline"></a></h2>
<p>Netscript ports are endpoints that can be used to communicate between scripts.
A port is implemented as a sort of serialized queue, where you can only write
and read one element at a time from the port. When you read data from a port,
the element that is read is removed from the port.</p>
<p>The <code class="xref js js-func docutils literal"><span class="pre">read()</span></code>, <code class="xref js js-func docutils literal"><span class="pre">write()</span></code>, <code class="xref js js-func docutils literal"><span class="pre">clear()</span></code>, and <code class="xref js js-func docutils literal"><span class="pre">peek()</span></code>
Netscript functions can be used to interact with ports.</p>
<p>Right now, there are only 20 ports for Netscript, denoted by the number 1
through 20. When using the functions above, the ports are specified
by passing the number as the first argument.</p>
<p>IMPORTANT: The data inside ports are not saved! This means if you close and
re-open the game, or reload the page then you will lose all of the data in
the ports!</p>
<p><strong>Example Usage</strong></p>
<p>Here's a brief example of how ports work. For the sake of simplicity we'll only deal with port 1.</p>
<p>Let's assume Port 1 starts out empty (no data inside). We'll represent the port as such:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[]</span>
</pre></div>
</div>
<p>Now assume we ran the following simple script:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
<span class="n">write</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span> <span class="o">//</span><span class="n">Writes</span> <span class="n">the</span> <span class="n">value</span> <span class="n">of</span> <span class="n">i</span> <span class="n">to</span> <span class="n">port</span> <span class="mi">1</span>
<span class="p">}</span>
</pre></div>
</div>
<p>After this script executes, our script will contain every number from 0 through 9, as so:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span> <span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
</pre></div>
</div>
<p>Then, assume we run the following script:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">print</span><span class="p">(</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">));</span> <span class="o">//</span><span class="n">Reads</span> <span class="n">a</span> <span class="n">value</span> <span class="kn">from</span> <span class="nn">port</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">then</span> <span class="n">prints</span> <span class="n">it</span>
<span class="p">}</span>
</pre></div>
</div>
<p>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:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="mi">0</span>
<span class="mi">1</span>
<span class="mi">2</span>
</pre></div>
</div>
<p>And the data in port 1 will look like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
</pre></div>
</div>
<p><strong>Port Handles</strong></p>
<p>The <code class="xref js js-func docutils literal"><span class="pre">getPortHandle()</span></code> Netscript function can be used to get a handle to a Netscript Port.
This handle allows you to access several new port-related functions and the
port's underlying data structure, which is just a Javascript array. The functions are:</p>
<dl class="method">
<dt id="NetscriptPort.write">
<code class="descclassname">NetscriptPort.</code><code class="descname">write</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#NetscriptPort.write" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Arguments:</th><td class="field-body"><ul class="first simple">
<li><strong>data</strong> -- Data to write to the port</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">If the port is full, the item that is removed from the port is returned.
Otherwise, null is returned.</p>
</td>
</tr>
</tbody>
</table>
<p>Writes <cite>data</cite> to the port. Works the same as the Netscript function <cite>write</cite>.</p>
</dd></dl>
<dl class="method">
<dt id="NetscriptPort.tryWrite">
<code class="descclassname">NetscriptPort.</code><code class="descname">tryWrite</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#NetscriptPort.tryWrite" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Arguments:</th><td class="field-body"><ul class="first simple">
<li><strong>data</strong> -- Data to try to write to the port</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">True if the data is successfully written to the port, and false otherwise.</p>
</td>
</tr>
</tbody>
</table>
<p>Attempts to write <cite>data</cite> to the Netscript port. If the port is full, the data will
not be written. Otherwise, the data will be written normally.</p>
</dd></dl>
<dl class="method">
<dt id="NetscriptPort.full">
<code class="descclassname">NetscriptPort.</code><code class="descname">full</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#NetscriptPort.full" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">True if the Netscript Port is full, and false otherwise</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="NetscriptPort.empty">
<code class="descclassname">NetscriptPort.</code><code class="descname">empty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#NetscriptPort.empty" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">True if the Netscript Port is empty, and false otherwise</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="NetscriptPort.clear">
<code class="descclassname">NetscriptPort.</code><code class="descname">clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#NetscriptPort.clear" title="Permalink to this definition"></a></dt>
<dd><p>Clears all data from the port. Works the same as the Netscript function <cite>clear</cite></p>
</dd></dl>
<dl class="attribute">
<dt id="NetscriptPort.data">
<code class="descclassname">NetscriptPort.</code><code class="descname">data</code><a class="headerlink" href="#NetscriptPort.data" title="Permalink to this definition"></a></dt>
<dd><p>The Netscript port underlying data structure, which is just a Javascript array. All
valid Javascript Array methods can be called on this.</p>
</dd></dl>
<p>Port Handle Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>port = getPortHandle(5);
back = port.data.pop(); //Get and remove last element in port
//Remove an element from the port
i = port.data.findIndex(&quot;foo&quot;);
if (i != -1) {
port.data.slice(i, 1);
}
//Wait for port data before reading
while(port.empty()) {
sleep(10000);
}
res = port.read();
//Wait for there to be room in a port before writing
while (!port.tryWrite(5)) {
sleep(5000);
}
//Successfully wrote to port!
</pre></div>
</div>
</div>
<div class="section" id="comments">
<h2>Comments<a class="headerlink" href="#comments" title="Permalink to this headline"></a></h2>
<p>Netscript supports comments using the same syntax as <a class="reference external" href="https://www.w3schools.com/js/js_comments.asp">Javascript comments</a>.
Comments are not evaluated as code, and can be used to document and/or explain code:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">//</span><span class="n">This</span> <span class="ow">is</span> <span class="n">a</span> <span class="n">comment</span> <span class="ow">and</span> <span class="n">will</span> <span class="ow">not</span> <span class="n">get</span> <span class="n">executed</span> <span class="n">even</span> <span class="n">though</span> <span class="n">its</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">code</span>
<span class="o">/*</span> <span class="n">Multi</span>
<span class="o">*</span> <span class="n">line</span>
<span class="o">*</span> <span class="n">comment</span> <span class="o">*/</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;This code will actually get executed&quot;</span><span class="p">);</span>
</pre></div>
</div>
</div>
2018-03-12 20:39:04 +01:00
<div class="section" id="importing-functions">
<h2>Importing Functions<a class="headerlink" href="#importing-functions" title="Permalink to this headline"></a></h2>
<p>In Netscript you can import functions that are declared in other scripts.
The script will incur the RAM usage of all imported functions.
There are two ways of doing this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="o">*</span> <span class="k">as</span> <span class="n">namespace</span> <span class="kn">from</span> <span class="s2">&quot;script filename&quot;</span><span class="p">;</span> <span class="o">//</span><span class="n">Import</span> <span class="nb">all</span> <span class="n">functions</span> <span class="kn">from</span> <span class="nn">script</span>
<span class="k">import</span> <span class="p">{</span><span class="n">fn1</span><span class="p">,</span> <span class="n">fn2</span><span class="p">,</span> <span class="o">...</span><span class="p">}</span> <span class="kn">from</span> <span class="s2">&quot;script filename&quot;</span><span class="p">;</span> <span class="o">//</span><span class="n">Import</span> <span class="n">specific</span> <span class="n">functions</span> <span class="kn">from</span> <span class="nn">script</span>
</pre></div>
</div>
<p>Suppose you have a library script called <em>testlibrary.script</em>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">function</span> <span class="n">foo1</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
<span class="o">//</span><span class="n">function</span> <span class="n">definition</span><span class="o">...</span>
<span class="p">}</span>
<span class="n">function</span> <span class="n">foo2</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
<span class="o">//</span><span class="n">function</span> <span class="n">definition</span><span class="o">...</span>
<span class="p">}</span>
<span class="n">function</span> <span class="n">foo3</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
<span class="o">//</span><span class="n">function</span> <span class="n">definition</span><span class="o">...</span>
<span class="p">}</span>
<span class="n">function</span> <span class="n">foo4</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
<span class="o">//</span><span class="n">function</span> <span class="n">definition</span><span class="o">...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Then, if you wanted to use these functions in another script, you can import them like so:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="o">*</span> <span class="k">as</span> <span class="n">testlib</span> <span class="kn">from</span> <span class="s2">&quot;testlibrary.script&quot;</span><span class="p">;</span>
<span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">];</span>
<span class="o">//</span><span class="n">The</span> <span class="n">imported</span> <span class="n">functions</span> <span class="n">must</span> <span class="n">be</span> <span class="n">specified</span> <span class="n">using</span> <span class="n">the</span> <span class="n">namespace</span>
<span class="n">someVal1</span> <span class="o">=</span> <span class="n">testlib</span><span class="o">.</span><span class="n">foo3</span><span class="p">(</span><span class="n">values</span><span class="p">);</span>
<span class="n">someVal2</span> <span class="o">=</span> <span class="n">testlib</span><span class="o">.</span><span class="n">foo1</span><span class="p">(</span><span class="n">values</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">someVal1</span> <span class="o">&gt;</span> <span class="n">someVal2</span><span class="p">)</span> <span class="p">{</span>
<span class="o">//...</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="o">//...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>If you only wanted to import certain functions, you can do so without needing
to specify a namespace for the import:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>import {foo1, foo3} from &quot;testlibrary.script&quot;; //Saves RAM since not all functions are imported!
values = [1,2,3];
//No namespace needed
someVal1 = foo3(values);
someVal2 = foo1(values);
if (someVal1 &gt; someVal2) {
//...
} else {
//...
}
</pre></div>
</div>
<p>Note that exporting functions is not required.</p>
</div>
<div class="section" id="javascript-math-module">
<h2>Javascript Math Module<a class="headerlink" href="#javascript-math-module" title="Permalink to this headline"></a></h2>
<p>The <a class="reference external" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math">Javascript Math Module</a> is
supported in Netscript and is used in the same way:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">numThreads</span> <span class="o">=</span> <span class="n">Math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">getServerRam</span><span class="p">(</span><span class="s2">&quot;foodnstuff&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="mf">3.4</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="javascript-date-module">
<h2>Javascript Date Module<a class="headerlink" href="#javascript-date-module" title="Permalink to this headline"></a></h2>
<p>The <a class="reference external" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date">Javascript Date Module</a> is supported in Netscript.
However, since the 'new' operator does not work in Netscript, only the Date module's static functions can be used:</p>
<ul class="simple">
<li>now()</li>
<li>UTC()</li>
<li>Parse()</li>
<li>Maybe some others I don't know about</li>
</ul>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">time</span> <span class="o">=</span> <span class="n">Date</span><span class="o">.</span><span class="n">now</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="javascript-number-module">
<h2>Javascript Number Module<a class="headerlink" href="#javascript-number-module" title="Permalink to this headline"></a></h2>
<p>The <a class="reference external" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">Javascript Number module</a> is supported in Netscript.</p>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">tprint</span><span class="p">(</span><span class="n">Number</span><span class="o">.</span><span class="n">isInteger</span><span class="p">(</span><span class="mi">1</span><span class="p">));</span> <span class="o">//</span><span class="kc">True</span>
<span class="n">tprint</span><span class="p">(</span><span class="n">Number</span><span class="o">.</span><span class="n">isInteger</span><span class="p">(</span><span class="mf">1.534059</span><span class="p">));</span> <span class="o">//</span><span class="kc">False</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sidebar">
<h3>Table Of Contents</h3>
<p class="caption"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="netscript.html"> Netscript</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="netscriptdatatypes.html"> Data Types and Variables</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptoperators.html"> Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptloopsandconditionals.html"> Loops and Conditionals</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptscriptarguments.html"> Script Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptfunctions.html"> Basic Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptadvancedfunctions.html"> Advanced Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscripthacknetnodeapi.html"> Hacknet Node API</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptixapi.html"> Trade Information eXchange (TIX) API</a></li>
<li class="toctree-l2"><a class="reference internal" href="netscriptsingularityfunctions.html"> Singularity Functions</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#"> Miscellaneous</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#netscript-ports">Netscript Ports</a></li>
<li class="toctree-l3"><a class="reference internal" href="#comments">Comments</a></li>
2018-03-12 20:39:04 +01:00
<li class="toctree-l3"><a class="reference internal" href="#importing-functions">Importing Functions</a></li>
<li class="toctree-l3"><a class="reference internal" href="#javascript-math-module">Javascript Math Module</a></li>
<li class="toctree-l3"><a class="reference internal" href="#javascript-date-module">Javascript Date Module</a></li>
<li class="toctree-l3"><a class="reference internal" href="#javascript-number-module">Javascript Number Module</a></li>
</ul>
</li>
</ul>
</li>
2018-03-12 20:39:04 +01:00
<li class="toctree-l1"><a class="reference internal" href="shortcuts.html"> Keyboard Shortcuts</a></li>
</ul>
<div role="search">
<h3 style="margin-top: 1.5em;">Search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
<div class="footer-wrapper">
<div class="footer">
<div class="left">
<div role="navigation" aria-label="related navigaton">
<a href="netscriptsingularityfunctions.html" title="Netscript Singularity Functions"
>previous</a> |
2018-03-12 20:39:04 +01:00
<a href="shortcuts.html" title="Keyboard Shortcuts"
>next</a> |
<a href="genindex.html" title="General Index"
>index</a>
</div>
<div role="note" aria-label="source link">
<br/>
<a href="_sources/netscriptmisc.rst.txt"
rel="nofollow">Show Source</a>
</div>
</div>
<div class="right">
<div class="footer" role="contentinfo">
&#169; Copyright 2017, Bitburner.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.4.
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>