evennia/docs/4.x/Howtos/Tutorial-NPC-Reacting.html
2024-03-17 14:15:56 +01:00

222 lines
No EOL
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>NPCs reacting to your presence &#8212; Evennia latest 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" />
<link rel="next" title="NPC merchants" href="Tutorial-NPC-Merchants.html" />
<link rel="prev" title="NPCs that listen to what is said" href="Tutorial-NPC-Listening.html" />
</head><body>
<div class="admonition important">
<p class="first admonition-title">Note</p>
<p class="last">You are reading an old version of the Evennia documentation. <a href="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="Tutorial-NPC-Merchants.html" title="NPC merchants"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="Tutorial-NPC-Listening.html" title="NPCs that listen to what is said"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Evennia latest</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="Howtos-Overview.html" accesskey="U">Tutorials and How-Tos</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">NPCs reacting to your presence</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../_static/evennia_logo.png" alt="Logo"/>
</a></p>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
<h4>Previous topic</h4>
<p class="topless"><a href="Tutorial-NPC-Listening.html"
title="previous chapter">NPCs that listen to what is said</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="Tutorial-NPC-Merchants.html"
title="next chapter">NPC merchants</a></p>
<div role="note" aria-label="source link">
<!--h3>This Page</h3-->
<ul class="this-page-menu">
<li><a href="../_sources/Howtos/Tutorial-NPC-Reacting.md.txt"
rel="nofollow">Show Page Source</a></li>
</ul>
</div><h3>Links</h3>
<ul>
<li><a href="https://www.evennia.com/docs/latest/index.html">Documentation Top</a> </li>
<li><a href="https://www.evennia.com">Evennia Home</a> </li>
<li><a href="https://github.com/evennia/evennia">Github</a> </li>
<li><a href="http://games.evennia.com">Game Index</a> </li>
<li>
<a href="https://discord.gg/AJJpcRUhtF">Discord</a> -
<a href="https://github.com/evennia/evennia/discussions">Discussions</a> -
<a href="https://evennia.blogspot.com/">Blog</a>
</li>
</ul>
</div>
</div>
<div class="bodywrapper">
<div class="body" role="main">
<section class="tex2jax_ignore mathjax_ignore" id="npcs-reacting-to-your-presence">
<h1>NPCs reacting to your presence<a class="headerlink" href="#npcs-reacting-to-your-presence" title="Permalink to this headline"></a></h1>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&gt; north
------------------------------------
Meadow
You are standing in a green meadow.
A bandit is here.
------------------------------------
Bandit gives you a menacing look!
</pre></div>
</div>
<p>This tutorial shows the implementation of an NPC object that responds to characters entering their
location.</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 <span class="xref myst">Room</span> 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>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># in mygame/typeclasses/npcs.py (for example)</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="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A NPC typeclass which extends the character class.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">at_char_entered</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">character</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A simple is_aggressive check.</span>
<span class="sd"> Can be expanded upon later.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">is_aggressive</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 Graaah! Die, </span><span class="si">{</span><span class="n">character</span><span class="si">}</span><span class="s2">!&quot;</span><span class="p">)</span>
<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>
</div>
<p>Here we make a simple method on the <code class="docutils literal notranslate"><span class="pre">NPC</span></code>˙. We expect it to be called when a (player-)character enters the room. We dont actually set the <code class="docutils literal notranslate"><span class="pre">is_aggressive</span></code> <a class="reference internal" href="../Components/Attributes.html"><span class="doc std std-doc">Attribute</span></a> beforehand; if its not set, the NPC is simply non-hostile.</p>
<p>Whenever <em>something</em> enters the <code class="docutils literal notranslate"><span class="pre">Room</span></code>, its <a class="reference internal" href="../api/evennia.objects.objects.html#evennia.objects.objects.DefaultObject.at_object_receive" title="evennia.objects.objects.DefaultObject.at_object_receive"><span class="xref myst py py-meth">at_object_receive</span></a> hook will be called. So we should override it.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># in mygame/typeclasses/rooms.py</span>
<span class="kn">from</span> <span class="nn">evennia</span> <span class="kn">import</span> <span class="n">utils</span>
<span class="c1"># ... </span>
<span class="k">class</span> <span class="nc">Room</span><span class="p">(</span><span class="n">ObjectParent</span><span class="p">,</span> <span class="n">DefaultRoom</span><span class="p">):</span>
<span class="c1"># ... </span>
<span class="k">def</span> <span class="nf">at_object_receive</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arriving_obj</span><span class="p">,</span> <span class="n">source_location</span><span class="p">):</span>
<span class="k">if</span> <span class="n">arriving_obj</span><span class="o">.</span><span class="n">account</span><span class="p">:</span>
<span class="c1"># this has an active acccount - a player character</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="p">:</span>
<span class="c1"># get all npcs in the room and inform them</span>
<span class="k">if</span> <span class="n">utils</span><span class="o">.</span><span class="n">inherits_from</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s2">&quot;typeclasses.npcs.NPC&quot;</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">at_char_entered</span><span class="p">(</span><span class="n">arriving_obj</span><span class="p">)</span>
</pre></div>
</div>
<aside class="sidebar">
<p class="sidebar-title">Universal Object methods</p>
<p>Remember that Rooms are <code class="docutils literal notranslate"><span class="pre">Objects</span></code>. So the same <code class="docutils literal notranslate"><span class="pre">at_object_receive</span></code> hook will fire for you when you pick something up (making you receive it). Or for a box when putting something inside it.</p>
</aside>
<p>A currently puppeted Character will have an <code class="docutils literal notranslate"><span class="pre">.account</span></code> attached to it. We use that to know that the thing arriving is a Character. We then use Evennias <a class="reference internal" href="../api/evennia.utils.utils.html#evennia.utils.utils.inherits_from" title="evennia.utils.utils.inherits_from"><span class="xref myst py py-func">utils.inherits_from</span></a> helper utility to get every NPC in the room can each of their newly created <code class="docutils literal notranslate"><span class="pre">at_char_entered</span></code> method.</p>
<p>Make sure to <code class="docutils literal notranslate"><span class="pre">reload</span></code>.</p>
<p>Lets create an NPC and make it aggressive. For the sake of this example, lets assume your name is “Anna” and that there is a room to the north of your current location.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&gt; create/drop Orc:typeclasses.npcs.NPC
&gt; north
&gt; south
Orc says, Greetings, Anna!
</pre></div>
</div>
<p>Now lets turn the orc aggressive.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&gt; set orc/is_aggressive = True
&gt; north
&gt; south
Orc says, Graah! Die, Anna!
</pre></div>
</div>
<p>Thats one easily aggravated Orc!</p>
</section>
</div>
</div>
</div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="Tutorial-NPC-Merchants.html" title="NPC merchants"
>next</a> |</li>
<li class="right" >
<a href="Tutorial-NPC-Listening.html" title="NPCs that listen to what is said"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Evennia latest</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="Howtos-Overview.html" >Tutorials and How-Tos</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">NPCs reacting to your presence</a></li>
</ul>
</div>
<div class="admonition important">
<p class="first admonition-title">Note</p>
<p class="last">You are reading an old version of the Evennia documentation. <a href="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2023, The Evennia developer community.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
</div>
</body>
</html>