Files
cultivation-world-simulator/src/classes/action/hunt.py
2025-10-04 16:29:29 +08:00

74 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
import random
from src.classes.action import TimedAction
from src.classes.event import Event
from src.classes.region import NormalRegion
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from src.classes.animal import Animal
class Hunt(TimedAction):
"""
狩猎动作在有动物的区域进行狩猎持续6个月
可以获得动物对应的物品
"""
COMMENT = "在当前区域狩猎动物,获取动物材料"
DOABLES_REQUIREMENTS = "在有动物的普通区域且avatar的境界必须大于等于动物的境界"
PARAMS = {}
def get_available_animals(self) -> list[Animal]:
"""
获取avatar境界足够的动物
"""
region = self.avatar.tile.region
avatar_realm = self.avatar.cultivation_progress.realm
return [animal for animal in region.animals if avatar_realm >= animal.realm]
duration_months = 6
def _execute(self) -> None:
"""
执行狩猎动作
"""
success_rate = self.get_success_rate()
available_animals = self.get_available_animals()
if len(available_animals) == 0:
return
if random.random() < success_rate:
# 成功狩猎从avatar境界足够的动物中随机选择一种
target_animal = random.choice(available_animals)
# 随机选择该动物的一种物品
item = random.choice(target_animal.items)
self.avatar.add_item(item, 1)
def get_success_rate(self) -> float:
"""
获取狩猎成功率预留接口目前固定为100%
"""
return 1.0 # 100%成功率
def can_start(self) -> bool:
region = self.avatar.tile.region
if not isinstance(region, NormalRegion):
return False
available_animals = self.get_available_animals()
if len(available_animals) == 0:
return False
return True
def start(self) -> Event:
region = self.avatar.tile.region
return Event(self.world.month_stamp, f"{self.avatar.name}{region.name} 开始狩猎")
# TimedAction 已统一 step 逻辑
def finish(self) -> list[Event]:
return []