DnD_5e.attack_class package

Submodules

DnD_5e.attack_class.saving_throw_attacks module

class DnD_5e.attack_class.saving_throw_attacks.SavingThrowMixin(**kwargs)[source]

Bases: object

get_dc() int[source]
Returns:

DC

Return type:

non-negative integer

get_save_type() str[source]
Returns:

ability score for saving throws

Return type:

one of these strings: “strength”, “dexterity”, “constitution”, “intelligence”, “wisdom”, “charisma”

get_prob_fail_save(**kwargs)[source]
Parameters:
  • target (Combatant) – the target you are forcing to make the saving throw

  • mod (int) – the modifier for the target’s saving throw. Use this or target

Returns:

the probability that target (or somebody with a saving throw modifier of mod) fails the saving throw

do_saving_thow(source, target) bool[source]
class DnD_5e.attack_class.saving_throw_attacks.SavingThrowAttack(**kwargs)[source]

Bases: SavingThrowMixin, Attack

This class represents Attacks that require a saving throw from the target instead of a roll to hit

get_damage_on_success() bool[source]
Returns:

damage on success

Return type:

bool

get_prob_hit(**kwargs) float[source]
Parameters:
  • target – the target who is making the saving throw

  • mod – the target’s modifier to the saving throw

Returns:

the probability of target failing their saving throw

get_dpr(**kwargs) float[source]
Parameters:

target (Combatant) – the target you are trying to hit

Returns:

the average damage per round (dpr)

make_attack(source, target, adv=0) int | None[source]

Call target. take_saving_throw() to see if the target makes the save. Call send_damage() (that method will handle whether to damage on a successful save).

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the saving throw

Returns:

the damage taken (if the attack misses, damage taken is None)

Return type:

non-negative integer or None

Raise:

ValueError if target can’t take attacks

on_hit(source, target, adv=0, crit=0)[source]

The target failed the save, now do whatever happens on a failed save

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

Returns:

the damage taken

on_miss(source, target, adv=0, crit=0)[source]

The target made the save, now do whatever happens on a successful save

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

Returns:

the damage taken

send_damage(target, saved: bool = False) int | None[source]

Roll damage using self.roll_damage and store the result in variable damage. Call target. take_damage(), passing in damage and _damage_type.

Arguments crit and crit_multiplier are not needed because crits are not possible

Parameters:
  • target (Combatant) – the Combatant that is taking the damage

  • saved (bool) – if this is True, damage is none if not _damage_on_success or half damage if _damage_on_success

Returns:

the damage taken (as returned by target.take_damage)

Return type:

non-negative integer or None

class DnD_5e.attack_class.saving_throw_attacks.SaveOrDieAttack(**kwargs)[source]

Bases: SavingThrowMixin, Attack

Attacks where the target must make a saving throw and if it fails it dies. Does not work for attacks that have a saving throw and then a save or die. For that, you need a different approach (maybe a MultiAttack?)

get_threshold() int[source]
Returns:

the hp below which the target must save or die

get_save_on_miss() bool[source]
Returns:

True if the target still has to make the saving throw if the main attack missed, False otherwise

get_dpr(**kwargs) float[source]

Calculate the damage per round, assuming that if the target fails the saving throw (and thus dies), they also take the damage of

Parameters:

target (Combatant) – the target you are trying to hit

Returns:

the average damage per round (dpr)

make_attack(source, target, adv: int = 0)[source]

Roll attack using roll_attack() and store the result in variable result. Call target. take_attack() to see if the attack hits. If the attack hits, call send_damage().

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

Returns:

the damage taken (if the attack misses, damage taken is None)

Return type:

non-negative integer or None

Raise:

ValueError if source can’t send attacks or target can’t take attacks

class DnD_5e.attack_class.saving_throw_attacks.HitAndSaveAttack(**kwargs)[source]

Bases: SavingThrowMixin, Attack

Attacks where the attacker makes a regular (roll to hit) attack and then, if that hits, makes a saving throw attack

get_save_damage_dice()[source]
Returns:

saving throw damage dice

get_save_damage_type() str[source]
Returns:

damage type for saving throw damage

get_damage_on_success() bool[source]
Returns:

whether the target takes damage on a successful save

on_hit(source, target, adv=0, crit=0)[source]

The attack hit! Do all the normal Attack stuff (i.e., send damage), then have the target make a saving throw. :param source: the Combatant that is making the attack :type source: Combatant :param target: the Combatant that source is attacking :type target: Combatant :param adv: advantage, disadvantage, or neither on the attack roll :type adv: one of these integers: -1, 0, 1 :type crit: whether or not the hit was a crit :return: the total damage taken

roll_save_damage()[source]

Roll _damage_dice (if crit == 1, multiply the number of dice to roll by crit_multiplier), then add _damage_mod

Returns:

the damage rolled

Return type:

namedtuple

Raise:

ValueError if crit_multiplier is invalid

send_save_damage(target, saved: bool = False) int | None[source]

Roll damage using self.roll_save_damage and store the result in variable damage. Call target. take_damage(), passing in damage and _damage_type.

Parameters:
  • target (Combatant) – the Combatant that is taking the damage

  • saved (bool) – if this is True, damage is none if not _damage_on_success or half damage if _damage_on_success

Returns:

the damage taken (as returned by target.take_damage)

Return type:

non-negative integer or None

DnD_5e.attack_class.spell_attacks module

class DnD_5e.attack_class.spell_attacks.Spell(**kwargs)[source]

Bases: Attack

This class represents spells

get_level() int[source]
Returns:

level

Return type:

integer between 1 and 9 (inclusive)

get_school()[source]
Returns:

school of magic

get_casting_time() int[source]
Returns:

amount of time it takes cast self, in rounds

Return type:

a positive integer for the number of rounds, or one of these strings: “1 action”, “1 bonus action”, “1 reaction”

get_components() list[source]
Returns:

the components needed to cast self

Return type:

list of strings

has_component(component: str) bool[source]
Parameters:

component (str) – the component in question

Returns:

True if self has that component, False otherwise

Return type:

bool

get_duration() int[source]
Returns:

duration in rounds

Return type:

non-negative integer

get_ritual() bool[source]
Returns:

whether spell can be cast as a ritual

make_attack(source, target, adv: int = 0, level: int | None = None) int[source]

First, try to spend the appropriate spell slot. Then, call the superclass method.

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

  • level (integer between 1 and 9 (inclusive)) – the level the spell is being cast at

Returns:

the damage taken (if the attack misses, damage taken is None)

Return type:

non-negative integer or None

Raise:

ValueError if source can’t send attacks or target can’t take attacks

class DnD_5e.attack_class.spell_attacks.SavingThrowSpell(**kwargs)[source]

Bases: Spell, SavingThrowAttack

Spells that require the target to make a saving throw

class DnD_5e.attack_class.spell_attacks.HealingSpell(**kwargs)[source]

Bases: Spell

Spells that heal (i.e., add hit points) instead of dealing damage; damage is used as healing

make_attack(source, target, adv: int = 0, level=None) int[source]

Spend the appropriate spell slot, then roll damage and heal the target for that amount

Parameters:
  • source (Combatant) – the Combatant that is sending the healing

  • target (Combatant) – the Combatant that source is healing

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll (NOT USED FOR THIS METHOD)

  • level (integer between 1 and 9 (inclusive)) – the level the spell is being cast at

Returns:

the points the target is healed for

Return type:

non-negative integer

Raise:

ValueError if input is invalid

Module contents

class DnD_5e.attack_class.Attack(**kwargs)[source]

Bases: object

This class represents attacks

set_name(name: str)[source]
get_attack_dice() Dice[source]
Returns:

attack dice

Return type:

dice.Dice

get_damage_dice() DamageDice[source]
Returns:

damage dice

Return type:

dice.DamageDice

get_attack_mod() int[source]
Returns:

attack mod

Return type:

int

get_damage_mod() int[source]
Returns:

damage mod

Return type:

int

get_damage_type() str | None[source]
Returns:

damage type

Return type:

str

has_damage_type(damage_type: str) bool[source]
Parameters:

damage_type (str) – the type of damage to look for

Returns:

True if self has the specified damage type, False otherwise

Return type:

bool

get_range() int[source]
Returns:

range for ranged attacks

Return type:

usually a positive integer, but currently other types are also supported

get_melee_range() int[source]
Returns:

range for melee attacks

Return type:

positive integer

get_adv() int[source]
Returns:

adv

Return type:

int

get_name() str[source]
Returns:

name

Return type:

str

get_weapon() Weapon | None[source]
Returns:

weapon, if any

Return type:

Weapon or None

get_max_hit() int[source]
Returns:

the maximum number that could result from an attack roll

Return type:

int

get_min_hit() int[source]
Returns:

the minimum number that could result from an attack roll

Return type:

int

get_average_hit() float[source]
Returns:

the average number that would result from an attack roll

Return type:

float

get_max_damage() int[source]
Returns:

the maximum number that could result from a damage roll (not counting extra damage from a critical hit)

Return type:

int

get_min_damage() int[source]
Returns:

the minimum number that could result from a damage roll (not counting extra damage from a critical hit)

Return type:

int

get_average_damage() float[source]
Returns:

the average number that would result from a damage roll

Return type:

float

get_prob_hit(**kwargs) float[source]
Parameters:

ac – the ac you’re trying to hit

Returns:

the probability of rolling greater than or equal to ac

get_dpr(**kwargs) float[source]
Parameters:

ac (int) – the armor class you need to hit

Returns:

the average damage per round (dpr)

set_attack_mod(attack_mod: int)[source]

Set self._attack_mod

Parameters:

attack_mod (int) – the new attack mod

Returns:

None

Raise:

ValueError if attack_mod is not an integer

shift_attack_mod(attack_mod: int)[source]

Modify self._attack_mod by the given value

Parameters:

attack_mod (int) – the damage mod to shift by

Returns:

None

Raise:

ValueError if attack_mod is not an integer

set_damage_mod(damage_mod: int)[source]

Set self._damage_mod to the given value

Parameters:

damage_mod (int) – the new damage mod

Returns:

None

Raise:

ValueError if damage_mod is not an integer

shift_damage_mod(damage_mod: int)[source]

Modify self._damage_mod by the given value

Parameters:

damage_mod (int) – the damage mod to shift by

Returns:

None

Raise:

ValueError if damage_mod is not an integer

roll_attack(adv: int = 0) Tuple[int, int][source]

Roll a d20 (with advantage/disadvantage as computed with adv and _adv), adding _attack_mod

Parameters:

adv (int) – advantage, disadvantage, or neither on the attack roll

Returns:

the roll and an indication of whether it was a critical hit, critical miss, or neither

Return type:

TYPE_ROLL_RESULT

roll_damage(crit: int = 0, crit_multiplier: int = 2)[source]

Roll _damage_dice (if crit == 1, multiply the number of dice to roll by crit_multiplier), then add _damage_mod

Parameters:
  • crit (one of these integers: -1, 0, 1) – indicates whether the hit was a crit

  • crit_multiplier (positive integer) – the multiplier for dice num if the hit is a crit

Returns:

the damage rolled

Return type:

namedtuple

Raise:

ValueError if crit_multiplier is invalid

make_attack(source, target, adv: int = 0) int | None[source]

Roll attack using roll_attack() and store the result in variable result. Call target. take_attack() to see if the attack hits. If the attack hits, call send_damage().

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

Returns:

the damage taken (if the attack misses, damage taken is None)

Return type:

non-negative integer or None

Raise:

ValueError if source can’t send attacks or target can’t take attacks

on_hit(source, target, adv=0, crit=0) int[source]

You hit the target, now do whatever happens on a hit

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

Returns:

the damage taken

on_miss(source, target, adv=0, crit=0)[source]

You missed the target, now do whatever happens on a miss

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant) – the Combatant that source is attacking

  • adv (one of these integers: -1, 0, 1) – advantage, disadvantage, or neither on the attack roll

Returns:

None

send_damage(target, crit: int = 0, crit_multiplier: int = 2) int[source]

Roll damage using roll_damage() and store the result in variable damage. Call target. take_damage() , passing in damage and damage_type.

Parameters:
  • target (Combatant) – the Combatant that is taking the damage

  • crit (one of these integers: -1, 0, 1) – indicates whether the hit was a crit

  • crit_multiplier (positive integer) – the number to multiply dice num by if the hit is a crit

Returns:

the damage taken, as returned by target.take_damage

Return type:

non-negative integer or None

class DnD_5e.attack_class.MultiAttack(**kwargs)[source]

Bases: Attack

Container for multiple Attacks

current_eq(other) bool[source]

For this class, it is the same as __eq__.

Parameters:

other (MultiAttack) – the MultiAttack to be compared

Returns:

True if self is identical to other, False otherwise

Return type:

bool

get_attacks() List[Attack][source]
Returns:

attacks

Return type:

list of Attacks

get_attack_by_name(name) Attack | None[source]

Get an Attack from _attack_list with the given name

Parameters:

name (str) – the name of the Attack

Returns:

the first Attack with the given name (or None, if no match is found)

Raise:

ValueError if name is invalid

get_max_damage() int[source]
Returns:

The maximum damage combined across all attacks

Return type:

non-negative integer

get_min_damage() int[source]
Returns:

The minimum damage combined across all attacks

Return type:

non-negative integer

get_average_damage() int[source]
Returns:

The average damage combined across all attacks

Return type:

non-negative integer

add_attack(attack: Attack)[source]

Add attack to the end of self._attack_list

Parameters:

attack (Attack) – the Attack to add

Returns:

None

Raise:

ValueError if attack is not an Attack

remove_attack(attack)[source]

Remove attack from _attack_list

Parameters:

attack (Attack or str) – the Attack to remove (or the name of the Attack to remove)

Returns:

None

make_attack(source, target, adv: int = 0) int | None[source]

Convert target and adv to lists so that each attack has one target and one adv. If target or adv is too short of a sequence, the last value in the provided list will be copied as needed. For each attack in get_attacks(), call the attack the appropriate target with the appropriate adv.

Parameters:
  • source (Combatant) – the Combatant that is making the attack

  • target (Combatant or list of Combatants) – the Combatant(s) that source is attacking

  • adv – one or more integers that indicates advantage on the attack roll

Returns:

the total damage taken

Return type:

non-negative integer

Raise:

ValueError if source can’t send attacks

get_damage_dice() Tuple[int, int][source]
Raise:

NotImplementedError

get_attack_mod() int[source]
Raise:

NotImplementedError

get_damage_mod() int[source]
Raise:

NotImplementedError

get_damage_type() str | None[source]
Raise:

NotImplementedError

get_range() int[source]
Raise:

NotImplementedError

get_melee_range() int[source]
Raise:

NotImplementedError

get_adv() int[source]
Raise:

NotImplementedError

get_weapon() Weapon | None[source]
Raise:

NotImplementedError

get_max_hit() int[source]
Raise:

NotImplementedError

get_average_hit() int[source]
Raise:

NotImplementedError

set_attack_mod(attack_mod: int)[source]
Raise:

NotImplementedError

set_damage_mod(damage_mod: int)[source]
Raise:

NotImplementedError

shift_damage_mod(damage_mod: int)[source]
Raise:

NotImplementedError

roll_attack(adv=0)[source]
Raise:

NotImplementedError

roll_damage(crit=0, crit_multiplier=2)[source]
Raise:

NotImplementedError