Skip to content

item

item

The item module includes classes that represent items in the game like weapons, armor, spells, and regular adventuring gear.

Armor

Armor(name: str, ac_modifier: int = -1, **kwargs: dict[str, any])

Bases: Item

An Armor item modifies the armor class (AC) of a player character (PC).

Inherits all attributes from the Item class and introduces an additional attribute for AC modification.

The bonus of any non-cursed magic armor or shield is subtracted from the character's AC. For example, a fighter wearing plate mail armor and using a shield (but with no Dexterity modifier) has an AC of 2. If the fighter uses a shield + 1, their AC = 1.

Attributes:

  • ac_modifier (int) –

    Armor class (AC) bonus or penalty provided by this armor. A positive number reduces AC (good) and a negative number increases AC (bad). Defaults to 1.

Example:

# Create a suit of plate mail
armor = Armor("Plate Mail", ac_modifier=7)

Parameters:

  • name (str) –

    Name of the item.

  • ac_modifier (int, default: -1 ) –

    AC modifier. Lower is better. Defaults to -1.

  • **kwargs (dict[str, any], default: {} ) –

    Additional arguments to pass to the parent class.

Item

Item(name: str, item_type: ItemType = ItemType.ITEM, usable_by_classes: Optional[Set[CharacterClassType]] = None, max_equipped: int = 0, gp_value: int = 0, quest: Optional[Quest] = None)

An item represents a piece of equipment, a weapon, spell, quest piece, any other item that can be owned by a player character (PC).

You can specify that an item can be equipped by a PC and that more than one of that item type can be equipped.

Any item can be marked as a quest piece by setting its quest attribute. Quest pieces are items required to be obtained as part of a quest. An item can be both a quest piece and usable by a PC, for example, an enchanted (or cursed!) sword or magic ring.

Attributes:

  • name (str) –

    Name of the item.

  • item_type (ItemType) –

    Type of the item. Defaults to ItemType.ITEM.

  • owner (Optional[PlayerCharacter]) –

    The owner of the item.

  • usable_by_classes (Set[CharacterClassType]) –

    Classes that can use the item.

  • max_equipped (int) –

    Max number of this item that can be equipped.

  • is_equipped (bool) –

    Whether the item is currently equipped.

  • gp_value (int) –

    The value of the item in gold pieces (gp).

  • quest (Optional[Quest]) –

    The quest that the item is a part of.

Example:

# Create a sword usable by Fighters and Thieves
usable_by = {CharacterClassType.FIGHTER, CharacterClassType.THIEF}
item = Item("Sword", ItemType.WEAPON, usable_by, max_equipped=1, gp_value=10)

Don't call the methods on this class directory. Instead, use a PlayerCharacter's InventoryManager (pc.inventory) to add/remove this item to/from a PC's inventory or add it to a Quest.

Parameters:

  • name (str) –

    Name of the item.

  • item_type (ItemType, default: ITEM ) –

    Type of the item.

  • usable_by_classes (Set[CharacterClassType], default: None ) –

    Classes that can use the item.

  • max_equipped (int, default: 0 ) –

    Max number that can be equipped. Defaults to 0.

  • gp_value (int, default: 0 ) –

    Value of the item in gold pieces (gp). Defaults to 0.

  • quest (Optional[Quest], default: None ) –

    The quest that the item is a part of. Defaults to None.

is_equipable property

is_equipable: bool

Whether the item is equipable by its owner.

Returns:

  • bool ( bool ) –

    True if the item is usable by the owner's character class and at least one of the item can be

  • bool

    equipped, otherwise False.

is_quest_piece property

is_quest_piece: bool

Whether the item is a quest piece.

This is a convenience property that checks whether the item's quest attribute is not None.

Returns:

  • bool ( bool ) –

    True if the item is associated with a quest, otherwise False.

is_usable_by_owner property

is_usable_by_owner: bool

Whether the item is usable by its owner.

The item is usable if the owner is not None and the owning player character's class is in the set of classes in the item's usable_by_classes attribute. If the item is usable and also has a max_equipped value greater than 0, it can be equipped.

Returns:

  • bool ( bool ) –

    True if the CharacterClassType of the owning PlayerCharacter can use the item, otherwise False.

ItemAlreadyHasOwnerError

Bases: Exception

Exception raised when an item already has an owner.

ItemAlreadyInInventoryError

Bases: Exception

Exception raised when trying to add an item to a player character's (PC) inventory already in the inventory.

ItemAlreadyInQuestError

Bases: Exception

Exception raised when trying to assign an item to a quest that's already been assigned to a quest.

ItemEquippedError

Bases: Exception

Exception raised when trying to equip an item the player character (PC) already has equipped.

ItemNotEquippedError

Bases: Exception

Exception raised when trying to unequip an item the player character (PC) doesn't have equipped.

ItemNotInInventoryError

Bases: Exception

Exception raised when trying to remove an item from a player character's (PC) inventory that's not in the inventory.

ItemNotUsableError

Bases: Exception

Exception raised when trying to use an item that the player character (PC) can't use.

The inability to use an item is typically due to a character class restriction. For example, a magic user can't use a sword and a thief can't wear plate mail armor.

Spell

Spell(name: str, spell_level: int, damage_die: Optional[str] = None, range: Optional[int] = None, duration_turns: Optional[int] = None, **kwargs: dict[str, any])

Bases: Item

Represents a spell item in the game.

The Spell class extends the Item class to represent spells in the game. Spells have a level and may have a range, damage die, and duration. Unlike weapons, spells do not have a to-hit roll (they always hit if the spellcaster successfully cast the spell).

Parameters:

  • name (str) –

    The name of the spell.

  • spell_level (int) –

    The level of the spell.

  • damage_die (Optional[str], default: None ) –

    The damage roll for the spell. Defaults to None.

  • range (Optional[int], default: None ) –

    The range of the spell in feet. Defaults to None for touch spells.

  • duration_turns (Optional[int], default: None ) –

    The duration of the spell in turns (1 turn = 10 minutes). Defaults to None for instantaneous spells.

  • **kwargs (dict[str, any], default: {} ) –

    Arbitrary keyword arguments inherited from the Item class.

Attributes:

  • spell_level (int) –

    The level of the spell.

  • damage_die (Optional[str]) –

    The damage die for the spell, formatted like '1d8', '2d6', etc.

  • range (Optional[int]) –

    The range of the spell in feet.

  • duration_minutes (Optional[int]) –

    The duration of the spell in minutes. Defaults to None which indicates an instantaneous spell.

Example:

# Create two spells, one of 3rd-level (fireball) and another of 6th-level (heal)
fireball = Spell(name="Fireball", spell_level=3, damage_die="8d6", range=150, duration_minutes=None)
heal = Spell(name="Heal", spell_level=6, damage_die=None, range=None, duration_minutes=10)

Weapon

Weapon(name: str, to_hit_damage_die: str = '1d4', range: Optional[int] = None, **kwargs: dict[str, any])

Bases: Item

Represents a weapon item in the game.

The Weapon class extends the Item class to represent weapons in the game. It specifies damage die and may have a range indicating how far it can attack. Melee weapons typically have None as their range value.

Parameters:

  • name (str) –

    The name of the weapon.

  • to_hit_damage_die (str, default: '1d4' ) –

    The to-hit and damage roll for the weapon. Defaults to '1d4'.

  • range (Optional[int], default: None ) –

    The range of the weapon in feet. Defaults to None for melee weapons.

  • **kwargs (dict[str, any], default: {} ) –

    Arbitrary keyword arguments inherited from the Item class.

Attributes:

  • damage_die (str) –

    The damage die for the weapon, formatted like '1d8', '2d4', '1d6+1', etc.

  • range (Optional[int]) –

    The range of the weapon in feet.

Example:

# Create a sword and a longbow +1
sword = Weapon(name="Longsword", to_hit_damage_die="1d8")
enchanted_bow = Weapon(name="Longbow of Accuracy", to_hit_damage_die="1d8+1", range=150)