Skip to content

encounter

encounter

Encounter

Encounter(name, description: str = '', monster_party: MonsterParty = None, treasure_type: TreasureType = TreasureType.NONE)

An encounter represents something the party discovers, confronts, or experiences at a Location in a Dungeon.

An encounter typically represents a group of monsters the party can fight, but it could also just be a notable location or other non-combat encounter in which the party receives information or perhaps a quest piece for reaching that Location.

Attributes:

  • name (str) –

    The name or ID of the encounter.

  • description (str) –

    The description of the encounter (location, environment, etc.).

  • monsters (MonsterParty) –

    The party of monsters in the encounter.

  • treasure_type ((TreasureType, Optional)) –

    The type of treasure available for award to the party at this location. This treasure is in addition to any treasure associated with monsters defeated by the party if this is a combat encounter.

  • turn_order (deque) –

    A deque of the combatants in the encounter, sorted by initiative roll.

  • is_started (bool) –

    Whether the encounter has started.

  • is_ended (bool) –

    Whether the encounter has ended.

Parameters:

  • name (str) –

    The name or ID of the encounter.

  • description (str, default: '' ) –

    The description of the encounter (location, environment, etc.). Optional.

  • monster_party (MonsterParty, default: None ) –

    The party of monsters in the encounter. Optional.

  • treasure_type (TreasureType, default: NONE ) –

    The type of treasure at this location available for award to the party.

end_encounter

end_encounter()

Ends an encounter that was previously started with start_encounter().

This method concludes the encounter, performing actions like determining the outcome if it was a combat encounter (whether the player's party or the monsters won) and awarding experience points, if applicable. It sets the is_started and is_ended flags appropriately to indicate that the encounter has concluded, and if the player's party won, awards experience points as dictated by the MonsterParty's xp_value to the party.

After calling end_encounter() is when you can consider the encounter "over," and when you'd typically record or otherwise process the play-by-play encounter log available through its get_encounter_log() method. It's also when you could check to see if any PCs in the player's party have gained a level, and perform appopriate actions in your UI (for example) if so.

Example:

# Assuming 'encounter' is an instance of Encounter, this ends the encounter and
# awards experience points to the party if appropriate, such as if they defeated
# a monsters, gained treasure, or both.
encounter.end_encounter()

from_dict classmethod

from_dict(encounter_dict: dict) -> Encounter

Deserialize a dictionary into an Encounter instance.

This class method creates a new instance of Encounter using the data in the dictionary. The dictionary should contain keys corresponding to the attributes of an Encounter, including a serialized MonsterParty instance if there was one in the Encounter when it was serialized. If the is_ended attribute of the Encounter being deserialized with from_dict is True, the Encounter's end_encounter() method is called as part of the deserialization process.

Example:

# Assuming 'encounter_dict' is a previously serialized Encounter dictionary
encounter = Encounter.from_dict(encounter_dict)

# The 'encounter' is now a rehydrated instance of the Encounter class

Parameters:

  • encounter_dict (dict) –

    A dictionary containing the encounter's attributes.

Returns:

  • Encounter ( Encounter ) –

    An instance of Encounter initialized with the data from the dictionary.

get_encounter_log

get_encounter_log() -> str

Return the encounter log as a string.

get_random_encounter classmethod

get_random_encounter(dungeon_level: int) -> Encounter

Get a random encounter appropriate for the specified dungeon level.

The dungeon_level corresponds to the monster's number of hit dice, and the encounter will contain monsters of the same type and at that level (number of hit dice). For example, if dungeon_level is 1, the encounter will contain monsters with 1d8 or 1d8+n hit die. If dungeon_level is 3, the encounter will contain monsters with 3 or 3d8+n hit dice.

Parameters:

  • dungeon_level (int) –

    The level of dungeon the encounter should be appropriate for.

Returns:

  • Encounter ( Encounter ) –

    A random encounter.

log_mesg

log_mesg(message: Optional[str])

Log an encounter message.

Parameters:

  • message (Optional[str]) –

    The message to log. If None, no message is logged.

start_encounter

start_encounter(party: Party)

Start the encounter with the given party.

This method initiates the encounter and sets up conditions like determining surprise and starting combat if the encounter contains a hostile MonsterParty. If so, it compares the surprise rolls of the player's party and the monster party to decide which party is surprised, then proceeds to start combat with its _start_combat private method if combat is necessary. If there are no monsters, the encounter proceeds as a non-combat scenario.

Parameters:

  • party (Party) –

    The player's party involved in the encounter.

Example:

# Assuming 'encounter' is an instance of Encounter and 'player_party' is an instance of Party
encounter.start_encounter(player_party)

# This initiates the encounter, determining surprise, starting combat if applicable,
# and logging the start of the encounter.

to_dict

to_dict() -> dict

Serialize the Encounter instance to a dictionary format.

This method converts the Encounter's attributes, including the associated MonsterParty, into a dictionary. This is particularly useful for saving the state of an encounter to persistent storage.

Returns:

  • dict ( dict ) –

    A dictionary representation of the Encounter instance.

Example:

# Assuming 'encounter' is an instance of Encounter
encounter_dict = encounter.to_dict()

# The encounter_dict could now be used to store the state of the Encounter,
# for example by converting the dict to JSON or storing in a database.
# You could also pass encounter_dict the from_dict() method to recreate
# the encounter.