<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
<h1>Turn based Combat System<aclass="headerlink"href="#turn-based-combat-system"title="Permalink to this headline">¶</a></h1>
<p>This tutorial gives an example of a full, if simplified, combat system for Evennia. It was inspired
by the discussions held on the <aclass="reference external"href="https://groups.google.com/forum/#%21msg/evennia/wnJNM2sXSfs/-dbLRrgWnYMJ">mailing
list</a>.</p>
<sectionid="overview-of-combat-system-concepts">
<h2>Overview of combat system concepts<aclass="headerlink"href="#overview-of-combat-system-concepts"title="Permalink to this headline">¶</a></h2>
<p>Most MUDs will use some sort of combat system. There are several main variations:</p>
<ulclass="simple">
<li><p><em>Freeform</em> - the simplest form of combat to implement, common to MUSH-style roleplaying games. This means the system only supplies dice rollers or maybe commands to compare skills and spit out the result. Dice rolls are done to resolve combat according to the rules of the game and to direct the scene. A game master may be required to resolve rule disputes.</p></li>
<li><p><em>Twitch</em> - This is the traditional MUD hack&slash style combat. In a twitch system there is often no difference between your normal “move-around-and-explore mode” and the “combat mode”. You enter an attack command and the system will calculate if the attack hits and how much damage was caused. Normally attack commands have some sort of timeout or notion of recovery/balance to reduce the advantage of spamming or client scripting. Whereas the simplest systems just means entering <codeclass="docutils literal notranslate"><spanclass="pre">kill</span><spanclass="pre"><target></span></code> over and over, more sophisticated twitch systems include anything from defensive stances to tactical positioning.</p></li>
<li><p><em>Turn-based</em> - a turn based system means that the system pauses to make sure all combatants can choose their actions before continuing. In some systems, such entered actions happen immediately (like twitch-based) whereas in others the resolution happens simultaneously at the end of the turn. The disadvantage of a turn-based system is that the game must switch to a “combat mode” and one also needs to take special care of how to handle new combatants and the passage of time. The advantage is that success is not dependent on typing speed or of setting up quick client macros. This potentially allows for emoting as part of combat which is an advantage for roleplay-heavy games.</p></li>
</ul>
<p>To implement a freeform combat system all you need is a dice roller and a roleplaying rulebook. See <aclass="reference internal"href="../Contribs/Contrib-Dice.html"><spanclass="doc std std-doc">contrib/dice.py</span></a> for an example dice roller. To implement at twitch-based system you basically need a few combat <aclass="reference internal"href="../Components/Commands.html"><spanclass="doc std std-doc">commands</span></a>, possibly ones with a <aclass="reference internal"href="Howto-Command-Cooldown.html"><spanclass="doc std std-doc">cooldown</span></a>. You also need a <aclass="reference internal"href="Implementing-a-game-rule-system.html"><spanclass="doc std std-doc">game rule module</span></a> that makes use of it. We will focus on the turn-based variety here.</p>
</section>
<sectionid="tutorial-overview">
<h2>Tutorial overview<aclass="headerlink"href="#tutorial-overview"title="Permalink to this headline">¶</a></h2>
<p>This tutorial will implement the slightly more complex turn-based combat system. Our example has the following properties:</p>
<ulclass="simple">
<li><p>Combat is initiated with <codeclass="docutils literal notranslate"><spanclass="pre">attack</span><spanclass="pre"><target></span></code>, this initiates the combat mode.</p></li>
<li><p>Characters may join an ongoing battle using <codeclass="docutils literal notranslate"><spanclass="pre">attack</span><spanclass="pre"><target></span></code> against a character already in
combat.</p></li>
<li><p>Each turn every combating character will get to enter two commands, their internal order matters and they are compared one-to-one in the order given by each combatant. Use of <codeclass="docutils literal notranslate"><spanclass="pre">say</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">pose</span></code> is free.</p></li>
<li><p>The commands are (in our example) simple; they can either <codeclass="docutils literal notranslate"><spanclass="pre">hit</span><spanclass="pre"><target></span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">feint</span><spanclass="pre"><target></span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">parry</span><spanclass="pre"><target></span></code>. They can also <codeclass="docutils literal notranslate"><spanclass="pre">defend</span></code>, a generic passive defense. Finally they may choose to <codeclass="docutils literal notranslate"><spanclass="pre">disengage/flee</span></code>.</p></li>
<li><p>When attacking we use a classic [rock-paper-scissors](<aclass="reference external"href="https://en.wikipedia.org/wiki/Rock-paper-">https://en.wikipedia.org/wiki/Rock-paper-</a> scissors) mechanic to determine success: <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code> defeats <codeclass="docutils literal notranslate"><spanclass="pre">feint</span></code>, which defeats <codeclass="docutils literal notranslate"><spanclass="pre">parry</span></code> which defeats <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code>. <codeclass="docutils literal notranslate"><spanclass="pre">defend</span></code> is a general passive action that has a percentage chance to win against <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code> (only).</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">disengage/flee</span></code> must be entered two times in a row and will only succeed if there is no <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code> against them in that time. If so they will leave combat mode.</p></li>
<li><p>Once every player has entered two commands, all commands are resolved in order and the result is reported. A new turn then begins.</p></li>
<li><p>If players are too slow the turn will time out and any unset commands will be set to <codeclass="docutils literal notranslate"><spanclass="pre">defend</span></code>.</p></li>
</ul>
<p>For creating the combat system we will need the following components:</p>
<ulclass="simple">
<li><p>A combat handler. This is the main mechanic of the system. This is a <aclass="reference internal"href="../Components/Scripts.html"><spanclass="doc std std-doc">Script</span></a> object created for each combat. It is not assigned to a specific object but is shared by the combating characters and handles all the combat information. Since Scripts are database entities it also means that the combat will not be affected by a server reload.</p></li>
<li><p>A combat <aclass="reference internal"href="../Components/Command-Sets.html"><spanclass="doc std std-doc">command set</span></a> with the relevant commands needed for combat, such as the various attack/defend options and the <codeclass="docutils literal notranslate"><spanclass="pre">flee/disengage</span></code> command to leave the combat mode.</p></li>
<li><p>A rule resolution system. The basics of making such a module is described in the <aclass="reference internal"href="Implementing-a-game-rule-system.html"><spanclass="doc std std-doc">rule system tutorial</span></a>. We will only sketch such a module here for our end-turn combat resolution.</p></li>
<li><p>An <codeclass="docutils literal notranslate"><spanclass="pre">attack</span></code><aclass="reference internal"href="../Components/Commands.html"><spanclass="doc std std-doc">command</span></a> for initiating the combat mode. This is added to the default command set. It will create the combat handler and add the character(s) to it. It will also assign the combat command set to the characters.</p></li>
</ul>
</section>
<sectionid="the-combat-handler">
<h2>The combat handler<aclass="headerlink"href="#the-combat-handler"title="Permalink to this headline">¶</a></h2>
<p>The <em>combat handler</em> is implemented as a stand-alone <aclass="reference internal"href="../Components/Scripts.html"><spanclass="doc std std-doc">Script</span></a>. This Script is created when the first Character decides to attack another and is deleted when no one is fighting any more. Each handler represents one instance of combat and one combat only. Each instance of combat can hold any number of characters but each character can only be part of one combat at a time (a player would
need to disengage from the first combat before they could join another).</p>
<p>The reason we don’t store this Script “on” any specific character is because any character may leave the combat at any time. Instead the script holds references to all characters involved in the combat. Vice-versa, all characters holds a back-reference to the current combat handler. While we don’t use this very much here this might allow the combat commands on the characters to access and update the combat handler state directly.</p>
<p><em>Note: Another way to implement a combat handler would be to use a normal Python object and handle time-keeping with the <aclass="reference internal"href="../Components/TickerHandler.html"><spanclass="doc std std-doc">TickerHandler</span></a>. This would require either adding custom hook methods on the character or to implement a custom child of the TickerHandler class to track turns. Whereas the TickerHandler is easy to use, a Script offers more power in this case.</em></p>
<p>Here is a basic combat handler. Assuming our game folder is named <codeclass="docutils literal notranslate"><spanclass="pre">mygame</span></code>, we store it in
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">interval</span><spanclass="o">=</span><spanclass="mi">60</span><spanclass="o">*</span><spanclass="mi">2</span><spanclass="c1"># two minute timeout</span>
<spanclass="c1"># less than 2 characters in battle, kill this handler</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">msg_all</span><spanclass="p">(</span><spanclass="s2">"Combat has ended"</span><spanclass="p">)</span>
<p>This implements all the useful properties of our combat handler. This Script will survive a reboot
and will automatically re-assert itself when it comes back online. Even the current state of the
combat should be unaffected since it is saved in Attributes at every turn. An important part to note
is the use of the Script’s standard <codeclass="docutils literal notranslate"><spanclass="pre">at_repeat</span></code> hook and the <codeclass="docutils literal notranslate"><spanclass="pre">force_repeat</span></code> method to end each turn.
This allows for everything to go through the same mechanisms with minimal repetition of code.</p>
<p>What is not present in this handler is a way for players to view the actions they set or to change
their actions once they have been added (but before the last one has added theirs). We leave this as an exercise.</p>
</section>
<sectionid="combat-commands">
<h2>Combat commands<aclass="headerlink"href="#combat-commands"title="Permalink to this headline">¶</a></h2>
<p>Our combat commands - the commands that are to be available to us during the combat - are (in our example) very simple. In a full implementation the commands available might be determined by the weapon(s) held by the player or by which skills they know.</p>
<p>We create them in <codeclass="docutils literal notranslate"><spanclass="pre">mygame/commands/combat.py</span></code>.</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"Usage: hit <target>"</span><spanclass="p">)</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"You add 'hit' to the combat queue"</span><spanclass="p">)</span>
<spanclass="k">else</span><spanclass="p">:</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"You can only queue two actions per turn!"</span><spanclass="p">)</span>
<spanclass="c1"># tell the handler to check if turn is over</span>
<p>The other commands <codeclass="docutils literal notranslate"><spanclass="pre">CmdParry</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">CmdFeint</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">CmdDefend</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">CmdDisengage</span></code> look basically the same. We should also add a custom <codeclass="docutils literal notranslate"><spanclass="pre">help</span></code> command to list all the available combat commands and what they do.</p>
<p>We just need to put them all in a cmdset. We do this at the end of the same module:</p>
<h2>Rules module<aclass="headerlink"href="#rules-module"title="Permalink to this headline">¶</a></h2>
<p>A general way to implement a rule module is found in the [rule system tutorial](Implementing-a-game- rule-system). Proper resolution would likely require us to change our Characters to store things like strength, weapon skills and so on. So for this example we will settle for a very simplistic rock-paper-scissors kind of setup with some randomness thrown in. We will not deal with damage here but just announce the results of each turn. In a real system the Character objects would hold stats to affect their skills, their chosen weapon affect the choices, they would be able to lose health etc.</p>
<p>Within each turn, there are “sub-turns”, each consisting of one action per character. The actions within each sub-turn happens simultaneously and only once they have all been resolved we move on to the next sub-turn (or end the full turn).</p>
<p><em>Note: In our simple example the sub-turns don’t affect each other (except for <codeclass="docutils literal notranslate"><spanclass="pre">disengage/flee</span></code>), nor do any effects carry over between turns. The real power of a turn-based system would be to add
real tactical possibilities here though; For example if your hit got parried you could be out of
balance and your next action would be at a disadvantage. A successful feint would open up for a
subsequent attack and so on …</em></p>
<p>Our rock-paper-scissor setup works like this:</p>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code> beats <codeclass="docutils literal notranslate"><spanclass="pre">feint</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">flee/disengage</span></code>. It has a random chance to fail against <codeclass="docutils literal notranslate"><spanclass="pre">defend</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">feint</span></code> beats <codeclass="docutils literal notranslate"><spanclass="pre">parry</span></code> and is then counted as a <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">defend</span></code> does nothing but has a chance to beat <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">flee/disengage</span></code> must succeed two times in a row (i.e. not beaten by a <codeclass="docutils literal notranslate"><spanclass="pre">hit</span></code> once during the turn). If so the character leaves combat.</p></li>
<spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> tries to hit </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2">, but </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2"> parries the attack!"</span>
<spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2"> defends against the attack by </span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2">."</span>
<spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> stops </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2"> from disengaging, with a hit!"</span>
<spanclass="n">messages</span><spanclass="o">.</span><spanclass="n">append</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> parries the attack by </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2">."</span><spanclass="p">)</span>
<spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> tries to parry, but </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2"> feints and hits!"</span>
<spanclass="p">)</span>
<spanclass="k">else</span><spanclass="p">:</span>
<spanclass="n">messages</span><spanclass="o">.</span><spanclass="n">append</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> parries to no avail."</span><spanclass="p">)</span>
<spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> feints past </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2">'s parry, landing a hit!"</span>
<spanclass="n">messages</span><spanclass="o">.</span><spanclass="n">append</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> feints but is defeated by </span><spanclass="si">{</span><spanclass="n">tchar</span><spanclass="si">}</span><spanclass="s2">'s hit!"</span><spanclass="p">)</span>
<spanclass="k">else</span><spanclass="p">:</span>
<spanclass="n">messages</span><spanclass="o">.</span><spanclass="n">append</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> feints to no avail."</span><spanclass="p">)</span>
<spanclass="n">combat_handler</span><spanclass="o">.</span><spanclass="n">msg_all</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="n">char</span><spanclass="si">}</span><spanclass="s2"> withdraws from combat."</span><spanclass="p">)</span>
<p>To make it simple (and to save space), this example rule module actually resolves each interchange twice - first when it gets to each character and then again when handling the target. Also, since we use the combat handler’s <codeclass="docutils literal notranslate"><spanclass="pre">msg_all</span></code> method here, the system will get pretty spammy. To clean it up, one could imagine tracking all the possible interactions to make sure each pair is only handled and reported once.</p>
</section>
<sectionid="combat-initiator-command">
<h2>Combat initiator command<aclass="headerlink"href="#combat-initiator-command"title="Permalink to this headline">¶</a></h2>
<p>This is the last component we need, a command to initiate combat. This will tie everything together. We store this with the other combat commands.</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"You attack </span><spanclass="si">{</span><spanclass="n">target</span><spanclass="si">}</span><spanclass="s2">! You are in combat."</span><spanclass="p">)</span>
<spanclass="n">target</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"</span><spanclass="si">{</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="si">}</span><spanclass="s2"> attacks you! You are in combat."</span><spanclass="p">)</span>
</pre></div>
</div>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">attack</span></code> command will not go into the combat cmdset but rather into the default cmdset. See e.g. the <aclass="reference internal"href="Beginner-Tutorial/Part1/Beginner-Tutorial-Adding-Commands.html"><spanclass="doc std std-doc">Adding Command Tutorial</span></a> if you are unsure about how to do this.</p>
</section>
<sectionid="expanding-the-example">
<h2>Expanding the example<aclass="headerlink"href="#expanding-the-example"title="Permalink to this headline">¶</a></h2>
<p>At this point you should have a simple but flexible turn-based combat system. We have taken several shortcuts and simplifications in this example. The output to the players is likely too verbose during combat and too limited when it comes to informing about things surrounding it. Methods for changing your commands or list them, view who is in combat etc is likely needed - this will require play testing for each game and style. There is also currently no information displayed for other people happening to be in the same room as the combat - some less detailed information should probably be echoed to the room to
<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.