evennia/docs/2.x/api/evennia.contrib.tutorials.evadventure.ai.html
Evennia docbuilder action 8b50029b86 Updated HTML docs.
2023-11-18 13:00:34 +00:00

317 lines
No EOL
19 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>evennia.contrib.tutorials.evadventure.ai &#8212; Evennia 2.x 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="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 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="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 2.x</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="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="evennia.contrib.tutorials.evadventure.html"
title="previous chapter">evennia.contrib.tutorials.evadventure</a></p>
<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 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="evennia.contrib.tutorials.evadventure.ai.html">2.x (main branch)</a></li>
<ul>
<li><a href="../1.3.0/index.html">1.3.0 (v1.3.0 branch)</a></li>
<li><a href="../0.9.5/index.html">0.9.5 (v0.9.5 branch)</a></li>
</ul>
</div>
</div>
<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="Permalink to this headline"></a></h1>
<p>NPC AI module for EvAdventure (WIP)</p>
<p>This implements a state machine for the NPCs, where it uses inputs from the game to determine what
to do next. The AI works on the concept of being ticks, at which point, the AI will decide to move
between different states, performing different actions within each state until changing to
another state. The odds of changing between states and performing actions are weighted, allowing for
an AI agent to be more or less likely to perform certain actions.</p>
<p>The state machine is fed a dictionary of states and their transitions, and a dictionary of available
actions to choose between.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s2">&quot;states&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;state1&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;action1&quot;</span><span class="p">:</span> <span class="n">odds</span><span class="p">,</span> <span class="s2">&quot;action2&quot;</span><span class="p">:</span> <span class="n">odds</span><span class="p">,</span> <span class="o">...</span><span class="p">},</span>
<span class="s2">&quot;state2&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;action1&quot;</span><span class="p">:</span> <span class="n">odds</span><span class="p">,</span> <span class="s2">&quot;action2&quot;</span><span class="p">:</span> <span class="n">odds</span><span class="p">,</span> <span class="o">...</span><span class="p">},</span> <span class="o">...</span>
<span class="p">}</span>
<span class="s2">&quot;transition&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;state1&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;state2&quot;</span><span class="p">:</span> <span class="s2">&quot;odds, &quot;</span><span class="n">state3</span><span class="s2">&quot;: odds, ...},</span>
<span class="s2">&quot;state2&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;state1&quot;</span><span class="p">:</span> <span class="s2">&quot;odds, &quot;</span><span class="n">state3</span><span class="s2">&quot;: odds, ...}, ...</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The NPC class needs to look like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">NPC</span><span class="p">(</span><span class="n">DefaultCharacter</span><span class="p">):</span>
<span class="c1"># ...</span>
<span class="nd">@lazy_property</span>
<span class="k">def</span> <span class="nf">ai</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">AIHandler</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">ai_roam</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span>
<span class="c1"># perform the action within the current state ai.state</span>
<span class="k">def</span> <span class="nf">ai_hunt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span>
<span class="c1"># etc</span>
</pre></div>
</div>
<dl class="py class">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler">
<em class="property">class </em><code class="sig-prename descclassname">evennia.contrib.tutorials.evadventure.ai.</code><code class="sig-name descname">AIHandler</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">obj</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler" title="Permalink 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>AIHandler class. This should be placed on the NPC object, and will handle the state machine,
including transitions and actions.</p>
<p>Add to typeclass with &#64;lazyproperty:</p>
<blockquote>
<div><p>class NPC(DefaultCharacter):</p>
<blockquote>
<div><p>ai_states = {…}</p>
<p># …</p>
<p>&#64;lazyproperty
def ai(self):</p>
<blockquote>
<div><p>return AIHandler(self)</p>
</div></blockquote>
</div></blockquote>
</div></blockquote>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.__init__">
<code class="sig-name descname">__init__</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">obj</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">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.__init__" title="Permalink to this definition"></a></dt>
<dd><p>Initialize self. See help(type(self)) for accurate signature.</p>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.state">
<em class="property">property </em><code class="sig-name descname">state</code><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.state" title="Permalink to this definition"></a></dt>
<dd><p>Return the current state of the AI.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p><em>str</em> Current state of the AI.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.states">
<em class="property">property </em><code class="sig-name descname">states</code><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.states" title="Permalink to this definition"></a></dt>
<dd><p>Return the states dictionary for the AI.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p><em>dict</em> States dictionary for the AI.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.transitions">
<em class="property">property </em><code class="sig-name descname">transitions</code><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.transitions" title="Permalink to this definition"></a></dt>
<dd><p>Return the transitions dictionary for the AI.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p><em>dict</em> Transitions dictionary for the AI.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.add_aidict">
<code class="sig-name descname">add_aidict</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">aidict</span></em>, <em class="sig-param"><span class="n">force</span><span class="o">=</span><span class="default_value">False</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.add_aidict"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.add_aidict" title="Permalink to this definition"></a></dt>
<dd><p>Add an AI dictionary to the AI handler, if one doesnt already exist.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>aidict</strong> (<em>dict</em>) AI dictionary to add.</p></li>
<li><p><strong>force</strong> (<em>bool</em><em>, </em><em>optional</em>) Force adding the AI dictionary, even if one already exists on</p></li>
<li><p><strong>handler.</strong> (<em>this</em>) </p></li>
</ul>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.adjust_transition_probability">
<code class="sig-name descname">adjust_transition_probability</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">state_start</span></em>, <em class="sig-param"><span class="n">state_end</span></em>, <em class="sig-param"><span class="n">odds</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.adjust_transition_probability"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.adjust_transition_probability" title="Permalink to this definition"></a></dt>
<dd><p>Adjust the transition probability between two states.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>state_start</strong> (<em>str</em>) State to start from.</p></li>
<li><p><strong>state_end</strong> (<em>str</em>) State to end at.</p></li>
<li><p><strong>odds</strong> (<em>int</em>) New odds for the transition.</p></li>
</ul>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This will normalize the odds across the other transitions from the starting state.</p>
</div>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.get_next_state">
<code class="sig-name descname">get_next_state</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.get_next_state"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_next_state" title="Permalink to this definition"></a></dt>
<dd><p>Get the next state for the AI.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p><em>str</em> Next state for the AI.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.get_next_action">
<code class="sig-name descname">get_next_action</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.get_next_action"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.get_next_action" title="Permalink to this definition"></a></dt>
<dd><p>Get the next action for the AI within the current state.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p><em>str</em> Next action for the AI.</p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt id="evennia.contrib.tutorials.evadventure.ai.AIHandler.execute_ai">
<code class="sig-name descname">execute_ai</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/evennia/contrib/tutorials/evadventure/ai.html#AIHandler.execute_ai"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#evennia.contrib.tutorials.evadventure.ai.AIHandler.execute_ai" title="Permalink to this definition"></a></dt>
<dd><p>Execute the next ai action in the current state.</p>
<p>This assumes that each available state exists as a method on the object, named
ai_&lt;state_name&gt;, taking an optional argument of the next action to perform. The method
will itself update the state or transition weights through this handler.</p>
<p>Some states have in-built state transitions, via the special “change_state” action.</p>
</dd></dl>
</dd></dl>
</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="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 2.x</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 2023, The Evennia developer community.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
</div>
</body>
</html>