src / companion.py
# -*- coding: utf-8 -*-
"""Companion - 队友系统
核心设计:
- Companion类:单个队友的属性、战斗、AP、浪漫值
- CompanionManager:管理所有潜在队友,招募/解散/AI决策/凌暮雪情绪状态
"""
import random
from typing import Optional
class Companion:
"""单个队友。
Attributes:
name: 队友名称
gender: 性别 'male'/'female'
skills: 技能字典 {'mechanical': 2, 'medical': 0, ...}
personality: 性格标签
weapon: 武器名称(None表示无武器)
ammo: 弹药数量
hp: 当前生命值
max_hp: 最大生命值
loyalty: 忠诚度 0-100
morale: 精神 0-100
ap: 每回合行动点数(默认3)
alive: 是否存活
in_party: 是否在队伍中
romance_level: 浪漫值(自然增长,无上限)
"""
def __init__(self, name: str, gender: str, skills: dict, personality: str,
weapon: Optional[str] = None):
self.name = name
self.gender = gender
self.skills = skills or {}
self.personality = personality
self.weapon = weapon
self.ammo = 0
self.hp = 100
self.max_hp = 100
self.loyalty = 50
self.morale = 50
self.ap = 3
self.alive = True
self.in_party = False
self.romance_level = 0
def reset_ap(self):
"""重置行动点数。"""
self.ap = 3
def take_damage(self, damage: float) -> bool:
"""受到伤害。
Returns:
True if died, False otherwise.
"""
self.hp -= damage
if self.hp <= 0:
self.hp = 0
self.alive = False
return True
return False
def heal(self, amount: float):
"""治疗。"""
self.hp = min(self.max_hp, self.hp + amount)
def use_ap(self, cost: int = 1) -> bool:
"""消耗行动点数。
Returns:
True if AP was sufficient, False otherwise.
"""
if self.ap >= cost:
self.ap -= cost
return True
return False
def add_ammo(self, amount: int):
"""添加弹药。"""
self.ammo += amount
def shoot(self) -> bool:
"""射击(消耗1弹药和1 AP)。
Returns:
True if shot was successful, False otherwise.
"""
if self.weapon and self.ammo > 0 and self.use_ap(1):
self.ammo -= 1
return True
return False
def get_skill_level(self, skill_name: str) -> int:
"""获取某技能等级。"""
return self.skills.get(skill_name, 0)
def status_line(self) -> str:
"""状态行。"""
status = f"{self.name} ({'女' if self.gender == 'female' else '男'})"
if not self.alive:
return f"{status} [已死亡]"
if not self.in_party:
return f"{status} [不在队伍中]"
hp_bar = "█" * int(self.hp / self.max_hp * 8) + "░" * (8 - int(self.hp / self.max_hp * 8))
skills_str = ', '.join(f"{k}:{v}" for k, v in self.skills.items() if v > 0)
lines = [
f"{status}",
f" HP [{hp_bar}] {self.hp:.0f}/{self.max_hp}",
f" 忠诚 {self.loyalty:.0f} 精神 {self.morale:.0f} 浪漫 {self.romance_level}",
f" 技能: {skills_str if skills_str else '无'}",
f" 武器: {self.weapon or '无'} (弹药: {self.ammo})",
f" AP: {self.ap}/3",
]
return '\n'.join(lines)
def __repr__(self) -> str:
return f"<Companion name={self.name} hp={self.hp} in_party={self.in_party}>"
class CompanionManager:
"""队友管理器。
管理所有潜在队友的招募、解散、战斗AI、凌暮雪情绪状态。
"""
def __init__(self):
self.companions: dict[str, Companion] = {}
self.party: list[Companion] = [] # 队伍中的队友(最多4人)
self.lingmuxue_mood: str = 'secure' # 安心/察觉/不安/疏远/受伤/心冷/离开
self._lingmuxue_interactions: list = [] # 记录玩家与女性队友的互动
def init_companions(self):
"""初始化所有潜在队友。"""
self.companions['老周'] = Companion(
'老周', 'male',
{'mechanical': 1, 'medical': 0, 'military': 3, 'survival': 2, 'russian': 0},
'cautious', '56冲'
)
self.companions['塔季扬娜'] = Companion(
'塔季扬娜', 'female',
{'mechanical': 0, 'medical': 3, 'military': 1, 'survival': 1, 'russian': 3},
'calm', '马卡洛夫'
)
self.companions['李雪梅'] = Companion(
'李雪梅', 'female',
{'mechanical': 0, 'medical': 2, 'military': 1, 'survival': 2, 'russian': 0},
'principled', '54手枪'
)
self.companions['张小雨'] = Companion(
'张小雨', 'female',
{'mechanical': 0, 'medical': 0, 'military': 0, 'survival': 1, 'russian': 0, 'radio': 1},
'dependent', None
)
self.companions['阿依努尔'] = Companion(
'阿依努尔', 'female',
{'mechanical': 0, 'medical': 1, 'military': 1, 'survival': 3, 'russian': 0, 'hunting': 2},
'straightforward', '猎枪'
)
def recruit(self, name: str) -> bool:
"""招募队友。
Returns:
True if recruited successfully, False otherwise.
"""
if name not in self.companions:
return False
if len(self.party) >= 4:
return False
companion = self.companions[name]
if companion.in_party:
return False
if not companion.alive:
return False
companion.in_party = True
self.party.append(companion)
return True
def dismiss(self, name: str) -> bool:
"""解散队友。
Returns:
True if dismissed successfully, False otherwise.
"""
companion = self.companions.get(name)
if not companion:
return False
if not companion.in_party:
return False
companion.in_party = False
if companion in self.party:
self.party.remove(companion)
return True
def get_companion(self, name: str) -> Optional[Companion]:
"""获取队友。"""
return self.companions.get(name)
def get_party_members(self) -> list[Companion]:
"""获取队伍中的所有队友。"""
return [c for c in self.party if c.alive and c.in_party]
def companion_combat_action(self, companion: Companion, combat_state: dict) -> tuple:
"""队友AI战斗决策。
Args:
companion: 队友实例。
combat_state: 战斗状态字典,包含:
- player_hp: 玩家HP
- nearest_enemy: 最近敌人
- allies: 队友列表
Returns:
(action_type, target) 元组。
action_type: 'heal'/'shoot'/'cover'/'move'
target: 目标名称或None
"""
if not companion.alive or not companion.in_party:
return ('move', None)
# 优先级1:治疗受伤的玩家
player_hp = combat_state.get('player_hp', 100)
if player_hp < 50 and companion.get_skill_level('medical') >= 2:
return ('heal', 'player')
# 优先级2:治疗受伤队友
allies = combat_state.get('allies', [])
for ally in allies:
if isinstance(ally, Companion) and ally.hp < 50 and ally != companion:
if companion.get_skill_level('medical') >= 2:
return ('heal', ally.name)
# 优先级3:射击最近敌人
nearest_enemy = combat_state.get('nearest_enemy')
if nearest_enemy and companion.weapon and companion.ammo > 0:
if companion.shoot():
return ('shoot', nearest_enemy)
# 优先级4:掩护玩家
if companion.get_skill_level('military') >= 2:
return ('cover', 'player')
# 默认:射击
return ('shoot', nearest_enemy)
def update_lingmuxue_mood(self, player_actions: list):
"""更新凌暮雪情绪状态(自然后果,无数值显示)。
Args:
player_actions: 玩家动作列表,每个动作是 (action_type, target_name) 元组。
action_type: 'romance'/'talk'/'gift'/'ignore'
"""
# 记录互动
self._lingmuxue_interactions.extend(player_actions)
# 计算与女性队友的浪漫互动次数
romance_count = sum(
1 for action, target in self._lingmuxue_interactions
if action == 'romance' and target in self.companions
and self.companions[target].gender == 'female'
)
# 根据浪漫互动次数调整情绪
if romance_count == 0:
self.lingmuxue_mood = 'secure'
elif romance_count == 1:
self.lingmuxue_mood = 'aware'
elif romance_count == 2:
self.lingmuxue_mood = 'unsettled'
elif romance_count == 3:
self.lingmuxue_mood = 'withdrawn'
elif romance_count == 4:
self.lingmuxue_mood = 'hurt'
elif romance_count == 5:
self.lingmuxue_mood = 'distant'
else:
self.lingmuxue_mood = 'leaving'
def get_lingmuxue_dialogue(self) -> str:
"""获取凌暮雪基于当前情绪的台词。"""
dialogues = LINGMUXUE_MOOD_DIALOGUES.get(self.lingmuxue_mood, LINGMUXUE_MOOD_DIALOGUES['secure'])
return random.choice(dialogues)
def romance_interaction(self, player_input: str, companion: Companion) -> str:
"""浪漫互动(无硬性限制)。
Args:
player_input: 玩家输入。
companion: 队友实例。
Returns:
队友的反应文本。
"""
if not companion.alive or not companion.in_party:
return "……她已经不在了。"
companion.romance_level += 10
# 凌暮雪会察觉(情绪变化,不是数值惩罚)
if companion.gender == 'female' and companion.name != '凌暮雪':
self.update_lingmuxue_mood([('romance', companion.name)])
# 不同女性反应不同
reactions = {
'塔季扬娜': '她冷静地看着你,但眼神里有一丝心动。',
'李雪梅': '她拒绝了,但你看到她转过头时脸上的难过。',
'张小雨': '她脸红了,低着头不说话。',
'阿依努尔': '她直率地笑了:"你这人倒是直接。"',
}
return reactions.get(companion.name, '她愣了一下,没有说话。')
def get_party_status(self) -> str:
"""获取队伍状态字符串。"""
if not self.party:
return "队伍中没有队友。"
lines = ["【队友状态】"]
for companion in self.party:
if companion.alive:
lines.append(companion.status_line())
return '\n\n'.join(lines)
def reset_turn(self):
"""重置回合(恢复所有队友的AP)。"""
for companion in self.party:
if companion.alive:
companion.reset_ap()
# 凌暮雪情绪状态常量
LINGMUXUE_MOODS = {
'secure': '安心',
'aware': '察觉',
'unsettled': '不安',
'withdrawn': '疏远',
'hurt': '受伤',
'distant': '心冷',
'leaving': '离开',
}
# 凌暮雪的台词反映情绪(玩家自己感受,不显示数值)
LINGMUXUE_MOOD_DIALOGUES = {
'secure': ['……你今天累不累?靠过来。', '……我想跟你说说话。'],
'aware': ['……你跟她聊了很久。', '……嗯。'],
'unsettled': ['……没什么。', '……你别管我。'],
'withdrawn': ['……哦。', '……随便。'],
'hurt': ['……你是不是,觉得她比我好?', '……我哪里不好?'],
'distant': ['……随便你。', '……我不想说话。'],
'leaving': ['……曾经坐在我身旁。', '……我走了你就不用愁了。'],
}