Updated HTML docs

This commit is contained in:
Griatch 2020-06-16 22:49:43 +02:00
parent f505351730
commit a551188691
1002 changed files with 30387 additions and 9820 deletions

View file

@ -7,11 +7,13 @@
<title>Tutorial Aggressive NPCs &#8212; Evennia 1.0-dev documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/language_data.js"></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" />
@ -25,7 +27,10 @@
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Evennia 1.0-dev documentation</a> &#187;</li>
<li class="nav-item nav-item-0"><a href="index.html">Evennia 1.0-dev documentation</a> &#187;</li>
<li class="nav-item nav-item-last"><a href="#">Tutorial Aggressive NPCs</a></li>
</ul>
</div>
@ -36,16 +41,22 @@
<div class="section" 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="Scripts.html"><span class="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="Tutorial-for-basic-MUSH-like-game.html"><span class="doc">Basic Game tutorial</span></a> if you havent already done this.</p>
<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="Scripts.html"><span class="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="Tutorial-for-basic-MUSH-like-game.html"><span class="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="/Objects.html#rooms">Room</a> typeclass that can tell the NPC that someone entered.</p></li>
<li><p>A custom <a class="reference external" href="Objects.html#rooms">Room</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>
<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> 1
2
3
@ -77,7 +88,11 @@
<span class="bp">self</span><span class="o">.</span><span class="n">execute_cmd</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;say Greetings, {character}!&quot;</span><span class="p">)</span>
</pre></div>
</td></tr></table></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>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> 1
2
@ -110,12 +125,25 @@
<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>
<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, <code class="docutils literal notranslate"><span class="pre">inherits_from</span></code> does not properly work if you import the class and only pass in the name of the class.</p>
<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,
<code class="docutils literal notranslate"><span class="pre">inherits_from</span></code> does not properly work if you import the class and only pass in the name of the
class.</p>
<blockquote>
<div><p>Note: <a class="reference external" href="https://github.com/evennia/evennia/blob/master/evennia/objects/objects.py#L1529">at_object_receive</a> is a default hook of the <code class="docutils literal notranslate"><span class="pre">DefaultObject</span></code> typeclass (and its children). Here we are overriding this hook in our customized room typeclass to suit our needs.</p>
<div><p>Note:
<a class="reference external" href="https://github.com/evennia/evennia/blob/master/evennia/objects/objects.py#L1529">at_object_receive</a>
is a default hook of the <code class="docutils literal notranslate"><span class="pre">DefaultObject</span></code> typeclass (and its children). Here we are overriding this
hook in our customized room typeclass to suit our needs.</p>
</div></blockquote>
<p>This room checks the typeclass of objects entering it (using <code class="docutils literal notranslate"><span class="pre">utils.inherits_from</span></code> and responds to <code class="docutils literal notranslate"><span class="pre">Characters</span></code>, ignoring other NPCs or objects. When triggered the room will look through its contents and inform any <code class="docutils literal notranslate"><span class="pre">NPCs</span> <span class="pre">inside</span> <span class="pre">by</span> <span class="pre">calling</span> <span class="pre">their</span> </code>at_char_entered` method.</p>
<p>Youll also see that we have added a look into this code. This is because, by default, the <code class="docutils literal notranslate"><span class="pre">at_object_receive</span></code> is carried out <em>before</em> the characters <code class="docutils literal notranslate"><span class="pre">at_after_move</span></code> which, we will now 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>
<p>This room checks the typeclass of objects entering it (using <code class="docutils literal notranslate"><span class="pre">utils.inherits_from</span></code> and responds to
<code class="docutils literal notranslate"><span class="pre">Characters</span></code>, ignoring other NPCs or objects. When triggered the room will look through its
contents and inform any <code class="docutils literal notranslate"><span class="pre">NPCs</span> <span class="pre">inside</span> <span class="pre">by</span> <span class="pre">calling</span> <span class="pre">their</span> </code>at_char_entered` method.</p>
<p>Youll also see that we have added a look into this code. This is because, by default, the
<code class="docutils literal notranslate"><span class="pre">at_object_receive</span></code> is carried out <em>before</em> the characters <code class="docutils literal notranslate"><span class="pre">at_after_move</span></code> which, we will now
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>1
2
3
@ -139,13 +167,16 @@
</pre></div>
</div>
<blockquote>
<div><p>Note: You could also give the path as <code class="docutils literal notranslate"><span class="pre">typeclasses.npcs.NPC</span></code>, but Evennia will look into the <code class="docutils literal notranslate"><span class="pre">typeclasses</span></code> folder automatically, so this is a little shorter.</p>
<div><p>Note: You could also give the path as <code class="docutils literal notranslate"><span class="pre">typeclasses.npcs.NPC</span></code>, but Evennia will look into the
<code class="docutils literal notranslate"><span class="pre">typeclasses</span></code> folder automatically, so this is a little shorter.</p>
</div></blockquote>
<p>When you enter the aggressive NPCs location, it will default to using its peaceful action (say your name is Anna):</p>
<p>When you enter the aggressive NPCs location, it will default to using its peaceful action (say your
name is Anna):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Orc</span> <span class="n">says</span><span class="p">,</span> <span class="s2">&quot;Greetings, Anna!&quot;</span>
</pre></div>
</div>
<p>Now we turn on the aggressive mode (we do it manually but it could also be triggered by some sort of AI code).</p>
<p>Now we turn on the aggressive mode (we do it manually but it could also be triggered by some sort of
AI code).</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">set</span> <span class="n">orc</span><span class="o">/</span><span class="n">is_aggressive</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
@ -200,7 +231,10 @@
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Evennia 1.0-dev documentation</a> &#187;</li>
<li class="nav-item nav-item-0"><a href="index.html">Evennia 1.0-dev documentation</a> &#187;</li>
<li class="nav-item nav-item-last"><a href="#">Tutorial Aggressive NPCs</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">