evennia/docs/latest/api/evennia.contrib.tutorials.evadventure.ai.html
Evennia docbuilder action 243d596662 Updated HTML docs.
2025-08-15 18:14:21 +00:00

299 lines
No EOL
22 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 lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>evennia.contrib.tutorials.evadventure.ai &#8212; Evennia latest documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
<link rel="stylesheet" type="text/css" href="../_static/nature.css?v=279e0f84" />
<link rel="stylesheet" type="text/css" href="../_static/custom.css?v=e4a91a55" />
<script src="../_static/documentation_options.js?v=c6e86fd7"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="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="evennia.contrib.tutorials.evadventure.build_techdemo" href="evennia.contrib.tutorials.evadventure.build_techdemo.html" />
<link rel="prev" title="evennia.contrib.tutorials.evadventure" href="evennia.contrib.tutorials.evadventure.html" />
</head><body>
<div class="related" role="navigation" aria-label="Related">
<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="evennia.contrib.tutorials.evadventure.build_techdemo.html" title="evennia.contrib.tutorials.evadventure.build_techdemo"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="evennia.contrib.tutorials.evadventure.html" title="evennia.contrib.tutorials.evadventure"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Evennia</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../Evennia-API.html" >API Summary</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="evennia-api.html" >evennia</a> &#187;</li>
<li class="nav-item nav-item-3"><a href="evennia.html" >evennia</a> &#187;</li>
<li class="nav-item nav-item-4"><a href="evennia.contrib.html" >evennia.contrib</a> &#187;</li>
<li class="nav-item nav-item-5"><a href="evennia.contrib.tutorials.html" >evennia.contrib.tutorials</a> &#187;</li>
<li class="nav-item nav-item-6"><a href="evennia.contrib.tutorials.evadventure.html" accesskey="U">evennia.contrib.tutorials.evadventure</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">evennia.contrib.tutorials.evadventure.ai</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-evennia.contrib.tutorials.evadventure.ai">
<span id="evennia-contrib-tutorials-evadventure-ai"></span><h1>evennia.contrib.tutorials.evadventure.ai<a class="headerlink" href="#module-evennia.contrib.tutorials.evadventure.ai" title="Link to this heading"></a></h1>
<p>NPC AI module for EvAdventure (WIP)</p>
<p>This implements a simple state machine for NPCs to follow.</p>
<p>The AIHandler class is stored on the NPC object and is queried by the game loop to determine what
the NPC does next. This leads to the calling of one of the relevant state methods on the NPC, which
is where the actual logic for the NPCs behaviour is implemented. Each state is responsible for
switching to the next state when the conditions are met.</p>
<p>The AIMixin class is a mixin that can be added to any object that needs AI. It provides the <strong>.ai</strong>
reference to the AIHandler and a few basic <strong>ai_*</strong> methods for basic AI behaviour.</p>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">evennia</span><span class="w"> </span><span class="kn">import</span> <span class="n">create_object</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.npc</span><span class="w"> </span><span class="kn">import</span> <span class="n">EvadventureNPC</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.ai</span><span class="w"> </span><span class="kn">import</span> <span class="n">AIMixin</span>
<span class="k">class</span><span class="w"> </span><span class="nc">MyMob</span><span class="p">(</span><span class="n">AIMixin</span><span class="p">,</span> <span class="n">EvadventureNPC</span><span class="p">):</span>
<span class="k">pass</span>
<span class="n">mob</span> <span class="o">=</span> <span class="n">create_object</span><span class="p">(</span><span class="n">MyMob</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="s2">&quot;Goblin&quot;</span><span class="p">,</span> <span class="n">location</span><span class="o">=</span><span class="n">room</span><span class="p">)</span>
<span class="n">mob</span><span class="o">.</span><span class="n">ai</span><span class="o">.</span><span class="n">set_state</span><span class="p">(</span><span class="s2">&quot;roam&quot;</span><span class="p">)</span>
<span class="c1"># tick the ai whenever needed</span>
<span class="n">mob</span><span class="o">.</span><span class="n">ai</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</pre></div>
</div>
<dl class="py class">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler">
<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">evennia.contrib.tutorials.evadventure.ai.</span></span><span class="sig-name descname"><span class="pre">AIHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler" title="Link to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<dl class="py attribute">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.attribute_name">
<span class="sig-name descname"><span class="pre">attribute_name</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'ai_state'</span></em><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.attribute_name" title="Link to this definition"></a></dt>
<dd></dd></dl>
<dl class="py attribute">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.attribute_category">
<span class="sig-name descname"><span class="pre">attribute_category</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'ai_state'</span></em><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.attribute_category" title="Link to this definition"></a></dt>
<dd></dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.__init__">
<span class="sig-name descname"><span class="pre">__init__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.__init__"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.__init__" title="Link to this definition"></a></dt>
<dd></dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.set_state">
<span class="sig-name descname"><span class="pre">set_state</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.set_state"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.set_state" title="Link to this definition"></a></dt>
<dd></dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.get_state">
<span class="sig-name descname"><span class="pre">get_state</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.get_state"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_state" title="Link to this definition"></a></dt>
<dd></dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.get_targets">
<span class="sig-name descname"><span class="pre">get_targets</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.get_targets"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_targets" title="Link to this definition"></a></dt>
<dd><p>Get a list of potential targets for the NPC to combat.</p>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.get_traversable_exits">
<span class="sig-name descname"><span class="pre">get_traversable_exits</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exclude_destination</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.get_traversable_exits"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_traversable_exits" title="Link to this definition"></a></dt>
<dd><p>Get a list of exits that the NPC can traverse. Optionally exclude a destination.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>exclude_destination</strong> (<em>Object</em><em>, </em><em>optional</em>) Exclude exits with this destination.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.random_probability">
<span class="sig-name descname"><span class="pre">random_probability</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">probabilities</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.random_probability"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.random_probability" title="Link to this definition"></a></dt>
<dd><p>Given a dictionary of probabilities, return the key of the chosen probability.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>probabilities</strong> (<em>dict</em>) A dictionary of probabilities, where the key is the action and the
value is the probability of that action.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIHandler.run">
<span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.run"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.run" title="Link to this definition"></a></dt>
<dd></dd></dl>
</dd></dl>
<dl class="py class">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIMixin">
<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">evennia.contrib.tutorials.evadventure.ai.</span></span><span class="sig-name descname"><span class="pre">AIMixin</span></span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIMixin"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIMixin" title="Link to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Mixin for adding AI to an Object. This is a simple state machine. Just add more <strong>ai_*</strong> methods
to the object to make it do more things.</p>
<p>In the tutorial, the handler is added directly to the Mob class, to avoid going into the details
of multiple inheritance. In a real game, you would probably want to use a mixin like this.</p>
<dl class="py attribute">
<dt class="sig sig-object py" id="evennia.contrib.tutorials.evadventure.ai.AIMixin.ai">
<span class="sig-name descname"><span class="pre">ai</span></span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIMixin.ai"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIMixin.ai" title="Link to this definition"></a></dt>
<dd></dd></dl>
</dd></dl>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../_static/evennia_logo.png" alt="Logo of Evennia"/>
</a></p>
<search 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" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
<h3><a href="../index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">evennia.contrib.tutorials.evadventure.ai</a><ul>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler"><code class="docutils literal notranslate"><span class="pre">AIHandler</span></code></a><ul>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.attribute_name"><code class="docutils literal notranslate"><span class="pre">AIHandler.attribute_name</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.attribute_category"><code class="docutils literal notranslate"><span class="pre">AIHandler.attribute_category</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.__init__"><code class="docutils literal notranslate"><span class="pre">AIHandler.__init__()</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.set_state"><code class="docutils literal notranslate"><span class="pre">AIHandler.set_state()</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_state"><code class="docutils literal notranslate"><span class="pre">AIHandler.get_state()</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_targets"><code class="docutils literal notranslate"><span class="pre">AIHandler.get_targets()</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_traversable_exits"><code class="docutils literal notranslate"><span class="pre">AIHandler.get_traversable_exits()</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.random_probability"><code class="docutils literal notranslate"><span class="pre">AIHandler.random_probability()</span></code></a></li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.run"><code class="docutils literal notranslate"><span class="pre">AIHandler.run()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIMixin"><code class="docutils literal notranslate"><span class="pre">AIMixin</span></code></a><ul>
<li><a class="reference internal" href="#evennia.contrib.tutorials.evadventure.ai.AIMixin.ai"><code class="docutils literal notranslate"><span class="pre">AIMixin.ai</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="evennia.contrib.tutorials.evadventure.html"
title="previous chapter">evennia.contrib.tutorials.evadventure</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="evennia.contrib.tutorials.evadventure.build_techdemo.html"
title="next chapter">evennia.contrib.tutorials.evadventure.build_techdemo</a></p>
</div>
<div role="note" aria-label="source link">
<!--h3>This Page</h3-->
<ul class="this-page-menu">
<li><a href="../_sources/api/evennia.contrib.tutorials.evadventure.ai.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>
<h3>Doc Versions</h3>
<ul>
<li>
<a href="https://www.evennia.com/docs/latest/index.html">latest (main branch)</a>
</li>
<li>
<a href="https://www.evennia.com/docs/5.x/index.html">v5.0.0 branch (outdated)</a>
</li>
<li>
<a href="https://www.evennia.com/docs/4.x/index.html">v4.0.0 branch (outdated)</a>
</li>
<li>
<a href="https://www.evennia.com/docs/3.x/index.html">v3.0.0 branch (outdated)</a>
</li>
<li>
<a href="https://www.evennia.com/docs/2.x/index.html">v2.0.0 branch (outdated)</a>
</li>
<li>
<a href="https://www.evennia.com/docs/1.x/index.html">v1.0.0 branch (outdated)</a>
</li>
<li>
<a href="https://www.evennia.com/docs/0.x/index.html">v0.9.5 branch (outdated)</a>
</li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<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="evennia.contrib.tutorials.evadventure.build_techdemo.html" title="evennia.contrib.tutorials.evadventure.build_techdemo"
>next</a> |</li>
<li class="right" >
<a href="evennia.contrib.tutorials.evadventure.html" title="evennia.contrib.tutorials.evadventure"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Evennia</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../Evennia-API.html" >API Summary</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="evennia-api.html" >evennia</a> &#187;</li>
<li class="nav-item nav-item-3"><a href="evennia.html" >evennia</a> &#187;</li>
<li class="nav-item nav-item-4"><a href="evennia.contrib.html" >evennia.contrib</a> &#187;</li>
<li class="nav-item nav-item-5"><a href="evennia.contrib.tutorials.html" >evennia.contrib.tutorials</a> &#187;</li>
<li class="nav-item nav-item-6"><a href="evennia.contrib.tutorials.evadventure.html" >evennia.contrib.tutorials.evadventure</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">evennia.contrib.tutorials.evadventure.ai</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2024, The Evennia developer community.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</body>
</html>