游戏 AI / 系统课时

让目标选择拥有解释记录

让目标选择拥有解释记录

当 NPC 突然从玩家 A 转向玩家 B,仅知道最终目标还不够。真正可调试的系统需要留下当时有哪些候选、每个候选得了多少分、旧目标为何没有继续保留,以及最终由哪条规则触发切换。

学习目标

  • 设计能够复盘一次目标选择过程的解释记录
  • 区分候选评分、切换判定和最终原因,避免只记录结果
  • 用稳定的数据结构把战斗异常转化为可查询证据

记录的不是目标,而是一次决策

最简单的实现通常只有 currentTargetId。它能回答“现在打谁”,却回答不了“为什么换成他”。当策划报告 NPC 频繁换目标时,如果日志只有目标 ID,开发者只能重新跑场景并猜测当时的距离、伤害和可见性。

解释记录应该以一次决策为单位保存快照,至少包含三类信息:候选集合及其分数、切换前后的目标,以及触发或阻止切换的规则。这样即使现场已经过去,也能还原当时的判断依据。

目标选择解释记录从候选评分到切换原因的组成关系

一条记录可以设计为下面这样:

python
from dataclasses import dataclass, asdict
from typing import Optional

@dataclass
class CandidateScore:
    target_id: str
    score: float
    distance: float
    visible: bool

@dataclass
class SelectionRecord:
    previous_target: Optional[str]
    selected_target: Optional[str]
    reason: str
    candidates: list[CandidateScore]


def select_target(previous_target: Optional[str], candidates: list[CandidateScore], switch_margin: float) -> SelectionRecord:
    # 固定使用 target_id 作为第二排序键,避免同分时结果随容器遍历顺序变化。
    ranked = sorted(candidates, key=lambda c: (-c.score, c.target_id))
    if not ranked:
        return SelectionRecord(previous_target, None, 'NO_VALID_CANDIDATE', [])

    best = ranked[0]
    current = next((c for c in ranked if c.target_id == previous_target), None)

    if current is None:
        reason = 'CURRENT_TARGET_INVALID' if previous_target else 'INITIAL_SELECTION'
        return SelectionRecord(previous_target, best.target_id, reason, ranked)

    # 新目标必须明显优于旧目标,否则保持当前目标,降低来回切换。
    if best.target_id != current.target_id and best.score >= current.score + switch_margin:
        return SelectionRecord(previous_target, best.target_id, 'BETTER_TARGET_EXCEEDS_MARGIN', ranked)

    return SelectionRecord(previous_target, current.target_id, 'KEEP_CURRENT_TARGET', ranked)


candidates = [
    CandidateScore('player-a', 72.0, 8.0, True),
    CandidateScore('player-b', 86.0, 11.0, True),
    CandidateScore('player-c', 51.0, 5.0, False),
]

record = select_target('player-a', candidates, switch_margin=10.0)
print(asdict(record))

预期输出:

text
{'previous_target': 'player-a', 'selected_target': 'player-b', 'reason': 'BETTER_TARGET_EXCEEDS_MARGIN', 'candidates': [{'target_id': 'player-b', 'score': 86.0, 'distance': 11.0, 'visible': True}, {'target_id': 'player-a', 'score': 72.0, 'distance': 8.0, 'visible': True}, {'target_id': 'player-c', 'score': 51.0, 'distance': 5.0, 'visible': False}]}

这里真正有价值的不是 selected_target,而是能同时看到旧目标得分 72、新目标得分 86,以及系统因为超过 10 分切换阈值而换人。

原因码要对应真实规则

不要把所有情况都写成 TARGET_CHANGED。原因码应该直接映射到决策规则,例如 INITIAL_SELECTION、CURRENT_TARGET_INVALID、BETTER_TARGET_EXCEEDS_MARGIN 和 KEEP_CURRENT_TARGET。这样统计日志时,可以直接回答“换目标主要因为旧目标失效,还是因为评分超过阈值”。

同样重要的是记录“没有切换”的决定。假设 B 比当前目标 A 高 3 分,但切换阈值要求至少高 10 分,那么保留 A 本身也是一次有效决策。如果只记录成功切换,就无法验证防抖规则是否真的在工作。

让记录能够支持复盘

一条可用的解释记录还应带上决策发生时间、NPC 标识和必要的上下文版本,例如当前行为状态或评分规则版本。否则规则调整后再看旧日志,很容易拿新公式解释旧数据。

日志内容也不要依赖随后还会变化的对象引用。候选分数、距离、可见性等值应该在决策发生时复制成快照;如果只保存对运行时目标对象的引用,等到查看日志时,对象状态可能已经完全不同。

实际排查时可以先按 NPC 找到异常切换时刻,再查看 previous_target、selected_target 和 reason,最后展开候选快照确认分数是否符合预期。这样“NPC 为什么突然换人”就从难以复现的战斗现象,变成一条可以直接核对规则和数据的决策记录。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

让目标选择拥有解释记录

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通