DnD_5e.combatant package

Submodules

DnD_5e.combatant.character module

class DnD_5e.combatant.character.Character(**kwargs)[source]

Bases: Combatant

This is for characters (PC, NPC, whatever)

validate_death_saves(death_saves)[source]
get_level() int[source]
Returns:

level

Return type:

int

get_hit_dice() Tuple[int, int][source]
Returns:

hit dice

Return type:

TYPE_DICE_TUPLE

get_death_saves()[source]
Returns:

death saves

reset_death_saves()[source]

Reset death saves to 0 failures and 0 successes.

Returns:

None

take_damage(damage: int, damage_type: str | None = None, is_critical: bool = False) int[source]

Take damage, applying vulnerabilities, resistances, and immunities as necessary

Parameters:
  • damage (positive integer) – the number of hit points of damage to take

  • damage_type (str) – the type of damage

  • is_critical (bool) – whether the damage is from a critical hit

Returns:

the actual damage taken

Return type:

int

should_die_from_damage(damage_taken: int, damage_type: str | None = None)[source]

Return whether taking the specified amount of damage should result in self dying :param damage_taken: the total amount of damage taken :param damage_type: the damage type :return:

become_unconscious()[source]

Add unconscious condition and set current_hp to 0

Returns:

None

become_conscious()[source]

Become conscious (removing unconcsious, unstable, and stable conditions)

Returns:

None

fail_death_save()[source]

Record that self failed a death save, die if this is the third failed death save

Returns:

None

succeed_death_save()[source]

Record that self succeeded a death save, stabilize if this is the third successful death save

Returns:

None

take_turn_unconscious()[source]

If unstable, make a death saving throw. If stable, do nothing.

Returns:

None (no damage was dealt)

reset()[source]

Reset attributes. Used to prepare to run an Encounter again, if the Encounter is simple enough.

Returns:

None

DnD_5e.combatant.creature module

class DnD_5e.combatant.creature.Creature(**kwargs)[source]

Bases: Combatant

This class represents creatures, monsters, etc. - basically anything that isn’t a character

get_cr() int[source]
Returns:

challenge rating

Return type:

non-negative integer

get_xp() int[source]
Returns:

xp

Return type:

non-negative integer

get_creature_type() str[source]
Returns:

creature type

Return type:

str

become_unconscious()[source]

Creatures don’t have death saves. If something happens that would make you unconscious, just die. :return:

DnD_5e.combatant.spellcaster module

class DnD_5e.combatant.spellcaster.SpellCaster(**kwargs)[source]

Bases: Combatant

A Combatant that can cast spells

current_eq(other) bool[source]

Compare self and other to determine if they are identical based on the attributes checked in equals and also these attributes: spell_slots

Parameters:

other (SpellCaster) – the SpellCaster to compare

Returns:

True if self is identical to other, False otherwise

Return type:

bool

get_spell_ability() str[source]
Returns:

spell ability

Return type:

str

get_spell_ability_mod() int[source]
Returns:

spell ability mod

Return type:

int

get_spell_save_dc() int[source]
Returns:

spell save dc

Return type:

int

get_spell_attack_mod() int[source]
Returns:

spell attack mod

Return type:

int

get_spell_slots() dict[source]
Returns:

spell slots

Return type:

dict mapping int to int

get_full_spell_slots() dict[source]
Returns:

full/maximum spell slots

Return type:

dict mapping int to int

get_level_spell_slots(level: int) int[source]

Determine the number of spell slots available for a given level

Parameters:

level (integer from 1 to 9 (inclusive)) – the spell level to look at

Returns:

the number of available spell slots

Return type:

int

Raise:

ValueError if level is invalid

get_spells() list[source]
Returns:

spells

Return type:

list of Spells

can_cast(spell: Spell) bool[source]

Determine if self can cast the given Spell. Note: does not check spell slots.

Parameters:

spell (py:class:Spell) – the Spell self is trying to cast

Returns:

True if self can cast spell, False otherwise

Return type:

bool

add_spell(spell: Spell)[source]

Add a given spell

Parameters:

spell (Spell) – the Spell to add

Returns:

None

Raise:

ValueError if spell is not a Spell

spend_spell_slot(level: int, spell=None)[source]

Spend a spell slot of the given level

Parameters:
  • level (an integer from 0 to 9 (inclusive)) – the spell level

  • spell (Spell) – the spell that is being used

Returns:

None

Raise:

ValueError if self doesn’t have any spell slots of level level

reset_spell_slots()[source]

Set spell slots to the maximum/full amount

Returns:

None

Module contents

class DnD_5e.combatant.Combatant(**kwargs)[source]

Bases: object

This class represents anything with a stat block that can fight

current_eq(other) bool[source]

Check to see if self is identical to other by looking at everything in equals as well as the following attributes: armor, current_hp, temp_hp, speed, conditions, vision, vulnerabilities, resistances, immunities, adv_to_be_hit, py:attr:weapons (don’t have to be identical, just equal), attacks, items, size, and team

Parameters:

other (Combatant) – the Combatant to be compared

Returns:

True if self is identical to other, False otherwise

Return type:

bool

get_ac() int[source]
Returns:

armor class

Return type:

positive integer

get_unarmored_ac() int[source]

Get unarmored AC. Can be overridden with a Feature.

Returns:

ac when not wearing armor

Return type:

positive integer

get_armor() Armor | None[source]
Returns:

armor

Return type:

Armor

get_max_hp() int[source]
Returns:

max hit points

Return type:

positive integer

get_temp_hp() int[source]
Returns:

temporary hit points

Return type:

non-negative integer

get_current_hp() int[source]
Returns:

current hit points

Return type:

non-negative integer

is_bloodied() bool[source]

Tell whether self is bloodied (current hit points at or below half of maximum)

Returns:

True if self is bloodied, False otherwise

Return type:

bool

is_hp_max() bool[source]
Returns:

True if current hp equals max hp, False otherwise

Return type:

bool

get_hp_to_max() int[source]

Get the number of hit points needed to bring current_hp to max_hp

Returns:

difference between max hp and current hp

Return type:

non-negative integer

get_speed() int[source]
Returns:

speed

Return type:

non-negative integer

get_climb_speed() int[source]
Returns:

speed

Return type:

non-negative integer

get_fly_speed() int[source]
Returns:

speed

Return type:

non-negative integer

get_swim_speed() int[source]
Returns:

speed

Return type:

non-negative integer

get_conditions() set[source]
Returns:

conditions

Return type:

set of strings

has_condition(condition: str) bool[source]

Check self._conditions to see if self has the given condition

Parameters:

condition (str) – a condition to look for

Returns:

True if self has condition, False otherwise

Return type:

bool

is_conscious() bool[source]
Returns:

True if self is not unconscious and not dead

get_vision() str[source]
Returns:

vision

Return type:

one of these strings: “normal”, “darkvision”, “blindsight”, “truesight”

can_see(light_src: str) bool[source]

Determine whether self can see a given light source

Parameters:

light_src (one of these strings: "normal", "dark", "magic") – a kind of light

Returns:

True if self can see light_src, False otherwise

Return type:

bool

Raise:

ValueError if light_src is not valid input

get_ability(ability: str) int[source]

Get the ability score modifier indicated by ability

Parameters:

ability (one of these strings: "strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma") – the name of an ability score

Returns:

the ability score modifier

Return type:

int

Raise:

ValueError if ability is not valid

get_strength() int[source]
Returns:

strength modifier

Return type:

int

get_dexterity() int[source]
Returns:

dexterity modifier

Return type:

int

get_constitution() int[source]
Returns:

constitution modifier

Return type:

int

get_intelligence() int[source]
Returns:

intelligence modifier

Return type:

int

get_wisdom() int[source]
Returns:

wisdom modifier

Return type:

int

get_charisma() int[source]
Returns:

charisma modifier

Return type:

int

get_proficiencies() set[source]
Returns:

proficiencies

Return type:

set of strings

has_proficiency(item: str) bool[source]

Check to see if self has a proficiency called item

Parameters:

item (str) – a weapon name/type, ability score (for saving throws), etc.

Returns:

True if self has a proficiency called item, False otherwise

Return type:

bool

get_proficiency_mod() int[source]
Returns:

proficiency bonus

Return type:

positive integer

get_expertise() set[source]
Returns:

expertise

Return type:

set of strings

has_expertise(item: str) bool[source]

Check to see if self has a proficiency called item

Parameters:

item (str) – a weapon name/type, ability score, etc.

Returns:

True if self has a proficiency called item, False otherwise

has_weapon_proficiency(weapon: Weapon) bool[source]

Check to see whether self has proficiency with the given weapon

Parameters:

weapon (Weapon) – the Weapon object to check for proficiency

Returns:

True if self has proficiency with weapon, False otherwise

Return type:

bool

Raise:

ValueError if weapon is not a Weapon

has_armor_proficiency(arm) bool[source]

Check to see whether self has proficiency with the given armor. Note: checks armor name based on class name (e.g., ChainMailArmor)

Parameters:

arm (Armor) – the armor in question

Returns:

True if self has proficiency with arm, False otherwise

Return type:

bool

Raise:

ValueError if arm is not a Armor

get_vulnerabilities() set[source]
Returns:

vulnerabilities

Return type:

set of strings

is_vulnerable(thing) bool[source]

Check to see if self is vulnerable to a given thing (usually a string for a damage type)

Parameters:

thing – what we’re checking for vulnerability

Returns:

True if self is vulnerable to thing, False otherwise

Return type:

bool

get_resistances() set[source]
Returns:

resistances

Return type:

set of strings

is_resistant(thing) bool[source]

Check to see if self is resistant to a given thing (usually a string for a damage type)

Parameters:

thing – what we’re checking for resistance

Returns:

True if self is resistant to thing, False otherwise

Return type:

bool

get_immunities() set[source]
Returns:

immunities

Return type:

set of strings

is_immune(thing) bool[source]

Check to see if self is resistant to a given thing (usually a string for a damage type)

Parameters:

thing – what we’re checking for immunity

Returns:

True if self is immune to thing, False otherwise

Return type:

bool

get_saving_throw(ability: str) int[source]

Get the modifier for an ability saving throw.

Warning

This does not roll the saving throw, it just returns the modifier to use for the throw

Parameters:

ability (one of these strings: "strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma') – an ability score name

Returns:

the modifier for an ability saving throw

Return type:

int

Raise:

ValueError if ability is not valid

get_adv_to_be_hit() int[source]

The sum of advantage (+1) and disadvantage (-1) circumstances affecting self is stored in _adv_to_be_hit. Look at this number and return an integer indicating whether an attack against self has advantage, disadvantage, or neither

Returns:

advantage to be hit (positive means advantage, negative means disadvantage, 0 means neither)

Return type:

int

get_features() set[source]
Returns:

features

Return type:

set of strings

has_feature(feature: str) bool[source]

Check to see if self has the feature feature

Parameters:

feature (str) – the name of a feature

Returns:

True if self has the feature feature, False otherwise

Return type:

bool

get_feature_dict()[source]

Get the dictionary that maps self’s methods to the overloaded methods of any Feature s self has. Internal use only! :return: feature_dict :rtype: dict

get_feature_classes()[source]
Returns:

all the Features affecting self

Return type:

set of Feature s

has_feature_class(item) bool[source]

Check whether self has the given Feature

Parameters:

item (py:class:Feature) – the feature to look for

Returns:

True if self has the feature, False otherwise

Return type:

bool

get_feature_methods()[source]
Returns:

all the methods affected by the Features self has

Return type:

set of methods

has_feature_method(item: str) bool[source]

Check whether self has a Feature affecting the given method

Parameters:

item (str) – the method to look for

Returns:

True if self has a Feature affecting item, False otherwise

Return type:

bool

get_fighting_styles() set[source]

If self has the “fighting style” feature, return fighting_styles (create it if it doesn’t already exist). Otherwise, return None

Returns:

fighting styles, or None

Return type:

set of strings, or None

has_fighting_style(fighting_style: str) bool[source]

Check to see if self has the fighting style fighting_style

Parameters:

fighting_style (str) – a fighting style

Returns:

True if self has fighting_style, False otherwise

Return type:

bool

get_weapons() list[source]
Returns:

the weapons self owns

Return type:

list of Weapons

get_main_hand() Weapon | None[source]
Returns:

what self is carrying in main hand

Return type:

Weapon

get_off_hand() Weapon | Shield | None[source]
Returns:

what self is carrying in off hand

Return type:

Weapon or Shield

get_size() str[source]
Returns:

size

Return type:

str

get_team()[source]
Returns:

team

Return type:

Team

is_on_my_team(other) bool[source]
Parameters:

other (Combatant) – the other Combatant to check

Returns:

True if other is on the same team as self, False otherwise

Return type:

bool

Raise:

ValueError if other is not a Combatant

get_items()[source]
Returns:

items

Return type:

list

get_attacks() list[source]
Returns:

attacks

Return type:

list of Attacks

get_attack_by_name(name: str)[source]

Find an attack by name and return it if it exists

Parameters:

name (str) – the name of the Attack we are looking for

Returns:

the Attack, or None if self does not have an Attack with the given name

get_weapon_attacks(weapon) list[source]

Get all attacks based on a given Weapon

Parameters:

weapon (Weapon) – the Weapon to look for

Returns:

a list of all attacks based on weapon

get_enemy_tactic() CombatantTactic[source]
Returns:

enemy_tactic (tactic for selecting who to attack)

Return type:

CombatantTactic

get_heal_tactic() CombatantTactic[source]
Returns:

heal_tactic (tactic for selecting who to heal)

Return type:

CombatantTactic

get_attack_tactic() AttackTactic[source]
Returns:

attack_tactic (tactic for selecting who to attack)

Return type:

AttackTactic

get_damage_dealt()[source]
Returns:

damage dealt

get_damage_taken()[source]
Returns:

damage taken

get_times_unconscious()[source]
Returns:

number of times gone unconscious

get_name() str[source]
Returns:

name

Return type:

str

set_name(name: str)[source]
get_logger() Logger[source]
Returns:

logger

Return type:

logging.Logger

set_logger(logger: str)[source]

Set self._logger to the logger with the name specified in logger

Parameters:

logger – the name of the Logger to use, or the logging.Logger object itself

Returns:

None

set_ac(ac: int)[source]

Set the armor class

Parameters:

ac (positive integer) – the new ac

Returns:

None

Raise:

ValueError if ac is invalid

set_armor(arm: Armor | None)[source]

Set the armor (and set the AC if appropriate). Assume arm is Armor or None.

Parameters:

arm (Armor) – the Armor to wear

Returns:

set_temp_hp(hp: int)[source]

Set temporary hp

Parameters:

hp (non-negative integer) – the new temporary hit points

Returns:

None

add_vulnerability(item)[source]

Add the given vulnerability

Parameters:

item – the vulnerability to add

Returns:

None

remove_vulnerability(item)[source]

Remove the given vulnerability

Parameters:

item – the vulnerability to remove

Returns:

None

add_resistance(item)[source]

Add the given resistance

Parameters:

item – the resistance to add

Returns:

None

remove_resistance(item)[source]

Remove the given resistance

Parameters:

item – the resistance to remove

Returns:

None

add_immunity(item)[source]

Add the given immunity

Parameters:

item – the immunity to add

Returns:

None

remove_immunity(item)[source]

Remove the given immunity

Parameters:

item – the immunity to remove

Returns:

None

modify_adv_to_be_hit(adv: int)[source]

Modify the advantage to be hit

Parameters:

adv (int) – the number to modify _adv_to_be_hit by

Returns:

None

Raise:

ValueError if adv is invalid

add_feature(feature: str)[source]

Add the given feature

Parameters:

feature (str) – the feature to add

Returns:

None

add_feature_class(feature)[source]

Add the given Feature

Parameters:

feature (Feature) – the Feature to add

Returns:

None

add_fighting_style(fighting_style: str)[source]

Add the given fighting style

Parameters:

fighting_style – the fighting style to add

Returns:

None

Raise:

ValueError if fighting_style is invalid or if self already has fighting_stlye

add_weapon(weapon: Weapon)[source]

Add the given weapon

Parameters:

weapon (Weapon) – the Weapon to add

Returns:

None

Raise:

ValueError if weapon is invalid

remove_weapon(weapon: Weapon)[source]

Remove the given weapon (based on identity) and all its attacks

Parameters:

weapon (a Weapon) – the Weapon to remove

Returns:

None

remove_all_weapons()[source]

Remove all of self’s weapons and their attacks

Returns:

None

add_weapon_attacks(weapon: Weapon)[source]

Add all the Attacks that weapon can make to self._attacks

Parameters:

weapon (Weapon) – the Weapon to add attacks for

Returns:

None

get_weapon_attack_mod(weapon)[source]

Get the base modifier for attacks with a given weapon (based on str/dex, features, etc.)

Parameters:

weapon (Weapon) – the weapon to look at

Returns:

attack modifier

Return type:

int

get_weapon_damage_mod(weapon)[source]

Get the base modifier for damage attacks with a given weapon (based on str/dex, features, etc.). May seem like duplication of get_weapon_attack_mod, but this is a different method so it’s easier to overload with a Feature

Parameters:

weapon (Weapon) – the weapon to look at

Returns:

damage modifier

Return type:

int

get_weapon_attack_modifiers(weapon)[source]

Gets the modifiers for attacks with a given weapon (based on str/dex, features, etc.)

Parameters:

weapon (Weapon) – the weapon to look at

Returns:

attack_mod, damage_mod, range_attack_mod

Return type:

tuple of ints

add_attack(attack: Attack)[source]

Add the given attack

Parameters:

attack (Attack) – the Attack to add

Returns:

None

remove_attack(attack: Attack)[source]

Remove the given Attack.

..Warning:: this calls list.remove(item), which uses item’s built-in __eq__() method. __eq__() is overriden for Attack to do a comparison based on instance variables and not memory address.

Parameters:

attack – the Attack to remove

Returns:

None

remove_weapon_attacks(weapon: Weapon)[source]

Remove all the attacks related to Weapon.

Warning

Doesn’t check to see whether self actually has the given weapon (checking something that is usually False is not that important), but no unexpected behavior happens if this is the case.

Parameters:

weapon (Weapon) – the Weapon to remove attacks based on

Returns:

None

add_condition(condition: str)[source]

Add the given condition

Parameters:

condition (str) – the condition to add

Returns:

None

remove_condition(condition: str)[source]

Remove the given condition

Parameters:

condition (str) – the condition to remove

Returns:

None

remove_all_conditions()[source]

Remove all conditions

Returns:

None

set_vision(vision: str)[source]

Set vision to the given value

Parameters:

vision (str) – the vision to change to

Returns:

None

Raise:

ValueError if vision is invalid

set_size(size: str)[source]

Set size to the given size

Parameters:

size (str) – the size to change to

Returns:

None

Raise:

ValueError if size is invalid

set_team(team)[source]

Set team to the given Team. Does not check that team is valid (this is to avoid circular imports)

Parameters:

team – the team to change to

Returns:

None

set_enemy_tactic(tact)[source]

Set rule for who to attack to tact

Parameters:

tact (CombatantTactic) – the Tactic self uses to select an enemy Combatant

Returns:

None

set_heal_tactic(tact)[source]

Set rule for who to heal to tact

Parameters:

tact (CombatantTactic) – the Tactic self uses to select a who to heal Combatant

Returns:

None

set_attack_tactic(tact)[source]

Set rule for which attack to use to tact

Parameters:

tact (AttackTactic) – the Tactic self uses to select an enemy Attack

Returns:

None

select_action() str[source]

Choose what action to take. # TODO: support actions other than “Attack”

Returns:

a string representing the action chosen

Return type:

str

select_enemy(choices, **kwargs)[source]

From a list of Combatant s, select who to attack

Parameters:

choices (list of Combatant s) – the list of Combatant s to choose from

Returns:

the selected Combatant s

Return type:

Combatant

select_heal(choices, **kwargs)[source]

From a list of Combatant s, select who to heal

Parameters:

choices (list of Combatant s) – the list of Combatant s

Returns:

the selected Combatant

Return type:

Combatant

select_attack(**kwargs)[source]

Choose which of self’s attacks to use

Parameters:

kwargs – keyword arguments

Returns:

the chosen attack

Return type:

Attack

send_attack(target, attack: Attack, adv=0) int | None[source]

Attack a given target using a given attack

Parameters:
  • target (Combatant) – the Combatant to attack

  • attack (Attack) – the Attack being made

  • adv (int) – indicates whether self has advantage for this attack

Returns:

the damage target took from attack, or None if the attack failed to hit

take_attack(attack_result: Tuple[int, int], source=None, attack: Attack | None = None) bool[source]

Read in an attack roll and determine whether the attack hits or not

Parameters:
  • attack_result (TYPE_ROLL_RESULT) – first number in tuple indicates roll value, second number indicates crit value

  • source – the thing that is making the attack (usually a Combatant)

  • attack (Attack) – the Attack that is received

Returns:

True if the attack hits, False otherwise

Return type:

bool

take_saving_throw(save_type: str, dc: int, attack: Attack | None = None, adv: int = 0) bool[source]

Respond to something that asks for a saving throw

Parameters:
  • save_type (one of these strings: "strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma") – the kind of saving throw to make

  • dc (int) – to succeed, the roll must be greater than or equal to the dc

  • attack (Attack) – the Attack that is asking for a saving throw

  • adv (int) – indicates advantage, disadvantage, or neither

Returns:

True if self makes the saving throw, False otherwise

Return type:

bool

make_saving_throw(save_type: str, adv: int = 0) int[source]

Roll a saving throw of the given type

Parameters:
  • save_type (one of these strings: "strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma") – the kind of saving throw to make

  • adv (int) – indicates advantage, disadvantage, or neither

Returns:

the number rolled for the saving throw

Return type:

int

should_die_from_damage(damage_taken: int, damage_type: str | None = None)[source]

Return whether taking the specified amount of damage should result in self dying :param damage_taken: the total amount of damage taken :param damage_type: the damage type :return:

take_damage(damage: int, damage_type: str | None = None, is_critical: bool = False) int[source]

Take damage, applying vulnerabilities, resistances, and immunities as necessary

Parameters:
  • damage (positive integer) – the number of hit points of damage to take

  • damage_type (str) – the type of damage

  • is_critical (bool) – whether the damage is from a critical hit

Returns:

the actual damage taken

Return type:

int

take_healing(healing: int) int[source]

Become healed for the given number of hit points

Parameters:

healing (int) – the number of hit points to gain

Returns:

the number of hit points actually healed

Return type:

int

become_conscious()[source]

Become conscious (removing unconcsious conditions)

Returns:

None

heal_to_max()[source]

Heal to max hp

Returns:

None

ability_check(ability) int[source]
roll_initiative() int[source]
take_turn(teams)[source]

Take a turn in combat

Parameters:

teams (list of Team s) – the teams in the current combat

Returns:

damage taken by the enemy

become_unconscious()[source]

Add unconscious condition and set current_hp to 0

Returns:

None

take_turn_unconscious()[source]

Do nothing

Returns:

None (no damage was dealt)

die()[source]

Die - set condition to “dead” and current_hp and temp_hp to 0

Returns:

None

reset()[source]

Reset attributes. Used to prepare to run an Encounter again, if the Encounter is simple enough.

Returns:

None