In this lesson we will be building on the [combat base](./Beginner-Tutorial-Combat-Base.md) to implement a combat system that works in turns and where you select your actions in a menu, like this:
2: Stunt - gain a later advantage against a target
3: Stunt - give an enemy disadvantage against yourself or an ally
4: Use an item on yourself or an ally
5: Use an item on an enemy
6: Wield/swap with an item from inventory
7: flee!
8: hold, doing nothing
Troll attacks You with Claws: Roll vs armor (12):
rolled 4 on d20 + strength(+3) vs 12 -> Fail
Troll missed you.
You use Potion of Strength.
Renewed strength coarses through your body!
Potion of Strength was used up.
```
> Note that this documentation doesn't show in-game colors. Also, if you interested in an alternative, see the [previous lesson](./Beginner-Tutorial-Combat-Twitch.md) where we implemented a 'twitch'-like combat system based on entering direct commands for every action.
With 'turnbased' combat, we mean combat that 'ticks' along at a slower pace, slow enough to allow the participants to select their options in a menu (the menu is not strictly necessary, but it's a good way to learn how to make menus as well). Their actions are queued and will be executed when the turn timer runs out. To avoid waiting unnecessarily, we will also move on to the next round whenever everyone has made their choices.
The advantage of a turnbased system is that it removes player speed from the equation; your prowess in combat does not depend on how quickly you can enter a command. For RPG-heavy games you could also allow players time to make RP emotes during the rounds of combat to flesh out the action.
The advantage of using a menu is that you have all possible actions directly available to you, making it beginner friendly and easy to know what you can do. It also means a lot less writing which can be an advantage to some players.
## General Principle
```{sidebar}
An example of an implemented Turnbased combat system can be found in [evennia/contrib/tutorials/evadventure/combat_turnbased.py](evennia.contrib.tutorials.evadventure.combat_turnbased).
```
Here is the general principle of the Turnbased combat handler:
- The turnbased version of the CombatHandler will be stored on the _current location_. That means that there will only be one combat per location. Anyone else starting combat will join the same handler and be assigned a side to fight on.
- The handler will run a central timer of 30s (in this example). When it fires, all queued actions will be executed. If everyone has submitted their actions, this will happen immediately when the last one submits.
- While in combat you will not be able to move around - you are stuck in the room. Fleeing combat is a separate action that takes a few turns to complete (we will need to create this).
- Starting the combat is done via the `attack <target>` command. After that you are in the combat menu and will use the menu for all subsequent actions.
## Turnbased combat handler
> Create a new module `evadventure/combat_turnbased.py`.
```python
# in evadventure/combat_turnbased.py
from .combat_base import (
CombatActionAttack,
CombatActionHold,
CombatActionStunt,
CombatActionUseItem,
CombatActionWield,
EvAdventureCombatBaseHandler,
)
from .combat_base import EvAdventureCombatBaseHandler
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
# track who is fleeing as {combatant: turn_they_started_fleeing}
fleeing_combatants = AttributeProperty(dict)
# list of who has been defeated so far
defeated_combatants = AttributeProperty(list)
```
We leave a placeholder for the `"flee"` action since we haven't created it yet.
Since the turnbased combat handler is shared between all combatants, we need to store references to those combatants on the handler, in the `combatants` [Attribute](Attribute). In the same way we must store a _matrix_ of who has advantage/disadvantage against whom. We must also track who is _fleeing_, in particular how long they have been fleeing, since they will be leaving combat after that time.
### Getting the sides of combat
The two sides are different depending on if we are in an [PvP room](./Beginner-Tutorial-Rooms.md) or not: In a PvP room everyone else is your enemy. Otherwise only NPCs in combat is your enemy (you are assumed to be teaming up with your fellow players).
```python
# in evadventure/combat_turnbased.py
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
# ...
def get_sides(self, combatant):
"""
Get a listing of the two 'sides' of this combat,
m the perspective of the provided combatant.
"""
if self.obj.allow_pvp:
# in pvp, everyone else is an ememy
allies = [combatant]
enemies = [comb for comb in self.combatants if comb != combatant]
else:
# otherwise, enemies/allies depend on who combatant is
pcs = [comb for comb in self.combatants if inherits_from(comb, EvAdventureCharacter)]
npcs = [comb for comb in self.combatants if comb not in pcs]
if combatant in pcs:
# combatant is a PC, so NPCs are all enemies
allies = [comb for comb in pcs if comb != combatant]
enemies = npcs
else:
# combatant is an NPC, so PCs are all enemies
allies = [comb for comb in npcs if comb != combatant]
enemies = pcs
return allies, enemies
```
Note that since the `EvadventureCombatBaseHandler` (which our turnbased handler is based on) is a [Script](../../../Components/Scripts.md), it provides many useful features. For example `self.obj` is the entity on which this Script 'sits'. Since we are planning to put this handler on the current location, then `self.obj` will be that Room.
All we do here is check if it's a PvP room or not and use this to figure out who would be an ally or an enemy. Note that the `combatant` is _not_ included in the `allies` return - we'll need to remember this.
### Tracking Advantage/Disadvantage
```python
# in evadventure/combat_turnbased.py
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
We use the `advantage/disadvantage_matrix` Attributes to track who has advantage against whom.
```{sidebar} .pop()
The Python `.pop()` method exists on lists and dicts as well as some other iterables. It 'pops' and returns an element from the container. For a list, it's either popped by index or by popping the last element. For a dict (like here), a specific key must be given to pop. If you don't provide a default value as a second element, an error will be raised if the key you try to pop is not found.
```
In the `has dis/advantage` methods we `pop` the target from the matrix which will result either in the value `True` or `False` (the default value we give to `pop` if the target is not in the matrix). This means that the advantage, once gained, can only be used once.
We also consider everyone to have advantage against fleeing combatants.
### Adding and removing combatants
Since the combat handler is shared we must be able to add- and remove combatants easily.
This is new compared to the base handler.
```python
# in evadventure/combat_turnbased.py
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
# ...
def add_combatant(self, combatant):
"""
Add a new combatant to the battle. Can be called multiple times safely.
We simply add the the combatant with the fallback action-dict to start with. We return a `bool` from `add_combatant` so that the calling function will know if they were actually added anew or not (we may want to do some extra setup if they are new).
For now we just `pop` the combatant, but in the future we'll need to do some extra cleanup of the menu when combat ends (we'll get to that).
### Flee Action
Since you can't just move away from the room to flee turnbased combat, we need to add a new `CombatAction` subclass like the ones we created in the [base combat lesson](./Beginner-Tutorial-Combat-Base.md#actions).
```python
# in evadventure/combat_turnbased.py
from .combat_base import CombatAction
# ...
class CombatActionFlee(CombatAction):
"""
Start (or continue) fleeing/disengaging from combat.
action_dict = {
"key": "flee",
}
"""
def execute(self):
combathandler = self.combathandler
if self.combatant not in combathandler.fleeing_combatants:
"$You() $conj(retreat), being exposed to attack while doing so (will escape in "
f"{time_left} $pluralize(turn, {time_left}))."
)
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
action_classes = {
"hold": CombatActionHold,
"attack": CombatActionAttack,
"stunt": CombatActionStunt,
"use": CombatActionUseItem,
"wield": CombatActionWield,
"flee": CombatActionFlee # <----added!
}
# ...
```
We create the action to make use of the `fleeing_combatants` dict we set up in the combat handler. This dict stores the fleeing combatant along with the `turn` its fleeing started. If performing the `flee` action multiple times, we will just display how many turns are remaining.
Finally, we make sure to add our new `CombatActionFlee` to the `action_classes` registry on the combat handler.
### Queue action
```python
# in evadventure/combat_turnbased.py
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
# ...
def queue_action(self, combatant, action_dict):
self.combatants[combatant] = action_dict
# track who inserted actions this turn (non-persistent)
did_action = set(self.ndb.did_action or set())
did_action.add(combatant)
if len(did_action) >= len(self.combatants):
# everyone has inserted an action. Start next turn without waiting!
self.force_repeat()
```
To queue an action, we simply store its `action_dict` with the combatant in the `combatants` Attribute.
We use a Python `set()` to track who has queued an action this turn. If all combatants have entered a new (or renewed) action this turn, we use the `.force_repeat()` method, which is available on all [Scripts](../../../Components/Scripts.md). When this is called, the next round will fire immediately instead of waiting until it times out.
### Execute an action and tick the round
```{code-block} python
:linenos:
:emphasize-lines: 13,17,31
# in evadventure/combat_turnbased.py
import random
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
(every `interval` seconds). Performs a full turn of
combat, performing everyone's actions in random order.
"""
self.turn += 1
# random turn order
combatants = list(self.combatants.keys())
random.shuffle(combatants) # shuffles in place
# do everyone's next queued combat action
for combatant in combatants:
self.execute_next_action(combatant)
self.ndb.did_action = set()
# check if one side won the battle
self.check_stop_combat()
```
Our action-execution consists of two parts - the `execute_next_action` (which was defined in the parent class for us to implement) and the `at_repeat` method which is a part of the [Script](../../../Components/Scripts.md)
For `execute_next_action` :
- **Line 11**: We get the `action_dict` from the `combatants` Attribute. We return the `fallback_action_dict` if nothing was queued (this defaults to `hold`).
- **Line 14**: We use the `key` of the `action_dict` (which would be something like "attack", "use", "wield" etc) to get the class of the matching Action from the `action_classes` dictionary.
- **Line 15**: Here the action class is instantiated with the combatant and action dict, making it ready to execute. This is then on the following lines.
The `at_repeat` is called repeatedly every `interval` seconds that the Script fires. This is what we use to track when each round ends.
- **Lines 28-33**: In this example, we have no internal order between actions. So we simply randomize in which order they fire.
- **Line 35**: We set this `set` in the `queue_action` to know when everyone submitted a new action. We must make sure to unset it here before the next round.
# if one way or another, there are no more enemies to fight
still_standing = list_to_string(f"$You({comb.key})" for comb in allies)
knocked_out = list_to_string(comb for comb in self.defeated_combatants if comb.hp > 0)
killed = list_to_string(comb for comb in self.defeated_combatants if comb.hp <= 0)
if still_standing:
txt = [f"The combat is over. {still_standing} are still standing."]
else:
txt = ["The combat is over. No-one stands as the victor."]
if knocked_out:
txt.append(f"{knocked_out} were taken down, but will live.")
if killed:
txt.append(f"{killed} were killed.")
self.msg(txt)
self.stop_combat()
```
The `check_stop_combat` is called at the end of the round. We want to figure out who is dead and if one of the 'sides' won.
- **Lines 28-38**: We go over all combatants and determine if they are out of HP. If so we fire the relevant hooks and add them to the `defeated_combatants` Attribute.
- **Line 38**: For all surviving combatants, we make sure give them the `fallback_action_dict`.
- **Lines 41-46**: The `fleeing_combatant` Attribute is a dict on the form `{fleeing_combatant: turn_number}`, tracking when they first started fleeing. We compare this with the current turn number and the `flee_timeout` to see if they now flee and should be allowed to be removed from combat.
- **Lines 49-56**: Here on we are determining if one 'side' of the conflict has defeated the other side.
- **Line 60**: The `list_to_string` Evennia utility converts a list of entries, like `["a", "b", "c"` to a nice string `"a, b and c"`. We use this to be able to present some nice ending messages to the combatants.
### Start combat
Since we are using the timer-component of the [Script](../../../Components/Scripts.md) to tick our combat, we also need a helper method to 'start' that.
```python
from evennia.utils.utils import list_to_string
# in evadventure/combat_turnbased.py
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
# ...
def start_combat(self, **kwargs):
"""
This actually starts the combat. It's safe to run this multiple times
since it will only start combat if it isn't already running.
"""
if not self.is_active:
self.start(**kwargs)
```
The `start(**kwargs)` method is a method on the Script, and will make it start to call `at_repeat` every `interval` seconds. We will pass that `interval` inside `kwargs` (so for example, we'll do `combathandler.start_combat(interval=30)` later).
## Using EvMenu for the combat menu
The [EvMenu](../../../Components/EvMenu.md) is used to create in-game menues in Evennia. We used a simple EvMenu already in the [Character Generation Lesson](./Beginner-Tutorial-Chargen.md). This time we'll need to be a bit more advanced. While EvMenu is described in detail on its own page, we will give a quick overview of how it works here.
An EvMenu is made up of _nodes_, which are regular functions on this form:
"desc": "Describing what happens when choosing this option."
"goto": "name of the node to go to" # OR (callable, {kwargs}}) returning said name
},
# other options here
]
return text, options
```
> There are more possibilities, described in the main [EvMenu docs](../../../Components/EvMenu.md),
So basically each node takes the arguments of `caller` (the one using the menu), `raw_string` (the empty string or what the user input on the _previous node_) and `**kwargs` which can be used to pass data from node to node. It returns `text` and `options`.
The `text` is what the user will see when entering this part of the menu, such as "Choose who you want to attack!". The `options` is a list of dicts describing each option. They will appear as a multi-choice list below the node text (see the example at the top of this lesson page).
When we create the EvMenu later, we will create a _node index_ - a mapping between a unique name and these "node functions". So something like this:
```python
# example of a EvMenu node index
{
"start": node_combat_main,
"node1": node_func1,
"node2": node_func2,
"some name": node_somenodename,
"end": node_abort_menu,
}
```
Each `option` dict has a key `"goto"` that determines which node the player should jump to if they choose that option. Inside the menu, each node needs to be referenced with these names (like `"start"`, `"node1"` etc).
The `"goto"` value of each option can either specify the name directly (like `"node1"`) _or_ it can be given as a tuple `(callable, {keywords})`. This `callable` is _called_ and is expected to in turn return the next node-name to use (like `"node1"`).
The `callable` (often called a "goto callable") looks very similar to a node function:
# do whatever is needed to determine the next node
return nodename # also nodename, dict works
```
Here, `caller` is still the one using the menu and `raw_string` is the actual string you entered to choose this option. `**kwargs` is the keywords you added to the `(callable, {keywords})` tuple.
The goto-callable must return the name of the next node. Optionally, you can return both `nodename, {kwargs}`. If you do the next node will get those kwargs as ingoing `**kwargs`. This way you can pass information from one node to the next. A special feature is that if `nodename` is returned as `None`, then the _current_ node will be _rerun_ again.
> To make node functions clearly separate from goto-callables, Evennia docs prefix the first with `node_...` and the latter with `_`.
Here's an example of how the goto-callable and node-function hang together:
Our combat menu will be pretty simple. We will have one central menu node with options indicating all the different actions of combat. When choosing an option, the player should be asked a series of question, each specifying one piece of information needed for that action. The last step will be the build this information into an `action-dict` we can queue with the combathandler.
To understand the process, here's how the action selection will work:
Looking at the above table we can see that we have a lot of re-use. The selection of allied/enemy/target/recipient/item are represent nodes that are reused between different actions.
Each of these actions also follow a linear sequence, like the step-by step 'wizard' you see in some software. We want to be able to step back and forth in each sequence, and also abort the action if you change your mind along the way.
After queueing the action, we should always go back to the start node where we will wait until the round ends and all actions are executed.
We will create a few helpers to make our particular menu easy to work with.
### The node index
These are the nodes we need for our menu:
```python
# not coded anywhere yet, just noting for reference
All callables are left as `None` since we haven't created them yet. But it's good to note down the expected names because we need them in order to jump from node to node. The important one to note is that `node_combat` will be the base node we should get back to over and over.
We only add this to not have to write as much when calling this later. We pass `caller.location`, which is what retrieves/creates the combathandler on the current location. The `interval` is how often the combathandler (which is a [Script](../../../Components/Scripts.md)) will call its `at_repeat` method. We set the `flee_time` Attribute at the same time.
### Queue an action
This is our first "goto function", the function that when execute when we select the last option in the 'wizard' of each Action. This should queue the action-dict and return us to the `combat_node`.
We make one assumption here - that `kwargs` contains the `action-dict` key with the action-dict built up by previous steps. We get the combathandler and queues it.
Since this is a goto-callable, we must return the next node to go to. Since this is the last step, we will always go back to the `node_combat` base node, so that's what we return.
### Rerun a node
A special feature of goto callables is the ability to rerun the same node by returning `None`.
Using this in an option will rerun the current node, but will preserve the `kwargs` that were sent in.
### Stepping through the wizard
Our particualr menu is very symmetric - you select an option and then you will just select a series of option before you come back. So we will make another goto-function to help us easily do this. To understand, let's first show how we plan to use this:
```python
# in the base combat-node function (just shown as an example)
When the user chooses to use an item on an enemy, will call `_step_wizard` with two keywords `steps` and `action_dict`. The first is the names of the menu nodes we will step through for this action. The latter is the `action_dict` that will eventually end up in the [`_queue_action`](#queue-an-action) goto function we defined earlier.
Furthermore, we want the ability to go "back" to the previous node like this:
Note the use of `**` here - `{**dict1, **dict2}` is a powerful one-liner way to combine two dicts into one. This preserves (and passes on) the incoming `kwargs` and just adds a new key "step" to it.
So let's implement a `_step_wizard` goto-function to handle this!
This depends on passing around `steps`, `step` and `istep` with the `**kwargs`. If `step` is "back" then we will go back in the sequence of `steps` otherwise forward. We increase/decrease the `istep` key value to track just where we are.
If we reach the end we call our `_queue_action` helper function directly. If we back up to the beginning we return to the base node.
We will make one final helper function, to quickly add the `back` (and `abort`) options to the nodes that need it:
This is not a goto-function, it's just a helper that we will call to quickly add these extra options a node's option list and not have to type it out over and over.
As we've seen before, the `back` option will use the `_step_wizard` to step back in the wizard. The `abort` option will simply jump back to the main node, aborting the wizard.
The `_default` option is special. This option key tells EvMenu: "use this option if none of the other match". That is, if they enter an empty input or garbage, we will just re-display the node. We make sure pass along the `kwargs` though, so we don't lose any information of where we were in the wizard.
Finally we are ready to write our menu nodes!
### Choosing targets and recipients
These nodes all work the same: They should present a list of suitable targets/recipients to choose from and then put that result in the action-dict as either `target` or `recipient` key.
# almost the same, except using allies + yourself and storing "recipient"
```
- **Line 11**: Here we use `combathandler.get_sides(caller)` to get the 'enemies' from the perspective of `caller` (the one using the menu).
- **Line 13-31**: This is a loop over all enemies we found.
- **Line 15**: We use `target.get_display_name(caller)`. This method (a default method on all Evennia `Objects`) allows the target to return a name while being aware of who's asking. It's what makes an admin see `Name (#5)` while a regular user just sees `Name`. If you didn't care about that, you could just do `target.key` here.
- **Line 18**: This line looks complex, but remember that `{**dict1, **dict2}` is a one-line way to merge two dicts together. What this does is to do this in three steps:
- First we add `action_dict` together with a dict `{"target": target}`. This has the same effect as doing `action_dict["target"] = target`, except we create a new dict out of the merger.
- Next we take this new merger and creates a new dict `{"action_dict": new_action_dict}`.
- Finally we merge this with the existing `kwargs` dict. The result is a new dict that now has the updated `"action_dict"` key pointing to an action-dict where `target` is set.
- **Line 23**: We extend the `options` list with the default wizard options (`back`, `abort`). Since we made a helper function for this, this is only one line.
Creating the three other needed nodes `node_choose_enemy_recipient`, `node_choose_allied_target` and `node_choose_allied_recipient` are following the same pattern; they just use either the `allies` or `enemies` return from `combathandler.get_sides()` (for the `allies`, don't forget to add `caller` so you can target yourself!). It then sets either the `target` or `recipient` field in the `action_dict`. We leave these up to the reader to implement.
### Choose an Ability
For Stunts, we need to be able to select which _Knave_ Ability (STR, DEX etc) you want to boost/foil.
The principle is the same as for the target/recipient-setter nodes, except that we just provide a list of the abilities to choose from. We update the `stunt_type` and `defense_type` keys in the `action_dict`, as needed by the Stunt action.
# same except using caller.equipment.get_wieldable_objects_from_backpack()
```
Our [equipment handler](./Beginner-Tutorial-Equipment.md) has the very useful help method `.get_usable_objects_from_backpack`. We just call this to get a list of all the items we want to choose. Otherwise this node should look pretty familiar by now.
The `node_choose_wiqld_item` is very similar, except it uses `caller.equipment.get_wieldable_objects_from_backpack()` instead. We'll leave the implementation of this up to the reader.
### The main menu node
This ties it all together.
```python
# in evadventure/combat_turnbased.py
# ...
def node_combat(caller, raw_string, **kwargs):
"""Base combat menu"""
combathandler = _get_combathandler(caller)
text = combathandler.get_combat_summary(caller)
options = [
{
"desc": "attack an enemy",
"goto": (
_step_wizard,
{
"steps": ["node_choose_enemy_target"],
"action_dict": {"key": "attack", "target": None},
},
),
},
{
"desc": "Stunt - gain a later advantage against a target",
This starts off the `_step_wizard` for each action choice. It also lays out the `action_dict` for every action, leaving `None` values for the fields that will be set by the following nodes.
## Attack Command
We will only need one single Command to run the Turnbased combat system. This is the `attack` command. Once you use it once, you will be in the menu.
```python
# in evadventure/combat_turnbased.py
from evennia import Command, CmdSet, EvMenu
# ...
class CmdTurnAttack(Command):
"""
Start or join combat.
Usage:
attack [<target>]
"""
key = "attack"
aliases = ["hit", "turnbased combat"]
turn_timeout = 30 # seconds
flee_time = 3 # rounds
def parse(self):
super().parse()
self.args = self.args.strip()
def func(self):
if not self.args:
self.msg("What are you attacking?")
return
target = self.caller.search(self.args)
if not target:
return
if not hasattr(target, "hp"):
self.msg("You can't attack that.")
return
elif target.hp <= 0:
self.msg(f"{target.get_display_name(self.caller)} is already down.")
return
if target.is_pc and not target.location.allow_pvp:
self.msg("PvP combat is not allowed here!")
return
combathandler = _get_combathandler(
self.caller, self.turn_timeout, self.flee_time)
# add combatants to combathandler. this can be done safely over and over
The `attack target` Command will determine if the target has health (only things with health can be attacked) and that the room allows fighting. If the target is a pc, it will check so PvP is allowed.
It then proceeds to either start up a new command handler or reuse a new one, while adding the attacker and target to it. If the target was already in combat, this does nothing (same with the `.start_combat()` call).
As we create the `EvMenu`, we pass it the "menu index" we talked to about earlier, now with the actual node functions in every slot. We make the menu persistent so it survives a reload.
To make the command available, add the `TurnCombatCmdSet` to the Character's default cmdset.
## Making sure the menu stops
The combat can end for a bunch of reasons. When that happens, we must make sure to clean up the menu so we go back normal operation. We will add this to the `remove_combatant` method on the combat handler (we left a TODO there before):
```python
# in evadventure/combat_turnbased.py
# ...
class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
# ...
def remove_combatant(self, combatant):
"""
Remove a combatant from the battle.
"""
self.combatants.pop(combatant, None)
# clean up menu if it exists
if combatant.ndb._evmenu: # <---new
combatant.ndb._evmenu.close_menu() # ''
```
When the evmenu is active, it is avaiable on its user as `.ndb._evmenu` (see the EvMenu docs). When we are removed from combat, we use this to get the evmenu and call its `close_menu()` method to shut down the menu.
Our turnbased combat system is complete!
## Testing
Unit testing of the Turnbased combat handler is straight forward, you follow the process of earlier lessons to test that each method on the handler returns what you expect with mocked inputs.
Unit-testing the menu is more complex. You will find examples of doing this in [evennia.utils.tests.test_evmenu](evennia.utils.tests.test_evmenu).
## A small combat test
Unit testing the code is not enough to see that combat works. We need to also make a little 'functional' test to see how it works in practice.
This is what we need for a minimal test:
- A room with combat enabled.
- An NPC to attack (it won't do anything back yet since we haven't added any AI)
- A weapon we can `wield`.
- An item (like a potion) we can `use`.
```{sidebar}
You can find an example batch-code script in [evennia/contrib/tutorials/evadventure/batchscripts/turnbased_combat_demo.ev](evennia.contrib.tutorials.evadventure.batchscripts)
```
In [The Twitch combat lesson](./Beginner-Tutorial-Combat-Twitch.md) we used a [batch-command script](../../../Components/Batch-Command-Processor.md) to create the testing environment in game. This runs in-game Evennia commands in sequence. For demonstration purposes we'll instead use a [batch-code script](../../../Components/Batch-Code-Processor.md), which runs raw Python code in a repeatable way. A batch-code script is much more flexible than a batch-command script.
> Create a new subfolder `evadventure/batchscripts/` (if it doesn't already exist)
> Create a new Python module `evadventure/batchscripts/combat_demo.py`
A batchcode file is a valid Python module. The only difference is that it has a `# HEADER` block and one or more `# CODE` sections. When the processor runs, the `# HEADER` part will be added on top of each `# CODE` part before executing that code block in isolation. Since you can run the file from in-game (including refresh it without reloading the server), this gives the ability to run longer Python codes on-demand.
```python
# Evadventure (Turnbased) combat demo - using a batch-code file.
#
# Sets up a combat area for testing turnbased combat.
attributes=[("desc", "A training dummy."), ("hp", 1000), ("hp_max", 1000)],
)
```
If editing this in an IDE, you may get errors on the `player = caller` line. This is because `caller` is not defined anywhere in this file. Instead `caller` (the one running the script) is injected by the `batchcode` runner.
But apart from the `# HEADER` and `# CODE` specials, this just a series of normal Evennia api calls.
Log into the game with a developer/superuser account and run
This should place you in the arena with the dummy (if not, check for errors in the output! Use `objects` and `delete` commands to list and delete objects if you need to start over.)
You can now try `attack dummy` and should be able to pound away at the dummy (lower its health to test destroying it). If you need to fix something, use `q` to exit the menu and get access to the `reload` command (for the final combat, you can disable this ability by passing `auto_quit=False` when you create the `EvMenu`).
## Conclusions
At this point we have coverered some ideas on how to implement both twitch- and turnbased combat systems. Along the way you have been exposed to many concepts such as classes, scripts and handlers, Commands, EvMenus and more.
Before our combat system is actually usable, we need our enemies to actually fight back. We'll get to that next.