diff --git a/src/classes/action/__init__.py b/src/classes/action/__init__.py index aabfcdf..63e6c0e 100644 --- a/src/classes/action/__init__.py +++ b/src/classes/action/__init__.py @@ -33,6 +33,7 @@ from .devour_mortals import DevourMortals from .self_heal import SelfHeal from .catch import Catch from .nurture_weapon import NurtureWeapon +from .switch_weapon import SwitchWeapon # 注册到 ActionRegistry(标注是否为实际可执行动作) register_action(actual=False)(Action) @@ -62,6 +63,7 @@ register_action(actual=True)(DevourMortals) register_action(actual=True)(SelfHeal) register_action(actual=True)(Catch) register_action(actual=True)(NurtureWeapon) +register_action(actual=True)(SwitchWeapon) # Talk 已移动到 mutual_action 模块,在那里注册 __all__ = [ @@ -93,6 +95,8 @@ __all__ = [ "DevourMortals", "SelfHeal", "Catch", + "NurtureWeapon", + "SwitchWeapon", # Talk 已移动到 mutual_action 模块 ] diff --git a/src/classes/action/switch_weapon.py b/src/classes/action/switch_weapon.py new file mode 100644 index 0000000..6472a8e --- /dev/null +++ b/src/classes/action/switch_weapon.py @@ -0,0 +1,80 @@ +from __future__ import annotations + +from src.classes.action import InstantAction +from src.classes.event import Event +from src.classes.weapon import get_common_weapon +from src.classes.weapon_type import WeaponType +from src.classes.normalize import normalize_weapon_type + + +class SwitchWeapon(InstantAction): + """ + 切换兵器:将当前兵器切换为指定类型的凡品兵器。 + 熟练度重置为0。 + """ + + COMMENT = "切换到指定类型的凡品兵器。当前兵器会丧失,熟练度会重置为0。适用于想要更换兵器类型或从头修炼新兵器的情况。" + DOABLES_REQUIREMENTS = "无前置条件" + PARAMS = {"weapon_type_name": "str"} + + def _execute(self, weapon_type_name: str) -> None: + # 规范化兵器类型名称 + normalized_type = normalize_weapon_type(weapon_type_name) + + # 匹配 WeaponType 枚举 + target_weapon_type = None + for wt in WeaponType: + if wt.value == normalized_type: + target_weapon_type = wt + break + + if target_weapon_type is None: + return + + # 获取凡品兵器 + common_weapon = get_common_weapon(target_weapon_type) + if common_weapon is None: + return + + # 切换兵器(使用 Avatar 的 change_weapon 方法) + self.avatar.change_weapon(common_weapon) + + def can_start(self, weapon_type_name: str | None = None) -> tuple[bool, str]: + if weapon_type_name is None: + # AI调用:总是可以切换兵器 + return True, "" + + # 规范化并验证兵器类型 + normalized_type = normalize_weapon_type(weapon_type_name) + target_weapon_type = None + for wt in WeaponType: + if wt.value == normalized_type: + target_weapon_type = wt + break + + if target_weapon_type is None: + return False, f"未知兵器类型: {weapon_type_name}(支持的类型:剑/刀/枪/棍/扇/鞭/琴/笛/暗器)" + + # 检查是否已经是该类型的凡品兵器 + if self.avatar.weapon.weapon_type == target_weapon_type and \ + self.avatar.weapon.name == f"凡品{target_weapon_type.value}": + return False, f"已经装备了凡品{target_weapon_type.value}" + + # 检查凡品兵器是否存在 + common_weapon = get_common_weapon(target_weapon_type) + if common_weapon is None: + return False, f"系统中不存在凡品{target_weapon_type.value}" + + return True, "" + + def start(self, weapon_type_name: str) -> Event: + normalized_type = normalize_weapon_type(weapon_type_name) + return Event( + self.world.month_stamp, + f"{self.avatar.name} 切换兵器为凡品{normalized_type}", + related_avatars=[self.avatar.id] + ) + + def finish(self, weapon_type_name: str) -> list[Event]: + return [] + diff --git a/src/classes/normalize.py b/src/classes/normalize.py index b77a0aa..84d7639 100644 --- a/src/classes/normalize.py +++ b/src/classes/normalize.py @@ -130,3 +130,30 @@ def normalize_item_name(name: str) -> str: s = s.rstrip(" -").strip() return s + +def normalize_weapon_type(name: str) -> str: + """ + 规范化兵器类型名称:映射到标准的WeaponType枚举值。 + + 处理格式: + - 去除空格和多余符号 + - "剑"/"剑类"/"剑兵器" -> "剑" + + Args: + name: 兵器类型名称 + + Returns: + 规范化后的兵器类型名称(WeaponType.value) + + Examples: + >>> normalize_weapon_type("剑 ") + '剑' + >>> normalize_weapon_type("刀类") + '刀' + """ + s = str(name).strip() + # 移除常见后缀 + for suffix in ["类", "兵器", "武器"]: + if s.endswith(suffix): + s = s[:-len(suffix)].strip() + return s diff --git a/src/classes/weapon.py b/src/classes/weapon.py index 99b983c..3cf0431 100644 --- a/src/classes/weapon.py +++ b/src/classes/weapon.py @@ -113,8 +113,8 @@ weapons_by_id, weapons_by_name, weapons_by_sect_id = _load_weapons() def get_common_weapon(weapon_type: WeaponType) -> Optional[Weapon]: - """获取指定类型的普通兵器(用于兜底)""" - weapon_name = f"普通{weapon_type.value}" + """获取指定类型的凡品兵器(用于兜底)""" + weapon_name = f"凡品{weapon_type.value}" return weapons_by_name.get(weapon_name) diff --git a/static/game_configs/weapon.csv b/static/game_configs/weapon.csv index c684010..8d2a133 100644 --- a/static/game_configs/weapon.csv +++ b/static/game_configs/weapon.csv @@ -3,15 +3,15 @@ id,name,weapon_type,grade,sect_id,desc,effects 1,本命剑匣,剑,法宝,1,以心御剑,匣启如霆,剑意随心破万法.,{extra_battle_strength_points: 3} 4,镇魂钟,棍,法宝,7,钟鸣摄魄,定魂镇邪,护心安魂.,"{extra_battle_strength_points: 2, extra_observation_radius: 1}" 6,万魂幡,扇,法宝,4,幡起万魂啾啾,阴风过境.,"{legal_actions: ['DevourMortals'], extra_battle_strength_points: 'avatar.weapon.special_data.get(""devoured_souls"", 0) // 100 * 0.1'}" -1001,普通剑,剑,普通,,平凡无奇的剑。,{extra_battle_strength_points: 1} -1002,普通刀,刀,普通,,平凡无奇的刀。,{extra_battle_strength_points: 1} -1003,普通枪,枪,普通,,平凡无奇的枪。,{extra_battle_strength_points: 1} -1004,普通棍,棍,普通,,平凡无奇的棍。,{extra_battle_strength_points: 1} -1005,普通扇,扇,普通,,平凡无奇的扇。,{extra_battle_strength_points: 1} -1006,普通鞭,鞭,普通,,平凡无奇的鞭。,{extra_battle_strength_points: 1} -1007,普通琴,琴,普通,,平凡无奇的琴。,{extra_battle_strength_points: 1} -1008,普通笛,笛,普通,,平凡无奇的笛。,{extra_battle_strength_points: 1} -1009,普通暗器,暗器,普通,,平凡无奇的暗器。,{extra_battle_strength_points: 1} +1001,凡品剑,剑,普通,,平凡无奇的剑。,{extra_battle_strength_points: 1} +1002,凡品刀,刀,普通,,平凡无奇的刀。,{extra_battle_strength_points: 1} +1003,凡品枪,枪,普通,,平凡无奇的枪。,{extra_battle_strength_points: 1} +1004,凡品棍,棍,普通,,平凡无奇的棍。,{extra_battle_strength_points: 1} +1005,凡品扇,扇,普通,,平凡无奇的扇。,{extra_battle_strength_points: 1} +1006,凡品鞭,鞭,普通,,平凡无奇的鞭。,{extra_battle_strength_points: 1} +1007,凡品琴,琴,普通,,平凡无奇的琴。,{extra_battle_strength_points: 1} +1008,凡品笛,笛,普通,,平凡无奇的笛。,{extra_battle_strength_points: 1} +1009,凡品暗器,暗器,普通,,平凡无奇的暗器。,{extra_battle_strength_points: 1} 2001,青霜剑,剑,宝物,,剑身寒气逼人,剑锋如霜。,{extra_battle_strength_points: 2} 2002,破军刀,刀,宝物,,刀势霸道凌厉,破军杀阵。,{extra_battle_strength_points: 2} 2003,龙吟枪,枪,宝物,,枪出如龙,势如破竹。,"{extra_battle_strength_points: 2, extra_max_hp: 50}"