evennia/docs/1.0-dev/Contribs/Contrib-Crafting.html

398 lines
31 KiB
HTML
Raw Normal View History

2020-11-28 23:23:09 +01:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
2021-05-16 00:06:01 +02:00
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
2022-01-08 16:38:41 +01:00
<title>Crafting system &#8212; Evennia 1.0-dev documentation</title>
2020-11-28 23:23:09 +01:00
<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>
2021-10-26 21:41:11 +02:00
<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>
2020-11-28 23:23:09 +01:00
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
2022-01-08 16:38:41 +01:00
<link rel="search" title="Search" href="../search.html" />
2020-11-28 23:23:09 +01:00
</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="nav-item nav-item-0"><a href="../index.html">Evennia 1.0-dev</a> &#187;</li>
2022-01-08 16:38:41 +01:00
<li class="nav-item nav-item-this"><a href="">Crafting system</a></li>
2020-11-28 23:23:09 +01:00
</ul>
2021-06-23 18:58:26 +02:00
<div class="develop">develop branch</div>
2020-11-28 23:23:09 +01:00
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
2022-01-08 16:38:41 +01:00
<section class="tex2jax_ignore mathjax_ignore" id="crafting-system">
<h1>Crafting system<a class="headerlink" href="#crafting-system" title="Permalink to this headline"></a></h1>
<p>Contribution by Griatch 2020</p>
<p>This implements a full crafting system. The principle is that of a recipe,
where you combine items (tagged as ingredients) create something new. The recipe can also
require certain (non-consumed) tools. An example would be to use the bread recipe to
combine flour, water and yeast with an oven to bake a loaf of bread.</p>
<p>The recipe process can be understood like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>ingredient(s) + tool(s) + recipe -&gt; object(s)
</pre></div>
2020-11-28 23:23:09 +01:00
</div>
2022-01-08 16:38:41 +01:00
<p>Here, ingredients are consumed by the crafting process, whereas tools are
necessary for the process but will not be destroyed by it.</p>
<p>The included <code class="docutils literal notranslate"><span class="pre">craft</span></code> command works like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>craft &lt;recipe&gt; [from &lt;ingredient&gt;,...] [using &lt;tool&gt;, ...]
</pre></div>
</div>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline"></a></h2>
<p>Using the <code class="docutils literal notranslate"><span class="pre">craft</span></code> command:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>craft toy car from plank, wooden wheels, nails using saw, hammer
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>A recipe does not have to use tools or even multiple ingredients:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>snow + snowball_recipe -&gt; snowball
2020-11-28 23:23:09 +01:00
</pre></div>
</div>
2022-01-08 16:38:41 +01:00
<p>Conversely one could also imagine using tools without consumables, like</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>spell_book + wand + fireball_recipe -&gt; fireball
</pre></div>
</div>
<p>The system is generic enough to be used also for adventure-like puzzles (but
one would need to change the command and determine the recipe on based on what
is being combined instead):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>stick + string + hook -&gt; makeshift_fishing_rod
makeshift_fishing_rod + storm_drain -&gt; key
</pre></div>
</div>
<p>See the <a class="reference internal" href="../api/evennia.contrib.game_systems.crafting.example_recipes.html#evennia-contrib-game-systems-crafting-example-recipes"><span class="std std-ref">sword example</span></a> for an example
of how to design a recipe tree for crafting a sword from base elements.</p>
</section>
<section id="intallation-and-usage">
<h2>Intallation and Usage<a class="headerlink" href="#intallation-and-usage" title="Permalink to this headline"></a></h2>
<p>Import the <code class="docutils literal notranslate"><span class="pre">CmdCraft</span></code> command from evennia/contrib/crafting/crafting.py and
add it to your Character cmdset. Reload and the <code class="docutils literal notranslate"><span class="pre">craft</span></code> command will be
available to you:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>craft &lt;recipe&gt; [from &lt;ingredient&gt;,...] [using &lt;tool&gt;, ...]
</pre></div>
</div>
<p>In code, you can craft using the
<code class="docutils literal notranslate"><span class="pre">evennia.contrib.game_systems.crafting.craft</span></code> function:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">evennia.contrib.game_systems.crafting</span> <span class="kn">import</span> <span class="n">craft</span>
2020-11-28 23:23:09 +01:00
2021-02-27 20:21:31 +01:00
<span class="n">result</span> <span class="o">=</span> <span class="n">craft</span><span class="p">(</span><span class="n">caller</span><span class="p">,</span> <span class="s2">&quot;recipename&quot;</span><span class="p">,</span> <span class="o">*</span><span class="n">inputs</span><span class="p">)</span>
2021-10-26 21:41:11 +02:00
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>Here, <code class="docutils literal notranslate"><span class="pre">caller</span></code> is the one doing the crafting and <code class="docutils literal notranslate"><span class="pre">*inputs</span></code> is any combination of
consumables and/or tool Objects. The system will identify which is which by the
<a class="reference internal" href="../Components/Tags.html"><span class="doc std std-doc">Tags</span></a> on them (see below) The <code class="docutils literal notranslate"><span class="pre">result</span></code> is always a list.</p>
<p>To use crafting you need recipes. Add a new variable to
<code class="docutils literal notranslate"><span class="pre">mygame/server/conf/settings.py</span></code>:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>CRAFT_RECIPE_MODULES = [&#39;world.recipes&#39;]
</pre></div>
</div>
<p>All top-level classes in these modules (whose name does not start with <code class="docutils literal notranslate"><span class="pre">_</span></code>) will
be parsed by Evennia as recipes to make available to the crafting system. Using
the above example, create <code class="docutils literal notranslate"><span class="pre">mygame/world/recipes.py</span></code> and add your recipies in
there:</p>
<p>A quick example (read on for more details):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span>
<span class="kn">from</span> <span class="nn">evennia.contrib.game_systems.crafting</span> <span class="kn">import</span> <span class="n">CraftingRecipe</span><span class="p">,</span> <span class="n">CraftingValidationError</span>
<span class="k">class</span> <span class="nc">RecipeBread</span><span class="p">(</span><span class="n">CraftingRecipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Bread is good for making sandwitches!</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;bread&quot;</span> <span class="c1"># used to identify this recipe in &#39;craft&#39; command</span>
<span class="n">tool_tags</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;bowl&quot;</span><span class="p">,</span> <span class="s2">&quot;oven&quot;</span><span class="p">]</span>
<span class="n">consumable_tags</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;flour&quot;</span><span class="p">,</span> <span class="s2">&quot;salt&quot;</span><span class="p">,</span> <span class="s2">&quot;yeast&quot;</span><span class="p">,</span> <span class="s2">&quot;water&quot;</span><span class="p">]</span>
<span class="n">output_prototypes</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">{</span><span class="s2">&quot;key&quot;</span><span class="p">:</span> <span class="s2">&quot;Loaf of Bread&quot;</span><span class="p">,</span>
<span class="s2">&quot;aliases&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;bread&quot;</span><span class="p">],</span>
<span class="s2">&quot;desc&quot;</span><span class="p">:</span> <span class="s2">&quot;A nice load of bread.&quot;</span><span class="p">,</span>
<span class="s2">&quot;typeclass&quot;</span><span class="p">:</span> <span class="s2">&quot;typeclasses.objects.Food&quot;</span><span class="p">,</span> <span class="c1"># assuming this exists</span>
<span class="s2">&quot;tags&quot;</span><span class="p">:</span> <span class="p">[(</span><span class="s2">&quot;bread&quot;</span><span class="p">,</span> <span class="s2">&quot;crafting_material&quot;</span><span class="p">)]</span> <span class="c1"># this makes it usable in other recipes ...</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="k">def</span> <span class="nf">pre_craft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># validates inputs etc. Raise `CraftingValidationError` if fails</span>
<span class="k">def</span> <span class="nf">do_craft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># performs the craft - report errors directly to user and return None (if</span>
<span class="c1"># failed) and the created object(s) if successful.</span>
<span class="k">def</span> <span class="nf">post_craft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># any post-crafting effects. Always called, even if do_craft failed (the</span>
<span class="c1"># result would be None then)</span>
</pre></div>
</div>
</section>
2021-05-16 00:06:01 +02:00
<section id="adding-new-recipes">
2020-11-28 23:23:09 +01:00
<h2>Adding new recipes<a class="headerlink" href="#adding-new-recipes" title="Permalink to this headline"></a></h2>
2022-01-08 16:38:41 +01:00
<p>A <em>recipe</em> is a class inheriting from
<code class="docutils literal notranslate"><span class="pre">evennia.contrib.crafting.crafting.CraftingRecipe</span></code>. This class implements the
most common form of crafting - that using in-game objects. Each recipe is a
separate class which gets initialized with the consumables/tools you provide.</p>
<p>For the <code class="docutils literal notranslate"><span class="pre">craft</span></code> command to find your custom recipes, you need to tell Evennia
where they are. Add a new line to your <code class="docutils literal notranslate"><span class="pre">mygame/server/conf/settings.py</span></code> file,
with a list to any new modules with recipe classes.</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">CRAFT_RECIPE_MODULES</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;world.myrecipes&quot;</span><span class="p">]</span>
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>(You need to reload after adding this). All global-level classes in these
modules (whose names dont start with underscore) are considered by the system
as viable recipes.</p>
<p>Here we assume you created <code class="docutils literal notranslate"><span class="pre">mygame/world/myrecipes.py</span></code> to match the above
example setting:</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># in mygame/world/myrecipes.py</span>
2020-11-28 23:23:09 +01:00
<span class="kn">from</span> <span class="nn">evennia.contrib.crafting.crafting</span> <span class="kn">import</span> <span class="n">CraftingRecipe</span>
<span class="k">class</span> <span class="nc">WoodenPuppetRecipe</span><span class="p">(</span><span class="n">CraftingRecipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;A puppet&quot;&quot;&quot;</span><span class="s2">&quot;</span>
<span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;wooden puppet&quot;</span> <span class="c1"># name to refer to this recipe as</span>
<span class="n">tool_tags</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;knife&quot;</span><span class="p">]</span>
<span class="n">consumable_tags</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;wood&quot;</span><span class="p">]</span>
<span class="n">output_prototypes</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">{</span><span class="s2">&quot;key&quot;</span><span class="p">:</span> <span class="s2">&quot;A carved wooden doll&quot;</span><span class="p">,</span>
<span class="s2">&quot;typeclass&quot;</span><span class="p">:</span> <span class="s2">&quot;typeclasses.objects.decorations.Toys&quot;</span><span class="p">,</span>
2020-11-29 00:02:35 +01:00
<span class="s2">&quot;desc&quot;</span><span class="p">:</span> <span class="s2">&quot;A small carved doll&quot;</span><span class="p">}</span>
2021-05-15 00:35:21 +02:00
<span class="p">]</span>
2021-10-26 21:41:11 +02:00
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>This specifies which tags to look for in the inputs. It defines a
<a class="reference internal" href="../Components/Prototypes.html"><span class="doc std std-doc">Prototype</span></a> for the recipe to use to spawn the
result on the fly (a recipe could spawn more than one result if needed).
Instead of specifying the full prototype-dict, you could also just provide a
list of <code class="docutils literal notranslate"><span class="pre">prototype_key</span></code>s to existing prototypes you have.</p>
<p>After reloading the server, this recipe would now be available to use. To try it
we should create materials and tools to insert into the recipe.</p>
<p>The recipe analyzes inputs, looking for <a class="reference internal" href="../Components/Tags.html"><span class="doc std std-doc">Tags</span></a> with
specific tag-categories. The tag-category used can be set per-recipe using the
(<code class="docutils literal notranslate"><span class="pre">.consumable_tag_category</span></code> and <code class="docutils literal notranslate"><span class="pre">.tool_tag_category</span></code> respectively). The defaults
are <code class="docutils literal notranslate"><span class="pre">crafting_material</span></code> and <code class="docutils literal notranslate"><span class="pre">crafting_tool</span></code>. For
the puppet we need one object with the <code class="docutils literal notranslate"><span class="pre">wood</span></code> tag and another with the <code class="docutils literal notranslate"><span class="pre">knife</span></code>
tag:</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">evennia</span> <span class="kn">import</span> <span class="n">create_object</span>
2020-11-28 23:23:09 +01:00
<span class="n">knife</span> <span class="o">=</span> <span class="n">create_object</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="s2">&quot;Hobby knife&quot;</span><span class="p">,</span> <span class="n">tags</span><span class="o">=</span><span class="p">[(</span><span class="s2">&quot;knife&quot;</span><span class="p">,</span> <span class="s2">&quot;crafting_tool&quot;</span><span class="p">)])</span>
<span class="n">wood</span> <span class="o">=</span> <span class="n">create_object</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="s2">&quot;Piece of wood&quot;</span><span class="p">,</span> <span class="n">tags</span><span class="p">[(</span><span class="s2">&quot;wood&quot;</span><span class="p">,</span> <span class="s2">&quot;crafting_material&quot;</span><span class="p">)])</span>
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>Note that the objects can have any name, all that matters is the
tag/tag-category. This means if a “bayonet” also had the “knife” crafting tag,
it could also be used to carve a puppet. This is also potentially interesting
for use in puzzles and to allow users to experiment and find alternatives to
2020-11-28 23:23:09 +01:00
know ingredients.</p>
2020-11-29 00:02:35 +01:00
<p>By the way, there is also a simple shortcut for doing this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">tools</span><span class="p">,</span> <span class="n">consumables</span> <span class="o">=</span> <span class="n">WoodenPuppetRecipe</span><span class="o">.</span><span class="n">seed</span><span class="p">()</span>
</pre></div>
</div>
2022-01-08 16:38:41 +01:00
<p>The <code class="docutils literal notranslate"><span class="pre">seed</span></code> class-method will create simple dummy objects that fulfills the
recipes requirements. This is great for testing.</p>
<p>Assuming these objects were put in our inventory, we could now craft using the
in-game command:</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>&gt; craft wooden puppet from wood using hobby knife
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2020-11-28 23:23:09 +01:00
<p>In code we would do</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">evennia.contrub.crafting.crafting</span> <span class="kn">import</span> <span class="n">craft</span>
2020-11-28 23:23:09 +01:00
<span class="n">puppet</span> <span class="o">=</span> <span class="n">craft</span><span class="p">(</span><span class="n">crafter</span><span class="p">,</span> <span class="s2">&quot;wooden puppet&quot;</span><span class="p">,</span> <span class="n">knife</span><span class="p">,</span> <span class="n">wood</span><span class="p">)</span>
2021-10-26 21:41:11 +02:00
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>In the call to <code class="docutils literal notranslate"><span class="pre">craft</span></code>, the order of <code class="docutils literal notranslate"><span class="pre">knife</span></code> and <code class="docutils literal notranslate"><span class="pre">wood</span></code> doesnt matter - the
recipe will sort out which is which based on their tags.</p>
2021-05-16 00:06:01 +02:00
</section>
<section id="deeper-customization-of-recipes">
2020-11-28 23:23:09 +01:00
<h2>Deeper customization of recipes<a class="headerlink" href="#deeper-customization-of-recipes" title="Permalink to this headline"></a></h2>
2022-01-08 16:38:41 +01:00
<p>For customizing recipes further, it helps to understand how to use the
recipe-class directly:</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyRecipe</span><span class="p">(</span><span class="n">CraftingRecipe</span><span class="p">):</span>
2020-11-29 00:02:35 +01:00
<span class="c1"># ...</span>
2020-11-28 23:23:09 +01:00
<span class="n">tools</span><span class="p">,</span> <span class="n">consumables</span> <span class="o">=</span> <span class="n">MyRecipe</span><span class="o">.</span><span class="n">seed</span><span class="p">()</span>
<span class="n">recipe</span> <span class="o">=</span> <span class="n">MyRecipe</span><span class="p">(</span><span class="n">crafter</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="n">tools</span> <span class="o">+</span> <span class="n">consumables</span><span class="p">))</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">recipe</span><span class="o">.</span><span class="n">craft</span><span class="p">()</span>
2021-10-26 21:41:11 +02:00
2020-11-28 23:23:09 +01:00
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2022-01-08 16:38:41 +01:00
<p>This is useful for testing and allows you to use the class directly without
adding it to a module in <code class="docutils literal notranslate"><span class="pre">settings.CRAFTING_RECIPE_MODULES</span></code>.</p>
<p>Even without modifying more than the class properties, there are a lot of
options to set on the <code class="docutils literal notranslate"><span class="pre">CraftingRecipe</span></code> class. Easiest is to refer to the
<a class="reference internal" href="../api/evennia.contrib.game_systems.crafting.crafting.html#evennia.contrib.game_systems.crafting.crafting.CraftingRecipe" title="evennia.contrib.game_systems.crafting.crafting.CraftingRecipe"><span class="xref myst py py-class">CraftingRecipe api
documentation</span></a>. For example,
you can customize the validation-error messages, decide if the ingredients have
to be exactly right, if a failure still consumes the ingredients or not, and
much more.</p>
2020-11-28 23:23:09 +01:00
<p>For even more control you can override hooks in your own class:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">pre_craft</span></code> - this should handle input validation and store its data in <code class="docutils literal notranslate"><span class="pre">.validated_consumables</span></code> and
<code class="docutils literal notranslate"><span class="pre">validated_tools</span></code> respectively. On error, this reports the error to the crafter and raises the
<code class="docutils literal notranslate"><span class="pre">CraftingValidationError</span></code>.</p></li>
2021-05-15 00:35:21 +02:00
<li><p><code class="docutils literal notranslate"><span class="pre">craft</span></code> - this will only be called if <code class="docutils literal notranslate"><span class="pre">pre_craft</span></code> finished without an exception. This should
2020-11-28 23:23:09 +01:00
return the result of the crafting, by spawnging the prototypes. Or the empty list if crafting
fails for some reason. This is the place to add skill-checks or random chance if you need it
for your game.</p></li>
2021-05-15 00:35:21 +02:00
<li><p><code class="docutils literal notranslate"><span class="pre">post_craft</span></code> - this receives the result from <code class="docutils literal notranslate"><span class="pre">craft</span></code> and handles error messages and also deletes
2020-11-28 23:23:09 +01:00
any consumables as needed. It may also modify the result before returning it.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">msg</span></code> - this is a wrapper for <code class="docutils literal notranslate"><span class="pre">self.crafter.msg</span></code> and should be used to send messages to the
crafter. Centralizing this means you can also easily modify the sending style in one place later.</p></li>
</ul>
<p>The class constructor (and the <code class="docutils literal notranslate"><span class="pre">craft</span></code> access function) takes optional <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>. These are passed
into each crafting hook. These are unused by default but could be used to customize things per-call.</p>
2021-05-16 00:06:01 +02:00
<section id="skilled-crafters">
2020-11-28 23:23:09 +01:00
<h3>Skilled crafters<a class="headerlink" href="#skilled-crafters" title="Permalink to this headline"></a></h3>
2022-01-08 16:38:41 +01:00
<p>What the crafting system does not have out of the box is a skill system - the
notion of being able to fail the craft if you are not skilled enough. Just how
skills work is game-dependent, so to add this you need to make your own recipe
parent class and have your recipes inherit from this.</p>
2021-10-26 21:41:11 +02:00
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">randint</span>
2020-11-28 23:23:09 +01:00
<span class="kn">from</span> <span class="nn">evennia.contrib.crafting.crafting</span> <span class="kn">import</span> <span class="n">CraftingRecipe</span>
<span class="k">class</span> <span class="nc">SkillRecipe</span><span class="p">(</span><span class="n">CraftingRecipe</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;A recipe that considers skill&quot;&quot;&quot;</span>
<span class="n">difficulty</span> <span class="o">=</span> <span class="mi">20</span>
2021-05-15 00:35:21 +02:00
<span class="k">def</span> <span class="nf">craft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
2020-11-28 23:23:09 +01:00
<span class="sd">&quot;&quot;&quot;The input is ok. Determine if crafting succeeds&quot;&quot;&quot;</span>
<span class="c1"># this is set at initialization</span>
<span class="n">crafter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">crafte</span>
<span class="c1"># let&#39;s assume the skill is stored directly on the crafter</span>
<span class="c1"># - the skill is 0..100.</span>
<span class="n">crafting_skill</span> <span class="o">=</span> <span class="n">crafter</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">skill_crafting</span>
<span class="c1"># roll for success:</span>
<span class="k">if</span> <span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="p">(</span><span class="n">crafting_skill</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">difficulty</span><span class="p">):</span>
<span class="c1"># all is good, craft away</span>
2021-05-15 00:35:21 +02:00
<span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">craft</span><span class="p">()</span>
2020-11-28 23:23:09 +01:00
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">msg</span><span class="p">(</span><span class="s2">&quot;You are not good enough to craft this. Better luck next time!&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="p">[]</span>
</pre></div>
2021-10-26 21:41:11 +02:00
</div>
2020-11-28 23:23:09 +01:00
<p>In this example we introduce a <code class="docutils literal notranslate"><span class="pre">.difficulty</span></code> for the recipe and makes a dice roll to see
if we succed. We would of course make this a lot more immersive and detailed in a full game. In
principle you could customize each recipe just the way you want it, but you could also inherit from
a central parent like this to cut down on work.</p>
2022-01-08 16:38:41 +01:00
<p>The <a class="reference internal" href="../api/evennia.contrib.game_systems.crafting.example_recipes.html#evennia-contrib-game-systems-crafting-example-recipes"><span class="std std-ref">sword recipe example module</span></a> also shows an example
2020-11-28 23:23:09 +01:00
of a random skill-check being implemented in a parent and then inherited for multiple use.</p>
2021-05-16 00:06:01 +02:00
</section>
</section>
<section id="even-more-customization">
2020-11-28 23:23:09 +01:00
<h2>Even more customization<a class="headerlink" href="#even-more-customization" title="Permalink to this headline"></a></h2>
2020-11-29 00:02:35 +01:00
<p>If you want to build something even more custom (maybe using different input types of validation logic)
you could also look at the <code class="docutils literal notranslate"><span class="pre">CraftingRecipe</span></code> parent class <code class="docutils literal notranslate"><span class="pre">CraftingRecipeBase</span></code>.
It implements just the minimum needed to be a recipe and for big changes you may be better off starting
from this rather than the more opinionated <code class="docutils literal notranslate"><span class="pre">CraftingRecipe</span></code>.</p>
2022-01-08 16:38:41 +01:00
<hr class="docutils" />
<p><small>This document page is generated from <code class="docutils literal notranslate"><span class="pre">evennia/contrib/game_systems/crafting/README.md</span></code>. Changes to this
file will be overwritten, so edit that file rather than this one.</small></p>
2021-05-16 00:06:01 +02:00
</section>
</section>
2020-11-28 23:23:09 +01:00
<div class="clearer"></div>
</div>
</div>
</div>
<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>
<p><h3><a href="../index.html">Table of Contents</a></h3>
<ul>
2022-01-08 16:38:41 +01:00
<li><a class="reference internal" href="#">Crafting system</a><ul>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#intallation-and-usage">Intallation and Usage</a></li>
2020-11-28 23:23:09 +01:00
<li><a class="reference internal" href="#adding-new-recipes">Adding new recipes</a></li>
<li><a class="reference internal" href="#deeper-customization-of-recipes">Deeper customization of recipes</a><ul>
<li><a class="reference internal" href="#skilled-crafters">Skilled crafters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#even-more-customization">Even more customization</a></li>
</ul>
</li>
</ul>
<div role="note" aria-label="source link">
<!--h3>This Page</h3-->
<ul class="this-page-menu">
2022-01-08 16:38:41 +01:00
<li><a href="../_sources/Contribs/Contrib-Crafting.md.txt"
2020-11-28 23:23:09 +01:00
rel="nofollow">Show Page Source</a></li>
</ul>
2021-03-06 01:37:43 +01:00
</div><h3>Links</h3>
<ul>
<li><a href="https://www.evennia.com">Home page</a> </li>
<li><a href="https://github.com/evennia/evennia">Evennia Github</a> </li>
<li><a href="http://games.evennia.com">Game Index</a> </li>
2021-08-06 00:47:51 +02:00
<li>
<a href="https://discord.gg/AJJpcRUhtF">Discord</a> -
<a href="https://github.com/evennia/evennia/discussions">Discussions</a> -
2021-08-06 00:53:44 +02:00
<a href="https://evennia.blogspot.com/">Blog</a>
2021-03-06 01:37:43 +01:00
</li>
</ul>
2020-11-28 23:23:09 +01:00
<h3>Versions</h3>
<ul>
2022-01-08 16:38:41 +01:00
<li><a href="Contrib-Crafting.html">1.0-dev (develop branch)</a></li>
2020-11-28 23:23:09 +01:00
</ul>
</div>
</div>
<div class="clearer"></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="nav-item nav-item-0"><a href="../index.html">Evennia 1.0-dev</a> &#187;</li>
2022-01-08 16:38:41 +01:00
<li class="nav-item nav-item-this"><a href="">Crafting system</a></li>
2020-11-28 23:23:09 +01:00
</ul>
2021-06-23 18:58:26 +02:00
<div class="develop">develop branch</div>
2020-11-28 23:23:09 +01:00
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2020, The Evennia developer community.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
</div>
</body>
</html>