Mass Battle Frameworkv1.20.0

一个 Agent = 车体 + 炮塔 An Agent Is a Chassis and a Turret

ENTITY MODEL

关于我们怎么看待一个 Agent · 2026-11-21How we read an agent · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

把"一个单位"当成"一个人形",是很自然的选择,很多游戏也这么干:坦克、弓箭手、骑兵各写一套,看着也直观。代价是每多一种形态,移动、转向、索敌、攻击就得再写一遍。我们选了另一条路,更通用的那条。

Treating "one unit" as "one humanoid" is a natural choice, and plenty of games do it: tanks, archers and cavalry each get their own system, which is easy to picture. The cost is that each new shape needs movement, turning, targeting and attack written all over again. We took the other road, the general one.

我们的读法反过来,也更通用:一个 Agent 是一个车体,加一个炮塔。车体负责跑、负责朝向,炮塔负责指、负责打。两者可以是一具身体上的两段,也可以是两个独立实体。至于"长什么样",只是这条线画出来的不同样子。

We read it the other way round, and it generalises: an agent is a chassis plus a turret. The chassis runs and decides which way the unit faces. The turret points and fires. The two can be sections of one body or two separate entities. What the unit "looks like" is just that line drawn differently.

从坦克说起 Start With the Tank

坦克是最没争议的例子:车体在地上跑,炮塔在车顶转。炮塔有两个自由度:偏航绕垂直轴,仰角绕横轴。转多快、能转到哪、什么时候算"到位",是三个独立的问题。

The tank is the uncontroversial case: a hull that drives and a turret on top that turns. The turret has two degrees of freedom, yaw about the vertical and pitch about the lateral. How fast it turns, how far it can turn, and when it counts as "on target" are three separate questions.

弓箭手其实是同一个结构 The Archer Is the Same Structure

弓箭手看着是连成一块的,拆开和坦克一模一样:双腿是车体,决定往哪跑、朝哪站;腰以上是炮塔,决定往哪指。差别只在材质。

The archer looks like one piece. Take it apart and it is the same thing: the legs are the chassis, deciding where it runs and which way it stands; everything above the waist is the turret, deciding where the shot points. Only the material differs.

而且弓箭手也有仰角。抛射的时候整个上身往后仰,腰就是那根横轴;目标越远抬得越高,近了就压下来。这是炮塔的两轴问题,只是藏在骨骼里。

The archer has pitch too. An arcing shot tips the whole upper body back, with the waist as that lateral axis. Distant targets need a higher angle, close ones a flatter one. It is the same two-axis turret problem, hidden inside a skeleton.

所以框架里的瞄准关节从来不是坦克专用。它按每根骨配置:挑一根骨当支点,选哪些骨跟着转(蒙版,也就是那根骨连同它的子树),每根轴各自的限位、速度、加速度与到位容差。只挑一根骨,坦克就是一个单骨关节;挑腰以上的子树,弓箭手用的也是同一套。

The aim joint in the framework was never meant for tanks alone. It is configured per bone: pick the pivot bone, pick which bones follow it (a mask, meaning that bone and its subtree), then give each axis its own limits, speed, acceleration and on-target tolerance. Pick one bone and the tank is a single-bone joint. Pick the subtree above the waist and the archer is the same setup.

骑手:一个人,两个身体部位 The Rider: One Body, Two Sections

骑手最能说明这个模型值在哪。人的下半身加上马,是车体;人的上半身,是炮塔。马负责位移和朝向,上身负责指哪打哪。没人会把这种组合叫"车体和炮塔",套进来却严丝合缝。

The rider is where the model earns its keep. The rider's lower body and the horse are the chassis; the rider's upper body is the turret. The horse carries position and facing. The torso decides where the weapon points. Nobody would call that combination "hull and turret", and it fits exactly.

建筑为什么也是 Agent Why a Building Is an Agent Too

有人会问:一栋建筑为什么要当成 Agent,不能就当个"死"的摆设?不用讲道理,去看 RTS 里那几栋最出名的建筑就行。每一栋都有某个时刻"不像建筑"。

Why treat a building as an agent and not as dead scenery? No abstract argument needed. Look at the best-known RTS buildings: every one of them stops behaving like a building at some point.

例子Example它在什么时候不像建筑When it stops being a building说明什么What it proves
星际人族:指挥中心、兵营、重工厂、星港StarCraft Terrans: command centre, barracks, factory, starport 能整体升空,飘到别处再落地;升空期间不能生产,落地恢复生产Lift off whole, drift elsewhere, land again; while airborne they cannot produce, and production resumes on landing "移动"与"生产"是两个可开可关的能力,不是两种建筑Moving and producing are two switches, not two kinds of building
魔兽暗夜精灵:战争古树Warcraft night elves: the Ancient of War 坐地时是建筑、能生产;拔根站起来能走、能打,还能啃树回血;坐下又变回建筑Seated it is a building that produces; uprooted it walks, fights, and can eat a tree to heal; seated again it is a building once more 同一个东西在两个状态之间来回切换,能力一直在,只是开或关One object toggles between two states; the abilities never leave, they are switched on or off

看这两例的共同点:结构上一件新东西都没多要。生命值、护甲、朝向、移动、攻击、受击、生产、动画切换,全和"单位"是同一套。差别只在当前开了哪几个开关。

What the two cases have in common: structurally they ask for nothing new. Health, armour, facing, movement, attack, hit reaction, production, animation switching, the whole set is the one a "unit" already uses. The only difference is which switches are currently on.

所以框架里没有"建筑"这种实体类型。一栋建筑就是一个暂时不动的 Agent:功能都在,只是没开。哪天要它站起来走,打开就能走;不打仗的时候,它照样生产。真单独做一个建筑类型,这几栋建筑马上就会要求你把移动、攻击、受击一样样搬过去。搬到最后,你做的还是 Agent,只是换了个名字,功能还少一半。

There is no "building" entity type in the framework. A building is an agent that does not move for now: everything is present, only switched off. The day it has to stand up and walk, switch it on and it walks; while it is not fighting, it keeps producing. Make a separate building type and these same buildings will immediately ask you to port movement, attack and hit reaction over one by one. You end up with an agent under another name, minus half the features.

两条边界 Two Boundaries

这个模型有两条边界。第一,底盘有朝向,但不一定有"前向":横着平移的履带车、边飞边转的直升机都装得下,别把"车体"读成"必须朝车头跑"。第二,多炮塔(战列舰那种)要靠"炮塔上再挂一个 Agent"来表达,而每多挂一层就是一个完整的 Agent、一份完整的成本。能表达,不代表能无限套下去;层级越深越难配,也越贵。

Two boundaries belong to this model. First, the chassis has a heading but not necessarily a forward: a tracked vehicle that strafes and a helicopter that turns while flying both fit, so do not read "chassis" as "must drive nose-first". Second, multiple turrets, a battleship for example, are expressed by hanging another agent on the turret. Every extra level is a complete agent with its full cost. Being expressible does not make it worth nesting without limit; deeper levels are harder to configure and costlier.

再往前一步:炮塔上挂一个 Agent One Step Further: an Agent on the Turret

既然 Agent = 车体 + 炮塔,那炮塔上能不能再挂一个 Agent?能。坦克顶上那挺自动机枪就是一个独立的 Agent:自己的一根关节,自己的索敌,自己的开火节奏。它挂在坦克身上,但它不算坦克的一部分。

If an agent is a chassis plus a turret, can a turret carry another agent? It can. The pintle-mounted machine gun on a tank is an agent in its own right: its own joint, its own target search, its own firing rhythm. It rides on the tank, but it is not part of the tank.

这里要设计的是数据往哪流,几何挂在哪儿倒在其次。子体可以继承父体的东西:同一个索敌查询、同一个队伍、同一套伤害参数,在出生时复制一次,或者每帧镜像。方向也能反过来:机枪先看见的目标,可以交给坦克去追。代码里这个方向的枚举名就叫"底盘配合炮塔",子体发现的目标成为父体的候选。

The design question is which way the data flows; where the geometry hangs comes second. A child can inherit from its parent: the same target query, the same team, the same damage parameters, either copied once at birth or mirrored every tick. The direction can reverse too: a target the machine gun spotted first can be handed to the tank to chase. In code the enum value for this is named after the idea, the chassis serving its turret: the child's target becomes a candidate for the parent.

于是"自动机枪塔"不必是一个新类型。它就是一个 Agent,挂在另一个 Agent 上,每个通道配好继承和回馈的方向。

So the "automatic gun turret" does not need to be a new type. It is an agent hung on another agent, with inheritance and feedback directions set per channel.

骑兵:一个模型还是两个 Agent Cavalry: One Model or Two Agents

同一个模型会把骑兵推到两种做法上,两种都对。选哪一种,看你的玩法要多少精细度,又愿意为它付多少性能。

The same model leads cavalry to two implementations, and both are right. Which one you want depends on how much fidelity the gameplay needs and how much performance you will pay.

做法Approach适合什么样的玩法Fits代价与收益Trade-off
一个模型,一个 AgentOne model, one agent 全战那种规模:骑兵是一个单位,人和马不分开,不存在"下马继续打"Total-War scale: a trooper is one unit, rider and horse never separate, dismounting is not a thing 最省:一个单位一套骨骼、一次渲染、一份模拟。看起来像人骑在马上,实际是一个半人马Cheapest: one unit, one skeleton, one draw, one simulation. It reads as a rider on a horse; in fact it is one centaur
两个 Agent,一个跑、一个打Two agents: one drives, one fights 骑砍那种精细度:掉下马立刻能跑能打,马也能自己跑、还能抬腿踢人Mount & Blade fidelity: knocked off the horse you can immediately run and fight, and the horse can run on its own or kick 贵:两个全属性 Agent、两套骨骼、两份模拟;换来的是分离的视觉与丰富得多的行为Costlier: two full-capability agents, two skeletons, two simulations — in exchange for separable visuals and far richer behaviour

第二种做法里,两个都是完整的 Agent,谁也不是谁的"配件":骑手被马带着走时,照样能自己打;马被骑着的时候,只是在履行"被骑"这条关系,人一掉下来,它自己的跑动和攻击照旧可用。骑上去就是建立了一条关系,东西本身没变。所以我们说驾驶是一种对状态的权威,不是父子关系。

In the second approach both are complete agents, and neither is an accessory. While the horse carries him, the rider can still fight. While it is ridden, the horse is only honouring a relationship; the moment the rider is gone, its own locomotion and attack are still there. Mounting establishes a relationship and leaves both things as they were. That is why we treat driving as an authority over state, not as parenting.

两样都要,性能不答应:精细度越高,同屏能跑的单位越少。这个问题没有统一答案,只有你这个 Game Mode 的答案。框架能做的是两条路都留着位置,让你按玩法选,不用开工前就定死。

You cannot have both. More fidelity means fewer units on screen. There is no universal answer, only the answer for your game mode. What the framework owes you is room for both, so you pick per gameplay instead of locking it in before you start.

为什么不把片段切成 trait Why the Fragment Set Is Not Sliced into Traits

把 Agent 的片段切成几组,每组打包成一个 trait;想要哪种能力就加哪个。加完,查询自然只收到带这个包的单位,处理器也就"收到"了这项能力。ECS 里很常见这种组合方式,"能力"这个词放在 trait 上也很自然,一个 trait 就相当于对象世界里的一个 component。我们没走这条路。

Slice an agent's fragments into groups and wrap each group in a trait. Add the trait for the capability you want, and the queries see only the entities carrying that bundle, so the processors pick the ability up for free. It is a common way to compose things in an ECS, and the most natural home for the word "capability": a trait is a component in the object-world sense. We do not do it.

这是权衡之后的选择。三个因素得放在一起看才成立:

The decision came after weighing them. The three factors only make sense together:

这一笔The accounttrait 换来什么What a trait buys你要付什么What you pay
学习曲线Learning curve 按能力增删,一个 Agent 可以只带它用得上的东西Add and remove by capability; an agent carries only what it uses "能加什么"成了要另学的一套:哪些包能一起加,哪些互斥,谁是谁的前提(炮塔不能没有车体)。现在没有这一层——所有 Agent 的片段集合相同,你只需要关心值,不必先判断组合合不合法"What may I add" becomes a subject of its own: which bundles combine, which exclude each other, which presuppose another (a turret cannot exist without a chassis). Today there is no such layer — every agent carries the same fragment set, so you reason about values, never about whether a combination is legal
代码Code 片段可以缺席,省下来的内存是真的Fragments can be absent, and the saved memory is real 每一处读它的地方都得先问"它在不在"。这一条把编译期的保证挪到了运行期:现在哪个处理器会碰到这个实体、GetFragmentDataPtr 返回什么,都是结构上确定的;可缺席之后,得到的是空指针而不是编译错误。而且这些检查落在最热的那几趟循环里,要钱Every place that reads it must first ask whether it is there. This moves a compile-time guarantee into run time: today which processors can see an entity, and what GetFragmentDataPtr returns, are settled by construction; once fragments may be absent, the failure mode becomes a null pointer instead of a compile error. And those checks land inside the hottest loops, so they cost
尺寸Size 单位变小,块里能塞更多实体A smaller unit, so more entities per chunk 砍掉的往往是不在热路径上的那些列;一次遍历真正要碰的列(位置、速度、移动状态、生命)每个 Agent 都有。块变小换来的是密度,不是"每个实体少干活",所以提升有限What gets cut tends to be the columns the hot path never touches, while the ones a pass actually reads — position, velocity, movement state, health — are carried by every agent. A smaller chunk buys density, not less work per entity, so the gain stays modest

建筑就是个现成的反例:它不动,却带着一整套移动片段,每次遍历都跟着走一遍。按 trait 的思路,它是最该砍掉的那一份。我们照样认了:砍掉它省下的那点密度,补不上全流程里多出来的那批"有没有这个片段"的判断。

A building is the obvious counter-example: it never moves, yet it carries a full set of movement fragments and gets walked with everything else. Under trait logic it is the first share to cut. We accept it anyway: the density that cut buys does not cover the extra "does this one have that fragment" tests it would add to every path.

这份代价落在资产面板上,补偿也就放在面板上。AgentConfig 里每个作者化的片段都有一个可见性开关(Agent Misc | VisToggles,按片段自己的分类分组,声明顺序和上面那些片段一致,那份开关列表读起来就是这份资产的目录),用不上的取消勾选,它就从 Details 里消失。这批开关只控制「画不画这一行」:每个开关在代码里只出现在该片段自己的 EditCondition 上,没有运行时分支读它们,勾不勾都不改模拟。bHideRuntimeFragments 只是把「缓存运行时数据」那一组默认不显示(默认 true),同样是个显示默认值。于是那栋不动的建筑可以把整套移动参数一并藏起来,调参时不用在它们中间翻。

That cost shows up on the asset panel, so the compensation lives there too. Every authored fragment gets a visibility switch in the AgentConfig (Agent Misc | VisToggles, grouped by the fragment's own category and declared in the order of the fragments above, so the switch list reads as a table of contents for the asset). Untick what you do not use and it leaves the Details panel. These switches only decide "is this row drawn": in code each one appears only as an EditCondition on its own fragment's property, no runtime branch reads them, and ticking makes no difference to the simulation. bHideRuntimeFragments just defaults the "runtime cache" group to hidden (true), another display default. So the building that never moves can hide the whole set of movement parameters and leave a shorter list to tune.

说清它改了什么、没改什么:片段还在,还在被遍历,运行时一个字节都没省。它只是让面板少显示几行,管的是操作舒适度,跟性能无关。这也是这套设计的取向:运行时保持同质,面板上的差异交给开关去解决。别为了面板好看去改数据的形状,那是拿运行时换编辑器。

Be precise about what it changes and what it does not: the fragment is still there, still walked, and not one byte is saved at run time. It only stops a few rows from being drawn. That is ergonomics, not performance. The design takes the same position throughout: keep run time homogeneous and let the switches absorb the difference in the panel. Do not reshape the data to make the panel look better; that trades run time for the editor.

组合并不是没有,只是落在另一个粒度上:投射物、掉落、障碍本来就走不同的原型,各带各的一套。"这是什么东西"在出生时就定了;"今天具备哪些能力"不能随时改。前者决定原型,后者只能靠可选性去撑,而可选性这个代价我们不打算付。

Composition is not missing; it sits at another granularity. Projectiles, loot and obstacles already travel on their own archetypes, each with its own set. "What kind of thing this is" is settled at spawn; "which capabilities it happens to have today" cannot be changed on the fly. The first decides the archetype. The second can only come from optionality, and optionality is the cost we decline to pay.

也不是永远不做。门槛是有的,而且可以量:能砍掉相当一部分尺寸,同时测出 25% 以上的提升,两条一起成立才值得开工。在那之前,Agent 的片段集合保持同质:每个 Agent 都带完整的一套,用不上的就闲着,处理器和查询也不用为"缺了什么"分支。有了这个前提,处理器才能切得这么粗,一趟查询覆盖所有单位。

It is not a permanent no. There is a gate, and the gate can be measured: cut a substantial share of the footprint and measure a gain above 25%, both at once, and only then is it worth starting. Until then an agent's fragment set stays homogeneous. Every agent carries the full set, the unused parts sit idle, and neither processors nor queries branch on what is missing. That is what lets us cut processors this coarsely and cover every agent in one query.

所以,"一个 Agent"是什么 So What Is an Agent

一个能跑、能转向、能指哪打哪的载体。它不一定是人形,也不一定是一个模型。车体和炮塔是它的两侧;附着关系决定它和谁共享这份能力,继承与回馈的方向决定数据往哪流。看起来是坦克、弓箭手、骑手还是半人马,那是内容,跟模型无关。

A carrier that runs, turns, and points at what it attacks. It is not necessarily humanoid, and not necessarily a model. Chassis and turret are its two sides. Attachment decides who shares the capability, and the direction of inheritance and feedback decides where the data flows. Whether it reads as a tank, an archer, a rider or a centaur is content, not model.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels