<spanid="evennia-contrib-rpg-dice-dice"></span><h1>evennia.contrib.rpg.dice.dice<aclass="headerlink"href="#module-evennia.contrib.rpg.dice.dice"title="Permalink to this headline">¶</a></h1>
<sectionid="dice">
<h2>Dice<aclass="headerlink"href="#dice"title="Permalink to this headline">¶</a></h2>
<p>Rolls dice for roleplaying, in-game gambling or GM:ing</p>
<p>Evennia contribution - Griatch 2012</p>
<p>This module implements a a dice-roller and a <strong>dice</strong>/<strong>roll</strong> command
to go with it. It uses standard RPG ‘d’-syntax (e.g. 2d6 to roll two
six-sided die) and also supports modifiers such as 3d6 + 5.</p>
<blockquote>
<div><p>> roll 1d20 + 2</p>
</div></blockquote>
<p>One can also specify a standard Python operator in order to specify
eventual target numbers and get results in a fair and guaranteed
unbiased way. For example a GM could (using the dice command) from
the start define the roll as 2d6 < 8 to show that a roll below 8 is
required to succeed. The command will normally echo this result to all
parties (although it also has options for hidden and secret rolls).</p>
</section>
<sectionid="installation">
<h2>Installation:<aclass="headerlink"href="#installation"title="Permalink to this headline">¶</a></h2>
<p>Add the <strong>CmdDice</strong> command from this module to your character’s cmdset
(and then restart the server):</p>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># in mygame/commands/default_cmdsets.py</span>
<h2>Usage:<aclass="headerlink"href="#usage"title="Permalink to this headline">¶</a></h2>
<blockquote>
<div><p>roll 1d100 + 10</p>
</div></blockquote>
<p>To roll dice in code, use the <strong>roll</strong> function from this module:</p>
<blockquote>
<div><p>from evennia.contrib.rpg import dice</p>
<p>dice.roll(“3d10 + 2”)</p>
</div></blockquote>
<p>If your system generates the dice dynamically you can also enter each part
of the roll separately:</p>
<blockquote>
<div><p>dice.roll(3, 10, (“+”, 2)) # 3d10 + 2</p>
</div></blockquote>
<dlclass="py function">
<dtid="evennia.contrib.rpg.dice.dice.roll">
<codeclass="sig-prename descclassname">evennia.contrib.rpg.dice.dice.</code><codeclass="sig-name descname">roll</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">dice</span></em>, <emclass="sig-param"><spanclass="n">dicetype</span><spanclass="o">=</span><spanclass="default_value">6</span></em>, <emclass="sig-param"><spanclass="n">modifier</span><spanclass="o">=</span><spanclass="default_value">None</span></em>, <emclass="sig-param"><spanclass="n">conditional</span><spanclass="o">=</span><spanclass="default_value">None</span></em>, <emclass="sig-param"><spanclass="n">return_tuple</span><spanclass="o">=</span><spanclass="default_value">False</span></em>, <emclass="sig-param"><spanclass="n">max_dicenum</span><spanclass="o">=</span><spanclass="default_value">10</span></em>, <emclass="sig-param"><spanclass="n">max_dicetype</span><spanclass="o">=</span><spanclass="default_value">1000</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/rpg/dice/dice.html#roll"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.roll"title="Permalink to this definition">¶</a></dt>
<dd><p>This is a standard dice roller.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><ulclass="simple">
<li><p><strong>dice</strong> (<em>int</em><em> or </em><em>str</em>) – If an <strong>int</strong>, this is the number of dice to roll, and <strong>dicetype</strong> is used
to determine the type. If a <strong>str</strong>, it should be on the form <strong>NdM</strong> where <strong>N</strong> is the number
of dice and <strong>M</strong> is the number of sides on each die. Also
<strong>NdM [modifier] [number] [conditional]</strong> is understood, e.g. <strong>1d6 + 3</strong>
or <strong>2d10 / 2 > 10</strong>.</p></li>
<li><p><strong>dicetype</strong> (<em>int</em><em>, </em><em>optional</em>) – Number of sides of the dice to be rolled. Ignored if
<strong>dice</strong> is a string.</p></li>
<li><p><strong>modifier</strong> (<em>tuple</em><em>, </em><em>optional</em>) – A tuple <strong>(operator, value)</strong>, where operator is
one of <strong>“+”</strong>, <strong>“-”</strong>, <strong>“/”</strong> or <strong>“*”</strong>. The result of the dice
roll(s) will be modified by this value. Ignored if <strong>dice</strong> is a string.</p></li>
<li><p><strong>conditional</strong> (<em>tuple</em><em>, </em><em>optional</em>) – A tuple <strong>(conditional, value)</strong>, where
conditional is one of <strong>“==”</strong>,**”<”<strong>,</strong>”>”<strong>,</strong>”>=”<strong>,</strong>”<=**” or “<strong>!=</strong>”.
Ignored if <strong>dice</strong> is a string.</p></li>
<li><p><strong>return_tuple</strong> (<em>bool</em>) – Return a tuple with all individual roll
results or not.</p></li>
<li><p><strong>max_dicenum</strong> (<em>int</em>) – The max number of dice to allow to be rolled.</p></li>
<li><p><strong>max_dicetype</strong> (<em>int</em>) – The max number of sides on the dice to roll.</p></li>
</ul>
</dd>
<dtclass="field-even">Returns</dt>
<ddclass="field-even"><p><em>int, bool or tuple</em>– By default, this is the result of the roll + modifiers. If
<strong>conditional</strong> is given, or <strong>dice</strong> is a string defining a conditional, then a True/False
value is returned. Finally, if <strong>return_tuple</strong> is set, this is a tuple
<strong>(result, outcome, diff, rolls)</strong>, where, <strong>result</strong> is the the normal result of the
roll + modifiers, <strong>outcome</strong> and <strong>diff</strong> are the boolean absolute difference between the roll
and the <strong>conditional</strong> input; both will be will be <strong>None</strong> if <strong>conditional</strong> is not set.
The <strong>rolls</strong> a tuple holding all the individual rolls (one or more depending on how many
dice were rolled).</p>
</dd>
<dtclass="field-odd">Raises</dt>
<ddclass="field-odd"><p><strong>TypeError if non-supported modifiers</strong><strong> or </strong><strong>conditionals are given.</strong>–</p>
</dd>
</dl>
<pclass="rubric">Notes</p>
<p>All input numbers are converted to integers.</p>
<pclass="rubric">Examples</p>
<dl>
<dt>::</dt><dd><p># string form
print roll(“3d6 + 2”)
10
print roll(“2d10 + 2 > 10”)
True
print roll(“2d20 - 2 >= 10”)
(8, False, 2, (4, 6)) # roll was 4 + 6 - 2 = 8</p>
<p># explicit arguments
print roll(2, 6) # 2d6
7
print roll(1, 100, (‘+’, 5) # 1d100 + 5
4
print roll(1, 20, conditional=(‘<’, 10) # let’say we roll 3
(8, False, 2, (4, 6)) # roll was 4 + 6 - 2 = 8</p>
</dd>
</dl>
</dd></dl>
<dlclass="py function">
<dtid="evennia.contrib.rpg.dice.dice.roll_dice">
<codeclass="sig-prename descclassname">evennia.contrib.rpg.dice.dice.</code><codeclass="sig-name descname">roll_dice</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">dice</span></em>, <emclass="sig-param"><spanclass="n">dicetype</span><spanclass="o">=</span><spanclass="default_value">6</span></em>, <emclass="sig-param"><spanclass="n">modifier</span><spanclass="o">=</span><spanclass="default_value">None</span></em>, <emclass="sig-param"><spanclass="n">conditional</span><spanclass="o">=</span><spanclass="default_value">None</span></em>, <emclass="sig-param"><spanclass="n">return_tuple</span><spanclass="o">=</span><spanclass="default_value">False</span></em>, <emclass="sig-param"><spanclass="n">max_dicenum</span><spanclass="o">=</span><spanclass="default_value">10</span></em>, <emclass="sig-param"><spanclass="n">max_dicetype</span><spanclass="o">=</span><spanclass="default_value">1000</span></em><spanclass="sig-paren">)</span><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.roll_dice"title="Permalink to this definition">¶</a></dt>
<dd><p>This is a standard dice roller.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><ulclass="simple">
<li><p><strong>dice</strong> (<em>int</em><em> or </em><em>str</em>) – If an <strong>int</strong>, this is the number of dice to roll, and <strong>dicetype</strong> is used
to determine the type. If a <strong>str</strong>, it should be on the form <strong>NdM</strong> where <strong>N</strong> is the number
of dice and <strong>M</strong> is the number of sides on each die. Also
<strong>NdM [modifier] [number] [conditional]</strong> is understood, e.g. <strong>1d6 + 3</strong>
or <strong>2d10 / 2 > 10</strong>.</p></li>
<li><p><strong>dicetype</strong> (<em>int</em><em>, </em><em>optional</em>) – Number of sides of the dice to be rolled. Ignored if
<strong>dice</strong> is a string.</p></li>
<li><p><strong>modifier</strong> (<em>tuple</em><em>, </em><em>optional</em>) – A tuple <strong>(operator, value)</strong>, where operator is
one of <strong>“+”</strong>, <strong>“-”</strong>, <strong>“/”</strong> or <strong>“*”</strong>. The result of the dice
roll(s) will be modified by this value. Ignored if <strong>dice</strong> is a string.</p></li>
<li><p><strong>conditional</strong> (<em>tuple</em><em>, </em><em>optional</em>) – A tuple <strong>(conditional, value)</strong>, where
conditional is one of <strong>“==”</strong>,**”<”<strong>,</strong>”>”<strong>,</strong>”>=”<strong>,</strong>”<=**” or “<strong>!=</strong>”.
Ignored if <strong>dice</strong> is a string.</p></li>
<li><p><strong>return_tuple</strong> (<em>bool</em>) – Return a tuple with all individual roll
results or not.</p></li>
<li><p><strong>max_dicenum</strong> (<em>int</em>) – The max number of dice to allow to be rolled.</p></li>
<li><p><strong>max_dicetype</strong> (<em>int</em>) – The max number of sides on the dice to roll.</p></li>
</ul>
</dd>
<dtclass="field-even">Returns</dt>
<ddclass="field-even"><p><em>int, bool or tuple</em>– By default, this is the result of the roll + modifiers. If
<strong>conditional</strong> is given, or <strong>dice</strong> is a string defining a conditional, then a True/False
value is returned. Finally, if <strong>return_tuple</strong> is set, this is a tuple
<strong>(result, outcome, diff, rolls)</strong>, where, <strong>result</strong> is the the normal result of the
roll + modifiers, <strong>outcome</strong> and <strong>diff</strong> are the boolean absolute difference between the roll
and the <strong>conditional</strong> input; both will be will be <strong>None</strong> if <strong>conditional</strong> is not set.
The <strong>rolls</strong> a tuple holding all the individual rolls (one or more depending on how many
dice were rolled).</p>
</dd>
<dtclass="field-odd">Raises</dt>
<ddclass="field-odd"><p><strong>TypeError if non-supported modifiers</strong><strong> or </strong><strong>conditionals are given.</strong>–</p>
</dd>
</dl>
<pclass="rubric">Notes</p>
<p>All input numbers are converted to integers.</p>
<pclass="rubric">Examples</p>
<dl>
<dt>::</dt><dd><p># string form
print roll(“3d6 + 2”)
10
print roll(“2d10 + 2 > 10”)
True
print roll(“2d20 - 2 >= 10”)
(8, False, 2, (4, 6)) # roll was 4 + 6 - 2 = 8</p>
<p># explicit arguments
print roll(2, 6) # 2d6
7
print roll(1, 100, (‘+’, 5) # 1d100 + 5
4
print roll(1, 20, conditional=(‘<’, 10) # let’say we roll 3
(8, False, 2, (4, 6)) # roll was 4 + 6 - 2 = 8</p>
</dd>
</dl>
</dd></dl>
<dlclass="py class">
<dtid="evennia.contrib.rpg.dice.dice.CmdDice">
<emclass="property">class </em><codeclass="sig-prename descclassname">evennia.contrib.rpg.dice.dice.</code><codeclass="sig-name descname">CmdDice</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="o">**</span><spanclass="n">kwargs</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/rpg/dice/dice.html#CmdDice"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">key</code><emclass="property"> = 'dice'</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.key"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">aliases</code><emclass="property"> = ['roll', '@dice']</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.aliases"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">locks</code><emclass="property"> = 'cmd:all()'</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.locks"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">func</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/rpg/dice/dice.html#CmdDice.func"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.func"title="Permalink to this definition">¶</a></dt>
<dd><p>Mostly parsing for calling the dice roller function</p>
<codeclass="sig-name descname">help_category</code><emclass="property"> = 'general'</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.help_category"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">lock_storage</code><emclass="property"> = 'cmd:all()'</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.lock_storage"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">search_index_entry</code><emclass="property"> = {'aliases': 'roll @dice', 'category': 'general', 'key': 'dice', 'no_prefix': ' roll dice', 'tags': '', 'text': "\n roll dice\n\n Usage:\n dice[/switch] <nr>d<sides> [modifier] [success condition]\n\n Switch:\n hidden - tell the room the roll is being done, but don't show the result\n secret - don't inform the room about neither roll nor result\n\n Examples:\n dice 3d6 + 4\n dice 1d100 - 2 < 50\n\n This will roll the given number of dice with given sides and modifiers.\n So e.g. 2d6 + 3 means to 'roll a 6-sided die 2 times and add the result,\n then add 3 to the total'.\n Accepted modifiers are +, -, * and /.\n A success condition is given as normal Python conditionals\n (<,>,<=,>=,==,!=). So e.g. 2d6 + 3 > 10 means that the roll will succeed\n only if the final result is above 8. If a success condition is given, the\n outcome (pass/fail) will be echoed along with how much it succeeded/failed\n with. The hidden/secret switches will hide all or parts of the roll from\n everyone but the person rolling.\n "}</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.CmdDice.search_index_entry"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><codeclass="sig-prename descclassname">evennia.contrib.rpg.dice.dice.</code><codeclass="sig-name descname">DiceCmdSet</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">cmdsetobj</span><spanclass="o">=</span><spanclass="default_value">None</span></em>, <emclass="sig-param"><spanclass="n">key</span><spanclass="o">=</span><spanclass="default_value">None</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/rpg/dice/dice.html#DiceCmdSet"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.DiceCmdSet"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">at_cmdset_creation</code><spanclass="sig-paren">(</span><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/contrib/rpg/dice/dice.html#DiceCmdSet.at_cmdset_creation"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.DiceCmdSet.at_cmdset_creation"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">path</code><emclass="property"> = 'evennia.contrib.rpg.dice.dice.DiceCmdSet'</em><aclass="headerlink"href="#evennia.contrib.rpg.dice.dice.DiceCmdSet.path"title="Permalink to this definition">¶</a></dt>