Files
cultivation-world-simulator/src/classes/action/hunt.py
2025-11-19 01:23:55 +08:00

72 lines
2.4 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
class Hunt(TimedAction):
"""
狩猎动作在有动物的区域进行狩猎持续6个月
可以获得动物对应的物品
"""
COMMENT = "在当前区域狩猎动物,获取动物材料"
DOABLES_REQUIREMENTS = "在有动物的普通区域且avatar的境界必须大于等于动物的境界"
PARAMS = {}
duration_months = 6
def _execute(self) -> None:
"""
执行狩猎动作
"""
region = self.avatar.tile.region
animals = getattr(region, "animals", [])
if len(animals) == 0:
return
available_animals = [
animal for animal in animals
if self.avatar.cultivation_progress.realm >= animal.realm
]
if len(available_animals) == 0:
return
# 目前固定100%成功率
if random.random() < 1.0:
target_animal = random.choice(available_animals)
# 随机选择该动物的一种物品
item = random.choice(target_animal.items)
# 基础获得1个额外物品来自effects
base_quantity = 1
extra_items = int(self.avatar.effects.get("extra_hunt_items", 0) or 0)
total_quantity = base_quantity + extra_items
self.avatar.add_item(item, total_quantity)
def can_start(self) -> tuple[bool, str]:
region = self.avatar.tile.region
if not isinstance(region, NormalRegion):
return False, "当前不在普通区域"
animals = getattr(region, "animals", [])
if len(animals) == 0:
return False, f"当前区域{region.name}没有动物"
available_animals = [
animal for animal in animals
if self.avatar.cultivation_progress.realm >= animal.realm
]
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} 开始狩猎", related_avatars=[self.avatar.id])
# TimedAction 已统一 step 逻辑
async def finish(self) -> list[Event]:
return []