Updated HTML docs

This commit is contained in:
Griatch 2021-10-26 21:41:11 +02:00
parent 66d0ad0bc9
commit 7900aad365
2073 changed files with 32986 additions and 41197 deletions

View file

@ -14,6 +14,8 @@
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "tex2jax_process|mathjax_process|math|output_area"}})</script>
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
@ -38,39 +40,25 @@
<div class="bodywrapper">
<div class="body" role="main">
<section id="tutorial-aggressive-npcs">
<section class="tex2jax_ignore mathjax_ignore" id="tutorial-aggressive-npcs">
<h1>Tutorial Aggressive NPCs<a class="headerlink" href="#tutorial-aggressive-npcs" title="Permalink to this headline"></a></h1>
<p>This tutorial shows the implementation of an NPC object that responds to characters entering their
location. In this example the NPC has the option to respond aggressively or not, but any actions
could be triggered this way.</p>
<p>One could imagine using a <a class="reference internal" href="../Components/Scripts.html"><span class="doc">Script</span></a> that is constantly checking for newcomers. This would be
<p>One could imagine using a <a class="reference internal" href="../Components/Scripts.html"><span class="doc std std-doc">Script</span></a> that is constantly checking for newcomers. This would be
highly inefficient (most of the time its check would fail). Instead we handle this on-demand by
using a couple of existing object hooks to inform the NPC that a Character has entered.</p>
<p>It is assumed that you already know how to create custom room and character typeclasses, please see
the <a class="reference internal" href="Starting/Part3/Tutorial-for-basic-MUSH-like-game.html"><span class="doc">Basic Game tutorial</span></a> if you havent already done this.</p>
the <a class="reference internal" href="Starting/Part3/Tutorial-for-basic-MUSH-like-game.html"><span class="doc std std-doc">Basic Game tutorial</span></a> if you havent already done this.</p>
<p>What we will need is the following:</p>
<ul class="simple">
<li><p>An NPC typeclass that can react when someone enters.</p></li>
<li><p>A custom <a class="reference external" href="Components/Objects.html#rooms">Room</a> typeclass that can tell the NPC that someone entered.</p></li>
<li><p>A custom <a class="reference internal" href="../Components/Objects.html#rooms"><span class="std std-doc">Room</span></a> typeclass that can tell the NPC that someone entered.</p></li>
<li><p>We will also tweak our default <code class="docutils literal notranslate"><span class="pre">Character</span></code> typeclass a little.</p></li>
</ul>
<p>To begin with, we need to create an NPC typeclass. Create a new file inside of your typeclasses
folder and name it <code class="docutils literal notranslate"><span class="pre">npcs.py</span></code> and then add the following code:</p>
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typeclasses.characters</span> <span class="kn">import</span> <span class="n">Character</span>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typeclasses.characters</span> <span class="kn">import</span> <span class="n">Character</span>
<span class="k">class</span> <span class="nc">NPC</span><span class="p">(</span><span class="n">Character</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
@ -86,28 +74,14 @@ folder and name it <code class="docutils literal notranslate"><span class="pre">
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">execute_cmd</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;say Greetings, </span><span class="si">{</span><span class="n">character</span><span class="si">}</span><span class="s2">!&quot;</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div>
<p>We will define our custom <code class="docutils literal notranslate"><span class="pre">Character</span></code> typeclass below. As for the new <code class="docutils literal notranslate"><span class="pre">at_char_entered</span></code> method weve
just defined, well ensure that it will be called by the room where the NPC is located, when a
player enters that room. Youll notice that right now, the NPC merely speaks. You can expand this
part as you like and trigger all sorts of effects here (like combat code, fleeing, bartering or
quest-giving) as your game design dictates.</p>
<p>Now your <code class="docutils literal notranslate"><span class="pre">typeclasses.rooms</span></code> module needs to have the following added:</p>
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="c1"># Add this import to the top of your file.</span>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Add this import to the top of your file.</span>
<span class="kn">from</span> <span class="nn">evennia</span> <span class="kn">import</span> <span class="n">utils</span>
<span class="c1"># Add this hook in any empty area within your Room class.</span>
@ -123,7 +97,7 @@ quest-giving) as your game design dictates.</p>
<span class="c1"># An NPC is in the room</span>
<span class="n">item</span><span class="o">.</span><span class="n">at_char_entered</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">inherits_from</span></code> must be given the full path of the class. If the object inherited a class from your
<code class="docutils literal notranslate"><span class="pre">world.races</span></code> module, then you would check inheritance with <code class="docutils literal notranslate"><span class="pre">world.races.Human</span></code>, for example. There
is no need to import these prior, as we are passing in the full path. As a matter of a fact,
@ -143,14 +117,7 @@ contents and inform any <code class="docutils literal notranslate"><span class="
overload. This means that a character entering would see the NPC perform its actions before the
look command. Deactivate the look command in the default <code class="docutils literal notranslate"><span class="pre">Character</span></code> class within the
<code class="docutils literal notranslate"><span class="pre">typeclasses.characters</span></code> module:</p>
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span>
<span class="normal">7</span>
<span class="normal">8</span></pre></div></td><td class="code"><div class="highlight"><pre><span></span> <span class="c1"># Add this hook in any blank area within your Character class.</span>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span> <span class="c1"># Add this hook in any blank area within your Character class.</span>
<span class="k">def</span> <span class="nf">at_after_move</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source_location</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Default is to look around after a move </span>
@ -159,7 +126,7 @@ overload. This means that a character entering would see the NPC perform its ac
<span class="c1">#self.execute_cmd(&#39;look&#39;)</span>
<span class="k">pass</span>
</pre></div>
</td></tr></table></div>
</div>
<p>Now lets create an NPC and make it aggressive. Type the following commands into your MUD client:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">reload</span>
<span class="n">create</span><span class="o">/</span><span class="n">drop</span> <span class="n">Orc</span><span class="p">:</span><span class="n">npcs</span><span class="o">.</span><span class="n">NPC</span>
@ -225,7 +192,7 @@ AI code).</p>
<h3>Versions</h3>
<ul>
<li><a href="Tutorial-Aggressive-NPCs.html">1.0-dev (develop branch)</a></li>
<li><a href="../../0.9.5/index.html">0.9.5 (v0.9.5 branch)</a></li>
<li><a href="../../0.95/index.html">0.95 (v0.9.5 branch)</a></li>
</ul>
</div>