The Battle object
Abstract Battle
- class poke_env.battle.abstract_battle.AbstractBattle(battle_tag: str, username: str, logger: Logger, save_replays: str | bool, gen: int)
Bases:
ABC- MESSAGES_TO_IGNORE = {'', '-anim', '-block', '-burst', '-center', '-combine', '-crit', '-fail', '-fieldactivate', '-hint', '-hitcount', '-miss', '-notarget', '-nothing', '-ohko', '-resisted', '-supereffective', '-waiting', '-zbroken', 'J', 'L', 'askreg', 'badge', 'c', 'chat', 'crit', 'debug', 'deinit', 'gametype', 'hidelines', 'html', 'immune', 'inactiveoff', 'init', 'j', 'join', 'l', 'leave', 'n', 'name', 'rated', 'resisted', 'sentchoice', 'split', 'supereffective', 'teampreview', 'uhtml', 'upkeep', 'zbroken'}
- apply_teambuilder_team(role: str, teambuilder_team: List[TeambuilderPokemon], teampreview_team: List[Pokemon])
- abstractmethod clear_all_boosts()
- property commanding: bool
- Returns:
Whether commander is active with Dondozo and Tatsugiri on the field
- Return type:
- property dynamax_turns_left: int | None
- Returns:
How many turns of dynamax are left. None if dynamax is not active
- Return type:
int, optional
- property finished: bool
- Returns:
A boolean indicating whether the battle is finished.
- Return type:
Optional[bool]
- property format: str | None
- Returns:
The format of the battle, in accordance with Showdown protocol
- Return type:
Optional[str]
- property gen: int
- Returns:
The generation of the battle; will be the parameter with which the the battle was initiated
- Return type:
- get_pokemon(identifier: str, force_self_team: bool = False, details: str = '', request: Dict[str, Any] | None = None) Pokemon
Returns the Pokemon object corresponding to given identifier. Can force to return object from the player’s team if force_self_team is True. If the Pokemon object does not exist, it will be created. Details can be given, which is necessary to initialize alternate forms (eg. alolan pokemons) properly.
When the message says the pokemon is active we can check if this correct or Zoroark is playing tricks on us:
The way to check for a zoroark is seeing that the pokemon it is asking for isn’t currently active. For that we need to have the Pokemon instance of both the active and the believed active in the team. If we don’t we need to use the normal get_pokemon.
If both are the same, we can simply return either. If they are different, we need to check which one has illusion. If there is none, we log a warning, but this should never happen.
- If the Pokémon with the Illusion is not active, the following has occurred:
For some reason or another in the current turn, our Zoroark has switched in.
The message from the switch in has made it so the illusion is the active Pokémon
Now the illusion has been broken, it says that the active is Zoroark, but in our battle it isn’t.
We need to return the Zoroark.
If it isn’t broken, the request for the next turn will tell us the Zoroark is active. However the messages that tell us the active Pokémon will be referring to the illusion, so we use: if believed_active._ability == “illusion”:
- Parameters:
identifier (str) – The identifier to use to retrieve the pokemon.
force_self_team (bool) – Wheter to force returning a Pokemon from the player’s team. Defaults to False.
details (str, defaults to '') – Detailled information about the pokemon. Defaults to ‘’.
request (Dict, optional, defaults to None) – Detailled information about the pokemon from a request. Defaults to None.
- Returns:
The corresponding pokemon object.
- Return type:
- Raises:
ValueError – If the team has too many pokemons, as determined by the teamsize component of battle initialisation.
- property last_request: Dict[str, Any]
- The last request received from the server. This allows players to track
rqid and also maintain parallel battle copies for search/inference
- Returns:
The last request.
- Return type:
Dict[str, Any]
- property lost: bool | None
- Returns:
If the battle is finished, a boolean indicating whether the battle is lost. Otherwise None.
- Return type:
Optional[bool]
- property max_team_size: int | None
- Returns:
The maximum acceptable size of the team to return in teampreview, if applicable.
- Return type:
int, optional
- property opponent_dynamax_turns_left: int | None
- Returns:
How many turns of dynamax are left for the opponent’s pokemon. None if dynamax is not active
- Return type:
int | None
- property opponent_rating: int | None
Opponent’s rating after the end of the battle, if it was received.
- Returns:
The opponent’s rating after the end of the battle.
- Return type:
int, optional
- property opponent_role: str | None
- Returns:
Opponent’s role in given battle. p1/p2
- Return type:
str, optional
- property opponent_side_conditions: Dict[SideCondition, int]
- Returns:
The opponent’s side conditions. Keys are SideCondition objects, values are:
- the number of layers of the SideCondition if the side condition is
stackable
the turn where the SideCondition was setup otherwise
- Return type:
Dict[SideCondition, int]
- property opponent_team: Dict[str, Pokemon]
During teampreview, keys are not definitive: please rely on values.
- property opponent_used_dynamax: bool
- Returns:
Whether or not opponent’s current active pokemon can dynamax
- Return type:
- property opponent_used_mega_evolve: bool
- Returns:
Whether or not opponent’s current active pokemon can mega-evolve
- Return type:
- property opponent_used_tera: bool
- Returns:
Whether or not opponent’s current active pokemon can terastallize
- Return type:
- property opponent_used_z_move: bool
- Returns:
Whether or not opponent’s current active pokemon can z-move
- Return type:
- property opponent_username: str | None
- Returns:
The opponent’s username, or None if unknown.
- Return type:
str, optional.
- property player_role: str | None
- Returns:
Player’s role in given battle. p1/p2
- Return type:
str, optional
- property rating: int | None
Player’s rating after the end of the battle, if it was received.
- Returns:
The player’s rating after the end of the battle.
- Return type:
int, optional
- save_replay(file_path: str | Path) Path
Writes this battle replay to a file.
- Parameters:
file_path (str | pathlib.Path) – Path where replay html should be written.
- Returns:
The written replay path.
- Return type:
- property side_conditions: Dict[SideCondition, int]
- Returns:
The player’s side conditions. Keys are SideCondition objects, values are:
- the number of layers of the side condition if the side condition is
stackable
the turn where the SideCondition was setup otherwise
- Return type:
Dict[SideCondition, int]
- property teambuilder_team: list[TeambuilderPokemon] | None
- property teampreview: bool
- Returns:
Wheter the battle is awaiting a teampreview order.
- Return type:
- property teampreview_opponent_team: List[Pokemon]
- Returns:
The opponent’s team during teampreview.
- Return type:
List[Pokemon]
- property teampreview_team: List[Pokemon]
- Returns:
The player’s team during teampreview.
- Return type:
List[Pokemon]
- tied()
- property used_dynamax: bool
- Returns:
Whether or not the current active pokemon can dynamax
- Return type:
- property used_mega_evolve: bool
- Returns:
Whether or not the current active pokemon can mega evolve.
- Return type:
- property used_tera: bool
- Returns:
Whether or not the current active pokemon can terastallize
- Return type:
- property used_z_move: bool
- Returns:
Whether or not the current active pokemon can z-move.
- Return type:
- property wait: bool
- Returns:
If True, the battle does not currently need an action from the player.
- Return type:
Battle
- class poke_env.battle.battle.Battle(battle_tag: str, username: str, logger: Logger, gen: int, save_replays: str | bool = False)
Bases:
AbstractBattle- property all_active_pokemons: List[Pokemon | None]
- Returns:
A list containing all active pokemons and/or Nones.
- Return type:
List[Optional[Pokemon]]
- property available_moves: List[Move]
- Returns:
The list of moves the player can use during the current move request.
- Return type:
List[Move]
- property available_switches: List[Pokemon]
- Returns:
The list of switches the player can do during the current move request.
- Return type:
List[Pokemon]
- property can_dynamax: bool
- Returns:
Whether or not the current active pokemon can dynamax
- Return type:
- property can_mega_evolve: bool
- Returns:
Whether or not the current active pokemon can mega evolve.
- Return type:
- property can_tera: bool
- Returns:
Whether or not the current active pokemon can terastallize
- Return type:
- property can_z_move: bool
- Returns:
Whether or not the current active pokemon can z-move.
- Return type:
- clear_all_boosts()
- property force_switch: bool
- Returns:
A boolean indicating whether the active pokemon is forced to switch out.
- Return type:
Optional[bool]
- property grounded: bool
- Returns:
A boolean indicating whether the active pokemon is grounded
- Return type:
- property maybe_trapped: bool
- Returns:
A boolean indicating whether the active pokemon is maybe trapped by the opponent.
- Return type:
- parse_request(request: Dict[str, Any], strict_battle_tracking: bool = False)
Update the object from a request. The player’s pokemon are all updated, as well as available moves, switches and other related information (z move, mega evolution, forced switch…).
- Parameters:
request (dict) – Parsed JSON request object.
- property trapped: bool
- Returns:
A boolean indicating whether the active pokemon is trapped, either by the opponent or as a side effect of one your moves.
- Return type:
- property valid_orders: List[SingleBattleOrder]
Double Battle
- class poke_env.battle.double_battle.DoubleBattle(battle_tag: str, username: str, logger: Logger, gen: int, save_replays: str | bool = False)
Bases:
AbstractBattle- EMPTY_TARGET_POSITION = 0
- OPPONENT_1_POSITION = 1
- OPPONENT_2_POSITION = 2
- POKEMON_1_POSITION = -1
- POKEMON_2_POSITION = -2
- property active_pokemon: List[Pokemon | None]
- Returns:
The active pokemon, always at least one is not None
- Return type:
List[Optional[Pokemon]]
- property all_active_pokemons: List[Pokemon | None]
- Returns:
A list containing all active pokemons and/or Nones.
- Return type:
List[Optional[Pokemon]]
- property available_moves: List[List[Move]]
- Returns:
A list of two lists of moves the player can use during the current move request for each Pokemon.
- Return type:
List[List[Move]]
- property available_switches: List[List[Pokemon]]
- Returns:
The list of two lists of switches the player can do during the current move request for each active pokemon
- Return type:
List[List[Pokemon]]
- property can_dynamax: List[bool]
- Returns:
Whether or not the current active pokemon can dynamax
- Return type:
List[bool]
- property can_mega_evolve: List[bool]
- Returns:
Whether or not either current active pokemon can mega evolve.
- Return type:
List[bool]
- property can_tera: List[bool]
- Returns:
Whether or not the current active pokemon can terastallize. If yes, will be a PokemonType.
- Return type:
List[Union[bool, PokemonType]]
- property can_z_move: List[bool]
- Returns:
Whether or not the current active pokemon can z-move.
- Return type:
List[bool]
- clear_all_boosts()
- property force_switch: List[bool]
- Returns:
A boolean indicating whether the active pokemon is forced to switch out.
- Return type:
List[bool]
- get_possible_showdown_targets(move: Move, pokemon: Pokemon, dynamax: bool = False) List[int]
Given move of an ALLY Pokemon, returns a list of possible Pokemon Showdown targets for it. This is smart enough so that it figures whether the Pokemon is already dynamaxed.
- Parameters:
- Returns:
a list of integers indicating Pokemon Showdown targets: -1, -2, 1, 2 or self.EMPTY_TARGET_POSITION that indicates “no target”
- Return type:
List[int]
- property grounded: List[bool]
- Returns:
A boolean indicating whether the active pokemon are grounded
- Return type:
List[bool]
- property maybe_trapped: List[bool]
- Returns:
A boolean indicating whether either active pokemon is maybe trapped by the opponent.
- Return type:
List[bool]
- property opponent_active_pokemon: List[Pokemon | None]
- Returns:
The opponent active pokemon, always at least one is not None
- Return type:
List[Optional[Pokemon]]
- parse_request(request: Dict[str, Any], strict_battle_tracking: bool = False)
Update the object from a request. The player’s pokemon are all updated, as well as available moves, switches and other related information (z move, mega evolution, forced switch…).
- Parameters:
request (dict) – Parsed JSON request object.
- property reviving: bool
- Returns:
Whether or not any of the player’s Pokemon is reviving.
- Return type:
- to_showdown_target(move: Move, target_mon: Pokemon | None) int
Returns the correct Showdown target of the Pokemon to be targeted. It will return 0 if no target is needed or if the target_mon is not an active pokemon; this is meaningless in showdown
- property trapped: List[bool]
- Returns:
A boolean indicating whether either active pokemon is trapped by the opponent.
- Return type:
List[bool]
- property valid_orders: List[List[SingleBattleOrder]]
Target
This module defines the Target class, which represents the types of targets a move can have
- class poke_env.battle.target.Target(value)
Bases:
EnumEnumeration, representing targets for each move in a battle.
- ADJACENT_ALLY = 1
- ADJACENT_ALLY_OR_SELF = 2
- ADJACENT_FOE = 3
- ALL = 4
- ALLIES = 7
- ALLY_SIDE = 8
- ALLY_TEAM = 9
- ALL_ADJACENT = 5
- ALL_ADJACENT_FOES = 6
- ANY = 10
- FOE_SIDE = 11
- NORMAL = 12
- RANDOM_NORMAL = 13
- SCRIPTED = 14
- SELF = 15