evennia.contrib.turnbattle.tb_items

Simple turn-based combat system with items and status effects

Contrib - Tim Ashley Jenkins 2017

This is a version of the ‘turnbattle’ combat system that includes conditions and usable items, which can instill these conditions, cure them, or do just about anything else.

Conditions are stored on characters as a dictionary, where the key is the name of the condition and the value is a list of two items: an integer representing the number of turns left until the condition runs out, and the character upon whose turn the condition timer is ticked down. Unlike most combat-related attributes, conditions aren’t wiped once combat ends - if out of combat, they tick down in real time instead.

This module includes a number of example conditions:

Regeneration: Character recovers HP every turn Poisoned: Character loses HP every turn Accuracy Up: +25 to character’s attack rolls Accuracy Down: -25 to character’s attack rolls Damage Up: +5 to character’s damage Damage Down: -5 to character’s damage Defense Up: +15 to character’s defense Defense Down: -15 to character’s defense Haste: +1 action per turn Paralyzed: No actions per turn Frightened: Character can’t use the ‘attack’ command

Since conditions can have a wide variety of effects, their code is scattered throughout the other functions wherever they may apply.

Items aren’t given any sort of special typeclass - instead, whether or not an object counts as an item is determined by its attributes. To make an object into an item, it must have the attribute ‘item_func’, with the value given as a callable - this is the function that will be called when an item is used. Other properties of the item, such as how many uses it has, whether it’s destroyed when its uses are depleted, and such can be specified on the item as well, but they are optional.

To install and test, import this module’s TBItemsCharacter object into your game’s character.py module:

from evennia.contrib.turnbattle.tb_items import TBItemsCharacter

And change your game’s character typeclass to inherit from TBItemsCharacter instead of the default:

class Character(TBItemsCharacter):

Next, import this module into your default_cmdsets.py module:

from evennia.contrib.turnbattle import tb_items

And add the battle command set to your default command set:

# # any commands you add below will overload the default ones. # self.add(tb_items.BattleCmdSet())

This module is meant to be heavily expanded on, so you may want to copy it to your game’s ‘world’ folder and modify it there rather than importing it in your game and using it as-is.

evennia.contrib.turnbattle.tb_items.DEF_DOWN_MOD = -15
evennia.contrib.turnbattle.tb_items.roll_init(character)[source]

Rolls a number between 1-1000 to determine initiative.

Parameters

character (obj) – The character to determine initiative for

Returns

initiative (int) – The character’s place in initiative - higher numbers go first.

Notes

By default, does not reference the character and simply returns a random integer from 1 to 1000.

Since the character is passed to this function, you can easily reference a character’s stats to determine an initiative roll - for example, if your character has a ‘dexterity’ attribute, you can use it to give that character an advantage in turn order, like so:

return (randint(1,20)) + character.db.dexterity

This way, characters with a higher dexterity will go first more often.

evennia.contrib.turnbattle.tb_items.get_attack(attacker, defender)[source]

Returns a value for an attack roll.

Parameters
  • attacker (obj) – Character doing the attacking

  • defender (obj) – Character being attacked

Returns

attack_value (int)

Attack roll value, compared against a defense value

to determine whether an attack hits or misses.

Notes

This is where conditions affecting attack rolls are applied, as well. Accuracy Up and Accuracy Down are also accounted for in itemfunc_attack(), so that attack items’ accuracy is affected as well.

evennia.contrib.turnbattle.tb_items.get_defense(attacker, defender)[source]

Returns a value for defense, which an attack roll must equal or exceed in order for an attack to hit.

Parameters
  • attacker (obj) – Character doing the attacking

  • defender (obj) – Character being attacked

Returns

defense_value (int)

Defense value, compared against an attack roll

to determine whether an attack hits or misses.

Notes

This is where conditions affecting defense are accounted for.

evennia.contrib.turnbattle.tb_items.get_damage(attacker, defender)[source]

Returns a value for damage to be deducted from the defender’s HP after abilities successful hit.

Parameters
  • attacker (obj) – Character doing the attacking

  • defender (obj) – Character being damaged

Returns

damage_value (int)

Damage value, which is to be deducted from the defending

character’s HP.

Notes

This is where conditions affecting damage are accounted for. Since attack items roll their own damage in itemfunc_attack(), their damage is unaffected by any conditions.

evennia.contrib.turnbattle.tb_items.apply_damage(defender, damage)[source]

Applies damage to a target, reducing their HP by the damage amount to a minimum of 0.

Parameters
  • defender (obj) – Character taking damage

  • damage (int) – Amount of damage being taken

evennia.contrib.turnbattle.tb_items.at_defeat(defeated)[source]

Announces the defeat of a fighter in combat.

Parameters

defeated (obj) – Fighter that’s been defeated.

Notes

All this does is announce a defeat message by default, but if you want anything else to happen to defeated fighters (like putting them into a dying state or something similar) then this is the place to do it.

evennia.contrib.turnbattle.tb_items.resolve_attack(attacker, defender, attack_value=None, defense_value=None, damage_value=None, inflict_condition=[])[source]

Resolves an attack and outputs the result.

Parameters
  • attacker (obj) – Character doing the attacking

  • defender (obj) – Character being attacked

Options:

attack_value (int): Override for attack roll defense_value (int): Override for defense value damage_value (int): Override for damage value inflict_condition (list): Conditions to inflict upon hit, a

list of tuples formated as (condition(str), duration(int))

Notes

This function is called by normal attacks as well as attacks made with items.

evennia.contrib.turnbattle.tb_items.combat_cleanup(character)[source]

Cleans up all the temporary combat-related attributes on a character.

Parameters

character (obj) – Character to have their combat attributes removed

Notes

Any attribute whose key begins with ‘combat_’ is temporary and no longer needed once a fight ends.

evennia.contrib.turnbattle.tb_items.is_in_combat(character)[source]

Returns true if the given character is in combat.

Parameters

character (obj) – Character to determine if is in combat or not

Returns

(bool) – True if in combat or False if not in combat

evennia.contrib.turnbattle.tb_items.is_turn(character)[source]

Returns true if it’s currently the given character’s turn in combat.

Parameters

character (obj) – Character to determine if it is their turn or not

Returns

(bool) – True if it is their turn or False otherwise

evennia.contrib.turnbattle.tb_items.spend_action(character, actions, action_name=None)[source]

Spends a character’s available combat actions and checks for end of turn.

Parameters
  • character (obj) – Character spending the action

  • actions (int) – Number of actions to spend, or ‘all’ to spend all actions

Keyword Arguments
  • action_name (str or None) – If a string is given, sets character’s last action in

  • to provided string (combat) –

evennia.contrib.turnbattle.tb_items.spend_item_use(item, user)[source]

Spends one use on an item with limited uses.

Parameters
  • item (obj) – Item being used

  • user (obj) – Character using the item

Notes

If item.db.item_consumable is ‘True’, the item is destroyed if it runs out of uses - if it’s a string instead of ‘True’, it will also spawn a new object as residue, using the value of item.db.item_consumable as the name of the prototype to spawn.

evennia.contrib.turnbattle.tb_items.use_item(user, item, target)[source]

Performs the action of using an item.

Parameters
  • user (obj) – Character using the item

  • item (obj) – Item being used

  • target (obj) – Target of the item use

evennia.contrib.turnbattle.tb_items.condition_tickdown(character, turnchar)[source]

Ticks down the duration of conditions on a character at the start of a given character’s turn.

Parameters
  • character (obj) – Character to tick down the conditions of

  • turnchar (obj) – Character whose turn it currently is

Notes

In combat, this is called on every fighter at the start of every character’s turn. Out of combat, it’s instead called when a character’s at_update() hook is called, which is every 30 seconds by default.

evennia.contrib.turnbattle.tb_items.add_condition(character, turnchar, condition, duration)[source]

Adds a condition to a fighter.

Parameters
  • character (obj) – Character to give the condition to

  • turnchar (obj) – Character whose turn to tick down the condition on in combat

  • condition (str) – Name of the condition

  • duration (int or True) – Number of turns the condition lasts, or True for indefinite

class evennia.contrib.turnbattle.tb_items.TBItemsCharacter(*args, **kwargs)[source]

Bases: evennia.objects.objects.DefaultCharacter

A character able to participate in turn-based combat. Has attributes for current and maximum HP, and access to combat commands.

at_object_creation()[source]

Called once, when this object is first created. This is the normal hook to overload for most object types.

at_before_move(destination)[source]

Called just before starting to move this object to destination.

Parameters

destination (Object) – The object we are moving to

Returns

shouldmove (bool) – If we should move or not.

Notes

If this method returns False/None, the move is cancelled before it is even started.

at_turn_start()[source]

Hook called at the beginning of this character’s turn in combat.

apply_turn_conditions()[source]

Applies the effect of conditions that occur at the start of each turn in combat, or every 30 seconds out of combat.

at_update()[source]

Fires every 30 seconds.

exception DoesNotExist

Bases: evennia.objects.objects.DefaultCharacter.DoesNotExist

exception MultipleObjectsReturned

Bases: evennia.objects.objects.DefaultCharacter.MultipleObjectsReturned

path = 'evennia.contrib.turnbattle.tb_items.TBItemsCharacter'
typename = 'TBItemsCharacter'
class evennia.contrib.turnbattle.tb_items.TBItemsCharacterTest(*args, **kwargs)[source]

Bases: evennia.contrib.turnbattle.tb_items.TBItemsCharacter

Just like the TBItemsCharacter, but doesn’t subscribe to the TickerHandler. This makes it easier to run unit tests on.

at_object_creation()[source]

Called once, when this object is first created. This is the normal hook to overload for most object types.

exception DoesNotExist

Bases: evennia.contrib.turnbattle.tb_items.TBItemsCharacter.DoesNotExist

exception MultipleObjectsReturned

Bases: evennia.contrib.turnbattle.tb_items.TBItemsCharacter.MultipleObjectsReturned

path = 'evennia.contrib.turnbattle.tb_items.TBItemsCharacterTest'
typename = 'TBItemsCharacterTest'
class evennia.contrib.turnbattle.tb_items.TBItemsTurnHandler(*args, **kwargs)[source]

Bases: evennia.scripts.scripts.DefaultScript

This is the script that handles the progression of combat through turns. On creation (when a fight is started) it adds all combat-ready characters to its roster and then sorts them into a turn order. There can only be one fight going on in a single room at a time, so the script is assigned to a room as its object.

Fights persist until only one participant is left with any HP or all remaining participants choose to end the combat with the ‘disengage’ command.

at_script_creation()[source]

Called once, when the script is created.

at_stop()[source]

Called at script termination.

at_repeat()[source]

Called once every self.interval seconds.

initialize_for_combat(character)[source]

Prepares a character for combat when starting or entering a fight.

Parameters

character (obj) – Character to initialize for combat.

start_turn(character)[source]

Readies a character for the start of their turn by replenishing their available actions and notifying them that their turn has come up.

Parameters

character (obj) – Character to be readied.

Notes

Here, you only get one action per turn, but you might want to allow more than one per turn, or even grant a number of actions based on a character’s attributes. You can even add multiple different kinds of actions, I.E. actions separated for movement, by adding “character.db.combat_movesleft = 3” or something similar.

next_turn()[source]

Advances to the next character in the turn order.

turn_end_check(character)[source]

Tests to see if a character’s turn is over, and cycles to the next turn if it is.

Parameters

character (obj) – Character to test for end of turn

join_fight(character)[source]

Adds a new character to a fight already in progress.

Parameters

character (obj) – Character to be added to the fight.

exception DoesNotExist

Bases: evennia.scripts.scripts.DefaultScript.DoesNotExist

exception MultipleObjectsReturned

Bases: evennia.scripts.scripts.DefaultScript.MultipleObjectsReturned

path = 'evennia.contrib.turnbattle.tb_items.TBItemsTurnHandler'
typename = 'TBItemsTurnHandler'
class evennia.contrib.turnbattle.tb_items.CmdFight(**kwargs)[source]

Bases: evennia.commands.command.Command

Starts a fight with everyone in the same room as you.

Usage:

fight

When you start a fight, everyone in the room who is able to fight is added to combat, and a turn order is randomly rolled. When it’s your turn, you can attack other characters.

key = 'fight'
help_category = 'combat'
func()[source]

This performs the actual command.

aliases = []
lock_storage = 'cmd:all();'
class evennia.contrib.turnbattle.tb_items.CmdAttack(**kwargs)[source]

Bases: evennia.commands.command.Command

Attacks another character.

Usage:

attack <target>

When in a fight, you may attack another character. The attack has a chance to hit, and if successful, will deal damage.

key = 'attack'
help_category = 'combat'
func()[source]

This performs the actual command.

aliases = []
lock_storage = 'cmd:all();'
class evennia.contrib.turnbattle.tb_items.CmdPass(**kwargs)[source]

Bases: evennia.commands.command.Command

Passes on your turn.

Usage:

pass

When in a fight, you can use this command to end your turn early, even if there are still any actions you can take.

key = 'pass'
aliases = ['hold', 'wait']
help_category = 'combat'
func()[source]

This performs the actual command.

lock_storage = 'cmd:all();'
class evennia.contrib.turnbattle.tb_items.CmdDisengage(**kwargs)[source]

Bases: evennia.commands.command.Command

Passes your turn and attempts to end combat.

Usage:

disengage

Ends your turn early and signals that you’re trying to end the fight. If all participants in a fight disengage, the fight ends.

key = 'disengage'
aliases = ['spare']
help_category = 'combat'
func()[source]

This performs the actual command.

lock_storage = 'cmd:all();'
class evennia.contrib.turnbattle.tb_items.CmdRest(**kwargs)[source]

Bases: evennia.commands.command.Command

Recovers damage.

Usage:

rest

Resting recovers your HP to its maximum, but you can only rest if you’re not in a fight.

key = 'rest'
help_category = 'combat'
func()[source]

This performs the actual command.

aliases = []
lock_storage = 'cmd:all();'
class evennia.contrib.turnbattle.tb_items.CmdCombatHelp(**kwargs)[source]

Bases: evennia.commands.default.help.CmdHelp

View help or a list of topics

Usage:

help <topic or command> help list help all

This will search for help on commands and other topics related to the game.

func()[source]

Run the dynamic help entry creator.

aliases = ['?']
help_category = 'general'
key = 'help'
lock_storage = 'cmd:all()'
class evennia.contrib.turnbattle.tb_items.CmdUse(**kwargs)[source]

Bases: evennia.commands.default.muxcommand.MuxCommand

Use an item.

Usage:

use <item> [= target]

An item can have various function - looking at the item may provide information as to its effects. Some items can be used to attack others, and as such can only be used in combat.

key = 'use'
help_category = 'combat'
func()[source]

This performs the actual command.

aliases = []
lock_storage = 'cmd:all();'
class evennia.contrib.turnbattle.tb_items.BattleCmdSet(cmdsetobj=None, key=None)[source]

Bases: evennia.commands.default.cmdset_character.CharacterCmdSet

This command set includes all the commmands used in the battle system.

key = 'DefaultCharacter'
at_cmdset_creation()[source]

Populates the cmdset

path = 'evennia.contrib.turnbattle.tb_items.BattleCmdSet'
evennia.contrib.turnbattle.tb_items.itemfunc_heal(item, user, target, **kwargs)[source]

Item function that heals HP.

kwargs:

min_healing(int): Minimum amount of HP recovered max_healing(int): Maximum amount of HP recovered

evennia.contrib.turnbattle.tb_items.itemfunc_add_condition(item, user, target, **kwargs)[source]

Item function that gives the target one or more conditions.

kwargs:
conditions (list): Conditions added by the item

formatted as a list of tuples: (condition (str), duration (int or True))

Notes

Should mostly be used for beneficial conditions - use itemfunc_attack for an item that can give an enemy a harmful condition.

evennia.contrib.turnbattle.tb_items.itemfunc_cure_condition(item, user, target, **kwargs)[source]

Item function that’ll remove given conditions from a target.

kwargs:

to_cure(list): List of conditions (str) that the item cures when used

evennia.contrib.turnbattle.tb_items.itemfunc_attack(item, user, target, **kwargs)[source]

Item function that attacks a target.

kwargs:

min_damage(int): Minimum damage dealt by the attack max_damage(int): Maximum damage dealth by the attack accuracy(int): Bonus / penalty to attack accuracy roll inflict_condition(list): List of conditions inflicted on hit,

formatted as a (str, int) tuple containing condition name and duration.

Notes

Calls resolve_attack at the end.

evennia.contrib.turnbattle.tb_items.ITEMFUNCS = {'add_condition': <function itemfunc_add_condition>, 'attack': <function itemfunc_attack>, 'cure_condition': <function itemfunc_cure_condition>, 'heal': <function itemfunc_heal>}

You can paste these prototypes into your game’s prototypes.py module in your /world/ folder, and use the spawner to create them - they serve as examples of items you can make and a handy way to demonstrate the system for conditions as well.

Items don’t have any particular typeclass - any object with a db entry “item_func” that references one of the functions given above can be used as an item with the ‘use’ command.

Only “item_func” is required, but item behavior can be further modified by specifying any of the following:

item_uses (int): If defined, item has a limited number of uses

item_selfonly (bool): If True, user can only use the item on themself

item_consumable(True or str): If True, item is destroyed when it runs

out of uses. If a string is given, the item will spawn a new object as it’s destroyed, with the string specifying what prototype to spawn.

item_kwargs (dict): Keyword arguments to pass to the function defined in

item_func. Unique to each function, and can be used to make multiple items using the same function work differently.