能力、行为、反应:三层与一条顺序 Capability, Behaviour, Reaction: Three Layers and One Order
Agent 的 AI 是怎么建模的 · 2026-11-21How an agent's AI is modelled · 2026-11-21
系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →
先分清三件事 Separate Three Things First
给单位写 AI,最容易写成一团的就是这里:一个状态里既有"我现在在干什么",又有"我能干什么",还夹着"我被打了怎么办"。三件事挤在一起,改一处塌一片。我们把它们分成三层,每层各有一套规矩。
AI for units turns into a mess the moment the current state, the list of what the unit can do, and what happens when it gets hit all sit in one place. Touch one and the others fall over. So we keep them in three layers, each with its own rules.
能力层:能做,但不会自己去做 Capability: It Can, Without Doing
能力层回答"这个单位会什么":会出生、会攻击、会受击、会死、会被选中、会掉战利品、会索敌,还会把关节瞄到目标上(Aim)、让头和眼跟着目标走(Look)。这些是一组被动的东西:参数配好,这项本领就在,它自己一个字都不会主动说。
The capability layer answers one question: what can this unit do? Appear, attack, take a hit, die, be selected, drop loot, find a target, aim the joint at that target (Aim), track it with head and eyes (Look). These are passive pieces. Configure the parameters and the ability is there; on its own it never speaks.
这样切的好处是能力能到处复用:同一套攻击能力,追逐的行为调它,"反击"也调它,一个从没打算打架的单位同样能借用。它不知道调用者是谁,也不知道为什么调用。瞄准与看向走同一条路:行为用自己的开关(bCanAim / bCanLook)去请求,攻击回合开头先把关节转上目标,一个正在跳的单位也能同时保持看向同一个目标。
The split pays off in reuse. The same attack capability gets called by a chase, by a retaliation, and by some unit you never expected to fight. It has no idea who called it or why. Aim and look work the same way: a behaviour asks for them through its own switches (bCanAim / bCanLook). An attack round starts by turning the joint onto the target; a unit in mid-jump can keep looking at the same target while it flies.
行为层:互斥,主动 Behaviour: Exclusive and Active
行为层回答"它现在决定干什么"。规矩只有一条,而且很硬:同一时间只有一个行为。待机、出生、休眠、巡逻、追逐、攻击、支援、跳跃,八种状态互斥,任意时刻只有一个在生效。行为结束时按配置跳到下一个:追进射程就转攻击,跳跃这种一次性动作落地后也按配置切走。瞄准本来也是这一层的一种状态,后来被我们收回能力层了。现在对齐只是攻击回合里的第一个阶段:回合先等瞄准服务把关节转上目标,再开火;超时就照常开火,冷却照吃。
The behaviour layer answers what the unit has decided to do. There is one hard rule: one behaviour at a time. Idle, appear, sleep, patrol, chase, attack, reinforce, jump: eight states, mutually exclusive, exactly one of them live. When a behaviour ends it moves to its configured next one — a chase switches to attack once the target is in range, and a one-shot like jump hands over on landing. Aim used to be a state here too. We pulled it back into the capability layer, so aligning is now the first phase inside an attack round: the round waits for the aim service to bring the joint on target, then opens; on timeout it opens anyway and pays the cooldown.
为什么必须互斥?因为几个行为抢的是同一批东西:体位移、朝向、动画主轨、攻击回合。两个行为一起跑,就是两套意图在抢同一双手,屏幕上看到的就是抖、抽搐、动作打断又重来。
Why exclusive? Because behaviours fight over the same handful of things: body translation, facing, the main animation track, the attack round. Run two at once and they pull in different directions. What you see is jitter, twitching, a swing that restarts halfway.
行为跑起来的时候调用能力(这一回合打几发、播哪一段攻击动画、要不要请求瞄准和看向),同时读属性(移动速度、血量、瞄准限位)。这条线要看清:选哪个能力、什么时候用,是行为的事;能力只管执行。
While it runs, a behaviour calls capabilities — how many shots this round, which clip plays, whether to ask for aim and look — and reads attributes such as move speed, health and aim limits. Keep that line clear: the behaviour picks which capability and when, and the capability only executes.
反应层:被动、可并存、可打断 Reaction: Passive, Coexisting, Interrupting
反应层回答"外界对它做了什么"。它和另外两层的关系完全是另一回事:它从不主动执行,没有"我现在进入受击反应"这种决定。挨了一发、被选中、被打死,是事件把反应推起来的。
The reaction layer answers what was done to it, and it relates to the other two differently. It is never a decision. Nobody enters "now I am in a hit reaction". Events drive it: took a hit, got selected, died.
被动带来一个好处,是它可以和别的东西并存:一个单位正在攻击,同时挨了一发,攻击行为还在,受击反应也一起播。两边不冲突,因为它们不在同一层上抢:一个在决定要做什么,一个在报告发生了什么事。
Being passive is also what lets it coexist with everything else. A unit is attacking and takes a hit; the attack behaviour is still running and the hit reaction plays on top. Nothing conflicts, because the two are not competing on the same level: one decides what to do, the other reports what happened.
它也能打断,但路径是列得出来、写得死的,只有两条:死亡把所有东西收掉(没有任何行为能在死后继续),以及睡眠中被打醒(一条专门的转移,醒来去哪由作者配)。除了这两条,受击本身不会打断攻击:"一记重击让挥砍踉跄中断"现在没有,想要那种手感得再加一条转移。反应也可以叠加,同一时刻好几个反应各自在自己的通道上生效,比如受击外观和选中状态。给新东西归类时用这条判据:能被"想要"驱动的是行为,只能被"发生"驱动的是反应。
It can also interrupt, but only along paths that actually exist, and there are two. Death takes everything away; no behaviour survives it. The other is a hit that wakes a sleeping agent, a dedicated transition whose destination is authored. Past those, a hit does not cancel an attack. "A heavy blow staggers the swing" is not current behaviour; that feel needs one more transition. Reactions can also stack: several live at once, each on its own channel, hit visuals and selection state for example. When you have to classify something new, the test is practical: what can be wanted is a behaviour; what can only happen is a reaction.
三层这架构本身允许加转移,"受击不打断攻击"只是我们选的默认策略。理由是攻防节奏:攻击回合一旦能随时被打断,双方都会在半途收手,出手变成赌博;而"打断又重来"就是上面那种抽搐。真想要"重击踉跄"的硬派手感,加的是多一条转移,三层怎么划分不用动,换一种手感也不必重画结构。
The three-layer architecture allows more transitions; "a hit does not cancel an attack" is the default policy we chose. The reason is combat rhythm. Let an attack be cancelled at any moment and both sides bail halfway, so committing to a swing becomes a gamble, and interrupted-then-restarted is exactly the twitching above. If you want the heavier feel of a staggering blow, you add one more transition; the three-layer split stays as it is. Changing the feel without redrawing the structure is what the split is for.
"想要还是发生"之外还有第二条判据,来自一帧里的顺序:谁读一个值,就得排在写它的人后面。数据要跨过阶段边界才对下一层可见,所以消费者排在生产者前面,读到的永远是上一帧的值。拿死亡试一遍。死亡判定读血量,血量只有反应波在写;把死亡做成行为,它就会跑在写血那趟波的前面。这一帧落下的杀招它下一帧才看见,而尸体在这一帧里照常追击、照常出手。语义判据说死亡只能被"发生"驱动,顺序判据说它得排在写血的波之后,两条路通向同一个答案:死亡留在反应层。
There is a second test beyond wanted-versus-happened, and it comes from the order inside a tick: whoever reads a value runs after whoever writes it. Data only crosses a stage boundary, so a consumer placed ahead of its producer always reads last frame. Take death through it. The death check reads health, and only the reaction wave writes health. If death were a behaviour it would run before the wave that writes health, so a killing blow landed this tick would only be seen next tick, and the corpse would keep chasing and shooting for one more frame. The semantic test says death can only happen; the ordering test says it sits after the wave that writes health. Two routes, one answer: death lives in the reaction layer.
一帧里的顺序 The Order Inside a Tick
三层排成一串,在一帧里按固定顺序往下走,每一层把结果交给下一层。顺序写错,上层读到的就是上一帧的旧结论,画面上看就是慢半拍。
The three layers run as a chain in a fixed order inside the tick, each handing its result to the next. Get the order wrong and a layer reads last frame's conclusion, and everything on screen reacts one beat late.
| 阶段Stage | 它在回答什么What it answers | 产出Produces |
|---|---|---|
| 感知 PerceptionPerception | 我看见了谁?(目前只做视觉:一次扇形索敌)Who do I see? (vision only today: one sector search) | 当前目标与其位置、速度、血量The current target with its position, velocity and health |
| 决策 DecisionDecision | 我要做什么?What will I do? | 一个激活行为 + 它的进入时间One active behaviour and when it started |
| 反馈 ReactionReaction | 外界对我做了什么?What was done to me? | 受击/选中/死亡等反应通道的当前状态State on the hit / selection / death channels |
| 运动 MovementMovement | 我下一步在哪、朝哪?Where am I next, facing which way? | 位置、朝向、速度;碰撞与分离Position, facing, velocity; collision and separation |
| 表现 RenderRender | 这一帧该画成什么样?What does this frame look like? | 动画帧、材质参数、特效、血条与跳字Animation frames, material parameters, FX, health bars, text pops |
后两层只读决策,不回写。运动把"要做什么"变成"在哪里",表现把"在哪里"变成"看起来怎样",两者都不能反过来改行为。上一篇文章里那条 CPU/GPU 分界线是同一个思路:每层只管一件事,层与层之间只传结论。
The last two stages read the decision and never write it back. Movement turns "what to do" into "where"; rendering turns "where" into "how it looks". Neither may reach back into behaviour. That is the same idea as the CPU/GPU line in the previous post: every layer owns one job, and layers exchange conclusions only.
新需求该放哪一层 Where a New Requirement Belongs
有了这三层,加功能之前先问四句话,放哪一层基本不会错。它是"能做到的事"吗?能力。它是"决定要去做的事"吗?行为,而且要接受互斥。它是对外部事件的被动响应吗?反应,而且要接受它和别的东西同时存在。它读的数据是谁写的?那就排在写入者后面。排反了,读到的永远是上一帧,因果链慢一拍。
With the three layers in place, four questions place almost any new feature. Is it something it can do? A capability. Is it something it decides to do? A behaviour, and it accepts exclusivity. Is it a passive response to an outside event? A reaction, and it accepts coexisting with everything else. Who writes the data it reads? Then it runs after that writer; put it before and it reads last frame, and the causal chain slips a beat.
最糟的情况是"一个状态里既做决定又播反应":互斥和并存这两套规则被塞进同一个开关,之后每加一种情况,都在同一个 if 里多一个分支。多带一层我们认,这条路不走。
The worst case is a state that both decides and reacts. It folds exclusivity and coexistence into one switch, and every new case becomes another branch in the same condition. We will carry an extra layer; we are not walking that road.