粒度的边界:为什么没有阵型 Actor Where We Stop: Why There Is No Formation Actor
框架该做多深,以及那条线画在哪 · 2026-11-21How deep a framework should go, and where the line is drawn · 2026-11-21
系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →
先摆出需求 First, the Requirements
先说游戏里通常要什么。阵型:一个队按某个形状站好。整队移动:动的是队不是兵,每个人朝自己该去的位置走。变阵:从方阵变成楔形。编队:哪几个兵算一个队。还有队长,命令直接下给队,不用逐个下给兵。
Plenty of games want the same layer. Formations: a squad standing in a shape. Group movement: you move the squad, not the soldiers, and each unit walks to its own slot. Morphing: square into wedge. Squads: a record of which units belong together. And a leader you hand an order to, instead of addressing every soldier.
上面这一整套,MassBattle 里都没有现成的。没有队长 Actor,没有阵型生成器,也没有一个负责"把这几个 handle 编成一队"的对象。这篇讲两件事:线为什么画在这儿,该谁去跨。
MassBattle ships none of that as a ready-made piece: no leader actor, no formation spawner, no object whose job is to "group these handles into a squad". This post is about why the line is here, and whose job it is to cross it.
我们给的是零件,不是那台机器 We Ship Parts, Not the Machine
这些东西做不出来吗?零件全在,缺的只是有人把它们拼成玩法。下面这些都已经有了:
Nothing here is hard to build. The parts all exist; what is missing is someone assembling them into gameplay. Everything below is already there:
| 零件Part | 它做的事What it does |
|---|---|
SpawnAgents* 系列 |
按模板生成一批单位,直接返回 handle 数组,这就是"一队人"的起点Spawns a batch from a template and returns the handles. That array is the beginning of a squad |
AssignPositionsToAgents |
给它一组 handle、阵型中心、朝向、边界与分配模式,它算出每个人该站的那个点(只算位置,不移动)Given handles, a formation centre, a forward axis, a footprint and an assignment mode, it computes the point each unit should stand on — positions only, no movement |
| 生成阵型任务Formation tasks | Grid / Circular / Triangle 三种异步节点:算位置并把它变成移动目标,一次完成Three async nodes — Grid, Circular, Triangle — that compute the layout and turn it into move goals in one call |
| 检测函数Trace functions | 球 / 扇 / 盒 / 胶囊与它们的扫掠变体,统一收在一个 Trace Entity 节点里——调一次拿回一批 handle,不要求你先摆一个 trace 或 trigger ActorSphere / sector / box / capsule and their sweeps, unified behind one Trace Entity node — call once, get handles back, with no trace or trigger actor to place first |
AgentsMoveTo |
把"去这个点"交给每个单位自己走——避障、交通规则、碰撞都在这一层之下自动生效Hands "walk to this point" to each unit — avoidance, traffic rules and collision all apply underneath automatically |
把这几样接起来,就是一条完整的链:生成 → 算槽位 → 逐个移动。剩下一件事没替你决定:谁记住"这队人是哪几个",什么时候重算。这一块归玩法,零件管不到这儿。
Wire those together and the chain is complete: spawn, compute slots, move each unit. One thing is deliberately left out: who remembers which units make up this squad, and when to recompute. That piece is gameplay, and no parts list covers it.
为什么这条线画在这里 Why the Line Sits Here
队和队长属于高阶玩法,而每家游戏的玩法不一样。同样是"队":RTS 里它是玩家框选的产物,随手重编;战棋里是固定编制,死了不补;骑砍式的战场上,它跟着队长的士气走,队长一死整队溃散;塔防里压根没有队,只有路线。
Squads and leaders are high-level gameplay, and gameplay differs from game to game. "A squad" is not one thing: in an RTS it is whatever the player box-selected, regrouped on a whim; in a tactics game it is a fixed roster that never refills; on a Mount-and-Blade-style field it follows the leader's morale, and the squad breaks when he falls; in a tower defence there are no squads at all, only lanes.
框架一旦替你回答"队是什么",就得把上面那些差异全部回答一遍。那样做出来的东西是某一款特定游戏,撑不起很多款。所以我们只碰不管什么玩法都必须正确的那部分:谁站在哪、能不能走过去、路上会不会互相挤、两端算出来是不是同一份结果。
Once a framework decides what a squad is, it has to decide every one of those differences too, and what it ships is one particular game, not a base that can carry many. So the framework sticks to what must be correct whatever the game is: where a unit stands, whether it can walk there, whether it collides on the way, and whether both peers compute the same answer.
那一层长什么样:一个最小的队 What That Layer Looks Like: a Minimal Squad
把缺的那层写出来,一个 Actor 就够,逻辑五步:
Writing the missing layer takes one actor and five steps:
① 生成:调生成函数,拿回 handle 数组,存成这个 Actor 的成员。② 算槽位:给中心、朝向和形状,算出每个 handle 该去的点。③ 下发:把每个 handle 和它的目标点交给移动节点。④ 变阵:换一套形状参数重算,再下发一次。新的移动目标覆盖旧的,队伍就走成新阵。⑤ 拖动:Actor 的位置和朝向就是阵型中心和朝向,挪它整个阵跟着走;重算一遍槽位,队形也跟着转。
1 Spawn: call the spawn function and keep the returned handles on the actor. 2 Compute slots: from a centre, a forward axis and a shape, work out where each handle should stand. 3 Dispatch: hand every handle and its own goal point to the move node. 4 Morph: swap the shape parameters, recompute, dispatch again. The new goals replace the old ones and the squad walks into the new shape. 5 Drag: the actor's location and rotation are the formation centre and facing. Move it and the whole formation moves; one recompute turns the ranks with it.
就这些。不用订阅,不用注册表,也没有要实现的接口,"队"就是那个 handle 数组。这种东西跟一个 coding agent 聊几轮就能写出来,每个人写出来的版本正好符合自己那款游戏:有人要编队持久化,有人要队长死后自动重编,有人要阵型随地形变形。框架替他们定下任何一条,都会变成别人的麻烦。
That is the whole thing. No subscriptions, no registry, no interface to implement: the squad is the handle array. A few rounds with a coding agent will produce it, and each version comes out fitted to that person's game: one wants squads that persist, one wants automatic regrouping when the leader dies, one wants formations that deform with the terrain. Any of those decisions made by the framework becomes someone else's problem.
同一条原则,早就用在了生成和检测上 The Same Line, Already Drawn at Spawn and at Detection
这套粒度标准早就用过一次了,就在生成上。生成给的是一组函数,没有"生成器 Actor"这种东西:任何能跑函数的地方都能调它,自己的 Actor 里、GameMode 里、甚至根本没有 Actor 的场合。拿回来一组 handle,怎么管随你。
This way of drawing the line was already used at spawn time. Spawning ships as functions, not as a "spawner actor": you can call them anywhere a function runs, inside your own actor, in a GameMode, in a place where no actor is involved. You get handles back and manage them however you like.
要是当初做成"摆一个生成器 Actor 进场景,想生成什么就得在它身上配",粒度就太粗了:它摆在哪、活多久、归谁所有,你都得接着,连它关于多人和确定性的假设也得一起接受。函数没这些包袱,因为它不持有状态。同一把尺子量下来,阵型和队长也该待在函数这一侧:给能算出位置的函数,不给替你管队伍的 Actor。
Had it shipped as "place a spawner actor in the scene and configure it there", the granularity would be too coarse: you would inherit where it sits, how long it lives, who owns it, and its assumptions about networking and determinism. A function carries none of that, because it holds no state. The same ruler puts formations and leaders on the same side: the function that computes positions, not the actor that runs your squads.
检测也一样。我们给的是一组检测函数(球、扇、盒、胶囊,以及它们的扫掠变体,现在统一收在一个"Trace Entity"智能节点里),没有 trace Actor,更没有触发器 Actor,不需要你在场景里摆个东西,让所有检测都从它身上发出。函数的用法是:在你需要的那一刻,用你需要的形状调一次,拿回一批 handle,后面全归你的逻辑管,怎么排序、谁优先、这次不打下次再打、已经在打的目标要不要忽略。
Detection works the same way. What we ship is a set of trace functions — sphere, sector, box, capsule, plus their sweep variants, now gathered behind one "Trace Entity" node. There is no trace actor and no trigger actor, nothing you place in the scene and route every query through. A function means: call it at the moment you need, with the shape you need, get handles back, and the rest is your logic's business. How to sort them, who goes first, skip this one and hit it next tick, ignore the target already under attack.
检测这里还有一条更硬的理由:它跑在模拟里。查询走的是同一条确定性路径(网格分块、按 tick 结算),两个端对"这次谁在范围内"能给出同一份候选集。换成"摆一个触发 Actor、用引擎的 overlap 事件",这个就没了:引擎的重叠是帧驱动、按物理结算时序触发,"谁在范围内"由帧时序决定,两端可能给出不同答案,那是失步,跟粒度已经没关系了。所以这条线画在这里,是为了正确性。
Detection has a harder reason behind it: it runs inside the simulation. The query takes the same deterministic path (grid blocks, settled per tick), so two peers produce the same candidate set for "who is in range this time". "Place a trigger actor and use engine overlap events" loses that: engine overlaps are frame-driven and fired in physics order, so "who is in range" depends on frame timing, peers can disagree, and that is a desync, no longer a granularity argument. The line here is about correctness.
这是 ECS,用法也该面向数据 It Is an ECS; Use It Data-Oriented
还有一层原因:MassBattle 是 ECS,一个单位就是一组片段加一个 handle。阵型层要是做成"必须继承某个 FormationActor 才能用",就等于把对象模型又压回数据上面,而 ECS 的好处来自不这么做。
One more reason: MassBattle is an ECS, and a unit is a handle plus a set of fragments. A formation layer that requires you to "inherit from this FormationActor" puts the object model back on top of the data, and the ECS advantage exists because we do not do that.
所以这一层的接口形状是数组进、数组出:给一组 handle、一组目标点,拿回结果。队伍、编队、队长这些概念,在你的游戏里住在哪儿都行:Actor、组件、GameMode、存档数据、服务器下发的表,因为那都是你这边的数据。框架只要求你最后把 handle 和目标点喂给它。
So the interface is shaped as arrays in, arrays out: give it handles and goals, get results. Squads, platoons, leaders can live wherever your game wants them: an actor, a component, a GameMode, save data, a table the server hands down. All of it is your data. The framework only asks that you eventually feed it handles and goal points.
代价要说清:这不是开箱即用 The Cost, Stated Plainly
线画在这里,代价实实在在:"加个阵型"不是一个勾选项。那个 Actor 你得自己写,还得自己定:队伍什么时候重编,变阵时正在走的单位怎么办,队长死了要不要重组。买一个把这些都替你决定好的产品,确实省事,前提是它的决定跟你的决定一样。
The cost of drawing the line here is real: "add a formation" is not a checkbox. You write that actor, and you decide when squads regroup, what happens to units still walking during a morph, and whether the squad re-forms when the leader dies. Buying a product that decides all of it for you is less work, as long as its decisions happen to be yours.
所以判据只有一条:这件事做错会让模拟变得不正确吗?会的话(移动积分、避障、索敌、伤害、锁步一致、槽位与碰撞的一致性),框架做,而且要做快、做确定。不会的话(谁和谁一队、站成什么形状、谁来下令、输了怎么处理),留给你,因为那本来就是你的游戏。
So there is one test: would getting this wrong make the simulation incorrect? If yes, the framework does it, and has to do it fast and deterministically: movement integration, avoidance, targeting, damage, lockstep agreement, slot and collision consistency. If no, it stays with you: who belongs with whom, what shape they stand in, who gives the order, what happens after a loss. That part is your game.
这条线不会一次画死,它会跟着真实需求移动。真碰到"每个游戏都要、做错了代价也一样大"的东西,就该把它沉进框架。沉下来的标准是普适且关系到正确性,用的人多不算数。
The line is not fixed once; it moves with real demand. When something really is needed by every game and equally costly to get wrong, it should sink into the framework. The test for sinking is whether it is universal and correctness-critical. Popularity is not the test.