PokéLLMon 源码解析(二)

2024-03-08 09:14:58 浏览数 (2)

.PokeLLMonpoke_envenvironmentdouble_battle.py

代码语言:javascript复制
# 从 logging 模块中导入 Logger 类
from logging import Logger
# 从 typing 模块中导入 Any, Dict, List, Optional, Union 类型
from typing import Any, Dict, List, Optional, Union

# 从 poke_env.environment.abstract_battle 模块中导入 AbstractBattle 类
from poke_env.environment.abstract_battle import AbstractBattle
# 从 poke_env.environment.move 模块中导入 SPECIAL_MOVES, Move 类
from poke_env.environment.move import SPECIAL_MOVES, Move
# 从 poke_env.environment.move_category 模块中导入 MoveCategory 类
from poke_env.environment.move_category import MoveCategory
# 从 poke_env.environment.pokemon 模块中导入 Pokemon 类
from poke_env.environment.pokemon import Pokemon
# 从 poke_env.environment.pokemon_type 模块中导入 PokemonType 类
from poke_env.environment.pokemon_type import PokemonType

# 定义 DoubleBattle 类,继承自 AbstractBattle 类
class DoubleBattle(AbstractBattle):
    # 定义常量 POKEMON_1_POSITION 为 -1
    POKEMON_1_POSITION = -1
    # 定义常量 POKEMON_2_POSITION 为 -2
    POKEMON_2_POSITION = -2
    # 定义常量 OPPONENT_1_POSITION 为 1
    OPPONENT_1_POSITION = 1
    # 定义常量 OPPONENT_2_POSITION 为 2
    OPPONENT_2_POSITION = 2
    # 定义常量 EMPTY_TARGET_POSITION 为 0,仅为符号,不被 showdown 使用

    # 初始化方法
    def __init__(
        self,
        battle_tag: str,
        username: str,
        logger: Logger,
        gen: int,
        save_replays: Union[str, bool] = False,
    ):
        # 调用父类的初始化方法
        super(DoubleBattle, self).__init__(
            battle_tag, username, logger, save_replays, gen=gen
        )

        # 初始化回合选择属性
        self._available_moves: List[List[Move]] = [[], []]
        self._available_switches: List[List[Pokemon]] = [[], []]
        self._can_mega_evolve: List[bool] = [False, False]
        self._can_z_move: List[bool] = [False, False]
        self._can_dynamax: List[bool] = [False, False]
        self._can_tera: List[Union[bool, PokemonType]] = [False, False]
        self._opponent_can_dynamax: List[bool] = [True, True]
        self._opponent_can_mega_evolve: List[bool] = [True, True]
        self._opponent_can_z_move: List[bool] = [True, True]
        self._force_switch: List[bool] = [False, False]
        self._maybe_trapped: List[bool] = [False, False]
        self._trapped: List[bool] = [False, False]

        # 初始化战斗状态属性
        self._active_pokemon: Dict[str, Pokemon] = {}
        self._opponent_active_pokemon: Dict[str, Pokemon] = {}

        # 其他属性
        self._move_to_pokemon_id: Dict[Move, str] = {}
    # 清除所有精灵的增益效果
    def clear_all_boosts(self):
        # 遍历己方和对手的所有精灵
        for active_pokemon_group in (self.active_pokemon, self.opponent_active_pokemon):
            for active_pokemon in active_pokemon_group:
                # 如果精灵存在,则清除其增益效果
                if active_pokemon is not None:
                    active_pokemon.clear_boosts()

    # 结束幻象状态
    def end_illusion(self, pokemon_name: str, details: str):
        # 获取玩家标识符和精灵标识符
        player_identifier = pokemon_name[:2]
        pokemon_identifier = pokemon_name[:3]
        # 根据玩家标识符确定要操作的精灵字典
        if player_identifier == self._player_role:
            active_dict = self._active_pokemon
        else:
            active_dict = self._opponent_active_pokemon
        # 获取要结束幻象状态的精灵
        active = active_dict.get(pokemon_identifier)
        
        # 在指定精灵上结束幻象状态
        active_dict[pokemon_identifier] = self._end_illusion_on(
            illusioned=active, illusionist=pokemon_name, details=details
        )

    # 获取活跃的精灵
    @staticmethod
    def _get_active_pokemon(
        active_pokemon: Dict[str, Pokemon], role: str
    ) -> List[Optional[Pokemon]]:
        # 获取指定角色的两只精灵
        pokemon_1 = active_pokemon.get(f"{role}a")
        pokemon_2 = active_pokemon.get(f"{role}b")
        # 如果第一只精灵不存在或不活跃或已经倒下,则置为None
        if pokemon_1 is None or not pokemon_1.active or pokemon_1.fainted:
            pokemon_1 = None
        # 如果第二只精灵不存在或不活跃或已经倒下,则置为None
        if pokemon_2 is None or not pokemon_2.active or pokemon_2.fainted:
            pokemon_2 = None
        # 返回两只精灵的列表
        return [pokemon_1, pokemon_2]

    # 精灵交换
    def switch(self, pokemon_str: str, details: str, hp_status: str):
        # 获取精灵标识符和玩家标识符
        pokemon_identifier = pokemon_str.split(":")[0][:3]
        player_identifier = pokemon_identifier[:2]
        # 根据玩家标识符确定要操作的精灵字典
        team = (
            self._active_pokemon
            if player_identifier == self._player_role
            else self._opponent_active_pokemon
        )
        # 弹出要交换出去的精灵
        pokemon_out = team.pop(pokemon_identifier, None)
        # 如果精灵存在,则执行交换出去的操作
        if pokemon_out is not None:
            pokemon_out.switch_out()
        # 获取要交换进来的精灵
        pokemon_in = self.get_pokemon(pokemon_str, details=details)
        # 执行交换进来的操作
        pokemon_in.switch_in()
        # 设置精灵的血量状态
        pokemon_in.set_hp_status(hp_status)
        # 将新的精灵放入对应的精灵字典中
        team[pokemon_identifier] = pokemon_in
    # 交换指定精灵和指定槽位的精灵
    def _swap(self, pokemon_str: str, slot: str):
        # 获取玩家标识符
        player_identifier = pokemon_str.split(":")[0][:2]
        # 根据玩家标识符确定当前活跃的精灵
        active = (
            self._active_pokemon
            if player_identifier == self.player_role
            else self._opponent_active_pokemon
        )
        # 确定玩家槽位标识符
        slot_a = f"{player_identifier}a"
        slot_b = f"{player_identifier}b"

        # 如果槽位A或槽位B的精灵已经倒下,则不执行交换操作
        if active[slot_a].fainted or active[slot_b].fainted:
            return

        # 获取槽位A和槽位B的精灵
        slot_a_mon = active[slot_a]
        slot_b_mon = active[slot_b]

        # 获取指定的精灵
        pokemon = self.get_pokemon(pokemon_str)

        # 如果槽位为0且精灵为槽位A的精灵,或者槽位为1且精灵为槽位B的精灵,则不执行交换操作
        if (slot == "0" and pokemon == slot_a_mon) or (
            slot == "1" and pokemon == slot_b_mon
        ):
            pass
        else:
            # 交换槽位A和槽位B的精灵
            active[slot_a], active[slot_b] = active[slot_b], active[slot_a]

    # 获取可能的对战目标
    def get_possible_showdown_targets(
        self, move: Move, pokemon: Pokemon, dynamax: bool = False
    @property
    # 返回当前活跃的精灵列表,至少有一个不为None
    def active_pokemon(self) -> List[Optional[Pokemon]]:
        """
        :return: The active pokemon, always at least one is not None
        :rtype: List[Optional[Pokemon]]
        """
        if self.player_role is None:
            raise ValueError("Unable to get active_pokemon, player_role is None")
        return self._get_active_pokemon(self._active_pokemon, self.player_role)

    @property
    # 返回所有活跃的精灵列表,包括玩家和对手的
    def all_active_pokemons(self) -> List[Optional[Pokemon]]:
        """
        :return: A list containing all active pokemons and/or Nones.
        :rtype: List[Optional[Pokemon]]
        """
        return [*self.active_pokemon, *self.opponent_active_pokemon]

    @property
    # 返回可用的招式列表
    def available_moves(self) -> List[List[Move]]:
        """
        :return: A list of two lists of moves the player can use during the current
            move request for each Pokemon.
        :rtype: List[List[Move]]
        """
        return self._available_moves

    @property
    def available_switches(self) -> List[List[Pokemon]]:
        """
        :return: The list of two lists of switches the player can do during the
            current move request for each active pokemon
        :rtype: List[List[Pokemon]]
        """
        # 返回玩家在当前移动请求期间每个活跃精灵可以执行的两个交换列表
        return self._available_switches

    @property
    def can_mega_evolve(self) -> List[bool]:
        """
        :return: Whether or not either current active pokemon can mega evolve.
        :rtype: List[bool]
        """
        # 返回当前活跃精灵是否可以超级进化的布尔值列表
        return self._can_mega_evolve

    @property
    def can_z_move(self) -> List[bool]:
        """
        :return: Whether or not the current active pokemon can z-move.
        :rtype: List[bool]
        """
        # 返回当前活跃精灵是否可以使用 Z 招式的布尔值列表
        return self._can_z_move

    @property
    def can_dynamax(self) -> List[bool]:
        """
        :return: Whether or not the current active pokemon can dynamax
        :rtype: List[bool]
        """
        # 返回当前活跃精灵是否可以极巨化的布尔值列表
        return self._can_dynamax

    @property
    def can_tera(self) -> List[Union[bool, PokemonType]]:
        """
        :return: Whether or not the current active pokemon can terastallize. If yes, will be a PokemonType.
        :rtype: List[Union[bool, PokemonType]]
        """
        # 返回当前活跃精灵是否可以进行特拉斯化的布尔值列表,如果可以,将是一个 PokemonType
        return self._can_tera

    @property
    def force_switch(self) -> List[bool]:
        """
        :return: A boolean indicating whether the active pokemon is forced
            to switch out.
        :rtype: List[bool]
        """
        # 返回一个布尔值,指示活跃精灵是否被迫交换出场
        return self._force_switch

    @property
    def maybe_trapped(self) -> List[bool]:
        """
        :return: A boolean indicating whether either active pokemon is maybe trapped
            by the opponent.
        :rtype: List[bool]
        """
        # 返回一个布尔值,指示任一活跃精灵是否可能被对手困住
        return self._maybe_trapped

    @property
    def opponent_active_pokemon(self) -> List[Optional[Pokemon]]:
        """
        :return: The opponent active pokemon, always at least one is not None
        :rtype: List[Optional[Pokemon]]
        """
        # 如果对手角色为空,则抛出数值错误异常
        if self.opponent_role is None:
            raise ValueError(
                "Unable to get opponent_active_pokemon, opponent_role is None"
            )
        # 返回对手当前活跃的宝可梦列表
        return self._get_active_pokemon(
            self._opponent_active_pokemon, self.opponent_role
        )

    @property
    def opponent_can_dynamax(self) -> List[bool]:
        """
        :return: Whether or not opponent's current active pokemons can dynamax
        :rtype: List[bool]
        """
        # 返回对手当前活跃的宝可梦是否可以极巨化的列表
        return self._opponent_can_dynamax

    @opponent_can_dynamax.setter
    def opponent_can_dynamax(self, value: Union[bool, List[bool]]):
        # 如果值是布尔类型,则设置对手当前活跃的宝可梦是否可以极巨化的列表为相同的值
        if isinstance(value, bool):
            self._opponent_can_dynamax = [value, value]
        else:
            self._opponent_can_dynamax = value

    @property
    def opponent_can_mega_evolve(self) -> List[bool]:
        """
        :return: Whether or not opponent's current active pokemons can mega evolve
        :rtype: List[bool]
        """
        # 返回对手当前活跃的宝可梦是否可以超级进化的列表
        return self._opponent_can_mega_evolve

    @opponent_can_mega_evolve.setter
    def opponent_can_mega_evolve(self, value: Union[bool, List[bool]]):
        # 如果值是布尔类型,则设置对手当前活跃的宝可梦是否可以超级进化的列表为相同的值
        if isinstance(value, bool):
            self._opponent_can_mega_evolve = [value, value]
        else:
            self._opponent_can_mega_evolve = value

    @property
    def opponent_can_z_move(self) -> List[bool]:
        """
        :return: Whether or not opponent's current active pokemons can z-move
        :rtype: List[bool]
        """
        # 返回对手当前活跃的宝可梦是否可以Z招式的列表
        return self._opponent_can_z_move

    @opponent_can_z_move.setter
    def opponent_can_z_move(self, value: Union[bool, List[bool]]):
        # 如果值是布尔类型,则设置对手当前活跃的宝可梦是否可以Z招式的列表为相同的值
        if isinstance(value, bool):
            self._opponent_can_z_move = [value, value]
        else:
            self._opponent_can_z_move = value

    @property
    # 返回一个布尔列表,指示当前双方是否有精灵被对手困住
    def trapped(self) -> List[bool]:
        """
        :return: A boolean indicating whether either active pokemon is trapped by the
            opponent.
        :rtype: List[bool]
        """
        # 返回私有属性_trapped
        return self._trapped

    # 设置trapped属性的setter方法
    @trapped.setter
    def trapped(self, value: List[bool]):
        # 将传入的值赋给私有属性_trapped
        self._trapped = value

.PokeLLMonpoke_envenvironmenteffect.py

代码语言:javascript复制
"""This module defines the Effect class, which represents in-game effects.
"""
# 导入必要的模块
from __future__ import annotations
import logging
from enum import Enum, auto, unique
from typing import Set

# 定义 Effect 枚举类,表示精灵可能受到的效果
@unique
class Effect(Enum):
    """Enumeration, represent an effect a Pokemon can be affected by."""

    # 枚举各种效果
    UNKNOWN = auto()
    AFTER_YOU = auto()
    AFTERMATH = auto()
    AQUA_RING = auto()
    AROMATHERAPY = auto()
    AROMA_VEIL = auto()
    ATTRACT = auto()
    AUTOTOMIZE = auto()
    BAD_DREAMS = auto()
    BANEFUL_BUNKER = auto()
    BATTLE_BOND = auto()
    BIDE = auto()
    BIND = auto()
    BURN_UP = auto()
    CELEBRATE = auto()
    CHARGE = auto()
    CLAMP = auto()
    CONFUSION = auto()
    COURT_CHANGE = auto()
    CRAFTY_SHIELD = auto()
    CUD_CHEW = auto()
    CURSE = auto()
    CUSTAP_BERRY = auto()
    DANCER = auto()
    DESTINY_BOND = auto()
    DISABLE = auto()
    DISGUISE = auto()
    DOOM_DESIRE = auto()
    DYNAMAX = auto()
    EERIE_SPELL = auto()
    ELECTRIC_TERRAIN = auto()
    EMBARGO = auto()
    EMERGENCY_EXIT = auto()
    ENCORE = auto()
    ENDURE = auto()
    FALLEN = auto()
    FALLEN1 = auto()
    FALLEN2 = auto()
    FALLEN3 = auto()
    FALLEN4 = auto()
    FALLEN5 = auto()
    FAIRY_LOCK = auto()
    FEINT = auto()
    FIRE_SPIN = auto()
    FLASH_FIRE = auto()
    FLOWER_VEIL = auto()
    FOCUS_BAND = auto()
    FOCUS_ENERGY = auto()
    FORESIGHT = auto()
    FOREWARN = auto()
    FUTURE_SIGHT = auto()
    G_MAX_CENTIFERNO = auto()
    G_MAX_CHI_STRIKE = auto()
    G_MAX_ONE_BLOW = auto()
    G_MAX_RAPID_FLOW = auto()
    G_MAX_SANDBLAST = auto()
    GRAVITY = auto()
    GRUDGE = auto()
    GUARD_SPLIT = auto()
    GULP_MISSILE = auto()
    HADRON_ENGINE = auto()
    HEAL_BELL = auto()
    HEAL_BLOCK = auto()
    HEALER = auto()
    HYDRATION = auto()
    HYPERSPACE_FURY = auto()
    HYPERSPACE_HOLE = auto()
    ICE_FACE = auto()
    ILLUSION = auto()
    IMMUNITY = auto()
    IMPRISON = auto()
    INFESTATION = auto()
    # 定义自动递增的枚举值,用于表示不同的技能或状态
    INGRAIN = auto()
    INNARDS_OUT = auto()
    INSOMNIA = auto()
    IRON_BARBS = auto()
    LASER_FOCUS = auto()
    LEECH_SEED = auto()
    LEPPA_BERRY = auto()
    LIGHTNING_ROD = auto()
    LIMBER = auto()
    LIQUID_OOZE = auto()
    LOCK_ON = auto()
    MAGMA_STORM = auto()
    MAGNET_RISE = auto()
    MAGNITUDE = auto()
    MAT_BLOCK = auto()
    MAX_GUARD = auto()
    MIMIC = auto()
    MIMICRY = auto()
    MIND_READER = auto()
    MINIMIZE = auto()
    MIRACLE_EYE = auto()
    MIST = auto()
    MISTY_TERRAIN = auto()
    MUMMY = auto()
    NEUTRALIZING_GAS = auto()
    NIGHTMARE = auto()
    NO_RETREAT = auto()
    OBLIVIOUS = auto()
    OCTOLOCK = auto()
    ORICHALCUM_PULSE = auto()
    OWN_TEMPO = auto()
    PASTEL_VEIL = auto()
    PERISH0 = auto()
    PERISH1 = auto()
    PERISH2 = auto()
    PERISH3 = auto()
    PHANTOM_FORCE = auto()
    POLTERGEIST = auto()
    POWDER = auto()
    POWER_CONSTRUCT = auto()
    POWER_SPLIT = auto()
    POWER_TRICK = auto()
    PROTECT = auto()
    PROTECTIVE_PADS = auto()
    PROTOSYNTHESIS = auto()
    PROTOSYNTHESISATK = auto()
    PROTOSYNTHESISDEF = auto()
    PROTOSYNTHESISSPA = auto()
    PROTOSYNTHESISSPD = auto()
    PROTOSYNTHESISSPE = auto()
    PSYCHIC_TERRAIN = auto()
    PURSUIT = auto()
    QUARK_DRIVE = auto()
    QUARKDRIVEATK = auto()
    QUARKDRIVEDEF = auto()
    QUARKDRIVESPA = auto()
    QUARKDRIVESPD = auto()
    QUARKDRIVESPE = auto()
    QUASH = auto()
    QUICK_CLAW = auto()
    QUICK_GUARD = auto()
    REFLECT = auto()
    RIPEN = auto()
    ROUGH_SKIN = auto()
    SAFEGUARD = auto()
    SAFETY_GOGGLES = auto()
    SALT_CURE = auto()
    SAND_TOMB = auto()
    SCREEN_CLEANER = auto()
    SHADOW_FORCE = auto()
    SHED_SKIN = auto()
    SKETCH = auto()
    SKILL_SWAP = auto()
    SKY_DROP = auto()
    SLOW_START = auto()
    SMACK_DOWN = auto()
    SNAP_TRAP = auto()
    SNATCH = auto()
    SPEED_SWAP = auto()
    SPITE = auto()
    STICKY_HOLD = auto()
    STICKY_WEB = auto()
    # 定义自动递增的枚举值,用于表示不同的效果
    STOCKPILE = auto()
    STOCKPILE1 = auto()
    STOCKPILE2 = auto()
    STOCKPILE3 = auto()
    STORM_DRAIN = auto()
    STRUGGLE = auto()
    SUBSTITUTE = auto()
    SUCTION_CUPS = auto()
    SUPREME_OVERLORD = auto()
    SWEET_VEIL = auto()
    SYMBIOSIS = auto()
    SYNCHRONIZE = auto()
    TAR_SHOT = auto()
    TAUNT = auto()
    TELEKINESIS = auto()
    TELEPATHY = auto()
    TIDY_UP = auto()
    TOXIC_DEBRIS = auto()
    THERMAL_EXCHANGE = auto()
    THROAT_CHOP = auto()
    THUNDER_CAGE = auto()
    TORMENT = auto()
    TRAPPED = auto()
    TRICK = auto()
    TYPEADD = auto()
    TYPECHANGE = auto()
    TYPE_CHANGE = auto()
    UPROAR = auto()
    VITAL_SPIRIT = auto()
    WANDERING_SPIRIT = auto()
    WATER_BUBBLE = auto()
    WATER_VEIL = auto()
    WHIRLPOOL = auto()
    WIDE_GUARD = auto()
    WIMP_OUT = auto()
    WRAP = auto()
    YAWN = auto()
    ZERO_TO_HERO = auto()

    # 定义返回对象描述字符串的方法
    def __str__(self) -> str:
        return f"{self.name} (effect) object"

    # 静态方法
    @staticmethod
    def from_showdown_message(message: str) -> Effect:
        """Returns the Effect object corresponding to the message.

        :param message: The message to convert.
        :type message: str
        :return: The corresponding Effect object.
        :rtype: Effect
        """
        # 替换消息中的特定字符串
        message = message.replace("item: ", "")
        message = message.replace("move: ", "")
        message = message.replace("ability: ", "")
        message = message.replace(" ", "_")
        message = message.replace("-", "_")
        message = message.upper()

        # 处理特殊情况
        if message == "FALLENUNDEFINED":
            message = "FALLEN"

        try:
            # 尝试返回对应的 Effect 对象
            return Effect[message]
        except KeyError:
            # 处理异常情况并返回默认值
            logging.getLogger("poke-env").warning(
                "Unexpected effect '%s' received. Effect.UNKNOWN will be used instead. "
                "If this is unexpected, please open an issue at "
                "https://github.com/hsahovic/poke-env/issues/ along with this error "
                "message and a description of your program.",
                message,
            )
            return Effect.UNKNOWN

    @property
    def breaks_protect(self):
        """
        :return: Whether this effect breaks protect-like states.
        :rtype: bool
        """
        # 检查该效果是否打破保护状态
        return self in _PROTECT_BREAKING_EFFECTS

    @property
    def is_turn_countable(self) -> bool:
        """
        :return: Whether it is useful to keep track of the number of turns this effect
            has been active for.
        :rtype: bool
        """
        # 检查是否需要跟踪该效果已经激活的回合数
        return self in _TURN_COUNTER_EFFECTS

    @property
    def is_action_countable(self) -> bool:
        """
        :return: Whether it is useful to keep track of the number of times this effect
            has been activated.
        :rtype: bool
        """
        # 检查是否需要跟踪该效果已经激活的次数
        return self in _TURN_COUNTER_EFFECTS
# 定义一组保护破坏效果的集合,包括FEINT、SHADOW_FORCE、PHANTOM_FORCE、HYPERSPACE_FURY、HYPERSPACE_HOLE
_PROTECT_BREAKING_EFFECTS: Set[Effect] = {
    Effect.FEINT,
    Effect.SHADOW_FORCE,
    Effect.PHANTOM_FORCE,
    Effect.HYPERSPACE_FURY,
    Effect.HYPERSPACE_HOLE,
}

# 定义一组回合计数效果的集合,包括BIND、CLAMP、DISABLE、DYNAMAX、EMBARGO、ENCORE等
_TURN_COUNTER_EFFECTS: Set[Effect] = {
    Effect.BIND,
    Effect.CLAMP,
    Effect.DISABLE,
    Effect.DYNAMAX,
    Effect.EMBARGO,
    Effect.ENCORE,
    Effect.FIRE_SPIN,
    Effect.HEAL_BLOCK,
    Effect.INFESTATION,
    Effect.MAGMA_STORM,
    Effect.MAGNET_RISE,
    Effect.SAND_TOMB,
    Effect.SKY_DROP,
    Effect.SLOW_START,
    Effect.TAUNT,
    Effect.WHIRLPOOL,
    Effect.WRAP,
}

# 定义一组动作计数效果的集合,包括CONFUSION和TORMENT
_ACTION_COUNTER_EFFECTS: Set[Effect] = {Effect.CONFUSION, Effect.TORMENT}

.PokeLLMonpoke_envenvironmentfield.py

代码语言:javascript复制
"""This module defines the Field class, which represents a battle field.
"""
# 导入必要的模块
from __future__ import annotations

import logging
from enum import Enum, auto, unique

# 定义 Field 类,表示战斗中的一个场地
@unique
class Field(Enum):
    """Enumeration, represent a non null field in a battle."""

    UNKNOWN = auto()
    ELECTRIC_TERRAIN = auto()
    GRASSY_TERRAIN = auto()
    GRAVITY = auto()
    HEAL_BLOCK = auto()
    MAGIC_ROOM = auto()
    MISTY_TERRAIN = auto()
    MUD_SPORT = auto()
    MUD_SPOT = auto()
    PSYCHIC_TERRAIN = auto()
    TRICK_ROOM = auto()
    WATER_SPORT = auto()
    WONDER_ROOM = auto()

    def __str__(self) -> str:
        return f"{self.name} (field) object"

    @staticmethod
    def from_showdown_message(message: str) -> Field:
        """Returns the Field object corresponding to the message.

        :param message: The message to convert.
        :type message: str
        :return: The corresponding Field object.
        :rtype: Field
        """
        # 处理消息,将空格替换为下划线
        message = message.replace("move: ", "")
        message = message.replace(" ", "_")

        # 处理特殊情况,将以 terrain 结尾的消息转换为以 _TERRAIN 结尾
        if message.endswith("terrain") and not message.endswith("_terrain"):
            message = message.replace("terrain", "_terrain")

        try:
            return Field[message.upper()]
        except KeyError:
            # 如果无法找到对应的 Field 对象,则记录警告信息并返回 Field.UNKNOWN
            logging.getLogger("poke-env").warning(
                "Unexpected field '%s' received. Field.UNKNOWN will be used instead. "
                "If this is unexpected, please open an issue at "
                "https://github.com/hsahovic/poke-env/issues/ along with this error "
                "message and a description of your program.",
                message,
            )
            return Field.UNKNOWN

    @property
    def is_terrain(self) -> bool:
        """Wheter this field is a terrain."""
        return self.name.endswith("_TERRAIN")

.PokeLLMonpoke_envenvironmenthelper.py

代码语言:javascript复制
# 导入 math 模块,用于数学运算
import math

.PokeLLMonpoke_envenvironmentmove.py

代码语言:javascript复制
# 导入 copy 模块,用于深拷贝对象
import copy
# 导入 lru_cache 装饰器,用于缓存函数的结果
from functools import lru_cache
# 导入 Any、Dict、List、Optional、Set、Tuple、Union 类型提示
from typing import Any, Dict, List, Optional, Set, Tuple, Union

# 导入 GenData、to_id_str 函数
from poke_env.data import GenData, to_id_str
# 导入 Field 类
from poke_env.environment.field import Field
# 导入 MoveCategory 枚举
from poke_env.environment.move_category import MoveCategory
# 导入 PokemonType 枚举
from poke_env.environment.pokemon_type import PokemonType
# 导入 Status 枚举
from poke_env.environment.status import Status
# 导入 Weather 枚举
from poke_env.environment.weather import Weather

# 特殊招式集合,包含 "struggle" 和 "recharge"
SPECIAL_MOVES: Set[str] = {"struggle", "recharge"}

# 保护招式集合
_PROTECT_MOVES = {
    "protect",
    "detect",
    "endure",
    "spikyshield",
    "kingsshield",
    "banefulbunker",
    "obstruct",
    "maxguard",
}
# 辅助保护招式集合
_SIDE_PROTECT_MOVES = {"wideguard", "quickguard", "matblock"}
# 总保护招式集合,包含保护招式和辅助保护招式
_PROTECT_COUNTER_MOVES = _PROTECT_MOVES | _SIDE_PROTECT_MOVES

# 招式类
class Move:
    # 杂项标志列表
    _MISC_FLAGS = [
        "onModifyMove",
        "onEffectiveness",
        "onHitField",
        "onAfterMoveSecondarySelf",
        "onHit",
        "onTry",
        "beforeTurnCallback",
        "onAfterMove",
        "onTryHit",
        "onTryMove",
        "hasCustomRecoil",
        "onMoveFail",
        "onPrepareHit",
        "onAfterHit",
        "onBasePower",
        "basePowerCallback",
        "damageCallback",
        "onTryHitSide",
        "beforeMoveCallback",
    ]
    # 每种宝可梦类型对应的招式类别预分配字典
    _MOVE_CATEGORY_PER_TYPE_PRE_SPLIT = {
        PokemonType.BUG: MoveCategory.PHYSICAL,
        PokemonType.DARK: MoveCategory.SPECIAL,
        PokemonType.DRAGON: MoveCategory.SPECIAL,
        PokemonType.ELECTRIC: MoveCategory.SPECIAL,
        PokemonType.FIGHTING: MoveCategory.PHYSICAL,
        PokemonType.FIRE: MoveCategory.SPECIAL,
        PokemonType.FLYING: MoveCategory.PHYSICAL,
        PokemonType.GHOST: MoveCategory.PHYSICAL,
        PokemonType.GRASS: MoveCategory.SPECIAL,
        PokemonType.GROUND: MoveCategory.PHYSICAL,
        PokemonType.ICE: MoveCategory.SPECIAL,
        PokemonType.NORMAL: MoveCategory.PHYSICAL,
        PokemonType.POISON: MoveCategory.PHYSICAL,
        PokemonType.PSYCHIC: MoveCategory.SPECIAL,
        PokemonType.ROCK: MoveCategory.PHYSICAL,
        PokemonType.STEEL: MoveCategory.PHYSICAL,
        PokemonType.WATER: MoveCategory.SPECIAL,
    }

    # 招式对象的属性列表
    __slots__ = (
        "_id",
        "_base_power_override",
        "_current_pp",
        "_dynamaxed_move",
        "_gen",
        "_is_empty",
        "_moves_dict",
        "_request_target",
    )

    # 初始化招式对象
    def __init__(self, move_id: str, gen: int, raw_id: Optional[str] = None):
        self._id = move_id
        self._base_power_override = None
        self._gen = gen
        self._moves_dict = GenData.from_gen(gen).moves

        # 处理隐藏力招式
        if move_id.startswith("hiddenpower") and raw_id is not None:
            base_power = "".join([c for c in raw_id if c.isdigit()])
            self._id = "".join([c for c in to_id_str(raw_id) if not c.isdigit()])

            if base_power:
                try:
                    base_power = int(base_power)
                    self._base_power_override = base_power
                except ValueError:
                    pass

        self._current_pp = self.max_pp
        self._is_empty: bool = False

        self._dynamaxed_move = None
        self._request_target = None

    # 返回招式对象的字符串表示
    def __repr__(self) -> str:
        return f"{self._id} (Move object)"
    # 减少当前 PP 值
    def use(self):
        self._current_pp -= 1

    # 判断给定的招式 ID 是否为 Z 招式
    @staticmethod
    def is_id_z(id_: str, gen: int) -> bool:
        if id_.startswith("z") and id_[1:] in GenData.from_gen(gen).moves:
            return True
        return "isZ" in GenData.from_gen(gen).moves[id_]

    # 判断给定的招式 ID 是否为 Max 招式
    @staticmethod
    def is_max_move(id_: str, gen: int) -> bool:
        if id_.startswith("max"):
            return True
        elif (
            GenData.from_gen(gen).moves[id_].get("isNonstandard", None) == "Gigantamax"
        ):
            return True
        elif GenData.from_gen(gen).moves[id_].get("isMax", None) is not None:
            return True
        return False

    # 判断给定的招式是否应该被存储
    @staticmethod
    @lru_cache(4096)
    def should_be_stored(move_id: str, gen: int) -> bool:
        if move_id in SPECIAL_MOVES:
            return False
        if move_id not in GenData.from_gen(gen).moves:
            return False
        if Move.is_id_z(move_id, gen):
            return False
        if Move.is_max_move(move_id, gen):
            return False
        return True

    # 获取招式的命中率(0 到 1 的范围)
    @property
    def accuracy(self) -> float:
        """
        :return: The move's accuracy (0 to 1 scale).
        :rtype: float
        """
        accuracy = self.entry["accuracy"]
        if accuracy is True:
            return 1.0
        return accuracy / 100

    # 获取招式的基础威力
    @property
    def base_power(self) -> int:
        """
        :return: The move's base power.
        :rtype: int
        """
        if self._base_power_override is not None:
            return self._base_power_override
        return self.entry.get("basePower", 0)

    # 获取招式对目标的增益效果
    @property
    def boosts(self) -> Optional[Dict[str, int]]:
        """
        :return: Boosts conferred to the target by using the move.
        :rtype: Dict[str, float] | None
        """
        return self.entry.get("boosts", None)
    # 返回移动是否打破类似保护的防御
    def breaks_protect(self) -> bool:
        """
        :return: Whether the move breaks proect-like defenses.
        :rtype: bool
        """
        return self.entry.get("breaksProtect", False)

    # 返回是否存在此移动的 Z-移动版本
    @property
    def can_z_move(self) -> bool:
        """
        :return: Wheter there exist a z-move version of this move.
        :rtype: bool
        """
        return self.id not in SPECIAL_MOVES

    # 返回移动的类别
    @property
    def category(self) -> MoveCategory:
        """
        :return: The move category.
        :rtype: MoveCategory
        """
        # 如果条目中没有类别信息,则打印出当前移动和条目
        if "category" not in self.entry:
            print(self, self.entry)

        # 如果世代小于等于3且类别为"PHYSICAL"或"SPECIAL",则返回对应的移动类别
        if self._gen <= 3 and self.entry["category"].upper() in {
            "PHYSICAL",
            "SPECIAL",
        }:
            return self._MOVE_CATEGORY_PER_TYPE_PRE_SPLIT[self.type]
        return MoveCategory[self.entry["category"].upper()]

    # 返回移动的暴击率。如果移动保证暴击,则返回6
    @property
    def crit_ratio(self) -> int:
        """
        :return: The move's crit ratio. If the move is guaranteed to crit, returns 6.
        :rtype:
        """
        if "critRatio" in self.entry:
            return int(self.entry["critRatio"])
        elif "willCrit" in self.entry:
            return 6
        return 0

    # 返回当前剩余的PP
    @property
    def current_pp(self) -> int:
        """
        :return: Remaining PP.
        :rtype: int
        """
        return self._current_pp

    # 返回移动的固定伤害。可以是整数或'level',例如Seismic Toss
    @property
    def damage(self) -> Union[int, str]:
        """
        :return: The move's fix damages. Can be an int or 'level' for moves such as
            Seismic Toss.
        :rtype: Union[int, str]
        """
        return self.entry.get("damage", 0)
    def deduced_target(self) -> Optional[str]:
        """
        :return: Move deduced target, based on Move.target and showdown's request
            messages.
        :rtype: str, optional
        """
        # 如果移动在特殊移动列表中,则返回移动的目标
        if self.id in SPECIAL_MOVES:
            return self.target
        # 如果有请求目标,则返回请求目标
        elif self.request_target:
            return self.request_target
        # 如果目标是"randomNormal",则返回请求目标
        elif self.target == "randomNormal":
            return self.request_target
        # 否则返回移动的目标
        return self.target

    @property
    def defensive_category(self) -> MoveCategory:
        """
        :return: Move's defender category.
        :rtype: MoveCategory
        """
        # 如果存在"overrideDefensiveStat"在条目中
        if "overrideDefensiveStat" in self.entry:
            # 如果"overrideDefensiveStat"为"def",则返回物理类别
            if self.entry["overrideDefensiveStat"] == "def":
                return MoveCategory["PHYSICAL"]
            # 如果"overrideDefensiveStat"为"spd",则返回特殊类别
            elif self.entry["overrideDefensiveStat"] == "spd":
                return MoveCategory["SPECIAL"]
            # 否则抛出值错误异常
            else:
                raise ValueError(
                    f"Unsupported value for overrideDefensiveStat: {self.entry['overrideDefensiveStat']}"
                )
        # 否则返回移动的类别
        return self.category

    @property
    def drain(self) -> float:
        """
        :return: Ratio of HP of inflicted damages, between 0 and 1.
        :rtype: float
        """
        # 如果存在"drain"在条目中,则返回伤害的HP比例
        if "drain" in self.entry:
            return self.entry["drain"][0] / self.entry["drain"][1]
        # 否则返回0.0
        return 0.0

    @property
    def dynamaxed(self):
        """
        :return: The dynamaxed version of the move.
        :rtype: DynamaxMove
        """
        # 如果存在动态移动,则返回动态移动
        if self._dynamaxed_move:
            return self._dynamaxed_move

        # 否则创建动态移动并返回
        self._dynamaxed_move = DynamaxMove(self)
        return self._dynamaxed_move

    @property
    def entry(self) -> Dict[str, Any]:
        """
        Should not be used directly.

        :return: The data entry corresponding to the move
        :rtype: Dict
        """
        # 检查是否存在与当前移动ID对应的数据条目,如果存在则返回该数据条目
        if self._id in self._moves_dict:
            return self._moves_dict[self._id]
        # 如果移动ID以"z"开头且去掉"z"后的部分在数据字典中存在,则返回对应的数据条目
        elif self._id.startswith("z") and self._id[1:] in self._moves_dict:
            return self._moves_dict[self._id[1:]]
        # 如果移动ID为"recharge",则返回一个预定义的数据条目
        elif self._id == "recharge":
            return {"pp": 1, "type": "normal", "category": "Special", "accuracy": 1}
        else:
            # 抛出数值错误,表示未知的移动ID
            raise ValueError("Unknown move: %s" % self._id)

    @property
    def expected_hits(self) -> float:
        """
        :return: Expected number of hits, between 1 and 5. Equal to n_hits if n_hits is
            constant.
        :rtype: float
        """
        # 对于特定的移动ID,返回预期的击中次数
        if self._id == "triplekick" or self._id == "tripleaxel":
            # 对于Triple Kick和Triple Axel,每次击中都有准确性检查,并且每次击中的威力逐渐增加
            return 1   2 * 0.9   3 * 0.81
        min_hits, max_hits = self.n_hit
        if min_hits == max_hits:
            return min_hits
        else:
            # 如果击中次数不固定,则返回一个范围内的预期值
            assert (
                min_hits == 2 and max_hits == 5
            ), f"Move {self._id} expected to hit 2-5 times. Got {min_hits}-{max_hits}"
            return (2   3) / 3   (4   5) / 6

    @property
    def flags(self) -> Set[str]:
        """
        This property is not well defined, and may be missing some information.
        If you need more information on some flag, please open an issue in the project.

        :return: Flags associated with this move. These can come from the data or be
            custom.
        :rtype: Set[str]
        """
        # 获取与该移动相关的标志,可能来自数据或自定义
        flags = set(self.entry["flags"])
        flags.update(set(self.entry.keys()).intersection(self._MISC_FLAGS))
        return flags

    @property
    def force_switch(self) -> bool:
        """
        :return: Whether this move forces switches.
        :rtype: bool
        """
        # 返回该移动是否强制交换精灵的布尔值
        return self.entry.get("forceSwitch", False)

    @property
    def heal(self) -> float:
        """
        :return: Proportion of the user's HP recovered.
        :rtype: float
        """
        # 如果"heal"在self.entry中
        if "heal" in self.entry:
            # 返回用户恢复的HP比例
            return self.entry["heal"][0] / self.entry["heal"][1]
        return 0.0

    @property
    def id(self) -> str:
        """
        :return: Move id.
        :rtype: str
        """
        # 返回移动的id
        return self._id

    @property
    def ignore_ability(self) -> bool:
        """
        :return: Whether the move ignore its target's ability.
        :rtype: bool
        """
        # 返回该移动是否忽略目标精灵的能力
        return self.entry.get("ignoreAbility", False)

    @property
    def ignore_defensive(self) -> bool:
        """
        :return: Whether the opponent's stat boosts are ignored.
        :rtype: bool
        """
        # 返回对手的属性提升是否被忽略
        return self.entry.get("ignoreDefensive", False)

    @property
    def ignore_evasion(self) -> bool:
        """
        :return: Wheter the opponent's evasion is ignored.
        :rtype: bool
        """
        # 返回对手的闪避是否被忽略
        return self.entry.get("ignoreEvasion", False)

    @property
    def ignore_immunity(self) -> Union[bool, Set[PokemonType]]:
        """
        :return: Whether the opponent's immunity is ignored, or a list of ignored
            immunities.
        :rtype: bool or set of Types
        """
        # 如果"ignoreImmunity"在self.entry中
        if "ignoreImmunity" in self.entry:
            # 如果self.entry["ignoreImmunity"]是布尔值
            if isinstance(self.entry["ignoreImmunity"], bool):
                return self.entry["ignoreImmunity"]
            else:
                # 返回被忽略的免疫类型的集合
                return {
                    PokemonType[t.upper().replace("'", "")]
                    for t in self.entry["ignoreImmunity"].keys()
                }
        return False

    @property
    def is_empty(self) -> bool:
        """
        :return: Whether the move is an empty move.
        :rtype: bool
        """
        # 返回移动是否为空的布尔值
        return self._is_empty

    @property
    def is_protect_counter(self) -> bool:
        """
        :return: Wheter this move increments a mon's protect counter.
        :rtype: int
        """
        # 返回移动是否增加宝可梦的保护计数器的布尔值
        return self._id in _PROTECT_COUNTER_MOVES

    @property
    def is_protect_move(self) -> bool:
        """
        :return: Wheter this move is a protect-like move.
        :rtype: int
        """
        # 返回移动是否类似于保护的布尔值
        return self._id in _PROTECT_MOVES

    @property
    def is_side_protect_move(self) -> bool:
        """
        :return: Wheter this move is a side-protect move.
        :rtype: int
        """
        # 返回移动是否是侧面保护的布尔值
        return self._id in _SIDE_PROTECT_MOVES

    @property
    def is_z(self) -> bool:
        """
        :return: Whether the move is a z move.
        :rtype: bool
        """
        # 返回移动是否是 Z 移动的布尔值
        return Move.is_id_z(self.id, gen=self._gen)

    @property
    def max_pp(self) -> int:
        """
        :return: The move's max pp.
        :rtype: int
        """
        # 返回移动的最大 PP 值
        return self.entry["pp"] * 8 // 5

    @property
    def n_hit(self) -> Tuple[int, int]:
        """
        :return: How many hits this move lands. Tuple of the form (min, max).
        :rtype: Tuple
        """
        # 返回移动命中的次数。返回形式为 (最小值, 最大值) 的元组
        if "multihit" in self.entry:
            if isinstance(self.entry["multihit"], list):
                assert len(self.entry["multihit"]) == 2
                min_hits, max_hits = self.entry["multihit"]
                return min_hits, max_hits
            else:
                return self.entry["multihit"], self.entry["multihit"]
        return 1, 1

    @property
    def no_pp_boosts(self) -> bool:
        """
        :return: Whether the move uses PPs.
        :rtype: bool
        """
        # 返回移动是否使用 PPs 的布尔值
        return "noPPBoosts" in self.entry
    def non_ghost_target(self) -> bool:
        """
        :return: True if the move targets non-ghost Pokemon.
        :rtype: bool
        """
        # Check if the key "nonGhostTarget" exists in the entry dictionary
        return "nonGhostTarget" in self.entry

    @property
    def priority(self) -> int:
        """
        :return: Priority of the move.
        :rtype: int
        """
        # Return the value associated with the key "priority" in the entry dictionary
        return self.entry["priority"]

    @property
    def pseudo_weather(self) -> str:
        """
        :return: Pseudo-weather triggered by this move.
        :rtype: str
        """
        # Return the value associated with the key "pseudoWeather" in the entry dictionary, or None if not found
        return self.entry.get("pseudoWeather", None)

    @property
    def recoil(self) -> float:
        """
        :return: Percentage of damage inflicted by the move as recoil.
        :rtype: float
        """
        # Calculate and return the recoil proportion based on the values in the entry dictionary
        if "recoil" in self.entry:
            return self.entry["recoil"][0] / self.entry["recoil"][1]
        elif "struggleRecoil" in self.entry:
            return 0.25
        return 0.0

    @property
    def request_target(self) -> Optional[str]:
        """
        :return: Target information provided by Showdown in a request message, if any.
        :rtype: str, optional
        """
        # Return the stored request target information
        return self._request_target

    @request_target.setter
    def request_target(self, request_target: Optional[str]):
        """
        :param request_target: Target information received from Showdown in a request message.
        :type request_target: str, optional
        """
        # Set the request target information received from Showdown
        self._request_target = request_target

    @staticmethod
    @lru_cache(maxsize=4096)
    def retrieve_id(move_name: str) -> str:
        """Retrieve the id of a move based on its full name.

        :param move_name: The string to convert into a move id.
        :type move_name: str
        :return: The corresponding move id.
        :rtype: str
        """
        # 将移动名称转换为标识符字符串
        move_name = to_id_str(move_name)
        # 如果移动名称以"return"开头,则返回"return"
        if move_name.startswith("return"):
            return "return"
        # 如果移动名称以"frustration"开头,则返回"frustration"
        if move_name.startswith("frustration"):
            return "frustration"
        # 如果移动名称以"hiddenpower"开头,则返回"hiddenpower"
        if move_name.startswith("hiddenpower"):
            return "hiddenpower"
        # 否则返回移动名称
        return move_name

    @property
    def secondary(self) -> List[Dict[str, Any]]:
        """
        :return: Secondary effects. At this point, the precise content of this property
            is not too clear.
        :rtype: Optional[Dict]
        """
        # 如果属性中包含"secondary"且不为空,则返回包含"secondary"的列表
        if "secondary" in self.entry and self.entry["secondary"]:
            return [self.entry["secondary"]]
        # 如果属性中包含"secondaries",则返回"secondaries"
        elif "secondaries" in self.entry:
            return self.entry["secondaries"]
        # 否则返回空列表
        return []

    @property
    def self_boost(self) -> Optional[Dict[str, int]]:
        """
        :return: Boosts applied to the move's user.
        :rtype: Dict[str, int]
        """
        # 如果属性中包含"selfBoost",则返回"selfBoost"中的"boosts",否则返回None
        if "selfBoost" in self.entry:
            return self.entry["selfBoost"].get("boosts", None)
        # 如果属性中包含"self"且"boosts"在"self"中,则返回"self"中的"boosts"
        elif "self" in self.entry and "boosts" in self.entry["self"]:
            return self.entry["self"]["boosts"]
        # 否则返回None
        return None

    @property
    def self_destruct(self) -> Optional[str]:
        """
        :return: Move's self destruct consequences.
        :rtype: str | None
        """
        # 返回属性中的"selfdestruct",如果不存在则返回None
        return self.entry.get("selfdestruct", None)

    @property
    def self_switch(self) -> Union[str, bool]:
        """
        :return: What kind of self swtich this move implies for the user.
        :rtype: str | None
        """
        # 返回属性中的"selfSwitch",如果不存在则返回False
        return self.entry.get("selfSwitch", False)

    @property
    def side_condition(self) -> Optional[str]:
        """
        :return: Side condition inflicted by the move.
        :rtype: str | None
        """
        # 返回移动造成的副作用条件
        return self.entry.get("sideCondition", None)

    @property
    def sleep_usable(self) -> bool:
        """
        :return: Whether the move can be user by a sleeping pokemon.
        :rtype: bool
        """
        # 返回移动是否可以被睡眠的宝可梦使用
        return self.entry.get("sleepUsable", False)

    @property
    def slot_condition(self) -> Optional[str]:
        """
        :return: Which slot condition is started by this move.
        :rtype: str | None
        """
        # 返回由此招式引发的槽位条件
        return self.entry.get("slotCondition", None)

    @property
    def stalling_move(self) -> bool:
        """
        :return: Showdown classification of the move as a stalling move.
        :rtype: bool
        """
        # 返回招式作为拖延招式的Showdown分类
        return self.entry.get("stallingMove", False)

    @property
    def status(self) -> Optional[Status]:
        """
        :return: The status inflicted by the move.
        :rtype: Optional[Status]
        """
        # 如果字典中包含"status"键,则返回对应的Status枚举值,否则返回None
        if "status" in self.entry:
            return Status[self.entry["status"].upper()]
        return None

    @property
    def steals_boosts(self) -> bool:
        """
        :return: Whether the move steals its target's boosts.
        :rtype: bool
        """
        # 返回招式是否偷取目标的增益效果
        return self.entry.get("stealsBoosts", False)
    # 返回移动的目标类型
    def target(self) -> str:
        """
        :return: Move target. Possible targets (copied from PS codebase):

            * adjacentAlly - Only relevant to Doubles or Triples, the move only
              targets an ally of the user.
            * adjacentAllyOrSelf - The move can target the user or its ally.
            * adjacentFoe - The move can target a foe, but not (in Triples)
              a distant foe.
            * all - The move targets the field or all Pokémon at once.
            * allAdjacent - The move is a spread move that also hits the user's ally.
            * allAdjacentFoes - The move is a spread move.
            * allies - The move affects all active Pokémon on the user's team.
            * allySide - The move adds a side condition on the user's side.
            * allyTeam - The move affects all unfainted Pokémon on the user's team.
            * any - The move can hit any other active Pokémon, not just those adjacent.
            * foeSide - The move adds a side condition on the foe's side.
            * normal - The move can hit one adjacent Pokémon of your choice.
            * randomNormal - The move targets an adjacent foe at random.
            * scripted - The move targets the foe that damaged the user.
            * self - The move affects the user of the move.
        :rtype: str
        """
        return self.entry["target"]

    # 返回由移动引发的地形
    @property
    def terrain(self) -> Optional[Field]:
        """
        :return: Terrain started by the move.
        :rtype: Optional[Field]
        """
        # 获取移动引发的地形,如果存在则转换成 Field 对象
        terrain = self.entry.get("terrain", None)
        if terrain is not None:
            terrain = Field.from_showdown_message(terrain)
        return terrain

    # 返回移动是否解冻目标
    @property
    def thaws_target(self) -> bool:
        """
        :return: Whether the move thaws its target.
        :rtype: bool
        """
        return self.entry.get("thawsTarget", False)

    @property
    def type(self) -> PokemonType:
        """
        :return: Move type.
        :rtype: PokemonType
        """
        # 返回移动的类型,通过从名称获取 PokemonType 对象
        return PokemonType.from_name(self.entry["type"])

    @property
    def use_target_offensive(self) -> bool:
        """
        :return: Whether the move uses the target's offensive statistics.
        :rtype: bool
        """
        # 返回移动是否使用目标的进攻统计信息
        return self.entry.get("overrideOffensivePokemon", False) == "target"

    @property
    def volatile_status(self) -> Optional[str]:
        """
        :return: Volatile status inflicted by the move.
        :rtype: str | None
        """
        # 返回移动造成的短暂状态
        return self.entry.get("volatileStatus", None)

    @property
    def weather(self) -> Optional[Weather]:
        """
        :return: Weather started by the move.
        :rtype: Optional[Weather]
        """
        # 返回由移动引起的天气
        if "weather" in self.entry:
            return Weather[self.entry["weather"].upper()]
        return None

    @property
    def z_move_boost(self) -> Optional[Dict[str, int]]:
        """
        :return: Boosts associated with the z-move version of this move.
        :rtype: Dict[str, int]
        """
        # 返回与 z-move 版本的移动相关联的增益
        if "zMove" in self.entry and "boost" in self.entry["zMove"]:
            return self.entry["zMove"]["boost"]
        return None

    @property
    def z_move_effect(self) -> Optional[str]:
        """
        :return: Effects associated with the z-move version of this move.
        :rtype: str | None
        """
        # 返回与 z-move 版本的移动相关联的效果
        if "zMove" in self.entry and "effect" in self.entry["zMove"]:
            return self.entry["zMove"]["effect"]
        return None

    @property
    # 返回该招式的 Z 招式版本的基础威力
    def z_move_power(self) -> int:
        """
        :return: Base power of the z-move version of this move.
        :rtype: int
        """
        # 如果该招式有 Z 招式属性,并且包含基础威力信息,则返回基础威力
        if "zMove" in self.entry and "basePower" in self.entry["zMove"]:
            return self.entry["zMove"]["basePower"]
        # 如果该招式为状态招式,则基础威力为 0
        elif self.category == MoveCategory.STATUS:
            return 0
        # 获取基础威力值
        base_power = self.base_power
        # 如果招式命中次数不是 (1, 1),则基础威力乘以 3
        if self.n_hit != (1, 1):
            base_power *= 3
        # 根据基础威力值返回对应的 Z 招式威力值
        elif base_power <= 55:
            return 100
        elif base_power <= 65:
            return 120
        elif base_power <= 75:
            return 140
        elif base_power <= 85:
            return 160
        elif base_power <= 95:
            return 175
        elif base_power <= 100:
            return 180
        elif base_power <= 110:
            return 185
        elif base_power <= 125:
            return 190
        elif base_power <= 130:
            return 195
        # 基础威力大于 130,则返回 200
        return 200
class EmptyMove(Move):
    # 定义一个空招式类,继承自Move类
    def __init__(self, move_id: str):
        # 初始化方法,接受一个招式ID参数
        self._id = move_id
        self._is_empty: bool = True
        # 设置_is_empty属性为True

    def __getattribute__(self, name: str):
        # 重写__getattribute__方法
        try:
            return super(Move, self).__getattribute__(name)
        except (AttributeError, TypeError, ValueError):
            return 0
        # 尝试获取属性,如果出现异常则返回0

    def __deepcopy__(self, memodict: Optional[Dict[int, Any]] = {}):
        # 重写__deepcopy__方法,实现深拷贝
        return EmptyMove(copy.deepcopy(self._id, memodict))
        # 返回一个深拷贝的EmptyMove对象


class DynamaxMove(Move):
    # 定义一个DynamaxMove类,继承自Move类
    BOOSTS_MAP = {
        # 定义BOOSTS_MAP字典,存储属性提升信息
        PokemonType.BUG: {"spa": -1},
        PokemonType.DARK: {"spd": -1},
        PokemonType.DRAGON: {"atk": -1},
        PokemonType.GHOST: {"def": -1},
        PokemonType.NORMAL: {"spe": -1},
    }
    SELF_BOOSTS_MAP = {
        # 定义SELF_BOOSTS_MAP字典,存储自身属性提升信息
        PokemonType.FIGHTING: {"atk":  1},
        PokemonType.FLYING: {"spe":  1},
        PokemonType.GROUND: {"spd":  1},
        PokemonType.POISON: {"spa":  1},
        PokemonType.STEEL: {"def":  1},
    }
    TERRAIN_MAP = {
        # 定义TERRAIN_MAP字典,存储地形信息
        PokemonType.ELECTRIC: Field.ELECTRIC_TERRAIN,
        PokemonType.FAIRY: Field.MISTY_TERRAIN,
        PokemonType.GRASS: Field.GRASSY_TERRAIN,
        PokemonType.PSYCHIC: Field.PSYCHIC_TERRAIN,
    }
    WEATHER_MAP = {
        # 定义WEATHER_MAP字典,存储天气信息
        PokemonType.FIRE: Weather.SUNNYDAY,
        PokemonType.ICE: Weather.HAIL,
        PokemonType.ROCK: Weather.SANDSTORM,
        PokemonType.WATER: Weather.RAINDANCE,
    }

    def __init__(self, parent: Move):
        # 初始化方法,接受一个Move对象作为参数
        self._parent: Move = parent

    def __getattr__(self, name: str):
        # 重写__getattr__方法
        if name[:2] == "__":
            raise AttributeError(name)
        return getattr(self._parent, name)
        # 获取父类Move对象的属性

    @property
    def accuracy(self):
        # 定义accuracy属性
        return 1
        # 返回值为1

    @property
    # 定义一个属性
    # 返回技能的基础威力,如果技能类型不是状态技能
    def base_power(self) -> int:
        # 如果技能类型不是状态技能,则获取父类的基础威力
        base_power = self._parent.base_power
        # 如果技能属性是毒或格斗
        if self.type in {PokemonType.POISON, PokemonType.FIGHTING}:
            # 根据基础威力的不同范围返回不同的威力值
            if base_power < 40:
                return 70
            if base_power < 50:
                return 75
            if base_power < 60:
                return 80
            if base_power < 70:
                return 85
            if base_power < 100:
                return 90
            if base_power < 140:
                return 95
            return 100
        else:
            # 根据基础威力的不同范围返回不同的威力值
            if base_power < 40:
                return 90
            if base_power < 50:
                return 100
            if base_power < 60:
                return 110
            if base_power < 70:
                return 120
            if base_power < 100:
                return 130
            if base_power < 140:
                return 140
            return 150
        # 如果技能类型是状态技能,则返回0
        return 0

    # 返回技能的增益效果,如果技能类型不是状态技能
    @property
    def boosts(self) -> Optional[Dict[str, int]]:
        if self.category != MoveCategory.STATUS:
            return self.BOOSTS_MAP.get(self.type, None)
        return None

    # 返回技能是否能破坏保护
    @property
    def breaks_protect(self):
        return False

    # 返回技能的暴击率
    @property
    def crit_ratio(self):
        return 0

    # 返回技能造成的伤害值
    @property
    def damage(self):
        return 0

    # 返回技能的防御类别
    @property
    def defensive_category(self):
        return self.category

    # 返回技能预期的命中次数
    @property
    def expected_hits(self):
        return 1

    # 返回是否强制交换精灵
    @property
    def force_switch(self):
        return False

    # 返回技能的治疗效果
    @property
    def heal(self):
        return 0

    # 返回技能是否是保护技能的反击
    @property
    def is_protect_counter(self):
        return self.category == MoveCategory.STATUS

    # 返回技能是否是保护技能
    @property
    def is_protect_move(self):
        return self.category == MoveCategory.STATUS

    # 返回技能的命中次数范围
    @property
    def n_hit(self):
        return 1, 1
    # 返回技能的优先级,这里默认为0
    def priority(self):
        return 0

    # 返回技能的反冲值,这里默认为0
    @property
    def recoil(self):
        return 0

    # 返回技能对自身的增益效果,如果技能不是状态技能,则返回对应类型的增益效果字典,否则返回None
    @property
    def self_boost(self) -> Optional[Dict[str, int]]:
        if self.category != MoveCategory.STATUS:
            return self.SELF_BOOSTS_MAP.get(self.type, None)
        return None

    # 返回技能的状态效果,这里默认为None
    @property
    def status(self):
        return None

    # 返回技能对场地的影响,如果技能不是状态技能,则返回对应类型的场地效果,否则返回None
    @property
    def terrain(self) -> Optional[Field]:
        if self.category != MoveCategory.STATUS:
            return self.TERRAIN_MAP.get(self.type, None)
        return None

    # 返回技能对天气的影响,如果技能不是状态技能,则返回对应类型的天气效果,否则返回None
    @property
    def weather(self) -> Optional[Weather]:
        if self.category != MoveCategory.STATUS:
            return self.WEATHER_MAP.get(self.type, None)
        return None

0 人点赞