1979/src/player.py

src / player.py

src/player.py · 8.0 KB

# -*- coding: utf-8 -*-
"""Player - 沈阳军区通讯团学员兵,党员,无线电技术

设定:沈阳军区通讯团学员兵,党员,干净成分。
核战失散不是逃兵。负责驾驶/56半射击/车辆战术指挥/无线电通讯。
"""

import random


class Player:
    """玩家状态封装(七维生存系统)。

    七维状态:
        hp: 生命值 0-100
        temperature: 体温 °C(36.5正常,<25死亡)
        hunger: 饥饿 0-100(越高越饿)
        thirst: 口渴 0-100(越高越渴)
        radiation: 辐射 mSv 0-1000(累积不可逆)
        fatigue: 疲劳 0-100
        morale: 精神 0-100(低则幻觉/队友离开)

    体温状态机:
        36.5-37.5: 舒适
        35-36: 寒冷
        32-35: 冰冻(HP-0.5/h,疲劳+40%,精神-1/h)
        28-32: 冻伤(HP-2/h,永久失去HP上限可能)
        25-28: 失温(HP-5/h,幻觉40%)
        <25: 死亡
    """

    MAX_HP = 100.0
    NORMAL_TEMP = 36.5

    def __init__(self, name: str = "无名") -> None:
        self.name = name
        # 核心生命
        self.hp = 78.0
        self._max_hp_cap = 100.0  # HP上限(冻伤可永久降低)

        # 七维状态
        self.temperature = 36.5  # 体温°C
        self.hunger = 30.0
        self.thirst = 20.0
        self.radiation = 12.0
        self.fatigue = 25.0
        self.morale = 55.0

        # 幻觉系统
        self._hallucination_active = False
        self._hallucination_timer = 0

        # 战斗系统字段(Combat 类访问)
        self.id = "player"
        self.ap = 3  # 行动点(每回合重置)
        self.state = "normal"  # normal/injured/fatigue/hallucination

        # 技能系统(哈尔滨段营救需要)
        self.radio_skill = 70      # 通讯兵本职
        self.military_skill = 55   # 学员兵中等
        self.speech_skill = 35     # 一般
        self.russian_skill = 25    # 边境驻军学过基础
        self.politics_skill = 45   # 党员但年轻

        # 专业等级(出国线结局判定)1-5
        self.professional_level = 1

    @property
    def max_hp(self) -> float:
        return self._max_hp_cap

    @max_hp.setter
    def max_hp(self, value: float) -> None:
        self._max_hp_cap = value

    @property
    def alive(self) -> bool:
        return self.hp > 0 and self.temperature >= 25.0

    @property
    def hp_ratio(self) -> float:
        return max(0.0, min(1.0, self.hp / self.MAX_HP))

    @property
    def temperature_state(self) -> str:
        """体温状态机返回字符串描述。"""
        t = self.temperature
        if t >= 36.5 and t <= 37.5:
            return "舒适"
        elif t >= 35.0 and t < 36.5:
            return "寒冷"
        elif t >= 32.0 and t < 35.0:
            return "冰冻"
        elif t >= 28.0 and t < 32.0:
            return "冻伤"
        elif t >= 25.0 and t < 28.0:
            return "失温"
        else:
            return "死亡"

    def take_damage(self, amount: float, kind: str = "外伤") -> None:
        self.hp = max(0.0, self.hp - amount)
        self._last_damage = (kind, amount)

    def heal(self, amount: float) -> float:
        healed = min(amount, self._max_hp_cap - self.hp)
        self.hp += healed
        return healed

    def consume_food(self, amount: float) -> None:
        """消耗食物,减少饥饿。"""
        self.hunger = max(0.0, self.hunger - amount)

    def consume_water(self, amount: float) -> None:
        """消耗水,减少口渴。"""
        self.thirst = max(0.0, self.thirst - amount)

    def consume_fuel_liters(self, amount: float) -> None:
        """消耗燃料(占位,实际由vehicle管理)。"""
        pass

    def is_hallucinating(self) -> bool:
        """检查是否处于幻觉状态。"""
        return self._hallucination_active

    def tick_effects(self, world=None, vehicle=None, lingmuxue=None) -> list:
        """每小时tick的效果结算。返回事件列表。"""
        events = []

        # 1. 体温连锁
        if self.temperature < 35.0:
            # 冰冻/冻伤/失温:疲劳加速
            self.fatigue = min(100.0, self.fatigue + 2.0)
            events.append("体温过低,疲劳加速积累")

        if self.temperature < 32.0 and self.temperature >= 28.0:
            # 冻伤:HP-2/h
            self.hp = max(0.0, self.hp - 2.0)
            events.append("冻伤:HP-2/h")
            # 永久失去HP上限5点可能(30%概率)
            if random.random() < 0.3:
                old_cap = self._max_hp_cap
                self._max_hp_cap = max(50.0, self._max_hp_cap - 5.0)
                events.append(f"冻伤导致永久失去HP上限:{old_cap:.0f}→{self._max_hp_cap:.0f}")

        if self.temperature < 28.0 and self.temperature >= 25.0:
            # 失温:HP-5/h,幻觉40%
            self.hp = max(0.0, self.hp - 5.0)
            events.append("失温:HP-5/h")
            if random.random() < 0.4:
                self._hallucination_active = True
                self._hallucination_timer = 3  # 持续3小时
                events.append("失温引发幻觉")

        if self.temperature < 35.0 and self.temperature >= 32.0:
            # 冰冻:HP-0.5/h,疲劳+40%,精神-1/h
            self.hp = max(0.0, self.hp - 0.5)
            self.fatigue = min(100.0, self.fatigue * 1.4)
            self.morale = max(0.0, self.morale - 1.0)
            events.append("冰冻:HP-0.5/h,疲劳+40%,精神-1/h")

        # 2. 饥饿连锁
        if self.hunger > 80:
            self.morale = max(0.0, self.morale - 3.0)
            events.append("极度饥饿:精神-3/h,效率-50%")

        # 3. 口渴连锁
        if self.thirst > 80:
            self.hp = max(0.0, self.hp - 2.0)
            events.append("极度口渴:HP-2/h")

        # 4. 辐射连锁
        if self.radiation > 600:
            self.hp = max(0.0, self.hp - 3.0)
            self.morale = max(0.0, self.morale - 3.0)
            events.append("急性辐射病:HP-3/h,精神-3/h")
        elif self.radiation > 300:
            self.hp = max(0.0, self.hp - 1.0)
            self.morale = max(0.0, self.morale - 3.0)
            events.append("辐射中毒:HP-1/h,精神-3/h")

        # 5. 疲劳连锁
        if self.fatigue > 80:
            self.morale = max(0.0, self.morale - 5.0)
            events.append("极度疲劳:精神-5/h")

        # 6. 精神低落幻觉
        if self.morale < 20 and not self._hallucination_active:
            if random.random() < 0.1:
                self._hallucination_active = True
                self._hallucination_timer = 2
                events.append("精神崩溃引发幻觉")

        # 7. 幻觉计时器递减
        if self._hallucination_active:
            self._hallucination_timer -= 1
            if self._hallucination_timer <= 0:
                self._hallucination_active = False
                events.append("幻觉消退")

        # 8. 体温自然恢复(如果有衣物/庇护所,这里简化)
        # 默认体温向36.5回归的速度很慢
        if self.temperature < 36.5:
            self.temperature += 0.05  # 缓慢回升
        elif self.temperature > 37.5:
            self.temperature -= 0.1

        return events

    def status_line(self) -> str:
        hp_bar = "█" * int(self.hp_ratio * 8) + "░" * (8 - int(self.hp_ratio * 8))
        temp_state = self.temperature_state
        hallucination_mark = " [幻觉]" if self._hallucination_active else ""
        return (
            f"你(沈阳军区通讯团学员兵·党员){hallucination_mark}\n"
            f"  HP   [{hp_bar}] {self.hp:5.1f}/{self.MAX_HP:.0f}\n"
            f"  体温 {self.temperature:5.1f}°C [{temp_state}]\n"
            f"  饥饿 {self.hunger:5.1f}  口渴 {self.thirst:5.1f}\n"
            f"  辐射 {self.radiation:5.1f} mSv\n"
            f"  疲劳 {self.fatigue:5.1f}  精神 {self.morale:5.1f}"
        )

    def __repr__(self) -> str:
        return f"<Player {self.name!r} hp={self.hp:.1f} temp={self.temperature:.1f}>"

← 返回文件树 · 在 GitHub 上查看